| 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 crate/::FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#; |
| 26 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 27 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/; |
| 28 | |
| 29 | /// Pair of the independent variable x and the function value y=F(x) |
| 30 | #[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:)] |
| 31 | pub struct Samplerust-analyzer cargo roots 0.0.8 numerical/Sample#<F> |
| 32 | where |
| 33 | F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#, |
| 34 | { |
| 35 | /// Value of the independent variable (X-axis) |
| 36 | xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: F, |
| 37 | /// Value of the dependent variable (Y-axis) |
| 38 | yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: F, |
| 39 | } |
| 40 | |
| 41 | impl<F> Samplerust-analyzer cargo roots 0.0.8 numerical/Sample#<F> |
| 42 | where |
| 43 | F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#, |
| 44 | { |
| 45 | fn is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&self, other: &Selfrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]) -> bool { |
| 46 | self.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().other.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Interval between two samples, including these samples |
| 51 | #[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:)] |
| 52 | pub struct Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval#<F> |
| 53 | where |
| 54 | F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#, |
| 55 | { |
| 56 | /// First sample |
| 57 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample#<F>, |
| 58 | /// Last sample |
| 59 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample#<F>, |
| 60 | } |
| 61 | |
| 62 | impl<F> Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval#<F> |
| 63 | where |
| 64 | F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#, |
| 65 | { |
| 66 | fn is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().(&self) -> bool { |
| 67 | self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.) |
| 68 | } |
| 69 | fn is_convergedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_converged().(&self, convergency: &mut dyn Convergencyrust-analyzer cargo roots 0.0.8 numerical/Convergency#<F>) -> bool { |
| 70 | convergency.is_convergedrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_converged().(self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x., self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.) |
| 71 | } |
| 72 | /// Check if the given X is inside the interval |
| 73 | fn contains_xrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]contains_x().(&self, x: &F) -> bool { |
| 74 | *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#le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x. && *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#ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge().self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x. |
| 75 | } |
| 76 | /// Returns a point somewhere in middle of the interval for narrowing this interval down. |
| 77 | /// Rules are as follows: |
| 78 | /// * If the interval is bracketed, use the secant to find the middle point. |
| 79 | /// ** The middle point may not be too close to either range of the interval. |
| 80 | /// * If the interval is not bracketed (why would one use an unbracketed interval?), bisect it. |
| 81 | fn middlerust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]middle().(&self) -> F { |
| 82 | let _2 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(2i16); |
| 83 | let _26 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(26i16); |
| 84 | let _27 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(27i16); |
| 85 | |
| 86 | if self.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().() && self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..yrust-analyzer cargo roots 0.0.8 numerical/Sample#y. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..yrust-analyzer cargo roots 0.0.8 numerical/Sample#y. { |
| 87 | let mut shift = -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..yrust-analyzer cargo roots 0.0.8 numerical/Sample#y. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().(self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.) 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().(self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..yrust-analyzer cargo roots 0.0.8 numerical/Sample#y. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.); |
| 88 | if shift rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().(self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.) 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()._27 { |
| 89 | shift = (self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.) 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()._27; |
| 90 | } |
| 91 | if shift 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().(self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul()._26 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()._27 { |
| 92 | shift = (self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul()._26 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()._27; |
| 93 | } |
| 94 | self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().shift |
| 95 | } else { |
| 96 | (self.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().self.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end..xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.) 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()._2 |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /// Possible errors |
| 102 | #[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:)] |
| 103 | pub enum SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError# { |
| 104 | /// The algorithm could not converge within the given number of iterations |
| 105 | NoConvergencyrust-analyzer cargo roots 0.0.8 numerical/SearchError#NoConvergency#, |
| 106 | /// Initial values do not bracket zero |
| 107 | NoBracketingrust-analyzer cargo roots 0.0.8 numerical/SearchError#NoBracketing#, |
| 108 | /// The algorithm cannot continue from the point where the derivative is zero |
| 109 | ZeroDerivativerust-analyzer cargo roots 0.0.8 numerical/SearchError#ZeroDerivative#, |
| 110 | } |
| 111 | |
| 112 | impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError# { |
| 113 | fn fmtrust-analyzer cargo roots 0.0.8 numerical/impl#[SearchError][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 114 | match self { |
| 115 | SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#::NoConvergencyrust-analyzer cargo roots 0.0.8 numerical/SearchError#NoConvergency# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "Convergency Error"), |
| 116 | SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#::NoBracketingrust-analyzer cargo roots 0.0.8 numerical/SearchError#NoBracketing# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "Bracketing Error"), |
| 117 | SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#::ZeroDerivativerust-analyzer cargo roots 0.0.8 numerical/SearchError#ZeroDerivative# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "Zero Derivative Error"), |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | impl Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError# { |
| 122 | fn descriptionrust-analyzer cargo roots 0.0.8 numerical/impl#[SearchError][Error]description().(&self) -> &str { |
| 123 | match self { |
| 124 | SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#::NoConvergencyrust-analyzer cargo roots 0.0.8 numerical/SearchError#NoConvergency# => "The algorithm could not converge within the given number of iterations", |
| 125 | SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#::NoBracketingrust-analyzer cargo roots 0.0.8 numerical/SearchError#NoBracketing# => "Initial values do not bracket zero", |
| 126 | SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#::ZeroDerivativerust-analyzer cargo roots 0.0.8 numerical/SearchError#ZeroDerivative# => "The algorithm cannot continue from the point where the derivative is zero", |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /// The way to check if the algorithm has finished by either finding a root |
| 132 | /// or reaching the iteration limit. |
| 133 | pub trait Convergencyrust-analyzer cargo roots 0.0.8 numerical/Convergency#<F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#> { |
| 134 | /// Return true if the given Y value is close enough to the zero |
| 135 | fn is_root_foundrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_root_found().(&mut self, y: F) -> bool; |
| 136 | /// Return true if given x values are close enough to each other |
| 137 | fn is_convergedrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_converged().(&mut self, x1: F, x2: F) -> bool; |
| 138 | /// Return true if no more iterations desired |
| 139 | fn is_iteration_limit_reachedrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_iteration_limit_reached().(&mut self, iter: usize) -> bool; |
| 140 | } |
| 141 | |
| 142 | impl<F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#> Convergencyrust-analyzer cargo roots 0.0.8 numerical/Convergency#<F> for F { |
| 143 | /// Return true if the given Y value is close enough to the zero |
| 144 | fn is_root_foundrust-analyzer cargo roots 0.0.8 numerical/impl#[F][`Convergency<Self>`]is_root_found().(&mut self, y: F) -> bool { |
| 145 | y.absrust-analyzer cargo roots 0.0.8 float/FloatType#abs().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().self.absrust-analyzer cargo roots 0.0.8 float/FloatType#abs().() |
| 146 | } |
| 147 | /// Return true if given x values are close enough to each other |
| 148 | fn is_convergedrust-analyzer cargo roots 0.0.8 numerical/impl#[F][`Convergency<Self>`]is_converged().(&mut self, x1: F, x2: F) -> bool { |
| 149 | (x1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().x2).absrust-analyzer cargo roots 0.0.8 float/FloatType#abs().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().self.absrust-analyzer cargo roots 0.0.8 float/FloatType#abs().() |
| 150 | } |
| 151 | /// Return true if no more iterations desired |
| 152 | fn is_iteration_limit_reachedrust-analyzer cargo roots 0.0.8 numerical/impl#[F][`Convergency<Self>`]is_iteration_limit_reached().(&mut self, iter: usize) -> bool { |
| 153 | iter rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().30 |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | pub mod brentrust-analyzer cargo roots 0.0.8 numerical/brent/; |
| 158 | pub mod eigenrust-analyzer cargo roots 0.0.8 numerical/eigen/; |
| 159 | pub mod inverse_quadraticrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/; |
| 160 | pub mod newton_raphsonrust-analyzer cargo roots 0.0.8 numerical/newton_raphson/; |
| 161 | pub mod polynomrust-analyzer cargo roots 0.0.8 numerical/polynom/; |
| 162 | pub mod regula_falsirust-analyzer cargo roots 0.0.8 numerical/regula_falsi/; |
| 163 | pub mod secantrust-analyzer cargo roots 0.0.8 numerical/secant/; |
| 164 | |
| 165 | pub mod debug_convergencyrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/; |
| 166 | pub mod simple_convergencyrust-analyzer cargo roots 0.0.8 numerical/simple_convergency/; |
| 167 | |
| 168 | #[cfg(test)] |
| 169 | mod testrust-analyzer cargo roots 0.0.8 numerical/test/ { |
| 170 | use superrust-analyzer cargo roots 0.0.8 numerical/::*; |
| 171 | |
| 172 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 173 | fn sample_bracketedrust-analyzer cargo roots 0.0.8 numerical/test/sample_bracketed().() { |
| 174 | let sample1 = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }; |
| 175 | let sample2 = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 1f64 }; |
| 176 | let sample3 = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64 }; |
| 177 | let sample4 = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }; |
| 178 | let sample5 = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 1f64 }; |
| 179 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sample1.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample2)); |
| 180 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sample1.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample3)); |
| 181 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sample1.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample4)); |
| 182 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sample1.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample5)); |
| 183 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sample2.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample3)); |
| 184 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sample2.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample4)); |
| 185 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(false, sample2.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample5)); |
| 186 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sample3.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample4)); |
| 187 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sample3.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample5)); |
| 188 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sample4.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample5)); |
| 189 | } |
| 190 | |
| 191 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 192 | fn root_interval_bracketedrust-analyzer cargo roots 0.0.8 numerical/test/root_interval_bracketed().() { |
| 193 | let sut1 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 194 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }, |
| 195 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }, |
| 196 | }; |
| 197 | let sut2 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 198 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f32, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f32 }, |
| 199 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 1f32, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f32 }, |
| 200 | }; |
| 201 | let sut3 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 202 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }, |
| 203 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 1f64 }, |
| 204 | }; |
| 205 | let sut4 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 206 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }, |
| 207 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }, |
| 208 | }; |
| 209 | let sut5 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 210 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }, |
| 211 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 1f64 }, |
| 212 | }; |
| 213 | let sut6 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 214 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1f32, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1f32 }, |
| 215 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f32, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 1f32 }, |
| 216 | }; |
| 217 | let sut7 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 218 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 1f64 }, |
| 219 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64 }, |
| 220 | }; |
| 221 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sut1.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().()); |
| 222 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sut2.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().()); |
| 223 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sut3.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().()); |
| 224 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sut4.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().()); |
| 225 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sut5.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().()); |
| 226 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sut6.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().()); |
| 227 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(true, sut7.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().()); |
| 228 | } |
| 229 | |
| 230 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 231 | fn root_interval_not_bracketedrust-analyzer cargo roots 0.0.8 numerical/test/root_interval_not_bracketed().() { |
| 232 | let sut1 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 233 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 1f64 }, |
| 234 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 1f64 }, |
| 235 | }; |
| 236 | let sut2 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 237 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64 }, |
| 238 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64 }, |
| 239 | }; |
| 240 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(false, sut1.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().()); |
| 241 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(false, sut2.is_bracketedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_bracketed().()); |
| 242 | } |
| 243 | |
| 244 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 245 | fn root_interval_middlerust-analyzer cargo roots 0.0.8 numerical/test/root_interval_middle().() { |
| 246 | let sut1 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 247 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 0f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 1f64 }, |
| 248 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 2f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().3f64 }, |
| 249 | }; |
| 250 | let sut2 = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# { |
| 251 | beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }, |
| 252 | endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 1f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 0f64 }, |
| 253 | }; |
| 254 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0.5f64, sut1.middlerust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]middle().()); |
| 255 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0f64, sut2.middlerust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]middle().()); |
| 256 | } |
| 257 | } |