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