Debian Codegraph @

src/numerical/regula_falsi.rs

raw symbols SCIP
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 numerical/::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 numerical/::Convergencyrust-analyzer cargo roots 0.0.8 numerical/Convergency#;
27 use superrust-analyzer cargo roots 0.0.8 numerical/::SearchErrorrust-analyzer cargo roots 0.0.8 numerical/SearchError#;
28
29 /// Illinois modification to the classical method
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 enum Edgerust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge# {
32 /// Value is close to X1, reduce the Y1 weight
33 EdgeX1rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#EdgeX1#,
34 /// Value is in the middle of the interval
35 NoEdgerust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#NoEdge#,
36 /// Value is close to X2, reduce the Y2 weight
37 EdgeX2rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#EdgeX2#,
38 }
39
40 /// Find a root of the function f(x) = 0 using the Illinois modification of the regula falsi method.
41 ///
42 /// Pro
43 ///
44 /// + Simple
45 /// + Robust
46 /// + No need for derivative function
47 ///
48 /// Contra
49 ///
50 /// - Slow
51 /// - Needs initial bracketing
52 ///
53 /// # Failures
54 /// ## NoBracketing
55 /// Initial values do not bracket the root.
56 /// ## NoConvergency
57 /// Algorithm cannot find a root within the given number of iterations.
58 /// # Examples
59 ///
60 /// ```
61 /// use roots::SimpleConvergency;
62 /// use roots::find_root_regula_falsi;
63 ///
64 /// let f = |x| { 1f64*x*x - 1f64 };
65 /// let mut convergency = SimpleConvergency { eps:1e-15f64, max_iter:30 };
66 ///
67 /// let root1 = find_root_regula_falsi(10f64, 0f64, &f, &mut convergency);
68 /// // Returns approximately Ok(1);
69 ///
70 /// let root2 = find_root_regula_falsi(-10f64, 0f64, &f, &mut 1e-15f64);
71 /// // Returns approximately Ok(-1);
72 /// ```
73 pub fn find_root_regula_falsirust-analyzer cargo roots 0.0.8 numerical/regula_falsi/find_root_regula_falsi().<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#>
74 where
75 F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#,
76 Func: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(F) -> F,
77 {
78 let _2 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(2i16);
79 let (mut x1, mut 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) };
80 let mut y1 = f(x1);
81 if convergency.is_root_foundrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_root_found().(y1) {
82 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(x1);
83 }
84 let mut y2 = f(x2);
85 if convergency.is_root_foundrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_root_found().(y2) {
86 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(x2);
87 }
88 if y1 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().y2 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().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() {
89 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#);
90 }
91 let mut edge = Edgerust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#::NoEdgerust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#NoEdge#;
92 let mut iter = 0;
93 loop {
94 let x = (x1 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().y2 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 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().y1) 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().(y2 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().y1);
95 if convergency.is_convergedrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_converged().(x1, x2) {
96 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(x);
97 }
98 let y = f(x);
99 if convergency.is_root_foundrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_root_found().(y) {
100 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(x);
101 }
102
103 if 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().y1 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().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() {
104 x1 = x;
105 y1 = y;
106 if edge rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/impl#[Edge][`PartialEq<Self>`]eq().==rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/impl#[Edge][`PartialEq<Self>`]eq(). rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/impl#[Edge][`PartialEq<Self>`]eq().Edgerust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#::EdgeX1rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#EdgeX1# {
107 y2 = y2 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;
108 }
109 edge = Edgerust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#::EdgeX1rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#EdgeX1#;
110 } else if 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().y2 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().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() {
111 x2 = x;
112 y2 = y;
113 if edge rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/impl#[Edge][`PartialEq<Self>`]eq().==rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/impl#[Edge][`PartialEq<Self>`]eq(). rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/impl#[Edge][`PartialEq<Self>`]eq().Edgerust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#::EdgeX2rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#EdgeX2# {
114 y1 = y1 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;
115 }
116 edge = Edgerust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#::EdgeX2rust-analyzer cargo roots 0.0.8 numerical/regula_falsi/Edge#EdgeX2#;
117 } else {
118 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(x);
119 }
120
121 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;
122 if convergency.is_iteration_limit_reachedrust-analyzer cargo roots 0.0.8 numerical/Convergency#is_iteration_limit_reached().(iter) {
123 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#);
124 }
125 }
126 }
127
128 #[cfg(test)]
129 mod testrust-analyzer cargo roots 0.0.8 numerical/regula_falsi/test/ {
130 use superrust-analyzer cargo roots 0.0.8 numerical/regula_falsi/::superrust-analyzer cargo roots 0.0.8 numerical/::*;
131 use superrust-analyzer cargo roots 0.0.8 numerical/regula_falsi/::*;
132
133 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
134 fn test_find_root_regula_falsirust-analyzer cargo roots 0.0.8 numerical/regula_falsi/test/test_find_root_regula_falsi().() {
135 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;
136 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);
137
138 conv.resetrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]reset().();
139 assert_float_eqrust-analyzer cargo roots 0.0.8 assert_float_eq!!(
140 1e-15f64,
141 find_root_regula_falsirust-analyzer cargo roots 0.0.8 numerical/regula_falsi/find_root_regula_falsi().(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().(),
142 1f64
143 );
144 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(11, conv.get_iter_countrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]get_iter_count().());
145
146 conv.resetrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]reset().();
147 assert_float_eqrust-analyzer cargo roots 0.0.8 assert_float_eq!!(
148 1e-15f64,
149 find_root_regula_falsirust-analyzer cargo roots 0.0.8 numerical/regula_falsi/find_root_regula_falsi().(-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().(),
150 -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64
151 );
152 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(11, conv.get_iter_countrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]get_iter_count().());
153
154 conv.resetrust-analyzer cargo roots 0.0.8 numerical/debug_convergency/impl#[`DebugConvergency<F>`]reset().();
155 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
156 find_root_regula_falsirust-analyzer cargo roots 0.0.8 numerical/regula_falsi/find_root_regula_falsi().(10f64, 20f64, &f, &mut conv),
157 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#)
158 );
159 let result = find_root_regula_falsirust-analyzer cargo roots 0.0.8 numerical/regula_falsi/find_root_regula_falsi().(10f64, 20f64, &f, &mut conv);
160 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");
161 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().());
162 }
163 }