当前位置:网站首页>牛顿插值法
牛顿插值法
2022-07-06 04:16:00 【Python ml】
#include <vector>
#include <iostream>
using namespace std;
void print(vector<double> s)
{
for(double x:s)
{
cout<<x<<" ";
}
cout<<endl;
}
vector<double> mul(vector<double> a,vector<double> b) //多项式乘法
{
int n=a.size(), m=b.size();
vector<double> s(n+m-1,0);
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
s[i+j]+=a[i]*b[j];
}
}
return s;
}
vector<double> add(vector<double> a,vector<double> b)
{
int n=a.size(), m=b.size();
vector<double> s(max(n,m),0);
for(int i=0;i<n;++i){
s[i]+=a[i];
}
for(int i=0;i<m;++i){
s[i]+=b[i];
}
return s;
}
void print_chashang_table(vector<vector<double>> A)
{
int n=A.size(), m=A[0].size();
for(int i=0;i<n;++i)
{
for(int j=0;j<=i;++j)
{
printf("% 5.2f ",A[i][j]);
}
printf("\n");
}
}
vector<double> Newton(vector<vector<double>> Point){
int n=Point.size()-1;
if(n==-1){
cout<<"错误"<<endl;
}
vector<vector<double>> diffCoef_Table;
for(int i=0;i<n+1;++i){
//n+1项差商,初始化差商表
vector<double> V(i+1,0);
diffCoef_Table.emplace_back(V);
diffCoef_Table[i][0]=Point[i][1]; //差商表第一列为函数值
}
for(int k=1;k<=n;++k){
//递推求差商表
for(int i=k;i<=n;++i){
diffCoef_Table[i][k]=(diffCoef_Table[i][k-1]-diffCoef_Table[i-1][k-1])/(Point[i][0]-Point[i-k][0]);
}
}
print_chashang_table(diffCoef_Table);
vector<double> ans(n+1,0);
ans[0]=Point[0][1];
for(int k=1;k<=n;++k){
vector<double>s={
diffCoef_Table[k][k]};
for(int j=0;j<=k-1;++j){
s=mul(s,{
-Point[j][0],1});
}
ans=add(ans,s);
}
return ans;
}
int main(){
vector<vector<double>> P={
{
0,0},{
2,0},{
1,1},{
5,3}};
vector<double> s=Newton(P);
cout<<endl;
print(s);
system("pause");
return 0;
}
边栏推荐
- Query the number and size of records in each table in MySQL database
- How to execute an SQL statement in MySQL
- When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
- math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
- Introduction to hashtable
- 使用JS完成一个LRU缓存
- Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
- C. The Third Problem(找规律)
- Tips for using dm8huge table
- VPP性能测试
猜你喜欢

About some basic DP -- those things about coins (the basic introduction of DP)

Mysql数据库慢sql抓取与分析

Practical development of member management applet 06 introduction to life cycle function and user-defined method

综合能力测评系统

Fedora/REHL 安装 semanage

Class A, B, C networks and subnet masks in IPv4

10个 Istio 流量管理 最常用的例子,你知道几个?

Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control

Stack and queue

电脑钉钉怎么调整声音
随机推荐
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
2/12 didn't learn anything
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
【leetcode】22. bracket-generating
About some basic DP -- those things about coins (the basic introduction of DP)
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
C. The Third Problem(找规律)
P2102 地砖铺设(dfs&贪心)
Data processing methods - smote series and adasyn
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
Fundamentals of SQL database operation
Web components series (VII) -- life cycle of custom components
10個 Istio 流量管理 最常用的例子,你知道幾個?
Python book learning notes - Chapter 09 section 01 create and use classes
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
729. 我的日程安排表 I(set or 动态开点线段树)
Redis (replicate dictionary server) cache
The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
How can programmers resist the "three poisons" of "greed, anger and ignorance"?
IDEA编译JSP页面生成的class文件路径