| 1 | #ifndef _Polynomial_h_ |
| 2 | #define _Polynomial_h_ |
| 3 | /* Polynomial.h |
| 4 | * |
| 5 | * Copyright (C) 1993-2020 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 | /* |
| 22 | djmw 20020813 GPL header |
| 23 | djmw 20110306 Latest modification. |
| 24 | */ |
| 25 | |
| 26 | #include "FunctionSeries.h" |
| 27 | #include "Graphics.h" |
| 28 | |
| 29 | Thing_define (Polynomial, FunctionSeries) { |
| 30 | // overridden methods: |
| 31 | public: |
| 32 | virtual double v_evaluate (double x); |
| 33 | virtual dcomplex v_evaluate_z (dcomplex z); |
| 34 | virtual void v_evaluateTerms (double x, VEC terms); |
| 35 | virtual void v_getExtrema (double x1, double x2, double *xmin, double *ymin, double *xmax, double *ymax); |
| 36 | }; |
| 37 | |
| 38 | autoPolynomial Polynomial_create (double xmin, double xmax, integer degree); |
| 39 | |
| 40 | autoPolynomial Polynomial_createWithCoefficients (double xmin, double xmax, constVECVU const& coefficients); |
| 41 | |
| 42 | void Polynomial_scaleCoefficients_monic (mutablePolynomial me); |
| 43 | /* Make coefficent of leading term 1.0 */ |
| 44 | |
| 45 | autoPolynomial Polynomial_scaleX (mutablePolynomial me, double xmin, double xmax); |
| 46 | /* x' = (x-location) / scale */ |
| 47 | |
| 48 | dcomplex Polynomial_evaluate_z (Polynomial me, dcomplex z); |
| 49 | /* Evaluate at complex z = x + iy */ |
| 50 | |
| 51 | |
| 52 | /* Product (i=1; numberOfTerms; (1 + a*x + x^2) |
| 53 | * Precondition : my numberOfCoeffcients >= 3+2*numberOfOmegas |
| 54 | * Polynomial is uses as a "buffer". We define it one and reuse it |
| 55 | */ |
| 56 | void Polynomial_initFromProductOfSecondOrderTerms (Polynomial me, constVECVU const& a); |
| 57 | autoPolynomial Polynomial_createFromProductOfSecondOrderTerms (double xmin, double xmax, constVECVU const& a); |
| 58 | |
| 59 | void Polynomial_initFromRealRoots (Polynomial me, constVECVU const& roots); |
| 60 | autoPolynomial Polynomial_createFromRealRoots (double xmin, double xmax, constVECVU const& roots); |
| 61 | |
| 62 | double Polynomial_getArea (Polynomial me, double xmin, double xmax); |
| 63 | |
| 64 | autoPolynomial Polynomial_getDerivative (Polynomial me); |
| 65 | |
| 66 | autoPolynomial Polynomial_getPrimitive (Polynomial me, double constant); |
| 67 | |
| 68 | void Polynomial_draw (Polynomial me, Graphics g, double xmin, double xmax, double ymin, double ymax, bool garnish); |
| 69 | |
| 70 | double Polynomial_evaluate (Polynomial me, double x); |
| 71 | |
| 72 | void Polynomial_evaluateWithDerivative (constPolynomial me, double x, double *fx, double *dfx); |
| 73 | |
| 74 | autoVEC Polynomial_evaluateDerivatives (constPolynomial me, double x, long numberOfDerivatives); |
| 75 | /* result [1] is function value at x, result [2..numberOfDerivatives+1] are derivatives at x. */ |
| 76 | |
| 77 | void Polynomial_evaluateTerms (Polynomial me, double x, double terms []); |
| 78 | |
| 79 | autoPolynomial Polynomials_multiply (Polynomial me, Polynomial thee); |
| 80 | |
| 81 | void Polynomial_multiply_firstOrderFactor (Polynomial me, double factor); |
| 82 | /* P(x) * (x-factor) |
| 83 | * Postcondition: my numberOfCoefficients = old_numberOfCoefficients + 1 |
| 84 | */ |
| 85 | |
| 86 | void Polynomial_multiply_secondOrderFactor (Polynomial me, double factor); |
| 87 | /* P(x) * (x^2 - a) |
| 88 | * Postcondition: my numberOfCoefficients = old_numberOfCoefficients + 2 |
| 89 | */ |
| 90 | |
| 91 | void Polynomials_divide (Polynomial me, Polynomial thee, autoPolynomial *out_q, autoPolynomial *out_r); |
| 92 | |
| 93 | void Polynomial_divide_firstOrderFactor (Polynomial me, double factor, double *p_remainder); // P(x)/(x-a) |
| 94 | /* Functions: calculate coefficients of new polynomial P(x)/(x-a) |
| 95 | * if p_remainder != nullptr it will contain |
| 96 | * remainder after dividing by monomial factor x-a. |
| 97 | * `undefined` if my numberOfCoefficients == 1 (error condition) |
| 98 | * Postcondition: my numberOfCoefficients reduced by 1 |
| 99 | */ |
| 100 | |
| 101 | void Polynomial_divide_secondOrderFactor (Polynomial me, double factor); |
| 102 | /* P(x) / (x^2 - a) |
| 103 | * Postcondition: my numberOfCoefficients = old_numberOfCoefficients - 2 |
| 104 | */ |
| 105 | |
| 106 | double Polynomial_findOneSimpleRealRoot_nr (Polynomial me, double xmin, double xmax); |
| 107 | double Polynomial_findOneSimpleRealRoot_ridders (Polynomial me, double xmin, double xmax); |
| 108 | /* Preconditions: there must be exactly one root in the [xmin, xmax] interval; |
| 109 | * Root will be found by newton-raphson with bisecting |
| 110 | */ |
| 111 | |
| 112 | #endif /* _Polynomial_h_ */ |