1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
pub mod degree;
use self::degree::Degree;
use ::fmt;
use ::str;
use ::cmp;
use ::ops::{self, Sub, Neg, BitAnd};
use ::num;
use ::num2;
#[derive(Debug, Clone, Copy)]
pub enum Term {
Determinate(f64),
Indeterminate(f64, Degree),
}
impl Default for Term {
fn default() -> Self {
Term::Determinate(f64::default())
}
}
impl Neg for Term {
type Output = Self;
fn neg(self) -> Self {
match self {
Term::Determinate(number) => Term::Determinate(number.neg()),
Term::Indeterminate(number, power) => Term::Indeterminate(number.neg(), power),
}
}
}
impl str::FromStr for Term {
type Err = num::ParseFloatError;
fn from_str(line: &str) -> Result<Self, Self::Err> {
if let Some(constant) = line.find('X') {
let (left, right): (&str, &str) = line.split_at(constant);
match Degree::from_str(right) {
Ok(Degree(0)) => Ok(Term::Determinate(try!(f64::from_str(left)))),
power => Ok(Term::Indeterminate(try!(f64::from_str(left)), power.unwrap_or_default())),
}
} else {
if let Ok(Degree(power)) = Degree::from_str(line) {
Ok(Term::Determinate(try!(f64::from_str(line)).powi(power)))
} else {
Ok(Term::Determinate(try!(f64::from_str(line))))
}
}
}
}
impl Eq for Term {}
impl cmp::PartialEq for Term {
fn eq(&self, other: &Term) -> bool {
match (self, other) {
(&Term::Determinate(left), &Term::Determinate(right)) if left.eq(&right) => true,
(&Term::Indeterminate(left, left_power), &Term::Indeterminate(right, right_power))
if left.eq(&right).bitand(left_power.eq(&right_power)) => true,
_ => false,
}
}
}
impl cmp::Ord for Term {
fn cmp(&self, other: &Term) -> cmp::Ordering {
match (self, other) {
(&Term::Determinate(right), &Term::Determinate(left)) => left.partial_cmp(&right).unwrap_or(cmp::Ordering::Equal),
(&Term::Determinate(..), &Term::Indeterminate(..)) => cmp::Ordering::Greater,
(&Term::Indeterminate(..), &Term::Determinate(..)) => cmp::Ordering::Less,
(&Term::Indeterminate(right, Degree(right_power)), &Term::Indeterminate(left, Degree(left_power)))
=> left_power.cmp(&right_power).then(left.partial_cmp(&right).unwrap_or(cmp::Ordering::Equal)),
}
}
}
impl cmp::PartialOrd for Term {
fn partial_cmp(&self, other: &Term) -> Option<cmp::Ordering> {
match (self, other) {
(&Term::Determinate(right), &Term::Determinate(left)) => left.partial_cmp(&right),
(&Term::Determinate(..), &Term::Indeterminate(..)) => Some(cmp::Ordering::Greater),
(&Term::Indeterminate(..), &Term::Determinate(..)) => Some(cmp::Ordering::Less),
(&Term::Indeterminate(right, Degree(right_power)), &Term::Indeterminate(left, Degree(left_power)))
=> Some(left_power.cmp(&right_power).then(left.partial_cmp(&right).unwrap_or(cmp::Ordering::Equal)))
}
}
}
impl ops::Sub for Term {
type Output = Self;
fn sub(self, other: Term) -> Self::Output {
match (self, other) {
(Term::Determinate(left), Term::Determinate(right)) => {
Term::Determinate(left.sub(right))
},
(Term::Indeterminate(left, left_power), Term::Indeterminate(right, _)) => {
Term::Indeterminate(left.sub(right), left_power)
},
(first, _) => first,
}
}
}
impl num2::CheckedSub for Term {
fn checked_sub(&self, other: &Term) -> Option<Self::Output> {
match (self, other) {
(&Term::Determinate(left), &Term::Determinate(right)) => {
Some(Term::Determinate(left.sub(right)))
},
(&Term::Indeterminate(right, right_power), &Term::Indeterminate(left, left_power))
if left_power == right_power => {
Some(Term::Indeterminate(left.sub(right), left_power))
},
(first, _) => Some(*first),
}
}
}
impl ops::Add for Term {
type Output = Self;
fn add(self, other: Term) -> Self::Output {
match (self, other) {
(Term::Determinate(left), Term::Determinate(right)) => {
Term::Determinate(left.add(right))
},
(Term::Indeterminate(left, left_power), Term::Indeterminate(right, _)) => {
Term::Indeterminate(left.add(right), left_power)
},
(first, _) => first,
}
}
}
impl num2::Zero for Term {
fn zero() -> Self {
Term::default()
}
fn is_zero(&self) -> bool {
match self {
&Term::Determinate(number) if number.eq(&f64::zero()) => true,
&Term::Indeterminate(number, _) if number.eq(&f64::zero()) => true,
_ => false,
}
}
}
impl fmt::Display for Term {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Term::Determinate(number) => write!(f, "{}", number),
&Term::Indeterminate(number, power) => write!(f, "{}x{}", number, power),
}
}
}