algebra_linear 1.2.0

Pure-Rust BLAS, LAPACK, and sparse LU
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Native complex LAPACK
//!
//! Each function names the LAPACK 3.12.1 routine it follows and is a
//! transliteration of that routine's *unblocked* form, with LAPACK's 1-based
//! indexing and `lda`/`lwork` arguments mechanically turned into 0-based indices
//! and `usize` lengths:
//!
//! | Here | Reference |
//! |---|---|
//! | [`zgetrf`] | `zgetf2` (the unblocked kernel of `zgetrf`) |
//! | [`zgetri`] | `zgetri`'s unblocked branch, over `ztrti2`'s upper inverse |
//! | [`zgesv`] | `zgetf2` + `zgetrs('N')` for a single right-hand side |
//! | [`zpotrf`] | `zpotf2`, both triangles |
//!
//! # Why transliterate rather than re-derive
//!
//! The complex routines differ from their real counterparts only in the
//! arithmetic, and that is exactly where the mistakes live: a missing conjugate
//! in the Cholesky update, or a transpose where a Hermitian transpose is
//! required, still produces a plausible-looking factor. Keeping the operations
//! in the reference's order makes the two comparable line by line, and the
//! parity tests in `native::parity` then compare the observable results
//! element-wise against the reference library.
//!
//! # Conventions
//!
//! * Column-major, `data[i + j * n]`, matching the column-major storage of a matrix.
//! * `usize` lengths, 0-based indices, no workspace arguments.
//! * Only the triangle LAPACK says is read is read, and only the triangle it
//!   says is written is written. See [`zpotrf`] in particular: `'U'` *reads* the
//!   upper triangle alone, so a caller may legitimately pass zeros below the
//!   diagonal, and it *writes* only the upper triangle.

use super::{LapackError, ZERO_DETERMINANT};
use crate::Complex64;

/// The zero element, named so the arithmetic reads like the Fortran `ZERO`.
fn zero() -> Complex64 {
    Complex64::new(0.0, 0.0)
}

/// The one element, named so the arithmetic reads like the Fortran `ONE`.
fn one() -> Complex64 {
    Complex64::new(1.0, 0.0)
}

/// `IZAMAX`'s measure of a complex entry: `|Re| + |Im|`.
///
/// This is what LAPACK's `izamax` maximizes when choosing a pivot, so using the
/// modulus here would pick a different row on some matrices and diverge from the
/// reference for no benefit.
fn abs1(z: Complex64) -> f64 {
    z.re.abs() + z.im.abs()
}

/// `zgetrf`/`zgetf2`: LU factorization with partial pivoting, in place.
///
/// Overwrites `a` with the packed unit-lower `L` (multipliers below the
/// diagonal) and `U`, and returns LAPACK's pivot indices made 0-based: at step
/// `k` rows `k` and `ipiv[k]` were interchanged. The last entry is always `n - 1`
/// because the final column has nothing to pivot against.
///
/// # Errors
///
/// [`LapackError::Singular`] if the pivot is zero to working precision, and
/// [`LapackError::BadShape`] if `a.len() != n * n`.
pub fn zgetrf(a: &mut [Complex64], n: usize) -> Result<Vec<usize>, LapackError> {
    if a.len() != n * n {
        return Err(LapackError::BadShape);
    }
    if n == 0 {
        return Ok(Vec::new());
    }

    let mut ipiv = vec![0usize; n];

    for j in 0..n {
        // JP = J - 1 + IZAMAX(M-J+1, A(J,J), 1) in Fortran, i.e. the first row at
        // or below the diagonal with the largest |Re| + |Im|.
        let mut best = abs1(a[j + j * n]);
        let mut jp = j;
        for i in (j + 1)..n {
            let cand = abs1(a[i + j * n]);
            if cand > best {
                best = cand;
                jp = i;
            }
        }
        ipiv[j] = jp;

        // LAPACK tests `A(JP,J) /= 0`; the crate's tolerance matches the
        // accelerated path's notion of "singular" so both agree on which systems
        // can be solved.
        if best <= ZERO_DETERMINANT {
            return Err(LapackError::Singular);
        }

        // ZSWAP(N, A(J,1), LDA, A(JP,1), LDA): swap the *whole* rows j and jp,
        // not just the trailing submatrix.
        if jp != j {
            for k in 0..n {
                a.swap(k * n + j, k * n + jp);
            }
        }

        // ZRSCL(M-J, A(J,J), A(J+1,J), 1): scale the column below the pivot by
        // 1/A(J,J) *before* the rank-1 update, which is what leaves the
        // multipliers in place of the eliminated entries.
        let d = a[j + j * n];
        for i in (j + 1)..n {
            a[i + j * n] /= d;
        }

        // ZGERU(M-J, N-J, -ONE, A(J+1,J), 1, A(J,J+1), LDA, A(J+1,J+1), LDA).
        for k in (j + 1)..n {
            let ajk = a[j + k * n];
            for i in (j + 1)..n {
                a[i + k * n] -= a[i + j * n] * ajk;
            }
        }
    }

    Ok(ipiv)
}

