当前位置:网站首页>牛顿插值法
牛顿插值法
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;
}
边栏推荐
- 《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
- lora网关以太网传输
- User datagram protocol UDP
- MySQL transaction isolation level
- MySql数据库root账户无法远程登陆解决办法
- Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
- 1291_ Add timestamp function in xshell log
- Mixed development of QML and QWidget (preliminary exploration)
- VPP性能测试
- Cross domain and jsonp details
猜你喜欢

Fundamentals of SQL database operation

During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input

View 工作流程

CertBot 更新证书失败解决

The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched

综合能力测评系统

1291_ Add timestamp function in xshell log

Tips for using dm8huge table

MySql數據庫root賬戶無法遠程登陸解决辦法

Stack and queue
随机推荐
1008 circular right shift of array elements (20 points)
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
2/11 matrix fast power +dp+ bisection
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
Yyds dry goods inventory web components series (VII) -- life cycle of custom components
Lombok principle and the pit of ⽤ @data and @builder at the same time
【leetcode】1189. Maximum number of "balloons"
MySql数据库root账户无法远程登陆解决办法
2328. 网格图中递增路径的数目(记忆化搜索)
What is the difference between gateway address and IP address in tcp/ip protocol?
综合能力测评系统
lora网关以太网传输
VNCTF2022 WriteUp
How can programmers resist the "three poisons" of "greed, anger and ignorance"?
1291_ Add timestamp function in xshell log
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
asp. Core is compatible with both JWT authentication and cookies authentication
Thread sleep, thread sleep application scenarios
Database, relational database and NoSQL non relational database
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)