当前位置:网站首页>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;
}
边栏推荐
- 2/12 didn't learn anything
- 2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
- cdc 能全量拉去oracle 表嘛
- VNCTF2022 WriteUp
- [adjustable delay network] development of FPGA based adjustable delay network system Verilog
- 10个 Istio 流量管理 最常用的例子,你知道几个?
- Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
- [tomato assistant installation]
- Fundamentals of SQL database operation
- Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
猜你喜欢

【leetcode】1189. Maximum number of "balloons"

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

Solution of storage bar code management system in food industry

Data processing methods - smote series and adasyn

Basic knowledge of binary tree, BFC, DFS
![[Zhao Yuqiang] deploy kubernetes cluster with binary package](/img/be/8710605c8da8d1553af974f0dc46bc.jpg)
[Zhao Yuqiang] deploy kubernetes cluster with binary package

How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server

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

Comprehensive ability evaluation system

About some basic DP -- those things about coins (the basic introduction of DP)
随机推荐
2/13 review Backpack + monotonic queue variant
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
Stack and queue
Mysql数据库慢sql抓取与分析
Certbot failed to update certificate solution
Cross domain and jsonp details
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
About some basic DP -- those things about coins (the basic introduction of DP)
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
R note prophet
Global and Chinese markets for otolaryngology devices 2022-2028: Research Report on technology, participants, trends, market size and share
Path of class file generated by idea compiling JSP page
C. The Third Problem(找规律)
HotSpot VM
[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
Codeforces Round #770 (Div. 2) B. Fortune Telling
VPP performance test
Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
cdc 能全量拉去oracle 表嘛
拉格朗日插值法