wee-matrix 0.1.0

A matrix library for Rust
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
//! Solvers for systems of linear equations
use lapack;
use lapack::c::Layout;

use errors::*;

use Matrix;
use SubMatrix;
use CloneSub;
use LUDecompose;
use decompose::{c_to_lapack_indexing};

#[inline]
fn max(x: usize, y: usize) -> usize { if x > y { x } else { y } }

#[derive(Debug, Clone)]
pub struct ApproxSoln<T> {
    pub soln: T,
    pub resid: Option<Vec<f64>>,
}

/// Trait to provide various solver implementations for systems of equations AX=B.
pub trait Solve {
    /// The right-hand side type (the type of B in the equation AX=B)
    type Rhs;
    /// The output (solution) type (the type of X in the equation AX=B)
    type Output;

    /// Solve the equation AX=B for X. Solution can either be exact (if possible) or approximate.
    /// Shortcut for calling solve_exact or solve_approx.
    fn solve(&self, b: &Self::Rhs) -> Result<Self::Output>;
    /// Solve the equation AX=B for the exact solution X
    ///
    /// # Failures
    /// Fails if no exact solution is possible for AX=B or the equation is improperly defined.
    fn solve_exact(&self, b: &Self::Rhs) -> Result<Self::Output>;
    /// Solve the equation AX=B for the exact solution X. Assumes A is a symmetric matrix. Only the
    /// upper-triangular portion of A is considered; no failure occurs if A is not symmetric.
    ///
    /// # Failures
    /// Fails if no exact solution is possible for AX=B orthe equation is improperly defined.
    fn solve_symm(&self, b: &Self::Rhs) -> Result<Self::Output>;
    /// Solve the equation AX=B for the approximate solution X.
    ///
    /// # Failures
    /// Fails if no approximate solution is possible for AX=B or the equation is improperly defined.
    fn solve_approx(&self, b: &Self::Rhs) -> Result<ApproxSoln<Self::Output>>;
    /// Compute the inverse of A.
    ///
    /// # Failures
    /// Fails if the matrix is not invertible.
    fn inverse(&self) -> Result<Self::Output>;
}

impl Solve for Matrix {
    type Rhs = Matrix;
    type Output = Matrix;

    fn solve(&self, b: &Matrix) -> Result<Matrix> {
        if self.is_square() {
            self.solve_exact(b)
        } else {
            self.solve_approx(b).map(|approx_soln| approx_soln.soln)
        }
    }

    fn solve_exact(&self, b: &Matrix) -> Result<Matrix> {
        if !self.is_square() {
            return Err(Error::from_kind(ErrorKind::SolveError(
                "solve_exact called with non-square matrix".to_string())))
        }
        let (m, n, nrhs) = (self.nrows(), self.ncols(), b.ncols());
        if b.nrows() != m {
            return Err(Error::from_kind(ErrorKind::SolveError(
                "right-hand side nrows must match left-hand matrix nrows".to_string())))
        }

        let (lda, ldb) = (n, n);
        let mut ipiv = vec![0; n];
        let inout = self.clone();
        let soln = b.clone();
        let (inout_data, soln_data) = (inout.data(), soln.data());
        let info = lapack::c::dgesv(Layout::ColumnMajor, n as i32, nrhs as i32,
            &mut inout_data.values_mut()[..], lda as i32, &mut ipiv[..],
            &mut soln_data.values_mut()[..], ldb as i32);

        if info < 0 {
            Err(Error::from_kind(ErrorKind::SolveError(
                format!("Matrix solver: Invalid call to dgesv in argument {}", -info))))
        } else if info > 0 {
            Err(Error::from_kind(ErrorKind::SolveError(
                "Matrix solver: matrix is singular ".to_string())))
        } else {
            Ok(soln)
        }
    }

