Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/banded/BandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,9 @@ julia> view(B, band(0)) isa BandedMatrices.BandedMatrixBand
true
```
"""
const BandedMatrixBand{T} = SubArray{T, 1, <:ReshapedArray{T,1,<:BandedMatrix{T}}, Tuple{BandSlice}}
const BandedMatrixBand{T} = SubArray{T, 1, <:ReshapedArray{T,1,
<:Union{BandedMatrix{T}, SubArray{T,2,<:BandedMatrix{T},
<:NTuple{2, AbstractUnitRange{Int}}}}}, Tuple{BandSlice}}


band(V::BandedMatrixBand) = first(parentindices(V)).band.i
Expand Down Expand Up @@ -497,23 +499,27 @@ function dataview(V::BandedMatrixBand)
A = parent(parent(V))
b = band(V)
m,n = size(A)
view(A.data, A.u - b + 1, max(b,0)+1:min(n,m+b))
u = bandwidth(A,2)
view(bandeddata(A), u - b + 1, max(b,0)+1:min(n,m+b))
end

@propagate_inbounds function getindex(B::BandedMatrixBand, i::Int)
A = parent(parent(B))
b = band(B)
if -A.l ≤ band(B) ≤ A.u
l, u = bandwidths(A)
if -l ≤ band(B) ≤ u
dataview(B)[i]
else
@boundscheck checkbounds(B, i)
zero(eltype(B))
end
end

# B[band(i)]
@inline function copyto!(v::AbstractVector, B::BandedMatrixBand)
A = parent(parent(B))
if -A.l ≤ band(B) ≤ A.u
l, u = bandwidths(A)
if -l ≤ band(B) ≤ u
copyto!(v, dataview(B))
else
Binds = axes(B,1)
Expand Down
34 changes: 22 additions & 12 deletions test/test_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -765,21 +765,31 @@ import BandedMatrices: rowstart, rowstop, colstart, colstop,
end

@testset "band views" begin
for A in (rand(11,10), brand(11,10,2,3), brand(Float32, 11,10,2,3),
for AA in (rand(11,10), brand(11,10,2,3), brand(Float32, 11,10,2,3),
brand(ComplexF64, 11,10,2,3))
for k = -5:5
V = view(A, band(k))
bs = BandedMatrices.parentindices(V)[1] # a bandslice
@test bs.indices == diagind(A, k)
@test bs.band == Band(k)
@test collect(bs) == collect(diagind(A, k))
@test Vector{eltype(A)}(V) == collect(V) == A[diagind(A,k)] == A[band(k)]
@test Vector{ComplexF64}(V) == Vector{ComplexF64}(A[diagind(A,k)]) ==
convert(AbstractVector{ComplexF64}, V) == convert(AbstractArray{ComplexF64}, V)
@test V ≡ convert(AbstractArray, V) ≡ convert(AbstractArray{eltype(A)}, V) ≡
convert(AbstractArray, V) ≡ convert(AbstractVector, V)
for A in (AA, view(AA, 1:size(AA,1)-1, 1:size(AA,2)-2))
for k = -5:5
V = view(A, band(k))
bs = BandedMatrices.parentindices(V)[1] # a bandslice
@test bs.indices == diagind(A, k)
@test bs.band == Band(k)
@test collect(bs) == collect(diagind(A, k))
@test Vector{eltype(A)}(V) == collect(V) == A[diagind(A,k)] == A[band(k)]
@test Vector{ComplexF64}(V) == Vector{ComplexF64}(A[diagind(A,k)]) ==
convert(AbstractVector{ComplexF64}, V) == convert(AbstractArray{ComplexF64}, V)
@test V ≡ convert(AbstractArray, V) ≡ convert(AbstractArray{eltype(A)}, V) ≡
convert(AbstractArray, V) ≡ convert(AbstractVector, V)

V .= 0
@test all(iszero, A[band(k)])
end
end
end
A = BandedMatrix(0=>1:4, 1=>5:7, -1=>8:10)
B = view(A, 2:4, 1:3)
@test B[band(0)] == A[band(-1)]
@test B[band(1)] == A[band(0)][2:3]
@test_throws BoundsError view(B, band(-1))[3]
end
end

Expand Down