Debian Codegraph @

src/analytical/cubic_depressed.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 depressed cubic equation x^3 + a1*x + a0 = 0.
29 ///
30 /// In case more than one roots are present, they are returned in the increasing order.
31 ///
32 /// # Examples
33 ///
34 /// ```
35 /// use roots::find_roots_cubic_depressed;
36 ///
37 /// let one_root = find_roots_cubic_depressed(0f64, 0f64);
38 /// // Returns Roots::One([0f64]) as 'x^3 = 0' has one root 0
39 ///
40 /// let three_roots = find_roots_cubic_depressed(-1f32, 0f32);
41 /// // Returns Roots::Three([-1f32, -0f32, 1f32]) as 'x^3 - x = 0' has roots -1, 0, and 1
42 /// ```
43 pub fn find_roots_cubic_depressedrust-analyzer cargo roots 0.0.8 analytical/cubic_depressed/find_roots_cubic_depressed().<F: FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#>(a1: F, a0: F) -> Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#<F> {
44 let _2 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(2i16);
45 let _3 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(3i16);
46 let _4 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(4i16);
47 let _9 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(9i16);
48 let _18 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(18i16);
49 let _27 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(27i16);
50 let _54 = F::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(54i16);
51
52 if a1 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().() {
53 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().a0.cbrtrust-analyzer cargo roots 0.0.8 float/FloatType#cbrt().()])
54 } else if a0 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().() {
55 superrust-analyzer cargo roots 0.0.8 analytical/::quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/::find_roots_quadraticrust-analyzer cargo roots 0.0.8 analytical/quadratic/find_roots_quadratic().(F::onerust-analyzer cargo roots 0.0.8 float/FloatType#one().(), F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().(), a1).add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(F::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().())
56 } else {
57 let d = a0 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 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()._4 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().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/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/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;
58 if d 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 // n*a0^2 + m*a1^3 < 0 => a1 < 0
60 let a = (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg()._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().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()._3).sqrtrust-analyzer cargo roots 0.0.8 float/FloatType#sqrt().();
61
62 let phi = (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg()._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().a0 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().(a 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().a 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().a)).acosrust-analyzer cargo roots 0.0.8 float/FloatType#acos().() 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()._3;
63 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([a 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().phi.cosrust-analyzer cargo roots 0.0.8 float/FloatType#cos().()])
64 .add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(a 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().(phi 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().F::two_third_pirust-analyzer cargo roots 0.0.8 float/FloatType#two_third_pi().()).cosrust-analyzer cargo roots 0.0.8 float/FloatType#cos().())
65 .add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(a 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().(phi 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().F::two_third_pirust-analyzer cargo roots 0.0.8 float/FloatType#two_third_pi().()).cosrust-analyzer cargo roots 0.0.8 float/FloatType#cos().())
66 } else {
67 let sqrt_d = d.sqrtrust-analyzer cargo roots 0.0.8 float/FloatType#sqrt().();
68 let a0_div_2 = a0 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;
69 let x1 = (sqrt_d 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().a0_div_2).cbrtrust-analyzer cargo roots 0.0.8 float/FloatType#cbrt().() 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().(sqrt_d 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().a0_div_2).cbrtrust-analyzer cargo roots 0.0.8 float/FloatType#cbrt().();
70 if d 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().() {
71 // one real root and one double root
72 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([x1]).add_new_rootrust-analyzer cargo roots 0.0.8 analytical/roots/impl#[`Roots<F>`]add_new_root().(a0_div_2)
73 } else {
74 // one real root
75 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([x1])
76 }
77 }
78 }
79 }
80
81 #[cfg(test)]
82 mod testrust-analyzer cargo roots 0.0.8 analytical/cubic_depressed/test/ {
83 use superrust-analyzer cargo roots 0.0.8 analytical/cubic_depressed/::superrust-analyzer cargo roots 0.0.8 analytical/::superrust-analyzer cargo roots 0.0.8 crate/::*;
84
85 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
86 fn test_find_roots_cubic_depressedrust-analyzer cargo roots 0.0.8 analytical/cubic_depressed/test/test_find_roots_cubic_depressed().() {
87 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_cubic_depressedrust-analyzer cargo roots 0.0.8 analytical/cubic_depressed/find_roots_cubic_depressed().(0f32, 0f32), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#([0f32]));
88 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_roots_cubic_depressedrust-analyzer cargo roots 0.0.8 analytical/cubic_depressed/find_roots_cubic_depressed().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64, 0f64), Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Threerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Three#([-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64, 0f64, 1f64]));
89
90 match find_roots_cubic_depressedrust-analyzer cargo roots 0.0.8 analytical/cubic_depressed/find_roots_cubic_depressed().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().2f64, 2f64) {
91 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Onerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#One#(x) => {
92 assert_float_array_eqrust-analyzer cargo roots 0.0.8 assert_float_array_eq!!(1e-15, x, [-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1.769292354238631415240409f64]);
93 }
94 _ => {
95 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(false);
96 }
97 }
98
99 match find_roots_cubic_depressedrust-analyzer cargo roots 0.0.8 analytical/cubic_depressed/find_roots_cubic_depressed().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().3f64, 2f64) {
100 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Tworust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Two#(x) => {
101 assert_float_array_eqrust-analyzer cargo roots 0.0.8 assert_float_array_eq!!(1e-15, x, [-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().2f64, 1f64]);
102 }
103 _ => {
104 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(false);
105 }
106 }
107
108 match find_roots_cubic_depressedrust-analyzer cargo roots 0.0.8 analytical/cubic_depressed/find_roots_cubic_depressed().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().2f64, 1f64) {
109 Rootsrust-analyzer cargo roots 0.0.8 analytical/roots/Roots#::Threerust-analyzer cargo roots 0.0.8 analytical/roots/Roots#Three#(x) => {
110 assert_float_array_eqrust-analyzer cargo roots 0.0.8 assert_float_array_eq!!(1e-15, x, [(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64 -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 5f64.sqrtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]sqrt().()) /rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). 2f64, (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1f64 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Add<Self>`]add(). 5f64.sqrtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]sqrt().()) /rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). 2f64, 1f64]);
111 }
112 _ => {
113 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(false);
114 }
115 }
116 }
117 }