gpt4 book ai didi

c++ - 有没有什么好的库可以在 C++ 中求解三次样条曲线?

转载 作者:IT老高 更新时间:2023-10-28 22:17:52 27 4
gpt4 key购买 nike

我正在寻找一个好的 C++ 库来为我提供解决大型三次样条曲线(大约 1000 点)的函数,有人知道吗?

最佳答案

自己写。这是我根据优秀 wiki algorithm 编写的 spline() 函数:

#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

using vec = vector<double>;

struct SplineSet{
double a;
double b;
double c;
double d;
double x;
};

vector<SplineSet> spline(vec &x, vec &y)
{
int n = x.size()-1;
vec a;
a.insert(a.begin(), y.begin(), y.end());
vec b(n);
vec d(n);
vec h;

for(int i = 0; i < n; ++i)
h.push_back(x[i+1]-x[i]);

vec alpha;
alpha.push_back(0);
for(int i = 1; i < n; ++i)
alpha.push_back( 3*(a[i+1]-a[i])/h[i] - 3*(a[i]-a[i-1])/h[i-1] );

vec c(n+1);
vec l(n+1);
vec mu(n+1);
vec z(n+1);
l[0] = 1;
mu[0] = 0;
z[0] = 0;

for(int i = 1; i < n; ++i)
{
l[i] = 2 *(x[i+1]-x[i-1])-h[i-1]*mu[i-1];
mu[i] = h[i]/l[i];
z[i] = (alpha[i]-h[i-1]*z[i-1])/l[i];
}

l[n] = 1;
z[n] = 0;
c[n] = 0;

for(int j = n-1; j >= 0; --j)
{
c[j] = z [j] - mu[j] * c[j+1];
b[j] = (a[j+1]-a[j])/h[j]-h[j]*(c[j+1]+2*c[j])/3;
d[j] = (c[j+1]-c[j])/3/h[j];
}

vector<SplineSet> output_set(n);
for(int i = 0; i < n; ++i)
{
output_set[i].a = a[i];
output_set[i].b = b[i];
output_set[i].c = c[i];
output_set[i].d = d[i];
output_set[i].x = x[i];
}
return output_set;
}

int main()
{
vec x(11);
vec y(11);
for(int i = 0; i < x.size(); ++i)
{
x[i] = i;
y[i] = sin(i);
}

vector<SplineSet> cs = spline(x, y);
for(int i = 0; i < cs.size(); ++i)
cout << cs[i].d << "\t" << cs[i].c << "\t" << cs[i].b << "\t" << cs[i].a << endl;
}

关于c++ - 有没有什么好的库可以在 C++ 中求解三次样条曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1204553/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com