1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.2.1 (2010/10/01)
#include "Wm5MathematicsPCH.h"
#include "Wm5ApprPolynomialFit2.h"
#include "Wm5LinearSystem.h"
namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
PolynomialFit2<Real>::PolynomialFit2 (int numSamples, const Real* xSamples,
const Real* wSamples, int numPowers, const int* powers)
:
mNumPowers(numPowers)
{
InitializePowers(numPowers, powers);
mPowers = new1<int>(mNumPowers);
memcpy(mPowers, powers, mNumPowers*sizeof(int));
mCoefficients = new1<Real>(mNumPowers);
GMatrix<Real> mat(mNumPowers, mNumPowers); // initially zero
GVector<Real> rhs(mNumPowers); // initially zero
GVector<Real> xPower(mNumPowers);
int i, row, col;
for (i = 0; i < numSamples; ++i)
{
// Compute relevant powers of x. TODO: Build minimum tree for
// powers of x?
Real x = xSamples[i];
Real w = wSamples[i];
for (int j = 0; j < mNumPowers; ++j)
{
xPower[j] = Math<Real>::Pow(x, (Real)mPowers[j]);
}
for (row = 0; row < mNumPowers; ++row)
{
// Update the upper-triangular portion of the symmetric matrix.
for (col = row; col < mNumPowers; ++col)
{
mat[row][col] += xPower[mPowers[row] + mPowers[col]];
}
// Update the right-hand side of the system.
rhs[row] += w*xPower[mPowers[row]];
}
}
// Copy the upper-triangular portion of the symmetric matrix to the
// lower-triangular portion.
for (row = 0; row < mNumPowers; ++row)
{
for (col = 0; col < row; ++col)
{
mat[row][col] = mat[col][row];
}
}
// Precondition by normalizing the sums.
Real invNumSamples = ((Real)1)/(Real)numSamples;
for (row = 0; row < mNumPowers; ++row)
{
for (col = 0; col < mNumPowers; ++col)
{
mat[row][col] *= invNumSamples;
}
rhs[row] *= invNumSamples;
}
if (LinearSystem<Real>().Solve(mat, rhs, mCoefficients))
{
mSolved = true;
}
else
{
memset(mCoefficients, 0, mNumPowers*sizeof(Real));
mSolved = false;
}
}
//----------------------------------------------------------------------------
template <typename Real>
PolynomialFit2<Real>::~PolynomialFit2 ()
{
delete1(mPowers);
delete1(mXPowers);
delete1(mCoefficients);
}
//----------------------------------------------------------------------------
template <typename Real>
PolynomialFit2<Real>::operator bool () const
{
return mSolved;
}
//----------------------------------------------------------------------------
template <typename Real>
Real PolynomialFit2<Real>::GetXMin () const
{
return mMin[0];
}
//----------------------------------------------------------------------------
template <typename Real>
Real PolynomialFit2<Real>::GetXMax () const
{
return mMax[0];
}
//----------------------------------------------------------------------------
template <typename Real>
Real PolynomialFit2<Real>::GetWMin () const
{
return mMin[1];
}
//----------------------------------------------------------------------------
template <typename Real>
Real PolynomialFit2<Real>::GetWMax () const
{
return mMax[1];
}
//----------------------------------------------------------------------------
template <typename Real>
Real PolynomialFit2<Real>::operator() (Real x) const
{
// Transform x from the original space to [-1,1].
x = (Real)-1 + ((Real)2)*(x - mMin[0])*mScale[0];
// Compute relevant powers of x.
for (int j = 1; j <= mMaxXPower; ++j)
{
mXPowers[j] = mXPowers[j-1] * x;
}
Real w = (Real)0;
for (int i = 0; i < mNumPowers; ++i)
{
Real xp = mXPowers[mPowers[i]];
w += mCoefficients[i] * xp;
}
// Transform w from [-1,1] back to the original space.
w = (w + (Real)1)*mInvTwoWScale + mMin[1];
return w;
}
//----------------------------------------------------------------------------
template <typename Real>
void PolynomialFit2<Real>::InitializePowers (int numPowers, const int* powers)
{
// Copy the powers for use in evaluation of the fitted polynomial.
mNumPowers = numPowers;
mPowers = new1<int>(mNumPowers);
memcpy(mPowers, powers, mNumPowers*sizeof(int));
// Determine the maximum power. Powers of x are computed up to twice the
// powers when constructing the fitted polynomial. Powers of x are
// computed up to the powers for the evaluation of the fitted polynomial.
mMaxXPower = mPowers[0];
int i;
for (i = 1; i < mNumPowers; ++i)
{
if (mPowers[i] > mMaxXPower)
{
mMaxXPower = mPowers[i];
}
}
mXPowers = new1<Real>(2*mMaxXPower + 1);
mXPowers[0] = (Real)1;
}
//----------------------------------------------------------------------------
template <typename Real>
void PolynomialFit2<Real>::TransformToUnit (int numSamples,
const Real* srcSamples[2], Real* trgSamples[2])
{
// Transform the data to [-1,1]^2 for numerical robustness.
for (int j = 0; j < 2; ++j)
{
mMin[j] = srcSamples[j][0];
mMax[j] = mMin[j];
int i;
for (i = 1; i < numSamples; ++i)
{
Real value = srcSamples[j][i];
if (value < mMin[j])
{
mMin[j] = value;
}
else if (value > mMax[j])
{
mMax[j] = value;
}
}
mScale[j] = (Real)1/(mMax[j] - mMin[j]);
trgSamples[j] = new1<Real>(numSamples);
for (i = 0; i < numSamples; ++i)
{
trgSamples[j][i] = (Real)-1 +
((Real)2)*(srcSamples[j][i] - mMin[j])*mScale[j];
}
}
mInvTwoWScale = ((Real)0.5)/mScale[1];
}
//----------------------------------------------------------------------------
template <typename Real>
void PolynomialFit2<Real>::DoLeastSquaresFit (int numSamples,
Real* trgSamples[2])
{
// The matrix and vector for a linear system that determines the
// coefficients of the fitted polynomial.
GMatrix<Real> mat(mNumPowers, mNumPowers); // initially zero
GVector<Real> rhs(mNumPowers); // initially zero
mCoefficients = new1<Real>(mNumPowers);
int row, col;
for (int i = 0; i < numSamples; ++i)
{
// Compute relevant powers of x.
Real x = trgSamples[0][i];
Real w = trgSamples[1][i];
for (int j = 1; j <= 2*mMaxXPower; ++j)
{
mXPowers[j] = mXPowers[j-1] * x;
}
for (row = 0; row < mNumPowers; ++row)
{
// Update the upper-triangular portion of the symmetric matrix.
Real xp;
for (col = row; col < mNumPowers; ++col)
{
xp = mXPowers[mPowers[row] + mPowers[col]];
mat[row][col] += xp;
}
// Update the right-hand side of the system.
xp = mXPowers[mPowers[row]];
rhs[row] += xp * w;
}
}
// Copy the upper-triangular portion of the symmetric matrix to the
// lower-triangular portion.
for (row = 0; row < mNumPowers; ++row)
{
for (col = 0; col < row; ++col)
{
mat[row][col] = mat[col][row];
}
}
// Precondition by normalizing the sums.
Real invNumSamples = ((Real)1)/(Real)numSamples;
for (row = 0; row < mNumPowers; ++row)
{
for (col = 0; col < mNumPowers; ++col)
{
mat[row][col] *= invNumSamples;
}
rhs[row] *= invNumSamples;
}
if (LinearSystem<Real>().Solve(mat, rhs, mCoefficients))
{
mSolved = true;
}
else
{
memset(mCoefficients, 0, mNumPowers*sizeof(Real));
mSolved = false;
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_MATHEMATICS_ITEM
class PolynomialFit2<float>;
template WM5_MATHEMATICS_ITEM
class PolynomialFit2<double>;
//----------------------------------------------------------------------------
}
|