CubicRealPolynomial.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import DeveloperError from "./DeveloperError.js";
  2. import QuadraticRealPolynomial from "./QuadraticRealPolynomial.js";
  3. /**
  4. * Defines functions for 3rd order polynomial functions of one variable with only real coefficients.
  5. *
  6. * @namespace CubicRealPolynomial
  7. */
  8. const CubicRealPolynomial = {};
  9. /**
  10. * Provides the discriminant of the cubic equation from the supplied coefficients.
  11. *
  12. * @param {number} a The coefficient of the 3rd order monomial.
  13. * @param {number} b The coefficient of the 2nd order monomial.
  14. * @param {number} c The coefficient of the 1st order monomial.
  15. * @param {number} d The coefficient of the 0th order monomial.
  16. * @returns {number} The value of the discriminant.
  17. */
  18. CubicRealPolynomial.computeDiscriminant = function (a, b, c, d) {
  19. //>>includeStart('debug', pragmas.debug);
  20. if (typeof a !== "number") {
  21. throw new DeveloperError("a is a required number.");
  22. }
  23. if (typeof b !== "number") {
  24. throw new DeveloperError("b is a required number.");
  25. }
  26. if (typeof c !== "number") {
  27. throw new DeveloperError("c is a required number.");
  28. }
  29. if (typeof d !== "number") {
  30. throw new DeveloperError("d is a required number.");
  31. }
  32. //>>includeEnd('debug');
  33. const a2 = a * a;
  34. const b2 = b * b;
  35. const c2 = c * c;
  36. const d2 = d * d;
  37. const discriminant =
  38. 18.0 * a * b * c * d +
  39. b2 * c2 -
  40. 27.0 * a2 * d2 -
  41. 4.0 * (a * c2 * c + b2 * b * d);
  42. return discriminant;
  43. };
  44. function computeRealRoots(a, b, c, d) {
  45. const A = a;
  46. const B = b / 3.0;
  47. const C = c / 3.0;
  48. const D = d;
  49. const AC = A * C;
  50. const BD = B * D;
  51. const B2 = B * B;
  52. const C2 = C * C;
  53. const delta1 = A * C - B2;
  54. const delta2 = A * D - B * C;
  55. const delta3 = B * D - C2;
  56. const discriminant = 4.0 * delta1 * delta3 - delta2 * delta2;
  57. let temp;
  58. let temp1;
  59. if (discriminant < 0.0) {
  60. let ABar;
  61. let CBar;
  62. let DBar;
  63. if (B2 * BD >= AC * C2) {
  64. ABar = A;
  65. CBar = delta1;
  66. DBar = -2.0 * B * delta1 + A * delta2;
  67. } else {
  68. ABar = D;
  69. CBar = delta3;
  70. DBar = -D * delta2 + 2.0 * C * delta3;
  71. }
  72. const s = DBar < 0.0 ? -1.0 : 1.0; // This is not Math.Sign()!
  73. const temp0 = -s * Math.abs(ABar) * Math.sqrt(-discriminant);
  74. temp1 = -DBar + temp0;
  75. const x = temp1 / 2.0;
  76. const p = x < 0.0 ? -Math.pow(-x, 1.0 / 3.0) : Math.pow(x, 1.0 / 3.0);
  77. const q = temp1 === temp0 ? -p : -CBar / p;
  78. temp = CBar <= 0.0 ? p + q : -DBar / (p * p + q * q + CBar);
  79. if (B2 * BD >= AC * C2) {
  80. return [(temp - B) / A];
  81. }
  82. return [-D / (temp + C)];
  83. }
  84. const CBarA = delta1;
  85. const DBarA = -2.0 * B * delta1 + A * delta2;
  86. const CBarD = delta3;
  87. const DBarD = -D * delta2 + 2.0 * C * delta3;
  88. const squareRootOfDiscriminant = Math.sqrt(discriminant);
  89. const halfSquareRootOf3 = Math.sqrt(3.0) / 2.0;
  90. let theta = Math.abs(Math.atan2(A * squareRootOfDiscriminant, -DBarA) / 3.0);
  91. temp = 2.0 * Math.sqrt(-CBarA);
  92. let cosine = Math.cos(theta);
  93. temp1 = temp * cosine;
  94. let temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  95. const numeratorLarge = temp1 + temp3 > 2.0 * B ? temp1 - B : temp3 - B;
  96. const denominatorLarge = A;
  97. const root1 = numeratorLarge / denominatorLarge;
  98. theta = Math.abs(Math.atan2(D * squareRootOfDiscriminant, -DBarD) / 3.0);
  99. temp = 2.0 * Math.sqrt(-CBarD);
  100. cosine = Math.cos(theta);
  101. temp1 = temp * cosine;
  102. temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  103. const numeratorSmall = -D;
  104. const denominatorSmall = temp1 + temp3 < 2.0 * C ? temp1 + C : temp3 + C;
  105. const root3 = numeratorSmall / denominatorSmall;
  106. const E = denominatorLarge * denominatorSmall;
  107. const F =
  108. -numeratorLarge * denominatorSmall - denominatorLarge * numeratorSmall;
  109. const G = numeratorLarge * numeratorSmall;
  110. const root2 = (C * F - B * G) / (-B * F + C * E);
  111. if (root1 <= root2) {
  112. if (root1 <= root3) {
  113. if (root2 <= root3) {
  114. return [root1, root2, root3];
  115. }
  116. return [root1, root3, root2];
  117. }
  118. return [root3, root1, root2];
  119. }
  120. if (root1 <= root3) {
  121. return [root2, root1, root3];
  122. }
  123. if (root2 <= root3) {
  124. return [root2, root3, root1];
  125. }
  126. return [root3, root2, root1];
  127. }
  128. /**
  129. * Provides the real valued roots of the cubic polynomial with the provided coefficients.
  130. *
  131. * @param {number} a The coefficient of the 3rd order monomial.
  132. * @param {number} b The coefficient of the 2nd order monomial.
  133. * @param {number} c The coefficient of the 1st order monomial.
  134. * @param {number} d The coefficient of the 0th order monomial.
  135. * @returns {number[]} The real valued roots.
  136. */
  137. CubicRealPolynomial.computeRealRoots = function (a, b, c, d) {
  138. //>>includeStart('debug', pragmas.debug);
  139. if (typeof a !== "number") {
  140. throw new DeveloperError("a is a required number.");
  141. }
  142. if (typeof b !== "number") {
  143. throw new DeveloperError("b is a required number.");
  144. }
  145. if (typeof c !== "number") {
  146. throw new DeveloperError("c is a required number.");
  147. }
  148. if (typeof d !== "number") {
  149. throw new DeveloperError("d is a required number.");
  150. }
  151. //>>includeEnd('debug');
  152. let roots;
  153. let ratio;
  154. if (a === 0.0) {
  155. // Quadratic function: b * x^2 + c * x + d = 0.
  156. return QuadraticRealPolynomial.computeRealRoots(b, c, d);
  157. } else if (b === 0.0) {
  158. if (c === 0.0) {
  159. if (d === 0.0) {
  160. // 3rd order monomial: a * x^3 = 0.
  161. return [0.0, 0.0, 0.0];
  162. }
  163. // a * x^3 + d = 0
  164. ratio = -d / a;
  165. const root =
  166. ratio < 0.0 ? -Math.pow(-ratio, 1.0 / 3.0) : Math.pow(ratio, 1.0 / 3.0);
  167. return [root, root, root];
  168. } else if (d === 0.0) {
  169. // x * (a * x^2 + c) = 0.
  170. roots = QuadraticRealPolynomial.computeRealRoots(a, 0, c);
  171. // Return the roots in ascending order.
  172. if (roots.Length === 0) {
  173. return [0.0];
  174. }
  175. return [roots[0], 0.0, roots[1]];
  176. }
  177. // Deflated cubic polynomial: a * x^3 + c * x + d= 0.
  178. return computeRealRoots(a, 0, c, d);
  179. } else if (c === 0.0) {
  180. if (d === 0.0) {
  181. // x^2 * (a * x + b) = 0.
  182. ratio = -b / a;
  183. if (ratio < 0.0) {
  184. return [ratio, 0.0, 0.0];
  185. }
  186. return [0.0, 0.0, ratio];
  187. }
  188. // a * x^3 + b * x^2 + d = 0.
  189. return computeRealRoots(a, b, 0, d);
  190. } else if (d === 0.0) {
  191. // x * (a * x^2 + b * x + c) = 0
  192. roots = QuadraticRealPolynomial.computeRealRoots(a, b, c);
  193. // Return the roots in ascending order.
  194. if (roots.length === 0) {
  195. return [0.0];
  196. } else if (roots[1] <= 0.0) {
  197. return [roots[0], roots[1], 0.0];
  198. } else if (roots[0] >= 0.0) {
  199. return [0.0, roots[0], roots[1]];
  200. }
  201. return [roots[0], 0.0, roots[1]];
  202. }
  203. return computeRealRoots(a, b, c, d);
  204. };
  205. export default CubicRealPolynomial;