-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlq.jl
More file actions
386 lines (357 loc) · 12 KB
/
lq.jl
File metadata and controls
386 lines (357 loc) · 12 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# Inputs
# ------
copy_input(::typeof(lq_full), A::AbstractMatrix) = copy!(similar(A, float(eltype(A))), A)
copy_input(::typeof(lq_compact), A) = copy_input(lq_full, A)
copy_input(::typeof(lq_null), A) = copy_input(lq_full, A)
copy_input(::typeof(lq_full), A::Diagonal) = copy(A)
function check_input(::typeof(lq_full!), A::AbstractMatrix, LQ, ::AbstractAlgorithm)
m, n = size(A)
L, Q = LQ
@assert L isa AbstractMatrix && Q isa AbstractMatrix
isempty(L) || @check_size(L, (m, n))
@check_scalar(L, A)
@check_size(Q, (n, n))
@check_scalar(Q, A)
return nothing
end
function check_input(::typeof(lq_compact!), A::AbstractMatrix, LQ, ::AbstractAlgorithm)
m, n = size(A)
minmn = min(m, n)
L, Q = LQ
@assert L isa AbstractMatrix && Q isa AbstractMatrix
isempty(L) || @check_size(L, (m, minmn))
@check_scalar(L, A)
@check_size(Q, (minmn, n))
@check_scalar(Q, A)
return nothing
end
function check_input(::typeof(lq_null!), A::AbstractMatrix, Nᴴ, ::AbstractAlgorithm)
m, n = size(A)
minmn = min(m, n)
@assert Nᴴ isa AbstractMatrix
@check_size(Nᴴ, (n - minmn, n))
@check_scalar(Nᴴ, A)
return nothing
end
function check_input(::typeof(lq_full!), A::AbstractMatrix, (L, Q), ::DiagonalAlgorithm)
m, n = size(A)
@assert m == n && isdiag(A)
@assert Q isa Diagonal && L isa Diagonal
isempty(L) || @check_size(L, (m, n))
@check_scalar(L, A)
@check_size(Q, (n, n))
@check_scalar(Q, A)
return nothing
end
function check_input(::typeof(lq_compact!), A::AbstractMatrix, LQ, alg::DiagonalAlgorithm)
return check_input(lq_full!, A, LQ, alg)
end
function check_input(::typeof(lq_null!), A::AbstractMatrix, N, ::DiagonalAlgorithm)
m, n = size(A)
@assert m == n && isdiag(A)
@assert N isa AbstractMatrix
@check_size(N, (0, m))
@check_scalar(N, A)
return nothing
end
# Outputs
# -------
function initialize_output(::typeof(lq_full!), A::AbstractMatrix, ::AbstractAlgorithm)
m, n = size(A)
L = similar(A, (m, n))
Q = similar(A, (n, n))
return (L, Q)
end
function initialize_output(::typeof(lq_compact!), A::AbstractMatrix, ::AbstractAlgorithm)
m, n = size(A)
minmn = min(m, n)
L = similar(A, (m, minmn))
Q = similar(A, (minmn, n))
return (L, Q)
end
function initialize_output(::typeof(lq_null!), A::AbstractMatrix, ::AbstractAlgorithm)
m, n = size(A)
minmn = min(m, n)
Nᴴ = similar(A, (n - minmn, n))
return Nᴴ
end
for f! in (:lq_full!, :lq_compact!)
@eval function initialize_output(::typeof($f!), A::AbstractMatrix, ::DiagonalAlgorithm)
return similar(A), A
end
end
# ==========================
# IMPLEMENTATIONS
# ==========================
# Householder
# -----------
function lq_full!(A, LQ, alg::Householder)
check_input(lq_full!, A, LQ, alg)
return householder_lq!(A, LQ...; alg.kwargs...)
end
function lq_compact!(A, LQ, alg::Householder)
check_input(lq_compact!, A, LQ, alg)
return householder_lq!(A, LQ...; alg.kwargs...)
end
function lq_null!(A, Nᴴ, alg::Householder)
check_input(lq_null!, A, Nᴴ, alg)
return householder_lq_null!(A, Nᴴ; alg.kwargs...)
end
# dispatch helpers
for f in (:gelqt!, :gemlqt!, :gelqf!, :unglq!, :unmlq!)
@eval begin
$f(::LAPACK, args...) = YALAPACK.$f(args...)
end
end
@inline householder_lq!(A, L, Q; driver::Driver = DefaultDriver(), kwargs...) =
householder_lq!(driver, A, L, Q; kwargs...)
householder_lq!(::DefaultDriver, A, L, Q; kwargs...) =
householder_lq!(default_householder_driver(A), A, L, Q; kwargs...)
householder_lq!(driver::Union{CUSOLVER, ROCSOLVER, GLA}, A, L, Q; kwargs...) =
lq_via_qr!(A, L, Q, Householder(; driver, kwargs...))
function householder_lq!(
driver::LAPACK, A::AbstractMatrix, L::AbstractMatrix, Q::AbstractMatrix;
positive = true, pivoted = false, blocksize::Int = 0
)
blocksize = blocksize > 0 ? blocksize : ((pivoted || A === Q) ? 1 : YALAPACK.default_qr_blocksize(A))
# error messages for disallowing driver - setting combinations
pivoted && (blocksize > 1) &&
throw(ArgumentError(lazy"$driver does not provide a blocked pivoted LQ decomposition"))
m, n = size(A)
minmn = min(m, n)
computeL = length(L) > 0
inplaceQ = Q === A
(inplaceQ && (computeL || positive || blocksize > 1 || n < m)) &&
throw(ArgumentError("inplace Q only supported if matrix is wide (`m <= n`), L is not required, and using the unblocked algorithm (`blocksize = 1`) with `positive = false`"))
if blocksize > 1
mb = min(minmn, blocksize)
if computeL # first use L as space for T
A, T = gelqt!(driver, A, view(L, 1:mb, 1:minmn))
else
A, T = gelqt!(driver, A, similar(A, mb, minmn))
end
Q = gemlqt!(driver, 'R', 'N', A, T, one!(Q))
else
A, τ = gelqf!(driver, A)
if inplaceQ
Q = unglq!(driver, A, τ)
else
Q = unmlq!(driver, 'R', 'N', A, τ, one!(Q))
end
end
if positive # already fix Q even if we do not need L
@inbounds for j in 1:n
@simd for i in 1:minmn
s = sign_safe(A[i, i])
Q[i, j] *= s
end
end
end
if computeL
L̃ = lowertriangular!(view(A, axes(L)...))
if positive
@inbounds for j in 1:minmn
s = conj(sign_safe(L̃[j, j]))
@simd for i in j:m
L̃[i, j] = L̃[i, j] * s
end
end
end
copyto!(L, L̃)
end
return L, Q
end
function householder_lq!(
driver::Native, A::AbstractMatrix, L::AbstractMatrix, Q::AbstractMatrix;
positive::Bool = true, pivoted::Bool = false, blocksize::Int = 0
)
# error messages for disallowing driver - setting combinations
blocksize <= 1 ||
throw(ArgumentError(lazy"$driver does not provide a blocked LQ decomposition"))
pivoted &&
throw(ArgumentError(lazy"$driver does not provide a pivoted LQ decomposition"))
# positive = true regardless of setting
m, n = size(A)
minmn = min(m, n)
@inbounds for i in 1:minmn
for j in 1:(i - 1)
L[i, j] = A[i, j]
end
β, v, L[i, i] = _householder!(conj!(view(A, i, i:n)), 1)
for j in (i + 1):size(L, 2)
L[i, j] = 0
end
H = HouseholderReflection(conj(β), v, i:n)
rmul!(A, H; rows = (i + 1):m)
# A[i, i] == 1; store β instead
A[i, i] = β
end
# copy remaining rows for m > n
@inbounds for j in 1:size(L, 2)
for i in (minmn + 1):m
L[i, j] = A[i, j]
end
end
# build Q
one!(Q)
@inbounds for i in minmn:-1:1
β = A[i, i]
A[i, i] = 1
Hᴴ = HouseholderReflection(β, view(A, i, i:n), i:n)
rmul!(Q, Hᴴ)
end
return L, Q
end
@inline householder_lq_null!(A, Nᴴ; driver::Driver = DefaultDriver(), kwargs...) =
householder_lq_null!(driver, A, Nᴴ; kwargs...)
householder_lq_null!(::DefaultDriver, A, Nᴴ; kwargs...) =
householder_lq_null!(default_householder_driver(A), A, Nᴴ; kwargs...)
householder_lq_null!(driver::Union{CUSOLVER, ROCSOLVER, GLA}, A, Nᴴ; kwargs...) =
lq_null_via_qr!(A, Nᴴ, Householder(; driver, kwargs...))
function householder_lq_null!(
driver::LAPACK, A::AbstractMatrix, Nᴴ::AbstractMatrix;
positive::Bool = true, pivoted::Bool = false, blocksize::Int = 0
)
blocksize = blocksize > 0 ? blocksize : (pivoted ? 1 : YALAPACK.default_qr_blocksize(A))
# error messages for disallowing driver - setting combinations
pivoted && (blocksize > 1) &&
throw(ArgumentError(lazy"$driver does not provide a blocked pivoted LQ decomposition"))
m, n = size(A)
minmn = min(m, n)
zero!(Nᴴ)
one!(view(Nᴴ, 1:(n - minmn), (minmn + 1):n))
if blocksize > 1
mb = min(minmn, blocksize)
A, T = gelqt!(driver, A, similar(A, mb, minmn))
Nᴴ = gemlqt!(driver, 'R', 'N', A, T, Nᴴ)
else
A, τ = gelqf!(driver, A)
Nᴴ = unmlq!(driver, 'R', 'N', A, τ, Nᴴ)
end
return Nᴴ
end
function householder_lq_null!(
driver::Native, A::AbstractMatrix, Nᴴ::AbstractMatrix;
positive::Bool = true, pivoted::Bool = false, blocksize::Int = 0
)
# error messages for disallowing driver - setting combinations
blocksize <= 1 ||
throw(ArgumentError(lazy"$driver does not provide a blocked LQ decomposition"))
pivoted &&
throw(ArgumentError(lazy"$driver does not provide a pivoted LQ decomposition"))
m, n = size(A)
minmn = min(m, n)
@inbounds for i in 1:minmn
β, v, ν = _householder!(conj!(view(A, i, i:n)), 1)
H = HouseholderReflection(conj(β), v, i:n)
rmul!(A, H; rows = (i + 1):m)
# A[i, i] == 1; store β instead
A[i, i] = β
end
# build Nᴴ
zero!(Nᴴ)
one!(view(Nᴴ, 1:(n - minmn), (minmn + 1):n))
@inbounds for i in minmn:-1:1
β = A[i, i]
A[i, i] = 1
Hᴴ = HouseholderReflection(β, view(A, i, i:n), i:n)
rmul!(Nᴴ, Hᴴ)
end
return Nᴴ
end
# LQ via transposition and QR
# ---------------------------
function lq_full!(A::AbstractMatrix, LQ, alg::LQViaTransposedQR)
check_input(lq_full!, A, LQ, alg)
L, Q = LQ
lq_via_qr!(A, L, Q, alg.qr_alg)
return L, Q
end
function lq_compact!(A::AbstractMatrix, LQ, alg::LQViaTransposedQR)
check_input(lq_compact!, A, LQ, alg)
L, Q = LQ
lq_via_qr!(A, L, Q, alg.qr_alg)
return L, Q
end
function lq_null!(A::AbstractMatrix, Nᴴ, alg::LQViaTransposedQR)
check_input(lq_null!, A, Nᴴ, alg)
lq_null_via_qr!(A, Nᴴ, alg.qr_alg)
return Nᴴ
end
function lq_via_qr!(
A::AbstractMatrix, L::AbstractMatrix, Q::AbstractMatrix, qr_alg::AbstractAlgorithm
)
At = adjoint!(similar(A'), A)::AbstractMatrix
Qt = (A === Q) ? At : similar(Q')
Lt = similar(L')
n = size(A, 2)
if size(Q) == (n, n)
Qt, Lt = qr_full!(At, (Qt, Lt), qr_alg)
else
Qt, Lt = qr_compact!(At, (Qt, Lt), qr_alg)
end
adjoint!(Q, Qt)
!isempty(L) && adjoint!(L, Lt)
return L, Q
end
function lq_null_via_qr!(A::AbstractMatrix, N::AbstractMatrix, qr_alg::AbstractAlgorithm)
At = adjoint!(similar(A'), A)::AbstractMatrix
Nt = similar(N')
Nt = qr_null!(At, Nt, qr_alg)
!isempty(N) && adjoint!(N, Nt)
return N
end
# Diagonal
# --------
function lq_full!(A::AbstractMatrix, LQ, alg::DiagonalAlgorithm)
check_input(lq_full!, A, LQ, alg)
L, Q = LQ
_diagonal_lq!(A, L, Q; alg.kwargs...)
return L, Q
end
function lq_compact!(A::AbstractMatrix, LQ, alg::DiagonalAlgorithm)
check_input(lq_compact!, A, LQ, alg)
L, Q = LQ
_diagonal_lq!(A, L, Q; alg.kwargs...)
return L, Q
end
function lq_null!(A::AbstractMatrix, N, alg::DiagonalAlgorithm)
check_input(lq_null!, A, N, alg)
return _diagonal_lq_null!(A, N; alg.kwargs...)
end
function _diagonal_lq!(
A::AbstractMatrix, L::AbstractMatrix, Q::AbstractMatrix; positive::Bool = true
)
# note: Ad and Qd might share memory here so order of operations is important
Ad = diagview(A)
Ld = diagview(L)
Qd = diagview(Q)
if positive
@. Ld = abs(Ad)
@. Qd = sign_safe(Ad)
else
Ld .= Ad
one!(Q)
end
return L, Q
end
_diagonal_lq_null!(A::AbstractMatrix, N; positive::Bool = true) = N
# Deprecations
# ------------
for drivertype in (:LAPACK, :Native)
algtype = Symbol(drivertype, :_HouseholderLQ)
@eval begin
Base.@deprecate(
lq_full!(A, LQ, alg::$algtype),
lq_full!(A, LQ, Householder(; driver = $drivertype(), alg.kwargs...))
)
Base.@deprecate(
lq_compact!(A, LQ, alg::$algtype),
lq_compact!(A, LQ, Householder(; driver = $drivertype(), alg.kwargs...))
)
Base.@deprecate(
lq_null!(A, Nᴴ, alg::$algtype),
lq_null!(A, Nᴴ, Householder(; driver = $drivertype(), alg.kwargs...))
)
end
end