Debian Codegraph @

src/analytical/quadratic.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 analytical/::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 analytical/::superrust-analyzer cargo roots 0.0.8 crate/::Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#;
27
28 /// Solves a quadratic equation a2*x^2 + a1*x + a0 = 0.
29 ///
30 /// In case two roots are present, the first returned root is less than the second one.
31 ///
32 /// # Examples
33 ///
34 /// ```
35 /// use roots::Roots;
36 /// use roots::find_roots_quadratic;
37 ///
38 /// let no_roots = find_roots_quadratic(1f32, 0f32, 1f32);
39 /// // Returns Roots::No([]) as 'x^2 + 1 = 0' has no roots
40 ///
41 /// let one_root = find_roots_quadratic(1f64, 0f64, 0f64);
42 /// // Returns Roots::One([0f64]) as 'x^2 = 0' has one root 0
43 ///
44 /// let two_roots = find_roots_quadratic(1f32, 0f32, -1f32);
45 /// // Returns Roots::Two([-1f32,1f32]) as 'x^2 - 1 = 0' has roots -1 and 1
46 /// ```
47 pub fn find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().<F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#>(a2: F, a1: F, a0: F) -> Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#<F> {
48 // Handle non-standard cases
49 if a2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() {
50 // a2 = 0; a1*x+a0=0; solve linear equation
51 superrust-analyzer cargo roots 0.0.8 analytical/::linearrust-analyzer cargo roots 0.0.8 analytical/linear/::find_roots_linearrust-analyzer cargo roots 0.0.8 analytical/linear/find_roots_linear().(a1, a0)
52 } else {
53 let _2 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(2i16);
54 let _4 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(4i16);
55
56 // Rust lacks a simple way to convert an integer constant to generic type F
57 let discriminant = a1 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().a1 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()._4 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().a2 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().a0;
58 if discriminant 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().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() {
59 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Norust-analyzer cargo roots 0.0.8 analytical/roots/Roots#No#([])
60 } else {
61 let a2x2 = _2 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().a2;
62 if discriminant rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() {
63 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().a1 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().a2x2])
64 } else {
65 // To improve precision, do not use the smallest divisor.
66 // See https://people.csail.mit.edu/bkph/articles/Quadratics.pdf
67 let sq = discriminant.sqrtrust-analyzer cargo roots 0.0.8 float/FloatType#sqrt().();
68
69 let (same_sign, diff_sign) = if a1 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().F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() {
70 (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().a1 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().sq, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().a1 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().sq)
71 } else {
72 (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().a1 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().sq, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().a1 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().sq)
73 };
74
75 let (x1, x2) = if same_sign.absrust-analyzer cargo roots 0.0.8 float/FloatType#abs().() 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().a2x2.absrust-analyzer cargo roots 0.0.8 float/FloatType#abs().() {
76 let a0x2 = _2 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().a0;
77 if diff_sign.absrust-analyzer cargo roots 0.0.8 float/FloatType#abs().() 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().a2x2.absrust-analyzer cargo roots 0.0.8 float/FloatType#abs().() {
78 // 2*a2 is the smallest divisor, do not use it
79 (a0x2 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().same_sign, a0x2 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().diff_sign)
80 } else {
81 // diff_sign is the smallest divisor, do not use it
82 (a0x2 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().same_sign, same_sign 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().a2x2)
83 }
84 } else {
85 // 2*a2 is the greatest divisor, use it
86 (diff_sign 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().a2x2, same_sign 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().a2x2)
87 };
88
89 // Order roots
90 if x1 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().x2 {
91 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([x1, x2])
92 } else {
93 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([x2, x1])
94 }
95 }
96 }
97 }
98 }
99
100 #[cfg(test)]
101 mod testrust-analyzer cargo roots 0.0.8 analytical/quadratic/test/ {
102 use superrust-analyzer cargo roots 0.0.8 analytical/quadratic/::superrust-analyzer cargo roots 0.0.8 analytical/::superrust-analyzer cargo roots 0.0.8 crate/::*;
103
104 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
105 fn test_find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/test/test_find_roots_quadratic().() {
106 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(0f32, 0f32, 0f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([0f32]));
107 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(1f32, 0f32, 1f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Norust-analyzer cargo roots 0.0.8 analytical/roots/Roots#No#([]));
108 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(1f64, 0f64, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64, 1f64]));
109 }
110
111 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
112 fn test_find_roots_quadratic_small_a2rust-analyzer cargo roots 0.0.8 analytical/quadratic/test/test_find_roots_quadratic_small_a2().() {
113 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
114 find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(1e-20f32, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1f32, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1e-30f32),
115 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1e-30f32, 1e20f32])
116 );
117 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
118 find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1e-20f32, 1f32, 1e-30f32),
119 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1e-30f32, 1e20f32])
120 );
121 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(1e-20f32, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1f32, 1f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([1f32, 1e20f32]));
122 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1e-20f32, 1f32, 1f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1f32, 1e20f32]));
123 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1e-20f32, 1f32, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([1f32, 1e20f32]));
124 }
125
126 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
127 fn test_find_roots_quadratic_big_a1rust-analyzer cargo roots 0.0.8 analytical/quadratic/test/test_find_roots_quadratic_big_a1().() {
128 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(1f32, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1e15f32, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1e-15f32, 1e15f32]));
129 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1f32, 1e15f32, 1f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1e-15f32, 1e15f32]));
130 }
131 }