当前位置:网站首页>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;
}
边栏推荐
- Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
- Knowledge consolidation source code implementation 3: buffer ringbuffer
- 10 exemples les plus courants de gestion du trafic istio, que savez - vous?
- 拉格朗日插值法
- 2/11 matrix fast power +dp+ bisection
- 电脑钉钉怎么调整声音
- 2328. Number of incremental paths in the grid graph (memory search)
- asp. Core is compatible with both JWT authentication and cookies authentication
- coreldraw2022新版本新功能介绍cdr2022
- 满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
猜你喜欢
Mysql数据库慢sql抓取与分析
Stable Huawei micro certification, stable Huawei cloud database service practice
Practical development of member management applet 06 introduction to life cycle function and user-defined method
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
Cross domain and jsonp details
View 工作流程
Certbot failed to update certificate solution
Solution to the problem that the root account of MySQL database cannot be logged in remotely
Benefits of automated testing
Mysql database storage engine
随机推荐
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
Implementation of knowledge consolidation source code 2: TCP server receives and processes half packets and sticky packets
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
About some basic DP -- those things about coins (the basic introduction of DP)
VNCTF2022 WriteUp
1291_ Add timestamp function in xshell log
Benefits of automated testing
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
【HBZ分享】云数据库如何定位慢查询
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
Brief tutorial for soft exam system architecture designer | general catalog
[disassembly] a visual air fryer. By the way, analyze the internal circuit
Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
[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
[Zhao Yuqiang] deploy kubernetes cluster with binary package
10個 Istio 流量管理 最常用的例子,你知道幾個?
Comprehensive ability evaluation system
1008 circular right shift of array elements (20 points)