| 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 linear equation a1*x + a0 = 0. |
| 29 | /// |
| 30 | /// # Examples |
| 31 | /// |
| 32 | /// ``` |
| 33 | /// use roots::Roots; |
| 34 | /// use roots::find_roots_linear; |
| 35 | /// |
| 36 | /// // Returns Roots::No([]) as '0*x + 1 = 0' has no roots; |
| 37 | /// let no_root = find_roots_linear(0f32, 1f32); |
| 38 | /// assert_eq!(no_root, Roots::No([])); |
| 39 | /// |
| 40 | /// // Returns Roots::Two([0f64]) as '1*x + 0 = 0' has the root 0 |
| 41 | /// let root = find_roots_linear(1f64, 0f64); |
| 42 | /// assert_eq!(root, Roots::One([0f64])); |
| 43 | /// |
| 44 | /// // Returns Roots::One([0f32]) as 0 is one of roots of '0*x + 0 = 0' |
| 45 | /// let zero_root = find_roots_linear(0f32, 0f32); |
| 46 | /// assert_eq!(zero_root, Roots::One([0f32])); |
| 47 | /// ``` |
| 48 | pub fn find_roots_linear<F: FloatType>(a1: F, a0: F) -> Roots<F> { |
| 49 | if a1 == F::zero() { |
| 50 | if a0 == F::zero() { |
| 51 | Roots::One([F::zero()]) |
| 52 | } else { |
| 53 | Roots::No([]) |
| 54 | } |
| 55 | } else { |
| 56 | Roots::One([-a0 / a1]) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[cfg(test)] |
| 61 | mod test { |
| 62 | use super::super::super::*; |
| 63 | |
| 64 | #[test] |
| 65 | fn test_find_roots_linear() { |
| 66 | assert_eq!(find_roots_linear(0f32, 0f32), Roots::One([0f32])); |
| 67 | assert_eq!(find_roots_linear(2f64, 1f64), Roots::One([-0.5f64])); |
| 68 | assert_eq!(find_roots_linear(0f32, 1f32), Roots::No([])); |
| 69 | } |
| 70 | } |