Thu, 26 Feb 2026 12:55:38 -0500
Oops, a τ had dropped in forward_pdps.
| 0 | 1 | //! Implementation of the hat function |
| 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 crate::types::Lipschitz; |
|
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::bisection_tree::{Constant, 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
|
5 | use alg_tools::bounds::{Bounded, Bounds, GlobalAnalysis, LocalAnalysis}; |
|
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::error::DynResult; |
|
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::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
|
8 | 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
|
9 | 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
|
10 | 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
|
11 | 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
|
12 | use alg_tools::types::*; |
| 0 | 13 | use numeric_literals::replace_float_literals; |
| 14 | use serde::Serialize; | |
| 15 | ||
| 16 | /// Representation of the hat function $f(x)=1-\\|x\\|\_1/ε$ of `width` $ε$ on $ℝ^N$. | |
|
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
|
17 | #[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
|
18 | pub struct Hat<C: Constant, const N: usize> { |
| 0 | 19 | /// The parameter $ε>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
|
20 | pub width: C, |
| 0 | 21 | } |
| 22 | ||
| 23 | #[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
|
24 | impl<'a, C: Constant, const N: usize> Mapping<Loc<N, C::Type>> for Hat<C, N> { |
| 35 | 25 | type Codomain = C::Type; |
| 26 | ||
| 0 | 27 | #[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
|
28 | fn apply<I: Instance<Loc<N, C::Type>>>(&self, x: I) -> Self::Codomain { |
| 0 | 29 | 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
|
30 | 0.0.max(1.0 - x.decompose().norm(L1) / ε) |
| 0 | 31 | } |
| 32 | } | |
| 33 | ||
| 34 | #[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
|
35 | impl<'a, C: Constant, const N: usize> Support<N, C::Type> for Hat<C, N> { |
| 0 | 36 | #[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
|
37 | fn support_hint(&self) -> Cube<N, C::Type> { |
| 0 | 38 | let ε = self.width.value(); |
| 39 | array_init(|| [-ε, ε]).into() | |
| 40 | } | |
| 41 | ||
| 42 | #[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
|
43 | fn in_support(&self, x: &Loc<N, C::Type>) -> bool { |
| 0 | 44 | x.norm(L1) < self.width.value() |
| 45 | } | |
|
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
|
46 | |
| 0 | 47 | /*fn fully_in_support(&self, _cube : &Cube<C::Type,N>) -> bool { |
| 48 | todo!("Not implemented, but not used at the moment") | |
| 49 | }*/ | |
| 50 | ||
| 51 | #[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
|
52 | fn bisection_hint(&self, cube: &Cube<N, C::Type>) -> [Option<C::Type>; N] { |
| 0 | 53 | let ε = self.width.value(); |
| 54 | cube.map(|a, b| { | |
| 55 | if a < 1.0 { | |
| 56 | if 1.0 < b { | |
| 57 | Some(1.0) | |
| 58 | } else { | |
| 59 | if a < -ε { | |
|
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
|
60 | if b > -ε { |
|
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
|
61 | Some(-ε) |
|
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
|
62 | } 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
|
63 | None |
|
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
|
64 | } |
| 0 | 65 | } else { |
| 66 | None | |
| 67 | } | |
| 68 | } | |
| 69 | } else { | |
|
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
|
70 | if b > ε { |
|
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
|
71 | Some(ε) |
|
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
|
72 | } 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
|
73 | None |
|
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 | } |
| 0 | 75 | } |
| 76 | }); | |
| 77 | todo!("also diagonals") | |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 81 | #[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
|
82 | impl<'a, C: Constant, const N: usize> GlobalAnalysis<C::Type, Bounds<C::Type>> for Hat<C, N> { |
| 0 | 83 | #[inline] |
| 84 | fn global_analysis(&self) -> Bounds<C::Type> { | |
| 85 | Bounds(0.0, 1.0) | |
| 86 | } | |
| 87 | } | |
| 88 | ||
| 35 | 89 | #[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
|
90 | impl<'a, C: Constant, const N: usize> Lipschitz<L1> for Hat<C, N> { |
| 35 | 91 | type FloatType = C::Type; |
| 92 | ||
|
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
|
93 | fn lipschitz_factor(&self, _l1: L1) -> DynResult<C::Type> { |
|
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 | Ok(1.0 / self.width.value()) |
| 35 | 95 | } |
| 96 | } | |
| 97 | ||
| 98 | #[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
|
99 | impl<'a, C: Constant, const N: usize> Lipschitz<L2> for Hat<C, N> { |
| 35 | 100 | type FloatType = C::Type; |
| 101 | ||
|
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
|
102 | fn lipschitz_factor(&self, _l2: L2) -> DynResult<C::Type> { |
|
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 | self.lipschitz_factor(L1) |
|
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 | .map(|l1| <L2 as Dominated<C::Type, L1, Loc<N, C::Type>>>::from_norm(&L2, l1, L1)) |
| 35 | 105 | } |
| 106 | } | |
| 107 | ||
|
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
|
108 | impl<'a, C: Constant, const N: usize> LocalAnalysis<C::Type, Bounds<C::Type>, N> for Hat<C, N> { |
| 0 | 109 | #[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
|
110 | fn local_analysis(&self, cube: &Cube<N, C::Type>) -> Bounds<C::Type> { |
| 0 | 111 | // The function is maximised/minimised where the 1-norm is minimised/maximised. |
| 112 | let lower = self.apply(cube.maxnorm_point()); | |
| 113 | let upper = self.apply(cube.minnorm_point()); | |
| 114 | Bounds(lower, upper) | |
| 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 Hat<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 | self.bounds().upper() |
| 123 | } | |
| 124 | } |