    fn solve_symm(&self, b: &Matrix) -> Result<Matrix> {
        if !self.is_square() {
            return Err(Error::from_kind(ErrorKind::SolveError(
                "solve_symm called with non-square matrix".to_string())))
        }
        let (m, n, nrhs) = (self.nrows(), self.ncols(), b.ncols());
        if b.nrows() != m {
            return Err(Error::from_kind(ErrorKind::SolveError(
                "right-hand side nrows must match left-hand matrix nrows".to_string())))
        }

        let (lda, ldb) = (n, n);
        let mut ipiv = vec![0; n];
        let inout = self.clone();
        let soln = b.clone();
        let (inout_data, soln_data) = (inout.data(), soln.data());
        let info = lapack::c::dsysv(Layout::ColumnMajor, b'U',  n as i32, nrhs as i32,
            &mut inout_data.values_mut()[..], lda as i32, &mut ipiv[..],
            &mut soln_data.values_mut()[..], ldb as i32);

        if info < 0 {
            Err(Error::from_kind(ErrorKind::SolveError(
                format!("Symmetric matrix solver: \
                    Invalid call to dsysv in argument {}", -info))))
        } else if info > 0 {
            Err(Error::from_kind(ErrorKind::SolveError(
                "Symmetric matrix solver: matrix is singular ".to_string())))
        } else {
            Ok(soln)
        }
    }

    fn solve_approx(&self, b: &Matrix) -> Result<ApproxSoln<Matrix>> {
        let (m, n, nrhs) = (self.nrows(), self.ncols(), b.ncols());
        if b.nrows() != m {
            return Err(Error::from_kind(ErrorKind::SolveError(
                "right-hand side nrows must match left-hand matrix nrows".to_string())))
        }

        let (lda, ldb) = (m, max(m, n));
        let inout = self.clone();
        let soln = if m >= n {
            b.clone()
        } else {
            b.clone().vcat(&Matrix::zeros(n - m, nrhs))
        };
        let (inout_data, soln_data) = (inout.data(), soln.data());
        let info = lapack::c::dgels(Layout::ColumnMajor, b'N', m as i32, n as i32, nrhs as i32,
            &mut inout_data.values_mut()[..], lda as i32,
            &mut soln_data.values_mut()[..], ldb as i32);

        if info < 0 {
            Err(Error::from_kind(ErrorKind::SolveError(
                format!("Approx matrix solver: \
                    Invalid call to dgels in argument {}", -info))))
        } else if info > 0 {
            Err(Error::from_kind(ErrorKind::SolveError(
                "Approx matrix solver: matrix is rank-deficient ".to_string())))
        } else {
            if m > n {
                Ok(ApproxSoln {
                    soln: soln.clone_subm(0..n, ..).unwrap(),
                    resid: {
                        let mut v: Vec<f64> = Vec::new();
                        for j in 0..nrhs {
                            v.push(soln.subm(n..m, j).unwrap().iter().fold(0.0,
                                |acc, f| acc + f * f));
                        }
                        Some(v)
                    },
                })
            } else {
                Ok(ApproxSoln {
                    soln: soln,
                    resid: None
                })
            }
        }
    }

    fn inverse(&self) -> Result<Matrix> {
        if !self.is_square() {
            return Err(Error::from_kind(ErrorKind::SolveError(
                "inverse called with non-square matrix".to_string())))
        }

        let m = self.nrows();
        let lda = m;

        let lu = self.lu()?;
        let inout = lu.lu_data().clone();
        let ipiv = c_to_lapack_indexing(lu.ipiv_data());

        let inout_data = inout.data();
        let info = lapack::c::dgetri(Layout::ColumnMajor, m as i32,
            &mut inout_data.values_mut()[..], lda as i32,
            &ipiv[..]);

        if info < 0 {
            Err(Error::from_kind(ErrorKind::SolveError(
                format!("Matrix inversion: \
                    Invalid call to dgetri in argument {}", -info))))
        } else if info > 0 {
            Err(Error::from_kind(ErrorKind::SolveError(
                "Matrix inversion: matrix is singular ".to_string())))
        } else {
            Ok(inout)
        }

    }
}

/// Trait providing a solver for the equation A'AX=B
pub trait GramSolve {
    /// The right-hand side type (the type of B in the equation A'AX=B)
    type Rhs;
    /// The output (solution) type (the type of X in the equation A'AX=B)
    type Output;

    /// Solve the equation A'AX=B for the exact solution X
    ///
    /// # Failures
    /// Fails if no exact solution is possible for AX=B or the equation is improperly defined.
    fn gram_solve(&self, b: &Self::Rhs) -> Result<Self::Output>;
}

impl GramSolve for Matrix {
    type Rhs = Matrix;
    type Output = Matrix;

    fn gram_solve(&self, b: &Matrix) -> Result<Matrix> {
        let udu = self.t() * self;
        udu.solve_symm(b)
    }
}

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

    use SymmetrizeMethod;

    macro_rules! assert_error {
        ($res:expr, $err_type:path, $needle:expr, $errtype_str:expr) => {
            assert!($res.is_err());
            let e = $res.unwrap_err();
            println!("{:?}", e.kind());
            match *e.kind() {
                $err_type(ref m) => {
                    assert!(m.find($needle).is_some());
                },
                _ => { panic!(format!("Expected {}, found: {}", $errtype_str, e.kind())) }
            }

        }
    }

    // all should result in singular matrices, but the sum and copyfirst method don't seem to be
    // working for me (probably numerical instability issues)
    #[allow(dead_code)]
    enum SingularMethod {
        Zeros,
        Sum,
        CopyFirst
    }
    fn generate_rank_deficient_matrix(m: usize, n: usize, method: SingularMethod) -> Matrix {
        assert!(m > 1);
        assert!(n > 1);

        let mut a = Matrix::randsn(m, 1);
        let v1_copy = a.clone();
        let mut sum = a.clone();
        for _ in 1..(n - 1) {
            let vi = Matrix::randsn(m, 1);
            a = a.hcat(&vi);
            sum = sum + vi;
        }
        // specify last column based on method chosen
        match method {
            SingularMethod::Zeros       => { a.hcat(&Matrix::zeros(m, 1)) }
            SingularMethod::Sum         => { a.hcat(&sum) }
            SingularMethod::CopyFirst   => { a.hcat(&v1_copy) }
        }
    }
    fn generate_singular_matrix(m: usize, method: SingularMethod) -> Matrix {
        generate_rank_deficient_matrix(m, m, method)
    }

    fn solve_exact_driver(a: &Matrix, b: &Matrix) -> Result<Matrix> {
        a.solve_exact(b).map(|x| {
            assert_eq!(x.dims(), (a.ncols(), b.ncols()));
            x
        })
    }
    #[test]
    fn test_solve_exact() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let x = solve_exact_driver(&a, &b).expect("solve_exact failed unexpectedly");

        assert_eq!(x.dims(), (m, 1));
        println!("a*x\n{}\nb\n{}", &a * &x, &b);
        assert_fpvec_eq!(&a * &x, &b);
    }
    #[test]
    fn test_solve_exact_nonsquare() {
        let (m, n) = (6, 4);
        let a = Matrix::randsn(m, n);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let solve_res = solve_exact_driver(&a, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "non-square", "SolveError");
    }
    #[test]
    fn test_solve_exact_singular() {
        let m = 6;
        let a = generate_singular_matrix(m, SingularMethod::Zeros);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let solve_res = solve_exact_driver(&a, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "singular", "SolveError");
    }
    #[test]
    fn test_solve_exact_invalidrhs() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        println!("{}", a);

        let b = Matrix::randsn(m + 1, 1); // invalud number of rows

        let solve_res = solve_exact_driver(&a, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "right-hand side", "SolveError");
    }

    fn solve_symm_driver(a: &Matrix, b: &Matrix) -> Result<Matrix> {
        a.solve_symm(b).map(|x| {
            assert_eq!(x.dims(), (a.ncols(), b.ncols()));
            x
        })
    }
    #[test]
    fn test_solve_symm() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        let a_symm = a.to_symmetric(SymmetrizeMethod::CopyUpper);
        println!("{}", a_symm);

        let b = Matrix::randsn(m, 1);

        let soln = solve_symm_driver(&a_symm, &b).expect("solve_symm failed unexpectedly");

        assert_eq!(soln.dims(), (m, 1));
        println!("a*x\n{}\nb\n{}", &a_symm * &soln, &b);
        assert_fpvec_eq!(&a_symm * &soln, &b);
    }
    #[test]
    fn test_solve_symm_nonsquare() {
        let (m, n) = (6, 4);
        let a = Matrix::randsn(m, n);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let solve_res = solve_symm_driver(&a, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "non-square", "SolveError");
    }
    #[test]
    fn test_solve_symm_nonsymm() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        println!("a\n{}", a);

        let b = Matrix::randsn(m, 1);

        // should succeed because it never references half the matrix
        let soln = solve_symm_driver(&a, &b).expect("solve_symm failed unexpectedly");
        println!("x\n{}", soln);

        assert_eq!(soln.dims(), (m, 1));

        // a * x != b, since a is the non-symmetric matrix
        println!("a*x\n{}\nb\n{}", &a * &soln, &b);
        assert_fpvec_neq!(&a * &soln, &b);

        // convert a to a symmetric matrix (using upper triangular part since we specify using
        // the upper triangular part of A when calling dsysv) and solution should work
        let a_symm = a.to_symmetric(SymmetrizeMethod::CopyUpper);
        println!("a_symm\n{}", a_symm);
        assert_fpvec_eq!(&a_symm * &soln, &b);
    }
    #[test]
    fn test_solve_symm_singular() {
        let m = 6;
        let a = generate_singular_matrix(m, SingularMethod::Zeros);
        let a_symm = a.to_symmetric(SymmetrizeMethod::CopyUpper);
        println!("{}", a_symm);

        let b = Matrix::randsn(m, 1);

        let solve_res = solve_symm_driver(&a_symm, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "singular", "SolveError");
    }
    #[test]
    fn test_solve_symm_invalidrhs() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        let a_symm = a.to_symmetric(SymmetrizeMethod::CopyUpper);
        println!("{}", a_symm);

        let b = Matrix::randsn(m + 1, 1); // invalud number of rows

        let solve_res = solve_symm_driver(&a_symm, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "right-hand side", "SolveError");
    }

    fn solve_approx_driver(a: &Matrix, b: &Matrix) -> Result<ApproxSoln<Matrix>> {
        a.solve_approx(b).map(|x| {
            assert_eq!(x.soln.dims(), (a.ncols(), b.ncols()));
            x
        })
    }
    #[test]
    fn test_solve_approx_square() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let approx_soln = solve_approx_driver(&a, &b).expect("solve_approx failed unexpectedly");
        assert!(approx_soln.resid.is_none());
        let x = approx_soln.soln;

        assert_eq!(x.dims(), (m, 1));
        println!("a*x\n{}\nb\n{}", &a * &x, &b);
        assert_fpvec_eq!(&a * &x, &b);
    }
    #[test]
    fn test_solve_approx_wide() {
        let (m, n) = (6, 8);
        let a = Matrix::randsn(m, n);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let approx_soln = solve_approx_driver(&a, &b).expect("solve_approx failed unexpectedly");
        assert!(approx_soln.resid.is_none());
        let x = approx_soln.soln;

        assert_eq!(x.dims(), (n, 1));
        println!("a*x\n{}\nb\n{}", &a * &x, &b);
        assert_fpvec_eq!(&a * &x, &b);
    }
    #[test]
    fn test_solve_approx_narrow() {
        let (m, n) = (8, 6);
        let a = Matrix::randsn(m, n);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let approx_soln = solve_approx_driver(&a, &b).expect("solve_approx failed unexpectedly");

        // make sure residual provided by solver matches computed error
        assert!(approx_soln.resid.is_some());
        let mut resid_vec = approx_soln.resid.unwrap();
        assert_eq!(resid_vec.len(), 1);
        let resid = resid_vec.pop().unwrap();
        println!("{}", resid);
        assert!(resid > 0.0);
        let x = approx_soln.soln;

        assert_eq!(x.dims(), (n, 1));
        println!("a*x\n{}\nb\n{}", &a * &x, &b);
        let ax = &a * &x;
        assert_eq!(ax.dims(), (m, 1));
        let mut sumsq = 0.0;
        for i in 0..m {
            let err = ax.get(i, 0).unwrap() - b.get(i, 0).unwrap();
            sumsq += err * err;
        }
        println!("resid:{} sumsq:{} diff:{}", resid, sumsq, (sumsq - resid).abs());
        assert!((sumsq - resid).abs() < 1e-8);
    }
    #[test]
    fn test_solve_approx_rank_deficient() {
        let (m, n) = (8, 6);
        let a = generate_rank_deficient_matrix(m, n, SingularMethod::Zeros);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let solve_res = solve_approx_driver(&a, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "rank-deficient", "SolveError");
    }
    #[test]
    fn test_solve_approx_invalidrhs() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        println!("{}", a);

        let b = Matrix::randsn(m + 1, 1);

        let solve_res = solve_approx_driver(&a, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "right-hand side", "SolveError");
    }

    fn solve_driver(a: &Matrix, b: &Matrix) -> Result<Matrix> {
        a.solve(b).map(|x| {
            assert_eq!(x.dims(), (a.ncols(), b.ncols()));
            x
        })
    }
    #[test]
    fn test_solve_square() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let x = solve_driver(&a, &b).expect("solve failed unexpectedly");

        assert_eq!(x.dims(), (m, 1));
        println!("a*x\n{}\nb\n{}", &a * &x, &b);
        assert_fpvec_eq!(&a * &x, &b);
    }
    #[test]
    fn test_solve_wide() {
        let (m, n) = (6, 8);
        let a = Matrix::randsn(m, n);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let x = solve_driver(&a, &b).expect("solve failed unexpectedly");

        assert_eq!(x.dims(), (n, 1));
        println!("a*x\n{}\nb\n{}", &a * &x, &b);
        assert_fpvec_eq!(&a * &x, &b);
    }
    #[test]
    fn test_solve_narrow() {
        let (m, n) = (8, 6);
        let a = Matrix::randsn(m, n);
        println!("{}", a);

        let b = Matrix::randsn(m, 1);

        let x = solve_driver(&a, &b).expect("solve failed unexpectedly");

        // this call doesn't provide the residual, so make an extra call to the approx driver to
        // get it
        let approx_soln = solve_approx_driver(&a, &b).expect("solve_approx failed unexpectedly");

        assert!(approx_soln.resid.is_some());
        let mut resid_vec = approx_soln.resid.unwrap();
        assert_eq!(resid_vec.len(), 1);
        let resid = resid_vec.pop().unwrap();
        println!("{}", resid);
        assert!(resid > 0.0);

        // ok, now we have the resid and we can compare it to the result of solve()
        assert_eq!(x.dims(), (n, 1));
        println!("a*x\n{}\nb\n{}", &a * &x, &b);
        let ax = &a * &x;
        assert_eq!(ax.dims(), (m, 1));
        let mut sumsq = 0.0;
        for i in 0..m {
            let err = ax.get(i, 0).unwrap() - b.get(i, 0).unwrap();
            sumsq += err * err;
        }
        println!("resid:{} sumsq:{} diff:{}", resid, sumsq, (sumsq - resid).abs());
        assert!((sumsq - resid).abs() < 1e-8);
    }
    #[test]
    fn test_solve_invalidrhs() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        println!("{}", a);

        let b = Matrix::randsn(m + 1, 1);

        let solve_res = solve_driver(&a, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "right-hand side", "SolveError");
    }
    #[test]
    fn test_solve_nrhs_square() {
        let m = 6;
        let nrhs = 3;
        let a = Matrix::randsn(m, m);
        println!("{}", a);

        let b = Matrix::randsn(m, nrhs);

        let x = solve_driver(&a, &b).expect("solve failed unexpectedly");

        assert_eq!(x.dims(), (m, nrhs));
        println!("a*x\n{}\nb\n{}", &a * &x, &b);
        assert_fpvec_eq!(&a * &x, &b);
    }
    #[test]
    fn test_solve_nrhs_wide() {
        let (m, n) = (6, 8);
        let nrhs = 3;
        let a = Matrix::randsn(m, n);
        println!("{}", a);

        let b = Matrix::randsn(m, nrhs);

        let x = solve_driver(&a, &b).expect("solve failed unexpectedly");

        assert_eq!(x.dims(), (n, nrhs));
        println!("a*x\n{}\nb\n{}", &a * &x, &b);
        assert_fpvec_eq!(&a * &x, &b);
    }
    #[test]
    fn test_solve_approx_nrhs_narrow() {
        let (m, n, nrhs) = (8, 6, 3);
        let a = Matrix::randsn(m, n);

        let b = Matrix::randsn(m, nrhs);

        let approx_soln = solve_approx_driver(&a, &b).expect("solve_approx failed unexpectedly");
        assert_eq!(approx_soln.soln.dims(), (n, nrhs));

        println!("soln\n{}", approx_soln.soln);
        assert!(approx_soln.resid.is_some());
        let resid_vec = approx_soln.resid.unwrap();
        println!("resids\n{:?}", resid_vec);
        assert_eq!(resid_vec.len(), nrhs);
        let ax = &a * &approx_soln.soln;
        println!("a*x\n{}\nb\n{}\nax-b\n{}", ax, b, &ax - &b);
        assert_eq!(ax.dims(), (m, nrhs));
        for k in 0..nrhs {
            let mut sumsq = 0.0;
            for i in 0..m {
                let err = b.get(i, k).unwrap() - ax.get(i, k).unwrap();
                sumsq += err * err;
            }
            println!("resid:{} sumsq:{} diff:{}", resid_vec[k], sumsq,
                (sumsq - resid_vec[k]).abs());
            assert_fp_eq!(sumsq, resid_vec[k], 1e-8);
        }
    }

    fn inverse_driver(a: &Matrix) -> Result<Matrix> {
        a.inverse().map(|x| {
            assert_eq!(x.dims(), a.dims());
            x
        })
    }
    #[test]
    fn test_inverse() {
        let m = 6;
        let a = Matrix::randsn(m, m);
        println!("a\n{}", a);

        let a_inverse = inverse_driver(&a).expect("inverse failed unexpectedly");
        println!("a_inverse\n{}\na*a_inverse\n{}", a_inverse, &a * &a_inverse);

        assert_fpvec_eq!(&a * &a_inverse, Matrix::eye(6), 1e-8);
    }
    #[test]
    fn test_inverse_nonsquare() {
        let (m, n) = (8, 6);
        let a = Matrix::randsn(m, n);
        println!("{}", a);

        let inverse_res = inverse_driver(&a);

        assert_error!(inverse_res, ErrorKind::SolveError, "non-square", "SolveError");
    }
    #[test]
    fn test_inverse_singular() {
        let m = 6;
        let a = generate_singular_matrix(m, SingularMethod::Zeros);
        println!("{}", a);

        let inverse_res = inverse_driver(&a);

        assert_error!(inverse_res, ErrorKind::SolveError, "singular", "SolveError");
    }

    fn gram_solve_driver(a: &Matrix, b: &Matrix) -> Result<Matrix> {
        a.gram_solve(b).map(|x| {
            assert_eq!(x.dims(), (a.ncols(), b.ncols()));
            x
        })
    }
    #[test]
    fn test_gram_solve() {
        let a = Matrix::ones(5, 1).hcat(
            &Matrix::from_vec(vec![0.45642, 0.86603, 0.38062, 0.62465, 0.15748], 5, 1));
        let b = Matrix::from_vec(vec![0.886446, 0.096545], 2, 1);

        let x = gram_solve_driver(&a, &b).expect("gram_solve failed");

        let expected_soln = mat![0.78168; -1.21599];
        println!("x\n{}\nexpected\n{}", x, expected_soln);
        assert_fpvec_eq!(x, expected_soln, 1e-5);
        println!("a'*a*x\n{}\nb\n{}", a.t() * &a * &x, &b);
        assert_fpvec_eq!(a.t() * &a * &x, &b);
    }
    #[test]
    fn test_gram_solve_singular() {
        let a = generate_singular_matrix(6, SingularMethod::Zeros);
        println!("{}", a);
        let b = Matrix::randsn(6, 1);

        let solve_res = gram_solve_driver(&a, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "singular", "SolveError");
    }
    #[test]
    fn test_gram_solve_invalidrhs() {
        let (m, nrhs) = (6, 1);
        let a = Matrix::randsn(m, m);
        let b = Matrix::randsn(m + 1, nrhs); // incorrect size

        let solve_res = gram_solve_driver(&a, &b);

        assert_error!(solve_res, ErrorKind::SolveError, "right-hand side", "SolveError");
    }
}