multicalc 0.7.0

Rust scientific computing for single and multi-variable calculus
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Column-pivoted Householder QR factorization, with an overflow-safe norm and helpers.
//!
//! The factorization, damped solve, and norm port MINPACK's `qrfac`, `qrsolv`, and `enorm` (Moré,
//! Garbow, Hillstrom; public domain, netlib) — a clean-room, fixed-size `no_std` reimplementation
//! on this crate's own [`Vector`] and [`Matrix`] types.

use crate::linear_algebra::{Matrix, Vector};
use crate::scalar::Numeric;
use crate::utils::error_codes::CalcError;

/// Euclidean norm of `v`, computed so it neither overflows on large components nor
/// underflows on small ones.
///
/// Components are split into three magnitude bands. Small and large components are summed
/// against a running maximum in that band, so every squared term stays within range; only
/// the mid band is squared directly. This is the MINPACK `enorm` scheme.
pub(crate) fn enorm<T: Numeric>(v: &[T]) -> T {
    // Below `rdwarf`, squaring underflows; above `agiant`, summing the squares overflows.
    let rdwarf = T::MIN_POSITIVE.sqrt();
    let rgiant = T::MAX.sqrt();
    let agiant = rgiant / T::from_usize(v.len());

    let mut small_sum = T::ZERO;
    let mut mid_sum = T::ZERO;
    let mut large_sum = T::ZERO;
    let mut small_max = T::ZERO;
    let mut large_max = T::ZERO;

    for &value in v {
        let a = value.abs();

        if a > rdwarf && a < agiant {
            mid_sum += a * a;
        } else if a > rdwarf {
            // Large band: rescale against the running large maximum.
            if a > large_max {
                let ratio = large_max / a;
                large_sum = T::ONE + large_sum * ratio * ratio;
                large_max = a;
            } else {
                let ratio = a / large_max;
                large_sum += ratio * ratio;
            }
        } else if a != T::ZERO {
            // Small band: rescale against the running small maximum.
            if a > small_max {
                let ratio = small_max / a;
                small_sum = T::ONE + small_sum * ratio * ratio;
                small_max = a;
            } else {
                let ratio = a / small_max;
                small_sum += ratio * ratio;
            }
        }
    }

    if large_sum != T::ZERO {
        large_max * (large_sum + (mid_sum / large_max) / large_max).sqrt()
    } else if mid_sum != T::ZERO {
        if mid_sum >= small_max {
            (mid_sum * (T::ONE + (small_max / mid_sum) * (small_max * small_sum))).sqrt()
        } else {
            (small_max * ((mid_sum / small_max) + (small_max * small_sum))).sqrt()
        }
    } else {
        small_max * small_sum.sqrt()
    }
}

/// Returns the larger of `a` and `b`. If the two do not compare (a NaN is involved),
/// returns `a`.
pub(crate) fn max<T: PartialOrd>(a: T, b: T) -> T {
    if b > a { b } else { a }
}

/// Returns the smaller of `a` and `b`. If the two do not compare (a NaN is involved),
/// returns `a`.
pub(crate) fn min<T: PartialOrd>(a: T, b: T) -> T {
    if b < a { b } else { a }
}

/// Column-pivoted Householder QR of an `M`-by-`N` matrix with `M >= N`.
///
/// Holds the factorization in packed form: the strict lower triangle of `qr` stores the
/// Householder vectors, the strict upper triangle stores the off-diagonal of `R`, and
/// `r_diag` holds the diagonal of `R`. `permutation` gives the pivot order, so
/// `A * P == Q * R`, where column `j` of `P` is column `permutation[j]` of the identity.
#[derive(Debug, Clone, Copy)]
#[must_use]
pub struct PivotedQr<const M: usize, const N: usize, T = f64> {
    /// Packed reflectors (below the diagonal) and off-diagonal `R` (above it).
    pub(crate) qr: Matrix<M, N, T>,
    /// Diagonal of `R`.
    pub(crate) r_diag: [T; N],
    /// Euclidean norms of the original columns of `A`, in original order.
    pub(crate) column_norms: [T; N],
    /// Pivot order: column `j` of `A * P` is column `permutation[j]` of `A`.
    pub(crate) permutation: [usize; N],
}

