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