当前位置:网站首页>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;
}
边栏推荐
- Easyrecovery靠谱不收费的数据恢复电脑软件
- Several important classes in unity
- hashlimit速率控制
- Fedora/REHL 安装 semanage
- PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
- Lambda expression learning
- 2327. Number of people who know secrets (recursive)
- flink sql 能同时读多个topic吗。with里怎么写
- Python book learning notes - Chapter 09 section 01 create and use classes
- pd. to_ numeric
猜你喜欢

During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input

MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】

综合能力测评系统

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
![[face recognition series] | realize automatic makeup](/img/a5/de98d0522b9dae809cd242aac305b3.jpg)
[face recognition series] | realize automatic makeup

Recommendation system (IX) PNN model (product based neural networks)

Slow SQL fetching and analysis of MySQL database
![Mlapi series - 04 - network variables and network serialization [network synchronization]](/img/fc/aebbad5295481788de5c1fdb432a77.jpg)
Mlapi series - 04 - network variables and network serialization [network synchronization]

How to realize automatic playback of H5 video
![[tomato assistant installation]](/img/06/672a616d4fc2a43b83054eb1057628.jpg)
[tomato assistant installation]
随机推荐
729. My schedule I (set or dynamic open point segment tree)
POI add border
P2102 floor tile laying (DFS & greed)
解决“C2001:常量中有换行符“编译问题
Class A, B, C networks and subnet masks in IPv4
[tomato assistant installation]
Several important classes in unity
pd. to_ numeric
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
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
Le compte racine de la base de données MySQL ne peut pas se connecter à distance à la solution
Lombok principle and the pit of ⽤ @data and @builder at the same time
asp. Core is compatible with both JWT authentication and cookies authentication
How can programmers resist the "three poisons" of "greed, anger and ignorance"?
Solve the compilation problem of "c2001: line breaks in constants"
1008 circular right shift of array elements (20 points)
What is the difference between gateway address and IP address in tcp/ip protocol?
After learning classes and objects, I wrote a date class
User datagram protocol UDP
【leetcode】22. bracket-generating