Skip to content

Commit dbc5098

Browse files
Juthokshyatt
authored andcommitted
add lq and tests
1 parent 55a70b9 commit dbc5098

File tree

7 files changed

+108
-14
lines changed

7 files changed

+108
-14
lines changed

src/MatrixAlgebraKit.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export left_polar!, right_polar!
3131
export left_orth, right_orth, left_null, right_null
3232
export left_orth!, right_orth!, left_null!, right_null!
3333

34+
export Native_HouseholderQR, Native_HouseholderLQ
3435
export LAPACK_HouseholderQR, LAPACK_HouseholderLQ, LAPACK_Simple, LAPACK_Expert,
3536
LAPACK_QRIteration, LAPACK_Bisection, LAPACK_MultipleRelativelyRobustRepresentations,
3637
LAPACK_DivideAndConquer, LAPACK_Jacobi

src/common/householder.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ end
99
Base.adjoint(H::Householder) = Householder(conj(H.β), H.v, H.r)
1010

1111
function householder(x::AbstractVector, r::IndexRange = axes(x, 1), k = first(r))
12-
i = findfirst(equalto(k), r)
12+
i = findfirst(==(k), r)
1313
i == nothing && error("k = $k should be in the range r = $r")
1414
β, v, ν = _householder!(x[r], i)
1515
return Householder(β, v, r), ν
1616
end
1717
# Householder reflector h that zeros the elements A[r,col] (except for A[k,col]) upon lmul!(h,A)
1818
function householder(A::AbstractMatrix, r::IndexRange, col::Int, k = first(r))
19-
i = findfirst(equalto(k), r)
19+
i = findfirst(==(k), r)
2020
i == nothing && error("k = $k should be in the range r = $r")
2121
β, v, ν = _householder!(A[r, col], i)
2222
return Householder(β, v, r), ν
2323
end
2424
# Householder reflector that zeros the elements A[row,r] (except for A[row,k]) upon rmul!(A,h')
2525
function householder(A::AbstractMatrix, row::Int, r::IndexRange, k = first(r))
26-
i = findfirst(equalto(k), r)
26+
i = findfirst(==(k), r)
2727
i == nothing && error("k = $k should be in the range r = $r")
2828
β, v, ν = _householder!(conj!(A[row, r]), i)
2929
return Householder(β, v, r), ν

