ferrix 0.1.0

A simple matrix library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#[macro_use]
mod op_macros {
    macro_rules! impl_inner {
        (mat, $lhs:ty, $rhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + $trait<T, Output=T>, $($generics)*> $trait<$rhs> for $lhs {
                type Output = $output;

                fn $method(self, other: $rhs) -> Self::Output {
                    Self::Output::from(std::array::from_fn(|i| std::array::from_fn(|j| self[(i, j)] $op other[(i, j)])))
                }
            }
        };
        (mat, $lhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + $trait<T, Output=T>, $($generics)*> $trait<T> for $lhs {
                type Output = $output;

                fn $method(self, scalar: T) -> Self::Output {
                    Self::Output::from(std::array::from_fn(|i| std::array::from_fn(|j| self[(i, j)] $op scalar)))
                }
            }
        };
        (vec, $lhs:ty, $rhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + $trait<T, Output=T>, $($generics)*> $trait<$rhs> for $lhs {
                type Output = $output;

                fn $method(self, other: $rhs) -> Self::Output {
                    Self::Output::from(std::array::from_fn(|i| self[i] $op other[i]))
                }
            }
        };
        (vec, $lhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + $trait<T, Output=T>, $($generics)*> $trait<T> for $lhs {
                type Output = $output;

                fn $method(self, scalar: T) -> Self::Output {
                    Self::Output::from(std::array::from_fn(|i| self[i] $op scalar))
                }
            }
        };
    }

    macro_rules! impl_combinations {
        ($type:tt, $lhs:ty, $rhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl_inner!($type, $lhs, $rhs, $trait, $method, $op, $output, $($generics)*);
            impl_inner!($type, &$lhs, $rhs, $trait, $method, $op, $output, $($generics)*);
            impl_inner!($type, $lhs, &$rhs, $trait, $method, $op, $output, $($generics)*);
            impl_inner!($type, &$lhs, &$rhs, $trait, $method, $op, $output, $($generics)*);
        };
        ($type:tt, $lhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl_inner!($type, $lhs, $trait, $method, $op, $output, $($generics)*);
            impl_inner!($type, &$lhs, $trait, $method, $op, $output, $($generics)*);
        };
    }

    macro_rules! generate_op_scalar_macros {
        ($trait:tt, $method:tt, $op:tt) => {
            macro_rules! impl_vv_op {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, Vector<T, N>, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, Vector<T, N>, const N: usize);
                }
            }

