| 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 | |
| 27 | /// Sorted and unique list of roots of an equation. |
| 28 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 29 | pub enum Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#<F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#> { |
| 30 | /// Equation has no roots |
| 31 | Norust-analyzer cargo roots 0.0.8 analytical/roots/Roots#No#([F; 0]), |
| 32 | /// Equation has one root (or all roots of the equation are the same) |
| 33 | Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([F; 1]), |
| 34 | /// Equation has two roots |
| 35 | Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([F; 2]), |
| 36 | /// Equation has three roots |
| 37 | Threerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Three#([F; 3]), |
| 38 | /// Equation has four roots |
| 39 | Fourrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Four#([F; 4]), |
| 40 | } |
| 41 | |
| 42 | impl<F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#> AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[F]> for Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#<F> { |
| 43 | fn as_refrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`][`AsRef<[F]>`]as_ref().(&self) -> &[F] { |
| 44 | match self { |
| 45 | &Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Norust-analyzer cargo roots 0.0.8 analytical/roots/Roots#No#(ref x) => x, |
| 46 | &Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#(ref x) => x, |
| 47 | &Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#(ref x) => x, |
| 48 | &Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Threerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Three#(ref x) => x, |
| 49 | &Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Fourrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Four#(ref x) => x, |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl<F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#> Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#<F> { |
| 55 | fn check_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]check_new_root().(&self, new_root: F) -> (bool, usize) { |
| 56 | let mut pos = 0; |
| 57 | let mut exists = false; |
| 58 | |
| 59 | for x in self.as_refrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`][`AsRef<[F]>`]as_ref().().iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 60 | if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().x 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().new_root { |
| 61 | exists = true; |
| 62 | break; |
| 63 | } |
| 64 | if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().new_root { |
| 65 | break; |
| 66 | } |
| 67 | pos = pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 68 | } |
| 69 | |
| 70 | (exists, pos) |
| 71 | } |
| 72 | |
| 73 | /// Add a new root to existing ones keeping the list of roots ordered and unique. |
| 74 | pub fn add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(self, new_root: F) -> Selfrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`] { |
| 75 | match self { |
| 76 | Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Norust-analyzer cargo roots 0.0.8 analytical/roots/Roots#No#(_) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([new_root]), |
| 77 | _ => { |
| 78 | let (exists, pos) = self.check_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]check_new_root().(new_root); |
| 79 | |
| 80 | if exists { |
| 81 | self |
| 82 | } else { |
| 83 | let old_roots = self.as_refrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`][`AsRef<[F]>`]as_ref().(); |
| 84 | match (old_roots.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), pos) { |
| 85 | (1, 0) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([new_root, old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().]), |
| 86 | (1, 1) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., new_root]), |
| 87 | (2, 0) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Threerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Three#([new_root, old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().]), |
| 88 | (2, 1) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Threerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Three#([old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., new_root, old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().]), |
| 89 | (2, 2) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Threerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Three#([old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., new_root]), |
| 90 | (3, 0) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Fourrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Four#([new_root, old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().]), |
| 91 | (3, 1) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Fourrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Four#([old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., new_root, old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().]), |
| 92 | (3, 2) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Fourrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Four#([old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., new_root, old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().]), |
| 93 | (3, 3) => Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Fourrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Four#([old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., old_roots[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., new_root]), |
| 94 | _ => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("Cannot add root"), |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 103 | fn test_rootsrust-analyzer cargo roots 0.0.8 analytical/roots/test_roots().() { |
| 104 | let mut roots = Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([1f32]); |
| 105 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(roots, Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([1f32])); |
| 106 | |
| 107 | roots = roots.add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(1f32); |
| 108 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(roots, Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([1f32])); |
| 109 | |
| 110 | roots = roots.add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(0f32); |
| 111 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(roots, Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([0f32, 1f32])); |
| 112 | |
| 113 | roots = roots.add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(0f32); |
| 114 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(roots, Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([0f32, 1f32])); |
| 115 | |
| 116 | roots = roots.add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(3f32); |
| 117 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(roots, Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Threerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Three#([0f32, 1f32, 3f32])); |
| 118 | |
| 119 | roots = roots.add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(2f32); |
| 120 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(roots, Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Fourrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Four#([0f32, 1f32, 2f32, 3f32])); |
| 121 | } |