Skip to content

Commit 1d39704

Browse files
committed
Fix bugs and increase coverage
1 parent e27d89b commit 1d39704

File tree

4 files changed

+131
-6
lines changed

4 files changed

+131
-6
lines changed

ext/InfiniteArraysBandedMatricesExt.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const TriPertToeplitz{T} = Tridiagonal{T,Vcat{T,1,Tuple{Vector{T},Fill{T,1,Tuple
6363
const AdjTriPertToeplitz{T} = Adjoint{T,Tridiagonal{T,Vcat{T,1,Tuple{Vector{T},Fill{T,1,Tuple{OneToInf{Int}}}}}}}
6464
const InfBandedMatrix{T,V<:AbstractMatrix{T}} = BandedMatrix{T,V,OneToInf{Int}}
6565

66-
_prepad(p, a) = Vcat(Zeros(max(p,0)), a)
66+
_prepad(p, a) = Vcat(Zeros{eltype(a)}(max(p,0)), a)
6767
_prepad(p, a::Zeros{T,1}) where T = Zeros{T}(length(a)+p)
6868
_prepad(p, a::Ones{T,1}) where T = Ones{T}(length(a)+p)
6969
_prepad(p, a::AbstractFill{T,1}) where T = Fill{T}(getindex_value(a), length(a)+p)
@@ -74,7 +74,6 @@ function BandedMatrix{T}(kv::Tuple{Vararg{Pair{<:Integer,<:AbstractVector}}},
7474
::NTuple{2,PosInfinity},
7575
(l,u)::NTuple{2,Integer}) where T
7676
ks = getproperty.(kv, :first)
77-
l,u = -minimum(ks),maximum(ks)
7877
rws = Vcat(permutedims.(_prepad.(ks,getproperty.(kv, :second)))...)
7978
c = zeros(T, l+u+1, length(ks))
8079
for (k,j) in zip(u .- ks .+ 1,1:length(ks))
@@ -425,8 +424,8 @@ _default_banded_broadcast(bc::Broadcasted, ::Tuple{<:OneToInf,<:Any}) = copy(Bro
425424
# Banded * Banded
426425
###
427426

428-
BandedMatrix{T}(::UndefInitializer, axes::Tuple{OneToInf{Int},OneTo{Int}}, lu::NTuple{2,Integer}) where T =
429-
BandedMatrix{T}(undef, map(length,axes), lu)
427+
BandedMatrix{T}(::UndefInitializer, axes::Tuple{OneToInf{Int},OneTo{Int}}, lu::NTuple{2,Integer}) where T = BandedMatrix{T}(undef, map(length,axes), lu)
428+
BandedMatrix{T}(::UndefInitializer, (m,n)::Tuple{Infinity,Int}, lu::NTuple{2,Integer}) where T = BandedMatrix{T}(undef, (ℵ₀,n), lu)
430429

431430
similar(M::MulAdd{<:AbstractBandedLayout,<:AbstractBandedLayout}, ::Type{T}, axes::Tuple{OneTo{Int},OneToInf{Int}}) where T =
432431
transpose(BandedMatrix{T}(undef, reverse(axes), reverse(bandwidths(M))))

src/reshapedarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using Base.MultiplicativeInverses: SignedMultiplicativeInverse
44

5-
struct ReshapedArray{T,N,P<:AbstractArray,DIMS<:Tuple,MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int}}}} <: AbstractArray{T,N}
5+
struct ReshapedArray{T,N,P<:AbstractArray,DIMS<:Tuple,MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int}}}} <: LayoutArray{T,N}
66
parent::P
77
dims::DIMS
88
mi::MI

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,8 @@ end
12601260
A = a * Ones{Complex{Int}}(1,∞)
12611261
@test A[:,1:5] == a * ones(1,5)
12621262

1263-
a*permutedims(1:∞)
1263+
@test (a*permutedims(1:∞))[:,1:5] == a*(1:5)'
1264+
@test (a*Hcat(Zeros(1,2), permutedims(1:∞)))[1,1:5] == (a*Vcat(Hcat(Zeros(1,2), permutedims(1:∞))))[1,1:5]
12641265
end
12651266

12661267
include("test_infbanded.jl")

