| 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 superrust-analyzer cargo roots 0.0.8 analytical/::superrust-analyzer cargo roots 0.0.8 crate/::FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#; |
| 26 | use superrust-analyzer cargo roots 0.0.8 analytical/::superrust-analyzer cargo roots 0.0.8 crate/::Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/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_linearrust-analyzer cargo roots 0.0.8 analytical/linear/find_roots_linear().<F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#>(a1: F, a0: F) -> Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#<F> { |
| 49 | if a1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() { |
| 50 | if a0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() { |
| 51 | Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().()]) |
| 52 | } else { |
| 53 | Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Norust-analyzer cargo roots 0.0.8 analytical/roots/Roots#No#([]) |
| 54 | } |
| 55 | } else { |
| 56 | Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().a0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div().a1]) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[cfg(test)] |
| 61 | mod testrust-analyzer cargo roots 0.0.8 analytical/linear/test/ { |
| 62 | use superrust-analyzer cargo roots 0.0.8 analytical/linear/::superrust-analyzer cargo roots 0.0.8 analytical/::superrust-analyzer cargo roots 0.0.8 crate/::*; |
| 63 | |
| 64 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 65 | fn test_find_roots_linearrust-analyzer cargo roots 0.0.8 analytical/linear/test/test_find_roots_linear().() { |
| 66 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_linearrust-analyzer cargo roots 0.0.8 analytical/linear/find_roots_linear().(0f32, 0f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([0f32])); |
| 67 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_linearrust-analyzer cargo roots 0.0.8 analytical/linear/find_roots_linear().(2f64, 1f64), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().0.5f64])); |
| 68 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_linearrust-analyzer cargo roots 0.0.8 analytical/linear/find_roots_linear().(0f32, 1f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Norust-analyzer cargo roots 0.0.8 analytical/roots/Roots#No#([])); |
| 69 | } |
| 70 | } |