| 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 normalized cubic equation x^3 + a2*x^2 + a1*x + a0 = 0. |
| 29 | /// |
| 30 | /// Trigonometric solution (arccos/cos) is implemented for three roots. |
| 31 | /// |
| 32 | /// In case more than one roots are present, they are returned in the increasing order. |
| 33 | /// |
| 34 | /// # Examples |
| 35 | /// |
| 36 | /// ``` |
| 37 | /// use roots::find_roots_cubic_normalized; |
| 38 | /// |
| 39 | /// let one_root = find_roots_cubic_normalized(0f64, 0f64, 0f64); |
| 40 | /// // Returns Roots::One([0f64]) as 'x^3 = 0' has one root 0 |
| 41 | /// |
| 42 | /// let three_roots = find_roots_cubic_normalized(0f32, -1f32, 0f32); |
| 43 | /// // Returns Roots::Three([-1f32, -0f32, 1f32]) as 'x^3 - x = 0' has roots -1, 0, and 1 |
| 44 | /// ``` |
| 45 | pub fn find_roots_cubic_normalized<F: FloatType>(a2: F, a1: F, a0: F) -> Roots<F> { |
| 46 | let _2 = F::from(2i16); |
| 47 | let _3 = F::from(3i16); |
| 48 | let _4 = F::from(4i16); |
| 49 | let _9 = F::from(9i16); |
| 50 | let _18 = F::from(18i16); |
| 51 | let _27 = F::from(27i16); |
| 52 | let _54 = F::from(54i16); |
| 53 | |
| 54 | let q = (_3 * a1 - a2 * a2) / _9; |
| 55 | let r = (_9 * a2 * a1 - _27 * a0 - _2 * a2 * a2 * a2) / _54; |
| 56 | let q3 = q * q * q; |
| 57 | let d = q3 + r * r; |
| 58 | let a2_div_3 = a2 / _3; |
| 59 | |
| 60 | if d < F::zero() { |
| 61 | let phi_3 = (r / (-q3).sqrt()).acos() / _3; |
| 62 | let sqrt_q_2 = _2 * (-q).sqrt(); |
| 63 | |
| 64 | Roots::One([sqrt_q_2 * phi_3.cos() - a2_div_3]) |
| 65 | .add_new_root(sqrt_q_2 * (phi_3 - F::two_third_pi()).cos() - a2_div_3) |
| 66 | .add_new_root(sqrt_q_2 * (phi_3 + F::two_third_pi()).cos() - a2_div_3) |
| 67 | } else { |
| 68 | let sqrt_d = d.sqrt(); |
| 69 | let s = (r + sqrt_d).cbrt(); |
| 70 | let t = (r - sqrt_d).cbrt(); |
| 71 | |
| 72 | if s == t { |
| 73 | if s + t == F::zero() { |
| 74 | Roots::One([s + t - a2_div_3]) |
| 75 | } else { |
| 76 | Roots::One([s + t - a2_div_3]).add_new_root(-(s + t) / _2 - a2_div_3) |
| 77 | } |
| 78 | } else { |
| 79 | Roots::One([s + t - a2_div_3]) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #[cfg(test)] |
| 85 | mod test { |
| 86 | use super::super::super::*; |
| 87 | |
| 88 | #[test] |
| 89 | fn test_find_roots_cubic_normalized() { |
| 90 | assert_eq!(find_roots_cubic_normalized(0f32, 0f32, 0f32), Roots::One([0f32])); |
| 91 | |
| 92 | match find_roots_cubic_normalized(0f64, -1f64, 0f64) { |
| 93 | Roots::Three(x) => { |
| 94 | assert_float_array_eq!(1e-15, x, [-1f64, 0f64, 1f64]); |
| 95 | } |
| 96 | _ => { |
| 97 | assert!(false); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | match find_roots_cubic_normalized(1f64, -2f64, 2f64) { |
| 102 | Roots::One(x) => { |
| 103 | assert_float_array_eq!(1e-15, x, [-2.269530842081142770853135f64]); |
| 104 | } |
| 105 | _ => { |
| 106 | assert!(false); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | match find_roots_cubic_normalized(0f64, -3f64, 2f64) { |
| 111 | Roots::Two(x) => { |
| 112 | assert_float_array_eq!(1e-15, x, [-2f64, 1f64]); |
| 113 | } |
| 114 | _ => { |
| 115 | assert!(false); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | match find_roots_cubic_normalized(-2f64, -3f64, 2f64) { |
| 120 | Roots::Three(x) => { |
| 121 | assert_float_array_eq!( |
| 122 | 1e-15, |
| 123 | x, |
| 124 | [ |
| 125 | -1.342923082777170208054859f64, |
| 126 | 0.5293165801288393926136314f64, |
| 127 | 2.813606502648330815441228f64 |
| 128 | ] |
| 129 | ); |
| 130 | } |
| 131 | _ => { |
| 132 | assert!(false); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn test_find_roots_cubic_normalized_huge_discriminant() { |
| 139 | // Try to find roots of the cubic polynomial where the highest coefficient is very small |
| 140 | // (as reported by Andrew Hunter in July 2019) |
| 141 | match find_roots_cubic_normalized( |
| 142 | 0.0126298310280606f64 / -0.000000000000000040410628481035f64, |
| 143 | -0.100896606408756f64 / -0.000000000000000040410628481035f64, |
| 144 | 0.0689539597036461f64 / -0.000000000000000040410628481035f64, |
| 145 | ) { |
| 146 | //Roots::Three(x) => { |
| 147 | // // (According to Wolfram Alpha, roots must be 0.7547108770537f64, 7.23404258961f64, 312537357195213f64) |
| 148 | // // These results are returned by find_roots_cubic (using complex numbers). |
| 149 | // assert_float_array_eq!(1e-8, x, [0.7583841816097057f64, 7.233267996296344f64, 312537357195212.9f64]); |
| 150 | //} |
| 151 | Roots::One(x) => { |
| 152 | // (According to Wolfram Alpha, roots must be 0.7547108770537f64, 7.23404258961f64, 312537357195213f64) |
| 153 | // Due to the limited precision of calculations of arccos, the function cannot cope with such numbers. |
| 154 | assert_float_array_eq!(1e-8, x, [312537357195212.4625f64]); |
| 155 | } |
| 156 | _ => { |
| 157 | assert!(false); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |