| 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 quartic equation a4*x^4 + a4*x^3 + a2*x^2 + a1*x + a0 = 0. |
| 29 | /// pp, rr, and dd are already computed while searching for multiple roots |
| 30 | fn find_roots_via_depressed_quartic<F: FloatType>(a4: F, a3: F, a2: F, a1: F, a0: F, pp: F, rr: F, dd: F) -> Roots<F> { |
| 31 | // Depressed quartic |
| 32 | // https://en.wikipedia.org/wiki/Quartic_function#Converting_to_a_depressed_quartic |
| 33 | |
| 34 | let _2 = F::from(2i16); |
| 35 | let _3 = F::from(3i16); |
| 36 | let _4 = F::from(4i16); |
| 37 | let _6 = F::from(6i16); |
| 38 | let _8 = F::from(8i16); |
| 39 | let _12 = F::from(12i16); |
| 40 | let _16 = F::from(16i16); |
| 41 | let _256 = F::from(256i16); |
| 42 | |
| 43 | // a4*x^4 + a3*x^3 + a2*x^2 + a1*x + a0 = 0 => y^4 + p*y^2 + q*y + r. |
| 44 | let a4_pow_2 = a4 * a4; |
| 45 | let a4_pow_3 = a4_pow_2 * a4; |
| 46 | let a4_pow_4 = a4_pow_2 * a4_pow_2; |
| 47 | // Re-use pre-calculated values |
| 48 | let p = pp / (_8 * a4_pow_2); |
| 49 | let q = rr / (_8 * a4_pow_3); |
| 50 | let r = (dd + _16 * a4_pow_2 * (_12 * a0 * a4 - _3 * a1 * a3 + a2 * a2)) / (_256 * a4_pow_4); |
| 51 | |
| 52 | let mut roots = Roots::No([]); |
| 53 | for y in super::quartic_depressed::find_roots_quartic_depressed(p, q, r) |
| 54 | .as_ref() |
| 55 | .iter() |
| 56 | { |
| 57 | roots = roots.add_new_root(*y - a3 / (_4 * a4)); |
| 58 | } |
| 59 | roots |
| 60 | } |
| 61 | |
| 62 | /// Solves a quartic equation a4*x^4 + a3*x^3 + a2*x^2 + a1*x + a0 = 0. |
| 63 | /// |
| 64 | /// Returned roots are ordered. |
| 65 | /// Precision is about 5e-15 for f64, 5e-7 for f32. |
| 66 | /// WARNING: f32 is often not enough to find multiple roots. |
| 67 | /// |
| 68 | /// # Examples |
| 69 | /// |
| 70 | /// ``` |
| 71 | /// use roots::find_roots_quartic; |
| 72 | /// |
| 73 | /// let one_root = find_roots_quartic(1f64, 0f64, 0f64, 0f64, 0f64); |
| 74 | /// // Returns Roots::One([0f64]) as 'x^4 = 0' has one root 0 |
| 75 | /// |
| 76 | /// let two_roots = find_roots_quartic(1f32, 0f32, 0f32, 0f32, -1f32); |
| 77 | /// // Returns Roots::Two([-1f32, 1f32]) as 'x^4 - 1 = 0' has roots -1 and 1 |
| 78 | /// |
| 79 | /// let multiple_roots = find_roots_quartic(-14.0625f64, -3.75f64, 29.75f64, 4.0f64, -16.0f64); |
| 80 | /// // Returns Roots::Two([-1.1016116464173349f64, 0.9682783130840016f64]) |
| 81 | /// |
| 82 | /// let multiple_roots_not_found = find_roots_quartic(-14.0625f32, -3.75f32, 29.75f32, 4.0f32, -16.0f32); |
| 83 | /// // Returns Roots::No([]) because of f32 rounding errors when trying to calculate the discriminant |
| 84 | /// ``` |
| 85 | pub fn find_roots_quartic<F: FloatType>(a4: F, a3: F, a2: F, a1: F, a0: F) -> Roots<F> { |
| 86 | // Handle non-standard cases |
| 87 | if a4 == F::zero() { |
| 88 | // a4 = 0; a3*x^3 + a2*x^2 + a1*x + a0 = 0; solve cubic equation |
| 89 | super::cubic::find_roots_cubic(a3, a2, a1, a0) |
| 90 | } else if a0 == F::zero() { |
| 91 | // a0 = 0; x^4 + a2*x^2 + a1*x = 0; reduce to cubic and arrange results |
| 92 | super::cubic::find_roots_cubic(a4, a3, a2, a1).add_new_root(F::zero()) |
| 93 | } else if a1 == F::zero() && a3 == F::zero() { |
| 94 | // a1 = 0, a3 =0; a4*x^4 + a2*x^2 + a0 = 0; solve bi-quadratic equation |
| 95 | super::biquadratic::find_roots_biquadratic(a4, a2, a0) |
| 96 | } else { |
| 97 | let _3 = F::from(3i16); |
| 98 | let _4 = F::from(4i16); |
| 99 | let _6 = F::from(6i16); |
| 100 | let _8 = F::from(8i16); |
| 101 | let _9 = F::from(9i16); |
| 102 | let _10 = F::from(10i16); |
| 103 | let _12 = F::from(12i16); |
| 104 | let _16 = F::from(16i16); |
| 105 | let _18 = F::from(18i16); |
| 106 | let _27 = F::from(27i16); |
| 107 | let _64 = F::from(64i16); |
| 108 | let _72 = F::from(72i16); |
| 109 | let _80 = F::from(80i16); |
| 110 | let _128 = F::from(128i16); |
| 111 | let _144 = F::from(144i16); |
| 112 | let _192 = F::from(192i16); |
| 113 | let _256 = F::from(256i16); |
| 114 | // Discriminant |
| 115 | // https://en.wikipedia.org/wiki/Quartic_function#Nature_of_the_roots |
| 116 | // Partially simplifed to keep intermediate values smaller (to minimize rounding errors). |
| 117 | let discriminant = a4 * a0 * a4 * (_256 * a4 * a0 * a0 + a1 * (_144 * a2 * a1 - _192 * a3 * a0)) |
| 118 | + a4 * a0 * a2 * a2 * (_16 * a2 * a2 - _80 * a3 * a1 - _128 * a4 * a0) |
| 119 | + (a3 |
| 120 | * a3 |
| 121 | * (a4 * a0 * (_144 * a2 * a0 - _6 * a1 * a1) |
| 122 | + (a0 * (_18 * a3 * a2 * a1 - _27 * a3 * a3 * a0 - _4 * a2 * a2 * a2) + a1 * a1 * (a2 * a2 - _4 * a3 * a1)))) |
| 123 | + a4 * a1 * a1 * (_18 * a3 * a2 * a1 - _27 * a4 * a1 * a1 - _4 * a2 * a2 * a2); |
| 124 | let pp = _8 * a4 * a2 - _3 * a3 * a3; |
| 125 | let rr = a3 * a3 * a3 + _8 * a4 * a4 * a1 - _4 * a4 * a3 * a2; |
| 126 | let delta0 = a2 * a2 - _3 * a3 * a1 + _12 * a4 * a0; |
| 127 | let dd = _64 * a4 * a4 * a4 * a0 - _16 * a4 * a4 * a2 * a2 + _16 * a4 * a3 * a3 * a2 |
| 128 | - _16 * a4 * a4 * a3 * a1 |
| 129 | - _3 * a3 * a3 * a3 * a3; |
| 130 | |
| 131 | // Handle special cases |
| 132 | let double_root = discriminant == F::zero(); |
| 133 | if double_root { |
| 134 | let triple_root = double_root && delta0 == F::zero(); |
| 135 | let quadruple_root = triple_root && dd == F::zero(); |
| 136 | let no_roots = dd == F::zero() && pp > F::zero() && rr == F::zero(); |
| 137 | if quadruple_root { |
| 138 | // Wiki: all four roots are equal |
| 139 | Roots::One([-a3 / (_4 * a4)]) |
| 140 | } else if triple_root { |
| 141 | // Wiki: At least three roots are equal to each other |
| 142 | // x0 is the unique root of the remainder of the Euclidean division of the quartic by its second derivative |
| 143 | // |
| 144 | // Solved by SymPy (ra is the desired reminder) |
| 145 | // a, b, c, d, e = symbols('a,b,c,d,e') |
| 146 | // f=a*x**4+b*x**3+c*x**2+d*x+e // Quartic polynom |
| 147 | // g=6*a*x**2+3*b*x+c // Second derivative |
| 148 | // q, r = div(f, g) // SymPy only finds the highest power |
| 149 | // simplify(f-(q*g+r)) == 0 // Verify the first division |
| 150 | // qa, ra = div(r/a,g/a) // Workaround to get the second division |
| 151 | // simplify(f-((q+qa)*g+ra*a)) == 0 // Verify the second division |
| 152 | // solve(ra,x) |
| 153 | // ----- yields |
| 154 | // (−72*a^2*e+10*a*c^2−3*b^2*c)/(9*(8*a^2*d−4*a*b*c+b^3)) |
| 155 | let x0 = (-_72 * a4 * a4 * a0 + _10 * a4 * a2 * a2 - _3 * a3 * a3 * a2) |
| 156 | / (_9 * (_8 * a4 * a4 * a1 - _4 * a4 * a3 * a2 + a3 * a3 * a3)); |
| 157 | let roots = Roots::One([x0]); |
| 158 | roots.add_new_root(-(a3 / a4 + _3 * x0)) |
| 159 | } else if no_roots { |
| 160 | // Wiki: two complex conjugate double roots |
| 161 | Roots::No([]) |
| 162 | } else { |
| 163 | find_roots_via_depressed_quartic(a4, a3, a2, a1, a0, pp, rr, dd) |
| 164 | } |
| 165 | } else { |
| 166 | let no_roots = discriminant > F::zero() && (pp > F::zero() || dd > F::zero()); |
| 167 | if no_roots { |
| 168 | // Wiki: two pairs of non-real complex conjugate roots |
| 169 | Roots::No([]) |
| 170 | } else { |
| 171 | find_roots_via_depressed_quartic(a4, a3, a2, a1, a0, pp, rr, dd) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | #[cfg(test)] |
| 178 | mod test { |
| 179 | use super::super::super::*; |
| 180 | |
| 181 | #[test] |
| 182 | fn test_find_roots_quartic() { |
| 183 | assert_eq!(find_roots_quartic(1f32, 0f32, 0f32, 0f32, 0f32), Roots::One([0f32])); |
| 184 | assert_eq!(find_roots_quartic(1f64, 0f64, 0f64, 0f64, -1f64), Roots::Two([-1f64, 1f64])); |
| 185 | assert_eq!( |
| 186 | find_roots_quartic(1f64, -10f64, 35f64, -50f64, 24f64), |
| 187 | Roots::Four([1f64, 2f64, 3f64, 4f64]) |
| 188 | ); |
| 189 | |
| 190 | match find_roots_quartic(1.1248467624839498f64, -4.8721513473605924f64, |
| 191 | 7.9323705711747614f64, -5.7774307699949397f64, |
| 192 | 1.5971379368787519f64) { |
| 193 | Roots::Two(x) => { |
| 194 | assert_float_array_eq!(2e-15f64, x, [1.225913506454221f64, 1.257275575390252f64]); |
| 195 | } |
| 196 | _ => { |
| 197 | assert!(false); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | match find_roots_quartic(3f64, 5f64, -5f64, -5f64, 2f64) { |
| 202 | Roots::Four(x) => { |
| 203 | assert_float_array_eq!(2e-15f64, x, [-2f64, -1f64, 0.33333333333333333f64, 1f64]); |
| 204 | } |
| 205 | _ => { |
| 206 | assert!(false); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | match find_roots_quartic(3f32, 5f32, -5f32, -5f32, 2f32) { |
| 211 | Roots::Four(x) => { |
| 212 | assert_float_array_eq!(5e-7, x, [-2f32, -1f32, 0.33333333333333333f32, 1f32]); |
| 213 | } |
| 214 | _ => { |
| 215 | assert!(false); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | #[test] |
| 221 | fn test_find_roots_quartic_tim_luecke() { |
| 222 | // Reported in December 2019 |
| 223 | assert_eq!( |
| 224 | find_roots_quartic(-14.0625f64, -3.75f64, 29.75f64, 4.0f64, -16.0f64), |
| 225 | Roots::Two([-1.1016116464173349f64, 0.9682783130840016f64]) |
| 226 | ); |
| 227 | // 32-bit floating point is not accurate enough to solve this case ... |
| 228 | assert_eq!( |
| 229 | find_roots_quartic(-14.0625f32, -3.75f32, 29.75f32, 4.0f32, -16.0f32), |
| 230 | Roots::No([]) |
| 231 | ); |
| 232 | // ... even after normalizing |
| 233 | assert_eq!( |
| 234 | find_roots_quartic( |
| 235 | 1f32, |
| 236 | -3.75f32 / -14.0625f32, |
| 237 | 29.75f32 / -14.0625f32, |
| 238 | 4.0f32 / -14.0625f32, |
| 239 | -16.0f32 / -14.0625f32 |
| 240 | ), |
| 241 | Roots::No([]) |
| 242 | ); |
| 243 | // assert_eq!( |
| 244 | // find_roots_quartic(-14.0625f32, -3.75f32, 29.75f32, 4.0f32, -16.0f32), |
| 245 | // Roots::Two([-1.1016117f32, 0.96827835f32]) |
| 246 | // ); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn test_find_roots_quartic_triple_root() { |
| 251 | // (x+3)(3x-1)^3 == 27 x^4 + 54 x^3 - 72 x^2 + 26 x - 3 |
| 252 | assert_eq!( |
| 253 | find_roots_quartic(27f64, 54f64, -72f64, 26f64, -3f64), |
| 254 | Roots::Two([-3.0f64, 0.3333333333333333f64]) |
| 255 | ); |
| 256 | assert_eq!( |
| 257 | find_roots_quartic(27f32, 54f32, -72f32, 26f32, -3f32), |
| 258 | Roots::Two([-3.0f32, 0.33333333f32]) |
| 259 | ); |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn test_find_roots_quartic_quadruple_root() { |
| 264 | // (7x+2)^4 == 2401 x^4 + 2744 x^3 + 1176 x^2 + 224 x + 16 |
| 265 | assert_eq!( |
| 266 | find_roots_quartic(2401f64, 2744f64, 1176f64, 224f64, 16f64), |
| 267 | Roots::One([-0.2857142857142857f64]) |
| 268 | ); |
| 269 | // 32-bit floating point is less accurate |
| 270 | assert_eq!( |
| 271 | find_roots_quartic(2401f32, 2744f32, 1176f32, 224f32, 16f32), |
| 272 | Roots::One([-0.2857143f32]) |
| 273 | ); |
| 274 | } |
| 275 | } |