| 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 | //#![crate_id = "roots"] |
| 26 | #![crate_type = "lib"] |
| 27 | |
| 28 | //! A set of functions to find real roots of numerical equations. |
| 29 | //! |
| 30 | //! This crate contains various algorithms for numerical and analytical solving |
| 31 | //! of 1-variable equations like f(x)=0. Only real roots are calculated. |
| 32 | //! Multiple (double etc.) roots are considered as one root. |
| 33 | //! |
| 34 | //! # Use |
| 35 | //! |
| 36 | //! Functions find_root_* try to find a root of any given closure function by |
| 37 | //! iterative approximations. Conditions for success/failure can be customized |
| 38 | //! by implementing the Convergency trait. |
| 39 | //! Functions find_roots_* return all roots of several simple equations at once. |
| 40 | |
| 41 | #[cfg(test)] |
| 42 | macro_rules! assert_float_eq( |
| 43 | ($precision:expr, $given:expr , $expected:expr) => ({ |
| 44 | match (&($precision), &($given), &($expected)) { |
| 45 | (precision_val, given_val, expected_val) => { |
| 46 | let diff = given_val-expected_val; |
| 47 | if diff.abs() > precision_val.abs() { |
| 48 | panic!("floats are not the same: (`{}`: `{:.15e}`, expected: `{:.15e}`, precision: `{:.15e}`, delta: `{:.15e}`)", stringify!($given), *given_val, *expected_val, *precision_val, diff ) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | }) |
| 53 | ); |
| 54 | |
| 55 | #[cfg(test)] |
| 56 | macro_rules! assert_float_array_eq( |
| 57 | ($precision:expr, $given:expr , $expected:expr) => ({ |
| 58 | match (&($precision), &($given), &($expected)) { |
| 59 | (precision_val, given_val, expected_val) => { |
| 60 | assert_eq!(given_val.len(),expected_val.len()); |
| 61 | for i in 0..given_val.len() { |
| 62 | assert_float_eq!(precision_val,given_val[i],expected_val[i]); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | }) |
| 67 | ); |
| 68 | |
| 69 | mod analytical; |
| 70 | mod float; |
| 71 | mod numerical; |
| 72 | |
| 73 | pub use self::float::FloatType; |
| 74 | |
| 75 | pub use self::analytical::biquadratic::find_roots_biquadratic; |
| 76 | pub use self::analytical::cubic::find_roots_cubic; |
| 77 | pub use self::analytical::cubic_depressed::find_roots_cubic_depressed; |
| 78 | pub use self::analytical::cubic_normalized::find_roots_cubic_normalized; |
| 79 | pub use self::analytical::linear::find_roots_linear; |
| 80 | pub use self::analytical::quadratic::find_roots_quadratic; |
| 81 | pub use self::analytical::quartic::find_roots_quartic; |
| 82 | pub use self::analytical::quartic_depressed::find_roots_quartic_depressed; |
| 83 | pub use self::analytical::roots::Roots; |
| 84 | |
| 85 | pub use self::numerical::brent::find_root_brent; |
| 86 | pub use self::numerical::debug_convergency::DebugConvergency; |
| 87 | pub use self::numerical::eigen::find_roots_eigen; |
| 88 | pub use self::numerical::inverse_quadratic::find_root_inverse_quadratic; |
| 89 | pub use self::numerical::inverse_quadratic::Parabola; |
| 90 | pub use self::numerical::newton_raphson::find_root_newton_raphson; |
| 91 | pub use self::numerical::polynom::find_roots_sturm; |
| 92 | pub use self::numerical::regula_falsi::find_root_regula_falsi; |
| 93 | pub use self::numerical::secant::find_root_secant; |
| 94 | pub use self::numerical::simple_convergency::SimpleConvergency; |
| 95 | pub use self::numerical::Convergency; |
| 96 | pub use self::numerical::Interval; |
| 97 | pub use self::numerical::Sample; |
| 98 | pub use self::numerical::SearchError; |