Mon, 06 Jan 2025 20:29:25 -0500
More Serialize / Deserialize / Debug derives
5 | 1 | /*! |
2 | Second order polynomical (P2) models on real intervals and planar 2D simplices. | |
3 | */ | |
4 | ||
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
5 | use serde::{Serialize, Deserialize}; |
0 | 6 | use crate::types::*; |
7 | use crate::loc::Loc; | |
8 | use crate::sets::{Set,NPolygon,SpannedHalfspace}; | |
9 | use crate::linsolve::*; | |
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
10 | use crate::euclidean::Euclidean; |
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
11 | use crate::instance::Instance; |
0 | 12 | use super::base::{LocalModel,RealLocalModel}; |
13 | use crate::sets::Cube; | |
14 | use numeric_literals::replace_float_literals; | |
15 | ||
5 | 16 | /// Type for simplices of arbitrary dimension `N`. |
17 | /// | |
18 | /// The type parameter `D` indicates the number of nodes. (Rust's const generics do not currently | |
19 | /// allow its automatic calculation from `N`.) | |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
20 | #[derive(Debug, Clone, Serialize, Deserialize)] |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
21 | #[serde(bound( |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
22 | serialize = "[Loc<F, N>; D] : Serialize", |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
23 | deserialize = "[Loc<F, N>; D] : for<'a> Deserialize<'a>", |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
24 | ))] |
0 | 25 | pub struct Simplex<F : Float, const N : usize, const D : usize>(pub [Loc<F, N>; D]); |
5 | 26 | /// A two-dimensional planar simplex |
27 | pub type PlanarSimplex<F> = Simplex<F, 2, 3>; | |
28 | /// A real interval | |
29 | pub type RealInterval<F> = Simplex<F, 1, 2>; | |
0 | 30 | |
5 | 31 | /// Calculates (a+b)/2 |
0 | 32 | #[inline] |
33 | #[replace_float_literals(F::cast_from(literal))] | |
34 | pub(crate) fn midpoint<F : Float, const N : usize>(a : &Loc<F,N>, b : &Loc<F,N>) -> Loc<F, N> { | |
35 | (a+b)/2.0 | |
36 | } | |
37 | ||
38 | impl<'a, F : Float> Set<Loc<F,1>> for RealInterval<F> { | |
39 | #[inline] | |
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
40 | fn contains<I : Instance<Loc<F, 1>>>(&self, z : I) -> bool { |
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
41 | let &Loc([x]) = z.ref_instance(); |
0 | 42 | let &[Loc([x0]), Loc([x1])] = &self.0; |
43 | (x0 < x && x < x1) || (x1 < x && x < x0) | |
44 | } | |
45 | } | |
46 | ||
47 | impl<'a, F : Float> Set<Loc<F,2>> for PlanarSimplex<F> { | |
48 | #[inline] | |
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
49 | fn contains<I : Instance<Loc<F, 2>>>(&self, z : I) -> bool { |
0 | 50 | let &[x0, x1, x2] = &self.0; |
51 | NPolygon([[x0, x1].spanned_halfspace(), | |
52 | [x1, x2].spanned_halfspace(), | |
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
53 | [x2, x0].spanned_halfspace()]).contains(z) |
0 | 54 | } |
55 | } | |
56 | ||
57 | trait P2Powers { | |
58 | type Output; | |
59 | type Diff; | |
60 | type Full; | |
61 | fn p2powers(&self) -> Self::Output; | |
62 | fn p2powers_full(&self) -> Self::Full; | |
63 | fn p2powers_diff(&self) -> Self::Diff; | |
64 | } | |
65 | ||
66 | #[replace_float_literals(F::cast_from(literal))] | |
67 | impl<F : Float> P2Powers for Loc<F, 1> { | |
68 | type Output = Loc<F, 1>; | |
69 | type Full = Loc<F, 3>; | |
70 | type Diff = Loc<Loc<F, 1>, 1>; | |
71 | ||
72 | #[inline] | |
73 | fn p2powers(&self) -> Self::Output { | |
74 | let &Loc([x0]) = self; | |
75 | [x0*x0].into() | |
76 | } | |
77 | ||
78 | #[inline] | |
79 | fn p2powers_full(&self) -> Self::Full { | |
80 | let &Loc([x0]) = self; | |
81 | [1.0, x0, x0*x0].into() | |
82 | } | |
83 | ||
84 | #[inline] | |
85 | fn p2powers_diff(&self) -> Self::Diff { | |
86 | let &Loc([x0]) = self; | |
87 | [[x0+x0].into()].into() | |
88 | } | |
89 | } | |
90 | ||
91 | #[replace_float_literals(F::cast_from(literal))] | |
92 | impl<F : Float> P2Powers for Loc<F, 2> { | |
93 | type Output = Loc<F, 3>; | |
94 | type Full = Loc<F, 6>; | |
95 | type Diff = Loc<Loc<F, 3>, 2>; | |
96 | ||
97 | #[inline] | |
98 | fn p2powers(&self) -> Self::Output { | |
99 | let &Loc([x0, x1]) = self; | |
100 | [x0*x0, x0*x1, x1*x1].into() | |
101 | } | |
102 | ||
103 | #[inline] | |
104 | fn p2powers_full(&self) -> Self::Full { | |
105 | let &Loc([x0, x1]) = self; | |
106 | [1.0, x0, x1, x0*x0, x0*x1, x1*x1].into() | |
107 | } | |
108 | ||
109 | #[inline] | |
110 | fn p2powers_diff(&self) -> Self::Diff { | |
111 | let &Loc([x0, x1]) = self; | |
112 | [[x0+x0, x1, 0.0].into(), [0.0, x0, x1+x1].into()].into() | |
113 | } | |
114 | } | |
115 | ||
5 | 116 | /// A trait for generating second order polynomial model of dimension `N` on `Self´. |
117 | /// | |
118 | /// `Self` should present a subset aset of elements of the type [`Loc`]`<F, N>`. | |
0 | 119 | pub trait P2Model<F : Num, const N : usize> { |
5 | 120 | /// Implementation type of the second order polynomical model. |
121 | /// Typically a [`P2LocalModel`]. | |
0 | 122 | type Model : LocalModel<Loc<F,N>,F>; |
5 | 123 | /// Generates a second order polynomial model of the function `g` on `Self`. |
0 | 124 | fn p2_model<G : Fn(&Loc<F, N>) -> F>(&self, g : G) -> Self::Model; |
125 | } | |
126 | ||
5 | 127 | /// A local second order polynomical model of dimension `N` with `E` edges |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
128 | #[derive(Serialize, Deserialize, Debug, Clone)] |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
129 | #[serde(bound( |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
130 | serialize = "F : Serialize, |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
131 | Loc<F, N> : Serialize, |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
132 | Loc<F, E> : Serialize", |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
133 | deserialize = "F : for<'a> Deserialize<'a>, |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
134 | Loc<F, N> : for<'a> Deserialize<'a>, |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
135 | Loc<F, E> : for<'a> Deserialize<'a>", |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
136 | ))] |
5 | 137 | pub struct P2LocalModel<F : Num, const N : usize, const E : usize/*, const V : usize, const Q : usize*/> { |
0 | 138 | a0 : F, |
139 | a1 : Loc<F, N>, | |
140 | a2 : Loc<F, E>, | |
141 | //node_values : Loc<F, V>, | |
142 | //edge_values : Loc<F, Q>, | |
143 | } | |
144 | ||
145 | // | |
146 | // 1D planar model construction | |
147 | // | |
148 | ||
149 | impl<F : Float> RealInterval<F> { | |
150 | #[inline] | |
151 | fn midpoints(&self) -> [Loc<F, 1>; 1] { | |
152 | let [ref n0, ref n1] = &self.0; | |
153 | let n01 = midpoint(n0, n1); | |
154 | [n01] | |
155 | } | |
156 | } | |
157 | ||
5 | 158 | impl<F : Float> P2LocalModel<F, 1, 1/*, 2, 0*/> { |
159 | /// Creates a new 1D second order polynomical model based on three nodal coordinates and | |
160 | /// corresponding function values. | |
0 | 161 | #[inline] |
162 | pub fn new( | |
163 | &[n0, n1, n01] : &[Loc<F, 1>; 3], | |
164 | &[v0, v1, v01] : &[F; 3], | |
165 | ) -> Self { | |
166 | let p = move |x : &Loc<F, 1>, v : F| { | |
167 | let Loc([c, d, e]) = x.p2powers_full(); | |
168 | [c, d, e, v] | |
169 | }; | |
170 | let [a0, a1, a11] = linsolve([ | |
171 | p(&n0, v0), | |
172 | p(&n1, v1), | |
173 | p(&n01, v01) | |
174 | ]); | |
175 | P2LocalModel { | |
176 | a0 : a0, | |
177 | a1 : [a1].into(), | |
178 | a2 : [a11].into(), | |
179 | //node_values : [v0, v1].into(), | |
180 | //edge_values: [].into(), | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | impl<F : Float> P2Model<F,1> for RealInterval<F> { | |
5 | 186 | type Model = P2LocalModel<F, 1, 1/*, 2, 0*/>; |
0 | 187 | |
188 | #[inline] | |
189 | fn p2_model<G : Fn(&Loc<F, 1>) -> F>(&self, g : G) -> Self::Model { | |
190 | let [n01] = self.midpoints(); | |
191 | let [n0, n1] = self.0; | |
192 | let vals = [g(&n0), g(&n1), g(&n01)]; | |
193 | let nodes = [n0, n1, n01]; | |
194 | Self::Model::new(&nodes, &vals) | |
195 | } | |
196 | } | |
197 | ||
198 | // | |
199 | // 2D planar model construction | |
200 | // | |
201 | ||
202 | impl<F : Float> PlanarSimplex<F> { | |
203 | #[inline] | |
5 | 204 | /// Returns the midpoints of all the edges of the simplex |
0 | 205 | fn midpoints(&self) -> [Loc<F, 2>; 3] { |
206 | let [ref n0, ref n1, ref n2] = &self.0; | |
207 | let n01 = midpoint(n0, n1); | |
208 | let n12 = midpoint(n1, n2); | |
209 | let n20 = midpoint(n2, n0); | |
210 | [n01, n12, n20] | |
211 | } | |
212 | } | |
213 | ||
5 | 214 | impl<F : Float> P2LocalModel<F, 2, 3/*, 3, 3*/> { |
215 | /// Creates a new 2D second order polynomical model based on six nodal coordinates and | |
216 | /// corresponding function values. | |
0 | 217 | #[inline] |
218 | pub fn new( | |
219 | &[n0, n1, n2, n01, n12, n20] : &[Loc<F, 2>; 6], | |
220 | &[v0, v1, v2, v01, v12, v20] : &[F; 6], | |
221 | ) -> Self { | |
222 | let p = move |x : &Loc<F,2>, v :F| { | |
223 | let Loc([c, d, e, f, g, h]) = x.p2powers_full(); | |
224 | [c, d, e, f, g, h, v] | |
225 | }; | |
226 | let [a0, a1, a2, a11, a12, a22] = linsolve([ | |
227 | p(&n0, v0), | |
228 | p(&n1, v1), | |
229 | p(&n2, v2), | |
230 | p(&n01, v01), | |
231 | p(&n12, v12), | |
232 | p(&n20, v20), | |
233 | ]); | |
234 | P2LocalModel { | |
235 | a0 : a0, | |
236 | a1 : [a1, a2].into(), | |
237 | a2 : [a11, a12, a22].into(), | |
238 | //node_values : [v0, v1, v2].into(), | |
239 | //edge_values: [v01, v12, v20].into(), | |
240 | } | |
241 | } | |
242 | } | |
243 | ||
244 | impl<F : Float> P2Model<F,2> for PlanarSimplex<F> { | |
5 | 245 | type Model = P2LocalModel<F, 2, 3/*, 3, 3*/>; |
0 | 246 | |
247 | #[inline] | |
248 | fn p2_model<G : Fn(&Loc<F, 2>) -> F>(&self, g : G) -> Self::Model { | |
249 | let midpoints = self.midpoints(); | |
250 | let [ref n0, ref n1, ref n2] = self.0; | |
251 | let [ref n01, ref n12, ref n20] = midpoints; | |
252 | let vals = [g(n0), g(n1), g(n2), g(n01), g(n12), g(n20)]; | |
253 | let nodes = [*n0, *n1, *n2, *n01, *n12, *n20]; | |
254 | Self::Model::new(&nodes, &vals) | |
255 | } | |
256 | } | |
257 | ||
258 | macro_rules! impl_local_model { | |
259 | ($n:literal, $e:literal, $v:literal, $q:literal) => { | |
5 | 260 | impl<F : Float> LocalModel<Loc<F, $n>, F> for P2LocalModel<F, $n, $e/*, $v, $q*/> { |
0 | 261 | #[inline] |
262 | fn value(&self, x : &Loc<F,$n>) -> F { | |
263 | self.a0 + x.dot(&self.a1) + x.p2powers().dot(&self.a2) | |
264 | } | |
265 | ||
266 | #[inline] | |
267 | fn differential(&self, x : &Loc<F,$n>) -> Loc<F,$n> { | |
268 | self.a1 + x.p2powers_diff().map(|di| di.dot(&self.a2)) | |
269 | } | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | impl_local_model!(1, 1, 2, 0); | |
275 | impl_local_model!(2, 3, 3, 3); | |
276 | ||
277 | ||
278 | // | |
279 | // Minimisation | |
280 | // | |
281 | ||
282 | #[replace_float_literals(F::cast_from(literal))] | |
5 | 283 | impl<F : Float> P2LocalModel<F, 1, 1/*, 2, 0*/> { |
284 | /// Minimises the model along the edge `[x0, x1]`. | |
0 | 285 | #[inline] |
286 | fn minimise_edge(&self, x0 : Loc<F, 1>, x1 : Loc<F,1>) -> (Loc<F,1>, F) { | |
287 | let &P2LocalModel{ | |
288 | a1 : Loc([a1]), | |
289 | a2 : Loc([a11]), | |
290 | //node_values : Loc([v0, v1]), | |
291 | .. | |
292 | } = self; | |
293 | // We do this in cases, first trying for an interior solution, then edges. | |
294 | // For interior solution, first check determinant; no point trying if non-positive | |
295 | if a11 > 0.0 { | |
296 | // An interior solution x[1] has to satisfy | |
297 | // 2a₁₁*x[1] + a₁ =0 | |
298 | // This gives | |
299 | let t = -a1/(2.0*a11); | |
300 | let (Loc([t0]), Loc([t1])) = (x0, x1); | |
301 | if (t0 <= t && t <= t1) || (t1 <= t && t <= t0) { | |
302 | let x = [t].into(); | |
303 | let v = self.value(&x); | |
304 | return (x, v) | |
305 | } | |
306 | } | |
307 | ||
308 | let v0 = self.value(&x0); | |
309 | let v1 = self.value(&x1); | |
310 | if v0 < v1 { (x0, v0) } else { (x1, v1) } | |
311 | } | |
312 | } | |
313 | ||
314 | impl<'a, F : Float> RealLocalModel<RealInterval<F>,Loc<F,1>,F> | |
5 | 315 | for P2LocalModel<F, 1, 1/*, 2, 0*/> { |
0 | 316 | #[inline] |
317 | fn minimise(&self, &Simplex([x0, x1]) : &RealInterval<F>) -> (Loc<F,1>, F) { | |
318 | self.minimise_edge(x0, x1) | |
319 | } | |
320 | } | |
321 | ||
322 | #[replace_float_literals(F::cast_from(literal))] | |
5 | 323 | impl<F : Float> P2LocalModel<F, 2, 3/*, 3, 3*/> { |
324 | /// Minimise the 2D model along the edge `[x0, x1] = {x0 + t(x1 - x0) | t ∈ [0, 1] }`. | |
0 | 325 | #[inline] |
326 | fn minimise_edge(&self, x0 : &Loc<F,2>, x1 : &Loc<F,2>/*, v0 : F, v1 : F*/) -> (Loc<F,2>, F) { | |
327 | let &P2LocalModel { | |
328 | a0, | |
329 | a1 : Loc([a1, a2]), | |
330 | a2 : Loc([a11, a12, a22]), | |
331 | .. | |
332 | } = self; | |
333 | let &Loc([x00, x01]) = x0; | |
334 | let d@Loc([d0, d1]) = x1 - x0; | |
335 | let b0 = a0 + a1*x00 + a2*x01 + a11*x00*x00 + a12*x00*x01 + a22*x01*x01; | |
336 | let b1 = a1*d0 + a2*d1 + 2.0*a11*d0*x00 + a12*(d0*x01 + d1*x00) + 2.0*a22*d1*x01; | |
337 | let b11 = a11*d0*d0 + a12*d0*d1 + a22*d1*d1; | |
338 | let edge_1d_model = P2LocalModel { | |
339 | a0 : b0, | |
340 | a1 : Loc([b1]), | |
341 | a2 : Loc([b11]), | |
342 | //node_values : Loc([v0, v1]), | |
343 | }; | |
344 | let (Loc([t]), v) = edge_1d_model.minimise_edge(0.0.into(), 1.0.into()); | |
345 | (x0 + d*t, v) | |
346 | } | |
347 | } | |
348 | ||
349 | #[replace_float_literals(F::cast_from(literal))] | |
350 | impl<'a, F : Float> RealLocalModel<PlanarSimplex<F>,Loc<F,2>,F> | |
5 | 351 | for P2LocalModel<F, 2, 3/*, 3, 3*/> { |
0 | 352 | #[inline] |
353 | fn minimise(&self, el : &PlanarSimplex<F>) -> (Loc<F,2>, F) { | |
354 | let &P2LocalModel { | |
355 | a1 : Loc([a1, a2]), | |
356 | a2 : Loc([a11, a12, a22]), | |
357 | //node_values : Loc([v0, v1, v2]), | |
358 | .. | |
359 | } = self; | |
360 | ||
361 | // We do this in cases, first trying for an interior solution, then edges. | |
362 | // For interior solution, first check determinant; no point trying if non-positive | |
363 | let r = 2.0*(a11*a22-a12*a12); | |
364 | if r > 0.0 { | |
365 | // An interior solution (x[1], x[2]) has to satisfy | |
366 | // 2a₁₁*x[1] + 2a₁₂*x[2]+a₁ =0 and 2a₂₂*x[1] + 2a₁₂*x[1]+a₂=0 | |
367 | // This gives | |
368 | let x = [(a22*a1-a12*a2)/r, (a12*a1-a11*a2)/r].into(); | |
369 | if el.contains(&x) { | |
370 | return (x, self.value(&x)) | |
371 | } | |
372 | } | |
373 | ||
374 | let &[ref x0, ref x1, ref x2] = &el.0; | |
375 | let mut min_edge = self.minimise_edge(x0, x1); | |
376 | let more_edge = [self.minimise_edge(x1, x2), | |
377 | self.minimise_edge(x2, x0)]; | |
378 | ||
379 | for edge in more_edge { | |
380 | if edge.1 < min_edge.1 { | |
381 | min_edge = edge; | |
382 | } | |
383 | } | |
384 | ||
385 | min_edge | |
386 | } | |
387 | } | |
388 | ||
389 | #[replace_float_literals(F::cast_from(literal))] | |
390 | impl<'a, F : Float> RealLocalModel<Cube<F, 2>,Loc<F,2>,F> | |
5 | 391 | for P2LocalModel<F, 2, 3/*, 3, 3*/> { |
0 | 392 | #[inline] |
393 | fn minimise(&self, el : &Cube<F, 2>) -> (Loc<F,2>, F) { | |
394 | let &P2LocalModel { | |
395 | a1 : Loc([a1, a2]), | |
396 | a2 : Loc([a11, a12, a22]), | |
397 | //node_values : Loc([v0, v1, v2]), | |
398 | .. | |
399 | } = self; | |
400 | ||
401 | // We do this in cases, first trying for an interior solution, then edges. | |
402 | // For interior solution, first check determinant; no point trying if non-positive | |
403 | let r = 2.0*(a11*a22-a12*a12); | |
404 | if r > 0.0 { | |
405 | // An interior solution (x[1], x[2]) has to satisfy | |
406 | // 2a₁₁*x[1] + 2a₁₂*x[2]+a₁ =0 and 2a₂₂*x[1] + 2a₁₂*x[1]+a₂=0 | |
407 | // This gives | |
408 | let x = [(a22*a1-a12*a2)/r, (a12*a1-a11*a2)/r].into(); | |
409 | if el.contains(&x) { | |
410 | return (x, self.value(&x)) | |
411 | } | |
412 | } | |
413 | ||
414 | let [x0, x1, x2, x3] = el.corners(); | |
415 | let mut min_edge = self.minimise_edge(&x0, &x1); | |
416 | let more_edge = [self.minimise_edge(&x1, &x2), | |
417 | self.minimise_edge(&x2, &x3), | |
418 | self.minimise_edge(&x3, &x0)]; | |
419 | ||
420 | for edge in more_edge { | |
421 | if edge.1 < min_edge.1 { | |
422 | min_edge = edge; | |
423 | } | |
424 | } | |
425 | ||
426 | min_edge | |
427 | } | |
428 | } | |
429 | ||
430 | #[cfg(test)] | |
431 | mod tests { | |
432 | use super::*; | |
433 | ||
434 | #[test] | |
435 | fn p2_model_1d_test() { | |
436 | let vertices = [Loc([0.0]), Loc([1.0])]; | |
437 | let domain = Simplex(vertices); | |
438 | // A simple quadratic function for which the approximation is exact on reals, | |
439 | // and appears exact on f64 as well. | |
440 | let f = |&Loc([x]) : &Loc<f64, 1>| x*x + x + 1.0; | |
441 | let model = domain.p2_model(f); | |
442 | let xs = [Loc([0.5]), Loc([0.25])]; | |
443 | ||
444 | for x in vertices.iter().chain(xs.iter()) { | |
445 | assert_eq!(model.value(&x), f(&x)); | |
446 | } | |
447 | ||
448 | assert_eq!(model.minimise(&domain), (Loc([0.0]), 1.0)); | |
449 | } | |
450 | ||
451 | #[test] | |
452 | fn p2_model_2d_test() { | |
453 | let vertices = [Loc([0.0, 0.0]), Loc([1.0, 0.0]), Loc([1.0, 1.0])]; | |
454 | let domain = Simplex(vertices); | |
455 | // A simple quadratic function for which the approximation is exact on reals, | |
456 | // and appears exact on f64 as well. | |
1
df3901ec2f5d
Fix some unit tests after fundamental changes that made them invalid
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
457 | let f = |&Loc([x, y]) : &Loc<f64, 2>| - (x*x + x*y + x - 2.0 * y + 1.0); |
0 | 458 | let model = domain.p2_model(f); |
459 | let xs = [Loc([0.5, 0.5]), Loc([0.25, 0.25])]; | |
460 | ||
461 | for x in vertices.iter().chain(xs.iter()) { | |
462 | assert_eq!(model.value(&x), f(&x)); | |
463 | } | |
464 | ||
1
df3901ec2f5d
Fix some unit tests after fundamental changes that made them invalid
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
465 | assert_eq!(model.minimise(&domain), (Loc([1.0, 0.0]), -3.0)); |
0 | 466 | } |
467 | } |