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