leto 0.46.2

N-dimensional strided arrays, zero-copy views, and storage backends
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
//! Fixed-size f64 linear algebra: the 2x2 and 3x3 `FixedMatrix`
//! symmetric eigendecompositions and inverses.
//!
//! Split from the parent module so the stack-backed types and their
//! elementwise operators stay one concern and the analytic linear algebra
//! another; the inherent methods remain on the same public types.

use super::{FixedMatrix, FixedVector};
use crate::geometry::Vector3;

impl FixedMatrix<f64, 3, 3> {
    /// Create a 3x3 matrix from row-major storage.
    pub const fn from_row_major(data: [f64; 9]) -> Self {
        Self::from_rows([
            [data[0], data[1], data[2]],
            [data[3], data[4], data[5]],
            [data[6], data[7], data[8]],
        ])
    }

    /// Create a 3x3 matrix from column-major storage.
    pub const fn from_column_major(data: [f64; 9]) -> Self {
        Self::from_rows([
            [data[0], data[3], data[6]],
            [data[1], data[4], data[7]],
            [data[2], data[5], data[8]],
        ])
    }

    /// Return the matrix entries in row-major order.
    pub fn into_row_major(self) -> [f64; 9] {
        [
            self[(0, 0)],
            self[(0, 1)],
            self[(0, 2)],
            self[(1, 0)],
            self[(1, 1)],
            self[(1, 2)],
            self[(2, 0)],
            self[(2, 1)],
            self[(2, 2)],
        ]
    }

    /// Return the matrix entries in column-major order.
    pub fn into_column_major(self) -> [f64; 9] {
        [
            self[(0, 0)],
            self[(1, 0)],
            self[(2, 0)],
            self[(0, 1)],
            self[(1, 1)],
            self[(2, 1)],
            self[(0, 2)],
            self[(1, 2)],
            self[(2, 2)],
        ]
    }

