当前位置:网站首页>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 安装 semanage
- Stack and queue
- 满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
- Knowledge consolidation source code implementation 3: buffer ringbuffer
- Stable Huawei micro certification, stable Huawei cloud database service practice
- 10個 Istio 流量管理 最常用的例子,你知道幾個?
- 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
- 解决“C2001:常量中有换行符“编译问题
- Viewing and verifying backup sets using dmrman
- Lambda expression learning
猜你喜欢
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
Stable Huawei micro certification, stable Huawei cloud database service practice
Figure application details
绑定在游戏对象上的脚本的执行顺序
Fedora/REHL 安装 semanage
10个 Istio 流量管理 最常用的例子,你知道几个?
[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
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
R note prophet
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
随机推荐
CertBot 更新证书失败解决
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
Stable Huawei micro certification, stable Huawei cloud database service practice
Path of class file generated by idea compiling JSP page
[Zhao Yuqiang] deploy kubernetes cluster with binary package
2328. Number of incremental paths in the grid graph (memory search)
Record an excel xxE vulnerability
How does computer nail adjust sound
Tips for using dm8huge table
Data processing methods - smote series and adasyn
flink sql 能同时读多个topic吗。with里怎么写
Lombok principle and the pit of ⽤ @data and @builder at the same time
MySql数据库root账户无法远程登陆解决办法
2/10 parallel search set +bfs+dfs+ shortest path +spfa queue optimization
Several important classes in unity
Script lifecycle
Mlapi series - 04 - network variables and network serialization [network synchronization]
MySQL transaction isolation level
2/11 matrix fast power +dp+ bisection
IDEA编译JSP页面生成的class文件路径