numr 0.5.1

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
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
//! Core linear algebra algorithm trait
//!
//! Defines the mathematical contract that all backends must implement
//! for consistent linear algebra operations.

use super::super::MatrixNormOrder;
use super::super::decompositions::*;
use crate::error::{Error, Result};
use crate::runtime::Runtime;
use crate::tensor::Tensor;

/// Algorithmic contract for linear algebra operations
///
/// All backends implementing linear algebra MUST implement this trait
/// using the EXACT SAME ALGORITHMS to ensure numerical parity.
///
/// # Implementation Requirements
///
/// Backends may differ in:
/// - Parallelization strategy (threads, SIMD, GPU blocks)
/// - Memory access patterns (blocking, tiling)
///
/// Backends MUST match in:
/// - Mathematical formula (same operations in same order)
/// - Pivot selection criteria (partial pivoting for LU)
/// - Special case handling (singular matrices, non-positive-definite)
pub trait LinearAlgebraAlgorithms<R: Runtime> {
    /// LU Decomposition with partial pivoting: PA = LU
    fn lu_decompose(&self, a: &Tensor<R>) -> Result<LuDecomposition<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::lu_decompose",
        })
    }

    /// Cholesky Decomposition: A = LL^T
    fn cholesky_decompose(&self, a: &Tensor<R>) -> Result<CholeskyDecomposition<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::cholesky_decompose",
        })
    }

    /// QR Decomposition using Householder reflections: A = QR
    fn qr_decompose(&self, a: &Tensor<R>) -> Result<QrDecomposition<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::qr_decompose",
        })
    }

    /// Thin QR Decomposition: A = QR where Q is `[m, k]` and R is `[k, n]`
    fn qr_decompose_thin(&self, a: &Tensor<R>) -> Result<QrDecomposition<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::qr_decompose_thin",
        })
    }

    /// Solve linear system Ax = b using LU decomposition
    fn solve(&self, a: &Tensor<R>, b: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = (a, b);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::solve",
        })
    }

    /// Solve triangular system Lx = b (forward substitution)
    fn solve_triangular_lower(
        &self,
        l: &Tensor<R>,
        b: &Tensor<R>,
        unit_diagonal: bool,
    ) -> Result<Tensor<R>> {
        let _ = (l, b, unit_diagonal);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::solve_triangular_lower",
        })
    }

    /// Solve triangular system Ux = b (backward substitution)
    fn solve_triangular_upper(&self, u: &Tensor<R>, b: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = (u, b);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::solve_triangular_upper",
        })
    }

    /// Least squares solution: minimize ||Ax - b||²
    fn lstsq(&self, a: &Tensor<R>, b: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = (a, b);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::lstsq",
        })
    }

    /// Matrix inverse using LU decomposition
    fn inverse(&self, a: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::inverse",
        })
    }

    /// Matrix determinant using LU decomposition
    fn det(&self, a: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::det",
        })
    }

    /// Matrix trace: sum of diagonal elements
    fn trace(&self, a: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::trace",
        })
    }

    /// Extract diagonal elements
    fn diag(&self, a: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::diag",
        })
    }

    /// Create diagonal matrix from 1D tensor
    fn diagflat(&self, a: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::diagflat",
        })
    }

    /// Kronecker product: A ⊗ B
    ///
    /// Computes the Kronecker product of two matrices. For A of shape [m, n]
    /// and B of shape [p, q], the result has shape [m*p, n*q].
    ///
    /// # Definition
    ///
    /// ```text
    /// (A ⊗ B)[i*p + k, j*q + l] = A[i, j] * B[k, l]
    /// ```
    ///
    /// Equivalently, the Kronecker product replaces each element a_ij of A
    /// with the block a_ij * B.
    ///
    /// # Properties
    ///
    /// - `(A ⊗ B) ⊗ C = A ⊗ (B ⊗ C)` (associative)
    /// - `A ⊗ (B + C) = A ⊗ B + A ⊗ C` (distributive)
    /// - `(A ⊗ B)^T = A^T ⊗ B^T`
    /// - `(A ⊗ B)(C ⊗ D) = (AC) ⊗ (BD)` (mixed-product property)
    /// - `det(A ⊗ B) = det(A)^q * det(B)^m` for square matrices
    ///
    /// # Use Cases
    ///
    /// - Quantum computing (tensor products of quantum states)
    /// - Control theory (Sylvester/Lyapunov equation solvers)
    /// - Signal processing (2D filtering)
    /// - Graph theory (graph products)
    fn kron(&self, a: &Tensor<R>, b: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = (a, b);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::kron",
        })
    }

    /// Upper triangular part of a matrix
    ///
    /// Returns a copy of the matrix with elements below the k-th diagonal zeroed.
    /// Supports all numeric dtypes (not just F32/F64).
    fn triu(&self, a: &Tensor<R>, diagonal: i64) -> Result<Tensor<R>> {
        let _ = (a, diagonal);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::triu",
        })
    }

    /// Lower triangular part of a matrix
    ///
    /// Returns a copy of the matrix with elements above the k-th diagonal zeroed.
    /// Supports all numeric dtypes (not just F32/F64).
    fn tril(&self, a: &Tensor<R>, diagonal: i64) -> Result<Tensor<R>> {
        let _ = (a, diagonal);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::tril",
        })
    }

    /// Sign and log-absolute-determinant
    ///
    /// Computes sign(det(A)) and log(|det(A)|) separately for numerical stability.
    fn slogdet(&self, a: &Tensor<R>) -> Result<SlogdetResult<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::slogdet",
        })
    }

    /// Solve banded linear system Ab*x = b
    ///
    /// Uses LAPACK-style band storage where `ab` has shape `[kl + ku + 1, n]`
    /// and `ab[ku + i - j, j] = A[i, j]` for `max(0, j-ku) <= i <= min(n-1, j+kl)`.
    ///
    /// For tridiagonal systems (kl=1, ku=1), uses Thomas algorithm O(n).
    /// For general banded systems, uses banded LU with partial pivoting.
    ///
    /// # Arguments
    ///
    /// * `ab` - Band matrix in LAPACK band storage `[kl + ku + 1, n]`
    /// * `b` - Right-hand side vector `[n]` or matrix `[n, nrhs]`
    /// * `kl` - Number of subdiagonals
    /// * `ku` - Number of superdiagonals
    fn solve_banded(
        &self,
        ab: &Tensor<R>,
        b: &Tensor<R>,
        kl: usize,
        ku: usize,
    ) -> Result<Tensor<R>> {
        let _ = (ab, b, kl, ku);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::solve_banded",
        })
    }

    /// Khatri-Rao product (column-wise Kronecker product): A ⊙ B
    ///
    /// Computes the column-wise Kronecker product of two matrices.
    /// For A of shape [m, k] and B of shape [n, k], the result has shape [m*n, k].
    ///
    /// # Definition
    ///
    /// ```text
    /// (A ⊙ B)[:, j] = A[:, j] ⊗ B[:, j]
    /// ```
    ///
    /// Each column of the result is the Kronecker product of the corresponding
    /// columns of A and B.
    ///
    /// # Properties
    ///
    /// - `(A ⊙ B)^T (A ⊙ B) = (A^T A) * (B^T B)` (Hadamard/element-wise product)
    /// - Essential for CP/PARAFAC tensor decomposition
    /// - Related to mode-n unfolding operations
    ///
    /// # Use Cases
    ///
    /// - CP/PARAFAC tensor decomposition (ALS updates)
    /// - Tucker decomposition
    /// - Multi-linear algebra
    /// - Compressed sensing
    fn khatri_rao(&self, a: &Tensor<R>, b: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = (a, b);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::khatri_rao",
        })
    }

    /// Matrix rank via SVD
    fn matrix_rank(&self, a: &Tensor<R>, tol: Option<f64>) -> Result<Tensor<R>> {
        let _ = (a, tol);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::matrix_rank",
        })
    }

    /// Matrix norm (Frobenius, Spectral, or Nuclear)
    fn matrix_norm(&self, a: &Tensor<R>, ord: MatrixNormOrder) -> Result<Tensor<R>> {
        let _ = (a, ord);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::matrix_norm",
        })
    }

    /// Singular Value Decomposition: A = U @ diag(S) @ V^T
    fn svd_decompose(&self, a: &Tensor<R>) -> Result<SvdDecomposition<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::svd_decompose",
        })
    }

    /// Eigendecomposition for symmetric matrices: A = V @ diag(λ) @ V^T
    fn eig_decompose_symmetric(&self, a: &Tensor<R>) -> Result<EigenDecomposition<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::eig_decompose_symmetric",
        })
    }

    /// General Eigendecomposition for non-symmetric matrices
    fn eig_decompose(&self, a: &Tensor<R>) -> Result<GeneralEigenDecomposition<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::eig_decompose",
        })
    }

    /// Schur Decomposition: A = Z @ T @ Z^T
    fn schur_decompose(&self, a: &Tensor<R>) -> Result<SchurDecomposition<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::schur_decompose",
        })
    }

    /// Moore-Penrose pseudo-inverse via SVD
    fn pinverse(&self, a: &Tensor<R>, rcond: Option<f64>) -> Result<Tensor<R>> {
        let _ = (a, rcond);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::pinverse",
        })
    }

    /// Matrix condition number via SVD
    fn cond(&self, a: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::cond",
        })
    }

    /// Covariance matrix
    fn cov(&self, a: &Tensor<R>, ddof: Option<usize>) -> Result<Tensor<R>> {
        let _ = (a, ddof);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::cov",
        })
    }

    /// Correlation coefficient matrix (Pearson correlation)
    fn corrcoef(&self, a: &Tensor<R>) -> Result<Tensor<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::corrcoef",
        })
    }

    /// Convert Real Schur form to Complex Schur form: rsf2csf
    ///
    /// Transforms the real Schur decomposition (with 2×2 blocks for complex
    /// eigenvalue pairs) into the complex Schur form where T is truly upper
    /// triangular with complex eigenvalues on the diagonal.
    ///
    /// # Algorithm
    ///
    /// For each 2×2 block in T corresponding to complex conjugate eigenvalues a ± bi:
    /// 1. Compute eigenvalues of the 2×2 block
    /// 2. Construct a 2×2 unitary rotation that diagonalizes the block
    /// 3. Apply the rotation to T and accumulate into Z
    ///
    /// # Input
    ///
    /// Takes the output of `schur_decompose` (real Schur form).
    ///
    /// # Output
    ///
    /// Returns complex Schur form where:
    /// - T is upper triangular (not quasi-triangular)
    /// - Eigenvalues appear on diagonal of T
    /// - Z is unitary
    fn rsf2csf(&self, schur: &SchurDecomposition<R>) -> Result<ComplexSchurDecomposition<R>> {
        let _ = schur;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::rsf2csf",
        })
    }

    /// Generalized Schur (QZ) decomposition for matrix pencil (A, B)
    ///
    /// Computes the QZ decomposition: A = Q @ S @ Z^H, B = Q @ T @ Z^H
    ///
    /// # Algorithm
    ///
    /// 1. Reduce (A, B) to Hessenberg-triangular form (H, T)
    /// 2. Apply QZ iteration to reduce H to quasi-triangular S
    /// 3. Extract generalized eigenvalues from diagonal blocks
    ///
    /// # Generalized Eigenvalues
    ///
    /// The generalized eigenvalues `λ` satisfy: `det(A - λB) = 0`
    /// Computed as: `λ_i = alpha_i / beta_i` where `alpha` and `beta` are
    /// extracted from the diagonal of `S` and `T` respectively.
    ///
    /// # Requirements
    ///
    /// Both A and B must be square matrices of the same size.
    fn qz_decompose(
        &self,
        a: &Tensor<R>,
        b: &Tensor<R>,
    ) -> Result<GeneralizedSchurDecomposition<R>> {
        let _ = (a, b);
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::qz_decompose",
        })
    }

    /// Polar decomposition: A = U @ P
    ///
    /// Decomposes matrix A into a unitary matrix U and a positive semi-definite
    /// Hermitian matrix P.
    ///
    /// # Algorithm
    ///
    /// Uses the Newton iteration for matrix sign function:
    /// 1. Compute SVD: A = U_svd @ S @ V^H
    /// 2. U = U_svd @ V^H (unitary factor)
    /// 3. P = V @ S @ V^H (positive semi-definite factor)
    ///
    /// Alternative: Newton iteration X_{k+1} = (X_k + X_k^{-H}) / 2
    ///
    /// # Properties
    ///
    /// - For invertible A: `U` is the closest unitary matrix to A (in Frobenius norm)
    /// - `P` is unique and equals `sqrt(A^H @ A)`
    /// - For real matrices: `U` is orthogonal, `P` is symmetric positive semi-definite
    fn polar_decompose(&self, a: &Tensor<R>) -> Result<PolarDecomposition<R>> {
        let _ = a;
        Err(Error::NotImplemented {
            feature: "LinearAlgebraAlgorithms::polar_decompose",
        })
    }
}