    /// Determinant of a 3x3 matrix.
    pub fn determinant(&self) -> f64 {
        let a = self[(0, 0)];
        let b = self[(0, 1)];
        let c = self[(0, 2)];
        let d = self[(1, 0)];
        let e = self[(1, 1)];
        let f = self[(1, 2)];
        let g = self[(2, 0)];
        let h = self[(2, 1)];
        let i = self[(2, 2)];

        a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g)
    }

    /// Eigendecomposition of a real symmetric 3x3 matrix.
    ///
    /// Uses the analytic cubic formula (trigonometric solution of the depressed
    /// cubic) for eigenvalues and the cross-product of `(A - λI)` columns for
    /// eigenvectors. Eigenvalues are sorted descending; eigenvectors are
    /// orthonormal columns of the returned matrix.
    pub fn symmetric_eigen(&self) -> (FixedVector<f64, 3>, Self) {
        let a = self[(0, 0)];
        let b = self[(0, 1)];
        let c = self[(0, 2)];
        let _d = self[(1, 0)];
        let e = self[(1, 1)];
        let f = self[(1, 2)];
        let _g = self[(2, 0)];
        let _h = self[(2, 1)];
        let i = self[(2, 2)];

        // Characteristic polynomial: λ³ - I₁λ² + I₂λ - I₃ = 0
        let i1 = a + e + i;
        let i2 = a * e + a * i + e * i - b * b - c * c - f * f;
        let i3 = a * e * i + 2.0 * b * c * f - a * f * f - e * c * c - i * b * b;

        // Depressed cubic: μ³ + pμ + q = 0,  λ = μ + I₁/3, obtained by
        // substituting λ = μ + I₁/3 into λ³ − I₁λ² + I₂λ − I₃ = 0.
        let p = i2 - i1 * i1 / 3.0;
        let q = (-2.0 * i1 * i1 * i1 + 9.0 * i1 * i2 - 27.0 * i3) / 27.0;
        let shift = i1 / 3.0;

        if p.abs() < 1e-30 {
            // Isotropic / nearly-equal eigenvalues: identity eigenvectors
            let val = shift;
            return (FixedVector::new([val, val, val]), Self::identity());
        }

        // Trigonometric solution:  μₖ = 2√(-p/3) cos(θ/3 + 2πk/3)
        let sqrt_neg_p3 = (-p / 3.0).sqrt();
        let r = 2.0 * sqrt_neg_p3;
        let cos_arg = (-q / 2.0) / (sqrt_neg_p3 * sqrt_neg_p3 * sqrt_neg_p3);
        let theta = cos_arg.clamp(-1.0, 1.0).acos() / 3.0;

        let two_pi_3 = 2.0943951023931953;
        let four_pi_3 = 4.1887902047863905;

        // Unsorted:  k=0 (θ) largest,  k=2 (θ+4π/3) middle,  k=1 (θ+2π/3) smallest
        let mut vals = [
            r * theta.cos() + shift,
            r * (theta + two_pi_3).cos() + shift,
            r * (theta + four_pi_3).cos() + shift,
        ];

        // Sort descending
        if vals[0] < vals[1] {
            vals.swap(0, 1);
        }
        if vals[0] < vals[2] {
            vals.swap(0, 2);
        }
        if vals[1] < vals[2] {
            vals.swap(1, 2);
        }

        let ev0 = eigenvector_3x3(a, b, c, e, f, i, vals[0]);
        let mut ev1 = eigenvector_3x3(a, b, c, e, f, i, vals[1]);

        // Orthogonalize ev1 against ev0 (the most well-conditioned direction)
        let dot01 = ev0[0] * ev1[0] + ev0[1] * ev1[1] + ev0[2] * ev1[2];
        ev1 = [
            ev1[0] - dot01 * ev0[0],
            ev1[1] - dot01 * ev0[1],
            ev1[2] - dot01 * ev0[2],
        ];
        normalize3(&mut ev1);

        // Third eigenvector = cross of first two → guaranteed orthonormal + right-handed
        let ev2 = cross3(ev0, ev1);

        let eigenvectors = Self::from_rows([
            [ev0[0], ev1[0], ev2[0]],
            [ev0[1], ev1[1], ev2[1]],
            [ev0[2], ev1[2], ev2[2]],
        ]);

        (FixedVector::new(vals), eigenvectors)
    }

    /// Inverse of a 3x3 matrix using the analytic cofactor formula.
    ///
    /// Returns `None` when the matrix is singular (determinant ≈ 0).
    pub fn try_inverse(&self) -> Option<Self> {
        let a = self[(0, 0)];
        let b = self[(0, 1)];
        let c = self[(0, 2)];
        let d = self[(1, 0)];
        let e = self[(1, 1)];
        let f = self[(1, 2)];
        let g = self[(2, 0)];
        let h = self[(2, 1)];
        let i = self[(2, 2)];

        let det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
        if det == 0.0 {
            return None;
        }

        let inv_det = 1.0 / det;
        Some(Self::from_rows([
            [
                (e * i - f * h) * inv_det,
                (c * h - b * i) * inv_det,
                (b * f - c * e) * inv_det,
            ],
            [
                (f * g - d * i) * inv_det,
                (a * i - c * g) * inv_det,
                (c * d - a * f) * inv_det,
            ],
            [
                (d * h - e * g) * inv_det,
                (b * g - a * h) * inv_det,
                (a * e - b * d) * inv_det,
            ],
        ]))
    }
}

impl FixedMatrix<f64, 2, 2> {
    /// Determinant of a 2x2 matrix.
    pub fn determinant(&self) -> f64 {
        self[(0, 0)] * self[(1, 1)] - self[(0, 1)] * self[(1, 0)]
    }

    /// Eigendecomposition of a real symmetric 2x2 matrix.
    ///
    /// Uses the closed-form quadratic formula. Eigenvalues are sorted
    /// descending; eigenvectors are orthonormal columns.
    pub fn symmetric_eigen(&self) -> (FixedVector<f64, 2>, Self) {
        let a = self[(0, 0)];
        let b = self[(0, 1)];
        let _c = self[(1, 0)];
        let d = self[(1, 1)];

        // Eigenvalues of [a b; b d]:  λ = ½[(a+d) ± √((a-d)² + 4b²)]
        let trace = a + d;
        let disc = ((a - d) * (a - d) + 4.0 * b * b).sqrt();
        let lambda1 = (trace + disc) / 2.0;
        let lambda2 = (trace - disc) / 2.0;

        // Eigenvector for λ₁ (larger eigenvalue)
        let (ev0, ev1) = if b.abs() > 1e-30 {
            let v0 = [b, lambda1 - a];
            let norm = (v0[0] * v0[0] + v0[1] * v0[1]).sqrt();
            ([v0[0] / norm, v0[1] / norm], [v0[1] / norm, -v0[0] / norm])
        } else {
            // Diagonal: eigenvectors are standard basis, sorted by value
            if a >= d {
                ([1.0, 0.0], [0.0, 1.0])
            } else {
                ([0.0, 1.0], [1.0, 0.0])
            }
        };

        let eigenvectors = Self::from_rows([[ev0[0], ev1[0]], [ev0[1], ev1[1]]]);

        (FixedVector::new([lambda1, lambda2]), eigenvectors)
    }

