-
Notifications
You must be signed in to change notification settings - Fork 65
/
least_squares.rs
138 lines (119 loc) · 4.2 KB
/
least_squares.rs
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
/// Solve least square problem `|b - Ax|`
use approx::AbsDiffEq;
use ndarray::*;
use ndarray_linalg::*;
/// A is square. `x = A^{-1} b`, `|b - Ax| = 0`
fn test_exact<T: Scalar + Lapack>(a: Array2<T>) {
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let b: Array1<T> = random_using(3, &mut rng);
let result = a.least_squares(&b).unwrap();
// unpack result
let x = result.solution;
let residual_l2_square = result.residual_sum_of_squares.unwrap()[()];
// must be full-rank
assert_eq!(result.rank, 3);
// |b - Ax| == 0
assert!(residual_l2_square < T::real(1.0e-4));
// b == Ax
let ax = a.dot(&x);
assert_close_max!(&b, &ax, T::real(1.0e-4));
}
macro_rules! impl_exact {
($scalar:ty) => {
paste::item! {
#[test]
fn [<least_squares_ $scalar _exact>]() {
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let a: Array2<$scalar> = random_using((3, 3), &mut rng);
test_exact(a)
}
#[test]
fn [<least_squares_ $scalar _exact_t>]() {
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let a: Array2<$scalar> = random_using((3, 3).f(), &mut rng);
test_exact(a)
}
}
};
}
impl_exact!(f32);
impl_exact!(f64);
impl_exact!(c32);
impl_exact!(c64);
/// #column < #row case.
/// Linear problem is overdetermined, `|b - Ax| > 0`.
fn test_overdetermined<T: Scalar + Lapack>(a: Array2<T>)
where
T::Real: AbsDiffEq<Epsilon = T::Real>,
{
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let b: Array1<T> = random_using(4, &mut rng);
let result = a.least_squares(&b).unwrap();
// unpack result
let x = result.solution;
let residual_l2_square = result.residual_sum_of_squares.unwrap()[()];
// Must be full-rank
assert_eq!(result.rank, 3);
// eval `residual = b - Ax`
let residual = &b - &a.dot(&x);
assert!(residual_l2_square.abs_diff_eq(&residual.norm_l2().powi(2), T::real(1.0e-4)));
// `|residual| < |b|`
assert!(residual.norm_l2() < b.norm_l2());
}
macro_rules! impl_overdetermined {
($scalar:ty) => {
paste::item! {
#[test]
fn [<least_squares_ $scalar _overdetermined>]() {
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let a: Array2<$scalar> = random_using((4, 3), &mut rng);
test_overdetermined(a)
}
#[test]
fn [<least_squares_ $scalar _overdetermined_t>]() {
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let a: Array2<$scalar> = random_using((4, 3).f(), &mut rng);
test_overdetermined(a)
}
}
};
}
impl_overdetermined!(f32);
impl_overdetermined!(f64);
impl_overdetermined!(c32);
impl_overdetermined!(c64);
/// #column > #row case.
/// Linear problem is underdetermined, `|b - Ax| = 0` and `x` is not unique
fn test_underdetermined<T: Scalar + Lapack>(a: Array2<T>) {
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let b: Array1<T> = random_using(3, &mut rng);
let result = a.least_squares(&b).unwrap();
assert_eq!(result.rank, 3);
assert!(result.residual_sum_of_squares.is_none());
// b == Ax
let x = result.solution;
let ax = a.dot(&x);
assert_close_max!(&b, &ax, T::real(1.0e-4));
}
macro_rules! impl_underdetermined {
($scalar:ty) => {
paste::item! {
#[test]
fn [<least_squares_ $scalar _underdetermined>]() {
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let a: Array2<$scalar> = random_using((3, 4), &mut rng);
test_underdetermined(a)
}
#[test]
fn [<least_squares_ $scalar _underdetermined_t>]() {
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let a: Array2<$scalar> = random_using((3, 4).f(), &mut rng);
test_underdetermined(a)
}
}
};
}
impl_underdetermined!(f32);
impl_underdetermined!(f64);
impl_underdetermined!(c32);
impl_underdetermined!(c64);