Polynomial.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #ifndef _Polynomial_h_
  2. #define _Polynomial_h_
  3. /* Polynomial.h
  4. *
  5. * Copyright (C) 1993-2017 David Weenink
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. djmw 20020813 GPL header
  22. djmw 20110306 Latest modification.
  23. */
  24. #define FITTER_PARAMETER_FREE 0
  25. #define FITTER_PARAMETER_FIXED 1
  26. #include "SimpleVector.h"
  27. #include "Function.h"
  28. #include "TableOfReal.h"
  29. #include "Graphics.h"
  30. #include "Minimizers.h"
  31. #include "Spectrum.h"
  32. #include "RealTier.h"
  33. #include "SSCP.h"
  34. #define Spline_MAXIMUM_DEGREE 20
  35. #include "Polynomial_def.h"
  36. void FunctionTerms_init (FunctionTerms me, double xmin, double xmax, integer numberOfCoefficients);
  37. void FunctionTerms_initFromString (FunctionTerms me, double xmin, double xmax, conststring32 s, bool allowTrailingZeros);
  38. autoFunctionTerms FunctionTerms_create (double xmin, double xmax, integer numberOfCoefficients);
  39. void FunctionTerms_setDomain (FunctionTerms me, double xmin, double xmax);
  40. void FunctionTerms_setCoefficient (FunctionTerms me, integer index, double value);
  41. double FunctionTerms_evaluate (FunctionTerms me, double x);
  42. void FunctionTerms_evaluate_z (FunctionTerms me, dcomplex *z, dcomplex *p);
  43. void FunctionTerms_evaluateTerms (FunctionTerms me, double x, double terms[]);
  44. void FunctionTerms_getExtrema (FunctionTerms me, double x1, double x2, double *xmin, double *ymin, double *xmax, double *ymax);
  45. integer FunctionTerms_getDegree (FunctionTerms me);
  46. double FunctionTerms_getMinimum (FunctionTerms me, double x1, double x2);
  47. double FunctionTerms_getXOfMinimum (FunctionTerms me, double x1, double x2);
  48. double FunctionTerms_getMaximum (FunctionTerms me, double x1, double x2);
  49. double FunctionTerms_getXOfMaximum (FunctionTerms me, double x1, double x2);
  50. /*
  51. Returns minimum and maximum function values (ymin, ymax) in
  52. interval [x1, x2] and their x-values (xmin, xmax).
  53. Precondition: [x1, x2] is a (sub)domain
  54. my xmin <= x1 < x2 <= my xmax
  55. */
  56. void FunctionTerms_draw (FunctionTerms me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  57. int extrapolate, int garnish);
  58. /*
  59. Extrapolate only for functions whose domain is extendable and that can be extrapolated.
  60. Polynomials can be extrapolated.
  61. LegendreSeries and ChebyshevSeries cannot be extrapolated.
  62. */
  63. void FunctionTerms_drawBasisFunction (FunctionTerms me, Graphics g, integer index, double xmin, double xmax,
  64. double ymin, double ymax, int extrapolate, int garnish);
  65. Thing_define (Polynomial, FunctionTerms) {
  66. // overridden methods:
  67. public:
  68. virtual double v_evaluate (double x);
  69. virtual dcomplex v_evaluate_z (dcomplex z);
  70. virtual void v_evaluateTerms (double x, double terms[]);
  71. virtual void v_getExtrema (double x1, double x2, double *xmin, double *ymin, double *xmax, double *ymax);
  72. //virtual integer v_getDegree (); David, is het OK dat deze niet overschreven wordt? Ja
  73. };
  74. autoPolynomial Polynomial_create (double xmin, double xmax, integer degree);
  75. autoPolynomial Polynomial_createFromString (double xmin, double xmax, conststring32 s);
  76. void Polynomial_scaleCoefficients_monic (Polynomial me);
  77. /* Make coefficent of leading term 1.0 */
  78. autoPolynomial Polynomial_scaleX (Polynomial me, double xmin, double xmax);
  79. /* x' = (x-location) / scale */
  80. dcomplex Polynomial_evaluate_z (Polynomial me, dcomplex z);
  81. /* Evaluate at complex z = x + iy */
  82. /* Product (i=1; numberOfTerms; (1 + a*x + x^2)
  83. * Precondition : my numberOfCoeffcients >= 3+2*numberOfOmegas
  84. * Polynomial is uses as a "buffer". We define it one and reuse it
  85. */
  86. void Polynomial_initFromProductOfSecondOrderTerms (Polynomial me, constVEC a);
  87. autoPolynomial Polynomial_createFromProductOfSecondOrderTermsString (double xmin, double xmax, conststring32 s);
  88. void Polynomial_initFromRealRoots (Polynomial me, constVEC roots);
  89. autoPolynomial Polynomial_createFromRealRootsString (double xmin, double xmax, conststring32 s);
  90. double Polynomial_getArea (Polynomial me, double xmin, double xmax);
  91. autoPolynomial Polynomial_getDerivative (Polynomial me);
  92. autoPolynomial Polynomial_getPrimitive (Polynomial me, double constant);
  93. void Polynomial_draw (Polynomial me, Graphics g, double xmin, double xmax, double ymin, double ymax, int garnish);
  94. double Polynomial_evaluate (Polynomial me, double x);
  95. void Polynomial_evaluateWithDerivative (Polynomial me, double x, double *fx, double *dfx);
  96. void Polynomial_evaluateDerivatives (Polynomial me, double x, double *derivatives /*[0.. numberOfDerivatives]*/, integer numberOfDerivatives);
  97. /* derivatives[0] = Polynomial_evaluate (me, x); */
  98. void Polynomial_evaluateTerms (Polynomial me, double x, double terms[]);
  99. autoPolynomial Polynomials_multiply (Polynomial me, Polynomial thee);
  100. void Polynomial_multiply_firstOrderFactor (Polynomial me, double factor);
  101. /* P(x) * (x-factor)
  102. * Postcondition: my numberOfCoefficients = old_numberOfCoefficients + 1
  103. */
  104. void Polynomial_multiply_secondOrderFactor (Polynomial me, double factor);
  105. /* P(x) * (x^2 - a)
  106. * Postcondition: my numberOfCoefficients = old_numberOfCoefficients + 2
  107. */
  108. void Polynomials_divide (Polynomial me, Polynomial thee, autoPolynomial *q, autoPolynomial *r);
  109. void Polynomial_divide_firstOrderFactor (Polynomial me, double factor, double *p_remainder); // P(x)/(x-a)
  110. /* Functions: calculate coefficients of new polynomial P(x)/(x-a)
  111. * if p_remainder != nullptr it will contain
  112. * remainder after dividing by monomial factor x-a.
  113. * `undefined` if my numberOfCoefficients == 1 (error condition)
  114. * Postcondition: my numberOfCoefficients reduced by 1
  115. */
  116. void Polynomial_divide_secondOrderFactor (Polynomial me, double factor);
  117. /* P(x) / (x^2 - a)
  118. * Postcondition: my numberOfCoefficients = old_numberOfCoefficients - 2
  119. */
  120. Thing_define (LegendreSeries, FunctionTerms) {
  121. // overridden methods:
  122. public:
  123. virtual double v_evaluate (double x);
  124. virtual void v_evaluateTerms (double x, double terms[]);
  125. virtual void v_getExtrema (double x1, double x2, double *xmin, double *ymin, double *xmax, double *ymax);
  126. };
  127. autoLegendreSeries LegendreSeries_create (double xmin, double xmax, integer numberOfPolynomials);
  128. autoLegendreSeries LegendreSeries_createFromString (double xmin, double xmax, conststring32 s);
  129. autoLegendreSeries LegendreSeries_getDerivative (LegendreSeries me);
  130. autoPolynomial LegendreSeries_to_Polynomial (LegendreSeries me);
  131. Thing_define (Roots, ComplexVector) {
  132. };
  133. autoRoots Roots_create (integer numberOfRoots);
  134. void Roots_fixIntoUnitCircle (Roots me);
  135. void Roots_sort (Roots me);
  136. /* Sort to size of real part a+bi, a-bi*/
  137. dcomplex Roots_evaluate_z (Roots me, dcomplex z);
  138. autoRoots Polynomial_to_Roots_ev (Polynomial me);
  139. integer Roots_getNumberOfRoots (Roots me);
  140. void Roots_draw (Roots me, Graphics g, double rmin, double rmax, double imin, double imax,
  141. conststring32 symbol, int fontSize, bool garnish);
  142. dcomplex Roots_getRoot (Roots me, integer index);
  143. void Roots_setRoot (Roots me, integer index, double re, double im);
  144. autoSpectrum Roots_to_Spectrum (Roots me, double nyquistFrequency, integer numberOfFrequencies, double radius);
  145. autoRoots Polynomial_to_Roots (Polynomial me);
  146. /* Find roots of polynomial and polish them */
  147. double Polynomial_findOneSimpleRealRoot_nr (Polynomial me, double xmin, double xmax);
  148. double Polynomial_findOneSimpleRealRoot_ridders (Polynomial me, double xmin, double xmax);
  149. /* Preconditions: there must be exactly one root in the [xmin, xmax] interval;
  150. * Root will be found by newton-raphson with bisecting
  151. */
  152. void Roots_Polynomial_polish (Roots me, Polynomial thee);
  153. autoPolynomial Roots_to_Polynomial (Roots me, bool rootsAreReal);
  154. autoPolynomial TableOfReal_to_Polynomial (TableOfReal me, integer degree, integer xcol, integer ycol, integer scol);
  155. autoLegendreSeries TableOfReal_to_LegendreSeries (TableOfReal me, integer numberOfPolynomials, integer xcol, integer ycol, integer scol);
  156. autoSpectrum Polynomial_to_Spectrum (Polynomial me, double nyquistFrequency, integer numberOfFrequencies, double radius);
  157. /*
  158. A ChebyshevSeries p(x) on a domain [xmin,xmax] is defined as the
  159. following linear combination of Chebyshev polynomials T[k](x') of
  160. degree k-1 and domain [-1, 1]:
  161. p(x) = sum (k=1..numberOfCoefficients, c[k]*T[k](x')) - c[1] / 2, where
  162. x' = (2 * x - xmin - xmax) / (xmax - xmin)
  163. This is equivalent to:
  164. p(x) = c[1] /2 + sum (k=2..numberOfCoefficients, c[k]*T[k](x'))
  165. */
  166. Thing_define (ChebyshevSeries, FunctionTerms) {
  167. // overridden methods:
  168. public:
  169. virtual double v_evaluate (double x);
  170. virtual void v_evaluateTerms (double x, double terms[]);
  171. virtual void v_getExtrema (double x1, double x2, double *xmin, double *ymin, double *xmax, double *ymax);
  172. };
  173. autoChebyshevSeries ChebyshevSeries_create (double xmin, double xmax, integer numberOfPolynomials);
  174. autoChebyshevSeries ChebyshevSeries_createFromString (double xmin, double xmax, conststring32 s);
  175. autoPolynomial ChebyshevSeries_to_Polynomial (ChebyshevSeries me);
  176. void Spline_init (Spline me, double xmin, double xmax, integer degree, integer numberOfCoefficients, integer numberOfKnots);
  177. integer Spline_getOrder (Spline me);
  178. void Spline_drawKnots (Spline me, Graphics g, double xmin, double xmax, double ymin, double ymax, int garnish);
  179. autoSpline Spline_scaleX (Spline me, double xmin, double xmax);
  180. /* scale domain and knots to new domain */
  181. Thing_define (MSpline, Spline) {
  182. // overridden methods:
  183. public:
  184. virtual double v_evaluate (double x);
  185. virtual void v_evaluateTerms (double x, double terms[]);
  186. };
  187. autoMSpline MSpline_create (double xmin, double xmax, integer degree, integer numberOfInteriorKnots);
  188. autoMSpline MSpline_createFromStrings (double xmin, double xmax, integer degree, conststring32 coef, conststring32 interiorKnots);
  189. Thing_define (ISpline, Spline) {
  190. // overridden methods:
  191. public:
  192. virtual double v_evaluate (double x);
  193. virtual void v_evaluateTerms (double x, double terms[]);
  194. virtual integer v_getOrder ();
  195. };
  196. autoISpline ISpline_create (double xmin, double xmax, integer degree, integer numberOfInteriorKnots);
  197. autoISpline ISpline_createFromStrings (double xmin, double xmax, integer degree, conststring32 coef, conststring32 interiorKnots);
  198. /****************** fit **********************************************/
  199. void FunctionTerms_RealTier_fit (FunctionTerms me, RealTier thee, int freezeCoefficients[], double tol, int ic, autoCovariance *c);
  200. autoPolynomial RealTier_to_Polynomial (RealTier me, integer degree, double tol, int ic, autoCovariance *cvm);
  201. autoLegendreSeries RealTier_to_LegendreSeries (RealTier me, integer degree, double tol, int ic, autoCovariance *cvm);
  202. autoChebyshevSeries RealTier_to_ChebyshevSeries (RealTier me, integer degree, double tol, int ic, autoCovariance *cvm);
  203. #endif /* _Polynomial_h_ */