Tue, 08 Apr 2025 13:31:39 -0500
Bump alg_tools version requirement
| 0 | 1 | |
| 2 | //! Implementation of the indicator function of a ball with respect to various norms. | |
| 35 | 3 | use float_extras::f64::tgamma as gamma; |
| 0 | 4 | use numeric_literals::replace_float_literals; |
| 5 | use serde::Serialize; | |
| 6 | use alg_tools::types::*; | |
| 7 | use alg_tools::norms::*; | |
| 8 | use alg_tools::loc::Loc; | |
| 9 | use alg_tools::sets::Cube; | |
| 10 | use alg_tools::bisection_tree::{ | |
| 11 | Support, | |
| 12 | Constant, | |
| 13 | Bounds, | |
| 14 | LocalAnalysis, | |
| 15 | GlobalAnalysis, | |
| 16 | }; | |
| 35 | 17 | use alg_tools::mapping::{ |
| 18 | Mapping, | |
| 19 | Differential, | |
| 20 | DifferentiableImpl, | |
| 21 | }; | |
| 22 | use alg_tools::instance::Instance; | |
| 23 | use alg_tools::euclidean::StaticEuclidean; | |
| 0 | 24 | use alg_tools::maputil::array_init; |
| 25 | use alg_tools::coefficients::factorial; | |
| 35 | 26 | use crate::types::*; |
| 0 | 27 | use super::base::*; |
| 28 | ||
| 29 | /// Representation of the indicator of the ball $𝔹_q = \\{ x ∈ ℝ^N \mid \\|x\\|\_q ≤ r \\}$, | |
| 30 | /// where $q$ is the `Exponent`, and $r$ is the radius [`Constant`] `C`. | |
| 31 | #[derive(Copy,Clone,Serialize,Debug,Eq,PartialEq)] | |
| 32 | pub struct BallIndicator<C : Constant, Exponent : NormExponent, const N : usize> { | |
| 33 | /// The radius of the ball. | |
| 34 | pub r : C, | |
| 35 | /// The exponent $q$ of the norm creating the ball | |
| 36 | pub exponent : Exponent, | |
| 37 | } | |
| 38 | ||
| 39 | /// Alias for the representation of the indicator of the $∞$-norm-ball | |
| 40 | /// $𝔹_∞ = \\{ x ∈ ℝ^N \mid \\|x\\|\_∞ ≤ c \\}$. | |
| 41 | pub type CubeIndicator<C, const N : usize> = BallIndicator<C, Linfinity, N>; | |
| 42 | ||
| 43 | #[replace_float_literals(C::Type::cast_from(literal))] | |
| 44 | impl<'a, F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> | |
| 35 | 45 | Mapping<Loc<C::Type, N>> |
| 0 | 46 | for BallIndicator<C, Exponent, N> |
| 35 | 47 | where |
| 48 | Loc<F, N> : Norm<F, Exponent> | |
| 49 | { | |
| 50 | type Codomain = C::Type; | |
| 51 | ||
| 0 | 52 | #[inline] |
| 35 | 53 | fn apply<I : Instance<Loc<C::Type, N>>>(&self, x : I) -> Self::Codomain { |
| 0 | 54 | let r = self.r.value(); |
| 35 | 55 | let n = x.eval(|x| x.norm(self.exponent)); |
| 0 | 56 | if n <= r { |
| 57 | 1.0 | |
| 58 | } else { | |
| 59 | 0.0 | |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 35 | 64 | impl<'a, F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> |
| 65 | DifferentiableImpl<Loc<C::Type, N>> | |
| 66 | for BallIndicator<C, Exponent, N> | |
| 67 | where | |
| 68 | C : Constant, | |
| 69 | Loc<F, N> : Norm<F, Exponent> | |
| 70 | { | |
| 71 | type Derivative = Loc<C::Type, N>; | |
| 72 | ||
| 73 | #[inline] | |
| 74 | fn differential_impl<I : Instance<Loc<C::Type, N>>>(&self, _x : I) -> Self::Derivative { | |
| 75 | Self::Derivative::origin() | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 0 | 79 | impl<F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> |
| 35 | 80 | Lipschitz<L2> |
| 0 | 81 | for BallIndicator<C, Exponent, N> |
| 35 | 82 | where C : Constant, |
| 83 | Loc<F, N> : Norm<F, Exponent> { | |
| 84 | type FloatType = C::Type; | |
| 85 | ||
| 86 | fn lipschitz_factor(&self, _l2 : L2) -> Option<C::Type> { | |
| 87 | None | |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 91 | impl<'b, F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> | |
| 92 | Lipschitz<L2> | |
| 93 | for Differential<'b, Loc<F, N>, BallIndicator<C, Exponent, N>> | |
| 94 | where C : Constant, | |
| 95 | Loc<F, N> : Norm<F, Exponent> { | |
| 96 | type FloatType = C::Type; | |
| 97 | ||
| 98 | fn lipschitz_factor(&self, _l2 : L2) -> Option<C::Type> { | |
| 99 | None | |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | impl<'a, 'b, F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> | |
| 104 | Lipschitz<L2> | |
| 105 | for Differential<'b, Loc<F, N>, &'a BallIndicator<C, Exponent, N>> | |
| 106 | where C : Constant, | |
| 107 | Loc<F, N> : Norm<F, Exponent> { | |
| 108 | type FloatType = C::Type; | |
| 109 | ||
| 110 | fn lipschitz_factor(&self, _l2 : L2) -> Option<C::Type> { | |
| 111 | None | |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 115 | ||
| 116 | impl<'b, F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> | |
| 117 | NormBounded<L2> | |
| 118 | for Differential<'b, Loc<F, N>, BallIndicator<C, Exponent, N>> | |
| 119 | where C : Constant, | |
| 120 | Loc<F, N> : Norm<F, Exponent> { | |
| 121 | type FloatType = C::Type; | |
| 122 | ||
| 123 | fn norm_bound(&self, _l2 : L2) -> C::Type { | |
| 124 | F::INFINITY | |
| 125 | } | |
| 126 | } | |
| 127 | ||
| 128 | impl<'a, 'b, F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> | |
| 129 | NormBounded<L2> | |
| 130 | for Differential<'b, Loc<F, N>, &'a BallIndicator<C, Exponent, N>> | |
| 131 | where C : Constant, | |
| 132 | Loc<F, N> : Norm<F, Exponent> { | |
| 133 | type FloatType = C::Type; | |
| 134 | ||
| 135 | fn norm_bound(&self, _l2 : L2) -> C::Type { | |
| 136 | F::INFINITY | |
| 0 | 137 | } |
| 138 | } | |
| 139 | ||
| 140 | ||
| 141 | impl<'a, F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> | |
| 142 | Support<C::Type, N> | |
| 143 | for BallIndicator<C, Exponent, N> | |
| 144 | where Loc<F, N> : Norm<F, Exponent>, | |
| 145 | Linfinity : Dominated<F, Exponent, Loc<F, N>> { | |
| 146 | ||
| 147 | #[inline] | |
| 148 | fn support_hint(&self) -> Cube<F,N> { | |
| 149 | let r = Linfinity.from_norm(self.r.value(), self.exponent); | |
| 150 | array_init(|| [-r, r]).into() | |
| 151 | } | |
| 152 | ||
| 153 | #[inline] | |
| 154 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
| 155 | let r = Linfinity.from_norm(self.r.value(), self.exponent); | |
| 156 | x.norm(self.exponent) <= r | |
| 157 | } | |
| 158 | ||
| 159 | /// This can only really work in a reasonable fashion for N=1. | |
| 160 | #[inline] | |
| 161 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
| 162 | let r = Linfinity.from_norm(self.r.value(), self.exponent); | |
| 163 | cube.map(|a, b| symmetric_interval_hint(r, a, b)) | |
| 164 | } | |
| 165 | } | |
| 166 | ||
| 167 | #[replace_float_literals(F::cast_from(literal))] | |
| 168 | impl<'a, F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> | |
| 169 | GlobalAnalysis<F, Bounds<F>> | |
| 170 | for BallIndicator<C, Exponent, N> | |
| 171 | where Loc<F, N> : Norm<F, Exponent> { | |
| 172 | #[inline] | |
| 173 | fn global_analysis(&self) -> Bounds<F> { | |
| 174 | Bounds(0.0, 1.0) | |
| 175 | } | |
| 176 | } | |
| 177 | ||
| 178 | #[replace_float_literals(F::cast_from(literal))] | |
| 179 | impl<'a, F : Float, C : Constant<Type=F>, Exponent : NormExponent, const N : usize> | |
| 180 | Norm<F, Linfinity> | |
| 181 | for BallIndicator<C, Exponent, N> | |
| 182 | where Loc<F, N> : Norm<F, Exponent> { | |
| 183 | #[inline] | |
| 184 | fn norm(&self, _ : Linfinity) -> F { | |
| 185 | 1.0 | |
| 186 | } | |
| 187 | } | |
| 188 | ||
| 189 | #[replace_float_literals(F::cast_from(literal))] | |
| 190 | impl<'a, F : Float, C : Constant<Type=F>, const N : usize> | |
| 191 | Norm<F, L1> | |
| 192 | for BallIndicator<C, L1, N> { | |
| 193 | #[inline] | |
| 194 | fn norm(&self, _ : L1) -> F { | |
| 195 | // Using https://en.wikipedia.org/wiki/Volume_of_an_n-ball#Balls_in_Lp_norms, | |
| 196 | // we have V_N^1(r) = (2r)^N / N! | |
| 197 | let r = self.r.value(); | |
| 198 | if N==1 { | |
| 199 | 2.0 * r | |
| 200 | } else if N==2 { | |
| 201 | r*r | |
| 202 | } else { | |
| 203 | (2.0 * r).powi(N as i32) * F::cast_from(factorial(N)) | |
| 204 | } | |
| 205 | } | |
| 206 | } | |
| 207 | ||
| 208 | #[replace_float_literals(F::cast_from(literal))] | |
| 209 | impl<'a, F : Float, C : Constant<Type=F>, const N : usize> | |
| 210 | Norm<F, L1> | |
| 211 | for BallIndicator<C, L2, N> { | |
| 212 | #[inline] | |
| 213 | fn norm(&self, _ : L1) -> F { | |
| 214 | // See https://en.wikipedia.org/wiki/Volume_of_an_n-ball#The_volume. | |
| 215 | let r = self.r.value(); | |
| 216 | let π = F::PI; | |
| 217 | if N==1 { | |
| 218 | 2.0 * r | |
| 219 | } else if N==2 { | |
| 220 | π * (r * r) | |
| 221 | } else { | |
| 222 | let ndiv2 = F::cast_from(N) / 2.0; | |
| 223 | let γ = F::cast_from(gamma((ndiv2 + 1.0).as_())); | |
| 224 | π.powf(ndiv2) / γ * r.powi(N as i32) | |
| 225 | } | |
| 226 | } | |
| 227 | } | |
| 228 | ||
| 229 | #[replace_float_literals(F::cast_from(literal))] | |
| 230 | impl<'a, F : Float, C : Constant<Type=F>, const N : usize> | |
| 231 | Norm<F, L1> | |
| 232 | for BallIndicator<C, Linfinity, N> { | |
| 233 | #[inline] | |
| 234 | fn norm(&self, _ : L1) -> F { | |
| 235 | let two_r = 2.0 * self.r.value(); | |
| 236 | two_r.powi(N as i32) | |
| 237 | } | |
| 238 | } | |
| 239 | ||
| 240 | ||
| 241 | macro_rules! indicator_local_analysis { | |
| 242 | ($exponent:ident) => { | |
| 243 | impl<'a, F : Float, C : Constant<Type=F>, const N : usize> | |
| 244 | LocalAnalysis<F, Bounds<F>, N> | |
| 245 | for BallIndicator<C, $exponent, N> | |
| 246 | where Loc<F, N> : Norm<F, $exponent>, | |
| 247 | Linfinity : Dominated<F, $exponent, Loc<F, N>> { | |
| 248 | #[inline] | |
| 249 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 250 | // The function is maximised/minimised where the 2-norm is minimised/maximised. | |
| 251 | let lower = self.apply(cube.maxnorm_point()); | |
| 252 | let upper = self.apply(cube.minnorm_point()); | |
| 253 | Bounds(lower, upper) | |
| 254 | } | |
| 255 | } | |
| 256 | } | |
| 257 | } | |
| 258 | ||
| 259 | indicator_local_analysis!(L1); | |
| 260 | indicator_local_analysis!(L2); | |
| 261 | indicator_local_analysis!(Linfinity); | |
| 262 | ||
| 263 | ||
| 264 | #[replace_float_literals(F::cast_from(literal))] | |
| 35 | 265 | impl<'a, F : Float, R, const N : usize> Mapping<Loc<F, N>> |
| 0 | 266 | for AutoConvolution<CubeIndicator<R, N>> |
| 267 | where R : Constant<Type=F> { | |
| 35 | 268 | type Codomain = F; |
| 0 | 269 | |
| 270 | #[inline] | |
| 35 | 271 | fn apply<I : Instance<Loc<F, N>>>(&self, y : I) -> F { |
| 0 | 272 | let two_r = 2.0 * self.0.r.value(); |
| 273 | // This is just a product of one-dimensional versions | |
| 35 | 274 | y.cow().iter().map(|&x| { |
| 0 | 275 | 0.0.max(two_r - x.abs()) |
| 276 | }).product() | |
| 277 | } | |
| 278 | } | |
| 279 | ||
| 280 | #[replace_float_literals(F::cast_from(literal))] | |
| 281 | impl<F : Float, R, const N : usize> Support<F, N> | |
| 282 | for AutoConvolution<CubeIndicator<R, N>> | |
| 283 | where R : Constant<Type=F> { | |
| 284 | #[inline] | |
| 285 | fn support_hint(&self) -> Cube<F, N> { | |
| 286 | let two_r = 2.0 * self.0.r.value(); | |
| 287 | array_init(|| [-two_r, two_r]).into() | |
| 288 | } | |
| 289 | ||
| 290 | #[inline] | |
| 291 | fn in_support(&self, y : &Loc<F, N>) -> bool { | |
| 292 | let two_r = 2.0 * self.0.r.value(); | |
| 293 | y.iter().all(|x| x.abs() <= two_r) | |
| 294 | } | |
| 295 | ||
| 296 | #[inline] | |
| 297 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
| 298 | let two_r = 2.0 * self.0.r.value(); | |
| 299 | cube.map(|c, d| symmetric_interval_hint(two_r, c, d)) | |
| 300 | } | |
| 301 | } | |
| 302 | ||
| 303 | #[replace_float_literals(F::cast_from(literal))] | |
| 304 | impl<F : Float, R, const N : usize> GlobalAnalysis<F, Bounds<F>> | |
| 305 | for AutoConvolution<CubeIndicator<R, N>> | |
| 306 | where R : Constant<Type=F> { | |
| 307 | #[inline] | |
| 308 | fn global_analysis(&self) -> Bounds<F> { | |
| 309 | Bounds(0.0, self.apply(Loc::ORIGIN)) | |
| 310 | } | |
| 311 | } | |
| 312 | ||
| 313 | impl<F : Float, R, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
| 314 | for AutoConvolution<CubeIndicator<R, N>> | |
| 315 | where R : Constant<Type=F> { | |
| 316 | #[inline] | |
| 317 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 318 | // The function is maximised/minimised where the absolute value is minimised/maximised. | |
| 319 | let lower = self.apply(cube.maxnorm_point()); | |
| 320 | let upper = self.apply(cube.minnorm_point()); | |
| 321 | Bounds(lower, upper) | |
| 322 | } | |
| 323 | } |