vqm 0.1.8

A vector, quaternion, and matrix library targeted at embedded systems and robotics.
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
#![allow(clippy::inline_always)]
use cfg_if::cfg_if;

cfg_if! {
    if #[cfg(feature = "simd")] {
        use core::simd::{f32x4,f32x8,num::SimdFloat};
        // must be aligned if using SIMD
        const _: () = assert!(core::mem::size_of::<Matrix3x3<f32>>() == 64);
        const _: () = assert!(core::mem::align_of::<Matrix3x3<f32>>() == 64);
    } else if #[cfg(feature = "no_align")] {
        const _: () = assert!(core::mem::size_of::<Matrix3x3<f32>>() == 36);
        const _: () = assert!(core::mem::align_of::<Matrix3x3<f32>>() == 4);
    } else {
        const _: () = assert!(core::mem::size_of::<Matrix3x3<f32>>() == 64);
        const _: () = assert!(core::mem::align_of::<Matrix3x3<f32>>() == 64);
    }
}

use crate::{Matrix3x3, Vector3d};

// **** Math ****

/// Math functions for Matrix3x3, using **SIMD** accelerations for `f32`.<br>
pub trait Matrix3x3Math: Sized {
    fn m3x3_neg(this: Matrix3x3<Self>) -> Matrix3x3<Self>;
    fn m3x3_abs(this: Matrix3x3<Self>) -> Matrix3x3<Self>;
    fn m3x3_add(this: Matrix3x3<Self>, this: Matrix3x3<Self>) -> Matrix3x3<Self>;
    fn m3x3_mul_scalar(this: Matrix3x3<Self>, other: Self) -> Matrix3x3<Self>;
    fn m3x3_div_scalar(this: Matrix3x3<Self>, other: Self) -> Matrix3x3<Self>;
    fn m3x3_mul_add(this: Matrix3x3<Self>, k: Self, other: Matrix3x3<Self>) -> Matrix3x3<Self>;
    fn m3x3_mul_vector(this: Matrix3x3<Self>, other: Vector3d<Self>) -> Vector3d<Self>;
    fn m3x3_vector_mul(this: Vector3d<Self>, other: Matrix3x3<Self>) -> Vector3d<Self>;
    fn m3x3_vector_outer_product(col: Vector3d<Self>, row: Vector3d<Self>) -> Matrix3x3<Self>;
    fn m3x3_mul(this: Matrix3x3<Self>, other: Matrix3x3<Self>) -> Matrix3x3<Self>;
    fn m3x3_trace(this: Matrix3x3<Self>) -> Self;
    fn m3x3_trace_sum_squares(this: Matrix3x3<Self>) -> Self;
    fn m3x3_sum(this: Matrix3x3<Self>) -> Self;
    fn m3x3_mean(this: Matrix3x3<Self>) -> Self;
    fn m3x3_product(this: Matrix3x3<Self>) -> Self;
    fn m3x3_top_right_determinant(this: Matrix3x3<Self>) -> Self;
    fn m3x3_top_right_sum_squares(this: Matrix3x3<Self>) -> Self;
    fn m3x3_determinant(this: Matrix3x3<Self>) -> Self;
    fn m3x3_adjugate(this: Matrix3x3<Self>) -> (Matrix3x3<Self>, Self);
}

impl Matrix3x3Math for f32 {
    #[inline(always)]
    fn m3x3_neg(this: Matrix3x3<Self>) -> Matrix3x3<Self> {
        let ret = core::array::from_fn(|ii| -this.a[ii]);
        Matrix3x3::from(ret)
    }

    #[inline(always)]
    fn m3x3_abs(this: Matrix3x3<Self>) -> Matrix3x3<Self> {
        let ret = core::array::from_fn(|ii| this.a[ii].abs());
        Matrix3x3::from(ret)
    }

