gsl_poly__solve_cubic.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* poly/solve_cubic.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. /* solve_cubic.c - finds the real roots of x^3 + a x^2 + b x + c = 0 */
  20. #include "gsl__config.h"
  21. #include <math.h>
  22. #include "gsl_math.h"
  23. #include "gsl_poly.h"
  24. #define SWAP(a,b) do { double tmp = b ; b = a ; a = tmp ; } while(0)
  25. int
  26. gsl_poly_solve_cubic (double a, double b, double c,
  27. double *x0, double *x1, double *x2)
  28. {
  29. double q = (a * a - 3 * b);
  30. double r = (2 * a * a * a - 9 * a * b + 27 * c);
  31. double Q = q / 9;
  32. double R = r / 54;
  33. double Q3 = Q * Q * Q;
  34. double R2 = R * R;
  35. double CR2 = 729 * r * r;
  36. double CQ3 = 2916 * q * q * q;
  37. if (R == 0 && Q == 0)
  38. {
  39. *x0 = - a / 3 ;
  40. *x1 = - a / 3 ;
  41. *x2 = - a / 3 ;
  42. return 3 ;
  43. }
  44. else if (CR2 == CQ3)
  45. {
  46. /* this test is actually R2 == Q3, written in a form suitable
  47. for exact computation with integers */
  48. /* Due to finite precision some double roots may be missed, and
  49. considered to be a pair of complex roots z = x +/- epsilon i
  50. close to the real axis. */
  51. double sqrtQ = sqrt (Q);
  52. if (R > 0)
  53. {
  54. *x0 = -2 * sqrtQ - a / 3;
  55. *x1 = sqrtQ - a / 3;
  56. *x2 = sqrtQ - a / 3;
  57. }
  58. else
  59. {
  60. *x0 = - sqrtQ - a / 3;
  61. *x1 = - sqrtQ - a / 3;
  62. *x2 = 2 * sqrtQ - a / 3;
  63. }
  64. return 3 ;
  65. }
  66. else if (CR2 < CQ3) /* equivalent to R2 < Q3 */
  67. {
  68. double sqrtQ = sqrt (Q);
  69. double sqrtQ3 = sqrtQ * sqrtQ * sqrtQ;
  70. double theta = acos (R / sqrtQ3);
  71. double norm = -2 * sqrtQ;
  72. *x0 = norm * cos (theta / 3) - a / 3;
  73. *x1 = norm * cos ((theta + 2.0 * M_PI) / 3) - a / 3;
  74. *x2 = norm * cos ((theta - 2.0 * M_PI) / 3) - a / 3;
  75. /* Sort *x0, *x1, *x2 into increasing order */
  76. if (*x0 > *x1)
  77. SWAP(*x0, *x1) ;
  78. if (*x1 > *x2)
  79. {
  80. SWAP(*x1, *x2) ;
  81. if (*x0 > *x1)
  82. SWAP(*x0, *x1) ;
  83. }
  84. return 3;
  85. }
  86. else
  87. {
  88. double sgnR = (R >= 0 ? 1 : -1);
  89. double A = -sgnR * pow (fabs (R) + sqrt (R2 - Q3), 1.0/3.0);
  90. double B = Q / A ;
  91. *x0 = A + B - a / 3;
  92. return 1;
  93. }
  94. }