impl<const M: usize, const N: usize, T: Numeric> PivotedQr<M, N, T> {
    /// Factorizes `a` by column-pivoted Householder QR.
    ///
    /// Returns [`CalcError::Underdetermined`] if `M < N`. Never panics: a zero pivot column
    /// leaves the corresponding `r_diag` entry at zero rather than dividing by it, so a
    /// rank-deficient matrix factorizes without error (the deficiency surfaces in a solve).
    ///
    /// # Examples
    /// ```
    /// use multicalc::linear_algebra::{Matrix, PivotedQr, Vector};
    ///
    /// // Least-squares fit of y = a + b*t through (0, 1), (1, 3), (2, 5): a = 1, b = 2.
    /// let a = Matrix::<3, 2>::new([[1.0, 0.0], [1.0, 1.0], [1.0, 2.0]]);
    /// let b = Vector::new([1.0, 3.0, 5.0]);
    /// let x = PivotedQr::decompose(a).unwrap().solve_least_squares(b).unwrap();
    /// assert!((x[0] - 1.0).abs() < 1e-12);
    /// assert!((x[1] - 2.0).abs() < 1e-12);
    /// ```
    pub fn decompose(a: Matrix<M, N, T>) -> Result<Self, CalcError> {
        if M < N {
            return Err(CalcError::Underdetermined);
        }

        let mut qr = a;
        let mut r_diag = [T::ZERO; N];
        let mut column_norms = [T::ZERO; N];
        let mut reference_norm = [T::ZERO; N];
        let mut permutation = [0usize; N];

        // Initial column norms; `r_diag` doubles as the running partial norm until each
        // column is reduced, after which it holds the final `R` diagonal.
        for j in 0..N {
            let mut column = [T::ZERO; M];
            for i in 0..M {
                column[i] = qr[(i, j)];
            }
            let norm = enorm(&column);
            column_norms[j] = norm;
            r_diag[j] = norm;
            reference_norm[j] = norm;
            permutation[j] = j;
        }

        let epsmch = T::EPSILON;
        let p05 = T::from_f64(0.05);

        for j in 0..N {
            // Bring the column of largest remaining partial norm into position `j`.
            let mut kmax = j;
            for k in j..N {
                if r_diag[k] > r_diag[kmax] {
                    kmax = k;
                }
            }
            if kmax != j {
                for i in 0..M {
                    let tmp = qr[(i, j)];
                    qr[(i, j)] = qr[(i, kmax)];
                    qr[(i, kmax)] = tmp;
                }
                r_diag[kmax] = r_diag[j];
                reference_norm[kmax] = reference_norm[j];
                permutation.swap(j, kmax);
            }

            // Householder transformation zeroing column `j` below the diagonal.
            let mut column = [T::ZERO; M];
            for i in j..M {
                column[i] = qr[(i, j)];
            }
            let mut ajnorm = enorm(&column[j..]);
            if ajnorm == T::ZERO {
                r_diag[j] = -ajnorm;
                continue;
            }
            // Sign chosen so the pivot element is at least one, keeping the divisor below safe.
            if qr[(j, j)] < T::ZERO {
                ajnorm = -ajnorm;
            }
            for i in j..M {
                qr[(i, j)] /= ajnorm;
            }
            qr[(j, j)] += T::ONE;

            // Apply the transformation to the remaining columns and downdate their norms.
            for k in (j + 1)..N {
                let mut sum = T::ZERO;
                for i in j..M {
                    sum += qr[(i, j)] * qr[(i, k)];
                }
                let factor = sum / qr[(j, j)];
                for i in j..M {
                    let reflected = qr[(i, k)] - factor * qr[(i, j)];
                    qr[(i, k)] = reflected;
                }

                if r_diag[k] != T::ZERO {
                    let ratio = qr[(j, k)] / r_diag[k];
                    r_diag[k] *= max(T::ZERO, T::ONE - ratio * ratio).sqrt();
                    // `reference_norm[k]` is the column's original norm, nonzero here.
                    let relative = r_diag[k] / reference_norm[k];
                    if p05 * relative * relative <= epsmch {
                        // Recompute from the remaining rows to shed accumulated round-off.
                        let mut tail = [T::ZERO; M];
                        for i in (j + 1)..M {
                            tail[i] = qr[(i, k)];
                        }
                        r_diag[k] = enorm(&tail[(j + 1)..]);
                        reference_norm[k] = r_diag[k];
                    }
                }
            }

            r_diag[j] = -ajnorm;
        }

        Ok(PivotedQr {
            qr,
            r_diag,
            column_norms,
            permutation,
        })
    }

