# HG changeset patch # User Tuomo Valkonen # Date 1734809234 18000 # Node ID cebedc4a83312ca8c55a21a02896a3b4c5232676 # Parent 05089fbc03105a70ca382b5904b41c534041accc Try to use HasDual with adjoints. Problem with nalgebra Instances. diff -r 05089fbc0310 -r cebedc4a8331 src/linops.rs --- a/src/linops.rs Tue Dec 31 08:30:43 2024 -0500 +++ b/src/linops.rs Sat Dec 21 14:27:14 2024 -0500 @@ -9,7 +9,7 @@ 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 crate::norms::{NormExponent, PairNorm, L1, L2, Linfinity, Norm, HasDual}; /// Trait for linear operators on `X`. pub trait Linear : Mapping @@ -80,13 +80,16 @@ }*/ /// Trait for forming the adjoint operator of `Self`. -pub trait Adjointable : Linear +pub trait Adjointable : Linear where - X : Space, - Yʹ : Space, + X : HasDual, + Self::Codomain : HasDual, { - type AdjointCodomain : Space; - type Adjoint<'a> : Linear where Self : 'a; + type AdjointCodomain : Instance; + type Adjoint<'a> : Linear< + >::DualSpace, + Codomain=Self::AdjointCodomain, + > where Self : 'a; /// Form the adjoint operator of `self`. fn adjoint(&self) -> Self::Adjoint<'_>; @@ -99,21 +102,20 @@ /// operator. The space `Ypre` is the predual of its codomain, and should be the /// domain of the adjointed operator. `Self::Preadjoint` should be /// [`Adjointable`]`<'a,Ypre,X>`. - -pub trait Preadjointable : Linear { +/// +/// We do not set further restrictions on the spacds, to allow preadjointing when `X` +/// is on the dual space of `Ypre`, but a subset of it. +pub trait Preadjointable : Linear +//where +// Ypre : HasDual, +{ type PreadjointCodomain : Space; - type Preadjoint<'a> : Adjointable< - Ypre, X, AdjointCodomain=Self::Codomain, Codomain=Self::PreadjointCodomain - > where Self : 'a; + 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)] @@ -159,15 +161,15 @@ 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; +impl, F : Float> Adjointable for IdOp { + type AdjointCodomain = X; + type Adjoint<'a> = IdOp where X : 'a; fn adjoint(&self) -> Self::Adjoint<'_> { IdOp::new() } } -impl Preadjointable for IdOp { - type PreadjointCodomain=X; +impl Preadjointable for IdOp { + type PreadjointCodomain = X; type Preadjoint<'a> = IdOp where X : 'a; fn preadjoint(&self) -> Self::Preadjoint<'_> { IdOp::new() } @@ -332,17 +334,23 @@ } -impl Adjointable, Yʹ> for RowOp +impl Adjointable, F> for RowOp where - A : Space, - B : Space, + F : Float, + A : HasDual, + B : HasDual, + S : Adjointable, + T : Adjointable, Yʹ : Space, - S : Adjointable, - T : Adjointable, - Self : Linear>, + S :: Codomain : HasDual, + T :: Codomain : HasDual, + S::Codomain : Add, + >::Output : HasDual, + Self::Codomain : HasDual, + //Self : Linear>, // for<'a> ColOp, T::Adjoint<'a>> : Linear< // Yʹ, - // Codomain=Pair + // //Codomain=Pair // >, { type AdjointCodomain = Pair; @@ -360,12 +368,10 @@ Yʹ : Space, S : Preadjointable, T : Preadjointable, - Self : Linear>, - for<'a> ColOp, T::Preadjoint<'a>> : Adjointable< - Yʹ, Pair, - Codomain=Pair, - AdjointCodomain = Self::Codomain, - >, + S::Codomain : Add, + >::Output : Space, + //Self : Linear, Codomain=Y>, + //for<'a> ColOp, T::Preadjoint<'a>> : Adjointable, { type PreadjointCodomain = Pair; type Preadjoint<'a> = ColOp, T::Preadjoint<'a>> where Self : 'a; @@ -376,21 +382,22 @@ } -impl Adjointable> for ColOp +impl Adjointable for ColOp where - A : Space, - Xʹ : Space, - Yʹ : Space, - R : Space + ClosedAdd, - S : Adjointable, - T : Adjointable, - Self : Linear, + F : Float, + A : HasDual, + S : Adjointable, + T : Adjointable, + T::Codomain : HasDual, + S::Codomain : HasDual, + Aʹ : Space + Instance, + >::AdjointCodomain : Add<>::AdjointCodomain, Output=Aʹ>, // for<'a> RowOp, T::Adjoint<'a>> : Linear< - // Pair, - // Codomain=R, + // Pair<>::DualSpace, >::DualSpace>, + // Codomain=Aʹ // >, { - type AdjointCodomain = R; + type AdjointCodomain = Aʹ; type Adjoint<'a> = RowOp, T::Adjoint<'a>> where Self : 'a; fn adjoint(&self) -> Self::Adjoint<'_> { @@ -398,22 +405,18 @@ } } -impl Preadjointable> for ColOp +impl Preadjointable> for ColOp where A : Space, + Aʹ : Space, Xʹ : Space, Yʹ : Space, - R : Space + ClosedAdd, - S : Preadjointable, - T : Preadjointable, - Self : Linear, - for<'a> RowOp, T::Preadjoint<'a>> : Adjointable< - Pair, A, - Codomain = R, - AdjointCodomain = Self::Codomain, - >, + S : Preadjointable, + T : Preadjointable, + Aʹ : ClosedAdd, + //for<'a> RowOp, T::Preadjoint<'a>> : Adjointable, F>, { - type PreadjointCodomain = R; + type PreadjointCodomain = Aʹ; type Preadjoint<'a> = RowOp, T::Preadjoint<'a>> where Self : 'a; fn preadjoint(&self) -> Self::Preadjoint<'_> { @@ -478,21 +481,17 @@ } } -impl Adjointable, Pair> for DiagOp +impl Adjointable, F> for DiagOp where - 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, - >, + F: Float, + A : HasDual, + B : HasDual, + S : Adjointable, + T : Adjointable, + T::Codomain : HasDual, + S::Codomain : HasDual, { - type AdjointCodomain = R; + type AdjointCodomain = Pair; type Adjoint<'a> = DiagOp, T::Adjoint<'a>> where Self : 'a; fn adjoint(&self) -> Self::Adjoint<'_> { @@ -500,23 +499,16 @@ } } -impl Preadjointable, Pair> for DiagOp +impl Preadjointable, Pair> for DiagOp where A : Space, B : Space, - Xʹ: Space, + Xʹ : Space, Yʹ : Space, - R : Space, S : Preadjointable, T : Preadjointable, - Self : Linear>, - for<'a> DiagOp, T::Preadjoint<'a>> : Adjointable< - Pair, Pair, - Codomain=R, - AdjointCodomain = Self::Codomain, - >, { - type PreadjointCodomain = R; + type PreadjointCodomain = Pair; type Preadjoint<'a> = DiagOp, T::Preadjoint<'a>> where Self : 'a; fn preadjoint(&self) -> Self::Preadjoint<'_> { diff -r 05089fbc0310 -r cebedc4a8331 src/nalgebra_support.rs --- a/src/nalgebra_support.rs Tue Dec 31 08:30:43 2024 -0500 +++ b/src/nalgebra_support.rs Sat Dec 21 14:27:14 2024 -0500 @@ -125,15 +125,18 @@ } } -impl<'own,SV1,SV2,SM,N,M,K,E> Adjointable, Matrix> +impl<'own,SV,SM,N,M,E> Adjointable, E> for Matrix -where SM: Storage, SV1: Storage + Clone, SV2: Storage + Clone, - N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + SimdComplexField, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator { - type AdjointCodomain = OMatrix; +where + SM: Storage, SV: Storage + Clone, + N : Dim, M : Dim, E : Scalar + Zero + One + RealField + Float, + DefaultAllocator : Allocator, + DefaultAllocator : Allocator, + DefaultAllocator : Allocator, + DefaultAllocator : Allocator, + OMatrix : Linear, Codomain = OVector>, +{ + type AdjointCodomain = OVector; type Adjoint<'a> = OMatrix where SM : 'a; #[inline] @@ -242,10 +245,12 @@ impl HasDual for Vector -where M : Dim, - S : Storage + Clone, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { +where + M : Dim, + S : Storage + Clone, + E : Float + Scalar + Zero + One + RealField, + DefaultAllocator : Allocator +{ // TODO: Doesn't work with different storage formats. type DualSpace = Self; }