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
//! Singular value decomposition by one-sided Jacobi, for tall or square matrices.
//!
//! The method follows Golub & Van Loan, *Matrix Computations*, and Demmel & Veselić for high
//! relative accuracy — a fixed-size `no_std` implementation on this crate's own
//! [`Vector`] and [`Matrix`] types. Reference values for the tests come from numpy/LAPACK.
use crate::error::LinalgError;
use crate::linear_algebra::{Matrix, Vector};
use crate::scalar::Numeric;
/// A thin singular value decomposition `A = U · diag(σ) · Vᵀ` for a matrix with `M ≥ N`.
///
/// `u` has orthonormal columns, `singular_values` holds the σ in descending order (all ≥ 0), and
/// `v` has orthonormal columns.
#[derive(Debug, Clone, Copy)]
#[must_use]
pub struct Svd<const M: usize, const N: usize, T = f64> {
/// Left factor `U` with orthonormal columns.
pub(crate) u: Matrix<M, N, T>,
/// Singular values in descending order.
pub(crate) singular_values: Vector<N, T>,
/// Right factor `V` with orthonormal columns.
pub(crate) v: Matrix<N, N, T>,
}
impl<const M: usize, const N: usize, T: Numeric> Matrix<M, N, T> {
/// Decomposes `self` as `U · diag(σ) · Vᵀ` by one-sided Jacobi (thin form, `M ≥ N`).
///
/// `U` has orthonormal columns, the σ are non-negative and descending, and `V` has orthonormal
/// columns. Returns [`LinalgError::Underdetermined`] for a wide matrix (`M < N`) — transpose it
/// first — or [`LinalgError::NonFinite`] if any entry is not finite.
///
/// ```
/// use multicalc::linear_algebra::Matrix;
/// let a = Matrix::<3, 2>::new([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]);
/// let svd = a.svd().unwrap();
/// let (u, s, v) = (svd.u(), svd.singular_values(), svd.v());
/// // U · diag(σ) · Vᵀ == A.
/// for r in 0..3 {
/// for c in 0..2 {
/// let mut acc = 0.0;
/// for k in 0..2 {
/// acc += u[(r, k)] * s[k] * v[(c, k)];
/// }
/// assert!((acc - a[(r, c)]).abs() < 1e-12);
/// }
/// }
/// ```
///
/// A wide matrix (`M < N`) has no thin form here; take the SVD of its transpose, whose singular
/// values are the same. Its pseudo-inverse then follows from `A⁺ = ((Aᵀ)⁺)ᵀ`, which
/// [`Matrix::pseudo_inverse`] applies for any shape.
///
/// ```
/// use multicalc::linear_algebra::Matrix;
/// // For a wide matrix, decompose its transpose.
/// let a = Matrix::<2, 3>::new([[1.0, 0.0, 2.0], [0.0, 1.0, 1.0]]);
/// let at = a.transpose();
/// let svd = at.svd().unwrap();
/// let (u, s, v) = (svd.u(), svd.singular_values(), svd.v());
/// for r in 0..3 {
/// for c in 0..2 {
/// let mut acc = 0.0;
/// for k in 0..2 {
/// acc += u[(r, k)] * s[k] * v[(c, k)];
/// }
/// assert!((acc - at[(r, c)]).abs() < 1e-12);
/// }
/// }
/// ```
pub fn svd(self) -> Result<Svd<M, N, T>, LinalgError> {
if M < N {
return Err(LinalgError::Underdetermined);
}
if !self.is_finite() {
return Err(LinalgError::NonFinite);
}
let mut u = self;
let mut v = Matrix::<N, N, T>::identity();
// One-sided Jacobi: rotate column pairs of U until its columns are orthogonal.
let max_sweeps = 60;
for _ in 0..max_sweeps {
let mut off_max = T::ZERO;
for p in 0..N {
for q in (p + 1)..N {
let cp = Vector::<M, T>::from_fn(|r| u[(r, p)]);
let cq = Vector::<M, T>::from_fn(|r| u[(r, q)]);
let alpha = cp.norm_squared();
let beta = cq.norm_squared();
let gamma = cp.dot(cq);
if alpha == T::ZERO || beta == T::ZERO {
continue;
}
let scale = (alpha * beta).sqrt();
let off = gamma.abs() / scale;
if off > off_max {
off_max = off;
}
if gamma.abs() <= T::EPSILON * scale {
continue;
}
// Rotation that makes columns p and q orthogonal.
let zeta = (beta - alpha) / (T::TWO * gamma);
let sign = if zeta < T::ZERO { -T::ONE } else { T::ONE };
let t = sign / (zeta.abs() + (T::ONE + zeta * zeta).sqrt());
let c = T::ONE / (T::ONE + t * t).sqrt();
let s = c * t;
for i in 0..M {
let up = u[(i, p)];
let uq = u[(i, q)];
u[(i, p)] = c * up - s * uq;
u[(i, q)] = s * up + c * uq;
}
for i in 0..N {
let vp = v[(i, p)];
let vq = v[(i, q)];
v[(i, p)] = c * vp - s * vq;
v[(i, q)] = s * vp + c * vq;
}
}
}
if off_max <= T::EPSILON {
break;
}
}
// The column norms are the singular values; normalize U's columns by them.
let mut singular_values = Vector::<N, T>::zeros();
for k in 0..N {
let sigma = Vector::<M, T>::from_fn(|r| u[(r, k)]).norm();
singular_values[k] = sigma;
if sigma > T::ZERO {
for i in 0..M {
u[(i, k)] /= sigma;
}
}
}
// Sort the singular values descending, carrying the matching U and V columns.
for k in 0..N {
let mut top = k;
for j in (k + 1)..N {
if singular_values[j] > singular_values[top] {
top = j;
}
}
if top != k {
let tmp = singular_values[k];
singular_values[k] = singular_values[top];
singular_values[top] = tmp;
for i in 0..M {
let tmp = u[(i, k)];
u[(i, k)] = u[(i, top)];
u[(i, top)] = tmp;
}
for i in 0..N {
let tmp = v[(i, k)];
v[(i, k)] = v[(i, top)];
v[(i, top)] = tmp;
}
}
}
// Sign convention: the largest-magnitude entry of each U column is positive.
for k in 0..N {
let mut row = 0;
let mut best = T::ZERO;
for i in 0..M {
let mag = u[(i, k)].abs();
if mag > best {
best = mag;
row = i;
}
}
if u[(row, k)] < T::ZERO {
for i in 0..M {
u[(i, k)] = -u[(i, k)];
}
for i in 0..N {
v[(i, k)] = -v[(i, k)];
}
}
}
Ok(Svd {
u,
singular_values,
v,
})
}
/// The Moore–Penrose pseudo-inverse of `self`, for any shape.
///
/// Tall or square inputs go straight through [`Matrix::svd`]; a wide input (`M < N`) is handled
/// as `((Aᵀ)⁺)ᵀ`. Returns [`LinalgError::NonFinite`] if any entry is not finite.
///
/// ```
/// use multicalc::linear_algebra::Matrix;
/// // A wide matrix, handled through the transpose route.
/// let a = Matrix::<2, 3>::new([[1.0, 0.0, 2.0], [0.0, 1.0, 1.0]]);
/// let pinv = a.pseudo_inverse().unwrap();
/// let recon = a * pinv * a; // A·A⁺·A == A
/// for r in 0..2 {
/// for c in 0..3 {
/// assert!((recon[(r, c)] - a[(r, c)]).abs() < 1e-12);
/// }
/// }
/// ```
pub fn pseudo_inverse(self) -> Result<Matrix<N, M, T>, LinalgError> {
if M >= N {
Ok(self.svd()?.pseudo_inverse())
} else {
Ok(self.transpose().svd()?.pseudo_inverse().transpose())
}
}
}
impl<const M: usize, const N: usize, T: Numeric> Svd<M, N, T> {
/// The singular values, descending and non-negative.
pub fn singular_values(&self) -> Vector<N, T> {
self.singular_values
}
/// The left factor `U`, with orthonormal columns.
pub fn u(&self) -> Matrix<M, N, T> {
self.u
}
/// The right factor `V`, with orthonormal columns.
pub fn v(&self) -> Matrix<N, N, T> {
self.v
}
/// The number of singular values greater than `tol`.
///
/// ```
/// use multicalc::linear_algebra::Matrix;
/// // Column 2 is twice column 1, so the matrix has rank 1.
/// let a = Matrix::<3, 2>::new([[1.0, 2.0], [2.0, 4.0], [3.0, 6.0]]);
/// let tolerance = 1e-9;
/// assert_eq!(a.svd().unwrap().rank(tolerance), 1);
/// ```
#[inline]
#[must_use]
pub fn rank(&self, tol: T) -> usize {
let mut count = 0;
for k in 0..N {
if self.singular_values[k] > tol {
count += 1;
}
}
count
}
/// The ratio `σ_max / σ_min`, or infinity when the smallest singular value is zero.
///
/// ```
/// use multicalc::linear_algebra::Matrix;
/// let a = Matrix::<2, 2>::new([[2.0, 0.0], [0.0, 1.0]]);
/// assert!((a.svd().unwrap().condition_number() - 2.0).abs() < 1e-12);
/// ```
#[inline]
#[must_use]
pub fn condition_number(&self) -> T {
if N == 0 {
return T::INFINITY;
}
let smallest = self.singular_values[N - 1];
if smallest <= T::ZERO {
T::INFINITY
} else {
self.singular_values[0] / smallest
}
}
/// The default cutoff below which a singular value counts as zero.
#[must_use]
fn default_tol(&self) -> T {
if N == 0 {
return T::ZERO;
}
T::from_usize(M.max(N)) * T::EPSILON * self.singular_values[0]
}
/// The Moore–Penrose pseudo-inverse `V · Σ⁺ · Uᵀ`, dropping singular values `<= tol`.
pub fn pseudo_inverse_tol(&self, tol: T) -> Matrix<N, M, T> {
Matrix::from_fn(|i, j| {
let mut acc = T::ZERO;
for k in 0..N {
let sigma = self.singular_values[k];
if sigma > tol {
acc += self.v[(i, k)] * self.u[(j, k)] / sigma;
}
}
acc
})
}
/// The Moore–Penrose pseudo-inverse, using a default cutoff from the largest singular value.
///
/// ```
/// use multicalc::linear_algebra::Matrix;
/// let a = Matrix::<3, 2>::new([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]);
/// let pinv = a.svd().unwrap().pseudo_inverse();
/// let recon = a * pinv * a; // A·A⁺·A == A
/// for r in 0..3 {
/// for c in 0..2 {
/// assert!((recon[(r, c)] - a[(r, c)]).abs() < 1e-12);
/// }
/// }
/// ```
pub fn pseudo_inverse(&self) -> Matrix<N, M, T> {
self.pseudo_inverse_tol(self.default_tol())
}
/// The minimum-norm least-squares solution of `A·x = b`, from `V · Σ⁺ · Uᵀ · b`.
///
/// The pseudo-inverse is never formed. Singular values `<= tol` are dropped.
///
/// ```
/// use multicalc::linear_algebra::{Matrix, Vector};
/// // Overdetermined and consistent: the exact solution is x = [1, 2].
/// let a = Matrix::<3, 2>::new([[1.0, 0.0], [0.0, 1.0], [1.0, 1.0]]);
/// let b = Vector::new([1.0, 2.0, 3.0]);
/// let x = a.svd().unwrap().solve(b);
/// assert!((x[0] - 1.0).abs() < 1e-12);
/// assert!((x[1] - 2.0).abs() < 1e-12);
/// ```
pub fn solve(&self, b: Vector<M, T>) -> Vector<N, T> {
let tol = self.default_tol();
let mut z = Vector::<N, T>::zeros();
for k in 0..N {
let sigma = self.singular_values[k];
if sigma > tol {
let uk = Vector::<M, T>::from_fn(|r| self.u[(r, k)]);
z[k] = uk.dot(b) / sigma;
}
}
self.v * z
}
}