ApprPolynomial2.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/ApprQuery.h>
  9. #include <Mathematics/Array2.h>
  10. #include <Mathematics/GMatrix.h>
  11. #include <array>
  12. // The samples are (x[i],w[i]) for 0 <= i < S. Think of w as a function of
  13. // x, say w = f(x). The function fits the samples with a polynomial of
  14. // degree d, say w = sum_{i=0}^d c[i]*x^i. The method is a least-squares
  15. // fitting algorithm. The mParameters stores the coefficients c[i] for
  16. // 0 <= i <= d. The observation type is std::array<Real,2>, which represents
  17. // a pair (x,w).
  18. //
  19. // WARNING. The fitting algorithm for polynomial terms
  20. // (1,x,x^2,...,x^d)
  21. // is known to be nonrobust for large degrees and for large magnitude data.
  22. // One alternative is to use orthogonal polynomials
  23. // (f[0](x),...,f[d](x))
  24. // and apply the least-squares algorithm to these. Another alternative is to
  25. // transform
  26. // (x',w') = ((x-xcen)/rng, w/rng)
  27. // where xmin = min(x[i]), xmax = max(x[i]), xcen = (xmin+xmax)/2, and
  28. // rng = xmax-xmin. Fit the (x',w') points,
  29. // w' = sum_{i=0}^d c'[i]*(x')^i.
  30. // The original polynomial is evaluated as
  31. // w = rng*sum_{i=0}^d c'[i]*((x-xcen)/rng)^i
  32. namespace WwiseGTE
  33. {
  34. template <typename Real>
  35. class ApprPolynomial2 : public ApprQuery<Real, std::array<Real, 2>>
  36. {
  37. public:
  38. // Initialize the model parameters to zero.
  39. ApprPolynomial2(int degree)
  40. :
  41. mDegree(degree),
  42. mSize(degree + 1),
  43. mParameters(mSize, (Real)0)
  44. {
  45. mXDomain[0] = std::numeric_limits<Real>::max();
  46. mXDomain[1] = -mXDomain[0];
  47. }
  48. // Basic fitting algorithm. See ApprQuery.h for the various Fit(...)
  49. // functions that you can call.
  50. virtual bool FitIndexed(
  51. size_t numObservations, std::array<Real, 2> const* observations,
  52. size_t numIndices, int const* indices) override
  53. {
  54. if (this->ValidIndices(numObservations, observations, numIndices, indices))
  55. {
  56. int s, i0, i1;
  57. // Compute the powers of x.
  58. int numSamples = static_cast<int>(numIndices);
  59. int twoDegree = 2 * mDegree;
  60. Array2<Real> xPower(twoDegree + 1, numSamples);
  61. for (s = 0; s < numSamples; ++s)
  62. {
  63. Real x = observations[indices[s]][0];
  64. mXDomain[0] = std::min(x, mXDomain[0]);
  65. mXDomain[1] = std::max(x, mXDomain[1]);
  66. xPower[s][0] = (Real)1;
  67. for (i0 = 1; i0 <= twoDegree; ++i0)
  68. {
  69. xPower[s][i0] = x * xPower[s][i0 - 1];
  70. }
  71. }
  72. // Matrix A is the Vandermonde matrix and vector B is the
  73. // right-hand side of the linear system A*X = B.
  74. GMatrix<Real> A(mSize, mSize);
  75. GVector<Real> B(mSize);
  76. for (i0 = 0; i0 <= mDegree; ++i0)
  77. {
  78. Real sum = (Real)0;
  79. for (s = 0; s < numSamples; ++s)
  80. {
  81. Real w = observations[indices[s]][1];
  82. sum += w * xPower[s][i0];
  83. }
  84. B[i0] = sum;
  85. for (i1 = 0; i1 <= mDegree; ++i1)
  86. {
  87. sum = (Real)0;
  88. for (s = 0; s < numSamples; ++s)
  89. {
  90. sum += xPower[s][i0 + i1];
  91. }
  92. A(i0, i1) = sum;
  93. }
  94. }
  95. // Solve for the polynomial coefficients.
  96. GVector<Real> coefficients = Inverse(A) * B;
  97. bool hasNonzero = false;
  98. for (int i = 0; i < mSize; ++i)
  99. {
  100. mParameters[i] = coefficients[i];
  101. if (coefficients[i] != (Real)0)
  102. {
  103. hasNonzero = true;
  104. }
  105. }
  106. return hasNonzero;
  107. }
  108. std::fill(mParameters.begin(), mParameters.end(), (Real)0);
  109. return false;
  110. }
  111. // Get the parameters for the best fit.
  112. std::vector<Real> const& GetParameters() const
  113. {
  114. return mParameters;
  115. }
  116. virtual size_t GetMinimumRequired() const override
  117. {
  118. return static_cast<size_t>(mSize);
  119. }
  120. // Compute the model error for the specified observation for the
  121. // current model parameters. The returned value for observation
  122. // (x0,w0) is |w(x0) - w0|, where w(x) is the fitted polynomial.
  123. virtual Real Error(std::array<Real, 2> const& observation) const override
  124. {
  125. Real w = Evaluate(observation[0]);
  126. Real error = std::fabs(w - observation[1]);
  127. return error;
  128. }
  129. virtual void CopyParameters(ApprQuery<Real, std::array<Real, 2>> const* input) override
  130. {
  131. auto source = dynamic_cast<ApprPolynomial2 const*>(input);
  132. if (source)
  133. {
  134. *this = *source;
  135. }
  136. }
  137. // Evaluate the polynomial. The domain interval is provided so you can
  138. // interpolate (x in domain) or extrapolate (x not in domain).
  139. std::array<Real, 2> const& GetXDomain() const
  140. {
  141. return mXDomain;
  142. }
  143. Real Evaluate(Real x) const
  144. {
  145. int i = mDegree;
  146. Real w = mParameters[i];
  147. while (--i >= 0)
  148. {
  149. w = mParameters[i] + w * x;
  150. }
  151. return w;
  152. }
  153. private:
  154. int mDegree, mSize;
  155. std::array<Real, 2> mXDomain;
  156. std::vector<Real> mParameters;
  157. };
  158. }