当前位置:网站首页>牛顿插值法
牛顿插值法
2022-07-06 04:16: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) //多项式乘法
{
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<<"错误"<<endl;
}
vector<vector<double>> diffCoef_Table;
for(int i=0;i<n+1;++i){
//n+1项差商,初始化差商表
vector<double> V(i+1,0);
diffCoef_Table.emplace_back(V);
diffCoef_Table[i][0]=Point[i][1]; //差商表第一列为函数值
}
for(int k=1;k<=n;++k){
//递推求差商表
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;
}
边栏推荐
- QML和QWidget混合开发(初探)
- P2102 地砖铺设(dfs&贪心)
- 10個 Istio 流量管理 最常用的例子,你知道幾個?
- P2022 有趣的数(二分&数位dp)
- Practical development of member management applet 06 introduction to life cycle function and user-defined method
- lora网关以太网传输
- Mysql数据库慢sql抓取与分析
- Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
- Interface idempotency
- POI add border
猜你喜欢

AcWing 243. A simple integer problem 2 (tree array interval modification interval query)

What is the difference between gateway address and IP address in tcp/ip protocol?

Record the pit of NETCORE's memory surge

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

Overturn your cognition? The nature of get and post requests

记一次excel XXE漏洞

Deep learning framework installation (tensorflow & pytorch & paddlepaddle)

《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动

Tips for using dm8huge table

20、 EEPROM memory (AT24C02) (similar to AD)
随机推荐
Web components series (VII) -- life cycle of custom components
P2102 地砖铺设(dfs&贪心)
Global and Chinese markets for MRI safe implants 2022-2028: technology, participants, trends, market size and share Research Report
绑定在游戏对象上的脚本的执行顺序
Several important classes in unity
ESP32_ FreeRTOS_ Arduino_ 1_ Create task
User datagram protocol UDP
深入浅出node模板解析错误escape is not a function
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
Solution of storage bar code management system in food industry
One question per day (Mathematics)
MySql數據庫root賬戶無法遠程登陸解决辦法
[face recognition series] | realize automatic makeup
图应用详解
Record the pit of NETCORE's memory surge
What is the difference between gateway address and IP address in tcp/ip protocol?
[Zhao Yuqiang] deploy kubernetes cluster with binary package
Practical development of member management applet 06 introduction to life cycle function and user-defined method
Mlapi series - 04 - network variables and network serialization [network synchronization]
Basic knowledge of binary tree, BFC, DFS