Tue, 25 Oct 2022 23:05:40 +0300
Added NormExponent trait for exponents of norms
| 5 | 1 | /*! |
| 2 | Linear grids. | |
| 3 | ||
| 4 | These are multi-dimensional intervals $\prod_{i=1}^N [a_i, b_i]$ divided along each dimension | |
| 5 | into n_i equally-spaced nodes, with $a_i$ the first node and $b_i$ the last node along each | |
| 6 | dimension. | |
| 7 | ||
| 8 | The [`LinSpace`]s provided by this module are similar to [`num::range_step_inclusive`], but as an | |
| 9 | iterator they are [restartable][RestartableIterator] and parametrised by the number of nodes | |
| 10 | instead of a step. This way it can be ensured that $a_i$ and $b_i$ are the last and the first node. | |
| 11 | ||
| 12 | The starting points for the use of this module are the [`linspace`], [`lingrid`], and | |
| 13 | [`lingrid_centered`] functions. They return a [`LinSpace`]s that implements [`IntoIterator`] for | |
| 14 | iteration over the grid. Additional utility functions are in the [`Grid`] trait. | |
| 15 | */ | |
| 0 | 16 | |
| 17 | use crate::types::*; | |
| 18 | use crate::loc::Loc; | |
| 19 | use crate::sets::Cube; | |
| 20 | use crate::iter::{RestartableIterator, StatefulIterator}; | |
| 21 | use crate::maputil::{map2, map4}; | |
| 5 | 22 | use serde::{Serialize, Deserialize}; |
| 0 | 23 | |
| 24 | // TODO: rewrite this using crate::sets::Cube. | |
| 25 | ||
| 5 | 26 | /// An abstraction of possibly multi-dimensional linear grids. |
| 27 | /// | |
| 28 | /// `U` is typically a `F` for a `Float` `F` for one-dimensional grids created by `linspace`, | |
| 29 | /// or [`Loc`]`<F, N>` for multi-dimensional grids created by `lingrid`. | |
| 30 | /// In the first case `count` of nodes is `usize`, and in the second case `[usize; N]`. | |
| 31 | #[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)] | |
| 32 | pub struct LinSpace<U, I> { | |
| 33 | pub start : U, | |
| 34 | pub end : U, | |
| 0 | 35 | pub count : I, |
| 36 | } | |
| 37 | ||
| 5 | 38 | /// A `N`-dimensional interval divided into an indicated number of equally-spaced nodes along |
| 39 | /// each dimension. | |
| 0 | 40 | #[allow(type_alias_bounds)] // Need it to access F::CompatibleSize. |
| 41 | pub type LinGrid<F : Float, const N : usize> = LinSpace<Loc<F, N>, [usize; N]>; | |
| 42 | ||
| 43 | /// Creates a [`LinSpace`] on the real line. | |
| 44 | pub fn linspace<F : Float>(start : F, end : F, count : usize) -> LinSpace<F, usize> { | |
| 45 | LinSpace{ start : start, end : end, count : count } | |
| 46 | } | |
| 47 | ||
| 5 | 48 | /// Creates a multi-dimensional linear grid. |
| 0 | 49 | /// |
| 50 | /// The first and last point in each dimension are the boundaries of the corresponding | |
| 51 | /// dimensions of `cube`, and there are `count` nodes along each dimension. | |
| 52 | pub fn lingrid<F : Float, const N : usize>( | |
| 53 | cube : &Cube<F, N>, | |
| 54 | count : &[usize; N] | |
| 55 | ) -> LinSpace<Loc<F, N>, [usize; N]> { | |
| 56 | LinSpace{ start : cube.span_start(), end : cube.span_end(), count : *count } | |
| 57 | } | |
| 58 | ||
| 59 | /// Create a multi-dimensional linear grid with centered nodes. | |
| 60 | /// | |
| 61 | /// There are `count` along each dimension and each node has equally-sized subcube surrounding it | |
| 62 | /// inside `cube`. Thus, if $w_i$ is the width of the cube along dimension $i$, and $n_i$ the number | |
| 63 | /// of nodes, the width of the subcube along this dimension is $h_i = w_i/(n_i+1)$, and the first | |
| 64 | /// and last nodes are at a distance $h_i/2$ from the closest boundary. | |
| 65 | pub fn lingrid_centered<F : Float, const N : usize>( | |
| 66 | cube : &Cube<F, N>, | |
| 67 | count : &[usize; N] | |
| 68 | ) -> LinSpace<Loc<F, N>, [usize; N]> { | |
| 69 | let h_div_2 = map2(cube.width(), count, |w, &n| w / F::cast_from(2 * (n + 1))); | |
| 70 | let span_start = map2(cube.span_start(), &h_div_2, |a, &t| a + t).into(); | |
| 71 | let span_end = map2(cube.span_end(), &h_div_2, |b, &t| b - t).into(); | |
| 72 | LinSpace{ start : span_start, end : span_end, count : *count } | |
| 73 | } | |
| 74 | ||
| 75 | ||
| 76 | /// Iterator over a `LinSpace`. | |
| 77 | #[derive(Clone, Debug)] | |
| 78 | pub struct LinSpaceIterator<F, I> { | |
| 79 | lingrid : LinSpace<F,I>, | |
| 80 | current : Option<I>, | |
| 81 | } | |
| 82 | ||
| 5 | 83 | /// Abstraction of a linear grid over space `U` with multi-dimensional index set `I`. |
| 84 | pub trait Grid<U, I> { | |
| 85 | /// Converts a linear index `i` into a grid point. | |
| 86 | fn entry_linear_unchecked(&self, i : usize) -> U; | |
| 87 | // Converts a multi-dimensional index `i` into a grid point. | |
| 88 | fn entry_unchecked(&self, i : &I) -> U; | |
| 89 | ||
| 90 | // fn entry(&self, i : I) -> Option<F> | |
| 0 | 91 | } |
| 92 | ||
| 5 | 93 | /// Helper trait for iteration of [`Grid`]s. |
| 0 | 94 | pub trait GridIteration<F, I> { |
| 5 | 95 | /// Returns the next multi-dimensional index (not yet converted into grid point). |
| 0 | 96 | fn next_index(&mut self) -> Option<I>; |
| 97 | } | |
| 98 | ||
| 99 | impl<F : Float + CastFrom<I>, I : Unsigned> Grid<F, I> for LinSpace<F, I> { | |
| 100 | /*fn entry(&self, i : I) -> Option<F> { | |
| 101 | if i < self.count { | |
| 102 | Some(self.entry_unchecked(i)) | |
| 103 | } else { | |
| 104 | None | |
| 105 | } | |
| 106 | }*/ | |
| 107 | ||
| 108 | #[inline] | |
| 109 | fn entry_linear_unchecked(&self, i : usize) -> F { | |
| 110 | self.entry_unchecked(&I::cast_from(i)) | |
| 111 | } | |
| 112 | ||
| 113 | #[inline] | |
| 114 | fn entry_unchecked(&self, i : &I) -> F { | |
| 115 | let idx = F::cast_from(*i); | |
| 116 | let scale = F::cast_from(self.count-I::ONE); | |
| 117 | self.start + (self.end-self.start)*idx/scale | |
| 118 | } | |
| 119 | } | |
| 120 | ||
| 121 | impl<F : Float + CastFrom<I>, I : Unsigned> GridIteration<F, I> | |
| 122 | for LinSpaceIterator<F, I> { | |
| 123 | #[inline] | |
| 124 | fn next_index(&mut self) -> Option<I> { | |
| 125 | match self.current { | |
| 5 | 126 | None if I::ZERO < self.lingrid.count |
| 127 | => { self.current = Some(I::ZERO); self.current } | |
| 128 | Some(v) if v+I::ONE < self.lingrid.count | |
| 129 | => { self.current = Some(v+I::ONE); self.current } | |
| 130 | _ | |
| 131 | => { None } | |
| 0 | 132 | } |
| 133 | } | |
| 134 | } | |
| 135 | ||
| 136 | impl<F : Float + CastFrom<I>, I : Unsigned, const N : usize> Grid<Loc<F,N>, [I; N]> | |
| 137 | for LinSpace<Loc<F,N>, [I; N]> { | |
| 138 | #[inline] | |
| 139 | fn entry_linear_unchecked(&self, i_ : usize) -> Loc<F, N> { | |
| 140 | let mut i = I::cast_from(i_); | |
| 141 | let mut tmp = [I::ZERO; N]; | |
| 142 | for k in 0..N { | |
| 143 | tmp[k] = i % self.count[k]; | |
| 144 | i /= self.count[k]; | |
| 145 | } | |
| 146 | self.entry_unchecked(&tmp) | |
| 147 | } | |
| 148 | ||
| 149 | #[inline] | |
| 150 | fn entry_unchecked(&self, i : &[I; N]) -> Loc<F, N> { | |
| 151 | let LinSpace{ start, end, count } = self; | |
| 152 | map4(i, start, end, count, |&ik, &sk, &ek, &ck| { | |
| 153 | let idx = F::cast_from(ik); | |
| 154 | let scale = F::cast_from(ck-I::ONE); | |
| 155 | sk + (ek - sk) * idx / scale | |
| 156 | }).into() | |
| 157 | } | |
| 158 | } | |
| 159 | ||
| 160 | impl<F : Float + CastFrom<I>, I : Unsigned, const N : usize> GridIteration<Loc<F,N>, [I; N]> | |
| 161 | for LinSpaceIterator<Loc<F,N>, [I; N]> { | |
| 162 | ||
| 163 | #[inline] | |
| 164 | fn next_index(&mut self) -> Option<[I; N]> { | |
| 165 | match self.current { | |
| 166 | None if self.lingrid.count.iter().all(|v| I::ZERO < *v) => { | |
| 167 | self.current = Some([I::ZERO; N]); | |
| 168 | self.current | |
| 169 | }, | |
| 170 | Some(ref mut v) => { | |
| 171 | for k in 0..N { | |
| 172 | let a = v[k] + I::ONE; | |
| 173 | if a < self.lingrid.count[k] { | |
| 174 | v[k] = a; | |
| 175 | return self.current | |
| 176 | } else { | |
| 177 | v[k] = I::ZERO; | |
| 178 | } | |
| 179 | } | |
| 180 | None | |
| 181 | }, | |
| 182 | _ => None | |
| 183 | } | |
| 184 | } | |
| 185 | } | |
| 186 | ||
| 187 | impl<F, I> IntoIterator for LinSpace<F,I> | |
| 188 | where LinSpace<F, I> : Grid<F, I>, | |
| 189 | LinSpaceIterator<F, I> : GridIteration<F, I> { | |
| 190 | type Item = F; | |
| 191 | type IntoIter = LinSpaceIterator<F,I>; | |
| 192 | ||
| 193 | #[inline] | |
| 194 | fn into_iter(self) -> Self::IntoIter { | |
| 195 | LinSpaceIterator { lingrid : self, current : None } | |
| 196 | } | |
| 197 | } | |
| 198 | ||
| 199 | impl<F, I> Iterator for LinSpaceIterator<F,I> | |
| 200 | where LinSpace<F, I> : Grid<F, I>, | |
| 201 | LinSpaceIterator<F, I> : GridIteration<F, I> { | |
| 202 | type Item = F; | |
| 203 | #[inline] | |
| 204 | fn next(&mut self) -> Option<F> { | |
| 205 | self.next_index().map(|v| self.lingrid.entry_unchecked(&v)) | |
| 206 | } | |
| 207 | } | |
| 208 | ||
| 209 | impl<F, I> StatefulIterator for LinSpaceIterator<F,I> | |
| 210 | where LinSpace<F, I> : Grid<F, I>, | |
| 211 | LinSpaceIterator<F, I> : GridIteration<F, I> { | |
| 212 | #[inline] | |
| 213 | fn current(&self) -> Option<F> { | |
| 214 | self.current.as_ref().map(|c| self.lingrid.entry_unchecked(c)) | |
| 215 | } | |
| 216 | } | |
| 217 | ||
| 218 | ||
| 219 | impl<F, I> RestartableIterator for LinSpaceIterator<F,I> | |
| 220 | where LinSpace<F, I> : Grid<F, I>, | |
| 221 | LinSpaceIterator<F, I> : GridIteration<F, I> { | |
| 222 | #[inline] | |
| 223 | fn restart(&mut self) -> Option<F> { | |
| 224 | self.current = None; | |
| 225 | self.next() | |
| 226 | } | |
| 227 | } |