当前位置:网站首页>牛顿插值法
牛顿插值法
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;
}
边栏推荐
- Lombok原理和同时使⽤@Data和@Builder 的坑
- Lombok principle and the pit of ⽤ @data and @builder at the same time
- Mlapi series - 04 - network variables and network serialization [network synchronization]
- Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
- lora网关以太网传输
- How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
- 51nod 1130 n factorial length V2 (Stirling approximation)
- Le compte racine de la base de données MySQL ne peut pas se connecter à distance à la solution
- asp. Core is compatible with both JWT authentication and cookies authentication
- VPP performance test
猜你喜欢
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
Maxay paper latex template description
1291_Xshell日志中增加时间戳的功能
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
Practical development of member management applet 06 introduction to life cycle function and user-defined method
Introduction to hashtable
Viewing and verifying backup sets using dmrman
Record the pit of NETCORE's memory surge
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
随机推荐
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
使用JS完成一个LRU缓存
Figure application details
VNCTF2022 WriteUp
VPP performance test
综合能力测评系统
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
View 工作流程
[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
Data processing methods - smote series and adasyn
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
Brief tutorial for soft exam system architecture designer | general catalog
Record the pit of NETCORE's memory surge
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
Record the pit of NETCORE's memory surge
R note prophet
绑定在游戏对象上的脚本的执行顺序
[tomato assistant installation]
Python book learning notes - Chapter 09 section 01 create and use classes
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0