    /// The `N`-by-`N` upper-triangular factor `R`.
    pub fn r(&self) -> Matrix<N, N, T> {
        Matrix::from_fn(|row, col| {
            if row == col {
                self.r_diag[row]
            } else if col > row {
                self.qr[(row, col)]
            } else {
                T::ZERO
            }
        })
    }

    /// The `M`-by-`N` factor `Q`, formed by applying the stored reflectors to the identity.
    /// Its columns are orthonormal.
    pub fn q(&self) -> Matrix<M, N, T> {
        let mut q = Matrix::from_fn(|row, col| if row == col { T::ONE } else { T::ZERO });
        for col in 0..N {
            for j in (0..N).rev() {
                let pivot = self.qr[(j, j)];
                if pivot == T::ZERO {
                    continue;
                }
                let mut sum = T::ZERO;
                for i in j..M {
                    sum += self.qr[(i, j)] * q[(i, col)];
                }
                let factor = sum / pivot;
                for i in j..M {
                    q[(i, col)] -= factor * self.qr[(i, j)];
                }
            }
        }
        q
    }

    /// The pivot order: column `j` of `A * P` is column `permutation()[j]` of `A`.
    #[inline]
    #[must_use]
    pub fn permutation(&self) -> [usize; N] {
        self.permutation
    }

    /// Solves the least-squares problem `min ‖A x − b‖`, reusing this factorization. When `A`
    /// is square and full rank this is the exact solve of `A x = b`.
    ///
    /// Returns [`CalcError::SingularMatrix`] if `A` is rank-deficient — a diagonal entry of `R`
    /// at or below `EPSILON * max(M, N)` times the largest — rather than dividing by a tiny pivot.
    ///
    /// ```
    /// use multicalc::linear_algebra::{Matrix, PivotedQr, Vector};
    /// // A x = b has the exact solution x = [1, 1, 1].
    /// let a = Matrix::<3, 3>::new([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 10.0]]);
    /// let b = Vector::new([6.0, 15.0, 25.0]);
    /// let x = PivotedQr::decompose(a).unwrap().solve_least_squares(b).unwrap();
    /// assert!((x[0] - 1.0).abs() < 1e-12);
    /// assert!((x[1] - 1.0).abs() < 1e-12);
    /// assert!((x[2] - 1.0).abs() < 1e-12);
    /// ```
    pub fn solve_least_squares(&self, b: Vector<M, T>) -> Result<Vector<N, T>, CalcError> {
        // Apply the reflectors to b, leaving Qᵀb in the first N entries.
        let mut qtb = b;
        for j in 0..N {
            let pivot = self.qr[(j, j)];
            if pivot == T::ZERO {
                continue;
            }
            let mut sum = T::ZERO;
            for i in j..M {
                sum += self.qr[(i, j)] * qtb[i];
            }
            let factor = sum / pivot;
            for i in j..M {
                qtb[i] -= factor * self.qr[(i, j)];
            }
        }

        // A diagonal entry at or below this fraction of the largest signals rank deficiency.
        let threshold = if N == 0 {
            T::ZERO
        } else {
            T::EPSILON * T::from_usize(M.max(N)) * self.r_diag[0].abs()
        };

        // Back-substitute R y = Qᵀb over the first N rows.
        let mut y = [T::ZERO; N];
        for row in (0..N).rev() {
            if self.r_diag[row].abs() <= threshold {
                return Err(CalcError::SingularMatrix);
            }
            let mut acc = qtb[row];
            for (col, &y_value) in y.iter().enumerate().skip(row + 1) {
                acc -= self.qr[(row, col)] * y_value;
            }
            y[row] = acc / self.r_diag[row];
        }

        // Undo the column permutation: x = P y.
        let mut x = [T::ZERO; N];
        for (j, &target) in self.permutation.iter().enumerate() {
            x[target] = y[j];
        }
        Ok(Vector::new(x))
    }

