dzahui/solvers/fem/basis/two_variables/
polynomials_2d.rs1use crate::solvers::basis::functions::{Function2D, Function2D2D, Composable2D, Differentiable2D};
3
4#[derive(PartialEq, Debug)]
5pub struct FirstDegreePolynomial2D {
16 pub(crate) x_coefficient: f64,
17 pub(crate) y_coefficient: f64,
18 pub(crate) independent_term: f64
19}
20
21#[derive(PartialEq, Debug)]
22pub struct SecondDegreePolynomial2D {
36 x_quadratic_coefficient: f64,
37 y_quadratic_coefficient: f64,
38 xy_coefficient: f64,
39 x_linear_coefficient: f64,
40 y_linear_coefficient: f64,
41 independent_term: f64,
42}
43
44pub struct Transformation2D {
57 a: f64,
58 b: f64,
59 c: f64,
60 d: f64
61}
62
63impl Transformation2D {
64
65 pub fn new(a: f64, b: f64, c: f64, d: f64) -> Transformation2D {
67 Transformation2D {
68 a,
69 b,
70 c,
71 d
72 }
73 }
74
75 pub fn inverse(self) -> Transformation2D {
77
78 let determinant = 1_f64 / (self.a * self.d - self.b * self.c);
79
80 Transformation2D {
81 a: self.d * determinant,
82 b: - self.b * determinant,
83 c: - self.c * determinant,
84 d: self.a * determinant
85 }
86 }
87
88}
89
90impl Function2D2D for Transformation2D {
91 fn evaluate(&self, x: f64, y: f64) -> (f64,f64) {
92 (self.a * x + self.b * y, self.c * x + self.d * y)
93 }
94}
95
96
97impl FirstDegreePolynomial2D {
98 pub fn new(x_coefficient: f64, y_coefficient: f64, independent_term: f64) -> FirstDegreePolynomial2D {
100 FirstDegreePolynomial2D {
101 x_coefficient,
102 y_coefficient,
103 independent_term,
104 }
105 }
106
107 pub fn zero() -> FirstDegreePolynomial2D {
109 Self {
110 x_coefficient: 0_f64,
111 y_coefficient: 0_f64,
112 independent_term: 0_f64,
113 }
114 }
115
116 pub fn constant(independent_term: f64) -> FirstDegreePolynomial2D {
118 Self {
119 x_coefficient: 0_f64,
120 y_coefficient: 0_f64,
121 independent_term,
122 }
123 }
124
125 pub fn translate(self, w: f64, z: f64) -> FirstDegreePolynomial2D {
127 Self {
128 x_coefficient: self.x_coefficient,
129 y_coefficient: self.y_coefficient,
130 independent_term: self.independent_term - self.x_coefficient * w - self.y_coefficient * z,
131 }
132 }
133
134 pub fn psi_1() -> FirstDegreePolynomial2D {
136 FirstDegreePolynomial2D {
137 x_coefficient: -1_f64,
138 y_coefficient: -1_f64,
139 independent_term: 1_f64,
140 }
141 }
142
143 pub fn psi_2() -> FirstDegreePolynomial2D {
145 FirstDegreePolynomial2D {
146 x_coefficient: 1_f64,
147 y_coefficient: 0_f64,
148 independent_term: 0_f64,
149 }
150 }
151
152 pub fn psi_3() -> FirstDegreePolynomial2D {
154 FirstDegreePolynomial2D {
155 x_coefficient: 0_f64,
156 y_coefficient: 1_f64,
157 independent_term: 0_f64
158 }
159 }
160}
161
162impl Function2D for FirstDegreePolynomial2D {
163 fn evaluate(&self, x: f64, y: f64) -> f64 {
164 self.x_coefficient * x + self.y_coefficient * y + self.independent_term
165 }
166}
167
168impl Composable2D<Transformation2D, FirstDegreePolynomial2D> for FirstDegreePolynomial2D {
169
170 fn compose(self, other: Transformation2D) -> Result<FirstDegreePolynomial2D,crate::Error> {
171
172 let x_coefficient = other.a * self.x_coefficient + other.c * self.y_coefficient;
173 let y_coefficient = other.b * self.x_coefficient + other.d * self.y_coefficient;
174
175 Ok(FirstDegreePolynomial2D {
176 x_coefficient,
177 y_coefficient,
178 independent_term: self.independent_term,
179 })
180
181 }
182}
183
184impl Differentiable2D<FirstDegreePolynomial2D,FirstDegreePolynomial2D> for FirstDegreePolynomial2D {
185
186 fn differentiate_x(&self) -> Result<FirstDegreePolynomial2D,crate::Error> {
187 Ok(
188 FirstDegreePolynomial2D {
189 x_coefficient: 0_f64,
190 y_coefficient: 0_f64,
191 independent_term: self.x_coefficient
192 }
193 )
194
195 }
196
197 fn differentiate_y(&self) -> Result<FirstDegreePolynomial2D,crate::Error> {
198 Ok(
199 FirstDegreePolynomial2D {
200 x_coefficient: 0_f64,
201 y_coefficient: 0_f64,
202 independent_term: self.y_coefficient
203 }
204 )
205
206 }
207}