当前位置:网站首页>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;
}
边栏推荐
- 软考 系统架构设计师 简明教程 | 总目录
- 拉格朗日插值法
- 电脑钉钉怎么调整声音
- [adjustable delay network] development of FPGA based adjustable delay network system Verilog
- Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
- C. The third problem
- lora网关以太网传输
- Slow SQL fetching and analysis of MySQL database
- 2/13 review Backpack + monotonic queue variant
- 2328. Number of incremental paths in the grid graph (memory search)
猜你喜欢

Mysql database storage engine

MySql数据库root账户无法远程登陆解决办法

IDEA编译JSP页面生成的class文件路径

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

Comprehensive ability evaluation system

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

解决“C2001:常量中有换行符“编译问题

How to realize automatic playback of H5 video

Guitar Pro 8.0最详细全面的更新内容及全部功能介绍

Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
随机推荐
Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
HotSpot VM
综合能力测评系统
Sentinel sliding window traffic statistics
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
Several important classes in unity
One question per day (Mathematics)
Certbot failed to update certificate solution
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
C. The Third Problem(找规律)
【HBZ分享】ArrayList的增删慢查询快的原因
JVM garbage collector concept
Slow SQL fetching and analysis of MySQL database
asp. Core is compatible with both JWT authentication and cookies authentication
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
QML和QWidget混合开发(初探)
[tomato assistant installation]
Explain in simple terms node template parsing error escape is not a function
. Net interprocess communication