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
use crate::Error;
use super::{polynomials_1d::{SecondDegreePolynomial, FirstDegreePolynomial}, piecewise_polynomials_1degree::PiecewiseFirstDegreePolynomial};
use crate::solvers::basis::functions::{Function1D,Differentiable1D};
#[derive(PartialEq, Debug)]
pub struct PiecewiseSecondDegreePolynomial {
polynomials: Vec<SecondDegreePolynomial>,
interval_breakpoints: Vec<f64>,
}
impl PiecewiseSecondDegreePolynomial {
pub fn from_values<A: IntoIterator<Item = f64>, B: IntoIterator<Item = f64>>(
quadratic_coefficients: A,
linear_coefficients: A,
independent_terms: A,
interval_breakpoints: B,
) -> Result<Self, Error> {
let independent_terms: Vec<f64> = independent_terms.into_iter().collect();
let linear_coefficients: Vec<f64> = linear_coefficients.into_iter().collect();
let quadratic_coefficients: Vec<f64> = quadratic_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() != linear_coefficients.len()
|| independent_terms.len() != quadratic_coefficients.len()
|| linear_coefficients.len() != independent_terms.len()
{
return Err(Error::PieceWiseDims);
}
let polynomials = quadratic_coefficients
.into_iter()
.zip(linear_coefficients)
.zip(independent_terms)
.map(|((quad_coef, lin_coef), i_term)| -> SecondDegreePolynomial {
SecondDegreePolynomial::new(quad_coef,lin_coef,i_term)
})
.collect();
Ok(Self {
polynomials,
interval_breakpoints,
})
}
pub fn from_polynomials<A: IntoIterator<Item = SecondDegreePolynomial>, B: IntoIterator<Item = f64>>(
polynomials: A,
interval_breakpoints: B,
) -> Result<Self, Error> {
let polynomials: Vec<SecondDegreePolynomial> = 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 PiecewiseSecondDegreePolynomial {
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 PiecewiseSecondDegreePolynomial {
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()
)
}
}