Skip to content

Commit be112c8

Browse files
committed
Stricter checks, fix left_orth trunc, test trunc
1 parent bd696cd commit be112c8

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/factorizations/orthnull.jl

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,56 @@ function MatrixAlgebraKit.initialize_output(
1414
)
1515
return nothing
1616
end
17-
function MatrixAlgebraKit.check_input(
18-
::typeof(left_orth!), A::AbstractBlockSparseMatrix, F::Nothing
19-
)
17+
function MatrixAlgebraKit.check_input(::typeof(left_orth!), A::AbstractBlockSparseMatrix, F)
18+
!isnothing(F) && throw(
19+
ArgumentError(
20+
"`left_orth!` on block sparse matrices does not support specifying the output"
21+
),
22+
)
2023
return nothing
2124
end
2225

2326
function MatrixAlgebraKit.left_orth_qr!(A::AbstractBlockSparseMatrix, F, alg)
27+
!isnothing(F) && throw(
28+
ArgumentError(
29+
"`left_orth!` on block sparse matrices does not support specifying the output"
30+
),
31+
)
2432
alg′ = select_algorithm(qr_compact!, A, alg)
2533
return qr_compact!(A, alg′)
2634
end
2735
function MatrixAlgebraKit.left_orth_polar!(A::AbstractBlockSparseMatrix, F, alg)
36+
!isnothing(F) && throw(
37+
ArgumentError(
38+
"`left_orth!` on block sparse matrices does not support specifying the output"
39+
),
40+
)
2841
alg′ = select_algorithm(left_polar!, A, alg)
2942
return left_polar!(A, alg′)
3043
end
3144
function MatrixAlgebraKit.left_orth_svd!(
3245
A::AbstractBlockSparseMatrix, F, alg, trunc::Nothing=nothing
3346
)
47+
!isnothing(F) && throw(
48+
ArgumentError(
49+
"`left_orth!` on block sparse matrices does not support specifying the output"
50+
),
51+
)
3452
alg′ = select_algorithm(svd_compact!, A, alg)
3553
U, S, Vᴴ = svd_compact!(A, alg′)
3654
return U, S * Vᴴ
3755
end
56+
function MatrixAlgebraKit.left_orth_svd!(A::AbstractBlockSparseMatrix, F, alg, trunc)
57+
!isnothing(F) && throw(
58+
ArgumentError(
59+
"`left_orth!` on block sparse matrices does not support specifying the output"
60+
),
61+
)
62+
alg′ = select_algorithm(svd_compact!, A, alg)
63+
alg_trunc = select_algorithm(svd_trunc!, A, alg′; trunc)
64+
U, S, Vᴴ = svd_trunc!(A, alg_trunc)
65+
return U, S * Vᴴ
66+
end
3867

3968
function MatrixAlgebraKit.initialize_output(
4069
::typeof(right_orth!), A::AbstractBlockSparseMatrix
@@ -44,25 +73,50 @@ end
4473
function MatrixAlgebraKit.check_input(
4574
::typeof(right_orth!), A::AbstractBlockSparseMatrix, F::Nothing
4675
)
76+
!isnothing(F) && throw(
77+
ArgumentError(
78+
"`right_orth!` on block sparse matrices does not support specifying the output"
79+
),
80+
)
4781
return nothing
4882
end
4983

5084
function MatrixAlgebraKit.right_orth_lq!(A::AbstractBlockSparseMatrix, F, alg)
85+
!isnothing(F) && throw(
86+
ArgumentError(
87+
"`right_orth!` on block sparse matrices does not support specifying the output"
88+
),
89+
)
5190
alg′ = select_algorithm(lq_compact!, A, alg)
5291
return lq_compact!(A, alg′)
5392
end
5493
function MatrixAlgebraKit.right_orth_polar!(A::AbstractBlockSparseMatrix, F, alg)
94+
!isnothing(F) && throw(
95+
ArgumentError(
96+
"`right_orth!` on block sparse matrices does not support specifying the output"
97+
),
98+
)
5599
alg′ = select_algorithm(right_polar!, A, alg)
56100
return right_polar!(A, alg′)
57101
end
58102
function MatrixAlgebraKit.right_orth_svd!(
59103
A::AbstractBlockSparseMatrix, F, alg, trunc::Nothing=nothing
60104
)
105+
!isnothing(F) && throw(
106+
ArgumentError(
107+
"`right_orth!` on block sparse matrices does not support specifying the output"
108+
),
109+
)
61110
alg′ = select_algorithm(svd_compact!, A, alg)
62111
U, S, Vᴴ = svd_compact!(A, alg′)
63112
return U * S, Vᴴ
64113
end
65114
function MatrixAlgebraKit.right_orth_svd!(A::AbstractBlockSparseMatrix, F, alg, trunc)
115+
!isnothing(F) && throw(
116+
ArgumentError(
117+
"`right_orth!` on block sparse matrices does not support specifying the output"
118+
),
119+
)
66120
alg′ = select_algorithm(svd_compact!, A, alg)
67121
alg_trunc = select_algorithm(svd_trunc!, A, alg′; trunc)
68122
U, S, Vᴴ = svd_trunc!(A, alg_trunc)

test/test_factorizations.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ end
250250
@test U * C A
251251
@test Matrix(U'U) LinearAlgebra.I
252252
end
253+
254+
U, C = left_orth(A; trunc=(; maxrank=2))
255+
@test size(U, 2) 2
256+
@test size(C, 1) 2
257+
@test Matrix(U'U) LinearAlgebra.I
253258
end
254259

255260
@testset "right_orth (T=$T)" for T in (Float32, Float64, ComplexF32, ComplexF64)
@@ -262,4 +267,9 @@ end
262267
@test C * U A
263268
@test Matrix(U * U') LinearAlgebra.I
264269
end
270+
271+
C, U = right_orth(A; trunc=(; maxrank=2))
272+
@test size(C, 2) 2
273+
@test size(U, 1) 2
274+
@test Matrix(U * U') LinearAlgebra.I
265275
end

0 commit comments

Comments
 (0)