src/implementations/lq.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,85 @@ function _diagonal_lq!(
270270
end
271271
272272
_diagonal_lq_null!(A::AbstractMatrix, N; positive::Bool = false) = N
273+
274+
# Native logic
275+
# -------------
276+
function lq_full!(A::AbstractMatrix, LQ, alg::Native_HouseholderLQ)
277+
check_input(lq_full!, A, LQ, alg)
278+
L, Q = LQ
279+
A === Q &&
280+
throw(ArgumentError("inplace Q not supported with native LQ implementation"))
281+
_native_lq!(A, L, Q; alg.kwargs...)
282+
return L, Q
283+
end
284+
function lq_compact!(A::AbstractMatrix, LQ, alg::Native_HouseholderLQ)
285+
check_input(lq_compact!, A, LQ, alg)
286+
L, Q = LQ
287+
A === Q &&
288+
throw(ArgumentError("inplace Q not supported with native LQ implementation"))
289+
_native_lq!(A, L, Q; alg.kwargs...)
290+
return L, Q
291+
end
292+
function lq_null!(A::AbstractMatrix, N, alg::Native_HouseholderLQ)
293+
check_input(lq_null!, A, N, alg)
294+
_native_lq_null!(A, N; alg.kwargs...)
295+
return N
296+
end
297+
298+
function _native_lq!(
299+
A::AbstractMatrix, L::AbstractMatrix, Q::AbstractMatrix;
300+
positive::Bool = true # always true regardless of setting
301+
)
302+
m, n = size(A)
303+
minmn = min(m, n)
304+
@inbounds for i in 1:minmn
305+
for j in 1:(i - 1)
306+
L[i, j] = A[i, j]
307+
end
308+
β, v, L[i, i] = _householder!(conj!(view(A, i, i:n)), 1)
309+
for j in (i + 1):size(L, 2)
310+
L[i, j] = 0
311+
end
312+
H = Householder(conj(β), v, i:n)
313+
rmul!(A, H; rows = (i + 1):m)
314+
# A[i, i] == 1; store β instead
315+
A[i, i] = β
316+
end
317+
# copy remaining rows for m > n
318+
@inbounds for j in 1:size(L, 2)
319+
for i in (minmn + 1):m
320+
L[i, j] = A[i, j]
321+
end
322+
end
323+
# build Q
324+
one!(Q)
325+
@inbounds for i in minmn:-1:1
326+
β = A[i, i]
327+
A[i, i] = 1
328+
Hᴴ = Householder(β, view(A, i, i:n), i:n)
329+
rmul!(Q, Hᴴ)
330+
end
331+
return L, Q
332+
end
333+
334+
function _native_lq_null!(A::AbstractMatrix, Nᴴ::AbstractMatrix; positive::Bool = true)
335+
m, n = size(A)
336+
minmn = min(m, n)
337+
@inbounds for i in 1:minmn
338+
β, v, ν = _householder!(conj!(view(A, i, i:n)), 1)
339+
H = Householder(conj(β), v, i:n)
340+
rmul!(A, H; rows = (i + 1):m)
341+
# A[i, i] == 1; store β instead
342+
A[i, i] = β
343+
end
344+
# build Nᴴ
345+
fill!(Nᴴ, zero(eltype(Nᴴ)))
346+
one!(view(Nᴴ, 1:(n - minmn), (minmn + 1):n))
347+
@inbounds for i in minmn:-1:1
348+
β = A[i, i]
349+
A[i, i] = 1
350+
Hᴴ = Householder(β, view(A, i, i:n), i:n)
351+
rmul!(Nᴴ, Hᴴ)
352+
end
353+
return Nᴴ
354+
end

src/implementations/qr.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ function qr_null!(A::AbstractMatrix, N, alg::Native_HouseholderQR)
350350
end
351351

352352
function _native_qr!(
353-
A::AbstractMatrix, Q::AbstractMatrix, R::AbstractMatrix
353+
A::AbstractMatrix, Q::AbstractMatrix, R::AbstractMatrix;
354+
positive::Bool = true # always true regardless of setting
354355
)
355356
m, n = size(A)
356357
minmn = min(m, n)
@@ -367,14 +368,15 @@ function _native_qr!(
367368
# A[j,j] == 1; store β instead
368369
A[j, j] = β
369370
end
371+
# copy remaining columns if m < n
370372
@inbounds for j in (minmn + 1):n
371373
for i in 1:size(R, 1)
372374
R[i, j] = A[i, j]
373375
end
374376
end
375377
# build Q
376378
one!(Q)
377-
for j in minmn:-1:1
379+
@inbounds for j in minmn:-1:1
378380
β = A[j, j]
379381
A[j, j] = 1
380382
Hᴴ = Householder(conj(β), view(A, j:m, j), j:m)
@@ -383,7 +385,7 @@ function _native_qr!(
383385
return Q, R
384386
end
385387

386-
function _native_qr_null!(A::AbstractMatrix, N::AbstractMatrix)
388+
function _native_qr_null!(A::AbstractMatrix, N::AbstractMatrix; positive::Bool = true)
387389
m, n = size(A)
388390
minmn = min(m, n)
389391
@inbounds for j in 1:minmn
@@ -393,10 +395,10 @@ function _native_qr_null!(A::AbstractMatrix, N::AbstractMatrix)
393395
# A[j,j] == 1; store β instead
394396
A[j, j] = β
395397
end
396-
# build Q
398+
# build N
397399
fill!(N, zero(eltype(N)))
398400
one!(view(N, (minmn + 1):m, 1:(m - minmn)))
399-
for j in minmn:-1:1
401+
@inbounds for j in minmn:-1:1
400402
β = A[j, j]
401403
A[j, j] = 1
402404
Hᴴ = Householder(conj(β), view(A, j:m, j), j:m)

src/interface/decompositions.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@
1010
# QR, LQ, QL, RQ Decomposition
1111
# ----------------------------
1212
"""
13-
Native_HouseholderQR(; blocksize, positive = false, pivoted = false)
13+
Native_HouseholderQR()
1414
1515
Algorithm type to denote a native implementation for computing the QR decomposition of
16-
a matrix using Householder reflectors, .The diagonal elements of `R` will be non-negative
16+
a matrix using Householder reflectors. The diagonal elements of `R` will be non-negative
1717
by construction.
1818
"""
1919
@algdef Native_HouseholderQR
2020

21+
"""
22+
Native_HouseholderLQ()
23+
24+
Algorithm type to denote a native implementation for computing the LQ decomposition of
25+
a matrix using Householder reflectors. The diagonal elements of `L` will be non-negative
26+
by construction.
27+
"""
28+
@algdef Native_HouseholderLQ
29+
2130
"""
2231
LAPACK_HouseholderQR(; blocksize, positive = false, pivoted = false)
2332

test/lq.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ for T in (BLASFloats..., GenericFloats...), n in (37, m, 63)
5252
TestSuite.test_lq_algs(T, (m, n), LAPACK_LQ_ALGS)
5353
elseif T GenericFloats
5454
TestSuite.test_lq(T, (m, n); test_pivoted = false, test_blocksize = false)
55-
GLA_LQ_ALGS = (LQViaTransposedQR(GLA_HouseholderQR()),)
56-
TestSuite.test_lq_algs(T, (m, n), GLA_LQ_ALGS)
55+
NATIVE_LQ_ALGS = (Native_HouseholderLQ(),)
56+
TestSuite.test_lq_algs(T, (m, n), NATIVE_LQ_ALGS)
5757
end
5858
if m == n
5959
AT = Diagonal{T, Vector{T}}

test/qr.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ for T in (BLASFloats..., GenericFloats...), n in (37, m, 63)
5151
TestSuite.test_qr_algs(T, (m, n), LAPACK_QR_ALGS)
5252
elseif T GenericFloats
5353
TestSuite.test_qr(T, (m, n); test_pivoted = false, test_blocksize = false)
54-
GLA_QR_ALGS = (GLA_HouseholderQR(),)
55-
TestSuite.test_qr_algs(T, (m, n), GLA_QR_ALGS)
54+
NATIVE_QR_ALGS = (Native_HouseholderQR(),)
55+
TestSuite.test_qr_algs(T, (m, n), NATIVE_QR_ALGS)
5656
end
5757
if m == n
5858
AT = Diagonal{T, Vector{T}}

0 commit comments

Comments
 (0)