| 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 quadratic equation a2*x^2 + a1*x + a0 = 0. |
| 29 | /// |
| 30 | /// In case two roots are present, the first returned root is less than the second one. |
| 31 | /// |
| 32 | /// # Examples |
| 33 | /// |
| 34 | /// ``` |
| 35 | /// use roots::Roots; |
| 36 | /// use roots::find_roots_quadratic; |
| 37 | /// |
| 38 | /// let no_roots = find_roots_quadratic(1f32, 0f32, 1f32); |
| 39 | /// // Returns Roots::No([]) as 'x^2 + 1 = 0' has no roots |
| 40 | /// |
| 41 | /// let one_root = find_roots_quadratic(1f64, 0f64, 0f64); |
| 42 | /// // Returns Roots::One([0f64]) as 'x^2 = 0' has one root 0 |
| 43 | /// |
| 44 | /// let two_roots = find_roots_quadratic(1f32, 0f32, -1f32); |
| 45 | /// // Returns Roots::Two([-1f32,1f32]) as 'x^2 - 1 = 0' has roots -1 and 1 |
| 46 | /// ``` |
| 47 | pub fn find_roots_quadratic<F: FloatType>(a2: F, a1: F, a0: F) -> Roots<F> { |
| 48 | // Handle non-standard cases |
| 49 | if a2 == F::zero() { |
| 50 | // a2 = 0; a1*x+a0=0; solve linear equation |
| 51 | super::linear::find_roots_linear(a1, a0) |
| 52 | } else { |
| 53 | let _2 = F::from(2i16); |
| 54 | let _4 = F::from(4i16); |
| 55 | |
| 56 | // Rust lacks a simple way to convert an integer constant to generic type F |
| 57 | let discriminant = a1 * a1 - _4 * a2 * a0; |
| 58 | if discriminant < F::zero() { |
| 59 | Roots::No([]) |
| 60 | } else { |
| 61 | let a2x2 = _2 * a2; |
| 62 | if discriminant == F::zero() { |
| 63 | Roots::One([-a1 / a2x2]) |
| 64 | } else { |
| 65 | // To improve precision, do not use the smallest divisor. |
| 66 | // See https://people.csail.mit.edu/bkph/articles/Quadratics.pdf |
| 67 | let sq = discriminant.sqrt(); |
| 68 | |
| 69 | let (same_sign, diff_sign) = if a1 < F::zero() { |
| 70 | (-a1 + sq, -a1 - sq) |
| 71 | } else { |
| 72 | (-a1 - sq, -a1 + sq) |
| 73 | }; |
| 74 | |
| 75 | let (x1, x2) = if same_sign.abs() > a2x2.abs() { |
| 76 | let a0x2 = _2 * a0; |
| 77 | if diff_sign.abs() > a2x2.abs() { |
| 78 | // 2*a2 is the smallest divisor, do not use it |
| 79 | (a0x2 / same_sign, a0x2 / diff_sign) |
| 80 | } else { |
| 81 | // diff_sign is the smallest divisor, do not use it |
| 82 | (a0x2 / same_sign, same_sign / a2x2) |
| 83 | } |
| 84 | } else { |
| 85 | // 2*a2 is the greatest divisor, use it |
| 86 | (diff_sign / a2x2, same_sign / a2x2) |
| 87 | }; |
| 88 | |
| 89 | // Order roots |
| 90 | if x1 < x2 { |
| 91 | Roots::Two([x1, x2]) |
| 92 | } else { |
| 93 | Roots::Two([x2, x1]) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[cfg(test)] |
| 101 | mod test { |
| 102 | use super::super::super::*; |
| 103 | |
| 104 | #[test] |
| 105 | fn test_find_roots_quadratic() { |
| 106 | assert_eq!(find_roots_quadratic(0f32, 0f32, 0f32), Roots::One([0f32])); |
| 107 | assert_eq!(find_roots_quadratic(1f32, 0f32, 1f32), Roots::No([])); |
| 108 | assert_eq!(find_roots_quadratic(1f64, 0f64, -1f64), Roots::Two([-1f64, 1f64])); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn test_find_roots_quadratic_small_a2() { |
| 113 | assert_eq!( |
| 114 | find_roots_quadratic(1e-20f32, -1f32, -1e-30f32), |
| 115 | Roots::Two([-1e-30f32, 1e20f32]) |
| 116 | ); |
| 117 | assert_eq!( |
| 118 | find_roots_quadratic(-1e-20f32, 1f32, 1e-30f32), |
| 119 | Roots::Two([-1e-30f32, 1e20f32]) |
| 120 | ); |
| 121 | assert_eq!(find_roots_quadratic(1e-20f32, -1f32, 1f32), Roots::Two([1f32, 1e20f32])); |
| 122 | assert_eq!(find_roots_quadratic(-1e-20f32, 1f32, 1f32), Roots::Two([-1f32, 1e20f32])); |
| 123 | assert_eq!(find_roots_quadratic(-1e-20f32, 1f32, -1f32), Roots::Two([1f32, 1e20f32])); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn test_find_roots_quadratic_big_a1() { |
| 128 | assert_eq!(find_roots_quadratic(1f32, -1e15f32, -1f32), Roots::Two([-1e-15f32, 1e15f32])); |
| 129 | assert_eq!(find_roots_quadratic(-1f32, 1e15f32, 1f32), Roots::Two([-1e-15f32, 1e15f32])); |
| 130 | } |
| 131 | } |