- 积分
- 21
- 注册时间
- 2007-2-27
- 仿真币
-
- 最后登录
- 1970-1-1
|
发表于 2011-7-16 02:32:07
|
显示全部楼层
来自 美国
本帖最后由 caoer 于 2011-7-15 13:35 编辑
your idea is doable.
here is my interpolation class written in C++.
No public variable exists, so the header file is ignored.
- #include "polynomial_interpolants.hpp"
- #include "../general/spectral_common.hpp"
- //algorithem 30
- void PolynomialInterpolants::BraycentricWeigths(const int N,
- const double *x_vec,
- double *w_vec)
- {
- int j,k;
- for (j = 0; j<=N; j++)
- *(w_vec+j) = 1.0;
- for (j=1; j<=N; j++)
- {
- for (k = 0; k<j; k++)
- {
- *(w_vec+k) = *(w_vec+k) * (*(x_vec+k) - *(x_vec+j));
- *(w_vec+j) = *(w_vec+j) * (*(x_vec+j) - *(x_vec+k));
- }
- }
- for (j=0; j<=N; j++)
- *(w_vec+j) = 1/ *(w_vec+j);
- };
- // Algorithm 31
- double PolynomialInterpolants::LagrangeInterpolation(const unsigned int N,
- const double x,
- const double *x_vec,
- const double *f_vec,
- const double *w_vec)
- {
- double t;
- double numerator = 0.0, denominator = 0.0;
- SpectralCommon *pSC = new SpectralCommon();
- // std::cout<<"N = "<< N << std::endl;
- // for (unsigned int i=0; i<= N; i++)
- // std::cout<<x_vec[i] <<" , "<< f_vec[i]<<" , "<< w_vec[i]<<std::endl;
- for (unsigned int j = 0; j<=N; j++)
- {
- if (pSC->AlmostEqual(x, x_vec[j]))
- {
- return f_vec[j];
- }
- else
- {
- t = w_vec[j]/(x - x_vec[j]);
- numerator = numerator + t* f_vec[j];
- denominator = denominator + t;
- }
- }
- delete pSC;
- return numerator/denominator;
- }
- // Algorithm 32
- void PolynomialInterpolants::PolynomialInterpolationMatrix(const int N,
- const int M,
- const double *x_vec,
- const double *w_vec,
- const double *xi_vec,
- double *T_mtx)
- {
- bool rowHasMatch;
- int k, j;
- double s, t;
- SpectralCommon *pSC = new SpectralCommon();
- for (k=0; k<=M; k++)
- {
- rowHasMatch = false;
- for (j=0; j<=N; j++)
- {
- *(T_mtx+k*(N+1) + j)=0;
- if (pSC->AlmostEqual(*(xi_vec+k), *(x_vec+j)))
- {
- rowHasMatch = true;
- *(T_mtx+k*(N+1)+j) = 1;
- } //if
- } // for j
- if (!rowHasMatch)
- {
- s = 0;
- for(j=0; j<=N; j++)
- {
- t = *(w_vec+j)/(*(xi_vec+k)- *(x_vec+j));
- *(T_mtx+k*(N+1)+j) = t;
- s = s + t;
- }
- for (j = 0; j<=N; j++)
- *(T_mtx +k*(N+1)+j) = *(T_mtx +k*(N+1)+j) / s;
- } // if
- } //for k
- delete pSC;
- }
- // Algorithm 33
- void PolynomialInterpolants::InterpolateToNewpoints(const int M,
- const int N,
- const double *T_mtx,
- const double *f_vec,
- double *fInterp_vec)
- {
- int i,j;
- double t;
- for(i =0; i<=M; i++)
- {
- t = 0;
- for(j=0; j<=N; j++)
- t = t+ *(T_mtx +i*(N+1)+j) * *(f_vec +j);
- *(fInterp_vec+i) = t;
- }
- }
- // Algorithm 34
- void PolynomialInterpolants::LagrangeInterpolatingPolynomials(const int N,
- const double x,
- const double *x_vec,
- const double *w_vec,
- double *l_vec)
- {
- bool xMatchesNode= false;
- double s, t;
- int j;
- SpectralCommon *pSC = new SpectralCommon();
- for (j =0; j<=N; j++)
- {
- *(l_vec+j) = 0.0;
- if (pSC->AlmostEqual(x, *(x_vec+j)))
- {
- *(l_vec+j) = 1.0;
- xMatchesNode = true;
- }
- } // for j
- if (!xMatchesNode)
- {
- s = 0.0;
- for (j = 0; j <= N; j++)
- {
- t = *(w_vec+j)/(x - *(x_vec+j));
- *(l_vec+j) = t;
- s = s + t;
- }
- for(j = 0; j<= N; j++)
- *(l_vec+j) = *(l_vec+j) / s;
- } //if
- delete pSC;
- }
复制代码 |
|