当前位置:网站首页>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;
}
边栏推荐
- flink sql 能同时读多个topic吗。with里怎么写
- Brief tutorial for soft exam system architecture designer | general catalog
- [Zhao Yuqiang] deploy kubernetes cluster with binary package
- Solution of storage bar code management system in food industry
- 深入浅出node模板解析错误escape is not a function
- Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
- P2102 地砖铺设(dfs&贪心)
- Class A, B, C networks and subnet masks in IPv4
- Several important classes in unity
- The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
Record an excel xxE vulnerability
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
JVM garbage collector concept
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Overturn your cognition? The nature of get and post requests
Data processing methods - smote series and adasyn
Fundamentals of SQL database operation
随机推荐
Leetcode32 longest valid bracket (dynamic programming difficult problem)
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
[disassembly] a visual air fryer. By the way, analyze the internal circuit
Implementation of knowledge consolidation source code 2: TCP server receives and processes half packets and sticky packets
Unity中几个重要类
DM8 archive log file manual switching
C. The third problem
hashlimit速率控制
How to realize automatic playback of H5 video
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
VNCTF2022 WriteUp
[network] channel attention network and spatial attention network
E. Best Pair
Slow SQL fetching and analysis of MySQL database
PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
Mixed development of QML and QWidget (preliminary exploration)
Several important classes in unity
Global and Chinese markets for MRI safe implants 2022-2028: technology, participants, trends, market size and share Research Report
After learning classes and objects, I wrote a date class