    #[inline(always)]
    fn m3x3_add(this: Matrix3x3<Self>, other: Matrix3x3<Self>) -> Matrix3x3<Self> {
        let ret = core::array::from_fn(|ii| this.a[ii] + other.a[ii]);
        Matrix3x3::from(ret)
    }

    #[inline(always)]
    fn m3x3_mul_scalar(this: Matrix3x3<Self>, other: Self) -> Matrix3x3<Self> {
        let ret = core::array::from_fn(|ii| this.a[ii] * other);
        Matrix3x3::from(ret)
    }

    #[inline(always)]
    fn m3x3_div_scalar(this: Matrix3x3<Self>, other: Self) -> Matrix3x3<Self> {
        Self::m3x3_mul_scalar(this, 1.0 / other)
    }

    #[inline(always)]
    fn m3x3_mul_add(this: Matrix3x3<Self>, k: Self, other: Matrix3x3<Self>) -> Matrix3x3<Self> {
        Self::m3x3_add(Self::m3x3_mul_scalar(this, k), other)
    }

    /*#[inline]
    fn m3x3_mul_vector(this: Matrix3x3<Self>, other: Vector3d<Self>) -> Vector3d<Self> {
        Vector3d {
            x: this.a[0] * other.x + this.a[1] * other.y + this.a[2] * other.z,
            y: this.a[3] * other.x + this.a[4] * other.y + this.a[5] * other.z,
            z: this.a[6] * other.x + this.a[7] * other.y + this.a[8] * other.z,
        }
    }*/

    #[inline]
    fn m3x3_mul_vector(this: Matrix3x3<Self>, other: Vector3d<Self>) -> Vector3d<Self> {
        // Map the 16-byte aligned vector into a uniform 4-element array.
        // The 4th element is zeroed out so it contributes nothing to the dot products.
        let v = [other.x, other.y, other.z, 0.0];

        // Unpack the flat matrix into 4-element padded rows.
        let r1 = [this.a[0], this.a[1], this.a[2], 0.0];
        let r2 = [this.a[3], this.a[4], this.a[5], 0.0];
        let r3 = [this.a[6], this.a[7], this.a[8], 0.0];

        // Compute row dot products using unrolled, 4-wide loops.
        // LLVM easily vectorizes a simple element-wise multiply-and-accumulate loop
        // spanning exactly 4 items, mapping it directly to hardware registers.
        let mut x = 0.0;
        let mut y = 0.0;
        let mut z = 0.0;

        for ii in 0..4 {
            x += r1[ii] * v[ii];
        }
        for ii in 0..4 {
            y += r2[ii] * v[ii];
        }
        for ii in 0..4 {
            z += r3[ii] * v[ii];
        }

        // Return the new vector
        Vector3d { x, y, z }
    }
    #[rustfmt::skip]
    #[inline]
    fn m3x3_vector_mul(this: Vector3d<Self>, other: Matrix3x3<Self>) -> Vector3d<Self> {
        Vector3d {
            x: this.x * other.a[0] + this.y * other.a[3] + this.z * other.a[6],
            y: this.x * other.a[1] + this.y * other.a[4] + this.z * other.a[7],
            z: this.x * other.a[2] + this.y * other.a[5] + this.z * other.a[8],
        }
    }

