-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathqr.jl
More file actions
168 lines (159 loc) · 4.93 KB
/
qr.jl
File metadata and controls
168 lines (159 loc) · 4.93 KB
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
# Inputs
# ------
function copy_input(::typeof(qr_full), A::AbstractMatrix)
return copy!(similar(A, float(eltype(A))), A)
end
function copy_input(::typeof(qr_compact), A::AbstractMatrix)
return copy!(similar(A, float(eltype(A))), A)
end
function copy_input(::typeof(qr_null), A::AbstractMatrix)
return copy!(similar(A, float(eltype(A))), A)
end
function check_input(::typeof(qr_full!), A::AbstractMatrix, QR)
m, n = size(A)
Q, R = QR
@assert Q isa AbstractMatrix && R isa AbstractMatrix
@check_size(Q, (m, m))
@check_scalar(Q, A)
isempty(R) || @check_size(R, (m, n))
@check_scalar(R, A)
return nothing
end
function check_input(::typeof(qr_compact!), A::AbstractMatrix, QR)
m, n = size(A)
minmn = min(m, n)
Q, R = QR
@assert Q isa AbstractMatrix && R isa AbstractMatrix
@check_size(Q, (m, minmn))
@check_scalar(Q, A)
isempty(R) || @check_size(R, (minmn, n))
@check_scalar(R, A)
return nothing
end
function check_input(::typeof(qr_null!), A::AbstractMatrix, N)
m, n = size(A)
minmn = min(m, n)
@assert N isa AbstractMatrix
@check_size(N, (m, m - minmn))
@check_scalar(N, A)
return nothing
end
# Outputs
# -------
function initialize_output(::typeof(qr_full!), A::AbstractMatrix, ::LAPACK_HouseholderQR)
m, n = size(A)
Q = similar(A, (m, m))
R = similar(A, (m, n))
return (Q, R)
end
function initialize_output(::typeof(qr_compact!), A::AbstractMatrix, ::LAPACK_HouseholderQR)
m, n = size(A)
minmn = min(m, n)
Q = similar(A, (m, minmn))
R = similar(A, (minmn, n))
return (Q, R)
end
function initialize_output(::typeof(qr_null!), A::AbstractMatrix, ::LAPACK_HouseholderQR)
m, n = size(A)
minmn = min(m, n)
N = similar(A, (m, m - minmn))
return N
end
# Implementation
# --------------
# actual implementation
function qr_full!(A::AbstractMatrix, QR, alg::LAPACK_HouseholderQR)
check_input(qr_full!, A, QR)
Q, R = QR
_lapack_qr!(A, Q, R; alg.kwargs...)
return Q, R
end
function qr_compact!(A::AbstractMatrix, QR, alg::LAPACK_HouseholderQR)
check_input(qr_compact!, A, QR)
Q, R = QR
_lapack_qr!(A, Q, R; alg.kwargs...)
return Q, R
end
function qr_null!(A::AbstractMatrix, N, alg::LAPACK_HouseholderQR)
check_input(qr_null!, A, N)
_lapack_qr_null!(A, N; alg.kwargs...)
return N
end
function _lapack_qr!(A::AbstractMatrix, Q::AbstractMatrix, R::AbstractMatrix;
positive=false,
pivoted=false,
blocksize=((pivoted || A === Q) ? 1 : YALAPACK.default_qr_blocksize(A)))
m, n = size(A)
minmn = min(m, n)
computeR = length(R) > 0
inplaceQ = Q === A
if pivoted && (blocksize > 1)
throw(ArgumentError("LAPACK does not provide a blocked implementation for a pivoted QR decomposition"))
end
if inplaceQ && (computeR || positive || blocksize > 1 || m < n)
throw(ArgumentError("inplace Q only supported if matrix is tall (`m >= n`), R is not required, and using the unblocked algorithm (`blocksize=1`) with `positive=false`"))
end
if blocksize > 1
nb = min(minmn, blocksize)
if computeR # first use R as space for T
A, T = YALAPACK.geqrt!(A, view(R, 1:nb, 1:minmn))
else
A, T = YALAPACK.geqrt!(A, similar(A, nb, minmn))
end
Q = YALAPACK.gemqrt!('L', 'N', A, T, one!(Q))
else
if pivoted
A, τ, jpvt = YALAPACK.geqp3!(A)
else
A, τ = YALAPACK.geqrf!(A)
end
if inplaceQ
Q = YALAPACK.ungqr!(A, τ)
else
Q = YALAPACK.unmqr!('L', 'N', A, τ, one!(Q))
end
end
if positive # already fix Q even if we do not need R
@inbounds for j in 1:minmn
s = sign_safe(A[j, j])
@simd for i in 1:m
Q[i, j] *= s
end
end
end
if computeR
R̃ = triu!(view(A, axes(R)...))
if positive
@inbounds for j in n:-1:1
@simd for i in 1:min(minmn, j)
R̃[i, j] = R̃[i, j] * conj(sign_safe(R̃[i, i]))
end
end
end
if !pivoted
copyto!(R, R̃)
else
# probably very inefficient in terms of memory access
copyto!(view(R, :, jpvt), R̃)
end
end
return Q, R
end
function _lapack_qr_null!(A::AbstractMatrix, N::AbstractMatrix;
positive=false,
pivoted=false,
blocksize=YALAPACK.default_qr_blocksize(A))
m, n = size(A)
minmn = min(m, n)
fill!(N, zero(eltype(N)))
one!(view(N, (minmn + 1):m, 1:(m - minmn)))
if blocksize > 1
nb = min(minmn, blocksize)
A, T = YALAPACK.geqrt!(A, similar(A, nb, minmn))
N = YALAPACK.gemqrt!('L', 'N', A, T, N)
else
A, τ = YALAPACK.geqrf!(A)
N = YALAPACK.unmqr!('L', 'N', A, τ, N)
end
return N
end