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