gsl_poly__zsolve_cubic.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* poly/zsolve_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. /* zsolve_cubic.c - finds the complex 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_complex.h"
  24. #include "gsl_poly.h"
  25. #define SWAP(a,b) do { double tmp = b ; b = a ; a = tmp ; } while(0)
  26. int
  27. gsl_poly_complex_solve_cubic (double a, double b, double c,
  28. gsl_complex *z0, gsl_complex *z1,
  29. gsl_complex *z2)
  30. {
  31. double q = (a * a - 3 * b);
  32. double r = (2 * a * a * a - 9 * a * b + 27 * c);
  33. double Q = q / 9;
  34. double R = r / 54;
  35. double Q3 = Q * Q * Q;
  36. double R2 = R * R;
  37. double CR2 = 729 * r * r;
  38. double CQ3 = 2916 * q * q * q;
  39. if (R == 0 && Q == 0)
  40. {
  41. GSL_REAL (*z0) = -a / 3;
  42. GSL_IMAG (*z0) = 0;
  43. GSL_REAL (*z1) = -a / 3;
  44. GSL_IMAG (*z1) = 0;
  45. GSL_REAL (*z2) = -a / 3;
  46. GSL_IMAG (*z2) = 0;
  47. return 3;
  48. }
  49. else if (CR2 == CQ3)
  50. {
  51. /* this test is actually R2 == Q3, written in a form suitable
  52. for exact computation with integers */
  53. /* Due to finite precision some double roots may be missed, and
  54. will be considered to be a pair of complex roots z = x +/-
  55. epsilon i close to the real axis. */
  56. double sqrtQ = sqrt (Q);
  57. if (R > 0)
  58. {
  59. GSL_REAL (*z0) = -2 * sqrtQ - a / 3;
  60. GSL_IMAG (*z0) = 0;
  61. GSL_REAL (*z1) = sqrtQ - a / 3;
  62. GSL_IMAG (*z1) = 0;
  63. GSL_REAL (*z2) = sqrtQ - a / 3;
  64. GSL_IMAG (*z2) = 0;
  65. }
  66. else
  67. {
  68. GSL_REAL (*z0) = -sqrtQ - a / 3;
  69. GSL_IMAG (*z0) = 0;
  70. GSL_REAL (*z1) = -sqrtQ - a / 3;
  71. GSL_IMAG (*z1) = 0;
  72. GSL_REAL (*z2) = 2 * sqrtQ - a / 3;
  73. GSL_IMAG (*z2) = 0;
  74. }
  75. return 3;
  76. }
  77. else if (CR2 < CQ3) /* equivalent to R2 < Q3 */
  78. {
  79. double sqrtQ = sqrt (Q);
  80. double sqrtQ3 = sqrtQ * sqrtQ * sqrtQ;
  81. double theta = acos (R / sqrtQ3);
  82. double norm = -2 * sqrtQ;
  83. double r0 = norm * cos (theta / 3) - a / 3;
  84. double r1 = norm * cos ((theta + 2.0 * M_PI) / 3) - a / 3;
  85. double r2 = norm * cos ((theta - 2.0 * M_PI) / 3) - a / 3;
  86. /* Sort r0, r1, r2 into increasing order */
  87. if (r0 > r1)
  88. SWAP (r0, r1);
  89. if (r1 > r2)
  90. {
  91. SWAP (r1, r2);
  92. if (r0 > r1)
  93. SWAP (r0, r1);
  94. }
  95. GSL_REAL (*z0) = r0;
  96. GSL_IMAG (*z0) = 0;
  97. GSL_REAL (*z1) = r1;
  98. GSL_IMAG (*z1) = 0;
  99. GSL_REAL (*z2) = r2;
  100. GSL_IMAG (*z2) = 0;
  101. return 3;
  102. }
  103. else
  104. {
  105. double sgnR = (R >= 0 ? 1 : -1);
  106. double A = -sgnR * pow (fabs (R) + sqrt (R2 - Q3), 1.0 / 3.0);
  107. double B = Q / A;
  108. if (A + B < 0)
  109. {
  110. GSL_REAL (*z0) = A + B - a / 3;
  111. GSL_IMAG (*z0) = 0;
  112. GSL_REAL (*z1) = -0.5 * (A + B) - a / 3;
  113. GSL_IMAG (*z1) = -(sqrt (3.0) / 2.0) * fabs(A - B);
  114. GSL_REAL (*z2) = -0.5 * (A + B) - a / 3;
  115. GSL_IMAG (*z2) = (sqrt (3.0) / 2.0) * fabs(A - B);
  116. }
  117. else
  118. {
  119. GSL_REAL (*z0) = -0.5 * (A + B) - a / 3;
  120. GSL_IMAG (*z0) = -(sqrt (3.0) / 2.0) * fabs(A - B);
  121. GSL_REAL (*z1) = -0.5 * (A + B) - a / 3;
  122. GSL_IMAG (*z1) = (sqrt (3.0) / 2.0) * fabs(A - B);
  123. GSL_REAL (*z2) = A + B - a / 3;
  124. GSL_IMAG (*z2) = 0;
  125. }
  126. return 3;
  127. }
  128. }