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
//! Matrix decomposition operations
//!
//! This module provides fundamental matrix decompositions including LU, QR, Cholesky,
//! SVD, and eigenvalue decomposition. These decompositions are building blocks for
//! many numerical algorithms and have extensive applications in machine learning,
//! scientific computing, and engineering.
use ;
use ;
use tensor_to_array2;
/// LU decomposition with partial pivoting
///
/// Computes the LU decomposition of a square matrix A:
/// ```text
/// P A = L U
/// ```text
///
/// ## Mathematical Background
///
/// LU decomposition factors a matrix into:
/// - **P**: Permutation matrix (partial pivoting for numerical stability)
/// - **L**: Lower triangular matrix with unit diagonal
/// - **U**: Upper triangular matrix
///
/// ## Applications
/// - **Linear systems**: Solve Ax = b via forward/backward substitution
/// - **Matrix inversion**: A⁻¹ = U⁻¹ L⁻¹ P^T
/// - **Determinant**: det(A) = det(P) × ∏ᵢ Uᵢᵢ
///
/// ## Parameters
/// * `tensor` - Square matrix to decompose
///
/// ## Returns
/// * Tuple (P, L, U) where P A = L U
///
/// ## Note
/// This is currently a placeholder implementation. A production version would
/// implement the actual LU decomposition algorithm with partial pivoting.
/// QR Decomposition
///
/// Computes the QR decomposition of a matrix A:
/// ```text
/// A = QR
/// ```text
///
/// ## Mathematical Definition
///
/// For an m×n matrix A, the QR decomposition is a factorization:
/// - **Q**: m×m orthogonal matrix (when reduced=false) or m×min(m,n) (when reduced=true)
/// - **R**: m×n upper triangular matrix (when reduced=false) or min(m,n)×n (when reduced=true)
///
/// ## Mathematical Properties
///
/// 1. **Orthogonality**: Q^T Q = I (Q has orthonormal columns)
/// 2. **Upper triangular**: R[i,j] = 0 for i > j
/// 3. **Uniqueness**: If A has full column rank and R has positive diagonal elements,
/// then the QR decomposition is unique
/// 4. **Determinant**: det(A) = det(R) (since det(Q) = ±1)
///
/// ## Gram-Schmidt Process
///
/// The QR decomposition can be computed via Gram-Schmidt orthogonalization:
/// ```text
/// q₁ = a₁ / ||a₁||
/// q₂ = (a₂ - (q₁·a₂)q₁) / ||(a₂ - (q₁·a₂)q₁)||
/// qₖ = (aₖ - Σᵢ₌₁ᵏ⁻¹(qᵢ·aₖ)qᵢ) / ||aₖ - Σᵢ₌₁ᵏ⁻¹(qᵢ·aₖ)qᵢ||
/// ```text
///
/// ## Parameters
/// - `tensor`: Input matrix A (m×n)
/// - `reduced`: If true, returns "thin" QR with Q (m×min(m,n)) and R (min(m,n)×n).
/// If false, returns "full" QR with Q (m×m) and R (m×n).
///
/// ## Returns
/// Tuple (Q, R) where:
/// - Q: Orthogonal matrix with orthonormal columns
/// - R: Upper triangular matrix
///
/// ## Applications
/// - **Least squares**: Solve Ax = b via Rx = Q^T b
/// - **Eigenvalue algorithms**: QR iteration for eigenvalue computation
/// - **Orthogonalization**: Extract orthonormal basis from column space
/// - **Matrix inversion**: A⁻¹ = R⁻¹ Q^T (when A is square and invertible)
/// Cholesky Decomposition
///
/// Computes the Cholesky decomposition of a positive definite matrix A:
/// ```text
/// A = L L^T (lower form)
/// A = U^T U (upper form)
/// ```text
///
/// ## Mathematical Definition
///
/// For a symmetric positive definite matrix A, the Cholesky decomposition uniquely factors A as:
/// - **Lower form**: A = L L^T where L is lower triangular with positive diagonal
/// - **Upper form**: A = U^T U where U is upper triangular with positive diagonal
///
/// ## Mathematical Properties
///
/// 1. **Uniqueness**: The Cholesky factor is unique for positive definite matrices
/// 2. **Efficiency**: Requires ~n³/3 operations (half of LU decomposition)
/// 3. **Numerical stability**: More stable than LU for positive definite systems
/// 4. **Determinant**: det(A) = (∏ᵢ Lᵢᵢ)² = ∏ᵢ Lᵢᵢ²
///
/// ## Cholesky Algorithm
///
/// For lower triangular L where A = L L^T:
/// ```text
/// for i = 0 to n-1:
/// Lᵢᵢ = √(Aᵢᵢ - Σⱼ₌₀ⁱ⁻¹ Lᵢⱼ²)
/// for k = i+1 to n-1:
/// Lₖᵢ = (Aₖᵢ - Σⱼ₌₀ⁱ⁻¹ Lₖⱼ Lᵢⱼ) / Lᵢᵢ
/// ```text
///
/// ## Parameters
/// - `tensor`: Input matrix A (n×n, must be symmetric positive definite)
/// - `upper`: If true, returns upper triangular U such that A = U^T U.
/// If false, returns lower triangular L such that A = L L^T.
///
/// ## Returns
/// Cholesky factor:
/// - Lower triangular L (if upper=false)
/// - Upper triangular U (if upper=true)
///
/// ## Applications
/// - **Linear systems**: Solve Ax = b via Ly = b, L^T x = y
/// - **Matrix inversion**: A⁻¹ = (L^T)⁻¹ L⁻¹
/// - **Determinant**: det(A) = ∏ᵢ Lᵢᵢ²
/// - **Gaussian sampling**: Generate x ~ N(μ, A) via x = μ + L z where z ~ N(0,I)
/// - **Optimization**: Newton's method with positive definite Hessians
/// Singular Value Decomposition (SVD)
///
/// Computes the singular value decomposition of a matrix A:
/// ```text
/// A = U Σ V^T
/// ```text
///
/// ## Mathematical Definition
///
/// For an m×n matrix A, the SVD is a factorization:
/// - **U**: m×m orthogonal matrix (left singular vectors)
/// - **Σ**: m×n diagonal matrix (singular values σ₁ ≥ σ₂ ≥ ... ≥ σₘᵢₙ ≥ 0)
/// - **V^T**: n×n orthogonal matrix (right singular vectors, transposed)
///
/// ## Mathematical Properties
///
/// 1. **Orthogonality**: U^T U = I, V^T V = I
/// 2. **Rank**: rank(A) = number of non-zero singular values
/// 3. **Norm preservation**: ||A||₂ = σ₁ (largest singular value)
/// 4. **Eckart-Young theorem**: Best rank-k approximation A_k = U_k Σ_k V_k^T
///
/// ## Parameters
/// - `tensor`: Input matrix A (m×n)
/// - `full_matrices`: If true, return full U (m×m) and V^T (n×n).
/// If false, return reduced form U (m×min(m,n)) and V^T (min(m,n)×n)
///
/// ## Returns
/// Tuple (U, Σ, V^T) where:
/// - U: Left singular vectors
/// - Σ: Singular values (1D tensor)
/// - V^T: Right singular vectors (transposed)
///
/// ## Example Applications
/// - **Matrix pseudoinverse**: A⁺ = V Σ⁺ U^T
/// - **Principal Component Analysis**: Components from U or V
/// - **Low-rank approximation**: A ≈ Σᵢ₌₁ᵏ σᵢ uᵢ vᵢ^T
/// - **Condition number**: κ(A) = σ₁/σₘᵢₙ
/// Eigenvalue decomposition for square matrices
///
/// Computes the eigenvalue decomposition of a square matrix A:
/// ```text
/// A V = V Λ or A = V Λ V⁻¹
/// ```text
///
/// ## Mathematical Background
///
/// For a square matrix A ∈ ℝⁿˣⁿ, the eigendecomposition finds:
/// - **Eigenvalues** λᵢ: Scalars such that A vᵢ = λᵢ vᵢ
/// - **Eigenvectors** vᵢ: Non-zero vectors satisfying the eigenvalue equation
///
/// ## Properties
/// - **Characteristic polynomial**: det(A - λI) = 0
/// - **Trace**: tr(A) = Σᵢ λᵢ
/// - **Determinant**: det(A) = ∏ᵢ λᵢ
/// - **Spectral radius**: ρ(A) = max |λᵢ|
///
/// ## Parameters
/// * `tensor` - Square matrix to decompose
///
/// ## Returns
/// * Tuple (eigenvalues, eigenvectors) where eigenvalues are 1D and eigenvectors are 2D
///
/// ## Applications
/// - **Principal Component Analysis**: Covariance matrix eigendecomposition
/// - **Stability analysis**: Eigenvalues determine system stability
/// - **Matrix powers**: A^k = V Λ^k V⁻¹
/// - **Matrix functions**: f(A) = V f(Λ) V⁻¹
/// Low-rank SVD approximation using randomized algorithms
///
/// Computes an approximate SVD for large matrices using randomized algorithms,
/// which can be much faster than full SVD for low-rank approximations.
///
/// ## Mathematical Background
///
/// Randomized SVD uses random projections to efficiently compute low-rank
/// approximations of large matrices. The algorithm works by:
/// 1. Finding a good subspace Q that captures the range of A
/// 2. Computing B = Q^T A (smaller projected matrix)
/// 3. Computing SVD of B and mapping back to original space
///
/// ## Parameters
/// * `tensor` - Input matrix to decompose
/// * `rank` - Target rank for approximation (default: min(m,n,10))
/// * `niter` - Number of power iterations for accuracy (default: 2)
///
/// ## Returns
/// * Tuple (U, S, V) representing the low-rank SVD approximation
/// Low-rank PCA approximation
///
/// Computes principal components using randomized SVD for efficient
/// dimensionality reduction of large datasets.
///
/// ## Mathematical Background
///
/// PCA finds the directions of maximum variance in data:
/// 1. Center the data: X̃ = X - μ
/// 2. Compute covariance: C = X̃^T X̃ / (n-1)
/// 3. Find eigenvectors: principal components are eigenvectors of C
///
/// ## Parameters
/// * `tensor` - Input data matrix (samples × features)
/// * `rank` - Number of principal components (default: min dimensions)
/// * `center` - Whether to center the data (default: true)
///
/// ## Returns
/// * Tuple (U, S, V^T) where V^T contains the principal components