    #[rustfmt::skip]
    #[inline(always)]
    fn m3x3_vector_outer_product(col: Vector3d<Self>, row: Vector3d<Self>) -> Matrix3x3<Self> {
        #[cfg(feature = "simd")]
        {
            // By taking ownership of the value, Rust guarantees no other pointer
            // can modify these values during our calculation loop.
            // let row_simd = unsafe { *(&row as *const Vector3df32 as *const f32x4) };
            let row_simd = f32x4::from_array([row.x, row.y, row.z, 0.0]);

            let col_x = f32x4::splat(col.x);
            let col_y = f32x4::splat(col.y);
            let col_z = f32x4::splat(col.z);

            let r1 = col_x * row_simd;
            let r2 = col_y * row_simd;
            let r3 = col_z * row_simd;

            Matrix3x3::from([r1.to_array(), r2.to_array(), r3.to_array()])
        }
        #[cfg(not(feature = "simd"))]
        {
            // Structure data into local fixed-size arrays of 4 elements.
            // Since row is align(16), we manually map the implicit 4th buffer element.
            let r = [row.x, row.y, row.z, 0.0];

            let mut m1 = [0.0; 4];
            let mut m2 = [0.0; 4];
            let mut m3 = [0.0; 4];

            // Write uniform loops spanning exactly 4 elements.
            // LLVM's auto-vectorizer recognizes 4-wide float operations
            // and combines these into parallel execution blocks, if the processor supports it.
            for ii in 0..4 {
                m1[ii] = col.x * r[ii];
            }
            for ii in 0..4 {
                m2[ii] = col.y * r[ii];
            }
            for ii in 0..4 {
                m3[ii] = col.z * r[ii];
            }

            // Populate the matrix, discarding the 4th padding lane.
            Matrix3x3::from([
                m1[0], m1[1], m1[2],
                m2[0], m2[1], m2[2],
                m3[0], m3[1], m3[2],
            ])
        }
    }

    #[inline(always)]
    fn m3x3_mul(this: Matrix3x3<Self>, other: Matrix3x3<Self>) -> Matrix3x3<Self> {
        #[cfg(feature = "simd")]
        {
            let a0_simd = f32x4::from_array([this.a[0], this.a[1], this.a[2], 0.0]);
            let a3_simd = f32x4::from_array([this.a[3], this.a[4], this.a[5], 0.0]);
            let a6_simd = f32x4::from_array([this.a[6], this.a[7], this.a[8], 0.0]);
            let b0_simd = f32x4::from_array([other.a[0], other.a[3], other.a[6], 0.0]);
            let b1_simd = f32x4::from_array([other.a[1], other.a[4], other.a[7], 0.0]);
            let b2_simd = f32x4::from_array([other.a[2], other.a[5], other.a[8], 0.0]);
            let a = [
                (a0_simd * b0_simd).reduce_sum(),
                (a0_simd * b1_simd).reduce_sum(),
                (a0_simd * b2_simd).reduce_sum(),
                (a3_simd * b0_simd).reduce_sum(),
                (a3_simd * b1_simd).reduce_sum(),
                (a3_simd * b2_simd).reduce_sum(),
                (a6_simd * b0_simd).reduce_sum(),
                (a6_simd * b1_simd).reduce_sum(),
                (a6_simd * b2_simd).reduce_sum(),
            ];
            Matrix3x3::from(a)
        }
        #[cfg(not(feature = "simd"))]
        {
            let a = [
                this.a[0] * other.a[0] + this.a[1] * other.a[3] + this.a[2] * other.a[6],
                this.a[0] * other.a[1] + this.a[1] * other.a[4] + this.a[2] * other.a[7],
                this.a[0] * other.a[2] + this.a[1] * other.a[5] + this.a[2] * other.a[8],
                this.a[3] * other.a[0] + this.a[4] * other.a[3] + this.a[5] * other.a[6],
                this.a[3] * other.a[1] + this.a[4] * other.a[4] + this.a[5] * other.a[7],
                this.a[3] * other.a[2] + this.a[4] * other.a[5] + this.a[5] * other.a[8],
                this.a[6] * other.a[0] + this.a[7] * other.a[3] + this.a[8] * other.a[6],
                this.a[6] * other.a[1] + this.a[7] * other.a[4] + this.a[8] * other.a[7],
                this.a[6] * other.a[2] + this.a[7] * other.a[5] + this.a[8] * other.a[8],
            ];
            Matrix3x3::from(a)
        }
    }

    #[inline(always)]
    fn m3x3_trace(this: Matrix3x3<Self>) -> Self {
        this.a[0] + this.a[4] + this.a[8]
    }