            macro_rules! impl_vv_op_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, Vector<T, M>, V: Index<usize, Output = T>, const N: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, Vector<T, M>, V: Index<usize, Output = T>, const N: usize, const M: usize);
                }
            }

            macro_rules! impl_vv_op_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, RowVector<T, N>, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, RowVector<T, N>, const N: usize);
                }
            }

            macro_rules! impl_vv_op_view_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, RowVector<T, M>, V: Index<usize, Output = T>, const N: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, RowVector<T, M>, V: Index<usize, Output = T>, const N: usize, const M: usize);
                }
            }

            macro_rules! impl_mm_op {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, M, N>, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, M, N>, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_mm_op_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, M, N>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, M, N>, const A: usize, const B: usize, const M: usize, const N: usize);
                }
            }
        };
    }

    macro_rules! generate_op_all_macros {
        ($trait:tt, $method:tt, $op:tt) => {
            generate_op_scalar_macros!($trait, $method, $op);

            macro_rules! impl_vv_op_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, Vector<T, M>, V1: Index<usize, Output = T>, V2: Index<usize, Output = T>, const A: usize, const N: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, Vector<T, M>, V1: Index<usize, Output = T>, V2: Index<usize, Output = T>, const A: usize, const N: usize, const M: usize);
                }
            }

            macro_rules! impl_vv_op_view_view_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, RowVector<T, M>, V1: Index<usize, Output = T>, V2: Index<usize, Output = T>, const A: usize, const N: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, RowVector<T, M>, V1: Index<usize, Output = T>, V2: Index<usize, Output = T>, const A: usize, const N: usize, const M: usize);
                }
            }

            macro_rules! impl_mm_op_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, M, N>, const A: usize, const B: usize, const C: usize, const D: usize, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, M, N>, const A: usize, const B: usize, const C: usize, const D: usize, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_mv_op {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, N, 1>, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, N, 1>, const N: usize);
                }
            }

            macro_rules! impl_mv_op_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, 1, N>, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, 1, N>, const N: usize);
                }
            }

            macro_rules! impl_vm_op_mat_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, Vector<T, M>, const A: usize, const B: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, Vector<T, M>, const A: usize, const B: usize, const M: usize);
                }
            }

            macro_rules! impl_vm_op_mat_view_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, RowVector<T, M>, const A: usize, const B: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, RowVector<T, M>, const A: usize, const B: usize, const M: usize);
                }
            }

            macro_rules! impl_vm_op_vec_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, Vector<T, M>, V: Index<usize, Output = T>, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, Vector<T, M>, V: Index<usize, Output = T>, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_vm_op_vec_view_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, RowVector<T, M>, V: Index<usize, Output = T>, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, RowVector<T, M>, V: Index<usize, Output = T>, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_vm_op_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, Vector<T, M>, V: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, Vector<T, M>, V: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_vm_op_view_view_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(vec, $lhs, $rhs, $trait, $method, $op, RowVector<T, M>, V: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(vec, $lhs, $trait, $method, $op, RowVector<T, M>, V: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_mv_op_mat_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, M, 1>, const A: usize, const B: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, M, 1>, const A: usize, const B: usize, const M: usize);
                }
            }

            macro_rules! impl_mv_op_mat_view_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, 1, M>, const A: usize, const B: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, 1, M>, const A: usize, const B: usize, const M: usize);
                }
            }

            macro_rules! impl_mv_op_vec_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, M, 1>, V: Index<usize, Output = T>, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, M, 1>, V: Index<usize, Output = T>, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_mv_op_vec_view_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, 1, M>, V: Index<usize, Output = T>, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, 1, M>, V: Index<usize, Output = T>, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_mv_op_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, M, 1>, V: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, M, 1>, V: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_mv_op_view_view_row {
                ($lhs:ty, $rhs:ty) => {
                    impl_combinations!(mat, $lhs, $rhs, $trait, $method, $op, Matrix<T, 1, M>, V: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_combinations!(mat, $lhs, $trait, $method, $op, Matrix<T, 1, M>, V: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                }
            }
        };
    }
}

#[macro_use]
mod op_assign_macros {
    macro_rules! impl_assign_inner {
        ($lhs:ty, $rhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + $trait<T>, $($generics)*> $trait<$rhs> for $lhs {
                fn $method(&mut self, other: $rhs) {
                    (0..self.capacity()).for_each(|i| self[i] $op other[i]);
                }
            }
        };
        ($lhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + $trait<T>, $($generics)*> $trait<T> for $lhs {
                fn $method(&mut self, scalar: T) {
                    (0..self.capacity()).for_each(|i| self[i] $op scalar);
                }
            }
        };
    }

    macro_rules! impl_assign_combinations {
        ($lhs:ty, $rhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl_assign_inner!($lhs, $rhs, $trait, $method, $op, $output, $($generics)*);
            impl_assign_inner!($lhs, &$rhs, $trait, $method, $op, $output, $($generics)*);
        };
        ($lhs:ty, $trait:tt, $method:tt, $op:tt, $output:ty, $($generics:tt)*) => {
            impl_assign_inner!($lhs, $trait, $method, $op, $output, $($generics)*);
        };
    }

