Skip to content

Commit 4b8c22e

Browse files
R-only QR and LQ decompositions (#316)
* R-only QR and LQ decompositions * Consistently use empty matrices instead of `nothing` for unneeded Q or R * `lq_via_qr!` fix * Test revision * Follow testsuite structure * Apply docstring and TODO suggestions * Undo some unnecessary refactoring * Rewrite docstring Co-authored-by: Lukas Devos <ldevos98@gmail.com> * Integrate new tests into existing files * Improve type stability of gauge fixing --------- Co-authored-by: Lukas Devos <ldevos98@gmail.com>
1 parent 508fa31 commit 4b8c22e

16 files changed

Lines changed: 158 additions & 71 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.*.swp
55
Manifest.toml
66
docs/build/
7+
.vscode

‎ext/MatrixAlgebraKitGenericLinearAlgebraExt.jl‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,16 @@ function qr_householder!(
6666

6767
m, n = size(A)
6868
minmn = min(m, n)
69-
computeR = length(R) > 0
7069

7170
# compute QR
7271
Q̃, R̃ = qr!(A)
73-
lmul!(Q̃, one!(Q))
72+
isempty(Q) || lmul!(Q̃, one!(Q))
7473

75-
if computeR
74+
if !isempty(R)
7675
copyto!(view(R, 1:minmn, :), R̃)
7776
zero!(view(R, (minmn + 1):size(R, 1), :))
78-
positive && gaugefix!(qr_householder!, Q, R, diagview(R̃))
79-
elseif positive
80-
gaugefix!(qr_householder!, Q, nothing, diagview(R̃))
8177
end
78+
positive && gaugefix!(qr_householder!, Q, R, diagview(R̃))
8279

8380
return Q, R
8481
end

‎src/common/gauge.jl‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ _largest(x::Complex, y::Complex) = abs2(x) < abs2(y) ? y : x
1515

1616
function gaugefix!(::typeof(qr_householder!), Q, R, Rd)
1717
ax = Base.OneTo(length(Rd))
18-
Qf = view(Q, axes(Q, 1), ax)
19-
Qf .*= sign_safe.(transpose(Rd))
20-
if !isnothing(R)
18+
if !isempty(Q)
19+
Qf = view(Q, axes(Q, 1), ax)
20+
Qf .*= sign_safe.(transpose(Rd))
21+
end
22+
if !isempty(R)
2123
Rf = view(R, ax, axes(R, 2))
2224
Rf .*= conj.(sign_safe.(Rd))
2325
end
@@ -26,9 +28,11 @@ end
2628

2729
function gaugefix!(::typeof(lq_householder!), L, Q, Ld)
2830
ax = Base.OneTo(length(Ld))
29-
Qf = view(Q, ax, axes(Q, 2))
30-
Qf .*= sign_safe.(Ld)
31-
if !isnothing(L)
31+
if !isempty(Q)
32+
Qf = view(Q, ax, axes(Q, 2))
33+
Qf .*= sign_safe.(Ld)
34+
end
35+
if !isempty(L)
3236
Lf = view(L, axes(L, 1), ax)
3337
Lf .*= conj.(sign_safe.(transpose(Ld)))
3438
end

‎src/implementations/lq.jl‎

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function check_input(::typeof(lq_full!), A::AbstractMatrix, LQ, ::AbstractAlgori
1212
@assert L isa AbstractMatrix && Q isa AbstractMatrix
1313
isempty(L) || @check_size(L, (m, n))
1414
@check_scalar(L, A)
15-
@check_size(Q, (n, n))
15+
isempty(Q) || @check_size(Q, (n, n))
1616
@check_scalar(Q, A)
1717
return nothing
1818
end
@@ -23,7 +23,7 @@ function check_input(::typeof(lq_compact!), A::AbstractMatrix, LQ, ::AbstractAlg
2323
@assert L isa AbstractMatrix && Q isa AbstractMatrix
2424
isempty(L) || @check_size(L, (m, minmn))
2525
@check_scalar(L, A)
26-
@check_size(Q, (minmn, n))
26+
isempty(Q) || @check_size(Q, (minmn, n))
2727
@check_scalar(Q, A)
2828
return nothing
2929
end
@@ -42,7 +42,7 @@ function check_input(::typeof(lq_full!), A::AbstractMatrix, (L, Q), ::DiagonalAl
4242
@assert Q isa Diagonal && L isa Diagonal
4343
isempty(L) || @check_size(L, (m, n))
4444
@check_scalar(L, A)
45-
@check_size(Q, (n, n))
45+
isempty(Q) || @check_size(Q, (n, n))
4646
@check_scalar(Q, A)
4747
return nothing
4848
end
@@ -148,7 +148,7 @@ function lq_householder!(
148148

149149
m, n = size(A)
150150
minmn = min(m, n)
151-
computeL = length(L) > 0
151+
computeL = !isempty(L)
152152
inplaceQ = Q === A
153153

154154
if inplaceQ
@@ -162,25 +162,25 @@ function lq_householder!(
162162
if blocksize > 1
163163
# L doubles as workspace for T, so Q is constructed before L is extracted
164164
mb = min(minmn, blocksize)
165-
if computeL # first use L as space for T
166-
A, T = gelqt!(driver, A, view(L, 1:mb, 1:minmn))
167-
else
168-
A, T = gelqt!(driver, A, similar(A, mb, minmn))
169-
end
170-
Q = gemlqt!(driver, 'R', 'N', A, T, one!(Q))
165+
T = computeL ? view(L, 1:mb, 1:minmn) : similar(A, mb, minmn)
166+
A, T = gelqt!(driver, A, T)
167+
isempty(Q) || (Q = gemlqt!(driver, 'R', 'N', A, T, one!(Q)))
171168
computeL && _lq_copyL!(L, A)
172-
positive && gaugefix!(lq_householder!, computeL ? L : nothing, Q, diagview(A))
169+
positive && gaugefix!(lq_householder!, L, Q, diagview(A))
173170
else
174171
A, τ = gelqf!(driver, A)
175172
computeL && _lq_copyL!(L, A)
176-
Lf = computeL ? L : nothing
177173
if inplaceQ
178-
Ld = positive ? copy(diagview(A)) : nothing # unglq! destroys the diagonal of A
179-
unglq!(driver, A, τ) # Q === A, so no need to rebind Q
180-
positive && gaugefix!(lq_householder!, Lf, Q, Ld)
174+
if positive
175+
Ld = copy(diagview(A)) # unglq! destroys the diagonal of A
176+
unglq!(driver, A, τ) # Q === A, so no need to rebind Q
177+
gaugefix!(lq_householder!, L, Q, Ld)
178+
else
179+
unglq!(driver, A, τ)
180+
end
181181
else
182-
Q = unmlq!(driver, 'R', 'N', A, τ, one!(Q))
183-
positive && gaugefix!(lq_householder!, Lf, Q, diagview(A))
182+
isempty(Q) || (Q = unmlq!(driver, 'R', 'N', A, τ, one!(Q)))
183+
positive && gaugefix!(lq_householder!, L, Q, diagview(A))
184184
end
185185
end
186186

@@ -220,6 +220,7 @@ function lq_householder!(
220220
L[i, j] = A[i, j]
221221
end
222222
end
223+
isempty(Q) && return L, Q
223224
# build Q
224225
one!(Q)
225226
@inbounds for i in minmn:-1:1
@@ -324,13 +325,13 @@ function lq_via_qr!(
324325
Qt = (A === Q) ? At : similar(Q')
325326
Lt = similar(L')
326327
n = size(A, 2)
327-
if size(Q) == (n, n)
328+
if size(Q) == (n, n) || size(L, 2) == n
328329
Qt, Lt = qr_full!(At, (Qt, Lt), qr_alg)
329330
else
330331
Qt, Lt = qr_compact!(At, (Qt, Lt), qr_alg)
331332
end
332-
adjoint!(Q, Qt)
333-
!isempty(L) && adjoint!(L, Lt)
333+
isempty(Q) || adjoint!(Q, Qt)
334+
isempty(L) || adjoint!(L, Lt)
334335
return L, Q
335336
end
336337

@@ -348,38 +349,28 @@ end
348349
function lq_full!(A::AbstractMatrix, LQ, alg::DiagonalAlgorithm)
349350
check_input(lq_full!, A, LQ, alg)
350351
L, Q = LQ
351-
lq_diagonal!(A, L, Q; alg.kwargs...)
352+
_lq_diagonal!(A, L, Q; alg.kwargs...)
352353
return L, Q
353354
end
354355
function lq_compact!(A::AbstractMatrix, LQ, alg::DiagonalAlgorithm)
355356
check_input(lq_compact!, A, LQ, alg)
356357
L, Q = LQ
357-
lq_diagonal!(A, L, Q; alg.kwargs...)
358+
_lq_diagonal!(A, L, Q; alg.kwargs...)
358359
return L, Q
359360
end
360361
function lq_null!(A::AbstractMatrix, N, alg::DiagonalAlgorithm)
361362
check_input(lq_null!, A, N, alg)
362-
return lq_null_diagonal!(A, N; alg.kwargs...)
363+
return _lq_null_diagonal!(A, N; alg.kwargs...)
363364
end
364365

365-
function lq_diagonal!(
366+
function _lq_diagonal!(
366367
A::AbstractMatrix, L::AbstractMatrix, Q::AbstractMatrix; positive::Bool = true
367368
)
368-
# note: Ad and Qd might share memory here so order of operations is important
369-
Ad = diagview(A)
370-
Ld = diagview(L)
371-
Qd = diagview(Q)
372-
if positive
373-
@. Ld = abs(Ad)
374-
@. Qd = sign_safe(Ad)
375-
else
376-
Ld .= Ad
377-
one!(Q)
378-
end
369+
Q, L = _qr_diagonal!(A, Q, L; positive)
379370
return L, Q
380371
end
381372

382-
lq_null_diagonal!(A::AbstractMatrix, N; positive::Bool = true) = N
373+
_lq_null_diagonal!(A::AbstractMatrix, N; positive::Bool = true) = N
383374

384375
# Deprecations
385376
# ------------

‎src/implementations/qr.jl‎

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function check_input(::typeof(qr_full!), A::AbstractMatrix, QR, ::AbstractAlgori
1010
m, n = size(A)
1111
Q, R = QR
1212
@assert Q isa AbstractMatrix && R isa AbstractMatrix
13-
@check_size(Q, (m, m))
13+
isempty(Q) || @check_size(Q, (m, m))
1414
@check_scalar(Q, A)
1515
isempty(R) || @check_size(R, (m, n))
1616
@check_scalar(R, A)
@@ -21,7 +21,7 @@ function check_input(::typeof(qr_compact!), A::AbstractMatrix, QR, ::AbstractAlg
2121
minmn = min(m, n)
2222
Q, R = QR
2323
@assert Q isa AbstractMatrix && R isa AbstractMatrix
24-
@check_size(Q, (m, minmn))
24+
isempty(Q) || @check_size(Q, (m, minmn))
2525
@check_scalar(Q, A)
2626
isempty(R) || @check_size(R, (minmn, n))
2727
@check_scalar(R, A)
@@ -40,7 +40,7 @@ function check_input(::typeof(qr_full!), A::AbstractMatrix, (Q, R), alg::Diagona
4040
m, n = size(A)
4141
@assert m == n && isdiag(A)
4242
@assert Q isa Diagonal && R isa Diagonal
43-
@check_size(Q, (m, n))
43+
isempty(Q) || @check_size(Q, (m, n))
4444
@check_scalar(Q, A)
4545
isempty(R) || @check_size(R, (m, n))
4646
@check_scalar(R, A)
@@ -170,7 +170,7 @@ function qr_householder!(
170170

171171
m, n = size(A)
172172
minmn = min(m, n)
173-
computeR = length(R) > 0
173+
computeR = !isempty(R)
174174
inplaceQ = Q === A
175175

176176
if inplaceQ
@@ -185,14 +185,11 @@ function qr_householder!(
185185
if blocksize > 1
186186
# R doubles as workspace for T, so Q is constructed before R is extracted
187187
nb = min(minmn, blocksize)
188-
if computeR # first use R as space for T
189-
A, T = geqrt!(driver, A, view(R, 1:nb, 1:minmn))
190-
else
191-
A, T = geqrt!(driver, A, similar(A, nb, minmn))
192-
end
193-
Q = gemqrt!(driver, 'L', 'N', A, T, one!(Q))
188+
T = computeR ? view(R, 1:nb, 1:minmn) : similar(A, nb, minmn)
189+
A, T = geqrt!(driver, A, T)
190+
isempty(Q) || (Q = gemqrt!(driver, 'L', 'N', A, T, one!(Q)))
194191
computeR && _qr_copyR!(R, A)
195-
positive && gaugefix!(qr_householder!, Q, computeR ? R : nothing, diagview(A))
192+
positive && gaugefix!(qr_householder!, Q, R, diagview(A))
196193
else
197194
if pivoted
198195
A, τ, jpvt = geqp3!(driver, A)
@@ -201,14 +198,18 @@ function qr_householder!(
201198
A, τ = geqrf!(driver, A)
202199
computeR && _qr_copyR!(R, A)
203200
end
204-
Rf = computeR ? R : nothing # gaugefix! rescales rows, which commutes with the pivoting
201+
# Gauge fixing rescales rows, which commutes with pivoting.
205202
if inplaceQ
206-
Rd = positive ? copy(diagview(A)) : nothing # ungqr! destroys the diagonal of A
207-
ungqr!(driver, A, τ) # Q === A, so no need to rebind Q
208-
positive && gaugefix!(qr_householder!, Q, Rf, Rd)
203+
if positive
204+
Rd = copy(diagview(A)) # ungqr! destroys the diagonal of A
205+
ungqr!(driver, A, τ) # Q === A, so no need to rebind Q
206+
gaugefix!(qr_householder!, Q, R, Rd)
207+
else
208+
ungqr!(driver, A, τ)
209+
end
209210
else
210-
Q = _qr_buildQ!(driver, Q, A, τ, minmn)
211-
positive && gaugefix!(qr_householder!, Q, Rf, diagview(A))
211+
isempty(Q) || (Q = _qr_buildQ!(driver, Q, A, τ, minmn))
212+
positive && gaugefix!(qr_householder!, Q, R, diagview(A))
212213
end
213214
end
214215

@@ -248,6 +249,7 @@ function qr_householder!(
248249
R[i, j] = A[i, j]
249250
end
250251
end
252+
isempty(Q) && return Q, R
251253
# build Q
252254
one!(Q)
253255
@inbounds for j in minmn:-1:1
@@ -330,22 +332,22 @@ end
330332
function qr_full!(A::AbstractMatrix, QR, alg::DiagonalAlgorithm)
331333
check_input(qr_full!, A, QR, alg)
332334
Q, R = QR
333-
_diagonal_qr!(A, Q, R; alg.kwargs...)
335+
_qr_diagonal!(A, Q, R; alg.kwargs...)
334336
return Q, R
335337
end
336338
function qr_compact!(A::AbstractMatrix, QR, alg::DiagonalAlgorithm)
337339
check_input(qr_compact!, A, QR, alg)
338340
Q, R = QR
339-
_diagonal_qr!(A, Q, R; alg.kwargs...)
341+
_qr_diagonal!(A, Q, R; alg.kwargs...)
340342
return Q, R
341343
end
342344
function qr_null!(A::AbstractMatrix, N, alg::DiagonalAlgorithm)
343345
check_input(qr_null!, A, N, alg)
344-
_diagonal_qr_null!(A, N; alg.kwargs...)
346+
_qr_null_diagonal!(A, N; alg.kwargs...)
345347
return N
346348
end
347349

348-
function _diagonal_qr!(
350+
function _qr_diagonal!(
349351
A::AbstractMatrix, Q::AbstractMatrix, R::AbstractMatrix; positive::Bool = true
350352
)
351353
# note: Ad and Qd might share memory here so order of operations is important
@@ -354,15 +356,15 @@ function _diagonal_qr!(
354356
Rd = diagview(R)
355357
if positive
356358
@. Rd = abs(Ad)
357-
@. Qd = sign_safe(Ad)
359+
isempty(Q) || (@. Qd = sign_safe(Ad))
358360
else
359361
Rd .= Ad
360362
one!(Q)
361363
end
362364
return Q, R
363365
end
364366

365-
_diagonal_qr_null!(A::AbstractMatrix, N; positive::Bool = true) = N
367+
_qr_null_diagonal!(A::AbstractMatrix, N; positive::Bool = true) = N
366368

367369
# Deprecations
368370
# ------------

‎src/interface/lq.jl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Compute the full LQ decomposition of the rectangular matrix `A`, such that `A =
1010
where `Q` is a square unitary matrix with the same number of rows as `A` and `L` is a
1111
lower triangular matrix with the same size as `A`.
1212
13+
Pass an empty `Q` output to `lq_full!` to compute only `L` without constructing `Q`.
14+
Conversely, an empty `L` output requests only `Q`.
15+
1316
!!! note
1417
The bang method `lq_full!` optionally accepts the output structure and
1518
possibly destroys the input matrix `A`. Always use the return value of the function
@@ -29,6 +32,9 @@ Compute the compact LQ decomposition of the rectangular matrix `A` of size `(m,n
2932
such that `A = L * Q` where the matrix `Q` of size `(min(m,n), n)` has orthogonal rows
3033
spanning the image of `A'`, and the matrix `L` of size `(m, min(m,n))` is lower triangular.
3134
35+
Pass an empty `Q` output to `lq_compact!` to compute only `L` without constructing `Q`.
36+
Conversely, an empty `L` output requests only `Q`.
37+
3238
!!! note
3339
The bang method `lq_compact!` optionally accepts the output structure and
3440
possibly destroys the input matrix `A`. Always use the return value of the function

‎src/interface/orthnull.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ deduce the decomposition kind with [`left_orth_alg(alg)`](@ref left_orth_alg).
7676
destroys the input matrix `A`. Always use the return value of the function as it may
7777
not always be possible to use the provided `CV` as output.
7878
79+
For QR-based decompositions, pass an empty `V` output to `left_orth!` to compute only `C` without constructing `V`.
80+
Conversely, an empty `C` output requests only `V`.
81+
The returned basis matrix remains empty, and its construction is skipped.
82+
7983
See also [`right_orth(!)`](@ref right_orth), [`left_null(!)`](@ref left_null) and
8084
[`right_null(!)`](@ref right_null).
8185
"""
@@ -157,6 +161,9 @@ deduce the decomposition kind with [`right_orth_alg`](@ref).
157161
destroys the input matrix `A`. Always use the return value of the function as it may not
158162
always be possible to use the provided `CVᴴ` as output.
159163
164+
For LQ-based decompositions, pass an empty `Vᴴ` output to `right_orth!` to compute only `C` without constructing `Vᴴ`.
165+
Conversely, an empty `C` output requests only `Vᴴ`.
166+
160167
See also [`left_orth(!)`](@ref left_orth), [`left_null(!)`](@ref left_null) and
161168
[`right_null(!)`](@ref right_null).
162169
"""

‎src/interface/qr.jl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Compute the full QR decomposition of the rectangular matrix `A`, such that `A =
1010
where `Q` is a square unitary matrix with the same number of rows as `A` and `R` is an
1111
upper triangular matrix with the same size as `A`.
1212
13+
Pass an empty `Q` output to `qr_full!` to compute only `R` without constructing `Q`.
14+
Conversely, an empty `R` output requests only `Q`.
15+
1316
!!! note
1417
The bang method `qr_full!` optionally accepts the output structure and
1518
possibly destroys the input matrix `A`. Always use the return value of the function
@@ -30,6 +33,9 @@ such that `A = Q * R` where the isometric matrix `Q` of size `(m, min(m,n))` has
3033
orthogonal columns spanning the image of `A`, and the matrix `R` of size `(min(m,n), n)`
3134
is upper triangular.
3235
36+
Pass an empty `Q` output to `qr_compact!` to compute only `R` without constructing `Q`.
37+
Conversely, an empty `R` output requests only `Q`.
38+
3339
!!! note
3440
The bang method `qr_compact!` optionally accepts the output structure and
3541
possibly destroys the input matrix `A`. Always use the return value of the function

0 commit comments

Comments
 (0)