diff -r 1f19c6bbf07b -r 3868555d135c src/nalgebra_support.rs --- a/src/nalgebra_support.rs Sun Apr 27 20:29:43 2025 -0500 +++ b/src/nalgebra_support.rs Fri May 15 14:46:30 2026 -0500 @@ -8,107 +8,390 @@ [`num_traits`] does. */ -use nalgebra::{ - Matrix, Storage, StorageMut, OMatrix, Dim, DefaultAllocator, Scalar, - ClosedAddAssign, ClosedMulAssign, SimdComplexField, Vector, OVector, RealField, - LpNorm, UniformNorm -}; -use nalgebra::base::constraint::{ - ShapeConstraint, SameNumberOfRows, SameNumberOfColumns -}; -use nalgebra::base::dimension::*; -use nalgebra::base::allocator::Allocator; -use std::ops::Mul; -use num_traits::identities::{Zero, One}; +use crate::euclidean::*; +use crate::instance::{Decomposition, Instance, MyCow, Ownable, Space}; use crate::linops::*; -use crate::euclidean::*; -use crate::mapping::{Space, BasicDecomposition}; +use crate::norms::*; use crate::types::Float; -use crate::norms::*; -use crate::instance::Instance; +use nalgebra::base::allocator::Allocator; +use nalgebra::base::constraint::{DimEq, SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; +use nalgebra::base::dimension::*; +use nalgebra::{ + ArrayStorage, ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix, + MatrixView, OMatrix, OVector, RawStorage, RealField, SimdComplexField, Storage, StorageMut, + UniformNorm, VecStorage, Vector, ViewStorage, ViewStorageMut, U1, +}; +use num_traits::identities::Zero; +use std::ops::Mul; + +impl Ownable for Matrix +where + S: Storage, + M: Dim, + N: Dim, + E: Float, + DefaultAllocator: Allocator, +{ + type OwnedVariant = OMatrix; + + #[inline] + fn into_owned(self) -> Self::OwnedVariant { + Matrix::into_owned(self) + } + + /// Returns an owned instance of a reference. + fn clone_owned(&self) -> Self::OwnedVariant { + Matrix::clone_owned(self) + } -impl Space for Matrix + fn cow_owned<'b>(self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b, + { + MyCow::Owned(self.into_owned()) + } + + fn ref_cow_owned<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant> + where + Self: 'b, + { + MyCow::Owned(self.clone_owned()) + } +} + +trait StridesOk>::Buffer>: + DimEq + DimEq where - SM: Storage + Clone, - N : Dim, M : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, - DefaultAllocator : Allocator, + S: RawStorage, + E: Float, + N: Dim, + M: Dim, + DefaultAllocator: Allocator, { - type Decomp = BasicDecomposition; +} + +impl StridesOk> for ShapeConstraint +where + M: Dim, + E: Float, + DefaultAllocator: Allocator, +{ } -impl Mapping> for Matrix -where SM: Storage, SV: Storage + Clone, - N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator { - type Codomain = OMatrix; +impl StridesOk, Const, ArrayStorage> + for ShapeConstraint +where + E: Float, +{ +} + +macro_rules! strides_ok { + ($R:ty, $C:ty where $($qual:tt)*) => { + impl<'a, E, N, M, $($qual)*> StridesOk> for ShapeConstraint + where + N: Dim, + M: Dim, + E: Float, + DefaultAllocator: Allocator, + { + } + impl<'a, E, N, M, $($qual)*> StridesOk> for ShapeConstraint + where + N: Dim, + M: Dim, + E: Float, + DefaultAllocator: Allocator, + { + } + }; +} + +strides_ok!(Dyn, Dyn where ); +strides_ok!(Dyn, Const where const C : usize); +strides_ok!(Const, Dyn where const R : usize); +strides_ok!(Const, Const where const R : usize, const C : usize); + +impl Space for Matrix +where + SM: Storage, + N: Dim, + M: Dim, + E: Float, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ + type Principal = OMatrix; + type Decomp = MatrixDecomposition; +} + +#[derive(Copy, Clone, Debug)] +pub struct MatrixDecomposition; + +impl Decomposition> for MatrixDecomposition +where + S: Storage, + M: Dim, + K: Dim, + E: Float, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ + type Decomposition<'b> + = MyCow<'b, OMatrix> + where + Matrix: 'b; + type Reference<'b> + = MatrixView<'b, E, M, K, Dyn, Dyn> + where + Matrix: 'b; #[inline] - fn apply>>( - &self, x : I - ) -> Self::Codomain { - x.either(|owned| self.mul(owned), |refr| self.mul(refr)) + fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b> + where + S: 'b, + { + MyCow::Owned(r.into_owned()) } } +impl Instance, MatrixDecomposition> for Matrix +where + S1: Storage, + S2: Storage, + M: Dim, + K: Dim, + E: Float, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk + StridesOk, +{ + #[inline] + fn eval_ref<'b, R>( + &'b self, + f: impl FnOnce(>>::Reference<'b>) -> R, + ) -> R + where + Self: 'b, + Matrix: 'b, + { + f(self.as_view::()) + } -impl<'a, SM,SV,N,M,K,E> Linear> for Matrix -where SM: Storage, SV: Storage + Clone, - N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator { + #[inline] + fn own(self) -> OMatrix { + self.into_owned() + } + + #[inline] + fn cow<'b>(self) -> MyCow<'b, OMatrix> + where + Self: 'b, + { + self.cow_owned() + } + + #[inline] + fn decompose<'b>(self) -> MyCow<'b, OMatrix> + where + Self: 'b, + { + self.cow_owned() + } +} + +impl<'a, S1, S2, M, K, E> Instance, MatrixDecomposition> + for &'a Matrix +where + S1: Storage, + S2: Storage, + M: Dim, + K: Dim, + E: Float, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk + StridesOk, +{ + fn eval_ref<'b, R>( + &'b self, + f: impl FnOnce(>>::Reference<'b>) -> R, + ) -> R + where + Self: 'b, + Matrix: 'b, + { + f((*self).as_view::()) + } + + #[inline] + fn own(self) -> OMatrix { + self.into_owned() + } + + #[inline] + fn cow<'b>(self) -> MyCow<'b, OMatrix> + where + Self: 'b, + { + self.cow_owned() + } + + #[inline] + fn decompose<'b>(self) -> MyCow<'b, OMatrix> + where + Self: 'b, + { + self.cow_owned() + } } -impl GEMV, Matrix> for Matrix -where SM: Storage, SV1: Storage + Clone, SV2: StorageMut, - N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + Float, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator, - DefaultAllocator : Allocator { +impl<'a, S1, M, SM, K, E> Instance, MatrixDecomposition> + for MyCow<'a, Matrix> +where + S1: Storage, + SM: Storage, + M: Dim, + K: Dim, + E: Float, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk + StridesOk, +{ + #[inline] + fn eval_ref<'b, R>( + &'b self, + f: impl FnOnce(>>::Reference<'b>) -> R, + ) -> R + where + Self: 'b, + Matrix: 'b, + { + f(self.as_view::()) + } + + #[inline] + fn own(self) -> OMatrix { + self.into_owned() + } + + #[inline] + fn cow<'b>(self) -> MyCow<'b, OMatrix> + where + Self: 'b, + { + self.cow_owned() + } #[inline] - fn gemv>>( - &self, y : &mut Matrix, α : E, x : I, β : E + fn decompose<'b>(self) -> MyCow<'b, OMatrix> + where + Self: 'b, + { + self.cow_owned() + } +} + +impl Mapping> for Matrix +where + SM: Storage, + N: Dim, + M: Dim, + K: Dim, + E: Float, + DefaultAllocator: Allocator + Allocator, + ShapeConstraint: StridesOk + StridesOk, +{ + type Codomain = OMatrix; + + #[inline] + fn apply>>(&self, x: I) -> Self::Codomain { + x.eval_ref(|refr| self.mul(refr)) + } +} + +impl<'a, SM, N, M, K, E> Linear> for Matrix +where + SM: Storage, + N: Dim, + M: Dim, + K: Dim, + E: Float + ClosedMulAssign + ClosedAddAssign, + DefaultAllocator: Allocator + Allocator, + ShapeConstraint: StridesOk + StridesOk, +{ +} + +impl GEMV, Matrix> for Matrix +where + SM: Storage, + SV2: StorageMut, + N: Dim, + M: Dim, + K: Dim, + E: Float, + DefaultAllocator: Allocator + Allocator, + ShapeConstraint: StridesOk + StridesOk, +{ + #[inline] + fn gemv>>( + &self, y: &mut Matrix, α: E, x: I, β: E ) { x.eval(|x̃| Matrix::gemm(y, α, self, x̃, β)) } #[inline] - fn apply_mut<'a, I : Instance>>(&self, y : &mut Matrix, x : I) { + fn apply_mut<'a, I: Instance>>(&self, y: &mut Matrix, x: I) { x.eval(|x̃| self.mul_to(x̃, y)) } } -impl AXPY> for Vector -where SM: StorageMut + Clone, SV1: Storage + Clone, - M : Dim, E : Scalar + Zero + One + Float, - DefaultAllocator : Allocator { - type Owned = OVector; +impl VectorSpace for Matrix +where + S: Storage, + M: Dim, + N: Dim, + E: Float, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ + type Field = E; + type PrincipalV = OMatrix; #[inline] - fn axpy>>(&mut self, α : E, x : I, β : E) { - x.eval(|x̃| Matrix::axpy(self, α, x̃, β)) + fn similar_origin(&self) -> Self::PrincipalV { + let (n, m) = self.shape_generic(); + OMatrix::zeros_generic(n, m) + } +} + +// This can only be implemented for the “principal” OMatrix as parameter, as otherwise +// we run into problems of multiple implementations when calling the methods. +impl AXPY> for Matrix +where + S: StorageMut, + M: Dim, + N: Dim, + E: Float, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ + #[inline] + fn axpy>>(&mut self, α: E, x: I, β: E) { + x.eval(|x̃| { + assert_eq!(self.ncols(), x̃.ncols()); + // nalgebra does not implement axpy for matrices, and flattenining + // also seems difficult, so loop over columns. + for (mut y, ỹ) in self.column_iter_mut().zip(x̃.column_iter()) { + Vector::axpy(&mut y, α, &ỹ, β) + } + }) } #[inline] - fn copy_from>>(&mut self, y : I) { - y.eval(|ỹ| Matrix::copy_from(self, ỹ)) + fn copy_from>>(&mut self, y: I) { + y.eval_ref(|ỹ| Matrix::copy_from(self, &ỹ)) } #[inline] fn set_zero(&mut self) { self.iter_mut().for_each(|e| *e = E::ZERO); } - - #[inline] - fn similar_origin(&self) -> Self::Owned { - OVector::zeros_generic(M::from_usize(self.len()), Const) - } } /* Implemented automatically as Euclidean. @@ -125,26 +408,52 @@ } }*/ -impl Projection for Vector -where SM: StorageMut + Clone, - M : Dim, E : Scalar + Zero + One + Float + RealField, - DefaultAllocator : Allocator { +impl Projection for Vector +where + SM: StorageMut, + M: Dim, + E: Float + RealField, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ #[inline] - fn proj_ball_mut(&mut self, ρ : E, _ : Linfinity) { - self.iter_mut().for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) + fn proj_ball(self, ρ: E, exp: Linfinity) -> ::Principal { + let mut owned = self.into_owned(); + owned.proj_ball_mut(ρ, exp); + owned } } -impl<'own,SV1,SV2,SM,N,M,K,E> Adjointable, Matrix> -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; - type Adjoint<'a> = OMatrix where SM : 'a; +impl ProjectionMut for Vector +where + SM: StorageMut, + M: Dim, + E: Float + RealField, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ + #[inline] + fn proj_ball_mut(&mut self, ρ: E, _: Linfinity) { + self.iter_mut() + .for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) + } +} + +impl<'own, SM, N, M, K, E> Adjointable, OMatrix> for Matrix +where + SM: Storage, + N: Dim, + M: Dim, + K: Dim, + E: Float + RealField, + DefaultAllocator: Allocator + Allocator + Allocator, + ShapeConstraint: StridesOk + StridesOk, +{ + type AdjointCodomain = OMatrix; + type Adjoint<'a> + = OMatrix + where + SM: 'a; #[inline] fn adjoint(&self) -> Self::Adjoint<'_> { @@ -152,6 +461,25 @@ } } +impl<'own, SM, N, M, K, E> SimplyAdjointable, OMatrix> + for Matrix +where + SM: Storage, + N: Dim, + M: Dim, + K: Dim, + E: Float + RealField, + DefaultAllocator: Allocator + Allocator + Allocator, + ShapeConstraint: StridesOk + StridesOk, +{ + type AdjointCodomain = OMatrix; + type SimpleAdjoint = OMatrix; + + #[inline] + fn adjoint(&self) -> Self::SimpleAdjoint { + Matrix::adjoint(self) + } +} /// This function is [`nalgebra::EuclideanNorm::metric_distance`] without the `sqrt`. #[inline] fn metric_distance_squared( @@ -160,7 +488,7 @@ m2: &Matrix, ) -> T::SimdRealField where - T: SimdComplexField, + T: SimdComplexField, R1: Dim, C1: Dim, S1: Storage, @@ -175,40 +503,41 @@ }) } -// TODO: should allow different input storages in `Euclidean`. - -impl Euclidean -for Vector -where M : Dim, - S : StorageMut + Clone, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { - - type Output = OVector; +impl Euclidean for Matrix +where + M: Dim, + N: Dim, + S: Storage, + E: Float + RealField, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ + type PrincipalE = OMatrix; #[inline] - fn dot>(&self, other : I) -> E { - Vector::::dot(self, other.ref_instance()) + fn dot>(&self, other: I) -> E { + other.eval_ref(|ref r| Matrix::::dot(self, r)) } #[inline] fn norm2_squared(&self) -> E { - Vector::::norm_squared(self) + Matrix::::norm_squared(self) } #[inline] - fn dist2_squared>(&self, other : I) -> E { - metric_distance_squared(self, other.ref_instance()) + fn dist2_squared>(&self, other: I) -> E { + other.eval_ref(|ref r| metric_distance_squared(self, r)) } } -impl StaticEuclidean -for Vector -where M : DimName, - S : StorageMut + Clone, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { - +impl StaticEuclidean for Vector +where + M: DimName, + S: Storage, + E: Float + RealField, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ #[inline] fn origin() -> OVector { OVector::zeros() @@ -216,13 +545,15 @@ } /// The default norm for `Vector` is [`L2`]. -impl Normed -for Vector -where M : Dim, - S : Storage + Clone, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { - +impl Normed for Matrix +where + M: Dim, + N: Dim, + S: Storage, + E: Float + RealField, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ type NormExp = L2; #[inline] @@ -232,92 +563,106 @@ #[inline] fn is_zero(&self) -> bool { - Vector::::norm_squared(self) == E::ZERO + Matrix::::norm_squared(self) == E::ZERO } } -impl HasDual -for Vector -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; +impl HasDual for Matrix +where + M: Dim, + N: Dim, + S: Storage, + E: Float + RealField, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ + type DualSpace = OMatrix; + + fn dual_origin(&self) -> OMatrix { + let (m, n) = self.shape_generic(); + OMatrix::zeros_generic(m, n) + } } -impl Norm -for Vector -where M : Dim, - S : Storage, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { - +impl Norm for Vector +where + M: Dim, + S: Storage, + E: Float + RealField, +{ #[inline] - fn norm(&self, _ : L1) -> E { + fn norm(&self, _: L1) -> E { nalgebra::Norm::norm(&LpNorm(1), self) } } -impl Dist -for Vector -where M : Dim, - S : Storage + Clone, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { +impl Dist for Vector +where + M: Dim, + S: Storage, + E: Float + RealField, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ #[inline] - fn dist>(&self, other : I, _ : L1) -> E { - nalgebra::Norm::metric_distance(&LpNorm(1), self, other.ref_instance()) + fn dist>(&self, other: I, _: L1) -> E { + other.eval_ref(|ref r| nalgebra::Norm::metric_distance(&LpNorm(1), self, r)) } } -impl Norm -for Vector -where M : Dim, - S : Storage, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { - +impl Norm for Matrix +where + M: Dim, + N: Dim, + S: Storage, + E: Float + RealField, +{ #[inline] - fn norm(&self, _ : L2) -> E { + fn norm(&self, _: L2) -> E { nalgebra::Norm::norm(&LpNorm(2), self) } } -impl Dist -for Vector -where M : Dim, - S : Storage + Clone, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { +impl Dist for Matrix +where + M: Dim, + N: Dim, + S: Storage, + E: Float + RealField, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ #[inline] - fn dist>(&self, other : I, _ : L2) -> E { - nalgebra::Norm::metric_distance(&LpNorm(2), self, other.ref_instance()) + fn dist>(&self, other: I, _: L2) -> E { + other.eval_ref(|ref r| nalgebra::Norm::metric_distance(&LpNorm(2), self, r)) } } -impl Norm -for Vector -where M : Dim, - S : Storage, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { - +impl Norm for Matrix +where + M: Dim, + N: Dim, + S: Storage, + E: Float + RealField, +{ #[inline] - fn norm(&self, _ : Linfinity) -> E { + fn norm(&self, _: Linfinity) -> E { nalgebra::Norm::norm(&UniformNorm, self) } } -impl Dist -for Vector -where M : Dim, - S : Storage + Clone, - E : Float + Scalar + Zero + One + RealField, - DefaultAllocator : Allocator { +impl Dist for Matrix +where + M: Dim, + N: Dim, + S: Storage, + E: Float + RealField, + DefaultAllocator: Allocator, + ShapeConstraint: StridesOk, +{ #[inline] - fn dist>(&self, other : I, _ : Linfinity) -> E { - nalgebra::Norm::metric_distance(&UniformNorm, self, other.ref_instance()) + fn dist>(&self, other: I, _: Linfinity) -> E { + other.eval_ref(|ref r| nalgebra::Norm::metric_distance(&UniformNorm, self, r)) } } @@ -329,15 +674,15 @@ /// from [`nalgebra`] conflicting with them. Only when absolutely necessary to work with /// nalgebra, one can convert to the nalgebra view of the same type using the methods of /// this trait. -pub trait ToNalgebraRealField : Float { +pub trait ToNalgebraRealField: Float { /// The nalgebra type corresponding to this type. Usually same as `Self`. /// /// This type only carries `nalgebra` traits. - type NalgebraType : RealField; + type NalgebraType: RealField; /// The “mixed” type corresponding to this type. Usually same as `Self`. /// /// This type carries both `num_traits` and `nalgebra` traits. - type MixedType : RealField + Float; + type MixedType: RealField + Float; /// Convert to the nalgebra view of `self`. fn to_nalgebra(self) -> Self::NalgebraType; @@ -346,10 +691,10 @@ fn to_nalgebra_mixed(self) -> Self::MixedType; /// Convert from the nalgebra view of `self`. - fn from_nalgebra(t : Self::NalgebraType) -> Self; + fn from_nalgebra(t: Self::NalgebraType) -> Self; /// Convert from the mixed (nalgebra and num_traits) view to `self`. - fn from_nalgebra_mixed(t : Self::MixedType) -> Self; + fn from_nalgebra_mixed(t: Self::MixedType) -> Self; } impl ToNalgebraRealField for f32 { @@ -357,17 +702,24 @@ type MixedType = f32; #[inline] - fn to_nalgebra(self) -> Self::NalgebraType { self } - - #[inline] - fn to_nalgebra_mixed(self) -> Self::MixedType { self } + fn to_nalgebra(self) -> Self::NalgebraType { + self + } #[inline] - fn from_nalgebra(t : Self::NalgebraType) -> Self { t } + fn to_nalgebra_mixed(self) -> Self::MixedType { + self + } #[inline] - fn from_nalgebra_mixed(t : Self::MixedType) -> Self { t } + fn from_nalgebra(t: Self::NalgebraType) -> Self { + t + } + #[inline] + fn from_nalgebra_mixed(t: Self::MixedType) -> Self { + t + } } impl ToNalgebraRealField for f64 { @@ -375,15 +727,22 @@ type MixedType = f64; #[inline] - fn to_nalgebra(self) -> Self::NalgebraType { self } + fn to_nalgebra(self) -> Self::NalgebraType { + self + } #[inline] - fn to_nalgebra_mixed(self) -> Self::MixedType { self } + fn to_nalgebra_mixed(self) -> Self::MixedType { + self + } #[inline] - fn from_nalgebra(t : Self::NalgebraType) -> Self { t } + fn from_nalgebra(t: Self::NalgebraType) -> Self { + t + } #[inline] - fn from_nalgebra_mixed(t : Self::MixedType) -> Self { t } + fn from_nalgebra_mixed(t: Self::MixedType) -> Self { + t + } } -