test/test_infbanded.jl

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,130 @@ using LazyArrays: simplifiable
186186
@testset "SubArray broadcasting" begin
187187
A = BandedMatrix(2 => 1:∞)
188188
@test exp.(A[1:2:∞,1:2:∞])[1:10,1:10] exp.(A[1:2:20,1:2:20])
189+
@test A[band(2)][1:5] == 1:5
190+
@test _BandedMatrix((1:∞)', ∞, -1,1)[band(1)][1:5] == 2:6
191+
@test exp.(view(A,band(2)))[1:10] exp.(1:10)
192+
193+
@test BandedMatrices.banded_similar(Int, (∞,5), (1,1)) isa BandedMatrix
194+
@test BandedMatrices.banded_similar(Int, (5,∞), (1,1)) isa Adjoint{<:Any,<:BandedMatrix}
195+
196+
A = BandedMatrix{Int}((2 => 1:∞,), (∞,∞), (0,2))
197+
@test eltype(A) == Int
198+
@test bandwidths(A) == (0,2)
199+
200+
A = BandedMatrix{Int}((2 => Vcat([1,2], Fill(2,∞)),), (∞,∞), (0,2))
201+
@test A[band(2)][1:5] == [1; fill(2,4)]
189202
end
203+
204+
@testset "Algebra" begin
205+
A = BandedMatrix(-3 => Fill(7 / 10, ∞), -2 => 1:∞, 1 => Fill(2im, ∞))
206+
@test A isa BandedMatrix{ComplexF64}
207+
@test A[1:10, 1:10] == diagm(-3 => Fill(7 / 10, 7), -2 => 1:8, 1 => Fill(2im, 9))
208+
209+
A = BandedMatrix(0 => Vcat([1, 2, 3], Zeros(∞)), 1 => Vcat(1, Zeros(∞)))
210+
@test A[1, 2] == 1
211+
212+
A = BandedMatrix(-3 => Fill(7 / 10, ∞), -2 => Fill(1, ∞), 1 => Fill(2im, ∞))
213+
Ac = BandedMatrix(A')
214+
At = BandedMatrix(transpose(A))
215+
@test Ac[1:10, 1:10] (A')[1:10, 1:10] A[1:10, 1:10]'
216+
@test At[1:10, 1:10] transpose(A)[1:10, 1:10] transpose(A[1:10, 1:10])
217+
218+
A = BandedMatrix(-1 => Vcat(Float64[], Fill(1 / 4, ∞)), 0 => Vcat([1.0 + im], Fill(0, ∞)), 1 => Vcat(Float64[], Fill(1, ∞)))
219+
@test MemoryLayout(typeof(view(A.data, :, 1:10))) == ApplyLayout{typeof(hcat)}()
220+
Ac = BandedMatrix(A')
221+
At = BandedMatrix(transpose(A))
222+
@test Ac[1:10, 1:10] (A')[1:10, 1:10] A[1:10, 1:10]'
223+
@test At[1:10, 1:10] transpose(A)[1:10, 1:10] transpose(A[1:10, 1:10])
224+
225+
A = BandedMatrix(-2 => Vcat(Float64[], Fill(1 / 4, ∞)), 0 => Vcat([1.0 + im, 2, 3], Fill(0, ∞)), 1 => Vcat(Float64[], Fill(1, ∞)))
226+
Ac = BandedMatrix(A')
227+
At = BandedMatrix(transpose(A))
228+
@test Ac[1:10, 1:10] (A')[1:10, 1:10] A[1:10, 1:10]'
229+
@test At[1:10, 1:10] transpose(A)[1:10, 1:10] transpose(A[1:10, 1:10])
230+
231+
A = _BandedMatrix(Fill(1, 4, ∞), ℵ₀, 1, 2)
232+
@test A^2 isa BandedMatrix
233+
@test (A^2)[1:10, 1:10] == (A*A)[1:10, 1:10] == (A[1:100, 1:100]^2)[1:10, 1:10]
234+
@test A^3 isa ApplyMatrix{<:Any,typeof(*)}
235+
@test (A^3)[1:10, 1:10] == (A*A*A)[1:10, 1:10] == ((A*A)*A)[1:10, 1:10] == (A*(A*A))[1:10, 1:10] == (A[1:100, 1:100]^3)[1:10, 1:10]
236+
237+
@testset "∞ x finite" begin
238+
A = BandedMatrix(1 => 1:∞) + BandedMatrix(-1 => Fill(2, ∞))
239+
B = _BandedMatrix(randn(3, 5), ℵ₀, 1, 1)
240+
241+
@test lmul!(2.0, copy(B)')[:, 1:10] == (2B')[:, 1:10]
242+
243+
@test_throws ArgumentError BandedMatrix(A)
244+
@test A * B isa MulMatrix
245+
@test B'A isa MulMatrix
246+
247+
@test all(diag(A[1:6, 1:6]) .=== zeros(6))
248+
249+
@test (A*B)[1:7, 1:5] A[1:7, 1:6] * B[1:6, 1:5]
250+
@test (B'A)[1:5, 1:7] (B')[1:5, 1:6] * A[1:6, 1:7]
251+
end
252+
end
253+
254+
@testset "Fill" begin
255+
A = _BandedMatrix(Ones(1, ∞), ℵ₀, -1, 1)
256+
@test 1.0 .* A isa BandedMatrix{Float64,<:Fill}
257+
@test Zeros(∞) .* A Zeros(∞, ∞) .* A A .* Zeros(1, ∞) A .* Zeros(∞, ∞) Zeros(∞, ∞)
258+
@test Ones(∞) .* A isa BandedMatrix{Float64,<:Ones}
259+
@test A .* Ones(1, ∞) isa BandedMatrix{Float64,<:Ones}
260+
@test 2.0 .* A isa BandedMatrix{Float64,<:Fill}
261+
@test A .* 2.0 isa BandedMatrix{Float64,<:Fill}
262+
@test Eye(∞) * A isa BandedMatrix{Float64,<:Ones}
263+
@test A * Eye(∞) isa BandedMatrix{Float64,<:Ones}
264+
265+
@test A * A isa BandedMatrix
266+
@test (A*A)[1:10, 1:10] == BandedMatrix(2 => Ones(8))
267+
268+
= _BandedMatrix(Fill(1, 1, ∞), ℵ₀, -1, 1)
269+
@test A *isa BandedMatrix
270+
@test* A isa BandedMatrix
271+
@test*isa BandedMatrix
272+
273+
B = _BandedMatrix(Ones(1, 10), ℵ₀, -1, 1)
274+
C = _BandedMatrix(Ones(1, 10), 10, -1, 1)
275+
D = _BandedMatrix(Ones(1, ∞), 10, -1, 1)
276+
277+
@test (A*B)[1:10, 1:10] == (B*C)[1:10, 1:10] == (D*A)[1:10, 1:10] == D * B == (C*D)[1:10, 1:10] == BandedMatrix(2 => Ones(8))
278+
end
279+
280+
@testset "Banded Broadcast" begin
281+
A = _BandedMatrix((1:∞)', ℵ₀, -1, 1)
282+
@test 2.0 .* A isa BandedMatrix{Float64,<:Adjoint}
283+
@test A .* 2.0 isa BandedMatrix{Float64,<:Adjoint}
284+
@test Eye(∞) * A isa BandedMatrix{Float64,<:Adjoint}
285+
@test A * Eye(∞) isa BandedMatrix{Float64,<:Adjoint}
286+
A = _BandedMatrix(Vcat((1:∞)', Ones(1, ∞)), ℵ₀, 0, 1)
287+
@test 2.0 .* A isa BandedMatrix
288+
@test A .* 2.0 isa BandedMatrix
289+
@test Eye(∞) * A isa BandedMatrix
290+
@test A * Eye(∞) isa BandedMatrix
291+
b = 1:
292+
@test bandwidths(b .* A) == (0, 1)
293+
294+
@test colsupport(b .* A, 1) == 1:1
295+
@test Base.replace_in_print_matrix(b .* A, 2, 1, "0.0") == ""
296+
@test bandwidths(A .* b) == (0, 1)
297+
@test A .* b' isa BroadcastArray
298+
@test bandwidths(A .* b') == bandwidths(A .* b')
299+
@test colsupport(A .* b', 3) == 2:3
300+
301+
A = _BandedMatrix(Ones{Int}(1, ∞), ℵ₀, 0, 0)'
302+
B = _BandedMatrix((-2:-2:-∞)', ℵ₀, -1, 1)
303+
C = Diagonal(2 ./ (1:2:∞))
304+
@test bandwidths(A * (B * C)) == (-1, 1)
305+
@test bandwidths((A * B) * C) == (-1, 1)
306+
307+
A = _BandedMatrix(Ones{Int}(1, ∞), ℵ₀, 0, 0)'
308+
B = _BandedMatrix((-2:-2:-∞)', ℵ₀, -1, 1)
309+
@test MemoryLayout(A + B) isa BroadcastBandedLayout{typeof(+)}
310+
@test MemoryLayout(2 * (A + B)) isa BroadcastBandedLayout{typeof(*)}
311+
@test bandwidths(A + B) == (0, 1)
312+
@test bandwidths(2 * (A + B)) == (0, 1)
313+
end
314+
190315
end

0 commit comments

Comments
 (0)