Debian Codegraph @

src/analytical/roots.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
27 /// Sorted and unique list of roots of an equation.
28 #[derive(Debug, PartialEq)]
29 pub enum Roots<F: FloatType> {
30 /// Equation has no roots
31 No([F; 0]),
32 /// Equation has one root (or all roots of the equation are the same)
33 One([F; 1]),
34 /// Equation has two roots
35 Two([F; 2]),
36 /// Equation has three roots
37 Three([F; 3]),
38 /// Equation has four roots
39 Four([F; 4]),
40 }
41
42 impl<F: FloatType> AsRef<[F]> for Roots<F> {
43 fn as_ref(&self) -> &[F] {
44 match self {
45 &Roots::No(ref x) => x,
46 &Roots::One(ref x) => x,
47 &Roots::Two(ref x) => x,
48 &Roots::Three(ref x) => x,
49 &Roots::Four(ref x) => x,
50 }
51 }
52 }
53
54 impl<F: FloatType> Roots<F> {
55 fn check_new_root(&self, new_root: F) -> (bool, usize) {
56 let mut pos = 0;
57 let mut exists = false;
58
59 for x in self.as_ref().iter() {
60 if *x == new_root {
61 exists = true;
62 break;
63 }
64 if *x > new_root {
65 break;
66 }
67 pos = pos + 1;
68 }
69
70 (exists, pos)
71 }
72
73 /// Add a new root to existing ones keeping the list of roots ordered and unique.
74 pub fn add_new_root(self, new_root: F) -> Self {
75 match self {
76 Roots::No(_) => Roots::One([new_root]),
77 _ => {
78 let (exists, pos) = self.check_new_root(new_root);
79
80 if exists {
81 self
82 } else {
83 let old_roots = self.as_ref();
84 match (old_roots.len(), pos) {
85 (1, 0) => Roots::Two([new_root, old_roots[0]]),
86 (1, 1) => Roots::Two([old_roots[0], new_root]),
87 (2, 0) => Roots::Three([new_root, old_roots[0], old_roots[1]]),
88 (2, 1) => Roots::Three([old_roots[0], new_root, old_roots[1]]),
89 (2, 2) => Roots::Three([old_roots[0], old_roots[1], new_root]),
90 (3, 0) => Roots::Four([new_root, old_roots[0], old_roots[1], old_roots[2]]),
91 (3, 1) => Roots::Four([old_roots[0], new_root, old_roots[1], old_roots[2]]),
92 (3, 2) => Roots::Four([old_roots[0], old_roots[1], new_root, old_roots[2]]),
93 (3, 3) => Roots::Four([old_roots[0], old_roots[1], old_roots[2], new_root]),
94 _ => panic!("Cannot add root"),
95 }
96 }
97 }
98 }
99 }
100 }
101
102 #[test]
103 fn test_roots() {
104 let mut roots = Roots::One([1f32]);
105 assert_eq!(roots, Roots::One([1f32]));
106
107 roots = roots.add_new_root(1f32);
108 assert_eq!(roots, Roots::One([1f32]));
109
110 roots = roots.add_new_root(0f32);
111 assert_eq!(roots, Roots::Two([0f32, 1f32]));
112
113 roots = roots.add_new_root(0f32);
114 assert_eq!(roots, Roots::Two([0f32, 1f32]));
115
116 roots = roots.add_new_root(3f32);
117 assert_eq!(roots, Roots::Three([0f32, 1f32, 3f32]));
118
119 roots = roots.add_new_root(2f32);
120 assert_eq!(roots, Roots::Four([0f32, 1f32, 2f32, 3f32]));
121 }