| 1 | /* poly/gsl_poly.h |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2007 Brian Gough |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifndef __GSL_POLY_H__ |
| 21 | #define __GSL_POLY_H__ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include "gsl_complex.h" |
| 25 | |
| 26 | #undef __BEGIN_DECLS |
| 27 | #undef __END_DECLS |
| 28 | #ifdef __cplusplus |
| 29 | # define __BEGIN_DECLS extern "C" { |
| 30 | # define __END_DECLS } |
| 31 | #else |
| 32 | # define __BEGIN_DECLS /* empty */ |
| 33 | # define __END_DECLS /* empty */ |
| 34 | #endif |
| 35 | |
| 36 | __BEGIN_DECLS |
| 37 | |
| 38 | |
| 39 | /* Evaluate polynomial |
| 40 | * |
| 41 | * c[0] + c[1] x + c[2] x^2 + ... + c[len-1] x^(len-1) |
| 42 | * |
| 43 | * exceptions: none |
| 44 | */ |
| 45 | double gsl_poly_eval(const double c[], const int len, const double x); |
| 46 | |
| 47 | |
| 48 | #ifdef HAVE_INLINE |
| 49 | extern inline |
| 50 | double |
| 51 | gsl_poly_eval(const double c[], const int len, const double x) |
| 52 | { |
| 53 | int i; |
| 54 | double ans = c[len-1]; |
| 55 | for(i=len-1; i>0; i--) ans = c[i-1] + x * ans; |
| 56 | return ans; |
| 57 | } |
| 58 | #endif /* HAVE_INLINE */ |
| 59 | |
| 60 | /* Work with divided-difference polynomials, Abramowitz & Stegun 25.2.26 */ |
| 61 | |
| 62 | int |
| 63 | gsl_poly_dd_init (double dd[], const double x[], const double y[], |
| 64 | size_t size); |
| 65 | |
| 66 | double |
| 67 | gsl_poly_dd_eval (const double dd[], const double xa[], const size_t size, const double x); |
| 68 | |
| 69 | #ifdef HAVE_INLINE |
| 70 | extern inline |
| 71 | double |
| 72 | gsl_poly_dd_eval(const double dd[], const double xa[], const size_t size, const double x) |
| 73 | { |
| 74 | size_t i; |
| 75 | double y = dd[size - 1]; |
| 76 | for (i = size - 1; i--;) y = dd[i] + (x - xa[i]) * y; |
| 77 | return y; |
| 78 | } |
| 79 | #endif /* HAVE_INLINE */ |
| 80 | |
| 81 | |
| 82 | int |
| 83 | gsl_poly_dd_taylor (double c[], double xp, |
| 84 | const double dd[], const double x[], size_t size, |
| 85 | double w[]); |
| 86 | |
| 87 | /* Solve for real or complex roots of the standard quadratic equation, |
| 88 | * returning the number of real roots. |
| 89 | * |
| 90 | * Roots are returned ordered. |
| 91 | */ |
| 92 | int gsl_poly_solve_quadratic (double a, double b, double c, |
| 93 | double * x0, double * x1); |
| 94 | |
| 95 | int |
| 96 | gsl_poly_complex_solve_quadratic (double a, double b, double c, |
| 97 | gsl_complex * z0, gsl_complex * z1); |
| 98 | |
| 99 | |
| 100 | /* Solve for real roots of the cubic equation |
| 101 | * x^3 + a x^2 + b x + c = 0, returning the |
| 102 | * number of real roots. |
| 103 | * |
| 104 | * Roots are returned ordered. |
| 105 | */ |
| 106 | int gsl_poly_solve_cubic (double a, double b, double c, |
| 107 | double * x0, double * x1, double * x2); |
| 108 | |
| 109 | int |
| 110 | gsl_poly_complex_solve_cubic (double a, double b, double c, |
| 111 | gsl_complex * z0, gsl_complex * z1, |
| 112 | gsl_complex * z2); |
| 113 | |
| 114 | |
| 115 | /* Solve for the complex roots of a general real polynomial */ |
| 116 | |
| 117 | typedef struct |
| 118 | { |
| 119 | size_t nc ; |
| 120 | double * matrix ; |
| 121 | } |
| 122 | gsl_poly_complex_workspace ; |
| 123 | |
| 124 | gsl_poly_complex_workspace * gsl_poly_complex_workspace_alloc (size_t n); |
| 125 | void gsl_poly_complex_workspace_free (gsl_poly_complex_workspace * w); |
| 126 | |
| 127 | int |
| 128 | gsl_poly_complex_solve (const double * a, size_t n, |
| 129 | gsl_poly_complex_workspace * w, |
| 130 | gsl_complex_packed_ptr z); |
| 131 | |
| 132 | __END_DECLS |
| 133 | |
| 134 | #endif /* __GSL_POLY_H__ */ |