    /// Turns this factorization into a reusable damped least-squares problem for `b`,
    /// precomputing `Qᵀb` so a whole family of damped systems shares one factorization.
    pub fn into_damped(self, b: Vector<M, T>) -> DampedLeastSquares<N, T> {
        // Apply the reflectors to b, leaving Qᵀb in the first N entries.
        let mut transformed = b;
        for j in 0..N {
            let pivot = self.qr[(j, j)];
            if pivot == T::ZERO {
                continue;
            }
            let mut sum = T::ZERO;
            for i in j..M {
                sum += self.qr[(i, j)] * transformed[i];
            }
            let factor = sum / pivot;
            for i in j..M {
                transformed[i] -= factor * self.qr[(i, j)];
            }
        }
        let mut qt_b = [T::ZERO; N];
        for (dst, &src) in qt_b.iter_mut().zip(transformed.as_array().iter().take(N)) {
            *dst = src;
        }

        DampedLeastSquares {
            r: self.r(),
            qt_b,
            permutation: self.permutation,
            column_norms: self.column_norms,
        }
    }
}

/// A reusable damped least-squares problem built from one QR factorization of `A`.
///
/// Given `A = Q R P` and `b`, it solves `(AᵀA + D²) x = Aᵀb` for any diagonal `D` without
/// refactorizing, using the MINPACK `qrsolv` scheme (Givens rotations that eliminate the `D`
/// rows into `R`). This is the linear subproblem the Levenberg-Marquardt trust region solves.
#[derive(Debug, Clone, Copy)]
#[must_use]
pub struct DampedLeastSquares<const N: usize, T = f64> {
    /// Upper-triangular factor `R`.
    pub(crate) r: Matrix<N, N, T>,
    /// `Qᵀb`, the right-hand side in factored coordinates.
    pub(crate) qt_b: [T; N],
    /// Pivot order carried over from the factorization.
    pub(crate) permutation: [usize; N],
    /// Euclidean norms of the original columns of `A`, in original order.
    pub(crate) column_norms: [T; N],
}

impl<const N: usize, T: Numeric> DampedLeastSquares<N, T> {
    /// Solves `(AᵀA + D²) x = Aᵀb` for the diagonal `D` given by `diag`, returning the solution
    /// and the Cholesky-like factor `S` of `AᵀA + D²`.
    pub fn solve_with_diagonal(&self, diag: &[T; N]) -> (Vector<N, T>, CholeskyFactor<N, T>) {
        // Working matrix: upper triangle is R, lower triangle mirrors it as scratch.
        let mut s = self.r;
        for j in 0..N {
            for i in (j + 1)..N {
                s[(i, j)] = s[(j, i)];
            }
        }

        let mut saved_diag = [T::ZERO; N];
        let mut wa = [T::ZERO; N];
        for j in 0..N {
            saved_diag[j] = s[(j, j)];
            wa[j] = self.qt_b[j];
        }

        let mut s_diag = [T::ZERO; N];
        let quarter = T::from_f64(0.25);

        for j in 0..N {
            let l = self.permutation[j];
            if diag[l] != T::ZERO {
                for entry in s_diag.iter_mut().skip(j) {
                    *entry = T::ZERO;
                }
                s_diag[j] = diag[l];

                // Eliminate the diagonal row of D with Givens rotations, carrying the extra
                // right-hand-side element (initially zero) alongside.
                let mut qtbpj = T::ZERO;
                for k in j..N {
                    if s_diag[k] == T::ZERO {
                        continue;
                    }
                    let (sin, cos) = if s[(k, k)].abs() >= s_diag[k].abs() {
                        let tan = s_diag[k] / s[(k, k)];
                        let cos = T::HALF / (quarter + quarter * tan * tan).sqrt();
                        (cos * tan, cos)
                    } else {
                        let cotan = s[(k, k)] / s_diag[k];
                        let sin = T::HALF / (quarter + quarter * cotan * cotan).sqrt();
                        (sin, sin * cotan)
                    };

                    s[(k, k)] = cos * s[(k, k)] + sin * s_diag[k];
                    let temp = cos * wa[k] + sin * qtbpj;
                    qtbpj = -sin * wa[k] + cos * qtbpj;
                    wa[k] = temp;

                    for i in (k + 1)..N {
                        let rotated = cos * s[(i, k)] + sin * s_diag[i];
                        s_diag[i] = -sin * s[(i, k)] + cos * s_diag[i];
                        s[(i, k)] = rotated;
                    }
                }
            }
            // Store the S diagonal and restore R's.
            s_diag[j] = s[(j, j)];
            s[(j, j)] = saved_diag[j];
        }

        // Solve the triangular system for the permuted solution, zeroing any singular tail.
        let mut nsing = N;
        for j in 0..N {
            if s_diag[j] == T::ZERO && nsing == N {
                nsing = j;
            }
            if nsing < N {
                wa[j] = T::ZERO;
            }
        }
        for k in 0..nsing {
            let j = nsing - 1 - k;
            let mut sum = T::ZERO;
            for i in (j + 1)..nsing {
                sum += s[(i, j)] * wa[i];
            }
            wa[j] = (wa[j] - sum) / s_diag[j];
        }

        // Permute the solution back to original coordinates.
        let mut x = [T::ZERO; N];
        for (j, &target) in self.permutation.iter().enumerate() {
            x[target] = wa[j];
        }

        (Vector::new(x), CholeskyFactor { s, s_diag })
    }

    /// Solves the undamped problem `AᵀA x = Aᵀb` (the Gauss-Newton direction).
    pub fn solve_with_zero_diagonal(&self) -> (Vector<N, T>, CholeskyFactor<N, T>) {
        self.solve_with_diagonal(&[T::ZERO; N])
    }

    /// The largest scaled gradient component `|Aᵀb|ⱼ / (b_norm · ‖columnⱼ‖)`, used by the
    /// gradient convergence test. Returns zero when `b_norm` is zero.
    #[must_use]
    pub fn max_a_t_b_scaled(&self, b_norm: T) -> T {
        if b_norm == T::ZERO {
            return T::ZERO;
        }
        let mut result = T::ZERO;
        for j in 0..N {
            let l = self.permutation[j];
            if self.column_norms[l] != T::ZERO {
                let mut sum = T::ZERO;
                for (i, &qi) in self.qt_b.iter().enumerate().take(j + 1) {
                    sum += self.r[(i, j)] * (qi / b_norm);
                }
                result = max(result, (sum / self.column_norms[l]).abs());
            }
        }
        result
    }

    /// The norm `‖A x‖`, computed as `‖R P x‖` since `Q` has orthonormal columns.
    #[must_use]
    pub fn a_x_norm(&self, x: &Vector<N, T>) -> T {
        let mut w = [T::ZERO; N];
        for j in 0..N {
            let xl = x[self.permutation[j]];
            for (i, slot) in w.iter_mut().enumerate().take(j + 1) {
                *slot += self.r[(i, j)] * xl;
            }
        }
        enorm(&w)
    }

    /// Whether `R` has full rank (no diagonal entry negligible against the largest).
    #[must_use]
    pub fn is_non_singular(&self) -> bool {
        if N == 0 {
            return true;
        }
        let threshold = T::EPSILON * T::from_usize(N) * self.r[(0, 0)].abs();
        (0..N).all(|j| self.r[(j, j)].abs() > threshold)
    }
}

/// The Cholesky-like factor `S` (upper triangular) of `AᵀA + D²` from a damped solve.
///
/// Stored in the working matrix's strict lower triangle (as `Sᵀ`) plus a separate diagonal.
/// Its [`solve`](CholeskyFactor::solve) forward-substitutes `Sᵀ`, which the trust-region
/// parameter search uses for its Newton correction.
#[derive(Debug, Clone, Copy)]
#[must_use]
pub struct CholeskyFactor<const N: usize, T = f64> {
    /// Working matrix whose strict lower triangle holds `Sᵀ`.
    pub(crate) s: Matrix<N, N, T>,
    /// Diagonal of `S`.
    pub(crate) s_diag: [T; N],
}

impl<const N: usize, T: Numeric> CholeskyFactor<N, T> {
    /// Forward-solves `Sᵀ w = rhs`, with `rhs` and the result in the factor's internal order.
    #[must_use]
    pub fn solve(&self, mut rhs: [T; N]) -> [T; N] {
        for j in 0..N {
            if self.s_diag[j] != T::ZERO {
                rhs[j] /= self.s_diag[j];
            } else {
                rhs[j] = T::ZERO;
            }
            let temp = rhs[j];
            for (i, slot) in rhs.iter_mut().enumerate().skip(j + 1) {
                *slot -= self.s[(i, j)] * temp;
            }
        }
        rhs
    }
}