    #[inline(always)]
    fn m3x3_trace_sum_squares(this: Matrix3x3<Self>) -> Self {
        #[cfg(feature = "simd")]
        {
            let trace_simd = f32x4::from_array([this.a[0], this.a[4], this.a[8], 0.0]);
            (trace_simd * trace_simd).reduce_sum()
        }
        #[cfg(not(feature = "simd"))]
        {
            this.a[0] * this.a[0] + this.a[4] * this.a[4] + this.a[8] * this.a[8]
        }
    }

    #[inline(always)]
    fn m3x3_sum(this: Matrix3x3<Self>) -> Self {
        this.a.iter().sum()
    }

    #[inline(always)]
    fn m3x3_mean(this: Matrix3x3<Self>) -> Self {
        this.sum() / 9.0
    }

    #[inline(always)]
    fn m3x3_product(this: Matrix3x3<Self>) -> Self {
        this.a.iter().product()
    }

    #[inline(always)]
    fn m3x3_top_right_sum_squares(this: Matrix3x3<Self>) -> Self {
        #[cfg(feature = "simd")]
        {
            let top_right_simd = f32x4::from_array([this.a[1], this.a[2], this.a[5], 0.0]);
            (top_right_simd * top_right_simd).reduce_sum()
        }
        #[cfg(not(feature = "simd"))]
        {
            this.a[1] * this.a[1] + this.a[2] * this.a[2] + this.a[5] * this.a[5]
        }
    }

    #[inline(always)]
    fn m3x3_top_right_determinant(this: Matrix3x3<Self>) -> Self {
        //let det_b = b00 * (b11 * b22 - b12 * b12) - b01 * (b01 * b22 - b12 * b02) + b02 * (b01 * b12 - b11 * b02);
        //            a0     a4    a8    a5    a5     a1     a1    a8    a5    a2     a2     a1    a5    a4    a2
        #[cfg(feature = "simd")]
        {
            let a_simd = f32x4::from_array([this.a[0], -this.a[1], this.a[2], 0.0]);

            let d = [
                this.a[4] * this.a[8] - this.a[5] * this.a[5],
                this.a[1] * this.a[8] - this.a[5] * this.a[2],
                this.a[1] * this.a[5] - this.a[4] * this.a[2],
                0.0,
            ];
            let d_simd = f32x4::from_array(d);

            (a_simd * d_simd).reduce_sum()
        }
        #[cfg(not(feature = "simd"))]
        {
            this.a[0] * (this.a[4] * this.a[8] - this.a[5] * this.a[5])
                - this.a[1] * (this.a[1] * this.a[8] - this.a[5] * this.a[2])
                + this.a[2] * (this.a[1] * this.a[5] - this.a[4] * this.a[2])
        }
    }

    #[rustfmt::skip]
    #[inline(always)]
    fn m3x3_determinant(this: Matrix3x3<Self>) -> Self {
        #[cfg(feature = "simd")]
        {
            let a_simd = f32x4::from_array([this.a[0], -this.a[1], this.a[2], 0.0]);

            let d = [
                this.a[4] * this.a[8] - this.a[5] * this.a[7],
                this.a[3] * this.a[8] - this.a[5] * this.a[6],
                this.a[3] * this.a[7] - this.a[4] * this.a[6],
                0.0,
            ];
            let d_simd = f32x4::from_array(d);

            (a_simd * d_simd).reduce_sum()
        }
        #[cfg(not(feature = "simd"))]
        {
             this.a[0] * (this.a[4] * this.a[8] - this.a[5] * this.a[7])
            -this.a[1] * (this.a[3] * this.a[8] - this.a[5] * this.a[6])
            +this.a[2] * (this.a[3] * this.a[7] - this.a[4] * this.a[6])
        }
    }

