gsl_poly__zsolve.c 2.1 KB

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