| 1 | // Copyright (c) 2015, Mikhail Vorotilov |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 18 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 20 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 21 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 22 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | |
| 25 | use super::super::FloatType; |
| 26 | use super::super::Roots; |
| 27 | |
| 28 | /// Solves a cubic equation a3*x^3 + a2*x^2 + a1*x + a0 = 0. |
| 29 | /// |
| 30 | /// General formula (complex numbers) is implemented for three roots. |
| 31 | /// |
| 32 | /// Note that very small values of a3 (comparing to other coefficients) will cause the loss of precision. |
| 33 | /// |
| 34 | /// In case more than one roots are present, they are returned in the increasing order. |
| 35 | /// |
| 36 | /// # Examples |
| 37 | /// |
| 38 | /// ``` |
| 39 | /// use roots::Roots; |
| 40 | /// use roots::find_roots_cubic; |
| 41 | /// |
| 42 | /// let no_roots = find_roots_cubic(0f32, 1f32, 0f32, 1f32); |
| 43 | /// // Returns Roots::No([]) as 'x^2 + 1 = 0' has no roots |
| 44 | /// |
| 45 | /// let one_root = find_roots_cubic(1f64, 0f64, 0f64, 0f64); |
| 46 | /// // Returns Roots::One([0f64]) as 'x^3 = 0' has one root 0 |
| 47 | /// |
| 48 | /// let three_roots = find_roots_cubic(1f32, 0f32, -1f32, 0f32); |
| 49 | /// // Returns Roots::Three([-1f32, 0f32, 1f32]) as 'x^3 - x = 0' has roots -1, 0, and 1 |
| 50 | /// |
| 51 | /// let three_roots_less_precision = find_roots_cubic( |
| 52 | /// -0.000000000000000040410628481035f64, |
| 53 | /// 0.0126298310280606f64, |
| 54 | /// -0.100896606408756f64, |
| 55 | /// 0.0689539597036461f64); |
| 56 | /// // Returns Roots::Three([0.7583841816097057f64, 7.233267996296344f64, 312537357195212.9f64]) |
| 57 | /// // while online math expects 0.7547108770537f64, 7.23404258961f64, 312537357195213f64 |
| 58 | /// ``` |
| 59 | pub fn find_roots_cubic<F: FloatType>(a3: F, a2: F, a1: F, a0: F) -> Roots<F> { |
| 60 | // Handle non-standard cases |
| 61 | if a3 == F::zero() { |
| 62 | // a3 = 0; a2*x^2+a1*x+a0=0; solve quadratic equation |
| 63 | super::quadratic::find_roots_quadratic(a2, a1, a0) |
| 64 | } else if a2 == F::zero() { |
| 65 | // a2 = 0; a3*x^3+a1*x+a0=0; solve depressed cubic equation |
| 66 | super::cubic_depressed::find_roots_cubic_depressed(a1 / a3, a0 / a3) |
| 67 | } else if a3 == F::one() { |
| 68 | // solve normalized cubic expression |
| 69 | super::cubic_normalized::find_roots_cubic_normalized(a2, a1, a0) |
| 70 | } else { |
| 71 | let _2 = F::from(2i16); |
| 72 | let _3 = F::from(3i16); |
| 73 | let _4 = F::from(4i16); |
| 74 | let _9 = F::from(9i16); |
| 75 | let _18 = F::from(18i16); |
| 76 | let _27 = F::from(27i16); |
| 77 | |
| 78 | // standard case |
| 79 | let d = _18 * a3 * a2 * a1 * a0 - _4 * a2 * a2 * a2 * a0 + a2 * a2 * a1 * a1 |
| 80 | - _4 * a3 * a1 * a1 * a1 |
| 81 | - _27 * a3 * a3 * a0 * a0; |
| 82 | let d0 = a2 * a2 - _3 * a3 * a1; |
| 83 | let d1 = _2 * a2 * a2 * a2 - _9 * a3 * a2 * a1 + _27 * a3 * a3 * a0; |
| 84 | if d < F::zero() { |
| 85 | // one real root |
| 86 | let sqrt = (-_27 * a3 * a3 * d).sqrt(); |
| 87 | let c = F::cbrt(if d1 < F::zero() { d1 - sqrt } else { d1 + sqrt } / _2); |
| 88 | let x = -(a2 + c + d0 / c) / (_3 * a3); |
| 89 | Roots::One([x]) |
| 90 | } else if d == F::zero() { |
| 91 | // multiple roots |
| 92 | if d0 == F::zero() { |
| 93 | // triple root |
| 94 | Roots::One([-a2 / (a3 * _3)]) |
| 95 | } else { |
| 96 | // single root and double root |
| 97 | Roots::One([(_9 * a3 * a0 - a2 * a1) / (d0 * _2)]) |
| 98 | .add_new_root((_4 * a3 * a2 * a1 - _9 * a3 * a3 * a0 - a2 * a2 * a2) / (a3 * d0)) |
| 99 | } |
| 100 | } else { |
| 101 | // three real roots |
| 102 | let c3_img = F::sqrt(_27 * a3 * a3 * d) / _2; |
| 103 | let c3_real = d1 / _2; |
| 104 | let c3_module = F::sqrt(c3_img * c3_img + c3_real * c3_real); |
| 105 | let c3_phase = _2 * F::atan(c3_img / (c3_real + c3_module)); |
| 106 | let c_module = F::cbrt(c3_module); |
| 107 | let c_phase = c3_phase / _3; |
| 108 | let c_real = c_module * F::cos(c_phase); |
| 109 | let c_img = c_module * F::sin(c_phase); |
| 110 | let x0_real = -(a2 + c_real + (d0 * c_real) / (c_module * c_module)) / (_3 * a3); |
| 111 | |
| 112 | let e_real = -F::one() / _2; |
| 113 | let e_img = F::sqrt(_3) / _2; |
| 114 | let c1_real = c_real * e_real - c_img * e_img; |
| 115 | let c1_img = c_real * e_img + c_img * e_real; |
| 116 | let x1_real = -(a2 + c1_real + (d0 * c1_real) / (c1_real * c1_real + c1_img * c1_img)) / (_3 * a3); |
| 117 | |
| 118 | let c2_real = c1_real * e_real - c1_img * e_img; |
| 119 | let c2_img = c1_real * e_img + c1_img * e_real; |
| 120 | let x2_real = -(a2 + c2_real + (d0 * c2_real) / (c2_real * c2_real + c2_img * c2_img)) / (_3 * a3); |
| 121 | |
| 122 | Roots::One([x0_real]).add_new_root(x1_real).add_new_root(x2_real) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[cfg(test)] |
| 128 | mod test { |
| 129 | use super::super::super::*; |
| 130 | |
| 131 | #[test] |
| 132 | fn test_find_roots_cubic() { |
| 133 | assert_eq!(find_roots_cubic(1f32, 0f32, 0f32, 0f32), Roots::One([0f32])); |
| 134 | |
| 135 | match find_roots_cubic(1f64, 0f64, -1f64, 0f64) { |
| 136 | Roots::Three(x) => { |
| 137 | assert_float_array_eq!(1e-15, x, [-1f64, 0f64, 1f64]); |
| 138 | } |
| 139 | _ => { |
| 140 | assert!(false); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn test_find_roots_cubic_small_discriminant() { |
| 147 | // Try to find roots of the cubic polynomial where the highest coefficient is very small |
| 148 | // (as reported by Andrew Hunter in July 2019) |
| 149 | match find_roots_cubic( |
| 150 | -0.000000000000000040410628481035f64, |
| 151 | 0.0126298310280606f64, |
| 152 | -0.100896606408756f64, |
| 153 | 0.0689539597036461f64, |
| 154 | ) { |
| 155 | Roots::Three(x) => { |
| 156 | // (According to Wolfram Alpha, roots must be 0.7547108770537f64, 7.23404258961f64, 312537357195213f64) |
| 157 | // Actual result differ a little due to the limited precision of calculations. |
| 158 | assert_float_array_eq!(1e-8, x, [0.7583841816097057f64, 7.233267996296344f64, 312537357195212.9f64]); |
| 159 | } |
| 160 | _ => { |
| 161 | assert!(false); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |