Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:03:52

0001 /* poly/gsl_poly.h
0002  * 
0003  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2007 Brian Gough
0004  * 
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 3 of the License, or (at
0008  * your option) any later version.
0009  * 
0010  * This program is distributed in the hope that it will be useful, but
0011  * WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * General Public License for more details.
0014  * 
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #ifndef __GSL_POLY_H__
0021 #define __GSL_POLY_H__
0022 
0023 #include <stdlib.h>
0024 #include <gsl/gsl_inline.h>
0025 #include <gsl/gsl_complex.h>
0026 
0027 #undef __BEGIN_DECLS
0028 #undef __END_DECLS
0029 #ifdef __cplusplus
0030 # define __BEGIN_DECLS extern "C" {
0031 # define __END_DECLS }
0032 #else
0033 # define __BEGIN_DECLS /* empty */
0034 # define __END_DECLS /* empty */
0035 #endif
0036 
0037 __BEGIN_DECLS
0038 
0039 
0040 /* Evaluate polynomial
0041  *
0042  * c[0] + c[1] x + c[2] x^2 + ... + c[len-1] x^(len-1)
0043  *
0044  * exceptions: none
0045  */
0046 
0047 /* real polynomial, real x */
0048 INLINE_DECL double gsl_poly_eval(const double c[], const int len, const double x);
0049 
0050 /* real polynomial, complex x */
0051 INLINE_DECL gsl_complex gsl_poly_complex_eval (const double c [], const int len, const gsl_complex z);
0052 
0053 /* complex polynomial, complex x */
0054 INLINE_DECL gsl_complex gsl_complex_poly_complex_eval (const gsl_complex c [], const int len, const gsl_complex z);
0055 
0056 int gsl_poly_eval_derivs(const double c[], const size_t lenc, const double x, double res[], const size_t lenres);
0057 
0058 #ifdef HAVE_INLINE
0059 INLINE_FUN
0060 double 
0061 gsl_poly_eval(const double c[], const int len, const double x)
0062 {
0063   int i;
0064   double ans = c[len-1];
0065   for(i=len-1; i>0; i--) ans = c[i-1] + x * ans;
0066   return ans;
0067 }
0068 
0069 INLINE_FUN
0070 gsl_complex
0071 gsl_poly_complex_eval(const double c[], const int len, const gsl_complex z)
0072 {
0073   int i;
0074   gsl_complex ans;
0075   GSL_SET_COMPLEX (&ans, c[len-1], 0.0);
0076   for(i=len-1; i>0; i--) {
0077     /* The following three lines are equivalent to
0078        ans = gsl_complex_add_real (gsl_complex_mul (z, ans), c[i-1]); 
0079        but faster */
0080     double tmp = c[i-1] + GSL_REAL (z) * GSL_REAL (ans) - GSL_IMAG (z) * GSL_IMAG (ans);
0081     GSL_SET_IMAG (&ans, GSL_IMAG (z) * GSL_REAL (ans) + GSL_REAL (z) * GSL_IMAG (ans));
0082     GSL_SET_REAL (&ans, tmp);
0083   } 
0084   return ans;
0085 }
0086 
0087 INLINE_FUN
0088 gsl_complex
0089 gsl_complex_poly_complex_eval(const gsl_complex c[], const int len, const gsl_complex z)
0090 {
0091   int i;
0092   gsl_complex ans = c[len-1];
0093   for(i=len-1; i>0; i--) {
0094     /* The following three lines are equivalent to
0095        ans = gsl_complex_add (c[i-1], gsl_complex_mul (x, ans));
0096        but faster */
0097     double tmp = GSL_REAL (c[i-1]) + GSL_REAL (z) * GSL_REAL (ans) - GSL_IMAG (z) * GSL_IMAG (ans);
0098     GSL_SET_IMAG (&ans, GSL_IMAG (c[i-1]) + GSL_IMAG (z) * GSL_REAL (ans) + GSL_REAL (z) * GSL_IMAG (ans));
0099     GSL_SET_REAL (&ans, tmp);
0100   }
0101   return ans;
0102 }
0103 #endif /* HAVE_INLINE */
0104 
0105 /* Work with divided-difference polynomials, Abramowitz & Stegun 25.2.26 */
0106 
0107 int
0108 gsl_poly_dd_init (double dd[], const double x[], const double y[],
0109                   size_t size);
0110 
0111 INLINE_DECL double
0112 gsl_poly_dd_eval (const double dd[], const double xa[], const size_t size, const double x);
0113 
0114 #ifdef HAVE_INLINE
0115 INLINE_FUN
0116 double 
0117 gsl_poly_dd_eval(const double dd[], const double xa[], const size_t size, const double x)
0118 {
0119   size_t i;
0120   double y = dd[size - 1];
0121   for (i = size - 1; i--;) y = dd[i] + (x - xa[i]) * y;
0122   return y;
0123 }
0124 #endif /* HAVE_INLINE */
0125 
0126 
0127 int
0128 gsl_poly_dd_taylor (double c[], double xp,
0129                     const double dd[], const double x[], size_t size,
0130                     double w[]);
0131 
0132 int
0133 gsl_poly_dd_hermite_init (double dd[], double z[], const double xa[], const double ya[],
0134                           const double dya[], const size_t size);
0135 
0136 /* Solve for real or complex roots of the standard quadratic equation,
0137  * returning the number of real roots.
0138  *
0139  * Roots are returned ordered.
0140  */
0141 int gsl_poly_solve_quadratic (double a, double b, double c, 
0142                               double * x0, double * x1);
0143 
0144 int 
0145 gsl_poly_complex_solve_quadratic (double a, double b, double c, 
0146                                   gsl_complex * z0, gsl_complex * z1);
0147 
0148 
0149 /* Solve for real roots of the cubic equation
0150  * x^3 + a x^2 + b x + c = 0, returning the
0151  * number of real roots.
0152  *
0153  * Roots are returned ordered.
0154  */
0155 int gsl_poly_solve_cubic (double a, double b, double c, 
0156                           double * x0, double * x1, double * x2);
0157 
0158 int 
0159 gsl_poly_complex_solve_cubic (double a, double b, double c, 
0160                               gsl_complex * z0, gsl_complex * z1, 
0161                               gsl_complex * z2);
0162 
0163 
0164 /* Solve for the complex roots of a general real polynomial */
0165 
0166 typedef struct 
0167 { 
0168   size_t nc ;
0169   double * matrix ; 
0170 } 
0171 gsl_poly_complex_workspace ;
0172 
0173 gsl_poly_complex_workspace * gsl_poly_complex_workspace_alloc (size_t n);
0174 void gsl_poly_complex_workspace_free (gsl_poly_complex_workspace * w);
0175 
0176 int
0177 gsl_poly_complex_solve (const double * a, size_t n, 
0178                         gsl_poly_complex_workspace * w,
0179                         gsl_complex_packed_ptr z);
0180 
0181 __END_DECLS
0182 
0183 #endif /* __GSL_POLY_H__ */