    /// Inverse of a 2x2 matrix using cofactor formula.
    ///
    /// Returns `None` when the matrix is singular (determinant ≈ 0).
    pub fn try_inverse(&self) -> Option<Self> {
        let det = self.determinant();
        if det == 0.0 {
            return None;
        }
        let inv_det = 1.0 / det;
        Some(Self::from_rows([
            [self[(1, 1)] * inv_det, -self[(0, 1)] * inv_det],
            [-self[(1, 0)] * inv_det, self[(0, 0)] * inv_det],
        ]))
    }
}

// --- 3x3 symmetric eigendecomposition helpers --------------------------------

/// Cross-product of two 3-vectors, routed through [`Vector3::cross`].
#[inline]
fn cross3(u: [f64; 3], v: [f64; 3]) -> [f64; 3] {
    Vector3::new(u[0], u[1], u[2])
        .cross(Vector3::new(v[0], v[1], v[2]))
        .data
}

/// Normalize a 3-vector in place, routed through [`Vector3::normalize`].
///
/// A zero vector keeps the canonical `+x` direction the analytic eigenvector
/// construction relies on for its fully degenerate fallback, rather than the
/// `0 · ∞` a bare division by the zero norm would produce.
#[inline]
fn normalize3(v: &mut [f64; 3]) {
    let vector = Vector3::new(v[0], v[1], v[2]);
    let norm = vector.norm();
    if norm > 0.0 {
        *v = (vector * norm.recip()).data;
    } else {
        *v = [1.0, 0.0, 0.0];
    }
}

/// Compute the unit eigenvector of the 3x3 symmetric matrix `[a b c; b e f; c f i]`
/// for eigenvalue `λ` using the cross-product of two columns of `(A - λI)`.
///
/// Picks the pair whose cross-product has the largest norm for numerical
/// stability. Degenerate (repeated-eigenvalue) cases fall back to `[1,0,0]`.
#[inline]
fn eigenvector_3x3(a: f64, b: f64, c: f64, e: f64, f: f64, i: f64, lambda: f64) -> [f64; 3] {
    let col0 = [a - lambda, b, c];
    let col1 = [b, e - lambda, f];
    let col2 = [c, f, i - lambda];

    let v01 = cross3(col0, col1);
    let v12 = cross3(col1, col2);
    let v20 = cross3(col2, col0);

    let n01 = v01[0] * v01[0] + v01[1] * v01[1] + v01[2] * v01[2];
    let n12 = v12[0] * v12[0] + v12[1] * v12[1] + v12[2] * v12[2];
    let n20 = v20[0] * v20[0] + v20[1] * v20[1] + v20[2] * v20[2];

    let mut best = if n01 >= n12 && n01 >= n20 {
        v01
    } else if n12 >= n01 && n12 >= n20 {
        v12
    } else {
        v20
    };
    normalize3(&mut best);
    best
}

#[cfg(test)]
mod tests {
    use super::FixedMatrix;

    #[test]
    fn fixed_3x3_symmetric_eigen_matches_known_values() {
        let m = FixedMatrix::from_rows([[2.0, -1.0, 0.0], [-1.0, 2.0, -1.0], [0.0, -1.0, 2.0]]);
        let (vals, vecs) = m.symmetric_eigen();

        // Known eigenvalues: 2±√2, 2+√2≈3.4142, 2, 2-√2≈0.5858
        assert!((vals[0] - (2.0 + 2.0_f64.sqrt())).abs() < 1e-14);
        assert!((vals[1] - 2.0).abs() < 1e-14);
        assert!((vals[2] - (2.0 - 2.0_f64.sqrt())).abs() < 1e-14);

        // A*V = V*Λ (reconstruct and check)
        let lambda = FixedMatrix::from_rows([
            [vals[0], 0.0, 0.0],
            [0.0, vals[1], 0.0],
            [0.0, 0.0, vals[2]],
        ]);
        let av = m * vecs;
        let vd = vecs * lambda;
        for row in 0..3 {
            for col in 0..3 {
                assert!((av[(row, col)] - vd[(row, col)]).abs() < 1e-12);
            }
        }
    }