/// `ztrti2('Upper', 'Non-unit')`: invert an upper triangular matrix in place.
///
/// The diagonal entry of the original is replaced by its reciprocal before the
/// column is formed, so the running product uses 1/U(j,j) exactly as the
/// reference does. Returns [`LapackError::Singular`] if a diagonal is zero.
fn ztrti2_upper(a: &mut [Complex64], n: usize) -> Result<(), LapackError> {
    for j in 0..n {
        // ZTRTRI's own singularity test, done up front there; here it is folded
        // into the only place a zero diagonal would be divided by.
        if a[j + j * n] == zero() {
            return Err(LapackError::Singular);
        }
        let inv = one() / a[j + j * n];
        a[j + j * n] = inv;
        let ajj = -inv;

        // ZTRMV('Upper','No transpose','N', J-1, A, LDA, A(1,J), 1), followed by
        // ZSCAL(J-1, AJJ, A(1,J), 1). The vector is staged so the in-place
        // triangular multiply cannot read a partly-updated column.
        let mut x = vec![zero(); j];
        for i in 0..j {
            let mut sum = zero();
            for k in i..j {
                sum += a[i + k * n] * a[k + j * n];
            }
            x[i] = sum;
        }
        for i in 0..j {
            a[i + j * n] = ajj * x[i];
        }
    }
    Ok(())
}

/// `zgetrf_rect`: LU factorization of a complex rectangular panel.
///
/// Complex twin of [`super::dgetrf_rect`]; the panel is `nrows x ncols` with
/// `nrows >= ncols`, and the pivot search uses `|Re| + |Im|`, as `zgetf2` does.
///
/// # Errors
///
/// [`LapackError::BadShape`] if the lengths disagree or `nrows < ncols`, and
/// [`LapackError::Singular`] if a pivot is zero.
pub fn zgetrf_rect(a: &mut [Complex64], nrows: usize, ncols: usize, ipiv: &mut [usize]) -> Result<(), LapackError> {
    if nrows < ncols || a.len() != nrows * ncols || ipiv.len() != ncols {
        return Err(LapackError::BadShape);
    }
    if ncols == 0 {
        return Ok(());
    }
    for k in 0..ncols {
        let mut pivot = k;
        let mut best = abs1(a[k + k * nrows]);
        for i in k + 1..nrows {
            let cand = abs1(a[i + k * nrows]);
            if cand > best {
                best = cand;
                pivot = i;
            }
        }
        if best <= ZERO_DETERMINANT {
            return Err(LapackError::Singular);
        }
        if pivot != k {
            for j in 0..ncols {
                a.swap(j * nrows + k, j * nrows + pivot);
            }
        }
        ipiv[k] = pivot;
        let d = a[k + k * nrows];
        for i in k + 1..nrows {
            let factor = a[i + k * nrows] / d;
            a[i + k * nrows] = factor;
            for j in k + 1..ncols {
                a[i + j * nrows] -= factor * a[k + j * nrows];
            }
        }
    }
    Ok(())
}

