diff -r 9738b51d90d7 -r 4f468d35fa29 src/seminorms.rs --- a/src/seminorms.rs Sun Apr 27 15:03:51 2025 -0500 +++ b/src/seminorms.rs Thu Feb 26 11:38:43 2026 -0500 @@ -6,13 +6,15 @@ use crate::measures::{DeltaMeasure, DiscreteMeasure, Radon, SpikeIter, RNDM}; use alg_tools::bisection_tree::*; +use alg_tools::bounds::Bounded; +use alg_tools::error::DynResult; use alg_tools::instance::Instance; use alg_tools::iter::{FilterMapX, Mappable}; use alg_tools::linops::{BoundedLinear, Linear, Mapping}; use alg_tools::loc::Loc; use alg_tools::mapping::RealMapping; use alg_tools::nalgebra_support::ToNalgebraRealField; -use alg_tools::norms::Linfinity; +use alg_tools::norms::{Linfinity, Norm, NormExponent}; use alg_tools::sets::Cube; use alg_tools::types::*; use itertools::Itertools; @@ -68,37 +70,37 @@ // /// A trait alias for simple convolution kernels. -pub trait SimpleConvolutionKernel: - RealMapping + Support + Bounded + Clone + 'static +pub trait SimpleConvolutionKernel: + RealMapping + Support + Bounded + Clone + 'static { } -impl SimpleConvolutionKernel for T where - T: RealMapping + Support + Bounded + Clone + 'static +impl SimpleConvolutionKernel for T where + T: RealMapping + Support + Bounded + Clone + 'static { } /// [`SupportGenerator`] for [`ConvolutionOp`]. #[derive(Clone, Debug)] -pub struct ConvolutionSupportGenerator +pub struct ConvolutionSupportGenerator where - K: SimpleConvolutionKernel, + K: SimpleConvolutionKernel, { kernel: K, - centres: RNDM, + centres: RNDM, } -impl ConvolutionSupportGenerator +impl ConvolutionSupportGenerator where - K: SimpleConvolutionKernel, + K: SimpleConvolutionKernel, { /// Construct the convolution kernel corresponding to `δ`, i.e., one centered at `δ.x` and /// weighted by `δ.α`. #[inline] fn construct_kernel<'a>( &'a self, - δ: &'a DeltaMeasure, F>, - ) -> Weighted, F> { + δ: &'a DeltaMeasure, F>, + ) -> Weighted, F> { self.kernel.clone().shift(δ.x).weigh(δ.α) } @@ -108,21 +110,21 @@ #[inline] fn construct_kernel_and_id_filtered<'a>( &'a self, - (id, δ): (usize, &'a DeltaMeasure, F>), - ) -> Option<(usize, Weighted, F>)> { + (id, δ): (usize, &'a DeltaMeasure, F>), + ) -> Option<(usize, Weighted, F>)> { (δ.α != F::ZERO).then(|| (id.into(), self.construct_kernel(δ))) } } -impl SupportGenerator for ConvolutionSupportGenerator +impl SupportGenerator for ConvolutionSupportGenerator where - K: SimpleConvolutionKernel, + K: SimpleConvolutionKernel, { type Id = usize; - type SupportType = Weighted, F>; + type SupportType = Weighted, F>; type AllDataIter<'a> = FilterMapX< 'a, - Zip, SpikeIter<'a, Loc, F>>, + Zip, SpikeIter<'a, Loc, F>>, Self, (Self::Id, Self::SupportType), >; @@ -150,13 +152,13 @@ pub struct ConvolutionOp where F: Float + ToNalgebraRealField, - BT: BTImpl, - K: SimpleConvolutionKernel, + BT: BTImpl, + K: SimpleConvolutionKernel, { /// Depth of the [`BT`] bisection tree for the outputs [`Mapping::apply`]. depth: BT::Depth, /// Domain of the [`BT`] bisection tree for the outputs [`Mapping::apply`]. - domain: Cube, + domain: Cube, /// The convolution kernel kernel: K, _phantoms: PhantomData<(F, BT)>, @@ -165,13 +167,13 @@ impl ConvolutionOp where F: Float + ToNalgebraRealField, - BT: BTImpl, - K: SimpleConvolutionKernel, + BT: BTImpl, + K: SimpleConvolutionKernel, { /// Creates a new convolution operator $𝒟$ with `kernel` on `domain`. /// /// The output of [`Mapping::apply`] is a [`BT`] of given `depth`. - pub fn new(depth: BT::Depth, domain: Cube, kernel: K) -> Self { + pub fn new(depth: BT::Depth, domain: Cube, kernel: K) -> Self { ConvolutionOp { depth: depth, domain: domain, @@ -181,7 +183,7 @@ } /// Returns the support generator for this convolution operator. - fn support_generator(&self, μ: RNDM) -> ConvolutionSupportGenerator { + fn support_generator(&self, μ: RNDM) -> ConvolutionSupportGenerator { // TODO: can we avoid cloning μ? ConvolutionSupportGenerator { kernel: self.kernel.clone(), @@ -195,18 +197,18 @@ } } -impl Mapping> for ConvolutionOp +impl Mapping> for ConvolutionOp where F: Float + ToNalgebraRealField, - BT: BTImpl, - K: SimpleConvolutionKernel, - Weighted, F>: LocalAnalysis, + BT: BTImpl, + K: SimpleConvolutionKernel, + Weighted, F>: LocalAnalysis, { - type Codomain = BTFN, BT, N>; + type Codomain = BTFN, BT, N>; fn apply(&self, μ: I) -> Self::Codomain where - I: Instance>, + I: Instance>, { let g = self.support_generator(μ.own()); BTFN::construct(self.domain.clone(), self.depth, g) @@ -214,46 +216,67 @@ } /// [`ConvolutionOp`]s as linear operators over [`DiscreteMeasure`]s. -impl Linear> for ConvolutionOp +impl Linear> for ConvolutionOp where F: Float + ToNalgebraRealField, - BT: BTImpl, - K: SimpleConvolutionKernel, - Weighted, F>: LocalAnalysis, + BT: BTImpl, + K: SimpleConvolutionKernel, + Weighted, F>: LocalAnalysis, { } -impl BoundedLinear, Radon, Linfinity, F> +impl BoundedLinear, Radon, Linfinity, F> for ConvolutionOp where F: Float + ToNalgebraRealField, - BT: BTImpl, - K: SimpleConvolutionKernel, - Weighted, F>: LocalAnalysis, + BT: BTImpl, + K: SimpleConvolutionKernel, + Weighted, F>: LocalAnalysis, { - fn opnorm_bound(&self, _: Radon, _: Linfinity) -> F { + fn opnorm_bound(&self, _: Radon, _: Linfinity) -> DynResult { // With μ = ∑_i α_i δ_{x_i}, we have // |𝒟μ|_∞ // = sup_z |∑_i α_i φ(z - x_i)| // ≤ sup_z ∑_i |α_i| |φ(z - x_i)| // ≤ ∑_i |α_i| |φ|_∞ // = |μ|_ℳ |φ|_∞ - self.kernel.bounds().uniform() + Ok(self.kernel.bounds().uniform()) } } -impl DiscreteMeasureOp, F> for ConvolutionOp +impl<'a, F, K, BT, const N: usize> NormExponent for &'a ConvolutionOp +where + F: Float + ToNalgebraRealField, + BT: BTImpl, + K: SimpleConvolutionKernel, + Weighted, F>: LocalAnalysis, +{ +} + +impl<'a, F, K, BT, const N: usize> Norm<&'a ConvolutionOp, F> for RNDM where F: Float + ToNalgebraRealField, - BT: BTImpl, - K: SimpleConvolutionKernel, - Weighted, F>: LocalAnalysis, + BT: BTImpl, + K: SimpleConvolutionKernel, + Weighted, F>: LocalAnalysis, { - type PreCodomain = PreBTFN, N>; + fn norm(&self, op𝒟: &'a ConvolutionOp) -> F { + self.apply(op𝒟.apply(self)).sqrt() + } +} + +impl DiscreteMeasureOp, F> for ConvolutionOp +where + F: Float + ToNalgebraRealField, + BT: BTImpl, + K: SimpleConvolutionKernel, + Weighted, F>: LocalAnalysis, +{ + type PreCodomain = PreBTFN, N>; fn findim_matrix<'a, I>(&self, points: I) -> DMatrix where - I: ExactSizeIterator> + Clone, + I: ExactSizeIterator> + Clone, { // TODO: Preliminary implementation. It be best to use sparse matrices or // possibly explicit operators without matrices @@ -268,7 +291,7 @@ /// A version of [`Mapping::apply`] that does not instantiate the [`BTFN`] codomain with /// a bisection tree, instead returning a [`PreBTFN`]. This can improve performance when /// the output is to be added as the right-hand-side operand to a proper BTFN. - fn preapply(&self, μ: RNDM) -> Self::PreCodomain { + fn preapply(&self, μ: RNDM) -> Self::PreCodomain { BTFN::new_pre(self.support_generator(μ)) } } @@ -277,27 +300,27 @@ /// for [`ConvolutionSupportGenerator`]. macro_rules! make_convolutionsupportgenerator_scalarop_rhs { ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { - impl, const N: usize> std::ops::$trait_assign - for ConvolutionSupportGenerator + impl, const N: usize> std::ops::$trait_assign + for ConvolutionSupportGenerator { fn $fn_assign(&mut self, t: F) { self.centres.$fn_assign(t); } } - impl, const N: usize> std::ops::$trait - for ConvolutionSupportGenerator + impl, const N: usize> std::ops::$trait + for ConvolutionSupportGenerator { - type Output = ConvolutionSupportGenerator; + type Output = ConvolutionSupportGenerator; fn $fn(mut self, t: F) -> Self::Output { std::ops::$trait_assign::$fn_assign(&mut self.centres, t); self } } - impl<'a, F: Float, K: SimpleConvolutionKernel, const N: usize> std::ops::$trait - for &'a ConvolutionSupportGenerator + impl<'a, F: Float, K: SimpleConvolutionKernel, const N: usize> std::ops::$trait + for &'a ConvolutionSupportGenerator { - type Output = ConvolutionSupportGenerator; + type Output = ConvolutionSupportGenerator; fn $fn(self, t: F) -> Self::Output { ConvolutionSupportGenerator { kernel: self.kernel.clone(), @@ -314,20 +337,20 @@ /// Generates an unary operation (e.g. [`std::ops::Neg`]) for [`ConvolutionSupportGenerator`]. macro_rules! make_convolutionsupportgenerator_unaryop { ($trait:ident, $fn:ident) => { - impl, const N: usize> std::ops::$trait - for ConvolutionSupportGenerator + impl, const N: usize> std::ops::$trait + for ConvolutionSupportGenerator { - type Output = ConvolutionSupportGenerator; + type Output = ConvolutionSupportGenerator; fn $fn(mut self) -> Self::Output { self.centres = self.centres.$fn(); self } } - impl<'a, F: Float, K: SimpleConvolutionKernel, const N: usize> std::ops::$trait - for &'a ConvolutionSupportGenerator + impl<'a, F: Float, K: SimpleConvolutionKernel, const N: usize> std::ops::$trait + for &'a ConvolutionSupportGenerator { - type Output = ConvolutionSupportGenerator; + type Output = ConvolutionSupportGenerator; fn $fn(self) -> Self::Output { ConvolutionSupportGenerator { kernel: self.kernel.clone(),