Skip to content

Commit b4e7e6a

Browse files
committed
Add BandedMatrix extension and tests
1 parent 90e526f commit b4e7e6a

File tree

4 files changed

+144
-13
lines changed

4 files changed

+144
-13
lines changed

Project.toml

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@ version = "3.0-dev"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
7+
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
78
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
89
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
910
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1011

12+
[weakdeps]
13+
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
14+
15+
[extensions]
16+
MatrixFactorizationsBandedMatricesExt = "BandedMatrices"
17+
1118
[compat]
1219
ArrayLayouts = "1.9.2"
20+
BandedMatrices = "1.6"
1321
julia = "1.9"
1422

1523
[extras]
24+
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
1625
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1726

1827
[targets]
19-
test = ["Test"]
28+
test = ["BandedMatrices", "Test"]

ext/MatrixFactorizationsBandedMatricesExt.jl

-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
###
2-
# MatrixFactorizations.QRPackedQ
3-
###
4-
5-
materialize!(M::Lmul{<:QRPackedQLayout{<:AbstractBandedLayout}}) = banded_qr_lmul!(M.A,M.B)
6-
materialize!(M::Lmul{<:AdjQRPackedQLayout{<:AbstractBandedLayout}}) = banded_qr_lmul!(M.A,M.B)
7-
8-
materialize!(M::Rmul{<:Any,<:QRPackedQLayout{<:AbstractBandedLayout}}) = banded_qr_rmul!(M.A,M.B)
9-
materialize!(M::Rmul{<:Any,<:AdjQRPackedQLayout{<:AbstractBandedLayout}}) = banded_qr_rmul!(M.A,M.B)
10-
11-
121
###
132
# QL
143
###

test/runtests.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ include("test_qr.jl")
2929
include("test_ql.jl")
3030
include("test_rq.jl")
3131
include("test_polar.jl")
32-
include("test_reversecholesky.jl")
32+
include("test_reversecholesky.jl")
33+
34+
include("test_banded.jl")