/// `zgetri`: inverse of a matrix from its [`zgetrf`] factorization, in place.
///
/// Follows the reference: invert `U`, solve `inv(A) * L = inv(U)` column by
/// column (the unblocked branch), then undo the row interchanges as column
/// swaps.
///
/// # Errors
///
/// [`LapackError::BadShape`] if the shapes disagree, or
/// [`LapackError::Singular`] if a diagonal of `U` is zero.
pub fn zgetri(lu: &mut [Complex64], n: usize, ipiv: &[usize]) -> Result<(), LapackError> {
    if lu.len() != n * n || ipiv.len() != n {
        return Err(LapackError::BadShape);
    }
    if n == 0 {
        return Ok(());
    }

    // Form inv(U). A zero diagonal means U was singular.
    ztrti2_upper(lu, n)?;

    // Solve inv(A)*L = inv(U) for inv(A): the unblocked branch of zgetri.
    let mut work = vec![zero(); n];
    let mut y = vec![zero(); n];
    for j in (0..n).rev() {
        // Copy the current column of L to WORK and replace it with zeros; the
        // gemv below must see a strictly-upper A.
        for i in (j + 1)..n {
            work[i] = lu[i + j * n];
            lu[i + j * n] = zero();
        }

        if j + 1 < n {
            // ZGEMV('N', N, N-J, -ONE, A(1,J+1), LDA, WORK(J+1), 1, ONE, A(1,J), 1).
            for i in 0..n {
                let mut sum = zero();
                for k in (j + 1)..n {
                    sum += lu[i + k * n] * work[k];
                }
                y[i] = sum;
            }
            for i in 0..n {
                lu[i + j * n] -= y[i];
            }
        }
    }

    // ZSWAP, column by column: J runs from N-1 down to 1 in Fortran, so the last
    // pivot is deliberately skipped (it is always N).
    for j in (0..n.saturating_sub(1)).rev() {
        let jp = ipiv[j];
        if jp != j {
            for i in 0..n {
                lu.swap(i + j * n, i + jp * n);
            }
        }
    }

    Ok(())
}

/// `zgesv`: solve `A X = B` for a single right-hand side.
///
/// `a` is `n x n` column-major and is destroyed; `b` is the right-hand side on
/// entry and the solution on exit. This is `zgetrf` followed by `zgetrs('N')`
/// with `nrhs == 1`: apply the interchanges to `b`, forward-solve with the unit
/// lower triangle, back-solve with the upper one.
///
/// # Errors
///
/// [`LapackError::Singular`] if `A` is singular, [`LapackError::BadShape`] if the
/// shapes disagree.
pub fn zgesv(a: &mut [Complex64], b: &mut [Complex64]) -> Result<(), LapackError> {
    let n = b.len();
    if a.len() != n * n {
        return Err(LapackError::BadShape);
    }
    if n == 0 {
        return Ok(());
    }

    let ipiv = zgetrf(a, n)?;

    // ZLASWP(NRHS=1, B, LDB, 1, N, IPIV, 1).
    for k in 0..n {
        b.swap(k, ipiv[k]);
    }

    // ZTRSM('Left','Lower','No transpose','Unit', ...).
    for i in 0..n {
        let mut sum = b[i];
        for j in 0..i {
            sum -= a[i + j * n] * b[j];
        }
        b[i] = sum;
    }

    // ZTRSM('Left','Upper','No transpose','Non-unit', ...).
    for i in (0..n).rev() {
        let mut sum = b[i];
        for j in (i + 1)..n {
            sum -= a[i + j * n] * b[j];
        }
        b[i] = sum / a[i + i * n];
    }

    Ok(())
}

