src/nalgebra_support.rs

branch
dev
changeset 172
73608862ef54
parent 171
fa8df5a14486
child 173
102421d462d1
equal deleted inserted replaced
171:fa8df5a14486 172:73608862ef54
116 E: Scalar + Zero + One + Copy, 116 E: Scalar + Zero + One + Copy,
117 DefaultAllocator: Allocator<M, K>, 117 DefaultAllocator: Allocator<M, K>,
118 ShapeConstraint: StridesOk<E, M, K, S> + StridesOk<E, M, K>, 118 ShapeConstraint: StridesOk<E, M, K, S> + StridesOk<E, M, K>,
119 { 119 {
120 type Decomposition<'b> 120 type Decomposition<'b>
121 = OMatrix<E, M, K> 121 = MyCow<'b, OMatrix<E, M, K>>
122 where 122 where
123 Matrix<E, M, K, S>: 'b; 123 Matrix<E, M, K, S>: 'b;
124 type Reference<'b> 124 type Reference<'b>
125 = MatrixView<'b, E, M, K, Dyn, Dyn> 125 = MatrixView<'b, E, M, K, Dyn, Dyn>
126 where 126 where
129 #[inline] 129 #[inline]
130 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b> 130 fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b>
131 where 131 where
132 S: 'b, 132 S: 'b,
133 { 133 {
134 r.into_owned() 134 MyCow::Owned(r.into_owned())
135 } 135 }
136 } 136 }
137 137
138 impl<S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition> for Matrix<E, M, K, S2> 138 impl<S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition> for Matrix<E, M, K, S2>
139 where 139 where
146 ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K, S2>, 146 ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K, S2>,
147 { 147 {
148 #[inline] 148 #[inline]
149 fn either<'b, R>( 149 fn either<'b, R>(
150 self, 150 self,
151 f: impl FnOnce(OMatrix<E, M, K>) -> R, 151 f: impl FnOnce(MyCow<'b, OMatrix<E, M, K>>) -> R,
152 _g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R, 152 _g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R,
153 ) -> R 153 ) -> R
154 where 154 where
155 Self: 'b, 155 Self: 'b,
156 { 156 {
157 // TODO: should not turn non-owned matrices into owned 157 // TODO: should not turn non-owned matrices into owned
158 f(self.into_owned()) 158 f(MyCow::Owned(self.into_owned()))
159 } 159 }
160 160
161 #[inline] 161 #[inline]
162 fn eval_ref<'b, R>( 162 fn eval_ref<'b, R>(
163 &'b self, 163 &'b self,
182 { 182 {
183 self.cow_owned() 183 self.cow_owned()
184 } 184 }
185 185
186 #[inline] 186 #[inline]
187 fn decompose<'b>(self) -> OMatrix<E, M, K> 187 fn decompose<'b>(self) -> MyCow<'b, OMatrix<E, M, K>>
188 where 188 where
189 Self: 'b, 189 Self: 'b,
190 { 190 {
191 self.into_owned() 191 self.cow_owned()
192 } 192 }
193 } 193 }
194 194
195 impl<'a, S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition> 195 impl<'a, S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition>
196 for &'a Matrix<E, M, K, S2> 196 for &'a Matrix<E, M, K, S2>
204 ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K, S2>, 204 ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K, S2>,
205 { 205 {
206 #[inline] 206 #[inline]
207 fn either<'b, R>( 207 fn either<'b, R>(
208 self, 208 self,
209 _f: impl FnOnce(OMatrix<E, M, K>) -> R, 209 _f: impl FnOnce(MyCow<'b, OMatrix<E, M, K>>) -> R,
210 g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R, 210 g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R,
211 ) -> R 211 ) -> R
212 where 212 where
213 Self: 'b, 213 Self: 'b,
214 { 214 {
238 { 238 {
239 self.cow_owned() 239 self.cow_owned()
240 } 240 }
241 241
242 #[inline] 242 #[inline]
243 fn decompose<'b>(self) -> OMatrix<E, M, K> 243 fn decompose<'b>(self) -> MyCow<'b, OMatrix<E, M, K>>
244 where 244 where
245 Self: 'b, 245 Self: 'b,
246 { 246 {
247 self.cow_owned()
248 }
249 }
250
251 impl<'a, S1, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition>
252 for MyCow<'a, OMatrix<E, M, K>>
253 where
254 S1: Storage<E, M, K>,
255 M: Dim,
256 K: Dim,
257 E: Scalar + Zero + One + Copy,
258 DefaultAllocator: Allocator<M, K>,
259 ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K>,
260 {
261 #[inline]
262 fn either<'b, R>(
263 self,
264 f: impl FnOnce(MyCow<'b, OMatrix<E, M, K>>) -> R,
265 _g: impl FnOnce(MatrixView<'b, E, M, K, Dyn, Dyn>) -> R,
266 ) -> R
267 where
268 Self: 'b,
269 {
270 f(self)
271 }
272
273 #[inline]
274 fn eval_ref<'b, R>(
275 &'b self,
276 f: impl FnOnce(<MatrixDecomposition as Decomposition<Matrix<E, M, K, S1>>>::Reference<'b>) -> R,
277 ) -> R
278 where
279 Self: 'b,
280 Matrix<E, M, K, S1>: 'b,
281 {
282 f(self.as_view::<M, K, Dyn, Dyn>())
283 }
284
285 #[inline]
286 fn own(self) -> OMatrix<E, M, K> {
247 self.into_owned() 287 self.into_owned()
288 }
289
290 #[inline]
291 fn cow<'b>(self) -> MyCow<'b, OMatrix<E, M, K>>
292 where
293 Self: 'b,
294 {
295 self
296 }
297
298 #[inline]
299 fn decompose<'b>(self) -> MyCow<'b, OMatrix<E, M, K>>
300 where
301 Self: 'b,
302 {
303 self
248 } 304 }
249 } 305 }
250 306
251 impl<SM, N, M, K, E> Mapping<OMatrix<E, M, K>> for Matrix<E, N, M, SM> 307 impl<SM, N, M, K, E> Mapping<OMatrix<E, M, K>> for Matrix<E, N, M, SM>
252 where 308 where
260 { 316 {
261 type Codomain = OMatrix<E, N, K>; 317 type Codomain = OMatrix<E, N, K>;
262 318
263 #[inline] 319 #[inline]
264 fn apply<I: Instance<OMatrix<E, M, K>>>(&self, x: I) -> Self::Codomain { 320 fn apply<I: Instance<OMatrix<E, M, K>>>(&self, x: I) -> Self::Codomain {
265 x.either(|owned| self.mul(owned), |refr| self.mul(refr)) 321 x.eval_ref(|refr| self.mul(refr))
266 } 322 }
267 } 323 }
268 324
269 impl<'a, SM, N, M, K, E> Linear<OMatrix<E, M, K>> for Matrix<E, N, M, SM> 325 impl<'a, SM, N, M, K, E> Linear<OMatrix<E, M, K>> for Matrix<E, N, M, SM>
270 where 326 where

mercurial