    macro_rules! generate_op_assign_scalar_macros {
        ($trait:tt, $method:tt, $op:tt) => {
            macro_rules! impl_vv_op_assign {
                ($lhs:ty, $rhs:ty) => {
                    impl_assign_combinations!($lhs, $rhs, $trait, $method, $op, Vector<T, N>, const N: usize);
                };
                ($lhs:ty) => {
                    impl_assign_combinations!($lhs, $trait, $method, $op, Vector<T, N>, const N: usize);
                }
            }

            macro_rules! impl_vv_op_assign_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_assign_combinations!($lhs, $rhs, $trait, $method, $op, Vector<T, M>, V: IndexMut<usize, Output = T>, const N: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_assign_combinations!($lhs, $trait, $method, $op, Vector<T, M>, V: IndexMut<usize, Output = T>, const N: usize, const M: usize);
                }
            }

            macro_rules! impl_mm_op_assign {
                ($lhs:ty, $rhs:ty) => {
                    impl_assign_combinations!($lhs, $rhs, $trait, $method, $op, Matrix<T, M, N>, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_assign_combinations!($lhs, $trait, $method, $op, Matrix<T, M, N>, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_mm_op_assign_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_assign_combinations!($lhs, $rhs, $trait, $method, $op, Matrix<T, M, N>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_assign_combinations!($lhs, $trait, $method, $op, Matrix<T, M, N>, const A: usize, const B: usize, const M: usize, const N: usize);
                }
            }
        };
    }

    macro_rules! generate_op_assign_all_macros {
        ($trait:tt, $method:tt, $op:tt) => {
            generate_op_assign_scalar_macros!($trait, $method, $op);

            macro_rules! impl_vv_op_assign_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_assign_combinations!($lhs, $rhs, $trait, $method, $op, Vector<T, M>, V1: IndexMut<usize, Output = T>, V2: Index<usize, Output = T>, const A: usize, const N: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_assign_combinations!($lhs, $trait, $method, $op, Vector<T, M>, V1: IndexMut<usize, Output = T>, V2: Index<usize, Output = T>, const A: usize, const N: usize, const M: usize);
                }
            }

            macro_rules! impl_mm_op_assign_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_assign_combinations!($lhs, $rhs, $trait, $method, $op, Matrix<T, M, N>, const A: usize, const B: usize, const C: usize, const D: usize, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_assign_combinations!($lhs, $trait, $method, $op, Matrix<T, M, N>, const A: usize, const B: usize, const C: usize, const D: usize, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_vm_op_assign_mat_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_assign_combinations!($lhs, $rhs, $trait, $method, $op, Vector<T, M>, const A: usize, const B: usize, const M: usize);
                };
                ($lhs:ty) => {
                    impl_assign_combinations!($lhs, $trait, $method, $op, Vector<T, M>, const A: usize, const B: usize, const M: usize);
                }
            }

            macro_rules! impl_vm_op_assign_vec_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_assign_combinations!($lhs, $rhs, $trait, $method, $op, Vector<T, M>, V: IndexMut<usize, Output = T>, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_assign_combinations!($lhs, $trait, $method, $op, Vector<T, M>, V: IndexMut<usize, Output = T>, const M: usize, const N: usize);
                }
            }

            macro_rules! impl_vm_op_assign_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_assign_combinations!($lhs, $rhs, $trait, $method, $op, Vector<T, M>, V: IndexMut<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
                ($lhs:ty) => {
                    impl_assign_combinations!($lhs, $trait, $method, $op, Vector<T, M>, V: IndexMut<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                }
            }
        };
    }
}

#[macro_use]
mod dot_macros {
    macro_rules! impl_dot_inner {
        ($lhs:ty, $rhs:ty, $($generics:tt)*) => {
            impl<T: Copy + Zero + Mul<T, Output = T> + Add<T, Output = T>, $($generics)*> DotProduct<$rhs> for $lhs {
                type Output = T;

                fn dot(self, other: $rhs) -> Self::Output {
                    let mut result = T::zero();
                    for i in 0..M {
                        result = result + (self[i] * other[i]);
                    }
                    result
                }
            }
        };
    }

    macro_rules! impl_dot_combinations {
        ($lhs:ty, $rhs:ty, $($generics:tt)*) => {
            impl_dot_inner!($lhs, $rhs, $($generics)*);
            impl_dot_inner!(&$lhs, $rhs, $($generics)*);
            impl_dot_inner!($lhs, &$rhs, $($generics)*);
            impl_dot_inner!(&$lhs, &$rhs, $($generics)*);
        };
    }

