--- a/src/prox_penalty/radon_squared.rs Sun Apr 27 15:03:51 2025 -0500 +++ b/src/prox_penalty/radon_squared.rs Thu Feb 26 11:38:43 2026 -0500 @@ -4,81 +4,66 @@ Instead of the $𝒟$-norm of `fb.rs`, this uses a standard Radon norm for the proximal map. */ -use numeric_literals::replace_float_literals; -use serde::{Serialize, Deserialize}; +use super::{InsertionConfig, ProxPenalty, ProxTerm, StepLengthBound, StepLengthBoundPD}; +use crate::dataterm::QuadraticDataTerm; +use crate::forward_model::ForwardModel; +use crate::measures::merging::SpikeMerging; +use crate::measures::{DeltaMeasure, DiscreteMeasure, Radon}; +use crate::regularisation::RegTerm; +use crate::types::*; +use alg_tools::bounds::MinMaxMapping; +use alg_tools::error::DynResult; +use alg_tools::instance::{Instance, Space}; +use alg_tools::iterate::{AlgIterator, AlgIteratorIteration}; +use alg_tools::linops::BoundedLinear; +use alg_tools::nalgebra_support::ToNalgebraRealField; +use alg_tools::norms::{Norm, L2}; +use anyhow::ensure; use nalgebra::DVector; - -use alg_tools::iterate::{ - AlgIteratorIteration, - AlgIterator -}; -use alg_tools::norms::{L2, Norm}; -use alg_tools::linops::Mapping; -use alg_tools::bisection_tree::{ - BTFN, - Bounds, - BTSearch, - SupportGenerator, - LocalAnalysis, -}; -use alg_tools::mapping::RealMapping; -use alg_tools::nalgebra_support::ToNalgebraRealField; - -use crate::types::*; -use crate::measures::{ - RNDM, - DeltaMeasure, - Radon, -}; -use crate::measures::merging::SpikeMerging; -use crate::regularisation::RegTerm; -use crate::forward_model::{ - ForwardModel, - AdjointProductBoundedBy -}; -use super::{ - FBGenericConfig, - ProxPenalty, -}; +use numeric_literals::replace_float_literals; +use serde::{Deserialize, Serialize}; /// Radon-norm squared proximal penalty -#[derive(Copy,Clone,Serialize,Deserialize)] +#[derive(Copy, Clone, Serialize, Deserialize)] pub struct RadonSquared; #[replace_float_literals(F::cast_from(literal))] -impl<F, GA, BTA, S, Reg, const N : usize> -ProxPenalty<F, BTFN<F, GA, BTA, N>, Reg, N> for RadonSquared +impl<Domain, F, M, Reg> ProxPenalty<Domain, M, Reg, F> for RadonSquared where - F : Float + ToNalgebraRealField, - GA : SupportGenerator<F, N, SupportType = S, Id = usize> + Clone, - BTA : BTSearch<F, N, Data=usize, Agg=Bounds<F>>, - S: RealMapping<F, N> + LocalAnalysis<F, Bounds<F>, N>, - Reg : RegTerm<F, N>, - RNDM<F, N> : SpikeMerging<F>, + Domain: Space + Clone + PartialEq + 'static, + for<'a> &'a Domain: Instance<Domain>, + F: Float + ToNalgebraRealField, + M: MinMaxMapping<Domain, F>, + Reg: RegTerm<Domain, F>, + DiscreteMeasure<Domain, F>: SpikeMerging<F>, { - type ReturnMapping = BTFN<F, GA, BTA, N>; + type ReturnMapping = M; + + fn prox_type() -> ProxTerm { + ProxTerm::RadonSquared + } fn insert_and_reweigh<I>( &self, - μ : &mut RNDM<F, N>, - τv : &mut BTFN<F, GA, BTA, N>, - μ_base : &RNDM<F, N>, - ν_delta: Option<&RNDM<F, N>>, - τ : F, - ε : F, - config : &FBGenericConfig<F>, - reg : &Reg, - _state : &AlgIteratorIteration<I>, - stats : &mut IterInfo<F, N>, - ) -> (Option<Self::ReturnMapping>, bool) + μ: &mut DiscreteMeasure<Domain, F>, + τv: &mut M, + μ_base: &DiscreteMeasure<Domain, F>, + ν_delta: Option<&DiscreteMeasure<Domain, F>>, + τ: F, + ε: F, + config: &InsertionConfig<F>, + reg: &Reg, + _state: &AlgIteratorIteration<I>, + stats: &mut IterInfo<F>, + ) -> DynResult<(Option<Self::ReturnMapping>, bool)> where - I : AlgIterator + I: AlgIterator, { let mut y = μ_base.masses_dvector(); - assert!(μ_base.len() <= μ.len()); - + ensure!(μ_base.len() <= μ.len()); + 'i_and_w: for i in 0..=1 { // Optimise weights if μ.len() > 0 { @@ -86,11 +71,13 @@ // from the beginning of the iteration are all contained in the immutable c and g. // TODO: observe negation of -τv after switch from minus_τv: finite-dimensional // problems have not yet been updated to sign change. - let g̃ = DVector::from_iterator(μ.len(), - μ.iter_locations() - .map(|ζ| - F::to_nalgebra_mixed(τv.apply(ζ)))); + let g̃ = DVector::from_iterator( + μ.len(), + μ.iter_locations() + .map(|ζ| -F::to_nalgebra_mixed(τv.apply(ζ))), + ); let mut x = μ.masses_dvector(); - y.extend(std::iter::repeat(0.0.to_nalgebra_mixed()).take(0.max(x.len()-y.len()))); + y.extend(std::iter::repeat(0.0.to_nalgebra_mixed()).take(0.max(x.len() - y.len()))); assert_eq!(y.len(), x.len()); // Solve finite-dimensional subproblem. // TODO: This assumes that ν_delta has no common locations with μ-μ_base, to @@ -101,51 +88,49 @@ μ.set_masses_dvector(&x); } - if i>0 { + if i > 0 { // Simple debugging test to see if more inserts would be needed. Doesn't seem so. //let n = μ.dist_matching(μ_base); //println!("{:?}", reg.find_tolerance_violation_slack(τv, τ, ε, false, config, n)); - break 'i_and_w + break 'i_and_w; } - + // Calculate ‖μ - μ_base‖_ℳ // TODO: This assumes that ν_delta has no common locations with μ-μ_base. let n = μ.dist_matching(μ_base) + ν_delta.map_or(0.0, |ν| ν.norm(Radon)); - + // Find a spike to insert, if needed. // This only check the overall tolerances, not tolerances on support of μ-μ_base or μ, // which are supposed to have been guaranteed by the finite-dimensional weight optimisation. match reg.find_tolerance_violation_slack(τv, τ, ε, false, config, n) { - None => { break 'i_and_w }, + None => break 'i_and_w, Some((ξ, _v_ξ, _in_bounds)) => { // Weight is found out by running the finite-dimensional optimisation algorithm // above - *μ += DeltaMeasure { x : ξ, α : 0.0 }; + *μ += DeltaMeasure { x: ξ, α: 0.0 }; stats.inserted += 1; } }; } - (None, true) + Ok((None, true)) } fn merge_spikes( &self, - μ : &mut RNDM<F, N>, - τv : &mut BTFN<F, GA, BTA, N>, - μ_base : &RNDM<F, N>, - ν_delta: Option<&RNDM<F, N>>, - τ : F, - ε : F, - config : &FBGenericConfig<F>, - reg : &Reg, - fitness : Option<impl Fn(&RNDM<F, N>) -> F>, - ) -> usize - { + μ: &mut DiscreteMeasure<Domain, F>, + τv: &mut M, + μ_base: &DiscreteMeasure<Domain, F>, + ν_delta: Option<&DiscreteMeasure<Domain, F>>, + τ: F, + ε: F, + config: &InsertionConfig<F>, + reg: &Reg, + fitness: Option<impl Fn(&DiscreteMeasure<Domain, F>) -> F>, + ) -> usize { if config.fitness_merging { if let Some(f) = fitness { - return μ.merge_spikes_fitness(config.merging, f, |&v| v) - .1 + return μ.merge_spikes_fitness(config.merging, f, |&v| v).1; } } μ.merge_spikes(config.merging, |μ_candidate| { @@ -167,16 +152,27 @@ } } - -impl<F, A, const N : usize> AdjointProductBoundedBy<RNDM<F, N>, RadonSquared> -for A +#[replace_float_literals(F::cast_from(literal))] +impl<'a, F, A, Domain> StepLengthBound<F, QuadraticDataTerm<F, Domain, A>> for RadonSquared where - F : Float, - A : ForwardModel<RNDM<F, N>, F> + F: Float + ToNalgebraRealField, + Domain: Space + Norm<Radon, F>, + A: ForwardModel<Domain, F> + BoundedLinear<Domain, Radon, L2, F>, { - type FloatType = F; - - fn adjoint_product_bound(&self, _ : &RadonSquared) -> Option<Self::FloatType> { - self.opnorm_bound(Radon, L2).powi(2).into() + fn step_length_bound(&self, f: &QuadraticDataTerm<F, Domain, A>) -> DynResult<F> { + // TODO: direct squared calculation + Ok(f.operator().opnorm_bound(Radon, L2)?.powi(2)) } } + +#[replace_float_literals(F::cast_from(literal))] +impl<'a, F, A, Domain> StepLengthBoundPD<F, A, DiscreteMeasure<Domain, F>> for RadonSquared +where + Domain: Space + Clone + PartialEq + 'static, + F: Float + ToNalgebraRealField, + A: BoundedLinear<DiscreteMeasure<Domain, F>, Radon, L2, F>, +{ + fn step_length_bound_pd(&self, opA: &A) -> DynResult<F> { + opA.opnorm_bound(Radon, L2) + } +}