    #[rustfmt::skip]
    #[inline]
    fn m3x3_adjugate(this: Matrix3x3<Self>) -> (Matrix3x3<Self>, Self) {
        /*#[cfg(feature = "simd")]
        {
            let a = this.a;

            // use SIMD to calculate the first 8 elements of the array, and then manually calculate the 9th element.
            // TODO: change the 4 from_arrays into 2 from_arrays and use swizzles.
            let r0a = [a[4], -a[1], a[1], -a[3], a[0], -a[0], a[3], -a[0]];
            let r0b = [a[8], a[8], a[5], a[8], a[8], a[5], a[7], a[7]];

            let r1a = [-a[5], a[2], -a[2], a[5], -a[2], a[2], -a[4], a[1]];
            let r1b = [a[7], a[7], a[4], a[6], a[6], a[3], a[6], a[6]];

            let r0a_simd = f32x8::from_array(r0a);
            let r0b_simd = f32x8::from_array(r0b);

            let r1a_simd = f32x8::from_array(r1a);
            let r1b_simd = f32x8::from_array(r1b);

            let r0_simd = r0a_simd * r0b_simd;
            let r1_simd = r1a_simd * r1b_simd;

            let r: [f32; 8] = (r0_simd + r1_simd).into();

            Matrix3x3::from([r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], a[0] * a[4] - a[1] * a[3]])
        }
        #[cfg(not(feature = "simd"))]*/
        let ei_fh = this.a[4] * this.a[8] - this.a[5] * this.a[7];
        let di_fg = this.a[3] * this.a[8] - this.a[5] * this.a[6];
        let dh_eg = this.a[3] * this.a[7] - this.a[4] * this.a[6];
        let determinant = this.a[0] * ei_fh - this.a[1]*di_fg + this.a[2]* dh_eg;

        let a = [
              ei_fh,                                          //  (e*i - f*h)
            -(this.a[1] * this.a[8] - this.a[2] * this.a[7]), // -(b*i - c*h)
              this.a[1] * this.a[5] - this.a[2] * this.a[4],  //  (b*f - c*e)
            - di_fg,                                          // -(d*i - f*g)
              this.a[0] * this.a[8] - this.a[2] * this.a[6],  //  (a*i - c*g)
            -(this.a[0] * this.a[5] - this.a[2] * this.a[3]), // -(a*f - c*d)
              dh_eg,                                          //  (d*h - e*g)
            -(this.a[0] * this.a[7] - this.a[1] * this.a[6]), // -(a*h - b*g)
              this.a[0] * this.a[4] - this.a[1] * this.a[3],  //  (a*e - b*d)
        ];
        (Matrix3x3::from(a), determinant)
    }
}

// **** f64 ****

impl Matrix3x3Math for f64 {
    #[inline(always)]
    fn m3x3_neg(this: Matrix3x3<Self>) -> Matrix3x3<Self> {
        let mut a = this.a;
        for r in &mut a {
            *r = -*r;
        }
        Matrix3x3::from(a)
    }

    #[inline(always)]
    fn m3x3_abs(this: Matrix3x3<Self>) -> Matrix3x3<Self> {
        let mut a = this.a;
        for r in &mut a {
            *r = r.abs();
        }
        Matrix3x3::from(a)
    }

    #[inline(always)]
    fn m3x3_add(this: Matrix3x3<Self>, other: Matrix3x3<Self>) -> Matrix3x3<Self> {
        let mut a = this.a;
        for (ii, r) in a.iter_mut().enumerate() {
            *r += other.a[ii];
        }
        Matrix3x3::from(a)
    }

    #[inline(always)]
    fn m3x3_mul_scalar(this: Matrix3x3<Self>, other: Self) -> Matrix3x3<Self> {
        let mut a = this.a;
        for r in &mut a {
            *r *= other;
        }
        Matrix3x3::from(a)
    }

    #[inline(always)]
    fn m3x3_div_scalar(this: Matrix3x3<Self>, other: Self) -> Matrix3x3<Self> {
        Self::m3x3_mul_scalar(this, 1.0 / other)
    }