    macro_rules! generate_dot_macros {
        () => {
            macro_rules! impl_dot {
                ($lhs:ty, $rhs:ty) => {
                    impl_dot_combinations!($lhs, $rhs, const M: usize);
                };
            }

            macro_rules! impl_dot_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_dot_combinations!($lhs, $rhs, V: Index<usize, Output = T>, const N: usize, const M: usize);
                };
            }

            macro_rules! impl_dot_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_dot_combinations!($lhs, $rhs, V1: Index<usize, Output = T>, V2: Index<usize, Output = T>, const A: usize, const N: usize, const M: usize);
                };
            }

            macro_rules! impl_dot_mat_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_dot_combinations!($lhs, $rhs, const A: usize, const B: usize, const M: usize);
                };
            }

            macro_rules! impl_dot_mat_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_dot_combinations!($lhs, $rhs, V: Index<usize, Output = T>, const A: usize, const B: usize, const N: usize, const M: usize);
                };
            }
        };
    }
}

#[macro_use]
mod matmul_macros {
    macro_rules! impl_matmul_inner {
        (scalar, $lhs:ty, $rhs:ty, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + Zero + Mul<T, Output = T> + Add<T, Output = T>, $($generics)*> Mul<$rhs> for $lhs
            {
                type Output = $output;

                fn mul(self, other: $rhs) -> Self::Output {
                    let mut result = T::zero();
                    for i in 0..M {
                        result = result + (self[i] * other[i]);
                    }
                    Self::Output::from([[result]])
                }
            }
        };
        (vecvec, $lhs:ty, $rhs:ty, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + Zero + Mul<T, Output = T> + Add<T, Output = T>, $($generics)*> Mul<$rhs> for $lhs
            {
                type Output = $output;

                fn mul(self, other: $rhs) -> Self::Output {
                    let mut result = Self::Output::zeros();
                    for i in 0..M {
                        for j in 0..N {
                            result[(i, j)] = result[(i, j)] + (self[i] * other[j]);
                        }
                    }
                    result
                }
            }
        };
        (vecmat, $lhs:ty, $rhs:ty, $output:ty, $($generics:tt)*) => {

            impl<T: Copy + Zero + Mul<T, Output = T> + Add<T, Output = T>, $($generics)*> Mul<$rhs> for $lhs
            {
                type Output = $output;

                fn mul(self, other: $rhs) -> Self::Output {
                    let mut result = Self::Output::zeros();
                    for j in 0..N {
                        for i in 0..M {
                            result[i] = result[i] + (self[j] * other[(j, i)]);
                        }
                    }
                    result
                }
            }
        };
        (matvec, $lhs:ty, $rhs:ty, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + Zero + Mul<T, Output = T> + Add<T, Output = T>, $($generics)*> Mul<$rhs> for $lhs {
                type Output = $output;

                fn mul(self, other: $rhs) -> Self::Output {
                    let mut result = Self::Output::zeros();
                    for i in 0..M {
                        for j in 0..N {
                            result[(i, 0)] = result[(i, 0)] + (self[(i, j)] * other[j]);
                        }
                    }
                    result
                }
            }
        };
        (matmat, $lhs:ty, $rhs:ty, $output:ty, $($generics:tt)*) => {
            impl<T: Copy + Zero + Mul<T, Output = T> + Add<T, Output = T>, $($generics)*> Mul<$rhs> for $lhs {
                type Output = $output;

                fn mul(self, other: $rhs) -> Self::Output {
                    let mut result = Self::Output::zeros();
                    for i in 0..M {
                        for k in 0..N {
                            for j in 0..P {
                                result[(i, j)] = result[(i, j)] + (self[(i, k)] * other[(k, j)]);
                            }
                        }
                    }
                    result
                }
            }
        };
    }

