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
use super::piecewise_polynomials_1degree::PiecewiseFirstDegreePolynomial;
use super::polynomials_1d::FirstDegreePolynomial;
use crate::solvers::basis::functions::Composable1D;
use crate::Error;
pub(crate) struct LinearBasis {
pub(crate) basis: Vec<PiecewiseFirstDegreePolynomial>,
}
impl LinearBasis {
pub(crate) fn new(mesh: &Vec<f64>) -> Result<LinearBasis, Error> {
let transformation = FirstDegreePolynomial::transformation_to_0_1(mesh[0], mesh[1]);
let initial_transform_function = FirstDegreePolynomial::phi_2().compose(transformation)?;
let first_function = PiecewiseFirstDegreePolynomial::from_polynomials(
vec![
FirstDegreePolynomial::zero(),
initial_transform_function,
FirstDegreePolynomial::zero(),
],
vec![mesh[0], mesh[1]],
)?;
let mut basis_vec = vec![first_function];
mesh.iter()
.zip(mesh.iter().skip(1))
.zip(mesh.iter().skip(2))
.map(|((prev, cur), next)| -> Result<(), Error> {
let transformation = FirstDegreePolynomial::transformation_to_0_1(*prev, *cur);
let basis_left = FirstDegreePolynomial::phi_1().compose(transformation)?;
let transformation = FirstDegreePolynomial::transformation_to_0_1(*cur, *next);
let basis_right = FirstDegreePolynomial::phi_2().compose(transformation)?;
let piecewise_function = PiecewiseFirstDegreePolynomial::from_polynomials(
vec![
FirstDegreePolynomial::zero(),
basis_left,
basis_right,
FirstDegreePolynomial::zero(),
],
vec![*prev, *cur, *next],
)?;
basis_vec.push(piecewise_function);
Ok(())
})
.collect::<Result<(), Error>>()?;
let transformation = FirstDegreePolynomial::transformation_to_0_1(
mesh[mesh.len() - 2],
mesh[mesh.len() - 1],
);
let final_transform_function = FirstDegreePolynomial::phi_1().compose(transformation)?;
let final_function = PiecewiseFirstDegreePolynomial::from_polynomials(
vec![
FirstDegreePolynomial::zero(),
final_transform_function,
FirstDegreePolynomial::zero(),
],
vec![mesh[mesh.len() - 2], mesh[mesh.len() - 1]],
)?;
basis_vec.push(final_function);
Ok(LinearBasis { basis: basis_vec })
}
}
#[cfg(test)]
mod test {
use super::LinearBasis;
use super::PiecewiseFirstDegreePolynomial;
#[test]
fn transform_basis_three_nodes() {
let mesh = vec![0_f64, 1_f64, 2_f64];
let transformed = LinearBasis::new(&mesh).unwrap();
assert!(transformed.basis.len() == 3);
let first_pol = PiecewiseFirstDegreePolynomial::from_values(
[0_f64, -1_f64, 0_f64],
[0_f64, 1_f64, 0_f64],
[0_f64, 1_f64],
)
.unwrap();
let second_pol = PiecewiseFirstDegreePolynomial::from_values(
[0_f64, 1_f64, -1_f64, 0_f64],
[0_f64, 0_f64, 2_f64, 0_f64],
[0_f64, 1_f64, 2_f64],
)
.unwrap();
let third_pol = PiecewiseFirstDegreePolynomial::from_values(
[0_f64, 1_f64, 0_f64],
[0_f64, -1_f64, 0_f64],
[1_f64, 2_f64],
)
.unwrap();
assert!(transformed.basis[0] == first_pol);
assert!(transformed.basis[1] == second_pol);
assert!(transformed.basis[2] == third_pol);
}
}