# HG changeset patch # User Tuomo Valkonen # Date 1756834673 18000 # Node ID 9c720f822c79c8dd2ad7af89afac47b6b50f694f # Parent 2ac69af6563644dd41a0606904faa31ac1be7edf fubar diff -r 2ac69af65636 -r 9c720f822c79 src/nalgebra_support.rs --- a/src/nalgebra_support.rs Tue Sep 02 08:01:57 2025 -0500 +++ b/src/nalgebra_support.rs Tue Sep 02 12:37:53 2025 -0500 @@ -14,11 +14,12 @@ use crate::norms::*; use crate::types::Float; use nalgebra::base::allocator::Allocator; -use nalgebra::base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; +use nalgebra::base::constraint::{DimEq, SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; use nalgebra::base::dimension::*; use nalgebra::{ ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix, MatrixView, OMatrix, - OVector, RealField, Scalar, SimdComplexField, Storage, StorageMut, UniformNorm, Vector, + OVector, RawStorage, RealField, Scalar, SimdComplexField, Storage, StorageMut, UniformNorm, + Vector, }; use num_traits::identities::{One, Zero}; use std::ops::Mul; @@ -59,13 +60,11 @@ #[derive(Copy, Clone, Debug)] pub struct MatrixDecomposition; -impl Decomposition> for MatrixDecomposition +impl Decomposition> for MatrixDecomposition where - S: Storage, + S: Storage, M: Dim, K: Dim, - RS: Dim, - CS: Dim, E: Scalar + Zero + One, DefaultAllocator: Allocator, { @@ -76,7 +75,7 @@ where Matrix: 'b; type Reference<'b> - = &'b MatrixView<'b, E, M, K, RS, CS> + = MatrixView<'b, E, M, K, Dyn, Dyn> where Matrix: 'b; @@ -89,40 +88,84 @@ } } -impl Instance, MatrixDecomposition> - for Matrix -where - S: Storage, - SV: Storage, - M: Dim, - K: Dim, - E: Scalar + Zero + One, - DefaultAllocator: Allocator, -{ - fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix) -> R) -> R - where - Self: 'b, - { - f(self.into_owned()) - } +macro_rules! impl_instances { + ($rstride:ty, $cstride:ty where $($qual:tt)*) => { + impl<$($qual)* S1, S2, M, K, E> Instance, MatrixDecomposition> for Matrix + where + S1: Storage, + S2: RawStorage, + //ShapeConstraint: DimEq>::RStride> + // + DimEq>::CStride>, + M: Dim, + K: Dim, + E: Scalar + Zero + One, + DefaultAllocator: Allocator, + { + fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix) -> R) -> R + where + Self: 'b, + { + f(self.into_owned()) + } + + fn eval_ref_decompose<'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() + } + } - fn eval_ref_decompose<'b, R>( - &'b self, - f: impl FnOnce(>::Reference<'b>) -> R, - ) -> R - where - Self: 'b, - Matrix: 'b, - { - f(&self.as_view::()) - } + impl<'a, $($qual)* S1, S2, M, K, E> Instance, MatrixDecomposition> + for &'a Matrix + where + S1: Storage, + S2: RawStorage, + M: Dim, + K: Dim, + E: Scalar + Zero + One, + DefaultAllocator: Allocator, + { + fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix) -> R) -> R + where + Self: 'b, + { + f(self.into_owned()) + } - #[inline] - fn own(self) -> OMatrix { - self.into_owned() - } + fn eval_ref_decompose<'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() + } + } + }; } +impl_instances!(Dyn, Dyn where ); +impl_instances!(Const, Dyn where const RS : usize,); +impl_instances!(Dyn, Const where const CS : usize,); +impl_instances!(Const, Const where const RS : usize, const CS : usize,); + impl Mapping> for Matrix where SM: Storage, @@ -347,7 +390,7 @@ #[inline] fn dot>(&self, other: I) -> E { - other.eval_ref_decompose(|r| Vector::::dot(self, r)) + other.eval_ref_decompose(|ref r| Vector::::dot(self, r)) } #[inline] @@ -357,7 +400,7 @@ #[inline] fn dist2_squared>(&self, other: I) -> E { - other.eval_ref_decompose(|r| metric_distance_squared(self, r)) + other.eval_ref_decompose(|ref r| metric_distance_squared(self, r)) } } @@ -431,7 +474,7 @@ { #[inline] fn dist>(&self, other: I, _: L1) -> E { - other.eval_ref_decompose(|r| nalgebra::Norm::metric_distance(&LpNorm(1), self, r)) + other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&LpNorm(1), self, r)) } } @@ -457,7 +500,7 @@ { #[inline] fn dist>(&self, other: I, _: L2) -> E { - other.eval_ref_decompose(|r| nalgebra::Norm::metric_distance(&LpNorm(2), self, r)) + other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&LpNorm(2), self, r)) } } @@ -483,7 +526,7 @@ { #[inline] fn dist>(&self, other: I, _: Linfinity) -> E { - other.eval_ref_decompose(|r| nalgebra::Norm::metric_distance(&UniformNorm, self, r)) + other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&UniformNorm, self, r)) } }