    #[test]
    fn fixed_3x3_symmetric_eigen_nonzero_depressed_constant() {
        // Regression: the `matches_known_values` matrix has eigenvalues symmetric
        // about their mean (q = 0 in the depressed cubic), which masks the sign of
        // the cubic's constant term. These matrices have q ≠ 0 and expose it.

        // Distinct eigenvalues (oracle from leto eigh, descending).
        let m = FixedMatrix::from_rows([[4.0, 1.0, 2.0], [1.0, 5.0, 3.0], [2.0, 3.0, 6.0]]);
        let (vals, _) = m.symmetric_eigen();
        assert!((vals[0] - 9.418_832_675_97).abs() < 1e-9);
        assert!((vals[1] - 3.386_770_156_608).abs() < 1e-9);
        assert!((vals[2] - 2.194_397_167_422).abs() < 1e-9);

        // Degenerate double root (isotropic-like): eigenvalues {3, 1, 1}, q ≠ 0.
        // At a double root the closed-form trig method is ill-conditioned:
        // cos_arg → ±1, and acos(1 − ε) ≈ √(2ε), so a rounding error ε ≈ machine
        // epsilon in cos_arg amplifies to O(√ε) ≈ 1e-8 in the repeated eigenvalue.
        let iso = FixedMatrix::from_rows([[2.0, 1.0, 0.0], [1.0, 2.0, 0.0], [0.0, 0.0, 1.0]]);
        let (vals, _) = iso.symmetric_eigen();
        assert!((vals[0] - 3.0).abs() < 1e-9);
        assert!((vals[1] - 1.0).abs() < 1e-7);
        assert!((vals[2] - 1.0).abs() < 1e-7);
    }

    #[test]
    fn fixed_3x3_symmetric_eigen_eigenvectors_are_orthonormal() {
        let m = FixedMatrix::from_rows([[4.0, 1.0, 2.0], [1.0, 5.0, 3.0], [2.0, 3.0, 6.0]]);
        let (_, vecs) = m.symmetric_eigen();

        // Columns should be orthonormal: V^T V = I
        let vt = vecs.transpose();
        let product = vt * vecs;
        for row in 0..3 {
            for col in 0..3 {
                let expected = if row == col { 1.0 } else { 0.0 };
                assert!((product[(row, col)] - expected).abs() < 1e-12);
            }
        }
    }

    #[test]
    fn fixed_3x3_symmetric_eigen_identity_matrix() {
        let m: FixedMatrix<f64, 3, 3> = FixedMatrix::identity();
        let (vals, vecs) = m.symmetric_eigen();

        assert!((vals[0] - 1.0).abs() < 1e-15);
        assert!((vals[1] - 1.0).abs() < 1e-15);
        assert!((vals[2] - 1.0).abs() < 1e-15);

        let i: FixedMatrix<f64, 3, 3> = FixedMatrix::identity();
        for row in 0..3 {
            for col in 0..3 {
                assert!((vecs[(row, col)] - i[(row, col)]).abs() < 1e-15);
            }
        }
    }

    #[test]
    fn fixed_2x2_symmetric_eigen_matches_quadratic_formula() {
        let m = FixedMatrix::from_rows([[2.0, 1.0], [1.0, 2.0]]);
        let (vals, vecs) = m.symmetric_eigen();

        assert!((vals[0] - 3.0).abs() < 1e-15);
        assert!((vals[1] - 1.0).abs() < 1e-15);

        // A*V = V*Λ
        let lambda = FixedMatrix::from_rows([[vals[0], 0.0], [0.0, vals[1]]]);
        let av = m * vecs;
        let vd = vecs * lambda;
        for row in 0..2 {
            for col in 0..2 {
                assert!((av[(row, col)] - vd[(row, col)]).abs() < 1e-12);
            }
        }
    }

    #[test]
    fn fixed_2x2_symmetric_eigen_eigenvectors_are_orthonormal() {
        let m = FixedMatrix::from_rows([[3.0, 2.0], [2.0, 6.0]]);
        let (_, vecs) = m.symmetric_eigen();

        let vt = vecs.transpose();
        let product = vt * vecs;
        for row in 0..2 {
            for col in 0..2 {
                let expected = if row == col { 1.0 } else { 0.0 };
                assert!((product[(row, col)] - expected).abs() < 1e-12);
            }
        }
    }
}