    #[inline(always)]
    fn m3x3_mul_add(this: Matrix3x3<Self>, k: Self, other: Matrix3x3<Self>) -> Matrix3x3<Self> {
        Self::m3x3_add(Self::m3x3_mul_scalar(this, k), other)
    }

    #[inline(always)]
    fn m3x3_mul_vector(this: Matrix3x3<Self>, other: Vector3d<Self>) -> Vector3d<Self> {
        // Map the 16-byte aligned vector into a uniform 4-element array.
        // The 4th element is zeroed out so it contributes nothing to the dot products.
        let v = [other.x, other.y, other.z, 0.0];

        // Unpack the flat matrix into 4-element padded rows.
        let r1 = [this.a[0], this.a[1], this.a[2], 0.0];
        let r2 = [this.a[3], this.a[4], this.a[5], 0.0];
        let r3 = [this.a[6], this.a[7], this.a[8], 0.0];

        // Compute row dot products using unrolled, 4-wide loops.
        // LLVM easily vectorizes a simple element-wise multiply-and-accumulate loop
        // spanning exactly 4 items, mapping it directly to hardware registers.
        let mut x = 0.0;
        let mut y = 0.0;
        let mut z = 0.0;

        for ii in 0..4 {
            x += r1[ii] * v[ii];
        }
        for ii in 0..4 {
            y += r2[ii] * v[ii];
        }
        for ii in 0..4 {
            z += r3[ii] * v[ii];
        }

        // Return the new vector
        Vector3d { x, y, z }
    }

    #[inline(always)]
    fn m3x3_vector_mul(this: Vector3d<Self>, other: Matrix3x3<Self>) -> Vector3d<Self> {
        Vector3d {
            x: this.x * other.a[0] + this.y * other.a[3] + this.z * other.a[6],
            y: this.x * other.a[1] + this.y * other.a[4] + this.z * other.a[7],
            z: this.x * other.a[2] + this.y * other.a[5] + this.z * other.a[8],
        }
    }

    #[rustfmt::skip]
    #[inline(always)]
    fn m3x3_vector_outer_product(col: Vector3d<Self>, row: Vector3d<Self>) -> Matrix3x3<Self> {
        // Structure data into local fixed-size arrays of 4 elements.
        // Since row is align(16), we manually map the implicit 4th buffer element.
        let r = [row.x, row.y, row.z, 0.0];

        let mut m1 = [0.0; 4];
        let mut m2 = [0.0; 4];
        let mut m3 = [0.0; 4];

        // Write uniform loops spanning exactly 4 elements.
        // LLVM's auto-vectorizer recognizes 4-wide float operations
        // and combines these into parallel execution blocks, if the processor supports it.
        for ii in 0..4 {
            m1[ii] = col.x * r[ii];
        }
        for ii in 0..4 {
            m2[ii] = col.y * r[ii];
        }
        for ii in 0..4 {
            m3[ii] = col.z * r[ii];
        }

        // Populate the matrix, discarding the 4th padding lane.
        Matrix3x3::from([
            m1[0], m1[1], m1[2],
            m2[0], m2[1], m2[2],
            m3[0], m3[1], m3[2],
        ])
    }

    #[inline(always)]
    fn m3x3_mul(this: Matrix3x3<Self>, other: Matrix3x3<Self>) -> Matrix3x3<Self> {
        let a = [
            this.a[0] * other.a[0] + this.a[1] * other.a[3] + this.a[2] * other.a[6],
            this.a[0] * other.a[1] + this.a[1] * other.a[4] + this.a[2] * other.a[7],
            this.a[0] * other.a[2] + this.a[1] * other.a[5] + this.a[2] * other.a[8],
            this.a[3] * other.a[0] + this.a[4] * other.a[3] + this.a[5] * other.a[6],
            this.a[3] * other.a[1] + this.a[4] * other.a[4] + this.a[5] * other.a[7],
            this.a[3] * other.a[2] + this.a[4] * other.a[5] + this.a[5] * other.a[8],
            this.a[6] * other.a[0] + this.a[7] * other.a[3] + this.a[8] * other.a[6],
            this.a[6] * other.a[1] + this.a[7] * other.a[4] + this.a[8] * other.a[7],
            this.a[6] * other.a[2] + this.a[7] * other.a[5] + this.a[8] * other.a[8],
        ];
        Matrix3x3::from(a)
    }

