| 1 |
/* poly/zsolve.c |
| 2 |
* |
| 3 |
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 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 |
/* zsolve.c - finds the complex roots of = 0 */ |
| 21 |
|
| 22 |
#include "gsl__config.h" |
| 23 |
#include <math.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include "gsl_math.h" |
| 26 |
#include "gsl_errno.h" |
| 27 |
#include "gsl_complex.h" |
| 28 |
#include "gsl_poly.h" |
| 29 |
|
| 30 |
/* C-style matrix elements */ |
| 31 |
#define MAT(m,i,j,n) ((m)[(i)*(n) + (j)]) |
| 32 |
|
| 33 |
/* Fortran-style matrix elements */ |
| 34 |
#define FMAT(m,i,j,n) ((m)[((i)-1)*(n) + ((j)-1)]) |
| 35 |
|
| 36 |
#include "gsl_poly__companion.c" |
| 37 |
#include "gsl_poly__balance.c" |
| 38 |
#include "gsl_poly__qr.c" |
| 39 |
|
| 40 |
int |
| 41 |
gsl_poly_complex_solve (const double *a, size_t n, |
| 42 |
gsl_poly_complex_workspace * w, |
| 43 |
gsl_complex_packed_ptr z) |
| 44 |
{ |
| 45 |
int status; |
| 46 |
double *m; |
| 47 |
|
| 48 |
if (n == 0) |
| 49 |
{ |
| 50 |
GSL_ERROR ("number of terms must be a positive integer", GSL_EINVAL); |
| 51 |
} |
| 52 |
|
| 53 |
if (n == 1) |
| 54 |
{ |
| 55 |
GSL_ERROR ("cannot solve for only one term", GSL_EINVAL); |
| 56 |
} |
| 57 |
|
| 58 |
if (a[n - 1] == 0) |
| 59 |
{ |
| 60 |
GSL_ERROR ("leading term of polynomial must be non-zero", GSL_EINVAL) ; |
| 61 |
} |
| 62 |
|
| 63 |
if (w->nc != n - 1) |
| 64 |
{ |
| 65 |
GSL_ERROR ("size of workspace does not match polynomial", GSL_EINVAL); |
| 66 |
} |
| 67 |
|
| 68 |
m = w->matrix; |
| 69 |
|
| 70 |
set_companion_matrix (a, n - 1, m); |
| 71 |
|
| 72 |
balance_companion_matrix (m, n - 1); |
| 73 |
|
| 74 |
status = qr_companion (m, n - 1, z); |
| 75 |
|
| 76 |
if (status) |
| 77 |
{ |
| 78 |
GSL_ERROR("root solving qr method failed to converge", GSL_EFAILED); |
| 79 |
} |
| 80 |
|
| 81 |
return GSL_SUCCESS; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|