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
//! Linear system solving operations
//!
//! This module provides functions for solving linear systems of equations,
//! including general linear systems, triangular systems, and least squares problems.
use ;
use ;
/// Solve system of linear equations Ax = b
///
/// ## Mathematical Background
///
/// Solves the linear system:
/// ```text
/// A x = b
/// ```text
/// where A is an n×n matrix, b is an n×m matrix (multiple right-hand sides),
/// and x is the n×m solution matrix.
///
/// ## Solution Methods
///
/// For different matrix types, optimal algorithms exist:
/// - **General matrices**: LU decomposition with partial pivoting
/// - **Symmetric positive definite**: Cholesky decomposition
/// - **Triangular**: Forward/backward substitution
/// - **Overdetermined**: Least squares via QR or SVD
///
/// ## Stability Considerations
///
/// The condition number κ(A) = ||A|| ||A⁻¹|| determines numerical stability:
/// - κ(A) ≈ 1: Well-conditioned, stable solution
/// - κ(A) >> 1: Ill-conditioned, solution may be unreliable
/// - κ(A) = ∞: Singular, no unique solution exists
///
/// ## Parameters
/// * `a` - Coefficient matrix A (n×n, must be non-singular)
/// * `b` - Right-hand side matrix b (n×m)
///
/// ## Returns
/// * Solution matrix x such that Ax = b
///
/// ## Applications
/// - **Regression**: Normal equations (X^T X) β = X^T y
/// - **Interpolation**: Vandermonde systems for polynomial fitting
/// - **Finite differences**: Discretized differential equations
/// - **Optimization**: Newton's method Hx = -g
///
/// ## Note
/// This is currently a placeholder implementation. Production code should
/// use LU decomposition with partial pivoting for general matrices.
/// Solve triangular system of linear equations
///
/// ## Mathematical Background
///
/// Solves triangular systems efficiently using substitution:
///
/// ### Forward Substitution (Lower Triangular)
/// For Lx = b where L is lower triangular:
/// ```text
/// x₁ = b₁ / L₁₁
/// x₂ = (b₂ - L₂₁x₁) / L₂₂
/// xᵢ = (bᵢ - Σⱼ₌₁ⁱ⁻¹ Lᵢⱼxⱼ) / Lᵢᵢ
/// ```text
///
/// ### Backward Substitution (Upper Triangular)
/// For Ux = b where U is upper triangular:
/// ```text
/// xₙ = bₙ / Uₙₙ
/// xₙ₋₁ = (bₙ₋₁ - Uₙ₋₁,ₙxₙ) / Uₙ₋₁,ₙ₋₁
/// xᵢ = (bᵢ - Σⱼ₌ᵢ₊₁ⁿ Uᵢⱼxⱼ) / Uᵢᵢ
/// ```text
///
/// ## Computational Complexity
/// - Time: O(n²) for n×n triangular matrix
/// - Space: O(1) additional space (in-place possible)
/// - Highly efficient compared to O(n³) general solve
///
/// ## Parameters
/// * `b` - Right-hand side matrix (n×m)
/// * `a` - Triangular coefficient matrix (n×n)
/// * `upper` - If true, A is upper triangular; if false, lower triangular
/// * `transpose` - If true, solve A^T x = b instead of Ax = b
/// * `unitriangular` - If true, assume diagonal entries are 1
///
/// ## Returns
/// * Solution matrix x such that Ax = b (or A^T x = b if transpose=true)
///
/// ## Applications
/// - **LU solving**: Forward substitution Ly = b, backward substitution Ux = y
/// - **Cholesky solving**: Forward Ly = b, backward L^T x = y
/// - **Iterative methods**: Preconditioning with triangular matrices
///
/// ## Note
/// This is currently a placeholder implementation. Production code should
/// implement actual forward/backward substitution algorithms.
/// Least squares solution to overdetermined systems
///
/// ## Mathematical Background
///
/// Solves the least squares problem:
/// ```text
/// minimize ||Ax - b||₂²
/// ```text
///
/// For an m×n matrix A with m ≥ n (overdetermined system), the least squares
/// solution minimizes the sum of squared residuals.
///
/// ## Solution Methods
///
/// ### Normal Equations
/// ```text
/// A^T A x = A^T b
/// x = (A^T A)⁻¹ A^T b
/// ```text
/// - Fast for well-conditioned problems
/// - Can be unstable for ill-conditioned A
///
/// ### QR Decomposition
/// ```text
/// A = QR → ||QRx - b||₂ = ||Rx - Q^T b||₂
/// ```text
/// - More stable than normal equations
/// - Standard choice for most problems
///
/// ### SVD Decomposition
/// ```text
/// A = UΣV^T → x = VΣ⁺U^T b
/// ```text
/// - Most stable, handles rank-deficient cases
/// - Slowest but most robust method
///
/// ## Mathematical Properties
///
/// 1. **Optimality**: x minimizes ||Ax - b||₂
/// 2. **Normal equations**: A^T(Ax - b) = 0 (residual orthogonal to column space)
/// 3. **Uniqueness**: Unique solution if A has full column rank
/// 4. **Residual**: r = b - Ax is orthogonal to column space of A
///
/// ## Parameters
/// * `a` - Coefficient matrix A (m×n, typically m ≥ n)
/// * `b` - Right-hand side matrix b (m×k for k different problems)
///
/// ## Returns
/// * Tuple (solution, residuals, rank, singular_values) where:
/// - solution: Least squares solution x (n×k)
/// - residuals: Sum of squared residuals for each column
/// - rank: Effective rank of matrix A
/// - singular_values: Singular values of A
///
/// ## Applications
/// - **Linear regression**: Fit y = Xβ + ε
/// - **Curve fitting**: Polynomial/function approximation
/// - **Data interpolation**: Overdetermined interpolation problems
/// - **Signal processing**: Parameter estimation from noisy measurements
///
/// ## Note
/// This is currently a placeholder implementation. Production code should
/// use QR decomposition or SVD for robust least squares solving.