src/nalgebra_support.rs

branch
dev
changeset 150
c4e394a9c84c
parent 148
26ef556870fd
child 151
402d717bb5c0
equal deleted inserted replaced
149:2f1798c65fd6 150:c4e394a9c84c
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; 12 use crate::instance::{Instance, Ownable};
13 use crate::linops::*; 13 use crate::linops::*;
14 use crate::mapping::{BasicDecomposition, Space}; 14 use crate::mapping::{BasicDecomposition, 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;
22 RealField, Scalar, SimdComplexField, Storage, StorageMut, UniformNorm, Vector, 22 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>
28 where
29 S: Storage<E, M, N>,
30 M: Dim,
31 N: Dim,
32 E: Scalar + Zero + One,
33 DefaultAllocator: Allocator<M, N>,
34 {
35 type OwnedVariant = OMatrix<E, M, N>;
36
37 #[inline]
38 fn into_owned(self) -> Self::OwnedVariant {
39 Matrix::into_owned(self)
40 }
41
42 /// Returns an owned instance of a reference.
43 fn clone_owned(&self) -> Self::OwnedVariant {
44 Matrix::clone_owned(self)
45 }
46 }
47
27 impl<SM, N, M, E> Space for Matrix<E, N, M, SM> 48 impl<SM, N, M, E> Space for Matrix<E, N, M, SM>
28 where 49 where
29 SM: Storage<E, N, M> + Clone, 50 SM: Storage<E, N, M>,
30 N: Dim, 51 N: Dim,
31 M: Dim, 52 M: Dim,
32 E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, 53 E: Scalar + Zero + One,
33 DefaultAllocator: Allocator<N, M>, 54 DefaultAllocator: Allocator<N, M>,
34 { 55 {
56 type OwnedSpace = OMatrix<E, N, M>;
35 type Decomp = BasicDecomposition; 57 type Decomp = BasicDecomposition;
36 } 58 }
37 59
38 impl<SM, SV, N, M, K, E> Mapping<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM> 60 impl<SM, SV, N, M, K, E> Mapping<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM>
39 where 61 where
101 fn apply_mut<'a, I: Instance<Matrix<E, M, K, SV1>>>(&self, y: &mut Matrix<E, N, K, SV2>, x: I) { 123 fn apply_mut<'a, I: Instance<Matrix<E, M, K, SV1>>>(&self, y: &mut Matrix<E, N, K, SV2>, x: I) {
102 x.eval(|x̃| self.mul_to(x̃, y)) 124 x.eval(|x̃| self.mul_to(x̃, y))
103 } 125 }
104 } 126 }
105 127
106 impl<SM, SV1, M, N, E> AXPY<Matrix<E, M, N, SV1>> for Matrix<E, M, N, SM> 128 impl<S, M, N, E> VectorSpace for Matrix<E, M, N, S>
107 where 129 where
108 SM: StorageMut<E, M, N> + Clone, 130 S: Storage<E, M, N>,
109 SV1: Storage<E, M, N> + Clone,
110 M: Dim, 131 M: Dim,
111 N: Dim, 132 N: Dim,
112 E: Scalar + Zero + One + Float, 133 E: Scalar + Zero + One + Float,
113 DefaultAllocator: Allocator<M, N>, 134 DefaultAllocator: Allocator<M, N>,
114 { 135 {
115 type Field = E; 136 type Field = E;
116 type Owned = OMatrix<E, M, N>; 137 type Owned = OMatrix<E, M, N>;
117 138
139 #[inline]
140 fn similar_origin(&self) -> Self::Owned {
141 let (n, m) = self.shape_generic();
142 OMatrix::zeros_generic(n, m)
143 }
144 }
145
146 impl<SM, SV1, M, N, E> AXPY<Matrix<E, M, N, SV1>> for Matrix<E, M, N, SM>
147 where
148 SM: StorageMut<E, M, N>,
149 SV1: Storage<E, M, N>,
150 M: Dim,
151 N: Dim,
152 E: Scalar + Zero + One + Float,
153 DefaultAllocator: Allocator<M, N>,
154 {
118 #[inline] 155 #[inline]
119 fn axpy<I: Instance<Matrix<E, M, N, SV1>>>(&mut self, α: E, x: I, β: E) { 156 fn axpy<I: Instance<Matrix<E, M, N, SV1>>>(&mut self, α: E, x: I, β: E) {
120 x.eval(|x̃| { 157 x.eval(|x̃| {
121 assert_eq!(self.ncols(), x̃.ncols()); 158 assert_eq!(self.ncols(), x̃.ncols());
122 // nalgebra does not implement axpy for matrices, and flattenining 159 // nalgebra does not implement axpy for matrices, and flattenining
134 171
135 #[inline] 172 #[inline]
136 fn set_zero(&mut self) { 173 fn set_zero(&mut self) {
137 self.iter_mut().for_each(|e| *e = E::ZERO); 174 self.iter_mut().for_each(|e| *e = E::ZERO);
138 } 175 }
139
140 #[inline]
141 fn similar_origin(&self) -> Self::Owned {
142 let (n, m) = self.shape_generic();
143 OMatrix::zeros_generic(n, m)
144 }
145 } 176 }
146 177
147 /* Implemented automatically as Euclidean. 178 /* Implemented automatically as Euclidean.
148 impl<SM,M,E> Projection<E, L2> for Vector<E,M,SM> 179 impl<SM,M,E> Projection<E, L2> for Vector<E,M,SM>
149 where SM: StorageMut<E,M> + Clone, 180 where SM: StorageMut<E,M> + Clone,
157 } 188 }
158 } 189 }
159 }*/ 190 }*/
160 191
161 impl<SM, M, E> Projection<E, Linfinity> for Vector<E, M, SM> 192 impl<SM, M, E> Projection<E, Linfinity> for Vector<E, M, SM>
193 where
194 SM: Storage<E, M> + Clone,
195 M: Dim,
196 E: Scalar + Zero + One + Float + RealField,
197 DefaultAllocator: Allocator<M>,
198 {
199 #[inline]
200 fn proj_ball(self, ρ: E, exp: Linfinity) -> <Self as Space>::OwnedSpace {
201 let mut owned = self.into_owned();
202 owned.proj_ball_mut(ρ, exp);
203 owned
204 }
205 }
206
207 impl<SM, M, E> ProjectionMut<E, Linfinity> for Vector<E, M, SM>
162 where 208 where
163 SM: StorageMut<E, M> + Clone, 209 SM: StorageMut<E, M> + Clone,
164 M: Dim, 210 M: Dim,
165 E: Scalar + Zero + One + Float + RealField, 211 E: Scalar + Zero + One + Float + RealField,
166 DefaultAllocator: Allocator<M>, 212 DefaultAllocator: Allocator<M>,

mercurial