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
use super::{polynomials_1d::FirstDegreePolynomial};
use crate::solvers::basis::functions::{Differentiable1D,Function1D};
use crate::Error;
#[derive(PartialEq, Debug)]
pub struct PiecewiseFirstDegreePolynomial {
polynomials: Vec<FirstDegreePolynomial>,
interval_breakpoints: Vec<f64>,
}
impl PiecewiseFirstDegreePolynomial {
pub fn from_values<A: IntoIterator<Item = f64>, B: IntoIterator<Item = f64>>(
coefficients: A,
independent_terms: A,
interval_breakpoints: B,
) -> Result<Self, Error> {
let independent_terms: Vec<f64> = independent_terms.into_iter().collect();
let coefficients: Vec<f64> = coefficients.into_iter().collect();
let interval_breakpoints: Vec<f64> = interval_breakpoints.into_iter().collect();
if independent_terms.len() != interval_breakpoints.len() + 1
|| independent_terms.len() != coefficients.len()
{
return Err(Error::PieceWiseDims);
}
let polynomials = coefficients
.into_iter()
.zip(independent_terms)
.map(|(coef, i_term)| -> FirstDegreePolynomial {
FirstDegreePolynomial::new(coef, i_term)
})
.collect();
Ok(Self {
polynomials,
interval_breakpoints,
})
}
pub fn from_constants<A: IntoIterator<Item = f64>, B: IntoIterator<Item = f64>>(
independent_terms: A,
interval_breakpoints: B,
) -> Result<Self, Error> {
let independent_terms: Vec<f64> = independent_terms.into_iter().collect();
let interval_breakpoints: Vec<f64> = interval_breakpoints.into_iter().collect();
if independent_terms.len() != interval_breakpoints.len() + 1 {
return Err(Error::PieceWiseDims);
}
let polynomials = independent_terms
.into_iter()
.map(|i_term| -> FirstDegreePolynomial { FirstDegreePolynomial::new(0_f64, i_term) })
.collect();
Ok(Self {
polynomials,
interval_breakpoints,
})
}
pub fn from_polynomials<A: IntoIterator<Item = FirstDegreePolynomial>, B: IntoIterator<Item = f64>>(
polynomials: A,
interval_breakpoints: B,
) -> Result<Self, Error> {
let polynomials: Vec<FirstDegreePolynomial> = polynomials.into_iter().collect();
let interval_breakpoints: Vec<f64> = interval_breakpoints.into_iter().collect();
if polynomials.len() != interval_breakpoints.len() + 1 {
return Err(Error::PieceWiseDims);
}
Ok(Self {
polynomials,
interval_breakpoints,
})
}
}
impl Function1D for PiecewiseFirstDegreePolynomial {
fn evaluate(&self, x: f64) -> f64 {
let val = self.interval_breakpoints.iter().enumerate().find_map(
|(i, breakpoint)| -> Option<f64> {
if x < *breakpoint {
Some(self.polynomials[i].evaluate(x))
} else {
None
}
},
);
match val {
Some(num) => num,
None => self.polynomials[self.interval_breakpoints.len()].evaluate(x),
}
}
}
impl Differentiable1D<PiecewiseFirstDegreePolynomial> for PiecewiseFirstDegreePolynomial {
fn differentiate(&self) -> Result<PiecewiseFirstDegreePolynomial,Error> {
let diff_polynomials: Vec<FirstDegreePolynomial> = self
.polynomials
.iter()
.map(|pol| -> Result<FirstDegreePolynomial,Error> { pol.differentiate() })
.collect::<Result<Vec<FirstDegreePolynomial>,_>>()?;
PiecewiseFirstDegreePolynomial::from_polynomials(
diff_polynomials,
self.interval_breakpoints.clone()
)
}
}