Debian Codegraph @

src/numerical/inverse_quadratic.rs

raw symbols SCIP
1 // Copyright (c) 2019, 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 numerical/::superrust-analyzer cargo roots 0.0.8 crate/::find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().;
26 use superrust-analyzer cargo roots 0.0.8 numerical/::superrust-analyzer cargo roots 0.0.8 crate/::FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#;
27 use superrust-analyzer cargo roots 0.0.8 numerical/::Convergencyrust-analyzer cargo roots 0.0.8 numerical/Convergency#;
28 use superrust-analyzer cargo roots 0.0.8 numerical/::Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval#;
29 use superrust-analyzer cargo roots 0.0.8 numerical/::Samplerust-analyzer cargo roots 0.0.8 numerical/Sample#;
30 use superrust-analyzer cargo roots 0.0.8 numerical/::SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#;
31
32 /// Definition of the quadratic equation a*x^2 + b*x + c
33 #[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:)]
34 pub struct Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#<F>
35 where
36 F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#,
37 {
38 arust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#a.: F,
39 brust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#b.: F,
40 crust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#c.: F,
41 }
42
43 impl<F> Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#<F>
44 where
45 F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#,
46 {
47 /// Restore coefficients of the quadratic equation by 3 points
48 fn from_three_pointsrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/impl#[`Parabola<F>`]from_three_points().(p1: &Samplerust-analyzer cargo roots 0.0.8 numerical/Sample#<F>, p2: &Samplerust-analyzer cargo roots 0.0.8 numerical/Sample#<F>, p3: &Samplerust-analyzer cargo roots 0.0.8 numerical/Sample#<F>) -> Selfrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/impl#[`Parabola<F>`] {
49 let denom = (p1.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().p2.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().(p1.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().p3.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().(p2.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().p3.xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.);
50 let a = (p3.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().(p2.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().p1.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.) 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().p2.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().(p1.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().p3.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.) 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().p1.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().(p3.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().p2.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.)) 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().denom;
51 let b = (p1.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().p1.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().(p2.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().p3.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.) 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().p3.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().p3.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().(p1.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().p2.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.) 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().p2.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().p2.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().(p3.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().p1.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.)) 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().denom;
52 let c = (p2.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().p2.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().(p3.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().p1.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().p1.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().p3.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.)
53 +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().p2.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().(p1.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().p1.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().p3.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().p3.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().p3.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().p1.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.)
54 +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().p1.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().p3.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().(p3.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().p1.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().p2.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.)
55 /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().denom;
56
57 Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola# { arust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#a.: a, brust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#b.: b, crust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#c.: c }
58 }
59 }
60
61 /// Find a root of the function f(x) = 0 using inverse quadratic approximation.
62 ///
63 /// Pro
64 ///
65 /// + Faster than linear approximation
66 /// + No need for derivative function
67 ///
68 /// Contra
69 ///
70 /// - sqrt is calculated on every step
71 /// - only works for polynomial-like functions
72 ///
73 /// # Failures
74 /// ## NoBracketing
75 /// Initial values do not bracket the root.
76 /// ## NoConvergency
77 /// Algorithm cannot find a root within the given number of iterations.
78 /// # Examples
79 ///
80 /// ```
81 /// use roots::SimpleConvergency;
82 /// use roots::find_root_inverse_quadratic;
83 ///
84 /// let f = |x| { 1f64*x*x - 1f64 };
85 /// let mut convergency = SimpleConvergency { eps:1e-15f64, max_iter:30 };
86 ///
87 /// let root1 = find_root_inverse_quadratic(10f64, 0f64, &f, &mut convergency);
88 /// // Returns approximately Ok(1);
89 ///
90 /// let root2 = find_root_inverse_quadratic(-10f64, 0f64, &f, &mut 1e-15f64);
91 /// // Returns approximately Ok(-1);
92 /// ```
93 pub fn find_root_inverse_quadraticrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/find_root_inverse_quadratic().<F, Func>(a: F, b: F, mut f: Func, convergency: &mut dyn Convergencyrust-analyzer cargo roots 0.0.8 numerical/Convergency#<F>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<F, SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#>
94 where
95 F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#,
96 Func: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(F) -> F,
97 {
98 let (x1, x2) = if a 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().b { (b, a) } else { (a, b) };
99 let sample1 = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: x1, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: f(x1) };
100 if convergency.is_root_foundrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_root_found().(sample1.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.) {
101 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sample1.xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.);
102 }
103 let sample2 = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: x2, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: f(x2) };
104 if convergency.is_root_foundrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_root_found().(sample2.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.) {
105 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sample2.xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.);
106 }
107 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().sample1.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&sample2) {
108 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#::NoBracketingrust-analyzer cargo roots 0.0.8 numerical/SearchError#NoBracketing#);
109 }
110
111 // Initially, find x3 using the regula falsi method
112 let mut interval = Intervalrust-analyzer cargo roots 0.0.8 numerical/Interval# {
113 beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.: sample1,
114 endrust-analyzer cargo roots 0.0.8 numerical/Interval#end.: sample2,
115 };
116 let mut x3 = interval.middlerust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]middle().();
117 if interval.is_convergedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_converged().(convergency) {
118 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(x3);
119 }
120 let mut sample3 = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: x3, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: f(x3) };
121 if convergency.is_root_foundrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_root_found().(sample3.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.) {
122 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sample3.xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.);
123 }
124
125 // Iterate quadratically
126 let mut iter = 0;
127 loop {
128 let parabola = Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#::from_three_pointsrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/impl#[`Parabola<F>`]from_three_points().(&interval.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin., &interval.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end., &sample3);
129
130 // Find the new approximation quadratically
131 x3 = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(root) = find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(parabola.arust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#a., parabola.brust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#b., parabola.crust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#c.)
132 .as_refrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`][`AsRef<[F]>`]as_ref().()
133 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
134 .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find().(|x| interval.contains_xrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]contains_x().(x))
135 {
136 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().root
137 } else {
138 // no roots inside interval, fallback to linear approximation
139 interval.middlerust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]middle().()
140 };
141
142 // calculate the approximated value
143 sample3 = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: x3, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: f(x3) };
144
145 if convergency.is_root_foundrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_root_found().(sample3.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.) {
146 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sample3.xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.);
147 }
148
149 // Narrow down the search interval while keeping the root bracketed
150 if sample3.is_bracketed_withrust-analyzer cargo roots 0.0.8 numerical/impl#[`Sample<F>`]is_bracketed_with().(&interval.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin.) {
151 interval.endrust-analyzer cargo roots 0.0.8 numerical/Interval#end. = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# {
152 xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: sample3.xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.,
153 yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: sample3.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.,
154 };
155 } else {
156 interval.beginrust-analyzer cargo roots 0.0.8 numerical/Interval#begin. = Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# {
157 xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: sample3.xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.,
158 yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: sample3.yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.,
159 };
160 }
161
162 if interval.is_convergedrust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]is_converged().(convergency) {
163 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(interval.middlerust-analyzer cargo roots 0.0.8 numerical/impl#[`Interval<F>`]middle().());
164 }
165
166 iter = iter 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;
167 if convergency.is_iteration_limit_reachedrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_iteration_limit_reached().(iter) {
168 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#::NoConvergencyrust-analyzer cargo roots 0.0.8 numerical/SearchError#NoConvergency#);
169 }
170 }
171 }
172
173 #[cfg(test)]
174 mod testrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/test/ {
175 use superrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/::superrust-analyzer cargo roots 0.0.8 numerical/::*;
176 use superrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/::*;
177
178 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
179 fn test_find_root_inverse_quadraticrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/test/test_find_root_inverse_quadratic().() {
180 let f = |x| 1f64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().1f64;
181 let mut conv = debug_convergencyrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/::DebugConvergencyrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/DebugConvergency#::newrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]new().(1e-15f64, 30);
182
183 conv.resetrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]reset().();
184 assert_float_eqrust-analyzer cargo roots 0.0.8 assert_float_eq!!(
185 1e-15f64,
186 find_root_inverse_quadraticrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/find_root_inverse_quadratic().(10f64, 0f64, &f, &mut conv).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
187 1f64
188 );
189 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, conv.get_iter_countrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]get_iter_count().());
190
191 conv.resetrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]reset().();
192 assert_float_eqrust-analyzer cargo roots 0.0.8 assert_float_eq!!(
193 1e-15f64,
194 find_root_inverse_quadraticrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/find_root_inverse_quadratic().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().10f64, 0f64, &f, &mut conv).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
195 -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64
196 );
197 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, conv.get_iter_countrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]get_iter_count().());
198
199 conv.resetrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]reset().();
200 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
201 find_root_inverse_quadraticrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/find_root_inverse_quadratic().(10f64, 20f64, &f, &mut conv),
202 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#::NoBracketingrust-analyzer cargo roots 0.0.8 numerical/SearchError#NoBracketing#)
203 );
204 let result = find_root_inverse_quadraticrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/find_root_inverse_quadratic().(10f64, 20f64, &f, &mut conv);
205 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "Bracketing Error");
206 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, conv.get_iter_countrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]get_iter_count().());
207 }
208
209 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
210 fn test_from_three_pointsrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/test/test_from_three_points().() {
211 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
212 Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola# {
213 arust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#a.: 1f64,
214 brust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#b.: 0f64,
215 crust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#c.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64
216 },
217 Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#::from_three_pointsrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/impl#[`Parabola<F>`]from_three_points().(
218 &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().10f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 99f64 },
219 &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().2f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 3f64 },
220 &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.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64 }
221 )
222 );
223 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
224 Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola# {
225 arust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#a.: 1f64,
226 brust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#b.: 0f64,
227 crust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#c.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64
228 },
229 Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#::from_three_pointsrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/impl#[`Parabola<F>`]from_three_points().(
230 &Samplerust-analyzer cargo roots 0.0.8 numerical/Sample# { xrust-analyzer cargo roots 0.0.8 numerical/Sample#x.: 10f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 99f64 },
231 &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.: 3f64 },
232 &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.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64 }
233 )
234 );
235 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
236 Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola# {
237 arust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#a.: 1f64,
238 brust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#b.: 0f64,
239 crust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#c.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64
240 },
241 Parabolarust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/Parabola#::from_three_pointsrust-analyzer cargo roots 0.0.8 numerical/inverse_quadratic/impl#[`Parabola<F>`]from_three_points().(
242 &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().3f64, yrust-analyzer cargo roots 0.0.8 numerical/Sample#y.: 8f64 },
243 &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.: 3f64 },
244 &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.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64 }
245 )
246 );
247 }
248 }