Skip to content

Commit b261448

Browse files
committed
Add tests
1 parent 1b66984 commit b261448

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/factorizations/qr.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ using MatrixAlgebraKit: MatrixAlgebraKit, qr_compact!, qr_full!
22

33
# TODO: this is a hardcoded for now to get around this function not being defined in the
44
# type domain
5-
function MatrixAlgebraKit.default_qr_algorithm(A::AbstractBlockSparseMatrix; kwargs...)
5+
function default_blocksparse_qr_algorithm(A::AbstractMatrix; kwargs...)
66
blocktype(A) <: StridedMatrix{<:LinearAlgebra.BLAS.BlasFloat} ||
77
error("unsupported type: $(blocktype(A))")
88
alg = MatrixAlgebraKit.LAPACK_HouseholderQR(; kwargs...)
99
return BlockPermutedDiagonalAlgorithm(alg)
1010
end
11+
function MatrixAlgebraKit.default_algorithm(
12+
::typeof(qr_compact!), A::AbstractBlockSparseMatrix; kwargs...
13+
)
14+
return default_blocksparse_qr_algorithm(A; kwargs...)
15+
end
16+
function MatrixAlgebraKit.default_algorithm(
17+
::typeof(qr_full!), A::AbstractBlockSparseMatrix; kwargs...
18+
)
19+
return default_blocksparse_qr_algorithm(A; kwargs...)
20+
end
1121

1222
function MatrixAlgebraKit.initialize_output(
1323
::typeof(qr_compact!), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm

test/test_factorizations.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BlockArrays: Block, BlockedMatrix, BlockedVector, blocks, mortar
22
using BlockSparseArrays: BlockSparseArray, BlockDiagonal, eachblockstoredindex
3-
using MatrixAlgebraKit: svd_compact, svd_full, svd_trunc, truncrank, trunctol
3+
using MatrixAlgebraKit:
4+
qr_compact, qr_full, svd_compact, svd_full, svd_trunc, truncrank, trunctol
45
using LinearAlgebra: LinearAlgebra
56
using Random: Random
67
using Test: @inferred, @testset, @test
@@ -154,3 +155,29 @@ end
154155
@test (V1ᴴ * V1ᴴ' LinearAlgebra.I)
155156
end
156157
end
158+
159+
@testset "qr_compact" for T in (Float32, Float64, ComplexF32, ComplexF64)
160+
for i in [1, 2], j in [1, 2], k in [1, 2], l in [1, 2]
161+
A = BlockSparseArray{T}(undef, ([i, j], [k, l]))
162+
A[Block(1, 1)] = randn(T, i, k)
163+
A[Block(2, 2)] = randn(T, j, l)
164+
Q, R = qr_compact(A)
165+
@test Matrix(Q'Q) LinearAlgebra.I
166+
@test A Q * R
167+
end
168+
end
169+
170+
@testset "qr_full" for T in (Float32, Float64, ComplexF32, ComplexF64)
171+
for i in [2, 3], j in [2, 3], k in [2, 3], l in [2, 3]
172+
A = BlockSparseArray{T}(undef, ([i, j], [k, l]))
173+
A[Block(1, 1)] = randn(T, i, k)
174+
A[Block(2, 2)] = randn(T, j, l)
175+
Q, R = qr_full(A)
176+
Q′, R′ = qr_full(Matrix(A))
177+
@test size(Q) == size(Q′)
178+
@test size(R) == size(R′)
179+
@test Matrix(Q'Q) LinearAlgebra.I
180+
@test Matrix(Q * Q') LinearAlgebra.I
181+
@test A Q * R
182+
end
183+
end

0 commit comments

Comments
 (0)