src/nalgebra_support.rs

branch
dev
changeset 124
6aa955ad8122
parent 70
672aec2e1acd
child 129
d2994e34a5f5
--- a/src/nalgebra_support.rs	Thu May 01 08:40:33 2025 -0500
+++ b/src/nalgebra_support.rs	Thu May 01 13:06:58 2025 -0500
@@ -8,95 +8,118 @@
 [`num_traits`] does.
 */
 
+use crate::euclidean::*;
+use crate::instance::Instance;
+use crate::linops::*;
+use crate::mapping::{BasicDecomposition, Space};
+use crate::norms::*;
+use crate::types::Float;
+use nalgebra::base::allocator::Allocator;
+use nalgebra::base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
+use nalgebra::base::dimension::*;
 use nalgebra::{
-    Matrix, Storage, StorageMut, OMatrix, Dim, DefaultAllocator, Scalar,
-    ClosedAddAssign, ClosedMulAssign, SimdComplexField, Vector, OVector, RealField,
-    LpNorm, UniformNorm
+    ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix, OMatrix, OVector,
+    RealField, Scalar, SimdComplexField, Storage, StorageMut, UniformNorm, Vector,
 };
-use nalgebra::base::constraint::{
-    ShapeConstraint, SameNumberOfRows, SameNumberOfColumns
-};
-use nalgebra::base::dimension::*;
-use nalgebra::base::allocator::Allocator;
+use num_traits::identities::{One, Zero};
 use std::ops::Mul;
-use num_traits::identities::{Zero, One};
-use crate::linops::*;
-use crate::euclidean::*;
-use crate::mapping::{Space, BasicDecomposition};
-use crate::types::Float;
-use crate::norms::*;
-use crate::instance::Instance;
 
-impl<SM,N,M,E> Space for Matrix<E,N,M,SM>
+impl<SM, N, M, E> Space for Matrix<E, N, M, SM>
 where
-    SM: Storage<E,N,M> + Clone,
-    N : Dim, M : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
-    DefaultAllocator : Allocator<N,M>,
+    SM: Storage<E, N, M> + Clone,
+    N: Dim,
+    M: Dim,
+    E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
+    DefaultAllocator: Allocator<N, M>,
 {
     type Decomp = BasicDecomposition;
 }
 
