nalgebra instance hacks dev

Wed, 03 Sep 2025 20:52:21 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 03 Sep 2025 20:52:21 -0500
branch
dev
changeset 172
73608862ef54
parent 171
fa8df5a14486
child 173
102421d462d1

nalgebra instance hacks

src/nalgebra_support.rs file | annotate | diff | comparison | revisions
--- a/src/nalgebra_support.rs	Wed Sep 03 20:19:41 2025 -0500
+++ b/src/nalgebra_support.rs	Wed Sep 03 20:52:21 2025 -0500
@@ -118,7 +118,7 @@
     ShapeConstraint: StridesOk<E, M, K, S> + StridesOk<E, M, K>,
 {
     type Decomposition<'b>
-        = OMatrix<E, M, K>
+        = MyCow<'b, OMatrix<E, M, K>>
     where
         Matrix<E, M, K, S>: 'b;
     type Reference<'b>
@@ -131,7 +131,7 @@
     where
         S: 'b,
     {
-        r.into_owned()
+        MyCow::Owned(r.into_owned())
     }
 }
 
@@ -148,14 +148,14 @@
     #[inline]
     fn either<'b, R>(
         self,
-        f: impl FnOnce(OMatrix<E, M, K>) -> R,
+        f: impl FnOnce(MyCow<'b, OMatrix<E, M, K>>) -> R,
         _g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R,
     ) -> R
     where
         Self: 'b,
     {
         // TODO: should not turn non-owned matrices into owned
-        f(self.into_owned())
+        f(MyCow::Owned(self.into_owned()))
     }
 
     #[inline]
@@ -184,11 +184,11 @@
     }
 
     #[inline]
-    fn decompose<'b>(self) -> OMatrix<E, M, K>
+    fn decompose<'b>(self) -> MyCow<'b, OMatrix<E, M, K>>
     where
         Self: 'b,
     {
-        self.into_owned()
+        self.cow_owned()
     }
 }
 
@@ -206,7 +206,7 @@
     #[inline]
     fn either<'b, R>(
         self,
-        _f: impl FnOnce(OMatrix<E, M, K>) -> R,
+        _f: impl FnOnce(MyCow<'b, OMatrix<E, M, K>>) -> R,
         g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R,
     ) -> R
     where
@@ -240,12 +240,68 @@
     }
 
     #[inline]
-    fn decompose<'b>(self) -> OMatrix<E, M, K>
+    fn decompose<'b>(self) -> MyCow<'b, OMatrix<E, M, K>>
+    where
+        Self: 'b,
+    {
+        self.cow_owned()
+    }
+}
+
+impl<'a, S1, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition>
+    for MyCow<'a, OMatrix<E, M, K>>
+where
+    S1: Storage<E, M, K>,
+    M: Dim,
+    K: Dim,
+    E: Scalar + Zero + One + Copy,
+    DefaultAllocator: Allocator<M, K>,
+    ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K>,
+{
+    #[inline]
+    fn either<'b, R>(
+        self,
+        f: impl FnOnce(MyCow<'b, OMatrix<E, M, K>>) -> R,
+        _g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R,
+    ) -> R
     where
         Self: 'b,
     {
+        f(self)
+    }
+
+    #[inline]
+    fn eval_ref<'b, R>(
+        &'b self,
+        f: impl FnOnce(<MatrixDecomposition as Decomposition<Matrix<E, M, K, S1>>>::Reference<'b>) -> R,
+    ) -> R
+    where
+        Self: 'b,
+        Matrix<E, M, K, S1>: 'b,
+    {
+        f(self.as_view::<M, K, Dyn, Dyn>())
+    }
+
+    #[inline]
+    fn own(self) -> OMatrix<E, M, K> {
         self.into_owned()
     }
+
+    #[inline]
+    fn cow<'b>(self) -> MyCow<'b, OMatrix<E, M, K>>
+    where
+        Self: 'b,
+    {
+        self
+    }
+
+    #[inline]
+    fn decompose<'b>(self) -> MyCow<'b, OMatrix<E, M, K>>
+    where
+        Self: 'b,
+    {
+        self
+    }
 }
 
 impl<SM, N, M, K, E> Mapping<OMatrix<E, M, K>> for Matrix<E, N, M, SM>
@@ -262,7 +318,7 @@
 
     #[inline]
     fn apply<I: Instance<OMatrix<E, M, K>>>(&self, x: I) -> Self::Codomain {
-        x.either(|owned| self.mul(owned), |refr| self.mul(refr))
+        x.eval_ref(|refr| self.mul(refr))
     }
 }
 

mercurial