# HG changeset patch # User Tuomo Valkonen # Date 1735651843 18000 # Node ID 05089fbc03105a70ca382b5904b41c534041accc # Parent 848ecc05becf7975deec090896b0ccd5b900dc81 Compositions diff -r 848ecc05becf -r 05089fbc0310 src/linops.rs --- a/src/linops.rs Tue Dec 31 09:02:55 2024 -0500 +++ b/src/linops.rs Tue Dec 31 08:30:43 2024 -0500 @@ -6,10 +6,10 @@ use std::marker::PhantomData; use serde::Serialize; use crate::types::*; -pub use crate::mapping::{Mapping, Space}; +pub use crate::mapping::{Mapping, Space, Composition}; use crate::direct_product::Pair; use crate::instance::Instance; -use crate::norms::{NormExponent, PairNorm, L1, L2, Linfinity}; +use crate::norms::{NormExponent, PairNorm, L1, L2, Linfinity, Norm}; /// Trait for linear operators on `X`. pub trait Linear : Mapping @@ -58,7 +58,7 @@ pub trait BoundedLinear : Linear where F : Num, - X : Space, + X : Space + Norm, XExp : NormExponent, CodExp : NormExponent { @@ -152,7 +152,7 @@ impl BoundedLinear for IdOp where - X : Space + Clone, + X : Space + Clone + Norm, F : Num, E : NormExponent { @@ -173,6 +173,53 @@ fn preadjoint(&self) -> Self::Preadjoint<'_> { IdOp::new() } } + +impl Linear for Composition +where + X : Space, + T : Linear, + S : Linear +{ } + +impl GEMV for Composition +where + F : Num, + X : Space, + T : Linear, + S : GEMV, +{ + 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){ + 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){ + 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, +{ + fn opnorm_bound(&self, xexp : Xexp, yexp : Yexp) -> F { + let zexp = self.intermediate_norm_exponent; + self.outer.opnorm_bound(zexp, yexp) * self.inner.opnorm_bound(xexp, zexp) + } +} + /// “Row operator” $(S, T)$; $(S, T)(x, y)=Sx + Ty$. pub struct RowOp(pub S, pub T); @@ -488,8 +535,8 @@ for RowOp where F : Float, - A : Space, - B : Space, + A : Space + Norm, + B : Space + Norm, S : BoundedLinear, T : BoundedLinear, S::Codomain : Add, @@ -516,7 +563,7 @@ for ColOp where F : Float, - A : Space, + A : Space + Norm, S : BoundedLinear, T : BoundedLinear, ExpA : NormExponent, diff -r 848ecc05becf -r 05089fbc0310 src/mapping.rs --- a/src/mapping.rs Tue Dec 31 09:02:55 2024 -0500 +++ b/src/mapping.rs Tue Dec 31 08:30:43 2024 -0500 @@ -4,10 +4,11 @@ use std::marker::PhantomData; use std::borrow::Cow; -use crate::types::Float; +use crate::types::{Num, Float}; use serde::Serialize; use crate::loc::Loc; pub use crate::instance::{Instance, Decomposition, BasicDecomposition, Space}; +use crate::norms::{Norm, NormExponent}; /// A mapping from `Domain` to `Codomain`. /// @@ -17,6 +18,33 @@ /// Compute the value of `self` at `x`. fn apply>(&self, x : I) -> Self::Codomain; + + #[inline] + /// Form the composition `self ∘ other` + fn compose>(self, other : T) + -> Composition + where + Self : Sized + { + Composition{ outer : self, inner : other, intermediate_norm_exponent : () } + } + + + #[inline] + /// Form the composition `self ∘ other`, assigning a norm to the inermediate space + fn compose_with_norm( + self, other : T, norm : E + ) -> Composition + where + Self : Sized, + X : Space, + T : Mapping, + E : NormExponent, + Domain : Norm, + F : Num + { + Composition{ outer : self, inner : other, intermediate_norm_exponent : norm } + } } /// Automatically implemented shorthand for referring to [`Mapping`]s from [`Loc`] to `F`. @@ -236,3 +264,25 @@ impl> + Clone, const N : usize> SliceCodomain for G {} + + +/// The composition S ∘ T. `E` is for storing a `NormExponent` for the intermediate space. +pub struct Composition { + pub outer : S, + pub inner : T, + pub intermediate_norm_exponent : E +} + +impl Mapping for Composition +where + X : Space, + T : Mapping, + S : Mapping +{ + type Codomain = S::Codomain; + + #[inline] + fn apply>(&self, x : I) -> Self::Codomain { + self.outer.apply(self.inner.apply(x)) + } +}