-
Notifications
You must be signed in to change notification settings - Fork 65
/
qr.rs
207 lines (193 loc) · 6.91 KB
/
qr.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
197
198
199
200
201
202
203
204
205
206
207
//! QR decomposition
use crate::{error::*, layout::MatrixLayout, *};
use cauchy::*;
use num_traits::{ToPrimitive, Zero};
pub trait QR_: Sized {
/// Execute Householder reflection as the first step of QR-decomposition
///
/// For C-continuous array,
/// this will call LQ-decomposition of the transposed matrix $ A^T = LQ^T $
fn householder(l: MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>>;
/// Reconstruct Q-matrix from Householder-reflectors
fn q(l: MatrixLayout, a: &mut [Self], tau: &[Self]) -> Result<()>;
/// Execute QR-decomposition at once
fn qr(l: MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>>;
}
macro_rules! impl_qr {
($scalar:ty, $qrf:path, $lqf:path, $gqr:path, $glq:path) => {
impl QR_ for $scalar {
fn householder(l: MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>> {
let m = l.lda();
let n = l.len();
let k = m.min(n);
let mut tau = vec_uninit(k as usize);
// eval work size
let mut info = 0;
let mut work_size = [Self::zero()];
unsafe {
match l {
MatrixLayout::F { .. } => {
$qrf(
&m,
&n,
AsPtr::as_mut_ptr(a),
&m,
AsPtr::as_mut_ptr(&mut tau),
AsPtr::as_mut_ptr(&mut work_size),
&(-1),
&mut info,
);
}
MatrixLayout::C { .. } => {
$lqf(
&m,
&n,
AsPtr::as_mut_ptr(a),
&m,
AsPtr::as_mut_ptr(&mut tau),
AsPtr::as_mut_ptr(&mut work_size),
&(-1),
&mut info,
);
}
}
}
info.as_lapack_result()?;
// calc
let lwork = work_size[0].to_usize().unwrap();
let mut work: Vec<MaybeUninit<Self>> = vec_uninit(lwork);
unsafe {
match l {
MatrixLayout::F { .. } => {
$qrf(
&m,
&n,
AsPtr::as_mut_ptr(a),
&m,
AsPtr::as_mut_ptr(&mut tau),
AsPtr::as_mut_ptr(&mut work),
&(lwork as i32),
&mut info,
);
}
MatrixLayout::C { .. } => {
$lqf(
&m,
&n,
AsPtr::as_mut_ptr(a),
&m,
AsPtr::as_mut_ptr(&mut tau),
AsPtr::as_mut_ptr(&mut work),
&(lwork as i32),
&mut info,
);
}
}
}
info.as_lapack_result()?;
let tau = unsafe { tau.assume_init() };
Ok(tau)
}
fn q(l: MatrixLayout, a: &mut [Self], tau: &[Self]) -> Result<()> {
let m = l.lda();
let n = l.len();
let k = m.min(n);
assert_eq!(tau.len(), k as usize);
// eval work size
let mut info = 0;
let mut work_size = [Self::zero()];
unsafe {
match l {
MatrixLayout::F { .. } => $gqr(
&m,
&k,
&k,
AsPtr::as_mut_ptr(a),
&m,
AsPtr::as_ptr(&tau),
AsPtr::as_mut_ptr(&mut work_size),
&(-1),
&mut info,
),
MatrixLayout::C { .. } => $glq(
&k,
&n,
&k,
AsPtr::as_mut_ptr(a),
&m,
AsPtr::as_ptr(&tau),
AsPtr::as_mut_ptr(&mut work_size),
&(-1),
&mut info,
),
}
};
// calc
let lwork = work_size[0].to_usize().unwrap();
let mut work: Vec<MaybeUninit<Self>> = vec_uninit(lwork);
unsafe {
match l {
MatrixLayout::F { .. } => $gqr(
&m,
&k,
&k,
AsPtr::as_mut_ptr(a),
&m,
AsPtr::as_ptr(&tau),
AsPtr::as_mut_ptr(&mut work),
&(lwork as i32),
&mut info,
),
MatrixLayout::C { .. } => $glq(
&k,
&n,
&k,
AsPtr::as_mut_ptr(a),
&m,
AsPtr::as_ptr(&tau),
AsPtr::as_mut_ptr(&mut work),
&(lwork as i32),
&mut info,
),
}
}
info.as_lapack_result()?;
Ok(())
}
fn qr(l: MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>> {
let tau = Self::householder(l, a)?;
let r = Vec::from(&*a);
Self::q(l, a, &tau)?;
Ok(r)
}
}
};
} // endmacro
impl_qr!(
f64,
lapack_sys::dgeqrf_,
lapack_sys::dgelqf_,
lapack_sys::dorgqr_,
lapack_sys::dorglq_
);
impl_qr!(
f32,
lapack_sys::sgeqrf_,
lapack_sys::sgelqf_,
lapack_sys::sorgqr_,
lapack_sys::sorglq_
);
impl_qr!(
c64,
lapack_sys::zgeqrf_,
lapack_sys::zgelqf_,
lapack_sys::zungqr_,
lapack_sys::zunglq_
);
impl_qr!(
c32,
lapack_sys::cgeqrf_,
lapack_sys::cgelqf_,
lapack_sys::cungqr_,
lapack_sys::cunglq_
);