/// `zpotrf`/`zpotf2`: Cholesky factorization of a Hermitian positive-definite
/// matrix, in place.
///
/// With `upper == false` it finds the lower triangular `L` with `A = L Lá´´`;
/// with `upper == true` it finds the upper triangular `U` with `A = Uá´´ U`.
///
/// # The triangle convention
///
/// As LAPACK specifies, only the requested triangle is read and only that
/// triangle is written:
///
/// * `upper == false` reads and writes `i >= j`; the strict upper triangle is
///   left exactly as it was found.
/// * `upper == true` reads and writes `i <= j`; the strict lower triangle is
///   left exactly as it was found, so a caller may pass zeros there without the
///   result depending on them.
///
/// This is why the upper factor is computed by its own branch rather than by
/// transposing a lower factor: mirroring the other triangle first would read
/// entries LAPACK says are not part of the input, and writing it would clobber
/// entries LAPACK says must survive.
///
/// # Errors
///
/// [`LapackError::Singular`] if `A` is not positive definite (a non-positive or
/// NaN diagonal appears), [`LapackError::BadShape`] on a shape mismatch. As in
/// `zpotf2`, the diagonal is overwritten with the offending value before the
/// error is returned.
pub fn zpotrf(a: &mut [Complex64], n: usize, upper: bool) -> Result<(), LapackError> {
    if a.len() != n * n {
        return Err(LapackError::BadShape);
    }

    if upper {
        // Compute the Cholesky factorization A = U**H * U.
        for j in 0..n {
            // AJJ = DBLE(A(J,J)) - DBLE(ZDOTC(J-1, A(1,J), 1, A(1,J), 1)); the
            // dot product is real and equals sum |A(i,j)|^2.
            let mut ajj = a[j + j * n].re;
            for i in 0..j {
                ajj -= a[i + j * n].norm_sqr();
            }
            if ajj <= 0.0 || ajj.is_nan() {
                a[j + j * n] = Complex64::new(ajj, 0.0);
                return Err(LapackError::Singular);
            }
            let ajj = ajj.sqrt();
            a[j + j * n] = Complex64::new(ajj, 0.0);

            if j + 1 < n {
                // ZLACGV conjugates column j, ZGEMV('Transpose') forms
                // A(J,J+1:N) - A(1:J-1,J+1:N)^T * conj(A(1:J-1,J)), and ZLACGV
                // restores the column. Composing the two gives the conjugated
                // product used below without ever writing the conjugated values.
                //
                // ZDSCAL(N-J, 1/AJJ, A(J,J+1), LDA) divides the row by the real
                // diagonal.
                for k in (j + 1)..n {
                    let mut sum = a[j + k * n];
                    for i in 0..j {
                        sum -= a[i + k * n] * a[i + j * n].conj();
                    }
                    a[j + k * n] = sum / ajj;
                }
            }
        }
    } else {
        // Compute the Cholesky factorization A = L*L**H.
        for j in 0..n {
            // AJJ = DBLE(A(J,J)) - sum |A(j,k)|^2 over the already-formed row.
            let mut ajj = a[j + j * n].re;
            for k in 0..j {
                ajj -= a[j + k * n].norm_sqr();
            }
            if ajj <= 0.0 || ajj.is_nan() {
                a[j + j * n] = Complex64::new(ajj, 0.0);
                return Err(LapackError::Singular);
            }
            let ajj = ajj.sqrt();
            a[j + j * n] = Complex64::new(ajj, 0.0);

            // ZLACGV on row j, ZGEMV('No transpose') forming
            // A(J+1:N,J) - A(J+1:N,1:J-1) * conj(A(J,1:J-1)), then ZLACGV back;
            // ZDSCAL divides the column by the real diagonal.
            for i in (j + 1)..n {
                let mut sum = a[i + j * n];
                for k in 0..j {
                    sum -= a[i + k * n] * a[j + k * n].conj();
                }
                a[i + j * n] = sum / ajj;
            }
        }
    }

    Ok(())
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cpx;

    /// A small Hermitian positive-definite matrix with non-zero imaginary parts,
    /// so a conjugation mistake cannot cancel out. Column-major, `n = 3`.
    fn spd3() -> Vec<Complex64> {
        // Row-major: [[5, 1+i, 2i], [1-i, 6, 1-2i], [-2i, 1+2i, 7]]
        vec![
            cpx!(5.0, 0.0),
            cpx!(1.0, -1.0),
            cpx!(0.0, 2.0), // column 0
            cpx!(1.0, 1.0),
            cpx!(6.0, 0.0),
            cpx!(1.0, 2.0), // column 1
            cpx!(0.0, -2.0),
            cpx!(1.0, -2.0),
            cpx!(7.0, 0.0), // column 2
        ]
    }

    /// A complex matrix that is neither Hermitian nor triangular.
    fn general3() -> Vec<Complex64> {
        // Row-major: [[4+1i, 1-2i, 2], [1+3i, 3, 0-1i], [2-1i, 1+1i, 5+2i]]
        vec![
            cpx!(4.0, 1.0),
            cpx!(1.0, 3.0),
            cpx!(2.0, -1.0), // column 0
            cpx!(1.0, -2.0),
            cpx!(3.0, 0.0),
            cpx!(1.0, 1.0), // column 1
            cpx!(2.0, 0.0),
            cpx!(0.0, -1.0),
            cpx!(5.0, 2.0), // column 2
        ]
    }

    fn mat_vec(a: &[Complex64], x: &[Complex64], n: usize) -> Vec<Complex64> {
        let mut y = vec![cpx!(0.0, 0.0); n];
        for i in 0..n {
            for j in 0..n {
                y[i] += a[i + j * n] * x[j];
            }
        }
        y
    }

    /// `zgetrf` + `zgetri` must give the inverse: `A * A^-1 = I`.
    ///
    /// The product is accumulated in a different order from the factorization,
    /// so this is not the trivial identity a shared bug could fake.
    #[test]
    fn zgetrf_then_zgetri_inverts() {
        let a = general3();
        let n = 3;
        let mut lu = a.clone();
        let piv = zgetrf(&mut lu, n).unwrap();
        assert_eq!(piv.len(), n);
        assert_eq!(piv[n - 1], n - 1, "the last pivot index is always n-1");
        zgetri(&mut lu, n, &piv).unwrap();

        // lu is now A^-1: check A * A^-1 = I column by column.
        for j in 0..n {
            let mut e = vec![cpx!(0.0, 0.0); n];
            e[j] = cpx!(1.0, 0.0);
            let inv_col = mat_vec(&lu, &e, n); // A^-1 e_j is column j of A^-1
            let back = mat_vec(&a, &inv_col, n);
            for i in 0..n {
                let want = if i == j { cpx!(1.0, 0.0) } else { cpx!(0.0, 0.0) };
                assert!((back[i] - want).norm() < 1e-12, "A*A^-1 column {j} row {i} = {:?}", back[i]);
            }
        }
    }

    /// `zgesv` recovers a known solution.
    #[test]
    fn zgesv_solves_a_system() {
        let a = general3();
        let n = 3;
        let x_true = [cpx!(1.0, -2.0), cpx!(3.0, 0.5), cpx!(-1.0, 1.0)];
        let mut b = mat_vec(&a, &x_true, n);
        let mut a_work = a.clone();
        zgesv(&mut a_work, &mut b).unwrap();
        for i in 0..n {
            assert!((b[i] - x_true[i]).norm() < 1e-12, "x[{i}] = {:?} vs {:?}", b[i], x_true[i]);
        }
    }

    /// `zgetrf` reports an exactly singular matrix instead of dividing by a zero
    /// pivot. Column 2 is `2 * column 0`, so the second column is dependent.
    #[test]
    fn zgetrf_detects_a_singular_matrix() {
        let mut a = vec![
            cpx!(1.0, 1.0),
            cpx!(3.0, 0.0), // column 0
            cpx!(2.0, 2.0),
            cpx!(6.0, 0.0), // column 1 = 2 * column 0
        ];
        assert_eq!(zgetrf(&mut a, 2), Err(LapackError::Singular));
    }

    /// A shape mismatch is reported, not read out of bounds.
    #[test]
    fn bad_shapes_are_rejected() {
        let mut a = vec![cpx!(1.0, 0.0); 3];
        assert_eq!(zgetrf(&mut a, 2), Err(LapackError::BadShape));
        let ipiv = [0usize; 1];
        assert_eq!(zgetri(&mut a, 2, &ipiv), Err(LapackError::BadShape));
        let mut b = vec![cpx!(1.0, 0.0); 2];
        assert_eq!(zgesv(&mut a, &mut b), Err(LapackError::BadShape));
        assert_eq!(zpotrf(&mut a, 2, false), Err(LapackError::BadShape));
    }

    /// `zpotrf` gives `A = L L^H` (lower) and `A = U^H U` (upper).
    ///
    /// Only the requested triangle is written, so the factor has to be extracted
    /// from that triangle rather than read from the whole buffer.
    #[test]
    fn zpotrf_factorises_both_triangles() {
        let a = spd3();
        let n = 3;

        let mut lower = a.clone();
        zpotrf(&mut lower, n, false).unwrap();
        for i in 0..n {
            for j in 0..=i {
                let mut sum = cpx!(0.0, 0.0);
                for k in 0..=j {
                    sum += lower[i + k * n] * lower[j + k * n].conj();
                }
                assert!(
                    (sum - a[i + j * n]).norm() < 1e-12,
                    "L L^H at ({i},{j}) = {:?} vs {:?}",
                    sum,
                    a[i + j * n]
                );
            }
        }

        let mut upper = a.clone();
        zpotrf(&mut upper, n, true).unwrap();
        for i in 0..n {
            for j in i..n {
                let mut sum = cpx!(0.0, 0.0);
                for k in 0..=i {
                    sum += upper[k + i * n].conj() * upper[k + j * n];
                }
                assert!(
                    (sum - a[i + j * n]).norm() < 1e-12,
                    "U^H U at ({i},{j}) = {:?} vs {:?}",
                    sum,
                    a[i + j * n]
                );
            }
        }
    }

    /// A Hermitian matrix with a negative eigenvalue is not positive definite and
    /// must be rejected rather than factored.
    #[test]
    fn zpotrf_rejects_a_non_positive_definite_matrix() {
        // [[1, 2], [2, 1]] has eigenvalues 3 and -1.
        let mut a = vec![cpx!(1.0, 0.0), cpx!(2.0, 0.0), cpx!(2.0, 0.0), cpx!(1.0, 0.0)];
        assert_eq!(zpotrf(&mut a, 2, false), Err(LapackError::Singular));
    }

    /// `n == 0` is a no-op everywhere, not a panic or an error.
    #[test]
    fn empty_matrices_are_handled() {
        let mut a: Vec<Complex64> = Vec::new();
        assert!(zgetrf(&mut a, 0).unwrap().is_empty());
        assert_eq!(zgetri(&mut a, 0, &[]), Ok(()));
        let mut b: Vec<Complex64> = Vec::new();
        assert_eq!(zgesv(&mut a, &mut b), Ok(()));
        assert_eq!(zpotrf(&mut a, 0, false), Ok(()));
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod rect_tests {
    use super::*;

    #[test]
    fn zgetrf_rect_reconstructs_a_tall_panel() {
        use crate::cpx;
        let (nrows, ncols) = (3, 2);
        let a0 = vec![
            cpx!(1.0, 1.0),
            cpx!(3.0, 0.0),
            cpx!(2.0, -1.0),
            cpx!(2.0, 0.0),
            cpx!(0.0, -1.0),
            cpx!(4.0, 0.0),
        ];
        let mut a = a0.clone();
        let mut ipiv = vec![0usize; ncols];
        zgetrf_rect(&mut a, nrows, ncols, &mut ipiv).unwrap();
        let mut pa = a0.clone();
        for k in 0..ncols {
            for j in 0..ncols {
                pa.swap(j * nrows + k, j * nrows + ipiv[k]);
            }
        }
        for j in 0..ncols {
            for i in 0..nrows {
                let mut sum = Complex64::new(0.0, 0.0);
                for l in 0..ncols {
                    let l_il = if i == l {
                        Complex64::new(1.0, 0.0)
                    } else if i > l {
                        a[i + l * nrows]
                    } else {
                        Complex64::new(0.0, 0.0)
                    };
                    let u_lj = if l <= j { a[l + j * nrows] } else { Complex64::new(0.0, 0.0) };
                    sum += l_il * u_lj;
                }
                assert!((sum - pa[i + j * nrows]).norm() < 1e-12, "entry {i},{j}");
            }
        }
    }

    #[test]
    fn zgetrf_rect_rejects_bad_shapes() {
        let mut a = [crate::cpx!(1.0, 0.0); 4];
        let mut ipiv = [0usize; 2];
        assert_eq!(zgetrf_rect(&mut a, 1, 2, &mut ipiv).unwrap_err(), LapackError::BadShape);
    }
}