diff -r 1f19c6bbf07b -r 3868555d135c src/linops.rs --- a/src/linops.rs Sun Apr 27 20:29:43 2025 -0500 +++ b/src/linops.rs Fri May 15 14:46:30 2026 -0500 @@ -2,81 +2,143 @@ Abstract linear operators. */ -use numeric_literals::replace_float_literals; -use std::marker::PhantomData; -use serde::Serialize; +use crate::direct_product::Pair; +use crate::error::DynResult; +use crate::euclidean::StaticEuclidean; +use crate::instance::Instance; +pub use crate::mapping::{ClosedSpace, Composition, DifferentiableImpl, Mapping, Space}; +use crate::norms::{HasDual, Linfinity, NormExponent, PairNorm, L1, L2}; use crate::types::*; -pub use crate::mapping::{Mapping, Space, Composition}; -use crate::direct_product::Pair; -use crate::instance::Instance; -use crate::norms::{NormExponent, PairNorm, L1, L2, Linfinity, Norm}; +use numeric_literals::replace_float_literals; +use serde::Serialize; +use std::marker::PhantomData; +use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; /// Trait for linear operators on `X`. -pub trait Linear : Mapping -{ } +pub trait Linear: Mapping {} + +// impl> DifferentiableImpl for A { +// type Derivative = >::Codomain; + +// /// Compute the differential of `self` at `x`, consuming the input. +// fn differential_impl>(&self, x: I) -> Self::Derivative { +// self.apply(x) +// } +// } + +/// Vector spaces +#[replace_float_literals(Self::Field::cast_from(literal))] +pub trait VectorSpace: + Space + + Mul + + Div + + Add + + Add + + Sub + + Sub + + Neg + + for<'b> Add<&'b Self, Output = ::PrincipalV> + + for<'b> Sub<&'b Self, Output = ::PrincipalV> +{ + /// Underlying scalar field + type Field: Num; + + /// Principal form of the space; always equal to [`Space::Principal`], but with + /// more traits guaranteed. + /// + /// `PrincipalV` is only assumed to be `AXPY` for itself, as [`AXPY`] + /// uses [`Instance`] to apply all other variants and avoid problems + /// of choosing multiple implementations of the trait. + type PrincipalV: ClosedSpace + + AXPY< + Self::PrincipalV, + Field = Self::Field, + PrincipalV = Self::PrincipalV, + OwnedVariant = Self::PrincipalV, + Principal = Self::PrincipalV, + >; + + /// Return a similar zero as `self`. + fn similar_origin(&self) -> Self::PrincipalV; + // { + // self.make_origin_generator().make_origin() + // } + + /// Return a similar zero as `x`. + fn similar_origin_inst>(x: I) -> Self::PrincipalV { + x.eval(|xr| xr.similar_origin()) + } +} /// Efficient in-place summation. -#[replace_float_literals(F::cast_from(literal))] -pub trait AXPY : Space + std::ops::MulAssign +#[replace_float_literals(Self::Field::cast_from(literal))] +pub trait AXPY: + VectorSpace + + MulAssign + + DivAssign + + AddAssign + + AddAssign + + SubAssign + + SubAssign + + for<'b> AddAssign<&'b Self> + + for<'b> SubAssign<&'b Self> where - F : Num, - X : Space, + X: Space, { - type Owned : AXPY; - /// Computes `y = βy + αx`, where `y` is `Self`. - fn axpy>(&mut self, α : F, x : I, β : F); + fn axpy>(&mut self, α: Self::Field, x: I, β: Self::Field); /// Copies `x` to `self`. - fn copy_from>(&mut self, x : I) { + fn copy_from>(&mut self, x: I) { self.axpy(1.0, x, 0.0) } /// Computes `y = αx`, where `y` is `Self`. - fn scale_from>(&mut self, α : F, x : I) { + fn scale_from>(&mut self, α: Self::Field, x: I) { self.axpy(α, x, 0.0) } - /// Return a similar zero as `self`. - fn similar_origin(&self) -> Self::Owned; - /// Set self to zero. fn set_zero(&mut self); } +pub trait ClosedVectorSpace: Instance + VectorSpace {} +impl + VectorSpace> ClosedVectorSpace for X {} + /// Efficient in-place application for [`Linear`] operators. #[replace_float_literals(F::cast_from(literal))] -pub trait GEMV>::Codomain> : Linear { +pub trait GEMV>::Codomain>: Linear { /// Computes `y = αAx + βy`, where `A` is `Self`. - fn gemv>(&self, y : &mut Y, α : F, x : I, β : F); + fn gemv>(&self, y: &mut Y, α: F, x: I, β: F); #[inline] /// Computes `y = Ax`, where `A` is `Self` - fn apply_mut>(&self, y : &mut Y, x : I){ + fn apply_mut>(&self, y: &mut Y, x: I) { self.gemv(y, 1.0, x, 0.0) } #[inline] /// Computes `y += Ax`, where `A` is `Self` - fn apply_add>(&self, y : &mut Y, x : I){ + fn apply_add>(&self, y: &mut Y, x: I) { self.gemv(y, 1.0, x, 1.0) } } - /// Bounded linear operators -pub trait BoundedLinear : Linear +pub trait BoundedLinear: Linear where - F : Num, - X : Space + Norm, - XExp : NormExponent, - CodExp : NormExponent + F: Num, + X: Space, + XExp: NormExponent, + CodExp: NormExponent, { /// A bound on the operator norm $\|A\|$ for the linear operator $A$=`self`. /// This is not expected to be the norm, just any bound on it that can be /// reasonably implemented. The [`NormExponent`] `xexp` indicates the norm /// in `X`, and `codexp` in the codomain. - fn opnorm_bound(&self, xexp : XExp, codexp : CodExp) -> F; + /// + /// This may fail with an error if the bound is for some reason incalculable. + fn opnorm_bound(&self, xexp: XExp, codexp: CodExp) -> DynResult; } // Linear operator application into mutable target. The [`AsRef`] bound @@ -90,18 +152,45 @@ }*/ /// Trait for forming the adjoint operator of `Self`. -pub trait Adjointable : Linear +pub trait Adjointable: Linear where - X : Space, - Yʹ : Space, + X: Space, + Yʹ: Space, { - type AdjointCodomain : Space; - type Adjoint<'a> : Linear where Self : 'a; + /// Codomain of the adjoint operator. + type AdjointCodomain: ClosedSpace; + /// Type of the adjoint operator. + type Adjoint<'a>: Linear + where + Self: 'a; /// Form the adjoint operator of `self`. fn adjoint(&self) -> Self::Adjoint<'_>; } +/// Variant of [`Adjointable`] where the adjoint does not depend on a lifetime parameter. +/// This exists due to restrictions of Rust's type system: if `A :: Adjointable`, and we make +/// further restrictions on the adjoint operator, through, e.g. +/// ``` +/// for<'a> A::Adjoint<'a> : GEMV, +/// ``` +/// Then `'static` lifetime is forced on `X`. Having `A::SimpleAdjoint` not depend on `'a` +/// avoids this, but makes it impossible for the adjoint to be just a light wrapper around the +/// original operator. +pub trait SimplyAdjointable: Linear +where + X: Space, + Yʹ: Space, +{ + /// Codomain of the adjoint operator. + type AdjointCodomain: ClosedSpace; + /// Type of the adjoint operator. + type SimpleAdjoint: Linear; + + /// Form the adjoint operator of `self`. + fn adjoint(&self) -> Self::SimpleAdjoint; +} + /// Trait for forming a preadjoint of an operator. /// /// For an operator $A$ this is an operator $A\_\*$ @@ -112,404 +201,667 @@ /// We do not make additional restrictions on `Self::Preadjoint` (in particular, it /// does not have to be adjointable) to allow `X` to be a subspace yet the preadjoint /// have the full space as the codomain, etc. -pub trait Preadjointable : Linear { - type PreadjointCodomain : Space; - type Preadjoint<'a> : Linear< - Ypre, Codomain=Self::PreadjointCodomain - > where Self : 'a; +pub trait Preadjointable>::Codomain>: + Linear +{ + type PreadjointCodomain: ClosedSpace; + type Preadjoint<'a>: Linear + where + Self: 'a; /// Form the adjoint operator of `self`. fn preadjoint(&self) -> Self::Preadjoint<'_>; } -/// Adjointable operators $A: X → Y$ between reflexive spaces $X$ and $Y$. -pub trait SimplyAdjointable : Adjointable>::Codomain> {} -impl<'a,X : Space, T> SimplyAdjointable for T -where T : Adjointable>::Codomain> {} - /// The identity operator -#[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] -pub struct IdOp (PhantomData); +#[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)] +pub struct IdOp(PhantomData); impl IdOp { - pub fn new() -> IdOp { IdOp(PhantomData) } + pub fn new() -> IdOp { + IdOp(PhantomData) + } } -impl Mapping for IdOp { - type Codomain = X; +impl Mapping for IdOp { + type Codomain = X::Principal; - fn apply>(&self, x : I) -> X { + fn apply>(&self, x: I) -> Self::Codomain { x.own() } } -impl Linear for IdOp -{ } +impl Linear for IdOp {} #[replace_float_literals(F::cast_from(literal))] -impl GEMV for IdOp +impl GEMV for IdOp where - Y : AXPY, - X : Clone + Space + Y: AXPY, + X: Space, { // Computes `y = αAx + βy`, where `A` is `Self`. - fn gemv>(&self, y : &mut Y, α : F, x : I, β : F) { + fn gemv>(&self, y: &mut Y, α: F, x: I, β: F) { y.axpy(α, x, β) } - fn apply_mut>(&self, y : &mut Y, x : I){ + fn apply_mut>(&self, y: &mut Y, x: I) { y.copy_from(x); } } impl BoundedLinear for IdOp where - X : Space + Clone + Norm, - F : Num, - E : NormExponent + X: Space + Clone, + F: Num, + E: NormExponent, { - fn opnorm_bound(&self, _xexp : E, _codexp : E) -> F { F::ONE } -} - -impl Adjointable for IdOp { - type AdjointCodomain=X; - type Adjoint<'a> = IdOp where X : 'a; - - fn adjoint(&self) -> Self::Adjoint<'_> { IdOp::new() } + fn opnorm_bound(&self, _xexp: E, _codexp: E) -> DynResult { + Ok(F::ONE) + } } -impl Preadjointable for IdOp { - type PreadjointCodomain=X; - type Preadjoint<'a> = IdOp where X : 'a; +impl Adjointable for IdOp { + type AdjointCodomain = X::Principal; + type Adjoint<'a> + = IdOp + where + X: 'a; - fn preadjoint(&self) -> Self::Preadjoint<'_> { IdOp::new() } + fn adjoint(&self) -> Self::Adjoint<'_> { + IdOp::new() + } } +impl SimplyAdjointable for IdOp { + type AdjointCodomain = X::Principal; + type SimpleAdjoint = IdOp; -/// The zero operator -#[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] -pub struct ZeroOp<'a, X, XD, Y, F> { - zero : &'a Y, // TODO: don't pass this in `new`; maybe not even store. - dual_or_predual_zero : XD, - _phantoms : PhantomData<(X, Y, F)>, -} - -// TODO: Need to make Zero in Instance. - -impl<'a, F : Num, X : Space, XD, Y : Space + Clone> ZeroOp<'a, X, XD, Y, F> { - pub fn new(zero : &'a Y, dual_or_predual_zero : XD) -> Self { - ZeroOp{ zero, dual_or_predual_zero, _phantoms : PhantomData } + fn adjoint(&self) -> Self::SimpleAdjoint { + IdOp::new() } } -impl<'a, F : Num, X : Space, XD, Y : AXPY + Clone> Mapping for ZeroOp<'a, X, XD, Y, F> { - type Codomain = Y; +impl Preadjointable for IdOp { + type PreadjointCodomain = X::Principal; + type Preadjoint<'a> + = IdOp + where + X: 'a; - fn apply>(&self, _x : I) -> Y { - self.zero.clone() + fn preadjoint(&self) -> Self::Preadjoint<'_> { + IdOp::new() } } -impl<'a, F : Num, X : Space, XD, Y : AXPY + Clone> Linear for ZeroOp<'a, X, XD, Y, F> -{ } +/// The zero operator from a space to itself +#[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)] +pub struct SimpleZeroOp; + +impl Mapping for SimpleZeroOp { + type Codomain = X::PrincipalV; + + fn apply>(&self, x: I) -> X::PrincipalV { + X::similar_origin_inst(x) + } +} + +impl Linear for SimpleZeroOp {} #[replace_float_literals(F::cast_from(literal))] -impl<'a, F, X, XD, Y> GEMV for ZeroOp<'a, X, XD, Y, F> +impl GEMV for SimpleZeroOp where - F : Num, - Y : AXPY + Clone, - X : Space + F: Num, + Y: AXPY, + X: VectorSpace + Instance, { // Computes `y = αAx + βy`, where `A` is `Self`. - fn gemv>(&self, y : &mut Y, _α : F, _x : I, β : F) { + fn gemv>(&self, y: &mut Y, _α: F, _x: I, β: F) { *y *= β; } - fn apply_mut>(&self, y : &mut Y, _x : I){ + fn apply_mut>(&self, y: &mut Y, _x: I) { y.set_zero(); } } -impl<'a, F, X, XD, Y, E1, E2> BoundedLinear for ZeroOp<'a, X, XD, Y, F> +impl BoundedLinear for SimpleZeroOp +where + F: Num, + X: VectorSpace, + E1: NormExponent, + E2: NormExponent, +{ + fn opnorm_bound(&self, _xexp: E1, _codexp: E2) -> DynResult { + Ok(F::ZERO) + } +} + +impl Adjointable for SimpleZeroOp where - X : Space + Norm, - Y : AXPY + Clone + Norm, - F : Num, - E1 : NormExponent, - E2 : NormExponent, + F: Num, + X: VectorSpace + HasDual, + X::DualSpace: ClosedVectorSpace, { - fn opnorm_bound(&self, _xexp : E1, _codexp : E2) -> F { F::ZERO } + type AdjointCodomain = X::DualSpace; + type Adjoint<'b> + = SimpleZeroOp + where + Self: 'b; + // () means not (pre)adjointable. + + fn adjoint(&self) -> Self::Adjoint<'_> { + SimpleZeroOp + } +} + +pub trait OriginGenerator { + type Ref<'b>: OriginGenerator + where + Self: 'b; + + fn origin(&self) -> Y::PrincipalV; + fn as_ref(&self) -> Self::Ref<'_>; } -impl<'a, F : Num, X, XD, Y, Yprime : Space> Adjointable for ZeroOp<'a, X, XD, Y, F> -where - X : Space, - Y : AXPY + Clone + 'static, - XD : AXPY + Clone + 'static, +#[derive(Copy, Clone, Debug)] +pub struct StaticEuclideanOriginGenerator; + +impl, F: Float> OriginGenerator + for StaticEuclideanOriginGenerator { - type AdjointCodomain = XD; - type Adjoint<'b> = ZeroOp<'b, Yprime, (), XD, F> where Self : 'b; - // () means not (pre)adjointable. + type Ref<'b> + = Self + where + Self: 'b; + + #[inline] + fn origin(&self) -> Y::PrincipalV { + return Y::origin(); + } + + #[inline] + fn as_ref(&self) -> Self::Ref<'_> { + *self + } +} + +impl OriginGenerator for Y { + type Ref<'b> + = &'b Y + where + Self: 'b; + + #[inline] + fn origin(&self) -> Y::PrincipalV { + return self.similar_origin(); + } + + #[inline] + fn as_ref(&self) -> Self::Ref<'_> { + self + } +} - fn adjoint(&self) -> Self::Adjoint<'_> { - ZeroOp::new(&self.dual_or_predual_zero, ()) +impl<'b, Y: VectorSpace> OriginGenerator for &'b Y { + type Ref<'c> + = Self + where + Self: 'c; + + #[inline] + fn origin(&self) -> Y::PrincipalV { + return self.similar_origin(); + } + + #[inline] + fn as_ref(&self) -> Self::Ref<'_> { + self + } +} + +/// A zero operator that can be eitherh dualised or predualised (once). +/// This is achieved by storing an oppropriate zero. +pub struct ZeroOp, OY: OriginGenerator, O, F: Float = f64> { + codomain_origin_generator: OY, + other_origin_generator: O, + _phantoms: PhantomData<(X, Y, F)>, +} + +impl ZeroOp +where + OY: OriginGenerator, + X: VectorSpace, + Y: VectorSpace, + F: Float, +{ + pub fn new(y_og: OY) -> Self { + ZeroOp { + codomain_origin_generator: y_og, + other_origin_generator: (), + _phantoms: PhantomData, + } } } -impl<'a, F, X, XD, Y, Ypre> Preadjointable for ZeroOp<'a, X, XD, Y, F> +impl ZeroOp +where + OY: OriginGenerator, + OXprime: OriginGenerator, + X: HasDual, + Y: HasDual, + F: Float, + Xprime: VectorSpace, + Xprime::PrincipalV: AXPY, + Yprime: Space + Instance, +{ + pub fn new_dualisable(y_og: OY, xprime_og: OXprime) -> Self { + ZeroOp { + codomain_origin_generator: y_og, + other_origin_generator: xprime_og, + _phantoms: PhantomData, + } + } +} + +impl Mapping for ZeroOp +where + X: Space, + Y: VectorSpace, + F: Float, + OY: OriginGenerator, +{ + type Codomain = Y::PrincipalV; + + fn apply>(&self, _x: I) -> Y::PrincipalV { + self.codomain_origin_generator.origin() + } +} + +impl Linear for ZeroOp +where + X: Space, + Y: VectorSpace, + F: Float, + OY: OriginGenerator, +{ +} + +#[replace_float_literals(F::cast_from(literal))] +impl GEMV for ZeroOp +where + X: Space, + Y: AXPY, + F: Float, + OY: OriginGenerator, +{ + // Computes `y = αAx + βy`, where `A` is `Self`. + fn gemv>(&self, y: &mut Y, _α: F, _x: I, β: F) { + *y *= β; + } + + fn apply_mut>(&self, y: &mut Y, _x: I) { + y.set_zero(); + } +} + +impl BoundedLinear for ZeroOp where - F : Num, - X : Space, - Y : AXPY + Clone, - Ypre : Space, - XD : AXPY + Clone + 'static, + X: Space + Instance, + Y: VectorSpace, + Y::PrincipalV: Clone, + F: Float, + E1: NormExponent, + E2: NormExponent, + OY: OriginGenerator, +{ + fn opnorm_bound(&self, _xexp: E1, _codexp: E2) -> DynResult { + Ok(F::ZERO) + } +} + +impl<'b, X, Y, OY, OXprime, Xprime, Yprime, F> Adjointable + for ZeroOp +where + X: HasDual, + Y: HasDual, + F: Float, + Xprime: ClosedVectorSpace, + //Xprime::Owned: AXPY, + Yprime: ClosedSpace, + OY: OriginGenerator, + OXprime: OriginGenerator, { - type PreadjointCodomain = XD; - type Preadjoint<'b> = ZeroOp<'b, Ypre, (), XD, F> where Self : 'b; - // () means not (pre)adjointable. + type AdjointCodomain = Xprime; + type Adjoint<'c> + = ZeroOp, (), F> + where + Self: 'c; + // () means not (pre)adjointable. + + fn adjoint(&self) -> Self::Adjoint<'_> { + ZeroOp { + codomain_origin_generator: self.other_origin_generator.as_ref(), + other_origin_generator: (), + _phantoms: PhantomData, + } + } +} - fn preadjoint(&self) -> Self::Preadjoint<'_> { - ZeroOp::new(&self.dual_or_predual_zero, ()) +impl<'b, X, Y, OY, OXprime, Xprime, Yprime, F> SimplyAdjointable + for ZeroOp +where + X: HasDual, + Y: HasDual, + F: Float, + Xprime: ClosedVectorSpace, + //Xprime::Owned: AXPY, + Yprime: ClosedSpace, + OY: OriginGenerator, + OXprime: OriginGenerator + Clone, +{ + type AdjointCodomain = Xprime; + type SimpleAdjoint = ZeroOp; + // () means not (pre)adjointable. + + fn adjoint(&self) -> Self::SimpleAdjoint { + ZeroOp { + codomain_origin_generator: self.other_origin_generator.clone(), + other_origin_generator: (), + _phantoms: PhantomData, + } } } impl Linear for Composition where - X : Space, - T : Linear, - S : Linear -{ } + X: Space, + T: Linear, + S: Linear, +{ +} impl GEMV for Composition where - F : Num, - X : Space, - T : Linear, - S : GEMV, + F: Num, + X: Space, + T: Linear, + S: GEMV, { - fn gemv>(&self, y : &mut Y, α : F, x : I, β : F) { + fn gemv>(&self, y: &mut Y, α: F, x: I, β: F) { self.outer.gemv(y, α, self.inner.apply(x), β) } /// Computes `y = Ax`, where `A` is `Self` - fn apply_mut>(&self, y : &mut Y, x : I){ + fn apply_mut>(&self, y: &mut Y, x: I) { self.outer.apply_mut(y, self.inner.apply(x)) } /// Computes `y += Ax`, where `A` is `Self` - fn apply_add>(&self, y : &mut Y, x : I){ + fn apply_add>(&self, y: &mut Y, x: I) { self.outer.apply_add(y, self.inner.apply(x)) } } impl BoundedLinear for Composition where - F : Num, - X : Space + Norm, - Z : Space + Norm, - Xexp : NormExponent, - Yexp : NormExponent, - Zexp : NormExponent, - T : BoundedLinear, - S : BoundedLinear, + F: Num, + X: Space, + Z: Space, + Xexp: NormExponent, + Yexp: NormExponent, + Zexp: NormExponent, + T: BoundedLinear, + S: BoundedLinear, { - fn opnorm_bound(&self, xexp : Xexp, yexp : Yexp) -> F { + fn opnorm_bound(&self, xexp: Xexp, yexp: Yexp) -> DynResult { let zexp = self.intermediate_norm_exponent; - self.outer.opnorm_bound(zexp, yexp) * self.inner.opnorm_bound(xexp, zexp) + Ok(self.outer.opnorm_bound(zexp, yexp)? * self.inner.opnorm_bound(xexp, zexp)?) } } /// “Row operator” $(S, T)$; $(S, T)(x, y)=Sx + Ty$. +#[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)] pub struct RowOp(pub S, pub T); -use std::ops::Add; - impl Mapping> for RowOp where - A : Space, - B : Space, - S : Mapping, - T : Mapping, - S::Codomain : Add, - >::Output : Space, - + A: Space, + B: Space, + S: Mapping, + T: Mapping, + S::Codomain: Add, + >::Output: ClosedSpace, { type Codomain = >::Output; - fn apply>>(&self, x : I) -> Self::Codomain { - let Pair(a, b) = x.decompose(); - self.0.apply(a) + self.1.apply(b) + fn apply>>(&self, x: I) -> Self::Codomain { + x.eval_decompose(|Pair(a, b)| self.0.apply(a) + self.1.apply(b)) } } impl Linear> for RowOp where - A : Space, - B : Space, - S : Linear, - T : Linear, - S::Codomain : Add, - >::Output : Space, -{ } - + A: Space, + B: Space, + S: Linear, + T: Linear, + S::Codomain: Add, + >::Output: ClosedSpace, +{ +} impl<'b, F, S, T, Y, U, V> GEMV, Y> for RowOp where - U : Space, - V : Space, - S : GEMV, - T : GEMV, - F : Num, - Self : Linear, Codomain=Y> + U: Space, + V: Space, + S: GEMV, + T: GEMV, + F: Num, + Self: Linear, Codomain = Y>, { - fn gemv>>(&self, y : &mut Y, α : F, x : I, β : F) { - let Pair(u, v) = x.decompose(); - self.0.gemv(y, α, u, β); - self.1.gemv(y, α, v, F::ONE); + fn gemv>>(&self, y: &mut Y, α: F, x: I, β: F) { + x.eval_decompose(|Pair(u, v)| { + self.0.gemv(y, α, u, β); + self.1.gemv(y, α, v, F::ONE); + }) } - fn apply_mut>>(&self, y : &mut Y, x : I) { - let Pair(u, v) = x.decompose(); - self.0.apply_mut(y, u); - self.1.apply_add(y, v); + fn apply_mut>>(&self, y: &mut Y, x: I) { + x.eval_decompose(|Pair(u, v)| { + self.0.apply_mut(y, u); + self.1.apply_add(y, v); + }) } /// Computes `y += Ax`, where `A` is `Self` - fn apply_add>>(&self, y : &mut Y, x : I) { - let Pair(u, v) = x.decompose(); - self.0.apply_add(y, u); - self.1.apply_add(y, v); + fn apply_add>>(&self, y: &mut Y, x: I) { + x.eval_decompose(|Pair(u, v)| { + self.0.apply_add(y, u); + self.1.apply_add(y, v); + }) } } /// “Column operator” $(S; T)$; $(S; T)x=(Sx, Tx)$. +#[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)] pub struct ColOp(pub S, pub T); impl Mapping for ColOp where - A : Space, - S : Mapping, - T : Mapping, + A: Space, + S: Mapping, + T: Mapping, { type Codomain = Pair; - fn apply>(&self, a : I) -> Self::Codomain { - Pair(self.0.apply(a.ref_instance()), self.1.apply(a)) + fn apply>(&self, a: I) -> Self::Codomain { + Pair(a.eval_ref(|r| self.0.apply(r)), self.1.apply(a)) } } impl Linear for ColOp where - A : Space, - S : Mapping, - T : Mapping, -{ } + A: Space, + S: Mapping, + T: Mapping, +{ +} impl GEMV> for ColOp where - X : Space, - S : GEMV, - T : GEMV, - F : Num, - Self : Linear> + X: Space, + S: GEMV, + T: GEMV, + F: Num, + Self: Linear>, { - fn gemv>(&self, y : &mut Pair, α : F, x : I, β : F) { - self.0.gemv(&mut y.0, α, x.ref_instance(), β); + fn gemv>(&self, y: &mut Pair, α: F, x: I, β: F) { + x.eval_ref(|r| self.0.gemv(&mut y.0, α, r, β)); self.1.gemv(&mut y.1, α, x, β); } - fn apply_mut>(&self, y : &mut Pair, x : I){ - self.0.apply_mut(&mut y.0, x.ref_instance()); + fn apply_mut>(&self, y: &mut Pair, x: I) { + x.eval_ref(|r| self.0.apply_mut(&mut y.0, r)); self.1.apply_mut(&mut y.1, x); } /// Computes `y += Ax`, where `A` is `Self` - fn apply_add>(&self, y : &mut Pair, x : I){ - self.0.apply_add(&mut y.0, x.ref_instance()); + fn apply_add>(&self, y: &mut Pair, x: I) { + x.eval_ref(|r| self.0.apply_add(&mut y.0, r)); self.1.apply_add(&mut y.1, x); } } - -impl Adjointable, Yʹ> for RowOp +impl Adjointable, Yʹ> for RowOp where - A : Space, - B : Space, - Yʹ : Space, - S : Adjointable, - T : Adjointable, - Self : Linear>, + A: Space, + B: Space, + Yʹ: Space, + S: Adjointable, + T: Adjointable, + Self: Linear>, // for<'a> ColOp, T::Adjoint<'a>> : Linear< // Yʹ, // Codomain=Pair // >, { type AdjointCodomain = Pair; - type Adjoint<'a> = ColOp, T::Adjoint<'a>> where Self : 'a; + type Adjoint<'a> + = ColOp, T::Adjoint<'a>> + where + Self: 'a; fn adjoint(&self) -> Self::Adjoint<'_> { ColOp(self.0.adjoint(), self.1.adjoint()) } } -impl Preadjointable, Yʹ> for RowOp +impl SimplyAdjointable, Yʹ> for RowOp where - A : Space, - B : Space, - Yʹ : Space, - S : Preadjointable, - T : Preadjointable, - Self : Linear>, - for<'a> ColOp, T::Preadjoint<'a>> : Linear< - Yʹ, Codomain=Pair, - >, + A: Space, + B: Space, + Yʹ: Space, + S: SimplyAdjointable, + T: SimplyAdjointable, + Self: Linear>, + // for<'a> ColOp, T::Adjoint<'a>> : Linear< + // Yʹ, + // Codomain=Pair + // >, +{ + type AdjointCodomain = Pair; + type SimpleAdjoint = ColOp; + + fn adjoint(&self) -> Self::SimpleAdjoint { + ColOp(self.0.adjoint(), self.1.adjoint()) + } +} + +impl Preadjointable, Yʹ> for RowOp +where + A: Space, + B: Space, + Yʹ: Space, + S: Preadjointable, + T: Preadjointable, + Self: Linear>, + for<'a> ColOp, T::Preadjoint<'a>>: + Linear>, { type PreadjointCodomain = Pair; - type Preadjoint<'a> = ColOp, T::Preadjoint<'a>> where Self : 'a; + type Preadjoint<'a> + = ColOp, T::Preadjoint<'a>> + where + Self: 'a; fn preadjoint(&self) -> Self::Preadjoint<'_> { ColOp(self.0.preadjoint(), self.1.preadjoint()) } } - -impl Adjointable> for ColOp +impl Adjointable> for ColOp where - A : Space, - Xʹ : Space, - Yʹ : Space, - R : Space + ClosedAdd, - S : Adjointable, - T : Adjointable, - Self : Linear, + A: Space, + Xʹ: Space, + Yʹ: Space, + R: ClosedSpace + ClosedAdd, + S: Adjointable, + T: Adjointable, + Self: Linear, // for<'a> RowOp, T::Adjoint<'a>> : Linear< // Pair, // Codomain=R, // >, { type AdjointCodomain = R; - type Adjoint<'a> = RowOp, T::Adjoint<'a>> where Self : 'a; + type Adjoint<'a> + = RowOp, T::Adjoint<'a>> + where + Self: 'a; fn adjoint(&self) -> Self::Adjoint<'_> { RowOp(self.0.adjoint(), self.1.adjoint()) } } -impl Preadjointable> for ColOp +impl SimplyAdjointable> for ColOp where - A : Space, - Xʹ : Space, - Yʹ : Space, - R : Space + ClosedAdd, - S : Preadjointable, - T : Preadjointable, - Self : Linear, - for<'a> RowOp, T::Preadjoint<'a>> : Linear< - Pair, Codomain = R, - >, + A: Space, + Xʹ: Space, + Yʹ: Space, + R: ClosedSpace + ClosedAdd, + S: SimplyAdjointable, + T: SimplyAdjointable, + Self: Linear, + // for<'a> RowOp, T::Adjoint<'a>> : Linear< + // Pair, + // Codomain=R, + // >, +{ + type AdjointCodomain = R; + type SimpleAdjoint = RowOp; + + fn adjoint(&self) -> Self::SimpleAdjoint { + RowOp(self.0.adjoint(), self.1.adjoint()) + } +} + +impl Preadjointable> for ColOp +where + A: Space, + Xʹ: Space, + Yʹ: Space, + R: ClosedSpace + ClosedAdd, + S: Preadjointable, + T: Preadjointable, + Self: Linear, + for<'a> RowOp, T::Preadjoint<'a>>: Linear, Codomain = R>, { type PreadjointCodomain = R; - type Preadjoint<'a> = RowOp, T::Preadjoint<'a>> where Self : 'a; + type Preadjoint<'a> + = RowOp, T::Preadjoint<'a>> + where + Self: 'a; fn preadjoint(&self) -> Self::Preadjoint<'_> { RowOp(self.0.preadjoint(), self.1.preadjoint()) @@ -517,100 +869,126 @@ } /// Diagonal operator +#[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)] pub struct DiagOp(pub S, pub T); impl Mapping> for DiagOp where - A : Space, - B : Space, - S : Mapping, - T : Mapping, + A: Space, + B: Space, + S: Mapping, + T: Mapping, { type Codomain = Pair; - fn apply>>(&self, x : I) -> Self::Codomain { - let Pair(a, b) = x.decompose(); - Pair(self.0.apply(a), self.1.apply(b)) + fn apply>>(&self, x: I) -> Self::Codomain { + x.eval_decompose(|Pair(a, b)| Pair(self.0.apply(a), self.1.apply(b))) } } impl Linear> for DiagOp where - A : Space, - B : Space, - S : Linear, - T : Linear, -{ } + A: Space, + B: Space, + S: Linear, + T: Linear, +{ +} impl GEMV, Pair> for DiagOp where - A : Space, - B : Space, - U : Space, - V : Space, - S : GEMV, - T : GEMV, - F : Num, - Self : Linear, Codomain=Pair>, + A: Space, + B: Space, + U: Space, + V: Space, + S: GEMV, + T: GEMV, + F: Num, + Self: Linear, Codomain = Pair>, { - fn gemv>>(&self, y : &mut Pair, α : F, x : I, β : F) { - let Pair(u, v) = x.decompose(); - self.0.gemv(&mut y.0, α, u, β); - self.1.gemv(&mut y.1, α, v, β); + fn gemv>>(&self, y: &mut Pair, α: F, x: I, β: F) { + x.eval_decompose(|Pair(u, v)| { + self.0.gemv(&mut y.0, α, u, β); + self.1.gemv(&mut y.1, α, v, β); + }) } - fn apply_mut>>(&self, y : &mut Pair, x : I){ - let Pair(u, v) = x.decompose(); - self.0.apply_mut(&mut y.0, u); - self.1.apply_mut(&mut y.1, v); + fn apply_mut>>(&self, y: &mut Pair, x: I) { + x.eval_decompose(|Pair(u, v)| { + self.0.apply_mut(&mut y.0, u); + self.1.apply_mut(&mut y.1, v); + }) } /// Computes `y += Ax`, where `A` is `Self` - fn apply_add>>(&self, y : &mut Pair, x : I){ - let Pair(u, v) = x.decompose(); - self.0.apply_add(&mut y.0, u); - self.1.apply_add(&mut y.1, v); + fn apply_add>>(&self, y: &mut Pair, x: I) { + x.eval_decompose(|Pair(u, v)| { + self.0.apply_add(&mut y.0, u); + self.1.apply_add(&mut y.1, v); + }) } } -impl Adjointable, Pair> for DiagOp +impl Adjointable, Pair> for DiagOp where - A : Space, - B : Space, + A: Space, + B: Space, Xʹ: Space, - Yʹ : Space, - R : Space, - S : Adjointable, - T : Adjointable, - Self : Linear>, - for<'a> DiagOp, T::Adjoint<'a>> : Linear< - Pair, Codomain=R, - >, + Yʹ: Space, + R: ClosedSpace, + S: Adjointable, + T: Adjointable, + Self: Linear>, + for<'a> DiagOp, T::Adjoint<'a>>: Linear, Codomain = R>, { type AdjointCodomain = R; - type Adjoint<'a> = DiagOp, T::Adjoint<'a>> where Self : 'a; + type Adjoint<'a> + = DiagOp, T::Adjoint<'a>> + where + Self: 'a; fn adjoint(&self) -> Self::Adjoint<'_> { DiagOp(self.0.adjoint(), self.1.adjoint()) } } -impl Preadjointable, Pair> for DiagOp +impl SimplyAdjointable, Pair> for DiagOp where - A : Space, - B : Space, + A: Space, + B: Space, Xʹ: Space, - Yʹ : Space, - R : Space, - S : Preadjointable, - T : Preadjointable, - Self : Linear>, - for<'a> DiagOp, T::Preadjoint<'a>> : Linear< - Pair, Codomain=R, - >, + Yʹ: Space, + R: ClosedSpace, + S: SimplyAdjointable, + T: SimplyAdjointable, + Self: Linear>, + for<'a> DiagOp: Linear, Codomain = R>, +{ + type AdjointCodomain = R; + type SimpleAdjoint = DiagOp; + + fn adjoint(&self) -> Self::SimpleAdjoint { + DiagOp(self.0.adjoint(), self.1.adjoint()) + } +} + +impl Preadjointable, Pair> for DiagOp +where + A: Space, + B: Space, + Xʹ: Space, + Yʹ: Space, + R: ClosedSpace, + S: Preadjointable, + T: Preadjointable, + Self: Linear>, + for<'a> DiagOp, T::Preadjoint<'a>>: Linear, Codomain = R>, { type PreadjointCodomain = R; - type Preadjoint<'a> = DiagOp, T::Preadjoint<'a>> where Self : 'a; + type Preadjoint<'a> + = DiagOp, T::Preadjoint<'a>> + where + Self: 'a; fn preadjoint(&self) -> Self::Preadjoint<'_> { DiagOp(self.0.preadjoint(), self.1.preadjoint()) @@ -620,65 +998,90 @@ /// Block operator pub type BlockOp = ColOp, RowOp>; - macro_rules! pairnorm { ($expj:ty) => { impl - BoundedLinear, PairNorm, ExpR, F> - for RowOp + BoundedLinear, PairNorm, ExpR, F> for RowOp where - F : Float, - A : Space + Norm, - B : Space + Norm, - S : BoundedLinear, - T : BoundedLinear, - S::Codomain : Add, - >::Output : Space, - ExpA : NormExponent, - ExpB : NormExponent, - ExpR : NormExponent, + F: Float, + A: Space, + B: Space, + S: BoundedLinear, + T: BoundedLinear, + S::Codomain: Add, + >::Output: ClosedSpace, + ExpA: NormExponent, + ExpB: NormExponent, + ExpR: NormExponent, { fn opnorm_bound( &self, - PairNorm(expa, expb, _) : PairNorm, - expr : ExpR - ) -> F { + PairNorm(expa, expb, _): PairNorm, + expr: ExpR, + ) -> DynResult { // An application of the triangle inequality bounds the norm by the maximum // of the individual norms. A simple observation shows this to be exact. - let na = self.0.opnorm_bound(expa, expr); - let nb = self.1.opnorm_bound(expb, expr); - na.max(nb) + let na = self.0.opnorm_bound(expa, expr)?; + let nb = self.1.opnorm_bound(expb, expr)?; + Ok(na.max(nb)) } } - - impl - BoundedLinear, F> - for ColOp + + impl BoundedLinear, F> + for ColOp where - F : Float, - A : Space + Norm, - S : BoundedLinear, - T : BoundedLinear, - ExpA : NormExponent, - ExpS : NormExponent, - ExpT : NormExponent, + F: Float, + A: Space, + S: BoundedLinear, + T: BoundedLinear, + ExpA: NormExponent, + ExpS: NormExponent, + ExpT: NormExponent, { fn opnorm_bound( &self, - expa : ExpA, - PairNorm(exps, expt, _) : PairNorm - ) -> F { + expa: ExpA, + PairNorm(exps, expt, _): PairNorm, + ) -> DynResult { // This is based on the rule for RowOp and ‖A^*‖ = ‖A‖, hence, // for A=[S; T], ‖A‖=‖[S^*, T^*]‖ ≤ max{‖S^*‖, ‖T^*‖} = max{‖S‖, ‖T‖} - let ns = self.0.opnorm_bound(expa, exps); - let nt = self.1.opnorm_bound(expa, expt); - ns.max(nt) + let ns = self.0.opnorm_bound(expa, exps)?; + let nt = self.1.opnorm_bound(expa, expt)?; + Ok(ns.max(nt)) } } - } + }; } pairnorm!(L1); pairnorm!(L2); pairnorm!(Linfinity); +/// The simplest linear mapping, scaling by a scalar. +/// +/// TODO: redefined/replace `Weighted` by composition with [`Scaled`]. +pub struct Scaled(pub F); + +impl Mapping for Scaled +where + F: Float, + Domain: Space, + Domain::Principal: Mul, + >::Output: ClosedSpace, +{ + type Codomain = >::Output; + + /// Compute the value of `self` at `x`. + fn apply>(&self, x: I) -> Self::Codomain { + x.own() * self.0 + } +} + +impl Linear for Scaled +where + F: Float, + Domain: Space, + Domain::Principal: Mul, + >::Output: ClosedSpace, +{ +}