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 super::super::FloatType;
26 use super::super::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_depressed<F: FloatType>(a1: F, a0: F) -> Roots<F> {
44 let _2 = F::from(2i16);
45 let _3 = F::from(3i16);
46 let _4 = F::from(4i16);
47 let _9 = F::from(9i16);
48 let _18 = F::from(18i16);
49 let _27 = F::from(27i16);
50 let _54 = F::from(54i16);
51
52 if a1 == F::zero() {
53 Roots::One([-a0.cbrt()])
54 } else if a0 == F::zero() {
55 super::quadratic::find_roots_quadratic(F::one(), F::zero(), a1).add_new_root(F::zero())
56 } else {
57 let d = a0 * a0 / _4 + a1 * a1 * a1 / _27;
58 if d < F::zero() {
59 // n*a0^2 + m*a1^3 < 0 => a1 < 0
60 let a = (-_4 * a1 / _3).sqrt();
61
62 let phi = (-_4 * a0 / (a * a * a)).acos() / _3;
63 Roots::One([a * phi.cos()])
64 .add_new_root(a * (phi + F::two_third_pi()).cos())
65 .add_new_root(a * (phi - F::two_third_pi()).cos())
66 } else {
67 let sqrt_d = d.sqrt();
68 let a0_div_2 = a0 / _2;
69 let x1 = (sqrt_d - a0_div_2).cbrt() - (sqrt_d + a0_div_2).cbrt();
70 if d == F::zero() {
71 // one real root and one double root
72 Roots::One([x1]).add_new_root(a0_div_2)
73 } else {
74 // one real root
75 Roots::One([x1])
76 }
77 }
78 }
79 }
80
81 #[cfg(test)]
82 mod test {
83 use super::super::super::*;
84
85 #[test]
86 fn test_find_roots_cubic_depressed() {
87 assert_eq!(find_roots_cubic_depressed(0f32, 0f32), Roots::One([0f32]));
88 assert_eq!(find_roots_cubic_depressed(-1f64, 0f64), Roots::Three([-1f64, 0f64, 1f64]));
89
90 match find_roots_cubic_depressed(-2f64, 2f64) {
91 Roots::One(x) => {
92 assert_float_array_eq!(1e-15, x, [-1.769292354238631415240409f64]);
93 }
94 _ => {
95 assert!(false);
96 }
97 }
98
99 match find_roots_cubic_depressed(-3f64, 2f64) {
100 Roots::Two(x) => {
101 assert_float_array_eq!(1e-15, x, [-2f64, 1f64]);
102 }
103 _ => {
104 assert!(false);
105 }
106 }
107
108 match find_roots_cubic_depressed(-2f64, 1f64) {
109 Roots::Three(x) => {
110 assert_float_array_eq!(1e-15, x, [(-1f64 - 5f64.sqrt()) / 2f64, (-1f64 + 5f64.sqrt()) / 2f64, 1f64]);
111 }
112 _ => {
113 assert!(false);
114 }
115 }
116 }
117 }