Thu, 19 Mar 2026 18:21:17 -0500
Remove apparently now unused dec2flt feature request
| 0 | 1 | //! Implementation of the standard mollifier |
| 2 | ||
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
3 | use alg_tools::bisection_tree::{Bounds, Constant, GlobalAnalysis, LocalAnalysis, Support}; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
4 | use alg_tools::euclidean::Euclidean; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
5 | use alg_tools::loc::Loc; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
6 | use alg_tools::mapping::{Instance, Mapping}; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
7 | use alg_tools::maputil::array_init; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
8 | use alg_tools::norms::*; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
9 | use alg_tools::sets::Cube; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
10 | use alg_tools::types::*; |
| 35 | 11 | use float_extras::f64::tgamma as gamma; |
| 0 | 12 | use numeric_literals::replace_float_literals; |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
13 | use rgsl::hypergeometric::hyperg_U; |
| 0 | 14 | use serde::Serialize; |
| 15 | ||
| 16 | /// Reresentation of the (unnormalised) standard mollifier. | |
| 17 | /// | |
| 18 | /// For the `width` parameter $ε>0$, this is | |
| 19 | /// <div>$$ | |
| 20 | /// f(x)=\begin{cases} | |
| 21 | /// e^{\frac{ε^2}{\|x\|_2^2-ε^2}}, & \|x\|_2 < ε, \\ | |
| 22 | /// 0, & \text{otherwise}. | |
| 23 | /// \end{cases} | |
| 24 | /// $$</div> | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
25 | #[derive(Copy, Clone, Serialize, Debug, Eq, PartialEq)] |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
26 | pub struct Mollifier<C: Constant, const N: usize> { |
| 0 | 27 | /// The parameter $ε$ of the mollifier. |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
28 | pub width: C, |
| 0 | 29 | } |
| 30 | ||
| 31 | #[replace_float_literals(C::Type::cast_from(literal))] | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
32 | impl<C: Constant, const N: usize> Mapping<Loc<N, C::Type>> for Mollifier<C, N> { |
| 35 | 33 | type Codomain = C::Type; |
| 34 | ||
| 0 | 35 | #[inline] |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
36 | fn apply<I: Instance<Loc<N, C::Type>>>(&self, x: I) -> Self::Codomain { |
| 0 | 37 | let ε = self.width.value(); |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
38 | let ε2 = ε * ε; |
| 35 | 39 | let n2 = x.eval(|x| x.norm2_squared()); |
| 0 | 40 | if n2 < ε2 { |
| 41 | (n2 / (n2 - ε2)).exp() | |
| 42 | } else { | |
| 43 | 0.0 | |
| 44 | } | |
| 45 | } | |
| 46 | } | |
| 47 | ||
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
48 | impl<'a, C: Constant, const N: usize> Support<N, C::Type> for Mollifier<C, N> { |
| 0 | 49 | #[inline] |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
50 | fn support_hint(&self) -> Cube<N, C::Type> { |
| 0 | 51 | let ε = self.width.value(); |
| 52 | array_init(|| [-ε, ε]).into() | |
| 53 | } | |
| 54 | ||
| 55 | #[inline] | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
56 | fn in_support(&self, x: &Loc<N, C::Type>) -> bool { |
| 0 | 57 | x.norm2() < self.width.value() |
| 58 | } | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
59 | |
| 0 | 60 | /*fn fully_in_support(&self, _cube : &Cube<C::Type,N>) -> bool { |
| 61 | todo!("Not implemented, but not used at the moment") | |
| 62 | }*/ | |
| 63 | } | |
| 64 | ||
| 65 | #[replace_float_literals(C::Type::cast_from(literal))] | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
66 | impl<'a, C: Constant, const N: usize> GlobalAnalysis<C::Type, Bounds<C::Type>> for Mollifier<C, N> { |
| 0 | 67 | #[inline] |
| 68 | fn global_analysis(&self) -> Bounds<C::Type> { | |
| 69 | // The function is maximised/minimised where the 2-norm is minimised/maximised. | |
| 70 | Bounds(0.0, 1.0) | |
| 71 | } | |
| 72 | } | |
| 73 | ||
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
74 | impl<'a, C: Constant, const N: usize> LocalAnalysis<C::Type, Bounds<C::Type>, N> |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
75 | for Mollifier<C, N> |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
76 | { |
| 0 | 77 | #[inline] |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
78 | fn local_analysis(&self, cube: &Cube<N, C::Type>) -> Bounds<C::Type> { |
| 0 | 79 | // The function is maximised/minimised where the 2-norm is minimised/maximised. |
| 80 | let lower = self.apply(cube.maxnorm_point()); | |
| 81 | let upper = self.apply(cube.minnorm_point()); | |
| 82 | Bounds(lower, upper) | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | /// Calculate integral of the standard mollifier of width 1 in $ℝ^n$. | |
| 87 | /// | |
| 88 | /// This is based on the formula from | |
| 89 | /// [https://math.stackexchange.com/questions/4359683/integral-of-the-usual-mollifier-function-finding-its-necessary-constant](). | |
| 90 | /// | |
| 91 | /// If `rescaled` is `true`, return the integral of the scaled mollifier that has value one at the | |
| 92 | /// origin. | |
| 93 | #[inline] | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
94 | pub fn mollifier_norm1(n_: usize, rescaled: bool) -> f64 { |
| 0 | 95 | assert!(n_ > 0); |
| 96 | let n = n_ as f64; | |
| 97 | let q = 2.0; | |
| 98 | let p = 2.0; | |
| 99 | let base = (2.0*gamma(1.0 + 1.0/p)).powi(n_ as i32) | |
| 100 | /*/ gamma(1.0 + n / p) | |
| 101 | * gamma(1.0 + n / q)*/ | |
| 102 | * hyperg_U(1.0 + n / q, 2.0, 1.0); | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
103 | if rescaled { |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
104 | base |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
105 | } else { |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
106 | base / f64::E |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
107 | } |
| 0 | 108 | } |
| 109 | ||
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
110 | impl<'a, C: Constant, const N: usize> Norm<L1, C::Type> for Mollifier<C, N> { |
| 0 | 111 | #[inline] |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
112 | fn norm(&self, _: L1) -> C::Type { |
| 0 | 113 | let ε = self.width.value(); |
| 114 | C::Type::cast_from(mollifier_norm1(N, true)) * ε.powi(N as i32) | |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | #[replace_float_literals(C::Type::cast_from(literal))] | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
119 | impl<'a, C: Constant, const N: usize> Norm<Linfinity, C::Type> for Mollifier<C, N> { |
| 0 | 120 | #[inline] |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
121 | fn norm(&self, _: Linfinity) -> C::Type { |
| 0 | 122 | 1.0 |
| 123 | } | |
| 124 | } |