Wm4ApprPolyFit3.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "Wm4FoundationPCH.h"
00018 #include "Wm4ApprPolyFit3.h"
00019 #include "Wm4LinearSystem.h"
00020
00021 namespace Wm4
00022 {
00023
00024 template <class Real>
00025 Real* PolyFit3 (int iSamples, const Real* afX, const Real* afY,
00026 const Real* afW, int iXDegree, int iYDegree)
00027 {
00028 int iXBound = iXDegree + 1;
00029 int iYBound = iYDegree + 1;
00030 int iQuantity = iXBound*iYBound;
00031 Real* afCoeff = WM4_NEW Real[iQuantity];
00032
00033 int i0, j0, i1, j1, iS;
00034
00035
00036 Real** aafXP;
00037 Real** aafYP;
00038 Allocate<Real>(2*iXDegree+1,iSamples,aafXP);
00039 Allocate<Real>(2*iYDegree+1,iSamples,aafYP);
00040 for (iS = 0; iS < iSamples; iS++)
00041 {
00042 aafXP[iS][0] = (Real)1.0;
00043 for (i0 = 1; i0 <= 2*iXDegree; i0++)
00044 {
00045 aafXP[iS][i0] = afX[iS]*aafXP[iS][i0-1];
00046 }
00047
00048 aafYP[iS][0] = (Real)1.0;
00049 for (j0 = 1; j0 <= 2*iYDegree; j0++)
00050 {
00051 aafYP[iS][j0] = afY[iS]*aafYP[iS][j0-1];
00052 }
00053 }
00054
00055
00056 GMatrix<Real> kA(iQuantity,iQuantity);
00057 Real* afB = WM4_NEW Real[iQuantity];
00058
00059 for (j0 = 0; j0 <= iYDegree; j0++)
00060 {
00061 for (i0 = 0; i0 <= iXDegree; i0++)
00062 {
00063 int iIndex0 = i0+iXBound*j0;
00064 Real fSum = (Real)0.0;
00065 for (iS = 0; iS < iSamples; iS++)
00066 {
00067 fSum += afW[iS]*aafXP[iS][i0]*aafYP[iS][j0];
00068 }
00069
00070 afB[iIndex0] = fSum;
00071
00072 for (j1 = 0; j1 <= iYDegree; j1++)
00073 {
00074 for (i1 = 0; i1 <= iXDegree; i1++)
00075 {
00076 int iIndex1 = i1+iXBound*j1;
00077 fSum = (Real)0.0;
00078 for (iS = 0; iS < iSamples; iS++)
00079 {
00080 fSum += aafXP[iS][i0+i1]*aafYP[iS][j0+j1];
00081 }
00082
00083 kA(iIndex0,iIndex1) = fSum;
00084 }
00085 }
00086 }
00087 }
00088
00089
00090 bool bHasSolution = LinearSystem<Real>().Solve(kA,afB,afCoeff);
00091 if (!bHasSolution) throw std::exception();
00092 assert(bHasSolution);
00093 (void)bHasSolution;
00094 WM4_DELETE[] afB;
00095 Deallocate<Real>(aafXP);
00096 Deallocate<Real>(aafYP);
00097
00098 return afCoeff;
00099 }
00100
00101
00102
00103
00104
00105 template WM4_FOUNDATION_ITEM
00106 float* PolyFit3<float> (int, const float*, const float*, const float*,
00107 int, int);
00108
00109 template WM4_FOUNDATION_ITEM
00110 double* PolyFit3<double> (int, const double*, const double*, const double*,
00111 int, int);
00112
00113 }