当前位置:网站首页>newton interpolation
newton interpolation
2022-07-06 04:21: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) // Polynomial multiplication
{
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<<" error "<<endl;
}
vector<vector<double>> diffCoef_Table;
for(int i=0;i<n+1;++i){
//n+1 Item difference quotient , Initialize the difference quotient table
vector<double> V(i+1,0);
diffCoef_Table.emplace_back(V);
diffCoef_Table[i][0]=Point[i][1]; // The first column of the difference quotient table is the function value
}
for(int k=1;k<=n;++k){
// Recursively calculate the difference quotient table
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;
}
边栏推荐
- Fedora/rehl installation semanage
- Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
- Lora gateway Ethernet transmission
- User datagram protocol UDP
- 【HBZ分享】云数据库如何定位慢查询
- Sentinel sliding window traffic statistics
- Web components series (VII) -- life cycle of custom components
- 10 exemples les plus courants de gestion du trafic istio, que savez - vous?
- 综合能力测评系统
- Data processing methods - smote series and adasyn
猜你喜欢
JVM garbage collector concept
[face recognition series] | realize automatic makeup
Fundamentals of SQL database operation
Slow SQL fetching and analysis of MySQL database
The value of two date types is subtracted and converted to seconds
Comprehensive ability evaluation system
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
Yyds dry goods inventory web components series (VII) -- life cycle of custom components
Stack and queue
Lombok原理和同时使⽤@Data和@Builder 的坑
随机推荐
Recommendation | recommendation of 9 psychotherapy books
P2022 有趣的数(二分&数位dp)
Figure application details
CertBot 更新证书失败解决
Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
asp. Core is compatible with both JWT authentication and cookies authentication
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
How to execute an SQL statement in MySQL
[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
Yyds dry goods inventory web components series (VII) -- life cycle of custom components
[tomato assistant installation]
asp. Core is compatible with both JWT authentication and cookies authentication
Introduction to hashtable
In depth MySQL transactions, stored procedures and triggers
Solution of storage bar code management system in food industry
DM8 archive log file manual switching
2/13 review Backpack + monotonic queue variant
POI add border
P2022 interesting numbers (binary & digit DP)