    #[inline(always)]
    fn m3x3_trace(this: Matrix3x3<Self>) -> Self {
        this.a[0] + this.a[4] + this.a[8]
    }

    #[inline(always)]
    fn m3x3_trace_sum_squares(this: Matrix3x3<Self>) -> Self {
        this.a[0] * this.a[0] + this.a[4] * this.a[4] + this.a[8] * this.a[8]
    }

    #[inline(always)]
    fn m3x3_sum(this: Matrix3x3<Self>) -> Self {
        this.a.iter().sum()
    }

    #[inline(always)]
    fn m3x3_mean(this: Matrix3x3<Self>) -> Self {
        this.sum() / 9.0
    }

    #[inline(always)]
    fn m3x3_product(this: Matrix3x3<Self>) -> Self {
        this.a.iter().product()
    }

    #[inline(always)]
    fn m3x3_top_right_sum_squares(this: Matrix3x3<Self>) -> Self {
        this.a[1] * this.a[1] + this.a[2] * this.a[2] + this.a[5] * this.a[5]
    }

    #[rustfmt::skip]
    #[inline(always)]
    fn m3x3_top_right_determinant(this: Matrix3x3<Self>) -> Self {
        //let det_b = b00 * (b11 * b22 - b12 * b12) - b01 * (b01 * b22 - b12 * b02) + b02 * (b01 * b12 - b11 * b02);
        //             0     4     8     5      5      1      1     8    5     2        2     1    5      4     2
          this.a[0] * (this.a[4] * this.a[8] - this.a[5] * this.a[5])
        - this.a[1] * (this.a[1] * this.a[8] - this.a[5] * this.a[2])
        + this.a[2] * (this.a[1] * this.a[5] - this.a[4] * this.a[2])
    }

    #[rustfmt::skip]
    #[inline(always)]
    fn m3x3_determinant(this: Matrix3x3<Self>) -> Self {
         this.a[0] * (this.a[4] * this.a[8] - this.a[5] * this.a[7])
        -this.a[1] * (this.a[3] * this.a[8] - this.a[5] * this.a[6])
        +this.a[2] * (this.a[3] * this.a[7] - this.a[4] * this.a[6])
    }

    #[rustfmt::skip]
    #[inline(always)]
    fn m3x3_adjugate(this: Matrix3x3<Self>) -> (Matrix3x3<Self>, Self) {
        let ei_fh = this.a[4] * this.a[8] - this.a[5] * this.a[7];
        let di_fg = this.a[3] * this.a[8] - this.a[5] * this.a[6];
        let dh_eg = this.a[3] * this.a[7] - this.a[4] * this.a[6];
        let determinant = this.a[0] * ei_fh - this.a[1]*di_fg + this.a[2]* dh_eg;

        let a = [
              ei_fh,                                          //  (e*i - f*h)
            -(this.a[1] * this.a[8] - this.a[2] * this.a[7]), // -(b*i - c*h)
              this.a[1] * this.a[5] - this.a[2] * this.a[4],  //  (b*f - c*e)
            - di_fg,                                          // -(d*i - f*g)
              this.a[0] * this.a[8] - this.a[2] * this.a[6],  //  (a*i - c*g)
            -(this.a[0] * this.a[5] - this.a[2] * this.a[3]), // -(a*f - c*d)
              dh_eg,                                          //  (d*h - e*g)
            -(this.a[0] * this.a[7] - this.a[1] * this.a[6]), // -(a*h - b*g)
              this.a[0] * this.a[4] - this.a[1] * this.a[3],  //  (a*e - b*d)
        ];
        (Matrix3x3::from(a), determinant)
    }
}