www.pudn.com > spline_c++.rar > spline.cpp
#include "spline.h"
void GetDerivdowntive2(double down[] , //对三角矩阵的下半部分
double upper[], //对三角矩阵的上半部分
PointInfo *pInfo, //方程右边的常数部分,同时作为导数的输出存放区
int n) //矩阵的维数
{
upper[1]/=2;
for (int i =2 ; iyDer2/= 2;
for (int i =2; i<=n; i++){
(pInfo+i)->yDer2=((pInfo+i)->yDer2-down[i]*((pInfo+i-1)->yDer2))/
(2-down[i]*upper[i-1]);
}
for (int i=n-1; i>0; --i){
((pInfo+i)->yDer2)-=upper[i]*(pInfo+i+1)->yDer2;
}
pInfo->yDer2=(pInfo+n+1)->yDer2=0;
}
void GenarateArray(PointInfo *pBegin,int newLengh, char *pDes, int oldLengh)
{
int nDesPos = 0;
PointInfo *pLeft,*pRight;
while(nDesPos < newLengh-1)
{
if(nDesPos == pBegin->x)
{
*(pDes+nDesPos) = (int)(pBegin->yDer2);
nDesPos++;
pBegin++;
}
else
if(nDesPos < pBegin->x)
{
pRight = pBegin;
pLeft = pBegin-1;
GenarateData(pLeft, pRight, pDes, nDesPos);
nDesPos ++;
}
}
}
void GenarateData(PointInfo *pLeft, PointInfo *pRight, char *pDes, int pos)
{
double h = pRight->x - pLeft->x;
double xR = pRight->x - pos;
double xL = pos -pLeft->x;
*(pDes+pos)= (char)((pLeft->yDer2)*pow( xR,3)/h/6 +
(pRight->yDer2)*pow( xL ,3)/h+
(pLeft->y - (pLeft->yDer2)*pow(h, 2)/6)*xR/h+
(pRight->y - (pRight->yDer2)*pow(h, 2)/6)*xL/h);
}
void GenarateMatrix(double upper[], double down[], PointInfo *pInfo , int oldLength)
{
for(int k=1; k< oldLength ;k++)
{
upper[k] = ((pInfo+k+1)->x -(pInfo+k)->x)/((pInfo+k+1)->x-(pInfo+k-1)->x);
(pInfo+k)->yDer2 = (((pInfo+k+1)->y-(pInfo+k)->y)/((pInfo+k+1)->x -(pInfo+k)->x) - ((pInfo+k)->y - (pInfo+k-1)->y)/((pInfo+k)->x-(pInfo+k-1)->x))*6/
((pInfo+k+1)->x-(pInfo+k-1)->x);
if(k>1)
down[k-1] = 1- upper[k];
}
}
void GenaratePiontInfo(PointInfo *pInfo ,char *pSrc, int oldLength,int newLength)
{
double scale;
scale = newLength /oldLength;
for(int k = 0;k< oldLength ; k++)
{
(pInfo+k)->x = k*scale;
(pInfo+k)->y = *(pSrc+k);
}
}