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
//! Matrix property computation functions
//!
//! This module provides functions for computing fundamental matrix properties
//! including rank, condition number, determinant, matrix inverse, and pseudoinverse.
use ;
use Tensor;
use NormOrd;
use svd;
/// Compute matrix rank using SVD
///
/// ## Mathematical Background
///
/// The rank of a matrix A is the dimension of its column space (or row space).
/// Equivalently, it's the number of linearly independent columns (or rows).
///
/// Using SVD, A = UΣV^T, the rank equals the number of non-zero singular values:
/// ```text
/// rank(A) = |{i : σᵢ > tolerance}|
/// ```text
///
/// ## Numerical Considerations
///
/// Due to floating-point arithmetic, singular values are rarely exactly zero.
/// The tolerance parameter determines the threshold below which singular values
/// are considered "numerically zero."
///
/// ## Parameters
/// * `tensor` - Input matrix A (m×n)
/// * `tol` - Tolerance for determining rank (default: 1e-6)
///
/// ## Returns
/// * Scalar tensor containing the rank of the matrix
///
/// ## Applications
/// - **Linear independence**: Determine if columns/rows are independent
/// - **Solvability**: rank(A) = rank([A|b]) for consistent systems
/// - **Dimensionality**: Effective dimension of data in matrix
/// - **Condition analysis**: Full rank indicates non-singularity
/// Compute matrix condition number
///
/// ## Mathematical Background
///
/// The condition number measures how sensitive a matrix's inverse is to small
/// changes in the matrix. For the 2-norm (spectral norm):
/// ```text
/// κ(A) = ||A||₂ ||A⁻¹||₂ = σₘₐₓ(A) / σₘᵢₙ(A)
/// ```text
///
/// where σₘₐₓ and σₘᵢₙ are the largest and smallest singular values.
///
/// ## Interpretation
///
/// - **κ(A) = 1**: Perfectly conditioned (orthogonal/unitary matrices)
/// - **κ(A) < 100**: Well-conditioned, reliable numerical computations
/// - **κ(A) > 10¹²**: Ill-conditioned, results may be unreliable
/// - **κ(A) = ∞**: Singular matrix, no unique solution
///
/// ## Condition Number Types
///
/// Different norms give different condition numbers:
/// - **2-norm**: κ₂(A) = σₘₐₓ/σₘᵢₙ (most common)
/// - **1-norm**: κ₁(A) = ||A||₁ ||A⁻¹||₁
/// - **∞-norm**: κ∞(A) = ||A||∞ ||A⁻¹||∞
/// - **Frobenius**: κF(A) = ||A||F ||A⁻¹||F
///
/// ## Parameters
/// * `tensor` - Input matrix A (must be square)
/// * `ord` - Norm type for condition number (currently uses 2-norm)
///
/// ## Returns
/// * Scalar tensor containing the condition number
///
/// ## Applications
/// - **Numerical stability**: Assess reliability of linear system solutions
/// - **Error analysis**: Bound propagation of input errors to output
/// - **Algorithm selection**: Choose appropriate solution method
/// - **Regularization**: Determine need for regularization in regression
/// Compute matrix determinant
///
/// ## Mathematical Background
///
/// The determinant is a scalar value that provides important information about
/// a square matrix. For an n×n matrix A:
///
/// ### Geometric Interpretation
/// - **Volume scaling**: |det(A)| = volume scaling factor of linear transformation
/// - **Orientation**: sign(det(A)) indicates orientation preservation
///
/// ### Algebraic Properties
/// 1. **Multiplicativity**: det(AB) = det(A) det(B)
/// 2. **Transpose**: det(A^T) = det(A)
/// 3. **Inverse**: det(A⁻¹) = 1/det(A) (if A is invertible)
/// 4. **Scalar multiple**: det(cA) = cⁿ det(A) for n×n matrix
///
/// ### Computation Methods
/// - **LU decomposition**: det(A) = det(P) ∏ᵢ Uᵢᵢ
/// - **Cofactor expansion**: Recursive expansion along rows/columns
/// - **SVD**: det(A) = ∏ᵢ σᵢ (product of singular values)
///
/// ## Special Cases
/// - **det(A) = 0**: Matrix is singular (non-invertible)
/// - **det(A) > 0**: Preserves orientation
/// - **det(A) < 0**: Reverses orientation
/// - **|det(A)| = 1**: Volume-preserving transformation
///
/// ## Parameters
/// * `tensor` - Square matrix A (n×n)
///
/// ## Returns
/// * Scalar tensor containing det(A)
///
/// ## Applications
/// - **Invertibility**: det(A) ≠ 0 ⟺ A is invertible
/// - **Volume computation**: Volume of parallelepiped from matrix columns
/// - **Characteristic polynomial**: det(A - λI) = 0 for eigenvalues λ
/// - **Cramer's rule**: Solution to Ax = b using determinants
///
/// ## Note
/// This is currently a placeholder returning 1.0. Production implementation
/// should use LU decomposition with partial pivoting for numerical stability.
/// Matrix Inverse
///
/// Computes the inverse of a square matrix A such that:
/// ```text
/// A A⁻¹ = A⁻¹ A = I
/// ```text
///
/// ## Mathematical Definition
///
/// For a square matrix A ∈ ℝⁿˣⁿ, the inverse A⁻¹ exists if and only if A is non-singular
/// (i.e., det(A) ≠ 0). The inverse satisfies:
/// - **Identity property**: A A⁻¹ = A⁻¹ A = I
/// - **Uniqueness**: If the inverse exists, it is unique
///
/// ## Mathematical Properties
///
/// 1. **(AB)⁻¹ = B⁻¹ A⁻¹**: Inverse of product (order reverses)
/// 2. **(A⁻¹)⁻¹ = A**: Inverse of inverse
/// 3. **(A^T)⁻¹ = (A⁻¹)^T**: Inverse of transpose
/// 4. **det(A⁻¹) = 1/det(A)**: Determinant of inverse
/// 5. **||A⁻¹||₂ = 1/σₘᵢₙ(A)**: Spectral norm of inverse
///
/// ## Gauss-Jordan Algorithm
///
/// This implementation uses Gauss-Jordan elimination with partial pivoting:
/// ```text
/// [A | I] → [I | A⁻¹]
/// ```text
///
/// 1. **Augment**: Create [A | I] where I is the identity matrix
/// 2. **Forward elimination**: Reduce A to row echelon form
/// 3. **Backward substitution**: Transform to reduced row echelon form [I | A⁻¹]
/// 4. **Extract**: A⁻¹ is the right half of the augmented matrix
///
/// ## Condition Number
///
/// The condition number κ(A) = ||A|| ||A⁻¹|| measures numerical stability:
/// - **κ(A) = 1**: Perfectly conditioned (orthogonal matrices)
/// - **κ(A) >> 1**: Ill-conditioned (near-singular)
/// - **κ(A) = ∞**: Singular (non-invertible)
///
/// ## Alternative Methods
///
/// For specific matrix types, more efficient algorithms exist:
/// - **Positive definite**: A⁻¹ = (L⁻¹)^T L⁻¹ via Cholesky L L^T = A
/// - **Symmetric**: A⁻¹ = V Λ⁻¹ V^T via eigendecomposition A = V Λ V^T
/// - **Well-conditioned**: A⁻¹ = V Σ⁻¹ U^T via SVD A = U Σ V^T
///
/// ## Parameters
/// - `tensor`: Input matrix A (n×n, must be non-singular)
///
/// ## Returns
/// Inverse matrix A⁻¹ such that A A⁻¹ = I
///
/// ## Applications
/// - **Linear systems**: Solve Ax = b via x = A⁻¹b (when multiple RHS)
/// - **Least squares**: Normal equations solution (X^T X)⁻¹ X^T y
/// - **Covariance**: Precision matrix Ω = Σ⁻¹
/// - **Kalman filtering**: Innovation covariance updates
/// - **Statistics**: Fisher information matrix inversion
///
/// ## Numerical Considerations
/// - **Avoid when possible**: Use solve() for Ax = b instead of x = A⁻¹b
/// - **Check condition number**: Large κ(A) indicates numerical instability
/// - **Use pseudoinverse**: For singular/rectangular matrices, use pinv()
/// Pseudo-inverse (Moore-Penrose inverse)
///
/// ## Mathematical Background
///
/// The Moore-Penrose pseudoinverse A⁺ generalizes matrix inversion to
/// non-square and singular matrices. For any m×n matrix A, A⁺ is the
/// unique n×m matrix satisfying:
///
/// 1. **A A⁺ A = A**: A⁺ is a generalized inverse
/// 2. **A⁺ A A⁺ = A⁺**: A⁺ is reflexive
/// 3. **(A A⁺)^T = A A⁺**: A A⁺ is symmetric
/// 4. **(A⁺ A)^T = A⁺ A**: A⁺ A is symmetric
///
/// ## SVD Construction
///
/// Given the SVD A = U Σ V^T, the pseudoinverse is:
/// ```text
/// A⁺ = V Σ⁺ U^T
/// ```text
/// where Σ⁺ is formed by:
/// - Transposing Σ
/// - Taking reciprocal of non-zero singular values
/// - Setting zero/small singular values to zero
///
/// ## Properties
///
/// 1. **Unique**: The pseudoinverse is uniquely defined
/// 2. **Inverse relationship**: If A is invertible, then A⁺ = A⁻¹
/// 3. **Least squares**: x = A⁺b minimizes ||Ax - b||₂
/// 4. **Best approximation**: A⁺ gives minimum norm solution when multiple solutions exist
///
/// ## Rank and Dimensions
///
/// For an m×n matrix A with rank r:
/// - **Full column rank** (r = n): A⁺ = (A^T A)⁻¹ A^T (left inverse)
/// - **Full row rank** (r = m): A⁺ = A^T (A A^T)⁻¹ (right inverse)
/// - **Rank deficient** (r < min(m,n)): Use SVD construction
///
/// ## Parameters
/// * `tensor` - Input matrix A (m×n, can be rectangular or singular)
/// * `rcond` - Relative condition number for singular value cutoff (default: 1e-15)
///
/// ## Returns
/// * Pseudoinverse A⁺ (n×m matrix)
///
/// ## Applications
/// - **Least squares**: Overdetermined systems Ax ≈ b
/// - **Underdetermined systems**: Minimum norm solutions to Ax = b
/// - **Data analysis**: Principal component regression
/// - **Signal processing**: Inverse filtering and deconvolution
/// - **Control theory**: System inversion and feedforward design
///
/// ## Numerical Considerations
/// - **Singular value threshold**: Small singular values indicate ill-conditioning
/// - **Condition number**: Large condition numbers lead to amplified noise
/// - **Regularization**: Consider Tikhonov regularization for ill-posed problems
///
/// ## Note
/// This is currently a placeholder implementation returning the transpose.
/// Production code should use the SVD-based construction described above.