-impl<SM,SV,N,M,K,E> Mapping<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM>
-where SM: Storage<E,N,M>, SV: Storage<E,M,K> + Clone,
-        N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
-        DefaultAllocator : Allocator<N,K>,
-        DefaultAllocator : Allocator<M,K>,
-        DefaultAllocator : Allocator<N,M>,
-        DefaultAllocator : Allocator<M,N> {
-    type Codomain = OMatrix<E,N,K>;
+impl<SM, SV, N, M, K, E> Mapping<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM>
+where
+    SM: Storage<E, N, M>,
+    SV: Storage<E, M, K> + Clone,
+    N: Dim,
+    M: Dim,
+    K: Dim,
+    E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
+    DefaultAllocator: Allocator<N, K>,
+    DefaultAllocator: Allocator<M, K>,
+    DefaultAllocator: Allocator<N, M>,
+    DefaultAllocator: Allocator<M, N>,
+{
+    type Codomain = OMatrix<E, N, K>;
 
     #[inline]
-    fn apply<I : Instance<Matrix<E,M,K,SV>>>(
-        &self, x : I
-    ) -> Self::Codomain {
+    fn apply<I: Instance<Matrix<E, M, K, SV>>>(&self, x: I) -> Self::Codomain {
         x.either(|owned| self.mul(owned), |refr| self.mul(refr))
     }
 }
 
-
-impl<'a, SM,SV,N,M,K,E> Linear<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM>
-where SM: Storage<E,N,M>, SV: Storage<E,M,K> + Clone,
-        N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
-        DefaultAllocator : Allocator<N,K>,
-        DefaultAllocator : Allocator<M,K>,
-        DefaultAllocator : Allocator<N,M>,
-        DefaultAllocator : Allocator<M,N> {
+impl<'a, SM, SV, N, M, K, E> Linear<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM>
+where
+    SM: Storage<E, N, M>,
+    SV: Storage<E, M, K> + Clone,
+    N: Dim,
+    M: Dim,
+    K: Dim,
+    E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
+    DefaultAllocator: Allocator<N, K>,
+    DefaultAllocator: Allocator<M, K>,
+    DefaultAllocator: Allocator<N, M>,
+    DefaultAllocator: Allocator<M, N>,
+{
 }
 
-impl<SM,SV1,SV2,N,M,K,E> GEMV<E, Matrix<E,M,K,SV1>, Matrix<E,N,K,SV2>> for Matrix<E,N,M,SM>
-where SM: Storage<E,N,M>, SV1: Storage<E,M,K> + Clone, SV2: StorageMut<E,N,K>,
-      N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + Float,
-      DefaultAllocator : Allocator<N,K>,
-      DefaultAllocator : Allocator<M,K>,
-      DefaultAllocator : Allocator<N,M>,
-      DefaultAllocator : Allocator<M,N> {
-
+impl<SM, SV1, SV2, N, M, K, E> GEMV<E, Matrix<E, M, K, SV1>, Matrix<E, N, K, SV2>>
+    for Matrix<E, N, M, SM>
+where
+    SM: Storage<E, N, M>,
+    SV1: Storage<E, M, K> + Clone,
+    SV2: StorageMut<E, N, K>,
+    N: Dim,
+    M: Dim,
+    K: Dim,
+    E: Scalar + Zero + One + Float,
+    DefaultAllocator: Allocator<N, K>,
+    DefaultAllocator: Allocator<M, K>,
+    DefaultAllocator: Allocator<N, M>,
+    DefaultAllocator: Allocator<M, N>,
+{
     #[inline]
-    fn gemv<I : Instance<Matrix<E,M,K,SV1>>>(
-        &self, y : &mut Matrix<E,N,K,SV2>, α : E, x : I, β : E
+    fn gemv<I: Instance<Matrix<E, M, K, SV1>>>(
+        &self,
+        y: &mut Matrix<E, N, K, SV2>,
+        α: E,
+        x: I,
+        β: E,
     ) {
         x.eval(|x̃| Matrix::gemm(y, α, self, x̃, β))
     }
 
     #[inline]
-    fn apply_mut<'a, I : Instance<Matrix<E,M,K,SV1>>>(&self, y : &mut Matrix<E,N,K,SV2>, x : I) {
+    fn apply_mut<'a, I: Instance<Matrix<E, M, K, SV1>>>(&self, y: &mut Matrix<E, N, K, SV2>, x: I) {
         x.eval(|x̃| self.mul_to(x̃, y))
     }
 }
 
-impl<SM,SV1,M,E> AXPY<E, Vector<E,M,SV1>> for Vector<E,M,SM>
-where SM: StorageMut<E,M> + Clone, SV1: Storage<E,M> + Clone,
-      M : Dim, E : Scalar + Zero + One + Float,
-      DefaultAllocator : Allocator<M> {
+impl<SM, SV1, M, E> AXPY<E, Vector<E, M, SV1>> for Vector<E, M, SM>
+where
+    SM: StorageMut<E, M> + Clone,
+    SV1: Storage<E, M> + Clone,
+    M: Dim,
+    E: Scalar + Zero + One + Float,
+    DefaultAllocator: Allocator<M>,
+{
     type Owned = OVector<E, M>;
 
     #[inline]
-    fn axpy<I : Instance<Vector<E,M,SV1>>>(&mut self, α : E, x : I, β : E) {
+    fn axpy<I: Instance<Vector<E, M, SV1>>>(&mut self, α: E, x: I, β: E) {
         x.eval(|x̃| Matrix::axpy(self, α, x̃, β))
     }
 
     #[inline]
-    fn copy_from<I : Instance<Vector<E,M,SV1>>>(&mut self, y : I) {
+    fn copy_from<I: Instance<Vector<E, M, SV1>>>(&mut self, y: I) {
         y.eval(|ỹ| Matrix::copy_from(self, ỹ))
     }
 
@@ -125,26 +148,40 @@
     }
 }*/
 
-impl<SM,M,E> Projection<E, Linfinity> for Vector<E,M,SM>
-where SM: StorageMut<E,M> + Clone,
-      M : Dim, E : Scalar + Zero + One + Float + RealField,
-      DefaultAllocator : Allocator<M> {
+impl<SM, M, E> Projection<E, Linfinity> for Vector<E, M, SM>
+where
+    SM: StorageMut<E, M> + Clone,
+    M: Dim,
+    E: Scalar + Zero + One + Float + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     #[inline]
-    fn proj_ball_mut(&mut self, ρ : E, _ : Linfinity) {
-        self.iter_mut().for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ))
+    fn proj_ball_mut(&mut self, ρ: E, _: Linfinity) {
+        self.iter_mut()
+            .for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ))
     }
 }
 
-impl<'own,SV1,SV2,SM,N,M,K,E> Adjointable<Matrix<E,M,K,SV1>, Matrix<E,N,K,SV2>>
-for Matrix<E,N,M,SM>
-where SM: Storage<E,N,M>, SV1: Storage<E,M,K> + Clone, SV2: Storage<E,N,K> + Clone,
-      N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + SimdComplexField,
-      DefaultAllocator : Allocator<N,K>,
-      DefaultAllocator : Allocator<M,K>,
-      DefaultAllocator : Allocator<N,M>,
-      DefaultAllocator : Allocator<M,N> {
-    type AdjointCodomain = OMatrix<E,M,K>;
-    type Adjoint<'a> = OMatrix<E,M,N> where SM : 'a;
+impl<'own, SV1, SV2, SM, N, M, K, E> Adjointable<Matrix<E, M, K, SV1>, Matrix<E, N, K, SV2>>
+    for Matrix<E, N, M, SM>
+where
+    SM: Storage<E, N, M>,
+    SV1: Storage<E, M, K> + Clone,
+    SV2: Storage<E, N, K> + Clone,
+    N: Dim,
+    M: Dim,
+    K: Dim,
+    E: Scalar + Zero + One + SimdComplexField,
+    DefaultAllocator: Allocator<N, K>,
+    DefaultAllocator: Allocator<M, K>,
+    DefaultAllocator: Allocator<N, M>,
+    DefaultAllocator: Allocator<M, N>,
+{
+    type AdjointCodomain = OMatrix<E, M, K>;
+    type Adjoint<'a>
+        = OMatrix<E, M, N>
+    where
+        SM: 'a;
 
     #[inline]
     fn adjoint(&self) -> Self::Adjoint<'_> {
@@ -160,7 +197,7 @@
     m2: &Matrix<T, R2, C2, S2>,
 ) -> T::SimdRealField
 where
-    T:  SimdComplexField,
+    T: SimdComplexField,
     R1: Dim,
     C1: Dim,
     S1: Storage<T, R1, C1>,
@@ -177,38 +214,38 @@
 
 // TODO: should allow different input storages in `Euclidean`.
 
-impl<E,M,S> Euclidean<E>
-for Vector<E,M,S>
-where M : Dim,
-      S : StorageMut<E,M> + Clone,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
-
+impl<E, M, S> Euclidean<E> for Vector<E, M, S>
+where
+    M: Dim,
+    S: StorageMut<E, M> + Clone,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     type Output = OVector<E, M>;
 
     #[inline]
-    fn dot<I : Instance<Self>>(&self, other : I) -> E {
-        Vector::<E,M,S>::dot(self, other.ref_instance())
+    fn dot<I: Instance<Self>>(&self, other: I) -> E {
+        Vector::<E, M, S>::dot(self, other.ref_instance())
     }
 
     #[inline]
     fn norm2_squared(&self) -> E {
-        Vector::<E,M,S>::norm_squared(self)
+        Vector::<E, M, S>::norm_squared(self)
     }
 
     #[inline]
-    fn dist2_squared<I : Instance<Self>>(&self, other : I) -> E {
+    fn dist2_squared<I: Instance<Self>>(&self, other: I) -> E {
         metric_distance_squared(self, other.ref_instance())
     }
 }
 
-impl<E,M,S> StaticEuclidean<E>
-for Vector<E,M,S>
-where M : DimName,
-      S : StorageMut<E,M> + Clone,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
-
+impl<E, M, S> StaticEuclidean<E> for Vector<E, M, S>
+where
+    M: DimName,
+    S: StorageMut<E, M> + Clone,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     #[inline]
     fn origin() -> OVector<E, M> {
         OVector::zeros()
@@ -216,13 +253,13 @@
 }
 
 /// The default norm for `Vector` is [`L2`].
-impl<E,M,S> Normed<E>
-for Vector<E,M,S>
-where M : Dim,
-      S : Storage<E,M> + Clone,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
-
+impl<E, M, S> Normed<E> for Vector<E, M, S>
+where
+    M: Dim,
+    S: Storage<E, M> + Clone,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     type NormExp = L2;
 
     #[inline]
@@ -232,91 +269,95 @@
 
     #[inline]
     fn is_zero(&self) -> bool {
-        Vector::<E,M,S>::norm_squared(self) == E::ZERO
+        Vector::<E, M, S>::norm_squared(self) == E::ZERO
     }
 }
 
-impl<E,M,S> HasDual<E>
-for Vector<E,M,S>
-where M : Dim,
-      S : Storage<E,M> + Clone,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
+impl<E, M, S> HasDual<E> for Vector<E, M, S>
+where
+    M: Dim,
+    S: Storage<E, M> + Clone,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     // TODO: Doesn't work with different storage formats.
     type DualSpace = Self;
 }
 
-impl<E,M,S> Norm<E, L1>
-for Vector<E,M,S>
-where M : Dim,
-      S : Storage<E,M>,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
-
+impl<E, M, S> Norm<L1, E> for Vector<E, M, S>
+where
+    M: Dim,
+    S: Storage<E, M>,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     #[inline]
-    fn norm(&self, _ : L1) -> E {
+    fn norm(&self, _: L1) -> E {
         nalgebra::Norm::norm(&LpNorm(1), self)
     }
 }
 
-impl<E,M,S> Dist<E, L1>
-for Vector<E,M,S>
-where M : Dim,
-      S : Storage<E,M> + Clone,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
+impl<E, M, S> Dist<E, L1> for Vector<E, M, S>
+where
+    M: Dim,
+    S: Storage<E, M> + Clone,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     #[inline]
-    fn dist<I : Instance<Self>>(&self, other : I, _ : L1) -> E {
+    fn dist<I: Instance<Self>>(&self, other: I, _: L1) -> E {
         nalgebra::Norm::metric_distance(&LpNorm(1), self, other.ref_instance())
     }
 }
 
-impl<E,M,S> Norm<E, L2>
-for Vector<E,M,S>
-where M : Dim,
-      S : Storage<E,M>,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
-
+impl<E, M, S> Norm<L2, E> for Vector<E, M, S>
+where
+    M: Dim,
+    S: Storage<E, M>,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     #[inline]
-    fn norm(&self, _ : L2) -> E {
+    fn norm(&self, _: L2) -> E {
         nalgebra::Norm::norm(&LpNorm(2), self)
     }
 }
 
-impl<E,M,S> Dist<E, L2>
-for Vector<E,M,S>
-where M : Dim,
-      S : Storage<E,M> + Clone,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
+impl<E, M, S> Dist<E, L2> for Vector<E, M, S>
+where
+    M: Dim,
+    S: Storage<E, M> + Clone,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     #[inline]
-    fn dist<I : Instance<Self>>(&self, other : I, _ : L2) -> E {
+    fn dist<I: Instance<Self>>(&self, other: I, _: L2) -> E {
         nalgebra::Norm::metric_distance(&LpNorm(2), self, other.ref_instance())
     }
 }
 
-impl<E,M,S> Norm<E, Linfinity>
-for Vector<E,M,S>
-where M : Dim,
-      S : Storage<E,M>,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
-
+impl<E, M, S> Norm<Linfinity, E> for Vector<E, M, S>
+where
+    M: Dim,
+    S: Storage<E, M>,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     #[inline]
-    fn norm(&self, _ : Linfinity) -> E {
+    fn norm(&self, _: Linfinity) -> E {
         nalgebra::Norm::norm(&UniformNorm, self)
     }
 }
 
-impl<E,M,S> Dist<E, Linfinity>
-for Vector<E,M,S>
-where M : Dim,
-      S : Storage<E,M> + Clone,
-      E : Float + Scalar + Zero + One + RealField,
-      DefaultAllocator : Allocator<M> {
+impl<E, M, S> Dist<E, Linfinity> for Vector<E, M, S>
+where
+    M: Dim,
+    S: Storage<E, M> + Clone,
+    E: Float + Scalar + Zero + One + RealField,
+    DefaultAllocator: Allocator<M>,
+{
     #[inline]
-    fn dist<I : Instance<Self>>(&self, other : I, _ : Linfinity) -> E {
+    fn dist<I: Instance<Self>>(&self, other: I, _: Linfinity) -> E {
         nalgebra::Norm::metric_distance(&UniformNorm, self, other.ref_instance())
     }
 }
@@ -329,15 +370,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 +387,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 +398,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 +423,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
+    }
 }
-

mercurial