src/nalgebra_support.rs

branch
dev
changeset 152
dab30b331f49
parent 151
402d717bb5c0
equal deleted inserted replaced
151:402d717bb5c0 152:dab30b331f49
7 force-feeding its own versions of the same basic mathematical methods on `f32` and `f64` as 7 force-feeding its own versions of the same basic mathematical methods on `f32` and `f64` as
8 [`num_traits`] does. 8 [`num_traits`] does.
9 */ 9 */
10 10
11 use crate::euclidean::*; 11 use crate::euclidean::*;
12 use crate::instance::{Instance, Ownable}; 12 use crate::instance::{Decomposition, EitherDecomp, Instance, MyCow, Ownable};
13 use crate::linops::*; 13 use crate::linops::*;
14 use crate::mapping::{BasicDecomposition, Space}; 14 use crate::mapping::Space;
15 use crate::norms::*; 15 use crate::norms::*;
16 use crate::types::Float; 16 use crate::types::Float;
17 use nalgebra::base::allocator::Allocator; 17 use nalgebra::base::allocator::Allocator;
18 use nalgebra::base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; 18 use nalgebra::base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
19 use nalgebra::base::dimension::*; 19 use nalgebra::base::dimension::*;
20 use nalgebra::{ 20 use nalgebra::{
21 ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix, OMatrix, OVector, 21 ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix, MatrixView, OMatrix,
22 RealField, Scalar, SimdComplexField, Storage, StorageMut, UniformNorm, Vector, 22 OVector, RealField, Scalar, SimdComplexField, Storage, StorageMut, UniformNorm, Vector,
23 }; 23 };
24 use num_traits::identities::{One, Zero}; 24 use num_traits::identities::{One, Zero};
25 use std::ops::Mul; 25 use std::ops::Mul;
26 26
27 impl<S, M, N, E> Ownable for Matrix<E, M, N, S> 27 impl<S, M, N, E> Ownable for Matrix<E, M, N, S>
41 41
42 /// Returns an owned instance of a reference. 42 /// Returns an owned instance of a reference.
43 fn clone_owned(&self) -> Self::OwnedVariant { 43 fn clone_owned(&self) -> Self::OwnedVariant {
44 Matrix::clone_owned(self) 44 Matrix::clone_owned(self)
45 } 45 }
46
47 fn owned_cow<'b>(self) -> MyCow<'b, Self::OwnedVariant>
48 where
49 Self: 'b,
50 {
51 EitherDecomp::Owned(self.into_owned())
52 }
53
54 fn ref_owned_cow<'b>(&'b self) -> MyCow<'b, Self::OwnedVariant>
55 where
56 Self: 'b,
57 {
58 EitherDecomp::Owned(self.into_owned())
59 }
46 } 60 }
47 61
48 impl<SM, N, M, E> Space for Matrix<E, N, M, SM> 62 impl<SM, N, M, E> Space for Matrix<E, N, M, SM>
49 where 63 where
50 SM: Storage<E, N, M>, 64 SM: Storage<E, N, M>,
52 M: Dim, 66 M: Dim,
53 E: Scalar + Zero + One, 67 E: Scalar + Zero + One,
54 DefaultAllocator: Allocator<N, M>, 68 DefaultAllocator: Allocator<N, M>,
55 { 69 {
56 type OwnedSpace = OMatrix<E, N, M>; 70 type OwnedSpace = OMatrix<E, N, M>;
57 type Decomp = BasicDecomposition; 71 type Decomp = MatrixDecomposition;
72 }
73
74 #[derive(Copy, Clone, Debug)]
75 pub struct MatrixDecomposition;
76
77 impl<E, M, K, S> Decomposition<Matrix<E, M, K, S>> for MatrixDecomposition
78 where
79 S: Storage<E, M, K>,
80 M: Dim,
81 K: Dim,
82 E: Scalar + Zero + One,
83 DefaultAllocator: Allocator<M, K>,
84 {
85 type Decomposition<'b>
86 = OMatrix<E, M, K>
87 where
88 Matrix<E, M, K, S>: 'b;
89 type Reference<'b>
90 = &'b MatrixView<'b, E, M, K, Dyn, Dyn>
91 where
92 Matrix<E, M, K, S>: 'b;
93
94 #[inline]
95 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b> {
96 r.into_owned()
97 }
98 }
99
100 impl<SM, SV, M, K, E> Instance<Matrix<E, M, K, SM>> for Matrix<E, M, K, SV>
101 where
102 SM: Storage<E, M, K>,
103 SV: Storage<E, M, K>,
104 M: Dim,
105 K: Dim,
106 E: Scalar + Zero + One,
107 DefaultAllocator: Allocator<M, K>,
108 {
109 fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix<E, M, K>) -> R) -> R
110 where
111 Self: 'b,
112 {
113 f(self.into_owned())
114 }
115
116 fn eval_ref_decompose<'b, R>(
117 &'b self,
118 f: impl FnOnce(&'b MatrixView<'b, E, M, K, Dyn, Dyn>) -> R,
119 ) -> R
120 where
121 Self: 'b,
122 Matrix<E, M, K, SM>: 'b,
123 {
124 f(&self.as_view::<M, K, Dyn, Dyn>())
125 }
58 } 126 }
59 127
60 impl<SM, SV, N, M, K, E> Mapping<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM> 128 impl<SM, SV, N, M, K, E> Mapping<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM>
61 where 129 where
62 SM: Storage<E, N, M>, 130 SM: Storage<E, N, M>,

mercurial