Debian Codegraph @

src/analytical/biquadratic.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 bi-quadratic equation a4*x^4 + a2*x^2 + a0 = 0.
29 ///
30 /// Returned roots are arranged in the increasing order.
31 ///
32 /// # Examples
33 ///
34 /// ```
35 /// use roots::find_roots_biquadratic;
36 ///
37 /// let no_roots = find_roots_biquadratic(1f32, 0f32, 1f32);
38 /// // Returns Roots::No([]) as 'x^4 + 1 = 0' has no roots
39 ///
40 /// let one_root = find_roots_biquadratic(1f64, 0f64, 0f64);
41 /// // Returns Roots::One([0f64]) as 'x^4 = 0' has one root 0
42 ///
43 /// let two_roots = find_roots_biquadratic(1f32, 0f32, -1f32);
44 /// // Returns Roots::Two([-1f32, 1f32]) as 'x^4 - 1 = 0' has roots -1 and 1
45 /// ```
46 pub fn find_roots_biquadratic<F: FloatType>(a4: F, a2: F, a0: F) -> Roots<F> {
47 // Handle non-standard cases
48 if a4 == F::zero() {
49 // a4 = 0; a2*x^2 + a0 = 0; solve quadratic equation
50 super::quadratic::find_roots_quadratic(a2, F::zero(), a0)
51 } else if a0 == F::zero() {
52 // a0 = 0; a4*x^4 + a2*x^2 = 0; solve quadratic equation and add zero root
53 super::quadratic::find_roots_quadratic(a4, F::zero(), a2).add_new_root(F::zero())
54 } else {
55 // solve the corresponding quadratic equation and order roots
56 let mut roots = Roots::No([]);
57 for x in super::quadratic::find_roots_quadratic(a4, a2, a0).as_ref().iter() {
58 if *x > F::zero() {
59 let sqrt_x = x.sqrt();
60 roots = roots.add_new_root(-sqrt_x).add_new_root(sqrt_x);
61 } else if *x == F::zero() {
62 roots = roots.add_new_root(F::zero());
63 }
64 }
65 roots
66 }
67 }
68
69 #[cfg(test)]
70 mod test {
71 use super::super::super::*;
72
73 #[test]
74 fn test_find_roots_biquadratic() {
75 assert_eq!(find_roots_biquadratic(0f32, 0f32, 0f32), Roots::One([0f32]));
76 assert_eq!(find_roots_biquadratic(1f32, 0f32, 1f32), Roots::No([]));
77 assert_eq!(find_roots_biquadratic(1f64, 0f64, -1f64), Roots::Two([-1f64, 1f64]));
78 assert_eq!(
79 find_roots_biquadratic(1f64, -5f64, 4f64),
80 Roots::Four([-2f64, -1f64, 1f64, 2f64])
81 );
82 }
83 }