    macro_rules! impl_matmul_combinations {
        ($type:tt, $lhs:ty, $rhs:ty, $output:ty, $($generics:tt)*) => {
            impl_matmul_inner!($type, $lhs, $rhs, $output, $($generics)*);
            impl_matmul_inner!($type, &$lhs, $rhs, $output, $($generics)*);
            impl_matmul_inner!($type, $lhs, &$rhs, $output, $($generics)*);
            impl_matmul_inner!($type, &$lhs, &$rhs, $output, $($generics)*);
        };
    }

    macro_rules! generate_matmul_macros {
        () => {
            macro_rules! impl_vecmul_scalar {
                ($lhs:ty, $rhs:ty) => {
                   impl_matmul_combinations!(scalar, $lhs, $rhs, Matrix<T, 1, 1>, const M: usize);
                };
            }

            macro_rules! impl_vecmul_scalar_view {
                ($lhs:ty, $rhs:ty) => {
                   impl_matmul_combinations!(scalar, $lhs, $rhs, Matrix<T, 1, 1>, V: Index<usize, Output = T>, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_vecmul_scalar_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(scalar, $lhs, $rhs, Matrix<T, 1, 1>, V1: Index<usize, Output = T>, V2: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize);
                };
            }

            macro_rules! impl_vecmul {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecvec, $lhs, $rhs, Matrix<T, M, N>, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_vecmul_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecvec, $lhs, $rhs, Matrix<T, M, N>, V: Index<usize, Output = T>, const A: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_vecmul_mat_row_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecvec, $lhs, $rhs, Matrix<T, M, N>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_vecmul_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecvec, $lhs, $rhs, Matrix<T, M, N>, V1: Index<usize, Output = T>, V2: Index<usize, Output = T>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_vecmul_mat_row_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecvec, $lhs, $rhs, Matrix<T, M, N>, V: Index<usize, Output = T>, const A: usize, const B: usize, const C: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_vecmul_mat {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecmat, $lhs, $rhs, Matrix<T, 1, M>, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_vecmul_mat_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecmat, $lhs, $rhs, Matrix<T, 1, M>, V: Index<usize, Output = T>, const A: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_vecmul_vecmat_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecmat, $lhs, $rhs, Matrix<T, 1, M>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_vecmul_vecmat_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecmat, $lhs, $rhs, Matrix<T, 1, M>, V: Index<usize, Output = T>, const A: usize, const B: usize, const C: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_matmul_vec {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(matvec, $lhs, $rhs, Matrix<T, M, 1>, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_matmul_vec_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(matvec, $lhs, $rhs, Matrix<T, M, 1>, V: Index<usize, Output = T>, const A: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_matmul_matvec {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecvec, $lhs, $rhs, Matrix<T, M, N>, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_matmul_matvec_vec_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecvec, $lhs, $rhs, Matrix<T, M, N>, V: Index<usize, Output = T>, const A: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_matmul_matvec_mat_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecvec, $lhs, $rhs, Matrix<T, M, N>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_matmul_matvec_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(vecvec, $lhs, $rhs, Matrix<T, M, N>, V: Index<usize, Output = T>, const A: usize, const B: usize, const C: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_matmul_mat_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(matvec, $lhs, $rhs, Matrix<T, M, 1>, const A: usize, const B: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_matmul_mat_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(matvec, $lhs, $rhs, Matrix<T, M, 1>, V: Index<usize, Output = T>, const A: usize, const B: usize, const C: usize, const M: usize, const N: usize);
                };
            }

            macro_rules! impl_matmul {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(matmat, $lhs, $rhs, Matrix<T, M, P>, const M: usize, const N: usize, const P: usize);
                };
            }

            macro_rules! impl_matmul_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(matmat, $lhs, $rhs, Matrix<T, M, P>, const A: usize, const B: usize, const M: usize, const N: usize, const P: usize);
                };
            }

            macro_rules! impl_matmul_view_view {
                ($lhs:ty, $rhs:ty) => {
                    impl_matmul_combinations!(matmat, $lhs, $rhs, Matrix<T, M, P>, const A: usize, const B: usize, const C: usize, const D: usize, const M: usize, const N: usize, const P: usize);
                };
            }
        }
    }
}