test/test_banded.jl

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
module BandedMatrixFactorizationTests
2+
using MatrixFactorizations, LinearAlgebra, BandedMatrices, Test
3+
4+
@testset "QL tests" begin
5+
for T in (Float64,ComplexF64,Float32,ComplexF32)
6+
A=brand(T,10,10,3,2)
7+
Q,L=ql(A)
8+
@test ql(A).factors ql!(Matrix(A)).factors
9+
@test ql(A).τ ql!(Matrix(A)).τ
10+
@test Matrix(Q)*Matrix(L) A
11+
b=rand(T,10)
12+
@test mul!(similar(b),Q,mul!(similar(b),Q',b)) b
13+
for j=1:size(A,2)
14+
@test Q' * A[:,j] L[:,j]
15+
end
16+
17+
A=brand(T,14,10,3,2)
18+
Q,L=ql(A)
19+
@test ql(A).factors ql!(Matrix(A)).factors
20+
@test ql(A).τ ql!(Matrix(A)).τ
21+
@test_broken Matrix(Q)*Matrix(L) A
22+
23+
for k=1:size(A,1),j=1:size(A,2)
24+
@test Q[k,j] Matrix(Q)[k,j]
25+
end
26+
27+
A=brand(T,10,14,3,2)
28+
Q,L=ql(A)
29+
@test ql(A).factors ql!(Matrix(A)).factors
30+
@test ql(A).τ ql!(Matrix(A)).τ
31+
@test Matrix(Q)*Matrix(L) A
32+
33+
for k=1:size(Q,1),j=1:size(Q,2)
34+
@test Q[k,j] Matrix(Q)[k,j]
35+
end
36+
37+
A=brand(T,10,14,3,6)
38+
Q,L=ql(A)
39+
@test ql(A).factors ql!(Matrix(A)).factors
40+
@test ql(A).τ ql!(Matrix(A)).τ
41+
@test Matrix(Q)*Matrix(L) A
42+
43+
for k=1:size(Q,1),j=1:size(Q,2)
44+
@test Q[k,j] Matrix(Q)[k,j]
45+
end
46+
47+
A=brand(T,100,100,3,4)
48+
@test ql(A).factors ql!(Matrix(A)).factors
49+
@test ql(A).τ ql!(Matrix(A)).τ
50+
b=rand(T,100)
51+
@test ql(A)\b Matrix(A)\b
52+
b=rand(T,100,2)
53+
@test ql(A)\b Matrix(A)\b
54+
@test_throws DimensionMismatch ql(A) \ randn(3)
55+
@test_throws DimensionMismatch ql(A).Q'randn(3)
56+
57+
A=brand(T,102,100,3,4)
58+
@test ql(A).factors ql!(Matrix(A)).factors
59+
@test ql(A).τ ql!(Matrix(A)).τ
60+
b=rand(T,102)
61+
@test_broken ql(A)\b Matrix(A)\b
62+
b=rand(T,102,2)
63+
@test_broken ql(A)\b Matrix(A)\b
64+
@test_throws DimensionMismatch ql(A) \ randn(3)
65+
@test_throws DimensionMismatch ql(A).Q'randn(3)
66+
67+
A=brand(T,100,102,3,4)
68+
@test ql(A).factors ql!(Matrix(A)).factors
69+
@test ql(A).τ ql!(Matrix(A)).τ
70+
b=rand(T,100)
71+
@test_broken ql(A)\b Matrix(A)\b
72+
73+
A = LinearAlgebra.Tridiagonal(randn(T,99), randn(T,100), randn(T,99))
74+
@test ql(A).factors ql!(Matrix(A)).factors
75+
@test ql(A).τ ql!(Matrix(A)).τ
76+
b=rand(T,100)
77+
@test ql(A)\b Matrix(A)\b
78+
b=rand(T,100,2)
79+
@test ql(A)\b Matrix(A)\b
80+
@test_throws DimensionMismatch ql(A) \ randn(3)
81+
@test_throws DimensionMismatch ql(A).Q'randn(3)
82+
end
83+
84+
@testset "lmul!/rmul!" begin
85+
for T in (Float32, Float64, ComplexF32, ComplexF64)
86+
A = brand(T,100,100,3,4)
87+
Q,R = qr(A)
88+
x = randn(T,100)
89+
b = randn(T,100,2)
90+
@test lmul!(Q, copy(x)) Matrix(Q)*x
91+
@test lmul!(Q, copy(b)) Matrix(Q)*b
92+
@test lmul!(Q', copy(x)) Matrix(Q)'*x
93+
@test lmul!(Q', copy(b)) Matrix(Q)'*b
94+
c = randn(T,2,100)
95+
@test rmul!(copy(c), Q) c*Matrix(Q)
96+
@test rmul!(copy(c), Q') c*Matrix(Q')
97+
98+
A = brand(T,100,100,3,4)
99+
Q,L = ql(A)
100+
x = randn(T,100)
101+
b = randn(T,100,2)
102+
@test lmul!(Q, copy(x)) Matrix(Q)*x
103+
@test lmul!(Q, copy(b)) Matrix(Q)*b
104+
@test lmul!(Q', copy(x)) Matrix(Q)'*x
105+
@test lmul!(Q', copy(b)) Matrix(Q)'*b
106+
c = randn(T,2,100)
107+
@test rmul!(copy(c), Q) c*Matrix(Q)
108+
@test rmul!(copy(c), Q') c*Matrix(Q')
109+
end
110+
end
111+
112+
@testset "Mixed types" begin
113+
A=brand(10,10,3,2)
114+
b=rand(ComplexF64,10)
115+
Q,L=ql(A)
116+
@test L\(Q'*b) ql(A)\b Matrix(A)\b
117+
@test Q*L A
118+
119+
A=brand(ComplexF64,10,10,3,2)
120+
b=rand(10)
121+
Q,L=ql(A)
122+
@test Q*L A
123+
@test L\(Q'*b) ql(A)\b Matrix(A)\b
124+
125+
A = BandedMatrix{Int}(undef, (2,1), (4,4))
126+
A.data .= 1:length(A.data)
127+
Q, L = ql(A)
128+
@test Q*L A
129+
end
130+
end
131+
end

0 commit comments

Comments
 (0)