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
use impl_prelude::*;
use permute::{MatrixPermutation, Permutes};
use ndarray as nd;
use lapack::c::{sgetrf, dgetrf, cgetrf, zgetrf, sgetri, dgetri, cgetri, zgetri};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LUError {
BadLayout,
NotSquare,
Singular,
InconsistentDimensions,
IllegalParameter(i32),
}
#[derive(Debug)]
pub struct LUFactors<T: LU> {
mat: Array<T, Ix2>,
perm: MatrixPermutation,
}
impl<T: LU> LUFactors<T> {
fn from_raw<Matrix>(mat: Matrix, perm: Vec<i32>) -> Result<LUFactors<T>, LUError>
where Matrix: Into<Array<T, Ix2>>
{
let mut ma = mat.into();
if slice_and_layout_mut(&mut ma).is_none() {
return Err(LUError::BadLayout);
}
Ok(LUFactors {
mat: ma,
perm: MatrixPermutation::from_ipiv(perm),
})
}
pub fn inverse(&self) -> Result<Array<T, Ix2>, LUError> {
LU::compute_inverse(&self.mat, self.perm.ipiv())
}
pub fn inverse_into(self) -> Result<Array<T, Ix2>, LUError> {
LU::compute_inverse_into(self.mat, self.perm.ipiv())
}
pub fn rows(&self) -> usize {
self.mat.rows()
}
pub fn cols(&self) -> usize {
self.mat.cols()
}
pub fn perm(&self) -> &MatrixPermutation {
&self.perm
}
fn k(&self) -> usize {
cmp::min(self.rows(), self.cols())
}
pub fn l(&self) -> Array<T, Ix2> {
let (m, _) = self.mat.dim();
let k = self.k();
let mut l = Array::zeros((m, k));
let top_left_slice = s![.., ..k as isize];
l.slice_mut(top_left_slice).assign(&self.mat.slice(top_left_slice));
let zero = T::zero();
let one = T::one();
for (r, mut row) in l.outer_iter_mut().enumerate().take(k) {
row[r] = one;
row.slice_mut(s![r as isize + 1..]).fill(zero);
}
l
}
#[inline]
pub fn u(&self) -> Array<T, Ix2> {
let (_, n) = self.mat.dim();
let k = self.k();
let mut u = Array::zeros((k, n));
let top_left_slice = s![..k as isize, ..];
u.slice_mut(top_left_slice).assign(&self.mat.slice(top_left_slice));
let zero = T::zero();
for (r, mut row) in u.outer_iter_mut().enumerate().take(k) {
row.slice_mut(s![..r as isize]).fill(zero);
}
u
}
pub fn reconstruct(&self) -> Array<T, Ix2> {
let lu = self.l().dot(&self.u());
self.perm.permute_into(lu).expect("guarantee that lu is the right size")
}
}
pub trait LU: nd::LinalgScalar + Permutes {
fn compute_into(a: Array<Self, Ix2>) -> Result<LUFactors<Self>, LUError>;
fn compute<D1: Data>(a: &ArrayBase<D1, Ix2>) -> Result<LUFactors<Self>, LUError>
where D1: Data<Elem = Self>
{
Self::compute_into(a.to_owned())
}
fn compute_inverse_into<D1>(mat: ArrayBase<D1, Ix2>, perm: &[i32])
-> Result<ArrayBase<D1, Ix2>, LUError>
where D1: DataOwned<Elem = Self> + DataMut<Elem=Self>;
fn compute_inverse<D1>(mat: &ArrayBase<D1, Ix2>, perm: &[i32])
-> Result<Array<Self, Ix2>, LUError>
where D1: Data<Elem = Self> {
if mat.rows() != mat.cols() {
return Err(LUError::NotSquare);
}
let copy_mat = mat.to_owned();
Self::compute_inverse_into(copy_mat, perm)
}
}
macro_rules! impl_lu {
($lu_type:ty, $lu_func:ident, $lu_invert:ident) => (
impl LU for $lu_type {
fn compute_into(mut a: Array<Self, Ix2>) -> Result<LUFactors<Self>, LUError> {
let dim = a.dim();
let (info, perm_i) = {
let (mut slice, layout, lda) = match slice_and_layout_mut(&mut a) {
None => return Err(LUError::BadLayout),
Some(x) => x,
};
let mut perm_i = Vec::new();
perm_i.resize(cmp::min(dim.0, dim.1), -1);
($lu_func(layout,
dim.0 as i32,
dim.1 as i32,
&mut slice,
lda as i32,
&mut perm_i),
perm_i)
};
if info == 0 {
LUFactors::from_raw(a, perm_i)
} else if info < 0 {
Err(LUError::IllegalParameter(-info))
} else {
Err(LUError::Singular)
}
}
fn compute_inverse_into<D1>(mut mat: ArrayBase<D1, Ix2>, perm: &[i32])
-> Result<ArrayBase<D1, Ix2>, LUError>
where D1: DataOwned<Elem = Self> + DataMut<Elem=Self> {
let dim = mat.dim();
if dim.0 != dim.1 {
return Err(LUError::NotSquare);
}
let info = {
let (mut slice, layout, lda) = match slice_and_layout_mut(&mut mat) {
None => return Err(LUError::BadLayout),
Some(x) => x,
};
$lu_invert(layout, dim.0 as i32, &mut slice, lda as i32, &perm)
};
if info == 0 {
Ok(mat)
} else if info < 0 {
Err(LUError::IllegalParameter(-info))
} else {
Err(LUError::Singular)
}
}
}
)
}
impl_lu!(f32, sgetrf, sgetri);
impl_lu!(f64, dgetrf, dgetri);
impl_lu!(c32, cgetrf, cgetri);
impl_lu!(c64, zgetrf, zgetri);