1use crate::internal_prelude::*;
2use crate::{Scale, assert, col, diag, get_global_parallelism, mat, perm, row};
3use core::ops::{
4 Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign,
5};
6extern crate alloc;
7macro_rules! impl_binop {
8 (
9 { $(impl <$($ty_param:ident $(: $bound:tt)?),* $(,)?> $trait:ident <$rhs:ty > for
10 $lhs:ty { type Output = $out:ty; fn $name:ident ($self:ident, $rhs_:ident : _
11 $(,)?) $block:block })* }
12 ) => {
13 $(impl <$($ty_param $(: $bound)?,)*> $trait <&$rhs > for &$lhs { type Output =
14 $out; #[track_caller] fn $name ($self, $rhs_ : &$rhs) -> Self::Output $block }
15 impl <$($ty_param $(: $bound)?,)*> $trait <$rhs > for $lhs { type Output = $out;
16 #[track_caller] fn $name ($self, $rhs_ : $rhs) -> Self::Output { $trait ::$name
17 (&$self, &$rhs_) } } impl <$($ty_param $(: $bound)?,)*> $trait <$rhs > for &$lhs
18 { type Output = $out; #[track_caller] fn $name ($self, $rhs_ : $rhs) ->
19 Self::Output { $trait ::$name ($self, &$rhs_) } } impl <$($ty_param $(:
20 $bound)?,)*> $trait <&$rhs > for $lhs { type Output = $out; #[track_caller] fn
21 $name ($self, $rhs_ : &$rhs) -> Self::Output { $trait ::$name (&$self, $rhs_) }
22 })*
23 };
24}
25macro_rules! impl_op {
26 (
27 { $(impl <$($ty_param:ident $(: $bound:tt)?),* $(,)?> $trait:ident for $lhs:ty {
28 type Output = $out:ty; fn $name:ident ($self:ident $(,)?) $block:block })* }
29 ) => {
30 $(impl <$($ty_param $(: $bound)?,)*> $trait for &$lhs { type Output = $out;
31 #[track_caller] fn $name ($self) -> Self::Output $block } impl <$($ty_param $(:
32 $bound)?,)*> $trait for $lhs { type Output = $out; #[track_caller] fn $name
33 ($self) -> Self::Output { $trait ::$name (&$self) } })*
34 };
35}
36macro_rules! impl_op_assign {
37 (
38 { $(impl <$($ty_param:ident $(: $bound:tt)?),* $(,)?> $trait:ident <$rhs:ty > for
39 $lhs:ty { fn $name:ident (& mut $self:ident, $rhs_:ident : _ $(,)?) $block:block
40 })* }
41 ) => {
42 $(impl <$($ty_param $(: $bound)?,)*> $trait <&$rhs > for $lhs { #[track_caller]
43 fn $name (& mut $self, $rhs_ : &$rhs) $block } impl <$($ty_param $(: $bound)?,)*>
44 $trait <$rhs > for $lhs { #[track_caller] fn $name (& mut $self, $rhs_ : $rhs) {
45 $trait ::$name ($self, &$rhs_) } })*
46 };
47}
48impl<
49 LT: PartialEq<RT>,
50 LRows: Shape,
51 LCols: Shape,
52 LRStride: Stride,
53 LCStride: Stride,
54 RT,
55 RRows: Shape,
56 RCols: Shape,
57 RRStride: Stride,
58 RCStride: Stride,
59 L: for<'a> Reborrow<
60 'a,
61 Target = mat::Ref<'a, LT, LRows, LCols, LRStride, LCStride>,
62 >,
63 R: for<'a> Reborrow<
64 'a,
65 Target = mat::Ref<'a, RT, RRows, RCols, RRStride, RCStride>,
66 >,
67> PartialEq<mat::generic::Mat<R>> for mat::generic::Mat<L>
68{
69 fn eq(&self, other: &mat::generic::Mat<R>) -> bool {
70 fn imp<LT: PartialEq<RT>, RT>(
71 l: MatRef<'_, LT>,
72 r: MatRef<'_, RT>,
73 ) -> bool {
74 if l.nrows() != r.nrows() {
75 return false;
76 }
77 with_dim!(M, l.nrows());
78 with_dim!(N, l.ncols());
79 let l = l.as_shape(M, N);
80 let r = r.as_shape(M, N);
81 for j in N.indices() {
82 for i in M.indices() {
83 if l[(i, j)] != r[(i, j)] {
84 return false;
85 }
86 }
87 }
88 true
89 }
90 imp(
91 self.rb().as_dyn().as_dyn_stride(),
92 other.rb().as_dyn().as_dyn_stride(),
93 )
94 }
95}
96impl<
97 LT: PartialEq<RT>,
98 LRows: Shape,
99 LRStride: Stride,
100 RT,
101 RRows: Shape,
102 RRStride: Stride,
103 L: for<'a> Reborrow<'a, Target = col::Ref<'a, LT, LRows, LRStride>>,
104 R: for<'a> Reborrow<'a, Target = col::Ref<'a, RT, RRows, RRStride>>,
105> PartialEq<col::generic::Col<R>> for col::generic::Col<L>
106{
107 fn eq(&self, other: &col::generic::Col<R>) -> bool {
108 fn imp<LT: PartialEq<RT>, RT>(
109 l: ColRef<'_, LT>,
110 r: ColRef<'_, RT>,
111 ) -> bool {
112 if l.nrows() != r.nrows() {
113 return false;
114 }
115 with_dim!(N, l.nrows());
116 let l = l.as_row_shape(N);
117 let r = r.as_row_shape(N);
118 for i in N.indices() {
119 if l[i] != r[i] {
120 return false;
121 }
122 }
123 true
124 }
125 imp(
126 self.rb().as_dyn_rows().as_dyn_stride(),
127 other.rb().as_dyn_rows().as_dyn_stride(),
128 )
129 }
130}
131impl<
132 LT: PartialEq<RT>,
133 LCols: Shape,
134 LCStride: Stride,
135 RT,
136 RCols: Shape,
137 RCStride: Stride,
138 L: for<'a> Reborrow<'a, Target = row::Ref<'a, LT, LCols, LCStride>>,
139 R: for<'a> Reborrow<'a, Target = row::Ref<'a, RT, RCols, RCStride>>,
140> PartialEq<row::generic::Row<R>> for row::generic::Row<L>
141{
142 fn eq(&self, other: &row::generic::Row<R>) -> bool {
143 self.rb().transpose() == other.rb().transpose()
144 }
145}
146impl<
147 LT: PartialEq<RT>,
148 LDim: Shape,
149 LStride: Stride,
150 RT,
151 RDim: Shape,
152 RStride: Stride,
153 L: for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, LDim, LStride>>,
154 R: for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, RDim, RStride>>,
155> PartialEq<diag::generic::Diag<R>> for diag::generic::Diag<L>
156{
157 fn eq(&self, other: &diag::generic::Diag<R>) -> bool {
158 self.rb().column_vector() == other.rb().column_vector()
159 }
160}
161impl<
162 LI: Index,
163 LN: Shape,
164 RI: Index,
165 RN: Shape,
166 L: for<'a> Reborrow<'a, Target = perm::Ref<'a, LI, LN>>,
167 R: for<'a> Reborrow<'a, Target = perm::Ref<'a, RI, RN>>,
168> PartialEq<perm::generic::Perm<R>> for perm::generic::Perm<L>
169where
170 LN::Idx<LI>: PartialEq<RN::Idx<RI>>,
171{
172 #[inline]
173 fn eq(&self, other: &perm::generic::Perm<R>) -> bool {
174 self.rb().arrays().0 == other.rb().arrays().0
175 }
176}
177impl_op_assign!({
178 impl<
179 T: ComplexField,
180 Rows: Shape,
181 Cols: Shape,
182 LRStride: Stride,
183 LCStride: Stride,
184 RT: (Conjugate<Canonical = T>),
185 RRStride: Stride,
186 RCStride: Stride,
187 L: (for<'a> ReborrowMut<
188 'a,
189 Target = mat::Mut<'a, T, Rows, Cols, LRStride, LCStride>,
190 >),
191 R: (for<'a> Reborrow<
192 'a,
193 Target = mat::Ref<'a, RT, Rows, Cols, RRStride, RCStride>,
194 >),
195 > SubAssign<mat::generic::Mat<R>> for mat::generic::Mat<L>
196 {
197 fn sub_assign(&mut self, rhs: _) {
198 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
199 lhs: MatMut<'_, T>,
200 rhs: MatRef<'_, RT>,
201 ) {
202 zip!(lhs, rhs).for_each(sub_assign_fn::<T, RT>())
203 }
204 let lhs = self.rb_mut();
205 imp(
206 lhs.as_dyn_mut().as_dyn_stride_mut(),
207 rhs.rb().as_dyn().as_dyn_stride(),
208 );
209 }
210 }
211 impl<
212 T: ComplexField,
213 Rows: Shape,
214 LRStride: Stride,
215 RT: (Conjugate<Canonical = T>),
216 RRStride: Stride,
217 L: (for<'a> ReborrowMut<'a, Target = col::Mut<'a, T, Rows, LRStride>>),
218 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Rows, RRStride>>),
219 > SubAssign<col::generic::Col<R>> for col::generic::Col<L>
220 {
221 fn sub_assign(&mut self, rhs: _) {
222 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
223 lhs: ColMut<'_, T>,
224 rhs: ColRef<'_, RT>,
225 ) {
226 zip!(lhs, rhs).for_each(sub_assign_fn::<T, RT>())
227 }
228 let lhs = self.rb_mut();
229 imp(
230 lhs.as_dyn_rows_mut().as_dyn_stride_mut(),
231 rhs.rb().as_dyn_rows().as_dyn_stride(),
232 );
233 }
234 }
235 impl<
236 T: ComplexField,
237 Cols: Shape,
238 LCStride: Stride,
239 RT: (Conjugate<Canonical = T>),
240 RCStride: Stride,
241 L: (for<'a> ReborrowMut<'a, Target = row::Mut<'a, T, Cols, LCStride>>),
242 R: (for<'a> Reborrow<'a, Target = row::Ref<'a, RT, Cols, RCStride>>),
243 > SubAssign<row::generic::Row<R>> for row::generic::Row<L>
244 {
245 fn sub_assign(&mut self, rhs: _) {
246 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
247 lhs: RowMut<'_, T>,
248 rhs: RowRef<'_, RT>,
249 ) {
250 SubAssign::sub_assign(
251 &mut lhs.transpose_mut(),
252 &rhs.transpose(),
253 )
254 }
255 let lhs = self.rb_mut();
256 imp(
257 lhs.as_dyn_cols_mut().as_dyn_stride_mut(),
258 rhs.rb().as_dyn_cols().as_dyn_stride(),
259 );
260 }
261 }
262 impl<
263 T: ComplexField,
264 Dim: Shape,
265 LStride: Stride,
266 RT: (Conjugate<Canonical = T>),
267 RStride: Stride,
268 L: (for<'a> ReborrowMut<'a, Target = diag::Mut<'a, T, Dim, LStride>>),
269 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
270 > SubAssign<diag::generic::Diag<R>> for diag::generic::Diag<L>
271 {
272 fn sub_assign(&mut self, rhs: _) {
273 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
274 lhs: ColMut<'_, T>,
275 rhs: ColRef<'_, RT>,
276 ) {
277 SubAssign::sub_assign(&mut { lhs }, &rhs)
278 }
279 let lhs = self.rb_mut();
280 imp(
281 lhs.column_vector_mut()
282 .as_dyn_rows_mut()
283 .as_dyn_stride_mut(),
284 rhs.rb().column_vector().as_dyn_rows().as_dyn_stride(),
285 );
286 }
287 }
288});
289impl_op_assign!({
290 impl<
291 T: ComplexField,
292 Rows: Shape,
293 Cols: Shape,
294 LRStride: Stride,
295 LCStride: Stride,
296 RT: (Conjugate<Canonical = T>),
297 RRStride: Stride,
298 RCStride: Stride,
299 L: (for<'a> ReborrowMut<
300 'a,
301 Target = mat::Mut<'a, T, Rows, Cols, LRStride, LCStride>,
302 >),
303 R: (for<'a> Reborrow<
304 'a,
305 Target = mat::Ref<'a, RT, Rows, Cols, RRStride, RCStride>,
306 >),
307 > AddAssign<mat::generic::Mat<R>> for mat::generic::Mat<L>
308 {
309 fn add_assign(&mut self, rhs: _) {
310 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
311 lhs: MatMut<'_, T>,
312 rhs: MatRef<'_, RT>,
313 ) {
314 zip!(lhs, rhs).for_each(add_assign_fn::<T, RT>())
315 }
316 let lhs = self.rb_mut();
317 imp(
318 lhs.as_dyn_mut().as_dyn_stride_mut(),
319 rhs.rb().as_dyn().as_dyn_stride(),
320 );
321 }
322 }
323 impl<
324 T: ComplexField,
325 Rows: Shape,
326 LRStride: Stride,
327 RT: (Conjugate<Canonical = T>),
328 RRStride: Stride,
329 L: (for<'a> ReborrowMut<'a, Target = col::Mut<'a, T, Rows, LRStride>>),
330 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Rows, RRStride>>),
331 > AddAssign<col::generic::Col<R>> for col::generic::Col<L>
332 {
333 fn add_assign(&mut self, rhs: _) {
334 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
335 lhs: ColMut<'_, T>,
336 rhs: ColRef<'_, RT>,
337 ) {
338 zip!(lhs, rhs).for_each(add_assign_fn::<T, RT>())
339 }
340 let lhs = self.rb_mut();
341 imp(
342 lhs.as_dyn_rows_mut().as_dyn_stride_mut(),
343 rhs.rb().as_dyn_rows().as_dyn_stride(),
344 );
345 }
346 }
347 impl<
348 T: ComplexField,
349 Cols: Shape,
350 LCStride: Stride,
351 RT: (Conjugate<Canonical = T>),
352 RCStride: Stride,
353 L: (for<'a> ReborrowMut<'a, Target = row::Mut<'a, T, Cols, LCStride>>),
354 R: (for<'a> Reborrow<'a, Target = row::Ref<'a, RT, Cols, RCStride>>),
355 > AddAssign<row::generic::Row<R>> for row::generic::Row<L>
356 {
357 fn add_assign(&mut self, rhs: _) {
358 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
359 lhs: RowMut<'_, T>,
360 rhs: RowRef<'_, RT>,
361 ) {
362 AddAssign::add_assign(
363 &mut lhs.transpose_mut(),
364 &rhs.transpose(),
365 )
366 }
367 let lhs = self.rb_mut();
368 imp(
369 lhs.as_dyn_cols_mut().as_dyn_stride_mut(),
370 rhs.rb().as_dyn_cols().as_dyn_stride(),
371 );
372 }
373 }
374 impl<
375 T: ComplexField,
376 Dim: Shape,
377 LStride: Stride,
378 RT: (Conjugate<Canonical = T>),
379 RStride: Stride,
380 L: (for<'a> ReborrowMut<'a, Target = diag::Mut<'a, T, Dim, LStride>>),
381 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
382 > AddAssign<diag::generic::Diag<R>> for diag::generic::Diag<L>
383 {
384 fn add_assign(&mut self, rhs: _) {
385 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
386 lhs: ColMut<'_, T>,
387 rhs: ColRef<'_, RT>,
388 ) {
389 AddAssign::add_assign(&mut { lhs }, &rhs)
390 }
391 let lhs = self.rb_mut();
392 imp(
393 lhs.column_vector_mut()
394 .as_dyn_rows_mut()
395 .as_dyn_stride_mut(),
396 rhs.rb().column_vector().as_dyn_rows().as_dyn_stride(),
397 );
398 }
399 }
400});
401impl_binop!({
402 impl<
403 T: ComplexField,
404 Rows: Shape,
405 Cols: Shape,
406 LT: (Conjugate<Canonical = T>),
407 LRStride: Stride,
408 LCStride: Stride,
409 RT: (Conjugate<Canonical = T>),
410 RRStride: Stride,
411 RCStride: Stride,
412 L: (for<'a> Reborrow<
413 'a,
414 Target = mat::Ref<'a, LT, Rows, Cols, LRStride, LCStride>,
415 >),
416 R: (for<'a> Reborrow<
417 'a,
418 Target = mat::Ref<'a, RT, Rows, Cols, RRStride, RCStride>,
419 >),
420 > Add<mat::generic::Mat<R>> for mat::generic::Mat<L>
421 {
422 type Output = Mat<T, Rows, Cols>;
423
424 fn add(self, rhs: _) {
425 #[track_caller]
426 fn imp<
427 T: ComplexField,
428 LT: Conjugate<Canonical = T>,
429 RT: Conjugate<Canonical = T>,
430 >(
431 lhs: MatRef<'_, LT>,
432 rhs: MatRef<'_, RT>,
433 ) -> Mat<T> {
434 assert!(all(
435 lhs.nrows() == rhs.nrows(),
436 lhs.ncols() == rhs.ncols()
437 ));
438 zip!(lhs, rhs).map(add_fn::<LT, RT>())
439 }
440 let lhs = self.rb();
441 imp(
442 lhs.as_dyn().as_dyn_stride(),
443 rhs.rb().as_dyn().as_dyn_stride(),
444 )
445 .into_shape(lhs.nrows(), lhs.ncols())
446 }
447 }
448 impl<
449 T: ComplexField,
450 Rows: Shape,
451 LT: (Conjugate<Canonical = T>),
452 LRStride: Stride,
453 RT: (Conjugate<Canonical = T>),
454 RRStride: Stride,
455 L: (for<'a> Reborrow<'a, Target = col::Ref<'a, LT, Rows, LRStride>>),
456 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Rows, RRStride>>),
457 > Add<col::generic::Col<R>> for col::generic::Col<L>
458 {
459 type Output = Col<T, Rows>;
460
461 fn add(self, rhs: _) {
462 #[track_caller]
463 fn imp<
464 T: ComplexField,
465 LT: Conjugate<Canonical = T>,
466 RT: Conjugate<Canonical = T>,
467 >(
468 lhs: ColRef<'_, LT>,
469 rhs: ColRef<'_, RT>,
470 ) -> Col<T> {
471 assert!(all(
472 lhs.nrows() == rhs.nrows(),
473 lhs.ncols() == rhs.ncols()
474 ));
475 zip!(lhs, rhs).map(add_fn::<LT, RT>())
476 }
477 let lhs = self.rb();
478 imp(
479 lhs.as_dyn_rows().as_dyn_stride(),
480 rhs.rb().as_dyn_rows().as_dyn_stride(),
481 )
482 .into_row_shape(lhs.nrows())
483 }
484 }
485 impl<
486 T: ComplexField,
487 Cols: Shape,
488 LT: (Conjugate<Canonical = T>),
489 LCStride: Stride,
490 RT: (Conjugate<Canonical = T>),
491 RCStride: Stride,
492 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Cols, LCStride>>),
493 R: (for<'a> Reborrow<'a, Target = row::Ref<'a, RT, Cols, RCStride>>),
494 > Add<row::generic::Row<R>> for row::generic::Row<L>
495 {
496 type Output = Row<T, Cols>;
497
498 fn add(self, rhs: _) {
499 #[track_caller]
500 fn imp<
501 T: ComplexField,
502 LT: Conjugate<Canonical = T>,
503 RT: Conjugate<Canonical = T>,
504 >(
505 lhs: RowRef<'_, LT>,
506 rhs: RowRef<'_, RT>,
507 ) -> Row<T> {
508 assert!(all(
509 lhs.nrows() == rhs.nrows(),
510 lhs.ncols() == rhs.ncols()
511 ));
512 (lhs.transpose() + rhs.transpose()).into_transpose()
513 }
514 let lhs = self.rb();
515 imp(
516 lhs.as_dyn_cols().as_dyn_stride(),
517 rhs.rb().as_dyn_cols().as_dyn_stride(),
518 )
519 .into_col_shape(lhs.ncols())
520 }
521 }
522 impl<
523 T: ComplexField,
524 Dim: Shape,
525 LT: (Conjugate<Canonical = T>),
526 LStride: Stride,
527 RT: (Conjugate<Canonical = T>),
528 RStride: Stride,
529 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
530 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
531 > Add<diag::generic::Diag<R>> for diag::generic::Diag<L>
532 {
533 type Output = Diag<T, Dim>;
534
535 fn add(self, rhs: _) {
536 #[track_caller]
537 fn imp<
538 T: ComplexField,
539 LT: Conjugate<Canonical = T>,
540 RT: Conjugate<Canonical = T>,
541 >(
542 lhs: ColRef<'_, LT>,
543 rhs: ColRef<'_, RT>,
544 ) -> Col<T> {
545 assert!(all(lhs.nrows() == rhs.nrows()));
546 lhs + rhs
547 }
548 let lhs = self.rb();
549 imp(
550 lhs.column_vector().as_dyn_rows().as_dyn_stride(),
551 rhs.rb().column_vector().as_dyn_rows().as_dyn_stride(),
552 )
553 .into_row_shape(lhs.dim())
554 .into_diagonal()
555 }
556 }
557});
558impl_binop!({
559 impl<
560 T: ComplexField,
561 Rows: Shape,
562 Cols: Shape,
563 LT: (Conjugate<Canonical = T>),
564 LRStride: Stride,
565 LCStride: Stride,
566 RT: (Conjugate<Canonical = T>),
567 RRStride: Stride,
568 RCStride: Stride,
569 L: (for<'a> Reborrow<
570 'a,
571 Target = mat::Ref<'a, LT, Rows, Cols, LRStride, LCStride>,
572 >),
573 R: (for<'a> Reborrow<
574 'a,
575 Target = mat::Ref<'a, RT, Rows, Cols, RRStride, RCStride>,
576 >),
577 > Sub<mat::generic::Mat<R>> for mat::generic::Mat<L>
578 {
579 type Output = Mat<T, Rows, Cols>;
580
581 fn sub(self, rhs: _) {
582 #[track_caller]
583 fn imp<
584 T: ComplexField,
585 LT: Conjugate<Canonical = T>,
586 RT: Conjugate<Canonical = T>,
587 >(
588 lhs: MatRef<'_, LT>,
589 rhs: MatRef<'_, RT>,
590 ) -> Mat<T> {
591 assert!(all(
592 lhs.nrows() == rhs.nrows(),
593 lhs.ncols() == rhs.ncols()
594 ));
595 zip!(lhs, rhs).map(sub_fn::<LT, RT>())
596 }
597 let lhs = self.rb();
598 imp(
599 lhs.as_dyn().as_dyn_stride(),
600 rhs.rb().as_dyn().as_dyn_stride(),
601 )
602 .into_shape(lhs.nrows(), lhs.ncols())
603 }
604 }
605 impl<
606 T: ComplexField,
607 Rows: Shape,
608 LT: (Conjugate<Canonical = T>),
609 LRStride: Stride,
610 RT: (Conjugate<Canonical = T>),
611 RRStride: Stride,
612 L: (for<'a> Reborrow<'a, Target = col::Ref<'a, LT, Rows, LRStride>>),
613 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Rows, RRStride>>),
614 > Sub<col::generic::Col<R>> for col::generic::Col<L>
615 {
616 type Output = Col<T, Rows>;
617
618 fn sub(self, rhs: _) {
619 #[track_caller]
620 fn imp<
621 T: ComplexField,
622 LT: Conjugate<Canonical = T>,
623 RT: Conjugate<Canonical = T>,
624 >(
625 lhs: ColRef<'_, LT>,
626 rhs: ColRef<'_, RT>,
627 ) -> Col<T> {
628 assert!(all(
629 lhs.nrows() == rhs.nrows(),
630 lhs.ncols() == rhs.ncols()
631 ));
632 zip!(lhs, rhs).map(sub_fn::<LT, RT>())
633 }
634 let lhs = self.rb();
635 imp(
636 lhs.as_dyn_rows().as_dyn_stride(),
637 rhs.rb().as_dyn_rows().as_dyn_stride(),
638 )
639 .into_row_shape(lhs.nrows())
640 }
641 }
642 impl<
643 T: ComplexField,
644 Cols: Shape,
645 LT: (Conjugate<Canonical = T>),
646 LCStride: Stride,
647 RT: (Conjugate<Canonical = T>),
648 RCStride: Stride,
649 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Cols, LCStride>>),
650 R: (for<'a> Reborrow<'a, Target = row::Ref<'a, RT, Cols, RCStride>>),
651 > Sub<row::generic::Row<R>> for row::generic::Row<L>
652 {
653 type Output = Row<T, Cols>;
654
655 fn sub(self, rhs: _) {
656 #[track_caller]
657 fn imp<
658 T: ComplexField,
659 LT: Conjugate<Canonical = T>,
660 RT: Conjugate<Canonical = T>,
661 >(
662 lhs: RowRef<'_, LT>,
663 rhs: RowRef<'_, RT>,
664 ) -> Row<T> {
665 assert!(all(
666 lhs.nrows() == rhs.nrows(),
667 lhs.ncols() == rhs.ncols()
668 ));
669 (lhs.transpose() - rhs.transpose()).into_transpose()
670 }
671 let lhs = self.rb();
672 imp(
673 lhs.as_dyn_cols().as_dyn_stride(),
674 rhs.rb().as_dyn_cols().as_dyn_stride(),
675 )
676 .into_col_shape(lhs.ncols())
677 }
678 }
679 impl<
680 T: ComplexField,
681 Dim: Shape,
682 LT: (Conjugate<Canonical = T>),
683 LStride: Stride,
684 RT: (Conjugate<Canonical = T>),
685 RStride: Stride,
686 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
687 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
688 > Sub<diag::generic::Diag<R>> for diag::generic::Diag<L>
689 {
690 type Output = Diag<T, Dim>;
691
692 fn sub(self, rhs: _) {
693 #[track_caller]
694 fn imp<
695 T: ComplexField,
696 LT: Conjugate<Canonical = T>,
697 RT: Conjugate<Canonical = T>,
698 >(
699 lhs: ColRef<'_, LT>,
700 rhs: ColRef<'_, RT>,
701 ) -> Col<T> {
702 assert!(all(lhs.nrows() == rhs.nrows()));
703 lhs - rhs
704 }
705 let lhs = self.rb();
706 imp(
707 lhs.column_vector().as_dyn_rows().as_dyn_stride(),
708 rhs.rb().column_vector().as_dyn_rows().as_dyn_stride(),
709 )
710 .into_row_shape(lhs.dim())
711 .into_diagonal()
712 }
713 }
714});
715impl_op!({
716 impl<
717 T: Conjugate,
718 Rows: Shape,
719 Cols: Shape,
720 RStride: Stride,
721 CStride: Stride,
722 Inner: (for<'a> Reborrow<
723 'a,
724 Target = mat::Ref<'a, T, Rows, Cols, RStride, CStride>,
725 >),
726 > Neg for mat::generic::Mat<Inner>
727 {
728 type Output = Mat<T::Canonical, Rows, Cols>;
729
730 fn neg(self) {
731 #[track_caller]
732 fn imp<T: Conjugate>(A: MatRef<'_, T>) -> Mat<T::Canonical> {
733 zip!(A).map(neg_fn::<T>())
734 }
735 let A = self.rb();
736 imp(A.as_dyn().as_dyn_stride()).into_shape(A.nrows(), A.ncols())
737 }
738 }
739 impl<
740 T: Conjugate,
741 Rows: Shape,
742 RStride: Stride,
743 Inner: (for<'a> Reborrow<'a, Target = col::Ref<'a, T, Rows, RStride>>),
744 > Neg for col::generic::Col<Inner>
745 {
746 type Output = Col<T::Canonical, Rows>;
747
748 fn neg(self) {
749 #[track_caller]
750 fn imp<T: Conjugate>(A: ColRef<'_, T>) -> Col<T::Canonical> {
751 zip!(A).map(neg_fn::<T>())
752 }
753 let A = self.rb();
754 imp(A.as_dyn_rows().as_dyn_stride()).into_row_shape(A.nrows())
755 }
756 }
757 impl<
758 T: Conjugate,
759 Cols: Shape,
760 CStride: Stride,
761 Inner: (for<'a> Reborrow<'a, Target = row::Ref<'a, T, Cols, CStride>>),
762 > Neg for row::generic::Row<Inner>
763 {
764 type Output = Row<T::Canonical, Cols>;
765
766 fn neg(self) {
767 #[track_caller]
768 fn imp<T: Conjugate>(A: RowRef<'_, T>) -> Row<T::Canonical> {
769 (-A.transpose()).into_transpose()
770 }
771 let A = self.rb();
772 imp(A.as_dyn_cols().as_dyn_stride()).into_col_shape(A.ncols())
773 }
774 }
775 impl<
776 T: Conjugate,
777 Dim: Shape,
778 Stride: (crate::Stride),
779 Inner: (for<'a> Reborrow<'a, Target = diag::Ref<'a, T, Dim, Stride>>),
780 > Neg for diag::generic::Diag<Inner>
781 {
782 type Output = Diag<T::Canonical, Dim>;
783
784 fn neg(self) {
785 #[track_caller]
786 fn imp<T: Conjugate>(A: ColRef<'_, T>) -> Col<T::Canonical> {
787 -A
788 }
789 let A = self.rb().column_vector();
790 imp(A.as_dyn_rows().as_dyn_stride())
791 .into_row_shape(A.nrows())
792 .into_diagonal()
793 }
794 }
795});
796#[inline]
797fn add_fn<
798 LhsT: Conjugate<Canonical: ComplexField>,
799 RhsT: Conjugate<Canonical = LhsT::Canonical>,
800>()
801-> impl FnMut(linalg::zip::Zip<&LhsT, linalg::zip::Last<&RhsT>>) -> LhsT::Canonical
802{
803 #[inline(always)]
804 move |unzip!(a, b)| Conj::apply(a) + Conj::apply(b)
805}
806#[inline]
807fn sub_fn<
808 LhsT: Conjugate<Canonical: ComplexField>,
809 RhsT: Conjugate<Canonical = LhsT::Canonical>,
810>()
811-> impl FnMut(linalg::zip::Zip<&LhsT, linalg::zip::Last<&RhsT>>) -> LhsT::Canonical
812{
813 #[inline(always)]
814 move |unzip!(a, b)| Conj::apply(a) - Conj::apply(b)
815}
816#[inline]
817fn mul_fn<
818 LhsT: Conjugate<Canonical: ComplexField>,
819 RhsT: Conjugate<Canonical = LhsT::Canonical>,
820>()
821-> impl FnMut(linalg::zip::Zip<&LhsT, linalg::zip::Last<&RhsT>>) -> LhsT::Canonical
822{
823 #[inline(always)]
824 move |unzip!(a, b)| Conj::apply(a) * Conj::apply(b)
825}
826#[inline]
827fn neg_fn<LhsT: Conjugate<Canonical: ComplexField>>()
828-> impl FnMut(linalg::zip::Last<&LhsT>) -> LhsT::Canonical {
829 #[inline(always)]
830 move |unzip!(a)| -Conj::apply(a)
831}
832#[inline]
833fn add_assign_fn<LhsT: ComplexField, RhsT: Conjugate<Canonical = LhsT>>()
834-> impl FnMut(linalg::zip::Zip<&mut LhsT, linalg::zip::Last<&RhsT>>) {
835 #[inline(always)]
836 move |unzip!(a, b)| *a = Conj::apply(a) + Conj::apply(b)
837}
838#[inline]
839fn sub_assign_fn<LhsT: ComplexField, RhsT: Conjugate<Canonical = LhsT>>()
840-> impl FnMut(linalg::zip::Zip<&mut LhsT, linalg::zip::Last<&RhsT>>) {
841 #[inline(always)]
842 move |unzip!(a, b)| *a = Conj::apply(a) - Conj::apply(b)
843}
844mod matmul {
845 use super::*;
846 use crate::assert;
847 impl_binop!({
848 impl<
849 T: ComplexField,
850 Rows: Shape,
851 Cols: Shape,
852 Depth: Shape,
853 LT: (Conjugate<Canonical = T>),
854 LRStride: Stride,
855 LCStride: Stride,
856 RT: (Conjugate<Canonical = T>),
857 RRStride: Stride,
858 RCStride: Stride,
859 L: (for<'a> Reborrow<
860 'a,
861 Target = mat::Ref<'a, LT, Rows, Depth, LRStride, LCStride>,
862 >),
863 R: (for<'a> Reborrow<
864 'a,
865 Target = mat::Ref<'a, RT, Depth, Cols, RRStride, RCStride>,
866 >),
867 > Mul<mat::generic::Mat<R>> for mat::generic::Mat<L>
868 {
869 type Output = Mat<T, Rows, Cols>;
870
871 fn mul(self, rhs: _) {
872 #[track_caller]
873 fn imp<
874 T: ComplexField,
875 LT: Conjugate<Canonical = T>,
876 RT: Conjugate<Canonical = T>,
877 >(
878 lhs: MatRef<'_, LT>,
879 rhs: MatRef<'_, RT>,
880 ) -> Mat<T> {
881 assert!(lhs.ncols() == rhs.nrows());
882 let mut out = Mat::zeros(lhs.nrows(), rhs.ncols());
883 crate::linalg::matmul::matmul(
884 out.as_mut(),
885 Accum::Replace,
886 lhs,
887 rhs,
888 T::one_impl(),
889 get_global_parallelism(),
890 );
891 out
892 }
893 let lhs = self.rb();
894 let rhs = rhs.rb();
895 imp(lhs.as_dyn().as_dyn_stride(), rhs.as_dyn().as_dyn_stride())
896 .into_shape(lhs.nrows(), rhs.ncols())
897 }
898 }
899 impl<
900 T: ComplexField,
901 Rows: Shape,
902 Cols: Shape,
903 LT: (Conjugate<Canonical = T>),
904 LRStride: Stride,
905 LCStride: Stride,
906 RT: (Conjugate<Canonical = T>),
907 RRStride: Stride,
908 L: (for<'a> Reborrow<
909 'a,
910 Target = mat::Ref<'a, LT, Rows, Cols, LRStride, LCStride>,
911 >),
912 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Cols, RRStride>>),
913 > Mul<col::generic::Col<R>> for mat::generic::Mat<L>
914 {
915 type Output = Col<T, Rows>;
916
917 fn mul(self, rhs: _) {
918 #[track_caller]
919 fn imp<
920 T: ComplexField,
921 LT: Conjugate<Canonical = T>,
922 RT: Conjugate<Canonical = T>,
923 >(
924 lhs: MatRef<'_, LT>,
925 rhs: ColRef<'_, RT>,
926 ) -> Col<T> {
927 assert!(lhs.ncols() == rhs.nrows());
928 let mut out = Col::zeros(lhs.nrows());
929 crate::linalg::matmul::matmul(
930 out.as_mut(),
931 Accum::Replace,
932 lhs,
933 rhs,
934 T::one_impl(),
935 get_global_parallelism(),
936 );
937 out
938 }
939 let lhs = self.rb();
940 let rhs = rhs.rb();
941 imp(
942 lhs.as_dyn().as_dyn_stride(),
943 rhs.as_dyn_rows().as_dyn_stride(),
944 )
945 .into_row_shape(lhs.nrows())
946 }
947 }
948 impl<
949 T: ComplexField,
950 Rows: Shape,
951 Cols: Shape,
952 LT: (Conjugate<Canonical = T>),
953 LCStride: Stride,
954 RT: (Conjugate<Canonical = T>),
955 RRStride: Stride,
956 RCStride: Stride,
957 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Rows, LCStride>>),
958 R: (for<'a> Reborrow<
959 'a,
960 Target = mat::Ref<'a, RT, Rows, Cols, RRStride, RCStride>,
961 >),
962 > Mul<mat::generic::Mat<R>> for row::generic::Row<L>
963 {
964 type Output = Row<T, Cols>;
965
966 fn mul(self, rhs: _) {
967 #[track_caller]
968 fn imp<
969 T: ComplexField,
970 LT: Conjugate<Canonical = T>,
971 RT: Conjugate<Canonical = T>,
972 >(
973 lhs: RowRef<'_, LT>,
974 rhs: MatRef<'_, RT>,
975 ) -> Row<T> {
976 assert!(lhs.ncols() == rhs.nrows());
977 let mut out = Row::zeros(rhs.ncols());
978 crate::linalg::matmul::matmul(
979 out.as_mut(),
980 Accum::Replace,
981 lhs,
982 rhs,
983 T::one_impl(),
984 get_global_parallelism(),
985 );
986 out
987 }
988 let lhs = self.rb();
989 let rhs = rhs.rb();
990 imp(
991 lhs.as_dyn_cols().as_dyn_stride(),
992 rhs.as_dyn().as_dyn_stride(),
993 )
994 .into_col_shape(rhs.ncols())
995 }
996 }
997 impl<
998 T: ComplexField,
999 Dim: Shape,
1000 LT: (Conjugate<Canonical = T>),
1001 LCStride: Stride,
1002 RT: (Conjugate<Canonical = T>),
1003 RRStride: Stride,
1004 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Dim, LCStride>>),
1005 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Dim, RRStride>>),
1006 > Mul<col::generic::Col<R>> for row::generic::Row<L>
1007 {
1008 type Output = T;
1009
1010 fn mul(self, rhs: _) {
1011 #[track_caller]
1012 fn imp<
1013 T: ComplexField,
1014 LT: Conjugate<Canonical = T>,
1015 RT: Conjugate<Canonical = T>,
1016 >(
1017 lhs: RowRef<'_, LT>,
1018 rhs: ColRef<'_, RT>,
1019 ) -> T {
1020 assert!(lhs.ncols() == rhs.nrows());
1021 let mut out = [[zero::<T>()]];
1022 crate::linalg::matmul::matmul(
1023 MatMut::from_row_major_array_mut(&mut out),
1024 Accum::Replace,
1025 lhs.as_mat(),
1026 rhs.as_mat(),
1027 T::one_impl(),
1028 get_global_parallelism(),
1029 );
1030 let [[out]] = out;
1031 out
1032 }
1033 let lhs = self.rb();
1034 let rhs = rhs.rb();
1035 imp(
1036 lhs.as_dyn_cols().as_dyn_stride(),
1037 rhs.as_dyn_rows().as_dyn_stride(),
1038 )
1039 }
1040 }
1041 impl<
1042 T: ComplexField,
1043 Rows: Shape,
1044 Cols: Shape,
1045 LT: (Conjugate<Canonical = T>),
1046 LRStride: Stride,
1047 RT: (Conjugate<Canonical = T>),
1048 RCStride: Stride,
1049 L: (for<'a> Reborrow<'a, Target = col::Ref<'a, LT, Rows, LRStride>>),
1050 R: (for<'a> Reborrow<'a, Target = row::Ref<'a, RT, Cols, RCStride>>),
1051 > Mul<row::generic::Row<R>> for col::generic::Col<L>
1052 {
1053 type Output = Mat<T, Rows, Cols>;
1054
1055 fn mul(self, rhs: _) {
1056 #[track_caller]
1057 fn imp<
1058 T: ComplexField,
1059 LT: Conjugate<Canonical = T>,
1060 RT: Conjugate<Canonical = T>,
1061 >(
1062 lhs: ColRef<'_, LT>,
1063 rhs: RowRef<'_, RT>,
1064 ) -> Mat<T> {
1065 assert!(lhs.ncols() == rhs.nrows());
1066 let mut out = Mat::zeros(lhs.nrows(), rhs.ncols());
1067 crate::linalg::matmul::matmul(
1068 out.rb_mut(),
1069 Accum::Replace,
1070 lhs,
1071 rhs,
1072 T::one_impl(),
1073 get_global_parallelism(),
1074 );
1075 out
1076 }
1077 let lhs = self.rb();
1078 let rhs = rhs.rb();
1079 imp(
1080 lhs.as_dyn_rows().as_dyn_stride(),
1081 rhs.as_dyn_cols().as_dyn_stride(),
1082 )
1083 .into_shape(lhs.nrows(), rhs.ncols())
1084 }
1085 }
1086 impl<
1087 T: ComplexField,
1088 Dim: Shape,
1089 Cols: Shape,
1090 LT: (Conjugate<Canonical = T>),
1091 LStride: Stride,
1092 RT: (Conjugate<Canonical = T>),
1093 RRStride: Stride,
1094 RCStride: Stride,
1095 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
1096 R: (for<'a> Reborrow<
1097 'a,
1098 Target = mat::Ref<'a, RT, Dim, Cols, RRStride, RCStride>,
1099 >),
1100 > Mul<mat::generic::Mat<R>> for diag::generic::Diag<L>
1101 {
1102 type Output = Mat<T, Dim, Cols>;
1103
1104 fn mul(self, rhs: _) {
1105 #[track_caller]
1106 fn imp<
1107 T: ComplexField,
1108 LT: Conjugate<Canonical = T>,
1109 RT: Conjugate<Canonical = T>,
1110 >(
1111 lhs: ColRef<'_, LT>,
1112 rhs: MatRef<'_, RT>,
1113 ) -> Mat<T> {
1114 let lhs_dim = lhs.nrows();
1115 let rhs_nrows = rhs.nrows();
1116 assert!(lhs_dim == rhs_nrows);
1117 Mat::from_fn(rhs.nrows(), rhs.ncols(), |i, j| {
1118 Conj::apply(lhs.at(i)) * Conj::apply(rhs.at(i, j))
1119 })
1120 }
1121 let lhs = self.rb();
1122 let rhs = rhs.rb();
1123 imp(
1124 lhs.column_vector().as_dyn_rows().as_dyn_stride(),
1125 rhs.as_dyn().as_dyn_stride(),
1126 )
1127 .into_shape(rhs.nrows(), rhs.ncols())
1128 }
1129 }
1130 impl<
1131 T: ComplexField,
1132 Dim: Shape,
1133 LT: (Conjugate<Canonical = T>),
1134 LStride: Stride,
1135 RT: (Conjugate<Canonical = T>),
1136 RRStride: Stride,
1137 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
1138 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Dim, RRStride>>),
1139 > Mul<col::generic::Col<R>> for diag::generic::Diag<L>
1140 {
1141 type Output = Col<T, Dim>;
1142
1143 fn mul(self, rhs: _) {
1144 #[track_caller]
1145 fn imp<
1146 T: ComplexField,
1147 LT: Conjugate<Canonical = T>,
1148 RT: Conjugate<Canonical = T>,
1149 >(
1150 lhs: ColRef<'_, LT>,
1151 rhs: ColRef<'_, RT>,
1152 ) -> Col<T> {
1153 let lhs_dim = lhs.nrows();
1154 let rhs_nrows = rhs.nrows();
1155 assert!(lhs_dim == rhs_nrows);
1156 zip!(lhs, rhs).map(mul_fn::<LT, RT>())
1157 }
1158 let lhs = self.rb();
1159 let rhs = rhs.rb();
1160 imp(
1161 lhs.column_vector().as_dyn_rows().as_dyn_stride(),
1162 rhs.as_dyn_rows().as_dyn_stride(),
1163 )
1164 .into_row_shape(rhs.nrows())
1165 }
1166 }
1167 impl<
1168 T: ComplexField,
1169 Dim: Shape,
1170 Rows: Shape,
1171 LT: (Conjugate<Canonical = T>),
1172 LRStride: Stride,
1173 LCStride: Stride,
1174 RT: (Conjugate<Canonical = T>),
1175 RStride: Stride,
1176 L: (for<'a> Reborrow<
1177 'a,
1178 Target = mat::Ref<'a, LT, Rows, Dim, LRStride, LCStride>,
1179 >),
1180 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
1181 > Mul<diag::generic::Diag<R>> for mat::generic::Mat<L>
1182 {
1183 type Output = Mat<T, Rows, Dim>;
1184
1185 fn mul(self, rhs: _) {
1186 #[track_caller]
1187 fn imp<
1188 T: ComplexField,
1189 LT: Conjugate<Canonical = T>,
1190 RT: Conjugate<Canonical = T>,
1191 >(
1192 lhs: MatRef<'_, LT>,
1193 rhs: ColRef<'_, RT>,
1194 ) -> Mat<T> {
1195 let lhs_ncols = lhs.ncols();
1196 let rhs_dim = rhs.nrows();
1197 assert!(lhs_ncols == rhs_dim);
1198 let mut f = mul_fn::<LT, RT>();
1199 Mat::from_fn(lhs.nrows(), lhs.ncols(), |i, j| {
1200 f(linalg::zip::Zip(
1201 lhs.at(i, j),
1202 linalg::zip::Last(rhs.at(j)),
1203 ))
1204 })
1205 }
1206 let lhs = self.rb();
1207 let rhs = rhs.rb();
1208 imp(
1209 lhs.as_dyn().as_dyn_stride(),
1210 rhs.column_vector().as_dyn_rows().as_dyn_stride(),
1211 )
1212 .into_shape(lhs.nrows(), lhs.ncols())
1213 }
1214 }
1215 impl<
1216 T: ComplexField,
1217 Dim: Shape,
1218 LT: (Conjugate<Canonical = T>),
1219 LCStride: Stride,
1220 RT: (Conjugate<Canonical = T>),
1221 RStride: Stride,
1222 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Dim, LCStride>>),
1223 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
1224 > Mul<diag::generic::Diag<R>> for row::generic::Row<L>
1225 {
1226 type Output = Row<T, Dim>;
1227
1228 fn mul(self, rhs: _) {
1229 #[track_caller]
1230 fn imp<
1231 T: ComplexField,
1232 LT: Conjugate<Canonical = T>,
1233 RT: Conjugate<Canonical = T>,
1234 >(
1235 lhs: RowRef<'_, LT>,
1236 rhs: ColRef<'_, RT>,
1237 ) -> Row<T> {
1238 let lhs_ncols = lhs.ncols();
1239 let rhs_dim = rhs.nrows();
1240 assert!(lhs_ncols == rhs_dim);
1241 (rhs.as_diagonal() * lhs.transpose()).into_transpose()
1242 }
1243 let lhs = self.rb();
1244 let rhs = rhs.rb();
1245 imp(
1246 lhs.as_dyn_cols().as_dyn_stride(),
1247 rhs.column_vector().as_dyn_rows().as_dyn_stride(),
1248 )
1249 .into_col_shape(lhs.ncols())
1250 }
1251 }
1252 impl<
1253 T: ComplexField,
1254 Dim: Shape,
1255 LT: (Conjugate<Canonical = T>),
1256 LStride: Stride,
1257 RT: (Conjugate<Canonical = T>),
1258 RStride: Stride,
1259 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
1260 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
1261 > Mul<diag::generic::Diag<R>> for diag::generic::Diag<L>
1262 {
1263 type Output = Diag<T, Dim>;
1264
1265 fn mul(self, rhs: _) {
1266 #[track_caller]
1267 fn imp<
1268 T: ComplexField,
1269 LT: Conjugate<Canonical = T>,
1270 RT: Conjugate<Canonical = T>,
1271 >(
1272 lhs: ColRef<'_, LT>,
1273 rhs: ColRef<'_, RT>,
1274 ) -> Col<T> {
1275 let lhs_dim = lhs.nrows();
1276 let rhs_dim = rhs.nrows();
1277 assert!(lhs_dim == rhs_dim);
1278 lhs.as_diagonal() * rhs
1279 }
1280 let lhs = self.rb().column_vector();
1281 let rhs = rhs.rb().column_vector();
1282 imp(
1283 lhs.as_dyn_rows().as_dyn_stride(),
1284 rhs.as_dyn_rows().as_dyn_stride(),
1285 )
1286 .into_row_shape(lhs.nrows())
1287 .into_diagonal()
1288 }
1289 }
1290 });
1291}
1292impl_binop!({
1293 impl<
1294 I: Index,
1295 N: Shape,
1296 L: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, N>>),
1297 R: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, N>>),
1298 > Mul<perm::generic::Perm<R>> for perm::generic::Perm<L>
1299 {
1300 type Output = Perm<I, N>;
1301
1302 fn mul(self, rhs: _) {
1303 #[track_caller]
1304 fn imp<I: Index>(
1305 lhs: PermRef<'_, I>,
1306 rhs: PermRef<'_, I>,
1307 ) -> Perm<I> {
1308 assert!(lhs.len() == rhs.len());
1309 let truncate = <I::Signed as SignedIndex>::truncate;
1310 let mut fwd =
1311 alloc::vec![I::from_signed(truncate(0)); lhs.len()]
1312 .into_boxed_slice();
1313 let mut inv =
1314 alloc::vec![I::from_signed(truncate(0)); lhs.len()]
1315 .into_boxed_slice();
1316 for (fwd, rhs) in fwd.iter_mut().zip(rhs.arrays().0) {
1317 *fwd = lhs.arrays().0[rhs.to_signed().zx()];
1318 }
1319 for (i, fwd) in fwd.iter().enumerate() {
1320 inv[fwd.to_signed().zx()] =
1321 I::from_signed(I::Signed::truncate(i));
1322 }
1323 Perm::new_checked(fwd, inv, lhs.len())
1324 }
1325 let lhs = self.rb();
1326 let rhs = rhs.rb();
1327 let N = lhs.len();
1328 let n = N.unbound();
1329 imp(lhs.as_shape(n), rhs.as_shape(n)).into_shape(N)
1330 }
1331 }
1332 impl<
1333 I: Index,
1334 T: ComplexField,
1335 Rows: Shape,
1336 Cols: Shape,
1337 RT: (Conjugate<Canonical = T>),
1338 RRStride: Stride,
1339 RCStride: Stride,
1340 L: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, Rows>>),
1341 R: (for<'a> Reborrow<
1342 'a,
1343 Target = mat::Ref<'a, RT, Rows, Cols, RRStride, RCStride>,
1344 >),
1345 > Mul<mat::generic::Mat<R>> for perm::generic::Perm<L>
1346 {
1347 type Output = Mat<T, Rows, Cols>;
1348
1349 fn mul(self, rhs: _) {
1350 #[track_caller]
1351 fn imp<I: Index, T: ComplexField, RT: Conjugate<Canonical = T>>(
1352 lhs: PermRef<'_, I>,
1353 rhs: MatRef<'_, RT>,
1354 ) -> Mat<T> {
1355 with_dim!(M, rhs.nrows());
1356 with_dim!(N, rhs.ncols());
1357 let rhs = rhs.as_shape(M, N);
1358 let lhs = lhs.as_shape(M);
1359 let mut out = Mat::zeros(rhs.nrows(), rhs.ncols());
1360 let fwd = lhs.bound_arrays().0;
1361 for j in rhs.ncols().indices() {
1362 for i in rhs.nrows().indices() {
1363 let fwd = fwd[i];
1364 let rhs = rhs.at(fwd.zx(), j);
1365 out[(i, j)] = Conj::apply(rhs);
1366 }
1367 }
1368 out.into_shape(*M, *N)
1369 }
1370 let lhs = self.rb();
1371 let rhs = rhs.rb();
1372 let m = rhs.nrows().unbound();
1373 imp(lhs.as_shape(m), rhs.as_dyn().as_dyn_stride())
1374 .into_shape(rhs.nrows(), rhs.ncols())
1375 }
1376 }
1377 impl<
1378 I: Index,
1379 T: ComplexField,
1380 Rows: Shape,
1381 RT: (Conjugate<Canonical = T>),
1382 RRStride: Stride,
1383 L: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, Rows>>),
1384 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Rows, RRStride>>),
1385 > Mul<col::generic::Col<R>> for perm::generic::Perm<L>
1386 {
1387 type Output = Col<T, Rows>;
1388
1389 fn mul(self, rhs: _) {
1390 #[track_caller]
1391 fn imp<I: Index, T: ComplexField, RT: Conjugate<Canonical = T>>(
1392 lhs: PermRef<'_, I>,
1393 rhs: ColRef<'_, RT>,
1394 ) -> Col<T> {
1395 with_dim!(M, rhs.nrows());
1396 let rhs = rhs.as_row_shape(M);
1397 let lhs = lhs.as_shape(M);
1398 let mut out = Col::zeros(rhs.nrows());
1399 let fwd = lhs.bound_arrays().0;
1400 for i in rhs.nrows().indices() {
1401 let fwd = fwd[i];
1402 let rhs = rhs.at(fwd.zx());
1403 out[i] = Conj::apply(rhs);
1404 }
1405 out.into_row_shape(*M)
1406 }
1407 let lhs = self.rb();
1408 let rhs = rhs.rb();
1409 let m = rhs.nrows().unbound();
1410 imp(lhs.as_shape(m), rhs.as_dyn_rows().as_dyn_stride())
1411 .into_row_shape(rhs.nrows())
1412 }
1413 }
1414 impl<
1415 I: Index,
1416 T: ComplexField,
1417 Rows: Shape,
1418 Cols: Shape,
1419 LT: (Conjugate<Canonical = T>),
1420 LRStride: Stride,
1421 LCStride: Stride,
1422 L: (for<'a> Reborrow<
1423 'a,
1424 Target = mat::Ref<'a, LT, Rows, Cols, LRStride, LCStride>,
1425 >),
1426 R: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, Cols>>),
1427 > Mul<perm::generic::Perm<R>> for mat::generic::Mat<L>
1428 {
1429 type Output = Mat<T, Rows, Cols>;
1430
1431 fn mul(self, rhs: _) {
1432 #[track_caller]
1433 fn imp<I: Index, T: ComplexField, LT: Conjugate<Canonical = T>>(
1434 lhs: MatRef<'_, LT>,
1435 rhs: PermRef<'_, I>,
1436 ) -> Mat<T> {
1437 with_dim!(M, lhs.nrows());
1438 with_dim!(N, lhs.ncols());
1439 let lhs = lhs.as_shape(M, N);
1440 let rhs = rhs.as_shape(N);
1441 let mut out = Mat::zeros(M, N);
1442 let inv = rhs.bound_arrays().1;
1443 for j in N.indices() {
1444 let inv = inv[j];
1445 for i in M.indices() {
1446 let lhs = lhs.at(i, inv.zx());
1447 out[(i, j)] = Conj::apply(lhs);
1448 }
1449 }
1450 out.into_shape(*M, *N)
1451 }
1452 let lhs = self.rb();
1453 let rhs = rhs.rb();
1454 let n = lhs.ncols().unbound();
1455 imp(lhs.as_dyn().as_dyn_stride(), rhs.as_shape(n))
1456 .into_shape(lhs.nrows(), lhs.ncols())
1457 }
1458 }
1459 impl<
1460 I: Index,
1461 T: ComplexField,
1462 Cols: Shape,
1463 LT: (Conjugate<Canonical = T>),
1464 LCStride: Stride,
1465 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Cols, LCStride>>),
1466 R: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, Cols>>),
1467 > Mul<perm::generic::Perm<R>> for row::generic::Row<L>
1468 {
1469 type Output = Row<T, Cols>;
1470
1471 fn mul(self, rhs: _) {
1472 #[track_caller]
1473 fn imp<I: Index, T: ComplexField, LT: Conjugate<Canonical = T>>(
1474 lhs: RowRef<'_, LT>,
1475 rhs: PermRef<'_, I>,
1476 ) -> Row<T> {
1477 with_dim!(N, lhs.ncols());
1478 let lhs = lhs.as_col_shape(N);
1479 let rhs = rhs.as_shape(N);
1480 let mut out = Row::zeros(N);
1481 let inv = rhs.bound_arrays().1;
1482 for j in N.indices() {
1483 let inv = inv[j];
1484 let lhs = lhs.at(inv.zx());
1485 out[j] = Conj::apply(lhs);
1486 }
1487 out.into_col_shape(*N)
1488 }
1489 let lhs = self.rb();
1490 let rhs = rhs.rb();
1491 let n = lhs.ncols().unbound();
1492 imp(lhs.as_dyn_cols().as_dyn_stride(), rhs.as_shape(n))
1493 .into_col_shape(lhs.ncols())
1494 }
1495 }
1496});
1497impl_binop!({
1498 impl<
1499 T: ComplexField,
1500 Rows: Shape,
1501 Cols: Shape,
1502 LT: (Conjugate<Canonical = T>),
1503 LRStride: Stride,
1504 LCStride: Stride,
1505 L: (for<'a> Reborrow<
1506 'a,
1507 Target = mat::Ref<'a, LT, Rows, Cols, LRStride, LCStride>,
1508 >),
1509 > Mul<Scale<T>> for mat::generic::Mat<L>
1510 {
1511 type Output = Mat<T, Rows, Cols>;
1512
1513 fn mul(self, rhs: _) {
1514 #[track_caller]
1515 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1516 lhs: MatRef<'_, LT>,
1517 rhs: &T,
1518 ) -> Mat<T> {
1519 zip!(lhs).map(|unzip!(x)| Conj::apply(x) * rhs)
1520 }
1521 let lhs = self.rb();
1522 let rhs = &rhs.0;
1523 imp(lhs.as_dyn().as_dyn_stride(), rhs)
1524 .into_shape(lhs.nrows(), lhs.ncols())
1525 }
1526 }
1527 impl<
1528 T: ComplexField,
1529 Rows: Shape,
1530 Cols: Shape,
1531 LT: (Conjugate<Canonical = T>),
1532 LRStride: Stride,
1533 LCStride: Stride,
1534 L: (for<'a> Reborrow<
1535 'a,
1536 Target = mat::Ref<'a, LT, Rows, Cols, LRStride, LCStride>,
1537 >),
1538 > Div<Scale<T>> for mat::generic::Mat<L>
1539 {
1540 type Output = Mat<T, Rows, Cols>;
1541
1542 fn div(self, rhs: _) {
1543 #[track_caller]
1544 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1545 lhs: MatRef<'_, LT>,
1546 rhs: &T,
1547 ) -> Mat<T> {
1548 lhs * Scale(rhs.recip())
1549 }
1550 let lhs = self.rb();
1551 let rhs = &rhs.0;
1552 imp(lhs.as_dyn().as_dyn_stride(), rhs)
1553 .into_shape(lhs.nrows(), lhs.ncols())
1554 }
1555 }
1556 impl<
1557 T: ComplexField,
1558 Rows: Shape,
1559 Cols: Shape,
1560 RT: (Conjugate<Canonical = T>),
1561 RRStride: Stride,
1562 RCStride: Stride,
1563 R: (for<'a> Reborrow<
1564 'a,
1565 Target = mat::Ref<'a, RT, Rows, Cols, RRStride, RCStride>,
1566 >),
1567 > Mul<mat::generic::Mat<R>> for Scale<T>
1568 {
1569 type Output = Mat<T, Rows, Cols>;
1570
1571 fn mul(self, rhs: _) {
1572 #[track_caller]
1573 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
1574 lhs: &T,
1575 rhs: MatRef<'_, RT>,
1576 ) -> Mat<T> {
1577 zip!(rhs).map(|unzip!(x)| lhs * Conj::apply(x))
1578 }
1579 let lhs = &self.0;
1580 let rhs = rhs.rb();
1581 imp(lhs, rhs.as_dyn().as_dyn_stride())
1582 .into_shape(rhs.nrows(), rhs.ncols())
1583 }
1584 }
1585});
1586impl_binop!({
1587 impl<
1588 T: ComplexField,
1589 Rows: Shape,
1590 LT: (Conjugate<Canonical = T>),
1591 LRStride: Stride,
1592 L: (for<'a> Reborrow<'a, Target = col::Ref<'a, LT, Rows, LRStride>>),
1593 > Mul<Scale<T>> for col::generic::Col<L>
1594 {
1595 type Output = Col<T, Rows>;
1596
1597 fn mul(self, rhs: _) {
1598 #[track_caller]
1599 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1600 lhs: ColRef<'_, LT>,
1601 rhs: &T,
1602 ) -> Col<T> {
1603 zip!(lhs).map(|unzip!(x)| Conj::apply(x) * rhs)
1604 }
1605 let lhs = self.rb();
1606 let rhs = &rhs.0;
1607 imp(lhs.as_dyn_rows().as_dyn_stride(), rhs)
1608 .into_row_shape(lhs.nrows())
1609 }
1610 }
1611 impl<
1612 T: ComplexField,
1613 Rows: Shape,
1614 LT: (Conjugate<Canonical = T>),
1615 LRStride: Stride,
1616 L: (for<'a> Reborrow<'a, Target = col::Ref<'a, LT, Rows, LRStride>>),
1617 > Div<Scale<T>> for col::generic::Col<L>
1618 {
1619 type Output = Col<T, Rows>;
1620
1621 fn div(self, rhs: _) {
1622 #[track_caller]
1623 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1624 lhs: ColRef<'_, LT>,
1625 rhs: &T,
1626 ) -> Col<T> {
1627 lhs * Scale(rhs.recip())
1628 }
1629 let lhs = self.rb();
1630 let rhs = &rhs.0;
1631 imp(lhs.as_dyn_rows().as_dyn_stride(), rhs)
1632 .into_row_shape(lhs.nrows())
1633 }
1634 }
1635 impl<
1636 T: ComplexField,
1637 Rows: Shape,
1638 RT: (Conjugate<Canonical = T>),
1639 RRStride: Stride,
1640 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Rows, RRStride>>),
1641 > Mul<col::generic::Col<R>> for Scale<T>
1642 {
1643 type Output = Col<T, Rows>;
1644
1645 fn mul(self, rhs: _) {
1646 #[track_caller]
1647 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
1648 lhs: &T,
1649 rhs: ColRef<'_, RT>,
1650 ) -> Col<T> {
1651 zip!(rhs).map(|unzip!(x)| lhs * Conj::apply(x))
1652 }
1653 let lhs = &self.0;
1654 let rhs = rhs.rb();
1655 imp(lhs, rhs.as_dyn_rows().as_dyn_stride())
1656 .into_row_shape(rhs.nrows())
1657 }
1658 }
1659});
1660impl_binop!({
1661 impl<
1662 T: ComplexField,
1663 Cols: Shape,
1664 LT: (Conjugate<Canonical = T>),
1665 LCStride: Stride,
1666 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Cols, LCStride>>),
1667 > Mul<Scale<T>> for row::generic::Row<L>
1668 {
1669 type Output = Row<T, Cols>;
1670
1671 fn mul(self, rhs: _) {
1672 #[track_caller]
1673 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1674 lhs: RowRef<'_, LT>,
1675 rhs: &T,
1676 ) -> Row<T> {
1677 (lhs.transpose() * Scale::from_ref(rhs)).into_transpose()
1678 }
1679 let lhs = self.rb();
1680 let rhs = &rhs.0;
1681 imp(lhs.as_dyn_cols().as_dyn_stride(), rhs)
1682 .into_col_shape(lhs.ncols())
1683 }
1684 }
1685 impl<
1686 T: ComplexField,
1687 Cols: Shape,
1688 LT: (Conjugate<Canonical = T>),
1689 LCStride: Stride,
1690 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Cols, LCStride>>),
1691 > Div<Scale<T>> for row::generic::Row<L>
1692 {
1693 type Output = Row<T, Cols>;
1694
1695 fn div(self, rhs: _) {
1696 #[track_caller]
1697 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1698 lhs: RowRef<'_, LT>,
1699 rhs: &T,
1700 ) -> Row<T> {
1701 lhs * Scale(rhs.recip())
1702 }
1703 let lhs = self.rb();
1704 let rhs = &rhs.0;
1705 imp(lhs.as_dyn_cols().as_dyn_stride(), rhs)
1706 .into_col_shape(lhs.ncols())
1707 }
1708 }
1709 impl<
1710 T: ComplexField,
1711 Cols: Shape,
1712 RT: (Conjugate<Canonical = T>),
1713 RCStride: Stride,
1714 R: (for<'a> Reborrow<'a, Target = row::Ref<'a, RT, Cols, RCStride>>),
1715 > Mul<row::generic::Row<R>> for Scale<T>
1716 {
1717 type Output = Row<T, Cols>;
1718
1719 fn mul(self, rhs: _) {
1720 #[track_caller]
1721 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
1722 lhs: &T,
1723 rhs: RowRef<'_, RT>,
1724 ) -> Row<T> {
1725 (Scale::from_ref(lhs) * rhs.transpose()).into_transpose()
1726 }
1727 let lhs = &self.0;
1728 let rhs = rhs.rb();
1729 imp(lhs, rhs.as_dyn_cols().as_dyn_stride())
1730 .into_col_shape(rhs.ncols())
1731 }
1732 }
1733});
1734impl_binop!({
1735 impl<
1736 T: ComplexField,
1737 Dim: Shape,
1738 LT: (Conjugate<Canonical = T>),
1739 LStride: Stride,
1740 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
1741 > Mul<Scale<T>> for diag::generic::Diag<L>
1742 {
1743 type Output = Diag<T, Dim>;
1744
1745 fn mul(self, rhs: _) {
1746 #[track_caller]
1747 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1748 lhs: ColRef<'_, LT>,
1749 rhs: &T,
1750 ) -> Col<T> {
1751 lhs * Scale::from_ref(rhs)
1752 }
1753 let lhs = self.rb().column_vector();
1754 let rhs = &rhs.0;
1755 imp(lhs.as_dyn_rows().as_dyn_stride(), rhs)
1756 .into_row_shape(lhs.nrows())
1757 .into_diagonal()
1758 }
1759 }
1760 impl<
1761 T: ComplexField,
1762 Dim: Shape,
1763 LT: (Conjugate<Canonical = T>),
1764 LStride: Stride,
1765 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
1766 > Div<Scale<T>> for diag::generic::Diag<L>
1767 {
1768 type Output = Diag<T, Dim>;
1769
1770 fn div(self, rhs: _) {
1771 #[track_caller]
1772 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1773 lhs: ColRef<'_, LT>,
1774 rhs: &T,
1775 ) -> Col<T> {
1776 lhs * Scale(rhs.recip())
1777 }
1778 let lhs = self.rb().column_vector();
1779 let rhs = &rhs.0;
1780 imp(lhs.as_dyn_rows().as_dyn_stride(), rhs)
1781 .into_row_shape(lhs.nrows())
1782 .into_diagonal()
1783 }
1784 }
1785 impl<
1786 T: ComplexField,
1787 Dim: Shape,
1788 RT: (Conjugate<Canonical = T>),
1789 RStride: Stride,
1790 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
1791 > Mul<diag::generic::Diag<R>> for Scale<T>
1792 {
1793 type Output = Diag<T, Dim>;
1794
1795 fn mul(self, rhs: _) {
1796 #[track_caller]
1797 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
1798 lhs: &T,
1799 rhs: ColRef<'_, RT>,
1800 ) -> Col<T> {
1801 Scale::from_ref(lhs) * rhs
1802 }
1803 let lhs = &self.0;
1804 let rhs = rhs.rb().column_vector();
1805 imp(lhs, rhs.as_dyn_rows().as_dyn_stride())
1806 .into_row_shape(rhs.nrows())
1807 .into_diagonal()
1808 }
1809 }
1810});
1811impl_binop!({
1812 impl<
1813 T: ComplexField,
1814 Rows: Shape,
1815 Cols: Shape,
1816 LT: (Conjugate<Canonical = T>),
1817 LRStride: Stride,
1818 LCStride: Stride,
1819 L: (for<'a> Reborrow<
1820 'a,
1821 Target = mat::Ref<'a, LT, Rows, Cols, LRStride, LCStride>,
1822 >),
1823 > Mul<f64> for mat::generic::Mat<L>
1824 {
1825 type Output = Mat<T, Rows, Cols>;
1826
1827 fn mul(self, rhs: _) {
1828 #[track_caller]
1829 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1830 lhs: MatRef<'_, LT>,
1831 rhs: &T,
1832 ) -> Mat<T> {
1833 lhs * Scale::from_ref(rhs)
1834 }
1835 let lhs = self.rb();
1836 let rhs = &from_f64::<T>(*rhs);
1837 imp(lhs.as_dyn().as_dyn_stride(), rhs)
1838 .into_shape(lhs.nrows(), lhs.ncols())
1839 }
1840 }
1841 impl<
1842 T: ComplexField,
1843 Rows: Shape,
1844 Cols: Shape,
1845 LT: (Conjugate<Canonical = T>),
1846 LRStride: Stride,
1847 LCStride: Stride,
1848 L: (for<'a> Reborrow<
1849 'a,
1850 Target = mat::Ref<'a, LT, Rows, Cols, LRStride, LCStride>,
1851 >),
1852 > Div<f64> for mat::generic::Mat<L>
1853 {
1854 type Output = Mat<T, Rows, Cols>;
1855
1856 fn div(self, rhs: _) {
1857 #[track_caller]
1858 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1859 lhs: MatRef<'_, LT>,
1860 rhs: &T,
1861 ) -> Mat<T> {
1862 lhs * Scale(rhs.recip())
1863 }
1864 let lhs = self.rb();
1865 let rhs = &from_f64::<T>(*rhs);
1866 imp(lhs.as_dyn().as_dyn_stride(), rhs)
1867 .into_shape(lhs.nrows(), lhs.ncols())
1868 }
1869 }
1870 impl<
1871 T: ComplexField,
1872 Rows: Shape,
1873 Cols: Shape,
1874 RT: (Conjugate<Canonical = T>),
1875 RRStride: Stride,
1876 RCStride: Stride,
1877 R: (for<'a> Reborrow<
1878 'a,
1879 Target = mat::Ref<'a, RT, Rows, Cols, RRStride, RCStride>,
1880 >),
1881 > Mul<mat::generic::Mat<R>> for f64
1882 {
1883 type Output = Mat<T, Rows, Cols>;
1884
1885 fn mul(self, rhs: _) {
1886 #[track_caller]
1887 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
1888 lhs: &T,
1889 rhs: MatRef<'_, RT>,
1890 ) -> Mat<T> {
1891 Scale::from_ref(lhs) * rhs
1892 }
1893 let lhs = &from_f64::<T>(*self);
1894 let rhs = rhs.rb();
1895 imp(lhs, rhs.as_dyn().as_dyn_stride())
1896 .into_shape(rhs.nrows(), rhs.ncols())
1897 }
1898 }
1899});
1900impl_binop!({
1901 impl<
1902 T: ComplexField,
1903 Rows: Shape,
1904 LT: (Conjugate<Canonical = T>),
1905 LRStride: Stride,
1906 L: (for<'a> Reborrow<'a, Target = col::Ref<'a, LT, Rows, LRStride>>),
1907 > Mul<f64> for col::generic::Col<L>
1908 {
1909 type Output = Col<T, Rows>;
1910
1911 fn mul(self, rhs: _) {
1912 #[track_caller]
1913 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1914 lhs: ColRef<'_, LT>,
1915 rhs: &T,
1916 ) -> Col<T> {
1917 lhs * Scale::from_ref(rhs)
1918 }
1919 let lhs = self.rb();
1920 let rhs = &from_f64::<T>(*rhs);
1921 imp(lhs.as_dyn_rows().as_dyn_stride(), rhs)
1922 .into_row_shape(lhs.nrows())
1923 }
1924 }
1925 impl<
1926 T: ComplexField,
1927 Rows: Shape,
1928 LT: (Conjugate<Canonical = T>),
1929 LRStride: Stride,
1930 L: (for<'a> Reborrow<'a, Target = col::Ref<'a, LT, Rows, LRStride>>),
1931 > Div<f64> for col::generic::Col<L>
1932 {
1933 type Output = Col<T, Rows>;
1934
1935 fn div(self, rhs: _) {
1936 #[track_caller]
1937 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1938 lhs: ColRef<'_, LT>,
1939 rhs: &T,
1940 ) -> Col<T> {
1941 lhs * Scale(rhs.recip())
1942 }
1943 let lhs = self.rb();
1944 let rhs = &from_f64::<T>(*rhs);
1945 imp(lhs.as_dyn_rows().as_dyn_stride(), rhs)
1946 .into_row_shape(lhs.nrows())
1947 }
1948 }
1949 impl<
1950 T: ComplexField,
1951 Rows: Shape,
1952 RT: (Conjugate<Canonical = T>),
1953 RRStride: Stride,
1954 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Rows, RRStride>>),
1955 > Mul<col::generic::Col<R>> for f64
1956 {
1957 type Output = Col<T, Rows>;
1958
1959 fn mul(self, rhs: _) {
1960 #[track_caller]
1961 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
1962 lhs: &T,
1963 rhs: ColRef<'_, RT>,
1964 ) -> Col<T> {
1965 Scale::from_ref(lhs) * rhs
1966 }
1967 let lhs = &from_f64::<T>(*self);
1968 let rhs = rhs.rb();
1969 imp(lhs, rhs.as_dyn_rows().as_dyn_stride())
1970 .into_row_shape(rhs.nrows())
1971 }
1972 }
1973});
1974impl_binop!({
1975 impl<
1976 T: ComplexField,
1977 Cols: Shape,
1978 LT: (Conjugate<Canonical = T>),
1979 LCStride: Stride,
1980 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Cols, LCStride>>),
1981 > Mul<f64> for row::generic::Row<L>
1982 {
1983 type Output = Row<T, Cols>;
1984
1985 fn mul(self, rhs: _) {
1986 #[track_caller]
1987 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
1988 lhs: RowRef<'_, LT>,
1989 rhs: &T,
1990 ) -> Row<T> {
1991 (lhs.transpose() * Scale::from_ref(rhs)).into_transpose()
1992 }
1993 let lhs = self.rb();
1994 let rhs = &from_f64::<T>(*rhs);
1995 imp(lhs.as_dyn_cols().as_dyn_stride(), rhs)
1996 .into_col_shape(lhs.ncols())
1997 }
1998 }
1999 impl<
2000 T: ComplexField,
2001 Cols: Shape,
2002 LT: (Conjugate<Canonical = T>),
2003 LCStride: Stride,
2004 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Cols, LCStride>>),
2005 > Div<f64> for row::generic::Row<L>
2006 {
2007 type Output = Row<T, Cols>;
2008
2009 fn div(self, rhs: _) {
2010 #[track_caller]
2011 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
2012 lhs: RowRef<'_, LT>,
2013 rhs: &T,
2014 ) -> Row<T> {
2015 lhs * Scale(rhs.recip())
2016 }
2017 let lhs = self.rb();
2018 let rhs = &from_f64::<T>(*rhs);
2019 imp(lhs.as_dyn_cols().as_dyn_stride(), rhs)
2020 .into_col_shape(lhs.ncols())
2021 }
2022 }
2023 impl<
2024 T: ComplexField,
2025 Cols: Shape,
2026 RT: (Conjugate<Canonical = T>),
2027 RCStride: Stride,
2028 R: (for<'a> Reborrow<'a, Target = row::Ref<'a, RT, Cols, RCStride>>),
2029 > Mul<row::generic::Row<R>> for f64
2030 {
2031 type Output = Row<T, Cols>;
2032
2033 fn mul(self, rhs: _) {
2034 #[track_caller]
2035 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
2036 lhs: &T,
2037 rhs: RowRef<'_, RT>,
2038 ) -> Row<T> {
2039 (Scale::from_ref(lhs) * rhs.transpose()).into_transpose()
2040 }
2041 let lhs = &from_f64::<T>(*self);
2042 let rhs = rhs.rb();
2043 imp(lhs, rhs.as_dyn_cols().as_dyn_stride())
2044 .into_col_shape(rhs.ncols())
2045 }
2046 }
2047});
2048impl_binop!({
2049 impl<
2050 T: ComplexField,
2051 Dim: Shape,
2052 LT: (Conjugate<Canonical = T>),
2053 LStride: Stride,
2054 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
2055 > Mul<f64> for diag::generic::Diag<L>
2056 {
2057 type Output = Diag<T, Dim>;
2058
2059 fn mul(self, rhs: _) {
2060 #[track_caller]
2061 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
2062 lhs: ColRef<'_, LT>,
2063 rhs: &T,
2064 ) -> Col<T> {
2065 lhs * Scale::from_ref(rhs)
2066 }
2067 let lhs = self.rb().column_vector();
2068 let rhs = &from_f64::<T>(*rhs);
2069 imp(lhs.as_dyn_rows().as_dyn_stride(), rhs)
2070 .into_row_shape(lhs.nrows())
2071 .into_diagonal()
2072 }
2073 }
2074 impl<
2075 T: ComplexField,
2076 Dim: Shape,
2077 LT: (Conjugate<Canonical = T>),
2078 LStride: Stride,
2079 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
2080 > Div<f64> for diag::generic::Diag<L>
2081 {
2082 type Output = Diag<T, Dim>;
2083
2084 fn div(self, rhs: _) {
2085 #[track_caller]
2086 fn imp<T: ComplexField, LT: Conjugate<Canonical = T>>(
2087 lhs: ColRef<'_, LT>,
2088 rhs: &T,
2089 ) -> Col<T> {
2090 lhs * Scale(rhs.recip())
2091 }
2092 let lhs = self.rb().column_vector();
2093 let rhs = &from_f64::<T>(*rhs);
2094 imp(lhs.as_dyn_rows().as_dyn_stride(), rhs)
2095 .into_row_shape(lhs.nrows())
2096 .into_diagonal()
2097 }
2098 }
2099 impl<
2100 T: ComplexField,
2101 Dim: Shape,
2102 RT: (Conjugate<Canonical = T>),
2103 RStride: Stride,
2104 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
2105 > Mul<diag::generic::Diag<R>> for f64
2106 {
2107 type Output = Diag<T, Dim>;
2108
2109 fn mul(self, rhs: _) {
2110 #[track_caller]
2111 fn imp<T: ComplexField, RT: Conjugate<Canonical = T>>(
2112 lhs: &T,
2113 rhs: ColRef<'_, RT>,
2114 ) -> Col<T> {
2115 Scale::from_ref(lhs) * rhs
2116 }
2117 let lhs = &from_f64::<T>(*self);
2118 let rhs = rhs.rb().column_vector();
2119 imp(lhs, rhs.as_dyn_rows().as_dyn_stride())
2120 .into_row_shape(rhs.nrows())
2121 .into_diagonal()
2122 }
2123 }
2124});
2125impl_op_assign!({
2126 impl<
2127 T: ComplexField,
2128 Rows: Shape,
2129 Cols: Shape,
2130 LRStride: Stride,
2131 LCStride: Stride,
2132 L: (for<'a> ReborrowMut<
2133 'a,
2134 Target = mat::Mut<'a, T, Rows, Cols, LRStride, LCStride>,
2135 >),
2136 > MulAssign<Scale<T>> for mat::generic::Mat<L>
2137 {
2138 fn mul_assign(&mut self, rhs: _) {
2139 #[track_caller]
2140 fn imp<T: ComplexField>(lhs: MatMut<'_, T>, rhs: &T) {
2141 zip!(lhs).for_each(|unzip!(x)| *x *= rhs)
2142 }
2143 let lhs = self.rb_mut();
2144 let rhs = &rhs.0;
2145 imp(lhs.as_dyn_mut().as_dyn_stride_mut(), rhs)
2146 }
2147 }
2148 impl<
2149 T: ComplexField,
2150 Rows: Shape,
2151 Cols: Shape,
2152 LRStride: Stride,
2153 LCStride: Stride,
2154 L: (for<'a> ReborrowMut<
2155 'a,
2156 Target = mat::Mut<'a, T, Rows, Cols, LRStride, LCStride>,
2157 >),
2158 > DivAssign<Scale<T>> for mat::generic::Mat<L>
2159 {
2160 fn div_assign(&mut self, rhs: _) {
2161 #[track_caller]
2162 fn imp<T: ComplexField>(mut lhs: MatMut<'_, T>, rhs: &T) {
2163 lhs *= Scale(rhs.recip());
2164 }
2165 let lhs = self.rb_mut();
2166 let rhs = &rhs.0;
2167 imp(lhs.as_dyn_mut().as_dyn_stride_mut(), rhs)
2168 }
2169 }
2170});
2171impl_op_assign!({
2172 impl<
2173 T: ComplexField,
2174 Rows: Shape,
2175 LRStride: Stride,
2176 L: (for<'a> ReborrowMut<'a, Target = col::Mut<'a, T, Rows, LRStride>>),
2177 > MulAssign<Scale<T>> for col::generic::Col<L>
2178 {
2179 fn mul_assign(&mut self, rhs: _) {
2180 #[track_caller]
2181 fn imp<T: ComplexField>(lhs: ColMut<'_, T>, rhs: &T) {
2182 zip!(lhs).for_each(|unzip!(x)| *x *= rhs)
2183 }
2184 let lhs = self.rb_mut();
2185 let rhs = &rhs.0;
2186 imp(lhs.as_dyn_rows_mut().as_dyn_stride_mut(), rhs)
2187 }
2188 }
2189 impl<
2190 T: ComplexField,
2191 Rows: Shape,
2192 LRStride: Stride,
2193 L: (for<'a> ReborrowMut<'a, Target = col::Mut<'a, T, Rows, LRStride>>),
2194 > DivAssign<Scale<T>> for col::generic::Col<L>
2195 {
2196 fn div_assign(&mut self, rhs: _) {
2197 #[track_caller]
2198 fn imp<T: ComplexField>(mut lhs: ColMut<'_, T>, rhs: &T) {
2199 lhs *= Scale(rhs.recip());
2200 }
2201 let lhs = self.rb_mut();
2202 let rhs = &rhs.0;
2203 imp(lhs.as_dyn_rows_mut().as_dyn_stride_mut(), rhs)
2204 }
2205 }
2206});
2207impl_op_assign!({
2208 impl<
2209 T: ComplexField,
2210 Cols: Shape,
2211 LCStride: Stride,
2212 L: (for<'a> ReborrowMut<'a, Target = row::Mut<'a, T, Cols, LCStride>>),
2213 > MulAssign<Scale<T>> for row::generic::Row<L>
2214 {
2215 fn mul_assign(&mut self, rhs: _) {
2216 #[track_caller]
2217 fn imp<T: ComplexField>(lhs: RowMut<'_, T>, rhs: &T) {
2218 let mut lhs = lhs.transpose_mut();
2219 lhs *= Scale::from_ref(rhs);
2220 }
2221 let lhs = self.rb_mut();
2222 let rhs = &rhs.0;
2223 imp(lhs.as_dyn_cols_mut().as_dyn_stride_mut(), rhs)
2224 }
2225 }
2226 impl<
2227 T: ComplexField,
2228 Cols: Shape,
2229 LCStride: Stride,
2230 L: (for<'a> ReborrowMut<'a, Target = row::Mut<'a, T, Cols, LCStride>>),
2231 > DivAssign<Scale<T>> for row::generic::Row<L>
2232 {
2233 fn div_assign(&mut self, rhs: _) {
2234 #[track_caller]
2235 fn imp<T: ComplexField>(mut lhs: RowMut<'_, T>, rhs: &T) {
2236 lhs *= Scale(rhs.recip());
2237 }
2238 let lhs = self.rb_mut();
2239 let rhs = &rhs.0;
2240 imp(lhs.as_dyn_cols_mut().as_dyn_stride_mut(), rhs)
2241 }
2242 }
2243});
2244impl_op_assign!({
2245 impl<
2246 T: ComplexField,
2247 Cols: Shape,
2248 LCStride: Stride,
2249 L: (for<'a> ReborrowMut<'a, Target = diag::Mut<'a, T, Cols, LCStride>>),
2250 > MulAssign<Scale<T>> for diag::generic::Diag<L>
2251 {
2252 fn mul_assign(&mut self, rhs: _) {
2253 #[track_caller]
2254 fn imp<T: ComplexField>(mut lhs: ColMut<'_, T>, rhs: &T) {
2255 lhs *= Scale::from_ref(rhs);
2256 }
2257 let lhs = self.rb_mut().column_vector_mut();
2258 let rhs = &rhs.0;
2259 imp(lhs.as_dyn_rows_mut().as_dyn_stride_mut(), rhs)
2260 }
2261 }
2262 impl<
2263 T: ComplexField,
2264 Cols: Shape,
2265 LCStride: Stride,
2266 L: (for<'a> ReborrowMut<'a, Target = diag::Mut<'a, T, Cols, LCStride>>),
2267 > DivAssign<Scale<T>> for diag::generic::Diag<L>
2268 {
2269 fn div_assign(&mut self, rhs: _) {
2270 #[track_caller]
2271 fn imp<T: ComplexField>(mut lhs: ColMut<'_, T>, rhs: &T) {
2272 lhs *= Scale(rhs.recip());
2273 }
2274 let lhs = self.rb_mut().column_vector_mut();
2275 let rhs = &rhs.0;
2276 imp(lhs.as_dyn_rows_mut().as_dyn_stride_mut(), rhs)
2277 }
2278 }
2279});
2280impl_op_assign!({
2281 impl<
2282 T: ComplexField,
2283 Rows: Shape,
2284 Cols: Shape,
2285 LRStride: Stride,
2286 LCStride: Stride,
2287 L: (for<'a> ReborrowMut<
2288 'a,
2289 Target = mat::Mut<'a, T, Rows, Cols, LRStride, LCStride>,
2290 >),
2291 > MulAssign<f64> for mat::generic::Mat<L>
2292 {
2293 fn mul_assign(&mut self, rhs: _) {
2294 #[track_caller]
2295 fn imp<T: ComplexField>(lhs: MatMut<'_, T>, rhs: &T) {
2296 zip!(lhs).for_each(|unzip!(x)| *x *= rhs)
2297 }
2298 let lhs = self.rb_mut();
2299 let rhs = &from_f64::<T>(*rhs);
2300 imp(lhs.as_dyn_mut().as_dyn_stride_mut(), rhs)
2301 }
2302 }
2303 impl<
2304 T: ComplexField,
2305 Rows: Shape,
2306 Cols: Shape,
2307 LRStride: Stride,
2308 LCStride: Stride,
2309 L: (for<'a> ReborrowMut<
2310 'a,
2311 Target = mat::Mut<'a, T, Rows, Cols, LRStride, LCStride>,
2312 >),
2313 > DivAssign<f64> for mat::generic::Mat<L>
2314 {
2315 fn div_assign(&mut self, rhs: _) {
2316 #[track_caller]
2317 fn imp<T: ComplexField>(mut lhs: MatMut<'_, T>, rhs: &T) {
2318 lhs *= Scale(rhs.recip());
2319 }
2320 let lhs = self.rb_mut();
2321 let rhs = &from_f64::<T>(*rhs);
2322 imp(lhs.as_dyn_mut().as_dyn_stride_mut(), rhs)
2323 }
2324 }
2325});
2326impl_op_assign!({
2327 impl<
2328 T: ComplexField,
2329 Rows: Shape,
2330 LRStride: Stride,
2331 L: (for<'a> ReborrowMut<'a, Target = col::Mut<'a, T, Rows, LRStride>>),
2332 > MulAssign<f64> for col::generic::Col<L>
2333 {
2334 fn mul_assign(&mut self, rhs: _) {
2335 #[track_caller]
2336 fn imp<T: ComplexField>(lhs: ColMut<'_, T>, rhs: &T) {
2337 zip!(lhs).for_each(|unzip!(x)| *x *= rhs)
2338 }
2339 let lhs = self.rb_mut();
2340 let rhs = &from_f64::<T>(*rhs);
2341 imp(lhs.as_dyn_rows_mut().as_dyn_stride_mut(), rhs)
2342 }
2343 }
2344 impl<
2345 T: ComplexField,
2346 Rows: Shape,
2347 LRStride: Stride,
2348 L: (for<'a> ReborrowMut<'a, Target = col::Mut<'a, T, Rows, LRStride>>),
2349 > DivAssign<f64> for col::generic::Col<L>
2350 {
2351 fn div_assign(&mut self, rhs: _) {
2352 #[track_caller]
2353 fn imp<T: ComplexField>(mut lhs: ColMut<'_, T>, rhs: &T) {
2354 lhs *= Scale(rhs.recip());
2355 }
2356 let lhs = self.rb_mut();
2357 let rhs = &from_f64::<T>(*rhs);
2358 imp(lhs.as_dyn_rows_mut().as_dyn_stride_mut(), rhs)
2359 }
2360 }
2361});
2362impl_op_assign!({
2363 impl<
2364 T: ComplexField,
2365 Cols: Shape,
2366 LCStride: Stride,
2367 L: (for<'a> ReborrowMut<'a, Target = row::Mut<'a, T, Cols, LCStride>>),
2368 > MulAssign<f64> for row::generic::Row<L>
2369 {
2370 fn mul_assign(&mut self, rhs: _) {
2371 #[track_caller]
2372 fn imp<T: ComplexField>(lhs: RowMut<'_, T>, rhs: &T) {
2373 let mut lhs = lhs.transpose_mut();
2374 lhs *= Scale::from_ref(rhs);
2375 }
2376 let lhs = self.rb_mut();
2377 let rhs = &from_f64::<T>(*rhs);
2378 imp(lhs.as_dyn_cols_mut().as_dyn_stride_mut(), rhs)
2379 }
2380 }
2381 impl<
2382 T: ComplexField,
2383 Cols: Shape,
2384 LCStride: Stride,
2385 L: (for<'a> ReborrowMut<'a, Target = row::Mut<'a, T, Cols, LCStride>>),
2386 > DivAssign<f64> for row::generic::Row<L>
2387 {
2388 fn div_assign(&mut self, rhs: _) {
2389 #[track_caller]
2390 fn imp<T: ComplexField>(mut lhs: RowMut<'_, T>, rhs: &T) {
2391 lhs *= Scale(rhs.recip());
2392 }
2393 let lhs = self.rb_mut();
2394 let rhs = &from_f64::<T>(*rhs);
2395 imp(lhs.as_dyn_cols_mut().as_dyn_stride_mut(), rhs)
2396 }
2397 }
2398});
2399impl_op_assign!({
2400 impl<
2401 T: ComplexField,
2402 Cols: Shape,
2403 LCStride: Stride,
2404 L: (for<'a> ReborrowMut<'a, Target = diag::Mut<'a, T, Cols, LCStride>>),
2405 > MulAssign<f64> for diag::generic::Diag<L>
2406 {
2407 fn mul_assign(&mut self, rhs: _) {
2408 #[track_caller]
2409 fn imp<T: ComplexField>(mut lhs: ColMut<'_, T>, rhs: &T) {
2410 lhs *= Scale::from_ref(rhs);
2411 }
2412 let lhs = self.rb_mut().column_vector_mut();
2413 let rhs = &from_f64::<T>(*rhs);
2414 imp(lhs.as_dyn_rows_mut().as_dyn_stride_mut(), rhs)
2415 }
2416 }
2417 impl<
2418 T: ComplexField,
2419 Cols: Shape,
2420 LCStride: Stride,
2421 L: (for<'a> ReborrowMut<'a, Target = diag::Mut<'a, T, Cols, LCStride>>),
2422 > DivAssign<f64> for diag::generic::Diag<L>
2423 {
2424 fn div_assign(&mut self, rhs: _) {
2425 #[track_caller]
2426 fn imp<T: ComplexField>(mut lhs: ColMut<'_, T>, rhs: &T) {
2427 lhs *= Scale(rhs.recip());
2428 }
2429 let lhs = self.rb_mut().column_vector_mut();
2430 let rhs = &from_f64::<T>(*rhs);
2431 imp(lhs.as_dyn_rows_mut().as_dyn_stride_mut(), rhs)
2432 }
2433 }
2434});
2435#[cfg(feature = "sparse")]
2436mod sparse {
2437 use super::*;
2438 use crate::internal_prelude_sp::*;
2439 use csc_numeric as csc;
2440 use csr_numeric as csr;
2441 impl_binop!({
2442 impl<
2443 I: Index,
2444 T: ComplexField,
2445 Rows: Shape,
2446 Cols: Shape,
2447 RT: (Conjugate<Canonical = T>),
2448 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Rows, Cols>>),
2449 > Mul<csr::generic::SparseRowMat<R>> for Scale<T>
2450 {
2451 type Output = SparseRowMat<I, T, Rows, Cols>;
2452
2453 fn mul(self, rhs: _) {
2454 #[track_caller]
2455 fn imp<
2456 I: Index,
2457 T: ComplexField,
2458 RT: Conjugate<Canonical = T>,
2459 >(
2460 lhs: &T,
2461 rhs: SparseRowMatRef<'_, I, RT>,
2462 ) -> SparseRowMat<I, T> {
2463 (Scale::from_ref(lhs) * rhs.transpose()).into_transpose()
2464 }
2465 let lhs = &self.0;
2466 let rhs = rhs.rb();
2467 imp(lhs, rhs.as_dyn()).into_shape(rhs.nrows(), rhs.ncols())
2468 }
2469 }
2470 impl<
2471 I: Index,
2472 T: ComplexField,
2473 Rows: Shape,
2474 Cols: Shape,
2475 RT: (Conjugate<Canonical = T>),
2476 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Rows, Cols>>),
2477 > Mul<csc::generic::SparseColMat<R>> for Scale<T>
2478 {
2479 type Output = SparseColMat<I, T, Rows, Cols>;
2480
2481 fn mul(self, rhs: _) {
2482 #[track_caller]
2483 fn imp<
2484 I: Index,
2485 T: ComplexField,
2486 RT: Conjugate<Canonical = T>,
2487 >(
2488 lhs: &T,
2489 rhs: SparseColMatRef<'_, I, RT>,
2490 ) -> SparseColMat<I, T> {
2491 with_dim!(M, rhs.nrows());
2492 with_dim!(N, rhs.ncols());
2493 let rhs = rhs.as_shape(M, N);
2494 let symbolic = rhs.symbolic().to_owned().unwrap();
2495 let mut val = alloc::vec::Vec::new();
2496 val.resize(symbolic.row_idx().len(), zero());
2497 for j in rhs.ncols().indices() {
2498 for (val, rhs) in iter::zip(
2499 &mut val[symbolic.col_range(j)],
2500 rhs.val_of_col(j),
2501 ) {
2502 *val = lhs * Conj::apply(rhs);
2503 }
2504 }
2505 SparseColMat::new(symbolic, val).into_shape(*M, *N)
2506 }
2507 let lhs = &self.0;
2508 let rhs = rhs.rb();
2509 imp(lhs, rhs.as_dyn()).into_shape(rhs.nrows(), rhs.ncols())
2510 }
2511 }
2512 });
2513 impl_binop!({
2514 impl<
2515 I: Index,
2516 T: ComplexField,
2517 Rows: Shape,
2518 Cols: Shape,
2519 LT: (Conjugate<Canonical = T>),
2520 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Cols>>),
2521 > Mul<Scale<T>> for csr::generic::SparseRowMat<L>
2522 {
2523 type Output = SparseRowMat<I, T, Rows, Cols>;
2524
2525 fn mul(self, rhs: _) {
2526 #[track_caller]
2527 fn imp<
2528 I: Index,
2529 T: ComplexField,
2530 LT: Conjugate<Canonical = T>,
2531 >(
2532 rhs: &T,
2533 lhs: SparseRowMatRef<'_, I, LT>,
2534 ) -> SparseRowMat<I, T> {
2535 (lhs.transpose() * Scale::from_ref(rhs)).into_transpose()
2536 }
2537 let rhs = &rhs.0;
2538 let lhs = self.rb();
2539 imp(rhs, lhs.as_dyn()).into_shape(lhs.nrows(), lhs.ncols())
2540 }
2541 }
2542 impl<
2543 I: Index,
2544 T: ComplexField,
2545 Rows: Shape,
2546 Cols: Shape,
2547 LT: (Conjugate<Canonical = T>),
2548 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Cols>>),
2549 > Mul<Scale<T>> for csc::generic::SparseColMat<L>
2550 {
2551 type Output = SparseColMat<I, T, Rows, Cols>;
2552
2553 fn mul(self, rhs: _) {
2554 #[track_caller]
2555 fn imp<
2556 I: Index,
2557 T: ComplexField,
2558 LT: Conjugate<Canonical = T>,
2559 >(
2560 rhs: &T,
2561 lhs: SparseColMatRef<'_, I, LT>,
2562 ) -> SparseColMat<I, T> {
2563 with_dim!(M, lhs.nrows());
2564 with_dim!(N, lhs.ncols());
2565 let lhs = lhs.as_shape(M, N);
2566 let symbolic = lhs.symbolic().to_owned().unwrap();
2567 let mut val = alloc::vec::Vec::new();
2568 val.resize(symbolic.row_idx().len(), zero());
2569 for j in lhs.ncols().indices() {
2570 for (val, lhs) in iter::zip(
2571 &mut val[symbolic.col_range(j)],
2572 lhs.val_of_col(j),
2573 ) {
2574 *val = Conj::apply(lhs) * rhs;
2575 }
2576 }
2577 SparseColMat::new(symbolic, val).into_shape(*M, *N)
2578 }
2579 let rhs = &rhs.0;
2580 let lhs = self.rb();
2581 imp(rhs, lhs.as_dyn()).into_shape(lhs.nrows(), lhs.ncols())
2582 }
2583 }
2584 impl<
2585 I: Index,
2586 T: ComplexField,
2587 Rows: Shape,
2588 Cols: Shape,
2589 LT: (Conjugate<Canonical = T>),
2590 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Cols>>),
2591 > Div<Scale<T>> for csr::generic::SparseRowMat<L>
2592 {
2593 type Output = SparseRowMat<I, T, Rows, Cols>;
2594
2595 fn div(self, rhs: _) {
2596 #[track_caller]
2597 fn imp<
2598 I: Index,
2599 T: ComplexField,
2600 LT: Conjugate<Canonical = T>,
2601 >(
2602 rhs: &T,
2603 lhs: SparseRowMatRef<'_, I, LT>,
2604 ) -> SparseRowMat<I, T> {
2605 lhs * Scale(rhs.recip())
2606 }
2607 let rhs = &rhs.0;
2608 let lhs = self.rb();
2609 imp(rhs, lhs.as_dyn()).into_shape(lhs.nrows(), lhs.ncols())
2610 }
2611 }
2612 impl<
2613 I: Index,
2614 T: ComplexField,
2615 Rows: Shape,
2616 Cols: Shape,
2617 LT: (Conjugate<Canonical = T>),
2618 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Cols>>),
2619 > Div<Scale<T>> for csc::generic::SparseColMat<L>
2620 {
2621 type Output = SparseColMat<I, T, Rows, Cols>;
2622
2623 fn div(self, rhs: _) {
2624 #[track_caller]
2625 fn imp<
2626 I: Index,
2627 T: ComplexField,
2628 LT: Conjugate<Canonical = T>,
2629 >(
2630 rhs: &T,
2631 lhs: SparseColMatRef<'_, I, LT>,
2632 ) -> SparseColMat<I, T> {
2633 lhs * Scale(rhs.recip())
2634 }
2635 let rhs = &rhs.0;
2636 let lhs = self.rb();
2637 imp(rhs, lhs.as_dyn()).into_shape(lhs.nrows(), lhs.ncols())
2638 }
2639 }
2640 });
2641 impl_op_assign!({
2642 impl<
2643 I: Index,
2644 T: ComplexField,
2645 Rows: Shape,
2646 Cols: Shape,
2647 L: (for<'a> ReborrowMut<'a, Target = csr::Mut<'a, I, T, Rows, Cols>>),
2648 > MulAssign<Scale<T>> for csr::generic::SparseRowMat<L>
2649 {
2650 fn mul_assign(&mut self, rhs: _) {
2651 #[track_caller]
2652 fn imp<I: Index, T: ComplexField>(
2653 rhs: &T,
2654 lhs: SparseRowMatMut<'_, I, T>,
2655 ) {
2656 let mut lhs = lhs.transpose_mut();
2657 lhs *= Scale::from_ref(rhs);
2658 }
2659 let rhs = &rhs.0;
2660 let lhs = self.rb_mut();
2661 imp(rhs, lhs.as_dyn_mut())
2662 }
2663 }
2664 impl<
2665 I: Index,
2666 T: ComplexField,
2667 Rows: Shape,
2668 Cols: Shape,
2669 L: (for<'a> ReborrowMut<'a, Target = csc::Mut<'a, I, T, Rows, Cols>>),
2670 > MulAssign<Scale<T>> for csc::generic::SparseColMat<L>
2671 {
2672 fn mul_assign(&mut self, rhs: _) {
2673 #[track_caller]
2674 fn imp<I: Index, T: ComplexField>(
2675 rhs: &T,
2676 lhs: SparseColMatMut<'_, I, T>,
2677 ) {
2678 with_dim!(M, lhs.nrows());
2679 with_dim!(N, lhs.ncols());
2680 let mut lhs = lhs.as_shape_mut(M, N);
2681 for j in lhs.ncols().indices() {
2682 for val in lhs.rb_mut().val_of_col_mut(j) {
2683 *val *= rhs;
2684 }
2685 }
2686 }
2687 let rhs = &rhs.0;
2688 let lhs = self.rb_mut();
2689 imp(rhs, lhs.as_dyn_mut())
2690 }
2691 }
2692 impl<
2693 I: Index,
2694 T: ComplexField,
2695 Rows: Shape,
2696 Cols: Shape,
2697 L: (for<'a> ReborrowMut<'a, Target = csr::Mut<'a, I, T, Rows, Cols>>),
2698 > DivAssign<Scale<T>> for csr::generic::SparseRowMat<L>
2699 {
2700 fn div_assign(&mut self, rhs: _) {
2701 #[track_caller]
2702 fn imp<I: Index, T: ComplexField>(
2703 rhs: &T,
2704 lhs: SparseRowMatMut<'_, I, T>,
2705 ) {
2706 let mut lhs = lhs.transpose_mut();
2707 lhs /= Scale::from_ref(rhs);
2708 }
2709 let rhs = &rhs.0;
2710 let lhs = self.rb_mut();
2711 imp(rhs, lhs.as_dyn_mut())
2712 }
2713 }
2714 impl<
2715 I: Index,
2716 T: ComplexField,
2717 Rows: Shape,
2718 Cols: Shape,
2719 L: (for<'a> ReborrowMut<'a, Target = csc::Mut<'a, I, T, Rows, Cols>>),
2720 > DivAssign<Scale<T>> for csc::generic::SparseColMat<L>
2721 {
2722 fn div_assign(&mut self, rhs: _) {
2723 #[track_caller]
2724 fn imp<I: Index, T: ComplexField>(
2725 rhs: &T,
2726 lhs: SparseColMatMut<'_, I, T>,
2727 ) {
2728 let mut lhs = lhs.transpose_mut();
2729 lhs *= Scale(rhs.recip());
2730 }
2731 let rhs = &rhs.0;
2732 let lhs = self.rb_mut();
2733 imp(rhs, lhs.as_dyn_mut())
2734 }
2735 }
2736 });
2737 impl_binop!({
2738 impl<
2739 I: Index,
2740 T: ComplexField,
2741 Rows: Shape,
2742 Cols: Shape,
2743 RT: (Conjugate<Canonical = T>),
2744 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Rows, Cols>>),
2745 > Mul<csr::generic::SparseRowMat<R>> for f64
2746 {
2747 type Output = SparseRowMat<I, T, Rows, Cols>;
2748
2749 fn mul(self, rhs: _) {
2750 #[track_caller]
2751 fn imp<
2752 I: Index,
2753 T: ComplexField,
2754 RT: Conjugate<Canonical = T>,
2755 >(
2756 lhs: &T,
2757 rhs: SparseRowMatRef<'_, I, RT>,
2758 ) -> SparseRowMat<I, T> {
2759 (Scale::from_ref(lhs) * rhs.transpose()).into_transpose()
2760 }
2761 let lhs = &from_f64::<T>(*self);
2762 let rhs = rhs.rb();
2763 imp(lhs, rhs.as_dyn()).into_shape(rhs.nrows(), rhs.ncols())
2764 }
2765 }
2766 impl<
2767 I: Index,
2768 T: ComplexField,
2769 Rows: Shape,
2770 Cols: Shape,
2771 RT: (Conjugate<Canonical = T>),
2772 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Rows, Cols>>),
2773 > Mul<csc::generic::SparseColMat<R>> for f64
2774 {
2775 type Output = SparseColMat<I, T, Rows, Cols>;
2776
2777 fn mul(self, rhs: _) {
2778 #[track_caller]
2779 fn imp<
2780 I: Index,
2781 T: ComplexField,
2782 RT: Conjugate<Canonical = T>,
2783 >(
2784 lhs: &T,
2785 rhs: SparseColMatRef<'_, I, RT>,
2786 ) -> SparseColMat<I, T> {
2787 Scale::from_ref(lhs) * rhs
2788 }
2789 let lhs = &from_f64::<T>(*self);
2790 let rhs = rhs.rb();
2791 imp(lhs, rhs.as_dyn()).into_shape(rhs.nrows(), rhs.ncols())
2792 }
2793 }
2794 });
2795 impl_binop!({
2796 impl<
2797 I: Index,
2798 T: ComplexField,
2799 Rows: Shape,
2800 Cols: Shape,
2801 LT: (Conjugate<Canonical = T>),
2802 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Cols>>),
2803 > Mul<f64> for csr::generic::SparseRowMat<L>
2804 {
2805 type Output = SparseRowMat<I, T, Rows, Cols>;
2806
2807 fn mul(self, rhs: _) {
2808 #[track_caller]
2809 fn imp<
2810 I: Index,
2811 T: ComplexField,
2812 LT: Conjugate<Canonical = T>,
2813 >(
2814 rhs: &T,
2815 lhs: SparseRowMatRef<'_, I, LT>,
2816 ) -> SparseRowMat<I, T> {
2817 (lhs.transpose() * Scale::from_ref(rhs)).into_transpose()
2818 }
2819 let rhs = &from_f64::<T>(*rhs);
2820 let lhs = self.rb();
2821 imp(rhs, lhs.as_dyn()).into_shape(lhs.nrows(), lhs.ncols())
2822 }
2823 }
2824 impl<
2825 I: Index,
2826 T: ComplexField,
2827 Rows: Shape,
2828 Cols: Shape,
2829 LT: (Conjugate<Canonical = T>),
2830 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Cols>>),
2831 > Mul<f64> for csc::generic::SparseColMat<L>
2832 {
2833 type Output = SparseColMat<I, T, Rows, Cols>;
2834
2835 fn mul(self, rhs: _) {
2836 #[track_caller]
2837 fn imp<
2838 I: Index,
2839 T: ComplexField,
2840 LT: Conjugate<Canonical = T>,
2841 >(
2842 rhs: &T,
2843 lhs: SparseColMatRef<'_, I, LT>,
2844 ) -> SparseColMat<I, T> {
2845 lhs * Scale::from_ref(rhs)
2846 }
2847 let rhs = &from_f64::<T>(*rhs);
2848 let lhs = self.rb();
2849 imp(rhs, lhs.as_dyn()).into_shape(lhs.nrows(), lhs.ncols())
2850 }
2851 }
2852 impl<
2853 I: Index,
2854 T: ComplexField,
2855 Rows: Shape,
2856 Cols: Shape,
2857 LT: (Conjugate<Canonical = T>),
2858 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Cols>>),
2859 > Div<f64> for csr::generic::SparseRowMat<L>
2860 {
2861 type Output = SparseRowMat<I, T, Rows, Cols>;
2862
2863 fn div(self, rhs: _) {
2864 #[track_caller]
2865 fn imp<
2866 I: Index,
2867 T: ComplexField,
2868 LT: Conjugate<Canonical = T>,
2869 >(
2870 rhs: &T,
2871 lhs: SparseRowMatRef<'_, I, LT>,
2872 ) -> SparseRowMat<I, T> {
2873 lhs * Scale(rhs.recip())
2874 }
2875 let rhs = &from_f64::<T>(*rhs);
2876 let lhs = self.rb();
2877 imp(rhs, lhs.as_dyn()).into_shape(lhs.nrows(), lhs.ncols())
2878 }
2879 }
2880 impl<
2881 I: Index,
2882 T: ComplexField,
2883 Rows: Shape,
2884 Cols: Shape,
2885 LT: (Conjugate<Canonical = T>),
2886 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Cols>>),
2887 > Div<f64> for csc::generic::SparseColMat<L>
2888 {
2889 type Output = SparseColMat<I, T, Rows, Cols>;
2890
2891 fn div(self, rhs: _) {
2892 #[track_caller]
2893 fn imp<
2894 I: Index,
2895 T: ComplexField,
2896 LT: Conjugate<Canonical = T>,
2897 >(
2898 rhs: &T,
2899 lhs: SparseColMatRef<'_, I, LT>,
2900 ) -> SparseColMat<I, T> {
2901 lhs * Scale(rhs.recip())
2902 }
2903 let rhs = &from_f64::<T>(*rhs);
2904 let lhs = self.rb();
2905 imp(rhs, lhs.as_dyn()).into_shape(lhs.nrows(), lhs.ncols())
2906 }
2907 }
2908 });
2909 impl_op_assign!({
2910 impl<
2911 I: Index,
2912 T: ComplexField,
2913 Rows: Shape,
2914 Cols: Shape,
2915 L: (for<'a> ReborrowMut<'a, Target = csr::Mut<'a, I, T, Rows, Cols>>),
2916 > MulAssign<f64> for csr::generic::SparseRowMat<L>
2917 {
2918 fn mul_assign(&mut self, rhs: _) {
2919 #[track_caller]
2920 fn imp<I: Index, T: ComplexField>(
2921 rhs: &T,
2922 lhs: SparseRowMatMut<'_, I, T>,
2923 ) {
2924 let mut lhs = lhs.transpose_mut();
2925 lhs *= Scale::from_ref(rhs);
2926 }
2927 let rhs = &from_f64::<T>(*rhs);
2928 let lhs = self.rb_mut();
2929 imp(rhs, lhs.as_dyn_mut())
2930 }
2931 }
2932 impl<
2933 I: Index,
2934 T: ComplexField,
2935 Rows: Shape,
2936 Cols: Shape,
2937 L: (for<'a> ReborrowMut<'a, Target = csc::Mut<'a, I, T, Rows, Cols>>),
2938 > MulAssign<f64> for csc::generic::SparseColMat<L>
2939 {
2940 fn mul_assign(&mut self, rhs: _) {
2941 #[track_caller]
2942 fn imp<I: Index, T: ComplexField>(
2943 rhs: &T,
2944 lhs: SparseColMatMut<'_, I, T>,
2945 ) {
2946 *&mut { lhs } *= Scale::from_ref(rhs)
2947 }
2948 let rhs = &from_f64::<T>(*rhs);
2949 let lhs = self.rb_mut();
2950 imp(rhs, lhs.as_dyn_mut())
2951 }
2952 }
2953 impl<
2954 I: Index,
2955 T: ComplexField,
2956 Rows: Shape,
2957 Cols: Shape,
2958 L: (for<'a> ReborrowMut<'a, Target = csr::Mut<'a, I, T, Rows, Cols>>),
2959 > DivAssign<f64> for csr::generic::SparseRowMat<L>
2960 {
2961 fn div_assign(&mut self, rhs: _) {
2962 #[track_caller]
2963 fn imp<I: Index, T: ComplexField>(
2964 rhs: &T,
2965 lhs: SparseRowMatMut<'_, I, T>,
2966 ) {
2967 let mut lhs = lhs.transpose_mut();
2968 lhs /= Scale::from_ref(rhs);
2969 }
2970 let rhs = &from_f64::<T>(*rhs);
2971 let lhs = self.rb_mut();
2972 imp(rhs, lhs.as_dyn_mut())
2973 }
2974 }
2975 impl<
2976 I: Index,
2977 T: ComplexField,
2978 Rows: Shape,
2979 Cols: Shape,
2980 L: (for<'a> ReborrowMut<'a, Target = csc::Mut<'a, I, T, Rows, Cols>>),
2981 > DivAssign<f64> for csc::generic::SparseColMat<L>
2982 {
2983 fn div_assign(&mut self, rhs: _) {
2984 #[track_caller]
2985 fn imp<I: Index, T: ComplexField>(
2986 rhs: &T,
2987 lhs: SparseColMatMut<'_, I, T>,
2988 ) {
2989 let mut lhs = lhs.transpose_mut();
2990 lhs *= Scale(rhs.recip());
2991 }
2992 let rhs = &from_f64::<T>(*rhs);
2993 let lhs = self.rb_mut();
2994 imp(rhs, lhs.as_dyn_mut())
2995 }
2996 }
2997 });
2998 impl_op!({
2999 impl<
3000 I: Index,
3001 T: Conjugate,
3002 Rows: Shape,
3003 Cols: Shape,
3004 Inner: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, T, Rows, Cols>>),
3005 > Neg for csr::generic::SparseRowMat<Inner>
3006 {
3007 type Output = SparseRowMat<I, T::Canonical, Rows, Cols>;
3008
3009 fn neg(self) {
3010 self.rb().transpose().neg().into_transpose()
3011 }
3012 }
3013 impl<
3014 I: Index,
3015 T: Conjugate,
3016 Rows: Shape,
3017 Cols: Shape,
3018 Inner: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, T, Rows, Cols>>),
3019 > Neg for csc::generic::SparseColMat<Inner>
3020 {
3021 type Output = SparseColMat<I, T::Canonical, Rows, Cols>;
3022
3023 fn neg(self) {
3024 #[track_caller]
3025 fn imp<I: Index, T: Conjugate>(
3026 A: SparseColMatRef<'_, I, T>,
3027 ) -> SparseColMat<I, T::Canonical> {
3028 with_dim!(M, A.nrows());
3029 with_dim!(N, A.ncols());
3030 let A = A.as_shape(M, N);
3031 let symbolic = A.symbolic().to_owned().unwrap();
3032 let mut val = alloc::vec::Vec::new();
3033 val.resize(symbolic.row_idx().len(), zero());
3034 for j in A.ncols().indices() {
3035 for (val, lhs) in iter::zip(
3036 &mut val[symbolic.col_range(j)],
3037 A.val_of_col(j),
3038 ) {
3039 *val = -Conj::apply(lhs);
3040 }
3041 }
3042 SparseColMat::new(symbolic, val).into_shape(*M, *N)
3043 }
3044 let A = self.rb();
3045 imp(A.as_dyn()).into_shape(A.nrows(), A.ncols())
3046 }
3047 }
3048 });
3049 impl_op_assign!({
3050 impl<
3051 I: Index,
3052 T: ComplexField,
3053 Rows: Shape,
3054 Cols: Shape,
3055 RT: (Conjugate<Canonical = T>),
3056 L: (for<'a> ReborrowMut<'a, Target = csc::Mut<'a, I, T, Rows, Cols>>),
3057 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Rows, Cols>>),
3058 > AddAssign<csc::generic::SparseColMat<R>> for csc::generic::SparseColMat<L>
3059 {
3060 fn add_assign(&mut self, rhs: _) {
3061 #[track_caller]
3062 fn imp<
3063 I: Index,
3064 T: ComplexField,
3065 RT: Conjugate<Canonical = T>,
3066 >(
3067 lhs: SparseColMatMut<'_, I, T>,
3068 rhs: SparseColMatRef<'_, I, RT>,
3069 ) {
3070 crate::sparse::ops::binary_op_assign_into(
3071 lhs,
3072 rhs,
3073 add_assign_fn::<T, RT>,
3074 )
3075 }
3076 let lhs = self.rb_mut();
3077 let rhs = rhs.rb();
3078 imp(lhs.as_dyn_mut(), rhs.as_dyn())
3079 }
3080 }
3081 impl<
3082 I: Index,
3083 T: ComplexField,
3084 Rows: Shape,
3085 Cols: Shape,
3086 RT: (Conjugate<Canonical = T>),
3087 L: (for<'a> ReborrowMut<'a, Target = csc::Mut<'a, I, T, Rows, Cols>>),
3088 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Rows, Cols>>),
3089 > SubAssign<csc::generic::SparseColMat<R>> for csc::generic::SparseColMat<L>
3090 {
3091 fn sub_assign(&mut self, rhs: _) {
3092 #[track_caller]
3093 fn imp<
3094 I: Index,
3095 T: ComplexField,
3096 RT: Conjugate<Canonical = T>,
3097 >(
3098 lhs: SparseColMatMut<'_, I, T>,
3099 rhs: SparseColMatRef<'_, I, RT>,
3100 ) {
3101 crate::sparse::ops::binary_op_assign_into(
3102 lhs,
3103 rhs,
3104 sub_assign_fn::<T, RT>,
3105 )
3106 }
3107 let lhs = self.rb_mut();
3108 let rhs = rhs.rb();
3109 imp(lhs.as_dyn_mut(), rhs.as_dyn())
3110 }
3111 }
3112 });
3113 impl_op_assign!({
3114 impl<
3115 I: Index,
3116 T: ComplexField,
3117 Rows: Shape,
3118 Cols: Shape,
3119 RT: (Conjugate<Canonical = T>),
3120 L: (for<'a> ReborrowMut<'a, Target = csr::Mut<'a, I, T, Rows, Cols>>),
3121 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Rows, Cols>>),
3122 > AddAssign<csr::generic::SparseRowMat<R>> for csr::generic::SparseRowMat<L>
3123 {
3124 fn add_assign(&mut self, rhs: _) {
3125 #[track_caller]
3126 fn imp<
3127 I: Index,
3128 T: ComplexField,
3129 RT: Conjugate<Canonical = T>,
3130 >(
3131 lhs: SparseRowMatMut<'_, I, T>,
3132 rhs: SparseRowMatRef<'_, I, RT>,
3133 ) {
3134 crate::sparse::ops::binary_op_assign_into(
3135 lhs.transpose_mut(),
3136 rhs.transpose(),
3137 add_assign_fn::<T, RT>,
3138 )
3139 }
3140 let lhs = self.rb_mut();
3141 let rhs = rhs.rb();
3142 imp(lhs.as_dyn_mut(), rhs.as_dyn())
3143 }
3144 }
3145 impl<
3146 I: Index,
3147 T: ComplexField,
3148 Rows: Shape,
3149 Cols: Shape,
3150 RT: (Conjugate<Canonical = T>),
3151 L: (for<'a> ReborrowMut<'a, Target = csr::Mut<'a, I, T, Rows, Cols>>),
3152 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Rows, Cols>>),
3153 > SubAssign<csr::generic::SparseRowMat<R>> for csr::generic::SparseRowMat<L>
3154 {
3155 fn sub_assign(&mut self, rhs: _) {
3156 #[track_caller]
3157 fn imp<
3158 I: Index,
3159 T: ComplexField,
3160 RT: Conjugate<Canonical = T>,
3161 >(
3162 lhs: SparseRowMatMut<'_, I, T>,
3163 rhs: SparseRowMatRef<'_, I, RT>,
3164 ) {
3165 crate::sparse::ops::binary_op_assign_into(
3166 lhs.transpose_mut(),
3167 rhs.transpose(),
3168 sub_assign_fn::<T, RT>,
3169 )
3170 }
3171 let lhs = self.rb_mut();
3172 let rhs = rhs.rb();
3173 imp(lhs.as_dyn_mut(), rhs.as_dyn())
3174 }
3175 }
3176 });
3177 impl_binop!({
3178 impl<
3179 I: Index,
3180 T: ComplexField,
3181 Rows: Shape,
3182 Cols: Shape,
3183 LT: (Conjugate<Canonical = T>),
3184 RT: (Conjugate<Canonical = T>),
3185 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Cols>>),
3186 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Rows, Cols>>),
3187 > Add<csc::generic::SparseColMat<R>> for csc::generic::SparseColMat<L>
3188 {
3189 type Output = SparseColMat<I, T, Rows, Cols>;
3190
3191 fn add(self, rhs: _) {
3192 #[track_caller]
3193 fn imp<
3194 I: Index,
3195 T: ComplexField,
3196 LT: Conjugate<Canonical = T>,
3197 RT: Conjugate<Canonical = T>,
3198 >(
3199 lhs: SparseColMatRef<'_, I, LT>,
3200 rhs: SparseColMatRef<'_, I, RT>,
3201 ) -> SparseColMat<I, T> {
3202 crate::sparse::ops::binary_op(lhs, rhs, add_fn::<T, LT, RT>)
3203 .unwrap()
3204 }
3205 let lhs = self.rb();
3206 let rhs = rhs.rb();
3207 let (nrows, ncols) = lhs.shape();
3208 imp(lhs.as_dyn(), rhs.as_dyn()).into_shape(nrows, ncols)
3209 }
3210 }
3211 impl<
3212 I: Index,
3213 T: ComplexField,
3214 Rows: Shape,
3215 Cols: Shape,
3216 LT: (Conjugate<Canonical = T>),
3217 RT: (Conjugate<Canonical = T>),
3218 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Cols>>),
3219 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Rows, Cols>>),
3220 > Sub<csc::generic::SparseColMat<R>> for csc::generic::SparseColMat<L>
3221 {
3222 type Output = SparseColMat<I, T, Rows, Cols>;
3223
3224 fn sub(self, rhs: _) {
3225 #[track_caller]
3226 fn imp<
3227 I: Index,
3228 T: ComplexField,
3229 LT: Conjugate<Canonical = T>,
3230 RT: Conjugate<Canonical = T>,
3231 >(
3232 lhs: SparseColMatRef<'_, I, LT>,
3233 rhs: SparseColMatRef<'_, I, RT>,
3234 ) -> SparseColMat<I, T> {
3235 crate::sparse::ops::binary_op(lhs, rhs, sub_fn::<T, LT, RT>)
3236 .unwrap()
3237 }
3238 let lhs = self.rb();
3239 let rhs = rhs.rb();
3240 let (nrows, ncols) = lhs.shape();
3241 imp(lhs.as_dyn(), rhs.as_dyn()).into_shape(nrows, ncols)
3242 }
3243 }
3244 });
3245 impl_binop!({
3246 impl<
3247 I: Index,
3248 T: ComplexField,
3249 Rows: Shape,
3250 Cols: Shape,
3251 LT: (Conjugate<Canonical = T>),
3252 RT: (Conjugate<Canonical = T>),
3253 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Cols>>),
3254 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Rows, Cols>>),
3255 > Add<csr::generic::SparseRowMat<R>> for csr::generic::SparseRowMat<L>
3256 {
3257 type Output = SparseRowMat<I, T, Rows, Cols>;
3258
3259 fn add(self, rhs: _) {
3260 #[track_caller]
3261 fn imp<
3262 I: Index,
3263 T: ComplexField,
3264 LT: Conjugate<Canonical = T>,
3265 RT: Conjugate<Canonical = T>,
3266 >(
3267 lhs: SparseRowMatRef<'_, I, LT>,
3268 rhs: SparseRowMatRef<'_, I, RT>,
3269 ) -> SparseRowMat<I, T> {
3270 crate::sparse::ops::binary_op(
3271 lhs.transpose(),
3272 rhs.transpose(),
3273 add_fn::<T, LT, RT>,
3274 )
3275 .unwrap()
3276 .into_transpose()
3277 }
3278 let lhs = self.rb();
3279 let rhs = rhs.rb();
3280 let (nrows, ncols) = lhs.shape();
3281 imp(lhs.as_dyn(), rhs.as_dyn()).into_shape(nrows, ncols)
3282 }
3283 }
3284 impl<
3285 I: Index,
3286 T: ComplexField,
3287 Rows: Shape,
3288 Cols: Shape,
3289 LT: (Conjugate<Canonical = T>),
3290 RT: (Conjugate<Canonical = T>),
3291 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Cols>>),
3292 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Rows, Cols>>),
3293 > Sub<csr::generic::SparseRowMat<R>> for csr::generic::SparseRowMat<L>
3294 {
3295 type Output = SparseRowMat<I, T, Rows, Cols>;
3296
3297 fn sub(self, rhs: _) {
3298 #[track_caller]
3299 fn imp<
3300 I: Index,
3301 T: ComplexField,
3302 LT: Conjugate<Canonical = T>,
3303 RT: Conjugate<Canonical = T>,
3304 >(
3305 lhs: SparseRowMatRef<'_, I, LT>,
3306 rhs: SparseRowMatRef<'_, I, RT>,
3307 ) -> SparseRowMat<I, T> {
3308 crate::sparse::ops::binary_op(
3309 lhs.transpose(),
3310 rhs.transpose(),
3311 sub_fn::<T, LT, RT>,
3312 )
3313 .unwrap()
3314 .into_transpose()
3315 }
3316 let lhs = self.rb();
3317 let rhs = rhs.rb();
3318 let (nrows, ncols) = lhs.shape();
3319 imp(lhs.as_dyn(), rhs.as_dyn()).into_shape(nrows, ncols)
3320 }
3321 }
3322 });
3323 impl_binop!({
3324 impl<
3325 I: Index,
3326 T: ComplexField,
3327 Rows: Shape,
3328 Cols: Shape,
3329 Depth: Shape,
3330 LT: (Conjugate<Canonical = T>),
3331 RT: (Conjugate<Canonical = T>),
3332 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Depth>>),
3333 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Depth, Cols>>),
3334 > Mul<csc::generic::SparseColMat<R>> for csc::generic::SparseColMat<L>
3335 {
3336 type Output = SparseColMat<I, T, Rows, Cols>;
3337
3338 fn mul(self, rhs: _) {
3339 #[track_caller]
3340 fn imp<
3341 I: Index,
3342 T: ComplexField,
3343 LT: Conjugate<Canonical = T>,
3344 RT: Conjugate<Canonical = T>,
3345 >(
3346 lhs: SparseColMatRef<'_, I, LT>,
3347 rhs: SparseColMatRef<'_, I, RT>,
3348 ) -> SparseColMat<I, T> {
3349 let nrows = lhs.nrows();
3350 let ncols = rhs.ncols();
3351 linalg_sp::matmul::sparse_sparse_matmul(
3352 lhs,
3353 rhs,
3354 one::<T>(),
3355 crate::get_global_parallelism(),
3356 )
3357 .unwrap()
3358 .into_shape(nrows, ncols)
3359 }
3360 let lhs = self.rb();
3361 let rhs = rhs.rb();
3362 let nrows = lhs.nrows();
3363 let ncols = rhs.ncols();
3364 imp(lhs.as_dyn(), rhs.as_dyn()).into_shape(nrows, ncols)
3365 }
3366 }
3367 impl<
3368 I: Index,
3369 T: ComplexField,
3370 Rows: Shape,
3371 Cols: Shape,
3372 Depth: Shape,
3373 LT: (Conjugate<Canonical = T>),
3374 RT: (Conjugate<Canonical = T>),
3375 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Depth>>),
3376 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Depth, Cols>>),
3377 > Mul<csr::generic::SparseRowMat<R>> for csr::generic::SparseRowMat<L>
3378 {
3379 type Output = SparseRowMat<I, T, Rows, Cols>;
3380
3381 fn mul(self, rhs: _) {
3382 #[track_caller]
3383 fn imp<
3384 I: Index,
3385 T: ComplexField,
3386 LT: Conjugate<Canonical = T>,
3387 RT: Conjugate<Canonical = T>,
3388 >(
3389 lhs: SparseRowMatRef<'_, I, LT>,
3390 rhs: SparseRowMatRef<'_, I, RT>,
3391 ) -> SparseRowMat<I, T> {
3392 (rhs.transpose() * lhs.transpose()).into_transpose()
3393 }
3394 let lhs = self.rb();
3395 let rhs = rhs.rb();
3396 let nrows = lhs.nrows();
3397 let ncols = rhs.ncols();
3398 imp(lhs.as_dyn(), rhs.as_dyn()).into_shape(nrows, ncols)
3399 }
3400 }
3401 });
3402 impl_binop!({
3403 impl<
3404 I: Index,
3405 T: ComplexField,
3406 Rows: Shape,
3407 Cols: Shape,
3408 Depth: Shape,
3409 LT: (Conjugate<Canonical = T>),
3410 RT: (Conjugate<Canonical = T>),
3411 RRStride: Stride,
3412 RCStride: Stride,
3413 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Depth>>),
3414 R: (for<'a> Reborrow<
3415 'a,
3416 Target = mat::Ref<'a, RT, Depth, Cols, RRStride, RCStride>,
3417 >),
3418 > Mul<mat::generic::Mat<R>> for csc::generic::SparseColMat<L>
3419 {
3420 type Output = Mat<T, Rows, Cols>;
3421
3422 fn mul(self, rhs: _) {
3423 #[track_caller]
3424 fn imp<
3425 I: Index,
3426 T: ComplexField,
3427 LT: Conjugate<Canonical = T>,
3428 RT: Conjugate<Canonical = T>,
3429 >(
3430 lhs: SparseColMatRef<'_, I, LT>,
3431 rhs: MatRef<'_, RT>,
3432 ) -> Mat<T> {
3433 let nrows = lhs.nrows();
3434 let ncols = rhs.ncols();
3435 let mut out = Mat::zeros(nrows, ncols);
3436 linalg_sp::matmul::sparse_dense_matmul(
3437 out.rb_mut(),
3438 Accum::Add,
3439 lhs,
3440 rhs,
3441 T::one_impl(),
3442 crate::get_global_parallelism(),
3443 );
3444 out
3445 }
3446 let lhs = self.rb();
3447 let rhs = rhs.rb();
3448 let nrows = lhs.nrows();
3449 let ncols = rhs.ncols();
3450 imp(lhs.as_dyn(), rhs.as_dyn().as_dyn_stride())
3451 .into_shape(nrows, ncols)
3452 }
3453 }
3454 impl<
3455 I: Index,
3456 T: ComplexField,
3457 Rows: Shape,
3458 Cols: Shape,
3459 Depth: Shape,
3460 LT: (Conjugate<Canonical = T>),
3461 RT: (Conjugate<Canonical = T>),
3462 RRStride: Stride,
3463 RCStride: Stride,
3464 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Depth>>),
3465 R: (for<'a> Reborrow<
3466 'a,
3467 Target = mat::Ref<'a, RT, Depth, Cols, RRStride, RCStride>,
3468 >),
3469 > Mul<mat::generic::Mat<R>> for csr::generic::SparseRowMat<L>
3470 {
3471 type Output = Mat<T, Rows, Cols>;
3472
3473 fn mul(self, rhs: _) {
3474 #[track_caller]
3475 fn imp<
3476 I: Index,
3477 T: ComplexField,
3478 LT: Conjugate<Canonical = T>,
3479 RT: Conjugate<Canonical = T>,
3480 >(
3481 lhs: SparseRowMatRef<'_, I, LT>,
3482 rhs: MatRef<'_, RT>,
3483 ) -> Mat<T> {
3484 let nrows = lhs.nrows();
3485 let ncols = rhs.ncols();
3486 let mut out = Mat::zeros(nrows, ncols);
3487 linalg_sp::matmul::dense_sparse_matmul(
3488 out.rb_mut().transpose_mut(),
3489 Accum::Add,
3490 rhs.transpose(),
3491 lhs.transpose(),
3492 T::one_impl(),
3493 crate::get_global_parallelism(),
3494 );
3495 out
3496 }
3497 let lhs = self.rb();
3498 let rhs = rhs.rb();
3499 let nrows = lhs.nrows();
3500 let ncols = rhs.ncols();
3501 imp(lhs.as_dyn(), rhs.as_dyn().as_dyn_stride())
3502 .into_shape(nrows, ncols)
3503 }
3504 }
3505 });
3506 impl_binop!({
3507 impl<
3508 I: Index,
3509 T: ComplexField,
3510 Rows: Shape,
3511 Depth: Shape,
3512 LT: (Conjugate<Canonical = T>),
3513 RT: (Conjugate<Canonical = T>),
3514 RRStride: Stride,
3515 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Depth>>),
3516 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Depth, RRStride>>),
3517 > Mul<col::generic::Col<R>> for csc::generic::SparseColMat<L>
3518 {
3519 type Output = Col<T, Rows>;
3520
3521 fn mul(self, rhs: _) {
3522 #[track_caller]
3523 fn imp<
3524 I: Index,
3525 T: ComplexField,
3526 LT: Conjugate<Canonical = T>,
3527 RT: Conjugate<Canonical = T>,
3528 >(
3529 lhs: SparseColMatRef<'_, I, LT>,
3530 rhs: ColRef<'_, RT>,
3531 ) -> Col<T> {
3532 let nrows = lhs.nrows();
3533 let mut out = Col::zeros(nrows);
3534 linalg_sp::matmul::sparse_dense_matmul(
3535 out.rb_mut().as_mat_mut(),
3536 Accum::Add,
3537 lhs,
3538 rhs.as_mat(),
3539 T::one_impl(),
3540 crate::get_global_parallelism(),
3541 );
3542 out
3543 }
3544 let lhs = self.rb();
3545 let rhs = rhs.rb();
3546 let nrows = lhs.nrows();
3547 imp(lhs.as_dyn(), rhs.as_dyn_rows().as_dyn_stride())
3548 .into_row_shape(nrows)
3549 }
3550 }
3551 impl<
3552 I: Index,
3553 T: ComplexField,
3554 Rows: Shape,
3555 Depth: Shape,
3556 LT: (Conjugate<Canonical = T>),
3557 RT: (Conjugate<Canonical = T>),
3558 RRStride: Stride,
3559 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Depth>>),
3560 R: (for<'a> Reborrow<'a, Target = col::Ref<'a, RT, Depth, RRStride>>),
3561 > Mul<col::generic::Col<R>> for csr::generic::SparseRowMat<L>
3562 {
3563 type Output = Col<T, Rows>;
3564
3565 fn mul(self, rhs: _) {
3566 #[track_caller]
3567 fn imp<
3568 I: Index,
3569 T: ComplexField,
3570 LT: Conjugate<Canonical = T>,
3571 RT: Conjugate<Canonical = T>,
3572 >(
3573 lhs: SparseRowMatRef<'_, I, LT>,
3574 rhs: ColRef<'_, RT>,
3575 ) -> Col<T> {
3576 let nrows = lhs.nrows();
3577 let mut out = Col::zeros(nrows);
3578 linalg_sp::matmul::dense_sparse_matmul(
3579 out.rb_mut().transpose_mut().as_mat_mut(),
3580 Accum::Add,
3581 rhs.transpose().as_mat(),
3582 lhs.transpose(),
3583 T::one_impl(),
3584 crate::get_global_parallelism(),
3585 );
3586 out
3587 }
3588 let lhs = self.rb();
3589 let rhs = rhs.rb();
3590 let nrows = lhs.nrows();
3591 imp(lhs.as_dyn(), rhs.as_dyn_rows().as_dyn_stride())
3592 .into_row_shape(nrows)
3593 }
3594 }
3595 });
3596 impl_binop!({
3597 impl<
3598 I: Index,
3599 T: ComplexField,
3600 Rows: Shape,
3601 Dim: Shape,
3602 LT: (Conjugate<Canonical = T>),
3603 RT: (Conjugate<Canonical = T>),
3604 RStride: Stride,
3605 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Dim>>),
3606 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
3607 > Mul<diag::generic::Diag<R>> for csc::generic::SparseColMat<L>
3608 {
3609 type Output = SparseColMat<I, T, Rows, Dim>;
3610
3611 fn mul(self, rhs: _) {
3612 #[track_caller]
3613 fn imp<
3614 I: Index,
3615 T: ComplexField,
3616 LT: Conjugate<Canonical = T>,
3617 RT: Conjugate<Canonical = T>,
3618 >(
3619 lhs: SparseColMatRef<'_, I, LT>,
3620 rhs: DiagRef<'_, RT>,
3621 ) -> SparseColMat<I, T> {
3622 with_dim!(M, lhs.nrows());
3623 with_dim!(N, lhs.ncols());
3624 let lhs = lhs.as_shape(M, N);
3625 let rhs = rhs.as_shape(N);
3626 let symbolic = lhs.symbolic().to_owned().unwrap();
3627 let mut out = alloc::vec::Vec::new();
3628 out.resize(symbolic.row_idx().len(), T::zero_impl());
3629 for j in lhs.ncols().indices() {
3630 let ref rhs = Conj::apply(&rhs[j]);
3631 for (out, lhs) in iter::zip(
3632 &mut out[symbolic.col_range(j)],
3633 lhs.val_of_col(j),
3634 ) {
3635 *out = Conj::apply(lhs) * rhs;
3636 }
3637 }
3638 SparseColMat::new(symbolic, out).into_shape(*M, *N)
3639 }
3640 let lhs = self.rb();
3641 let rhs = rhs.rb().column_vector();
3642 let (nrows, ncols) = lhs.shape();
3643 imp(
3644 lhs.as_dyn(),
3645 rhs.as_dyn_rows().as_dyn_stride().as_diagonal(),
3646 )
3647 .into_shape(nrows, ncols)
3648 }
3649 }
3650 impl<
3651 I: Index,
3652 T: ComplexField,
3653 Rows: Shape,
3654 Dim: Shape,
3655 LT: (Conjugate<Canonical = T>),
3656 RT: (Conjugate<Canonical = T>),
3657 RStride: Stride,
3658 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Dim>>),
3659 R: (for<'a> Reborrow<'a, Target = diag::Ref<'a, RT, Dim, RStride>>),
3660 > Mul<diag::generic::Diag<R>> for csr::generic::SparseRowMat<L>
3661 {
3662 type Output = SparseRowMat<I, T, Rows, Dim>;
3663
3664 fn mul(self, rhs: _) {
3665 #[track_caller]
3666 fn imp<
3667 I: Index,
3668 T: ComplexField,
3669 LT: Conjugate<Canonical = T>,
3670 RT: Conjugate<Canonical = T>,
3671 >(
3672 lhs: SparseRowMatRef<'_, I, LT>,
3673 rhs: DiagRef<'_, RT>,
3674 ) -> SparseRowMat<I, T> {
3675 with_dim!(M, lhs.nrows());
3676 with_dim!(N, lhs.ncols());
3677 let lhs = lhs.as_shape(M, N);
3678 let rhs = rhs.as_shape(N);
3679 let symbolic = lhs.symbolic().to_owned().unwrap();
3680 let mut out = alloc::vec::Vec::new();
3681 out.resize(symbolic.col_idx().len(), T::zero_impl());
3682 for i in lhs.nrows().indices() {
3683 for ((j, out), lhs) in iter::zip(
3684 iter::zip(
3685 symbolic.col_idx_of_row(i),
3686 &mut out[symbolic.row_range(i)],
3687 ),
3688 lhs.val_of_row(i),
3689 ) {
3690 *out = Conj::apply(lhs) * Conj::apply(&rhs[j]);
3691 }
3692 }
3693 SparseRowMat::new(symbolic, out).into_shape(*M, *N)
3694 }
3695 let lhs = self.rb();
3696 let rhs = rhs.rb().column_vector();
3697 let (nrows, ncols) = lhs.shape();
3698 imp(
3699 lhs.as_dyn(),
3700 rhs.as_dyn_rows().as_dyn_stride().as_diagonal(),
3701 )
3702 .into_shape(nrows, ncols)
3703 }
3704 }
3705 });
3706 impl_binop!({
3707 impl<
3708 I: Index,
3709 T: ComplexField,
3710 Rows: Shape,
3711 Dim: Shape,
3712 LT: (Conjugate<Canonical = T>),
3713 L: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, LT, Rows, Dim>>),
3714 R: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, Dim>>),
3715 > Mul<perm::generic::Perm<R>> for csc::generic::SparseColMat<L>
3716 {
3717 type Output = SparseColMat<I, T, Rows, Dim>;
3718
3719 fn mul(self, rhs: _) {
3720 #[track_caller]
3721 fn imp<
3722 I: Index,
3723 T: ComplexField,
3724 LT: Conjugate<Canonical = T>,
3725 >(
3726 lhs: SparseColMatRef<'_, I, LT>,
3727 rhs: PermRef<'_, I>,
3728 ) -> SparseColMat<I, T> {
3729 with_dim!(M, lhs.nrows());
3730 with_dim!(N, lhs.ncols());
3731 let lhs = lhs.as_shape(M, N);
3732 let rhs = rhs.as_shape(N);
3733 let symbolic = lhs.symbolic();
3734 let mut out_col_ptr = alloc::vec::Vec::new();
3735 let mut out_row_idx = alloc::vec::Vec::new();
3736 let mut out = alloc::vec::Vec::new();
3737 out_col_ptr
3738 .resize(symbolic.col_ptr().len(), I::truncate(0));
3739 out_row_idx
3740 .resize(symbolic.col_ptr().len(), I::truncate(0));
3741 out.resize(
3742 symbolic.row_idx().len(),
3743 T::Canonical::zero_impl(),
3744 );
3745 let inv = rhs.bound_arrays().1;
3746 let mut pos = 0usize;
3747 for j in lhs.ncols().indices() {
3748 let inv = inv[j].zx();
3749 let row_idx = lhs.as_dyn().row_idx_of_col_raw(*inv);
3750 let len = row_idx.len();
3751 out_row_idx[pos..][..len].copy_from_slice(row_idx);
3752 for (out, lhs) in iter::zip(
3753 &mut out[pos..][..len],
3754 lhs.val_of_col(inv),
3755 ) {
3756 *out = Conj::apply(lhs);
3757 }
3758 pos += row_idx.len();
3759 }
3760 out_row_idx.truncate(pos);
3761 out.truncate(pos);
3762 SparseColMat::new(
3763 unsafe {
3764 SymbolicSparseColMat::new_unchecked(
3765 symbolic.nrows(),
3766 symbolic.ncols(),
3767 out_col_ptr,
3768 None,
3769 out_row_idx,
3770 )
3771 },
3772 out,
3773 )
3774 .into_shape(*M, *N)
3775 }
3776 let lhs = self.rb();
3777 let rhs = rhs.rb();
3778 let (nrows, ncols) = lhs.shape();
3779 imp(lhs.as_dyn(), rhs.as_shape(ncols.unbound()))
3780 .into_shape(nrows, ncols)
3781 }
3782 }
3783 impl<
3784 I: Index,
3785 T: ComplexField,
3786 Rows: Shape,
3787 Dim: Shape,
3788 LT: (Conjugate<Canonical = T>),
3789 L: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, LT, Rows, Dim>>),
3790 R: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, Dim>>),
3791 > Mul<perm::generic::Perm<R>> for csr::generic::SparseRowMat<L>
3792 {
3793 type Output = SparseRowMat<I, T, Rows, Dim>;
3794
3795 fn mul(self, rhs: _) {
3796 #[track_caller]
3797 fn imp<
3798 I: Index,
3799 T: ComplexField,
3800 LT: Conjugate<Canonical = T>,
3801 >(
3802 lhs: SparseRowMatRef<'_, I, LT>,
3803 rhs: PermRef<'_, I>,
3804 ) -> SparseRowMat<I, T> {
3805 with_dim!(M, lhs.nrows());
3806 with_dim!(N, lhs.ncols());
3807 let lhs = lhs.as_shape(M, N);
3808 let rhs = rhs.as_shape(N);
3809 let symbolic = lhs.symbolic();
3810 let mut out_row_ptr = alloc::vec::Vec::new();
3811 let mut out_col_idx = alloc::vec::Vec::new();
3812 let mut out = alloc::vec::Vec::new();
3813 out_row_ptr
3814 .resize(symbolic.row_ptr().len(), I::truncate(0));
3815 out_col_idx
3816 .resize(symbolic.row_ptr().len(), I::truncate(0));
3817 out.resize(
3818 symbolic.col_idx().len(),
3819 T::Canonical::zero_impl(),
3820 );
3821 let inv = rhs.bound_arrays().0;
3822 let mut pos = 0usize;
3823 for i in lhs.nrows().indices() {
3824 let col_idx = lhs.col_idx_of_row_raw(i);
3825 let len = col_idx.len();
3826 for ((out_j, out_v), (lhs_j, lhs_v)) in iter::zip(
3827 iter::zip(
3828 &mut out_col_idx[pos..][..len],
3829 &mut out[pos..][..len],
3830 ),
3831 iter::zip(lhs.col_idx_of_row(i), lhs.val_of_row(i)),
3832 ) {
3833 *out_j = *inv[lhs_j];
3834 *out_v = Conj::apply(lhs_v);
3835 }
3836 pos += col_idx.len();
3837 }
3838 out_col_idx.truncate(pos);
3839 out.truncate(pos);
3840 SparseRowMat::new(
3841 unsafe {
3842 SymbolicSparseRowMat::new_unchecked(
3843 symbolic.nrows(),
3844 symbolic.ncols(),
3845 out_row_ptr,
3846 None,
3847 out_col_idx,
3848 )
3849 },
3850 out,
3851 )
3852 .into_shape(*M, *N)
3853 }
3854 let lhs = self.rb();
3855 let rhs = rhs.rb();
3856 let (nrows, ncols) = lhs.shape();
3857 imp(lhs.as_dyn(), rhs.as_shape(ncols.unbound()))
3858 .into_shape(nrows, ncols)
3859 }
3860 }
3861 });
3862 impl_binop!({
3863 impl<
3864 I: Index,
3865 T: ComplexField,
3866 Cols: Shape,
3867 Dim: Shape,
3868 RT: (Conjugate<Canonical = T>),
3869 L: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, Dim>>),
3870 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Dim, Cols>>),
3871 > Mul<csc::generic::SparseColMat<R>> for perm::generic::Perm<L>
3872 {
3873 type Output = SparseColMat<I, T, Dim, Cols>;
3874
3875 fn mul(self, rhs: _) {
3876 (rhs.rb().transpose() * self.rb().inverse()).into_transpose()
3877 }
3878 }
3879 impl<
3880 I: Index,
3881 T: ComplexField,
3882 Cols: Shape,
3883 Dim: Shape,
3884 RT: (Conjugate<Canonical = T>),
3885 L: (for<'a> Reborrow<'a, Target = perm::Ref<'a, I, Dim>>),
3886 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Dim, Cols>>),
3887 > Mul<csr::generic::SparseRowMat<R>> for perm::generic::Perm<L>
3888 {
3889 type Output = SparseRowMat<I, T, Dim, Cols>;
3890
3891 fn mul(self, rhs: _) {
3892 (rhs.rb().transpose() * self.rb().inverse()).into_transpose()
3893 }
3894 }
3895 });
3896 impl_binop!({
3897 impl<
3898 I: Index,
3899 T: ComplexField,
3900 Cols: Shape,
3901 Dim: Shape,
3902 LT: (Conjugate<Canonical = T>),
3903 RT: (Conjugate<Canonical = T>),
3904 LStride: Stride,
3905 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
3906 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Dim, Cols>>),
3907 > Mul<csc::generic::SparseColMat<R>> for diag::generic::Diag<L>
3908 {
3909 type Output = SparseColMat<I, T, Dim, Cols>;
3910
3911 fn mul(self, rhs: _) {
3912 #[track_caller]
3913 fn imp<
3914 I: Index,
3915 T: ComplexField,
3916 LT: Conjugate<Canonical = T>,
3917 RT: Conjugate<Canonical = T>,
3918 >(
3919 lhs: DiagRef<'_, LT>,
3920 rhs: SparseColMatRef<'_, I, RT>,
3921 ) -> SparseColMat<I, T> {
3922 (rhs.transpose() * lhs).into_transpose()
3923 }
3924 let lhs = self.rb().column_vector();
3925 let rhs = rhs.rb();
3926 let (nrows, ncols) = rhs.shape();
3927 imp(
3928 lhs.as_dyn_rows().as_dyn_stride().as_diagonal(),
3929 rhs.as_dyn(),
3930 )
3931 .into_shape(nrows, ncols)
3932 }
3933 }
3934 impl<
3935 I: Index,
3936 T: ComplexField,
3937 Cols: Shape,
3938 Dim: Shape,
3939 LT: (Conjugate<Canonical = T>),
3940 RT: (Conjugate<Canonical = T>),
3941 LStride: Stride,
3942 L: (for<'a> Reborrow<'a, Target = diag::Ref<'a, LT, Dim, LStride>>),
3943 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Dim, Cols>>),
3944 > Mul<csr::generic::SparseRowMat<R>> for diag::generic::Diag<L>
3945 {
3946 type Output = SparseRowMat<I, T, Dim, Cols>;
3947
3948 fn mul(self, rhs: _) {
3949 #[track_caller]
3950 fn imp<
3951 I: Index,
3952 T: ComplexField,
3953 LT: Conjugate<Canonical = T>,
3954 RT: Conjugate<Canonical = T>,
3955 >(
3956 lhs: DiagRef<'_, LT>,
3957 rhs: SparseRowMatRef<'_, I, RT>,
3958 ) -> SparseRowMat<I, T> {
3959 (rhs.transpose() * lhs).into_transpose()
3960 }
3961 let lhs = self.rb().column_vector();
3962 let rhs = rhs.rb();
3963 let (nrows, ncols) = rhs.shape();
3964 imp(
3965 lhs.as_dyn_rows().as_dyn_stride().as_diagonal(),
3966 rhs.as_dyn(),
3967 )
3968 .into_shape(nrows, ncols)
3969 }
3970 }
3971 });
3972 impl_binop!({
3973 impl<
3974 I: Index,
3975 T: ComplexField,
3976 Rows: Shape,
3977 Cols: Shape,
3978 Depth: Shape,
3979 LT: (Conjugate<Canonical = T>),
3980 LRStride: Stride,
3981 LCStride: Stride,
3982 RT: (Conjugate<Canonical = T>),
3983 L: (for<'a> Reborrow<
3984 'a,
3985 Target = mat::Ref<'a, LT, Rows, Depth, LRStride, LCStride>,
3986 >),
3987 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Depth, Cols>>),
3988 > Mul<csc::generic::SparseColMat<R>> for mat::generic::Mat<L>
3989 {
3990 type Output = Mat<T, Rows, Cols>;
3991
3992 fn mul(self, rhs: _) {
3993 #[track_caller]
3994 fn imp<
3995 I: Index,
3996 T: ComplexField,
3997 LT: Conjugate<Canonical = T>,
3998 RT: Conjugate<Canonical = T>,
3999 >(
4000 lhs: MatRef<'_, LT>,
4001 rhs: SparseColMatRef<'_, I, RT>,
4002 ) -> Mat<T> {
4003 let nrows = lhs.nrows();
4004 let ncols = rhs.ncols();
4005 let mut out = Mat::zeros(nrows, ncols);
4006 linalg_sp::matmul::dense_sparse_matmul(
4007 out.rb_mut(),
4008 Accum::Add,
4009 lhs,
4010 rhs,
4011 T::one_impl(),
4012 crate::get_global_parallelism(),
4013 );
4014 out
4015 }
4016 let lhs = self.rb();
4017 let rhs = rhs.rb();
4018 let nrows = lhs.nrows();
4019 let ncols = rhs.ncols();
4020 imp(lhs.as_dyn().as_dyn_stride(), rhs.as_dyn())
4021 .into_shape(nrows, ncols)
4022 }
4023 }
4024 impl<
4025 I: Index,
4026 T: ComplexField,
4027 Rows: Shape,
4028 Cols: Shape,
4029 Depth: Shape,
4030 LT: (Conjugate<Canonical = T>),
4031 LRStride: Stride,
4032 LCStride: Stride,
4033 RT: (Conjugate<Canonical = T>),
4034 L: (for<'a> Reborrow<
4035 'a,
4036 Target = mat::Ref<'a, LT, Rows, Depth, LRStride, LCStride>,
4037 >),
4038 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Depth, Cols>>),
4039 > Mul<csr::generic::SparseRowMat<R>> for mat::generic::Mat<L>
4040 {
4041 type Output = Mat<T, Rows, Cols>;
4042
4043 fn mul(self, rhs: _) {
4044 #[track_caller]
4045 fn imp<
4046 I: Index,
4047 T: ComplexField,
4048 LT: Conjugate<Canonical = T>,
4049 RT: Conjugate<Canonical = T>,
4050 >(
4051 lhs: MatRef<'_, LT>,
4052 rhs: SparseRowMatRef<'_, I, RT>,
4053 ) -> Mat<T> {
4054 let nrows = lhs.nrows();
4055 let ncols = rhs.ncols();
4056 let mut out = Mat::zeros(nrows, ncols);
4057 linalg_sp::matmul::sparse_dense_matmul(
4058 out.rb_mut().transpose_mut(),
4059 Accum::Add,
4060 rhs.transpose(),
4061 lhs.transpose(),
4062 T::one_impl(),
4063 crate::get_global_parallelism(),
4064 );
4065 out
4066 }
4067 let lhs = self.rb();
4068 let rhs = rhs.rb();
4069 let nrows = lhs.nrows();
4070 let ncols = rhs.ncols();
4071 imp(lhs.as_dyn().as_dyn_stride(), rhs.as_dyn())
4072 .into_shape(nrows, ncols)
4073 }
4074 }
4075 });
4076 impl_binop!({
4077 impl<
4078 I: Index,
4079 T: ComplexField,
4080 Cols: Shape,
4081 Depth: Shape,
4082 LT: (Conjugate<Canonical = T>),
4083 LCStride: Stride,
4084 RT: (Conjugate<Canonical = T>),
4085 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Depth, LCStride>>),
4086 R: (for<'a> Reborrow<'a, Target = csc::Ref<'a, I, RT, Depth, Cols>>),
4087 > Mul<csc::generic::SparseColMat<R>> for row::generic::Row<L>
4088 {
4089 type Output = Row<T, Cols>;
4090
4091 fn mul(self, rhs: _) {
4092 #[track_caller]
4093 fn imp<
4094 I: Index,
4095 T: ComplexField,
4096 LT: Conjugate<Canonical = T>,
4097 RT: Conjugate<Canonical = T>,
4098 >(
4099 lhs: RowRef<'_, LT>,
4100 rhs: SparseColMatRef<'_, I, RT>,
4101 ) -> Row<T> {
4102 let ncols = rhs.ncols();
4103 let mut out = Row::zeros(ncols);
4104 linalg_sp::matmul::dense_sparse_matmul(
4105 out.rb_mut().as_mat_mut(),
4106 Accum::Add,
4107 lhs.as_mat(),
4108 rhs,
4109 T::one_impl(),
4110 crate::get_global_parallelism(),
4111 );
4112 out
4113 }
4114 let lhs = self.rb();
4115 let rhs = rhs.rb();
4116 let ncols = rhs.ncols();
4117 imp(lhs.as_dyn_cols().as_dyn_stride(), rhs.as_dyn())
4118 .into_col_shape(ncols)
4119 }
4120 }
4121 impl<
4122 I: Index,
4123 T: ComplexField,
4124 Cols: Shape,
4125 Depth: Shape,
4126 LT: (Conjugate<Canonical = T>),
4127 LCStride: Stride,
4128 RT: (Conjugate<Canonical = T>),
4129 L: (for<'a> Reborrow<'a, Target = row::Ref<'a, LT, Depth, LCStride>>),
4130 R: (for<'a> Reborrow<'a, Target = csr::Ref<'a, I, RT, Depth, Cols>>),
4131 > Mul<csr::generic::SparseRowMat<R>> for row::generic::Row<L>
4132 {
4133 type Output = Row<T, Cols>;
4134
4135 fn mul(self, rhs: _) {
4136 #[track_caller]
4137 fn imp<
4138 I: Index,
4139 T: ComplexField,
4140 LT: Conjugate<Canonical = T>,
4141 RT: Conjugate<Canonical = T>,
4142 >(
4143 lhs: RowRef<'_, LT>,
4144 rhs: SparseRowMatRef<'_, I, RT>,
4145 ) -> Row<T> {
4146 let ncols = rhs.ncols();
4147 let mut out = Row::zeros(ncols);
4148 linalg_sp::matmul::sparse_dense_matmul(
4149 out.rb_mut().transpose_mut().as_mat_mut(),
4150 Accum::Add,
4151 rhs.transpose(),
4152 lhs.transpose().as_mat(),
4153 T::one_impl(),
4154 crate::get_global_parallelism(),
4155 );
4156 out
4157 }
4158 let lhs = self.rb();
4159 let rhs = rhs.rb();
4160 let ncols = rhs.ncols();
4161 imp(lhs.as_dyn_cols().as_dyn_stride(), rhs.as_dyn())
4162 .into_col_shape(ncols)
4163 }
4164 }
4165 });
4166 fn add_fn<
4167 T: ComplexField,
4168 LhsT: Conjugate<Canonical = T>,
4169 RhsT: Conjugate<Canonical = T>,
4170 >(
4171 lhs: Option<&LhsT>,
4172 rhs: Option<&RhsT>,
4173 ) -> T {
4174 lhs.map(Conj::apply).unwrap_or_else(zero::<T>)
4175 + rhs.map(Conj::apply).unwrap_or_else(zero::<T>)
4176 }
4177 fn sub_fn<
4178 T: ComplexField,
4179 LhsT: Conjugate<Canonical = T>,
4180 RhsT: Conjugate<Canonical = T>,
4181 >(
4182 lhs: Option<&LhsT>,
4183 rhs: Option<&RhsT>,
4184 ) -> T {
4185 lhs.map(Conj::apply).unwrap_or_else(zero::<T>)
4186 - rhs.map(Conj::apply).unwrap_or_else(zero::<T>)
4187 }
4188 fn add_assign_fn<T: ComplexField, RhsT: Conjugate<Canonical = T>>(
4189 dst: &mut T,
4190 rhs: Option<&RhsT>,
4191 ) {
4192 if let Some(rhs) = rhs {
4193 *dst += Conj::apply(rhs);
4194 }
4195 }
4196 fn sub_assign_fn<T: ComplexField, RhsT: Conjugate<Canonical = T>>(
4197 dst: &mut T,
4198 rhs: Option<&RhsT>,
4199 ) {
4200 if let Some(rhs) = rhs {
4201 *dst -= Conj::apply(rhs);
4202 }
4203 }
4204}
4205#[cfg(test)]
4206#[allow(non_snake_case)]
4207mod test {
4208 use crate::assert;
4209 use crate::col::*;
4210 use crate::internal_prelude::*;
4211 use crate::mat::*;
4212 use crate::perm::*;
4213 use crate::row::*;
4214 use assert_approx_eq::assert_approx_eq;
4215 fn matrices() -> (Mat<f64>, Mat<f64>) {
4216 let A = mat![[2.8, -3.3], [-1.7, 5.2], [4.6, -8.3],];
4217 let B = mat![[-7.9, 8.3], [4.7, -3.2], [3.8, -5.2],];
4218 (A, B)
4219 }
4220 fn rows() -> (Row<f64>, Row<f64>) {
4221 (row![2.8, -3.3], row![-7.9, 8.3])
4222 }
4223 fn cols() -> (Col<f64>, Col<f64>) {
4224 (col![2.8, -3.3], col![-7.9, 8.3])
4225 }
4226 #[test]
4227 #[should_panic]
4228 fn test_adding_matrices_of_different_sizes_should_panic() {
4229 let A = mat![[1.0, 2.0], [3.0, 4.0]];
4230 let B = mat![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
4231 _ = A + B;
4232 }
4233 #[test]
4234 #[should_panic]
4235 fn test_subtracting_two_matrices_of_different_sizes_should_panic() {
4236 let A = mat![[1.0, 2.0], [3.0, 4.0]];
4237 let B = mat![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
4238 _ = A - B;
4239 }
4240 #[test]
4241 fn test_matrix_add() {
4242 let (A, B) = matrices();
4243 let expected = mat![[-5.1, 5.0], [3.0, 2.0], [8.4, -13.5],];
4244 assert_matrix_approx_eq(A.as_ref() + B.as_ref(), &expected);
4245 assert_matrix_approx_eq(&A + &B, &expected);
4246 assert_matrix_approx_eq(A.as_ref() + &B, &expected);
4247 assert_matrix_approx_eq(&A + B.as_ref(), &expected);
4248 assert_matrix_approx_eq(A.as_ref() + B.clone(), &expected);
4249 assert_matrix_approx_eq(&A + B.clone(), &expected);
4250 assert_matrix_approx_eq(A.clone() + B.as_ref(), &expected);
4251 assert_matrix_approx_eq(A.clone() + &B, &expected);
4252 assert_matrix_approx_eq(A + B, &expected);
4253 }
4254 #[test]
4255 fn test_row_add() {
4256 let (A, B) = rows();
4257 let expected = row![-5.1, 5.0];
4258 assert_row_approx_eq(A.as_ref() + B.as_ref(), &expected);
4259 assert_row_approx_eq(&A + &B, &expected);
4260 assert_row_approx_eq(A.as_ref() + &B, &expected);
4261 assert_row_approx_eq(&A + B.as_ref(), &expected);
4262 assert_row_approx_eq(A.as_ref() + B.clone(), &expected);
4263 assert_row_approx_eq(A.clone() + B.as_ref(), &expected);
4264 assert_row_approx_eq(&A + B.clone(), &expected);
4265 assert_row_approx_eq(A.clone() + &B, &expected);
4266 assert_row_approx_eq(A + B, &expected);
4267 }
4268 #[test]
4269 fn test_col_add() {
4270 let (A, B) = cols();
4271 let expected = col![-5.1, 5.0];
4272 assert_col_approx_eq(A.as_ref() + B.as_ref(), &expected);
4273 assert_col_approx_eq(&A + &B, &expected);
4274 assert_col_approx_eq(A.as_ref() + &B, &expected);
4275 assert_col_approx_eq(&A + B.as_ref(), &expected);
4276 assert_col_approx_eq(A.as_ref() + B.clone(), &expected);
4277 assert_col_approx_eq(A.clone() + B.as_ref(), &expected);
4278 assert_col_approx_eq(&A + B.clone(), &expected);
4279 assert_col_approx_eq(A.clone() + &B, &expected);
4280 assert_col_approx_eq(A + B, &expected);
4281 }
4282 #[test]
4283 fn test_matrix_sub() {
4284 let (A, B) = matrices();
4285 let expected = mat![[10.7, -11.6], [-6.4, 8.4], [0.8, -3.1],];
4286 assert_matrix_approx_eq(A.as_ref() - B.as_ref(), &expected);
4287 assert_matrix_approx_eq(&A - &B, &expected);
4288 assert_matrix_approx_eq(A.as_ref() - &B, &expected);
4289 assert_matrix_approx_eq(&A - B.as_ref(), &expected);
4290 assert_matrix_approx_eq(A.as_ref() - B.clone(), &expected);
4291 assert_matrix_approx_eq(&A - B.clone(), &expected);
4292 assert_matrix_approx_eq(A.clone() - B.as_ref(), &expected);
4293 assert_matrix_approx_eq(A.clone() - &B, &expected);
4294 assert_matrix_approx_eq(A - B, &expected);
4295 }
4296 #[test]
4297 fn test_row_sub() {
4298 let (A, B) = rows();
4299 let expected = row![10.7, -11.6];
4300 assert_row_approx_eq(A.as_ref() - B.as_ref(), &expected);
4301 assert_row_approx_eq(&A - &B, &expected);
4302 assert_row_approx_eq(A.as_ref() - &B, &expected);
4303 assert_row_approx_eq(&A - B.as_ref(), &expected);
4304 assert_row_approx_eq(A.as_ref() - B.clone(), &expected);
4305 assert_row_approx_eq(A.clone() - B.as_ref(), &expected);
4306 assert_row_approx_eq(&A - B.clone(), &expected);
4307 assert_row_approx_eq(A.clone() - &B, &expected);
4308 assert_row_approx_eq(A - B, &expected);
4309 }
4310 #[test]
4311 fn test_col_sub() {
4312 let (A, B) = cols();
4313 let expected = col![10.7, -11.6];
4314 assert_col_approx_eq(A.as_ref() - B.as_ref(), &expected);
4315 assert_col_approx_eq(&A - &B, &expected);
4316 assert_col_approx_eq(A.as_ref() - &B, &expected);
4317 assert_col_approx_eq(&A - B.as_ref(), &expected);
4318 assert_col_approx_eq(A.as_ref() - B.clone(), &expected);
4319 assert_col_approx_eq(A.clone() - B.as_ref(), &expected);
4320 assert_col_approx_eq(&A - B.clone(), &expected);
4321 assert_col_approx_eq(A.clone() - &B, &expected);
4322 assert_col_approx_eq(A - B, &expected);
4323 }
4324 #[test]
4325 fn test_neg() {
4326 let (A, _) = matrices();
4327 let expected = mat![[-2.8, 3.3], [1.7, -5.2], [-4.6, 8.3],];
4328 assert!(-A == expected);
4329 }
4330 #[test]
4331 fn test_scalar_mul() {
4332 use crate::Scale as scale;
4333 let (A, _) = matrices();
4334 let k = 3.0;
4335 let expected =
4336 Mat::from_fn(A.nrows(), A.ncols(), |i, j| A.as_ref()[(i, j)] * k);
4337 {
4338 assert_matrix_approx_eq(A.as_ref() * scale(k), &expected);
4339 assert_matrix_approx_eq(&A * scale(k), &expected);
4340 assert_matrix_approx_eq(A.as_ref() * scale(k), &expected);
4341 assert_matrix_approx_eq(&A * scale(k), &expected);
4342 assert_matrix_approx_eq(A.as_ref() * scale(k), &expected);
4343 assert_matrix_approx_eq(&A * scale(k), &expected);
4344 assert_matrix_approx_eq(A.clone() * scale(k), &expected);
4345 assert_matrix_approx_eq(A.clone() * scale(k), &expected);
4346 assert_matrix_approx_eq(A * scale(k), &expected);
4347 }
4348 let (A, _) = matrices();
4349 {
4350 assert_matrix_approx_eq(scale(k) * A.as_ref(), &expected);
4351 assert_matrix_approx_eq(scale(k) * &A, &expected);
4352 assert_matrix_approx_eq(scale(k) * A.as_ref(), &expected);
4353 assert_matrix_approx_eq(scale(k) * &A, &expected);
4354 assert_matrix_approx_eq(scale(k) * A.as_ref(), &expected);
4355 assert_matrix_approx_eq(scale(k) * &A, &expected);
4356 assert_matrix_approx_eq(scale(k) * A.clone(), &expected);
4357 assert_matrix_approx_eq(scale(k) * A.clone(), &expected);
4358 assert_matrix_approx_eq(scale(k) * A, &expected);
4359 }
4360 }
4361 #[test]
4362 fn test_diag_mul() {
4363 let (A, _) = matrices();
4364 let diag_left = mat![[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]];
4365 let diag_right = mat![[4.0, 0.0], [0.0, 5.0]];
4366 assert!(&diag_left * &A == diag_left.as_ref().diagonal() * &A);
4367 assert!(&A * &diag_right == &A * diag_right.as_ref().diagonal());
4368 }
4369 #[test]
4370 fn test_perm_mul() {
4371 let A = Mat::from_fn(6, 5, |i, j| (j + 5 * i) as f64);
4372 let pl = Perm::<usize>::new_checked(
4373 Box::new([5, 1, 4, 0, 2, 3]),
4374 Box::new([3, 1, 4, 5, 2, 0]),
4375 6,
4376 );
4377 let pr = Perm::<usize>::new_checked(
4378 Box::new([1, 4, 0, 2, 3]),
4379 Box::new([2, 0, 3, 4, 1]),
4380 5,
4381 );
4382 let perm_left = mat![
4383 [0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
4384 [0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
4385 [0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
4386 [1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
4387 [0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
4388 [0.0, 0.0, 0.0, 1.0, 0.0, 0.0],
4389 ];
4390 let perm_right = mat![
4391 [0.0, 1.0, 0.0, 0.0, 0.0],
4392 [0.0, 0.0, 0.0, 0.0, 1.0],
4393 [1.0, 0.0, 0.0, 0.0, 0.0],
4394 [0.0, 0.0, 1.0, 0.0, 0.0],
4395 [0.0, 0.0, 0.0, 1.0, 0.0],
4396 ];
4397 assert!(
4398 &pl * pl.as_ref().inverse()
4399 == PermRef::<'_, usize>::new_checked(
4400 &[0, 1, 2, 3, 4, 5],
4401 &[0, 1, 2, 3, 4, 5],
4402 6
4403 )
4404 );
4405 assert!(&perm_left * &A == &pl * &A);
4406 assert!(&A * &perm_right == &A * &pr);
4407 }
4408 #[test]
4409 fn test_matmul_col_row() {
4410 let A = Col::from_fn(6, |i| i as f64);
4411 let B = Row::from_fn(6, |j| (5 * j + 1) as f64);
4412 assert!(&A * &B == A.as_mat() * B.as_mat());
4413 assert!(&B * &A == (B.as_mat() * A.as_mat())[(0, 0)],);
4414 }
4415 fn assert_row_approx_eq(given: Row<f64>, expected: &Row<f64>) {
4416 assert_eq!(given.nrows(), expected.nrows());
4417 for i in 0..given.nrows() {
4418 assert_approx_eq!(given.as_ref()[i], expected.as_ref()[i]);
4419 }
4420 }
4421 fn assert_col_approx_eq(given: Col<f64>, expected: &Col<f64>) {
4422 assert_eq!(given.ncols(), expected.ncols());
4423 for i in 0..given.ncols() {
4424 assert_approx_eq!(given.as_ref()[i], expected.as_ref()[i]);
4425 }
4426 }
4427 fn assert_matrix_approx_eq(given: Mat<f64>, expected: &Mat<f64>) {
4428 assert_eq!(given.nrows(), expected.nrows());
4429 assert_eq!(given.ncols(), expected.ncols());
4430 for i in 0..given.nrows() {
4431 for j in 0..given.ncols() {
4432 assert_approx_eq!(
4433 given.as_ref()[(i, j)],
4434 expected.as_ref()[(i, j)]
4435 );
4436 }
4437 }
4438 }
4439}