| 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 depressed quartic equation x^4 + a2*x^2 + a1*x + a0 = 0. |
| 29 | /// |
| 30 | /// Returned roots are ordered. Precision is about 1e-14 for f64. |
| 31 | /// |
| 32 | /// # Examples |
| 33 | /// |
| 34 | /// ``` |
| 35 | /// use roots::find_roots_quartic_depressed; |
| 36 | /// |
| 37 | /// let one_root = find_roots_quartic_depressed(1f64, 0f64, 0f64); |
| 38 | /// // Returns Roots::One([0f64]) as 'x^4 = 0' has one root 0 |
| 39 | /// |
| 40 | /// let two_roots = find_roots_quartic_depressed(1f32, 0f32, -1f32); |
| 41 | /// // Returns Roots::Two([-1f32, 1f32]) as 'x^4 - 1 = 0' has roots -1 and 1 |
| 42 | /// ``` |
| 43 | pub fn find_roots_quartic_depressed<F: FloatType>(a2: F, a1: F, a0: F) -> Roots<F> { |
| 44 | // Handle non-standard cases |
| 45 | if a1 == F::zero() { |
| 46 | // a1 = 0; x^4 + a2*x^2 + a0 = 0; solve biquadratic equation |
| 47 | super::biquadratic::find_roots_biquadratic(F::one(), a2, a0) |
| 48 | } else if a0 == F::zero() { |
| 49 | // a0 = 0; x^4 + a2*x^2 + a1*x = 0; reduce to normalized cubic and add zero root |
| 50 | super::cubic_normalized::find_roots_cubic_normalized(F::zero(), a2, a1).add_new_root(F::zero()) |
| 51 | } else { |
| 52 | let _2 = F::from(2i16); |
| 53 | let _5 = F::from(5i16); |
| 54 | |
| 55 | // Solve the auxiliary equation y^3 + (5/2)*a2*y^2 + (2*a2^2-a0)*y + (a2^3/2 - a2*a0/2 - a1^2/8) = 0 |
| 56 | let a2_pow_2 = a2 * a2; |
| 57 | let a1_div_2 = a1 / _2; |
| 58 | let b2 = a2 * _5 / _2; |
| 59 | let b1 = _2 * a2_pow_2 - a0; |
| 60 | let b0 = (a2_pow_2 * a2 - a2 * a0 - a1_div_2 * a1_div_2) / _2; |
| 61 | |
| 62 | // At least one root always exists. The last root is the maximal one. |
| 63 | let resolvent_roots = super::cubic_normalized::find_roots_cubic_normalized(b2, b1, b0); |
| 64 | let y = resolvent_roots.as_ref().iter().last().unwrap(); |
| 65 | |
| 66 | let _a2_plus_2y = a2 + _2 * *y; |
| 67 | if _a2_plus_2y > F::zero() { |
| 68 | let sqrt_a2_plus_2y = _a2_plus_2y.sqrt(); |
| 69 | let q0a = a2 + *y - a1_div_2 / sqrt_a2_plus_2y; |
| 70 | let q0b = a2 + *y + a1_div_2 / sqrt_a2_plus_2y; |
| 71 | |
| 72 | let mut roots = super::quadratic::find_roots_quadratic(F::one(), sqrt_a2_plus_2y, q0a); |
| 73 | for x in super::quadratic::find_roots_quadratic(F::one(), -sqrt_a2_plus_2y, q0b) |
| 74 | .as_ref() |
| 75 | .iter() |
| 76 | { |
| 77 | roots = roots.add_new_root(*x); |
| 78 | } |
| 79 | roots |
| 80 | } else { |
| 81 | Roots::No([]) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #[cfg(test)] |
| 87 | mod test { |
| 88 | use super::super::super::*; |
| 89 | |
| 90 | #[test] |
| 91 | fn test_find_roots_quartic_depressed() { |
| 92 | assert_eq!(find_roots_quartic_depressed(0f32, 0f32, 0f32), Roots::One([0f32])); |
| 93 | assert_eq!(find_roots_quartic_depressed(1f32, 1f32, 1f32), Roots::No([])); |
| 94 | |
| 95 | // Thanks WolframAlpha for the test data |
| 96 | match find_roots_quartic_depressed(1f64, 1f64, -1f64) { |
| 97 | Roots::Two(x) => { |
| 98 | assert_float_array_eq!(1e-15, x, [-1f64, 0.5698402909980532659114f64]); |
| 99 | } |
| 100 | _ => { |
| 101 | assert!(false); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | match find_roots_quartic_depressed(-10f64, 5f64, 1f64) { |
| 106 | Roots::Four(x) => { |
| 107 | assert_float_array_eq!( |
| 108 | 1e-15, |
| 109 | x, |
| 110 | [ |
| 111 | -3.3754294311910698f64, |
| 112 | -0.1531811728532153f64, |
| 113 | 0.67861075799846644f64, |
| 114 | 2.84999984604581877f64 |
| 115 | ] |
| 116 | ); |
| 117 | } |
| 118 | _ => { |
| 119 | assert!(false); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |