-
Notifications
You must be signed in to change notification settings - Fork 65
/
det.rs
196 lines (189 loc) · 6.38 KB
/
det.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
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
use ndarray::*;
use ndarray_linalg::*;
use num_traits::{Float, One, Zero};
/// Returns the matrix with the specified `row` and `col` removed.
fn matrix_minor<A, S>(a: &ArrayBase<S, Ix2>, (row, col): (usize, usize)) -> Array2<A>
where
A: Scalar,
S: Data<Elem = A>,
{
let mut select_rows = (0..a.nrows()).collect::<Vec<_>>();
select_rows.remove(row);
let mut select_cols = (0..a.ncols()).collect::<Vec<_>>();
select_cols.remove(col);
a.select(Axis(0), &select_rows)
.select(Axis(1), &select_cols)
}
/// Computes the determinant of matrix `a`.
///
/// Note: This implementation is written to be clearly correct so that it's
/// useful for verification, but it's very inefficient.
fn det_naive<A, S>(a: &ArrayBase<S, Ix2>) -> A
where
A: Scalar,
S: Data<Elem = A>,
{
assert_eq!(a.nrows(), a.ncols());
match a.ncols() {
0 => A::one(),
1 => a[(0, 0)],
cols => (0..cols)
.map(|col| {
let sign = if col % 2 == 0 { A::one() } else { -A::one() };
sign * a[(0, col)] * det_naive(&matrix_minor(a, (0, col)))
})
.fold(A::zero(), |sum, subdet| sum + subdet),
}
}
#[test]
fn det_empty() {
macro_rules! det_empty {
($elem:ty) => {
let a: Array2<$elem> = Array2::zeros((0, 0));
let det = One::one();
let (sign, ln_det) = (One::one(), Zero::zero());
assert_eq!(a.factorize().unwrap().det().unwrap(), det);
assert_eq!(a.factorize().unwrap().sln_det().unwrap(), (sign, ln_det));
assert_eq!(a.factorize().unwrap().det_into().unwrap(), det);
assert_eq!(
a.factorize().unwrap().sln_det_into().unwrap(),
(sign, ln_det)
);
assert_eq!(a.det().unwrap(), det);
assert_eq!(a.sln_det().unwrap(), (sign, ln_det));
assert_eq!(a.clone().det_into().unwrap(), det);
assert_eq!(a.sln_det_into().unwrap(), (sign, ln_det));
};
}
det_empty!(f64);
det_empty!(f32);
det_empty!(c64);
det_empty!(c32);
}
#[test]
fn det_zero() {
macro_rules! det_zero {
($elem:ty) => {
let a: Array2<$elem> = Array2::zeros((1, 1));
let det = Zero::zero();
let (sign, ln_det) = (Zero::zero(), Float::neg_infinity());
assert_eq!(a.det().unwrap(), det);
assert_eq!(a.sln_det().unwrap(), (sign, ln_det));
assert_eq!(a.clone().det_into().unwrap(), det);
assert_eq!(a.sln_det_into().unwrap(), (sign, ln_det));
};
}
det_zero!(f64);
det_zero!(f32);
det_zero!(c64);
det_zero!(c32);
}
#[test]
fn det_zero_nonsquare() {
macro_rules! det_zero_nonsquare {
($elem:ty, $shape:expr) => {
let a: Array2<$elem> = Array2::zeros($shape);
assert!(a.det().is_err());
assert!(a.sln_det().is_err());
assert!(a.clone().det_into().is_err());
assert!(a.sln_det_into().is_err());
};
}
for &shape in &[(1, 2).into_shape(), (1, 2).f()] {
det_zero_nonsquare!(f64, shape);
det_zero_nonsquare!(f32, shape);
det_zero_nonsquare!(c64, shape);
det_zero_nonsquare!(c32, shape);
}
}
#[test]
fn det() {
fn det_impl<A>(a: Array2<A>, rtol: A::Real)
where
A: Scalar + Lapack,
{
let det = det_naive(&a);
let sign = det.div_real(det.abs());
let ln_det = Float::ln(det.abs());
assert_rclose!(a.factorize().unwrap().det().unwrap(), det, rtol);
{
let result = a.factorize().unwrap().sln_det().unwrap();
assert_rclose!(result.0, sign, rtol);
assert_rclose!(result.1, ln_det, rtol);
}
assert_rclose!(a.factorize().unwrap().det_into().unwrap(), det, rtol);
{
let result = a.factorize().unwrap().sln_det_into().unwrap();
assert_rclose!(result.0, sign, rtol);
assert_rclose!(result.1, ln_det, rtol);
}
assert_rclose!(a.det().unwrap(), det, rtol);
{
let result = a.sln_det().unwrap();
assert_rclose!(result.0, sign, rtol);
assert_rclose!(result.1, ln_det, rtol);
}
assert_rclose!(a.clone().det_into().unwrap(), det, rtol);
{
let result = a.sln_det_into().unwrap();
assert_rclose!(result.0, sign, rtol);
assert_rclose!(result.1, ln_det, rtol);
}
}
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
for rows in 1..5 {
det_impl(random_regular_using::<f64, _>(rows, &mut rng), 1e-9);
det_impl(random_regular_using::<f32, _>(rows, &mut rng), 1e-4);
det_impl(random_regular_using::<c64, _>(rows, &mut rng), 1e-9);
det_impl(random_regular_using::<c32, _>(rows, &mut rng), 1e-4);
det_impl(
random_regular_using::<f64, _>(rows, &mut rng)
.t()
.to_owned(),
1e-9,
);
det_impl(
random_regular_using::<f32, _>(rows, &mut rng)
.t()
.to_owned(),
1e-4,
);
det_impl(
random_regular_using::<c64, _>(rows, &mut rng)
.t()
.to_owned(),
1e-9,
);
det_impl(
random_regular_using::<c32, _>(rows, &mut rng)
.t()
.to_owned(),
1e-4,
);
}
}
#[test]
fn det_nonsquare() {
macro_rules! det_nonsquare {
($elem:ty, $shape:expr) => {
let mut rng = rand_pcg::Mcg128Xsl64::new(0xcafef00dd15ea5e5);
let a: Array2<$elem> = random_using($shape, &mut rng);
assert!(a.factorize().unwrap().det().is_err());
assert!(a.factorize().unwrap().sln_det().is_err());
assert!(a.factorize().unwrap().det_into().is_err());
assert!(a.factorize().unwrap().sln_det_into().is_err());
assert!(a.det().is_err());
assert!(a.sln_det().is_err());
assert!(a.clone().det_into().is_err());
assert!(a.sln_det_into().is_err());
};
}
for &dims in &[(1, 0), (1, 2), (2, 1), (2, 3)] {
for &shape in &[dims.into_shape(), dims.f()] {
det_nonsquare!(f64, shape);
det_nonsquare!(f32, shape);
det_nonsquare!(c64, shape);
det_nonsquare!(c32, shape);
}
}
}