Skip to content

Commit 40ecd59

Browse files
authored
Parameterize BandSlice to accept arbitrary ranges (#415)
* Parameterize BandSlice to accept arbitrary ranges * Test show for BandSlice
1 parent 175e1f7 commit 40ecd59

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BandedMatrices"
22
uuid = "aae01518-5342-5314-be14-df237901396f"
3-
version = "1.3.1"
3+
version = "1.4.0"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/banded/BandedMatrix.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,11 @@ julia> view(B, band(0)) isa BandedMatrices.BandedMatrixBand
456456
true
457457
```
458458
"""
459-
const BandedMatrixBand{T} = SubArray{T, 1, <:ReshapedArray{T,1,
460-
<:Union{BandedMatrix{T}, SubArray{T,2,<:BandedMatrix{T},
461-
<:NTuple{2, AbstractUnitRange{Int}}}}}, Tuple{BandSlice}}
459+
const BandedMatrixBand{T} = Union{
460+
SubArray{T, 1, <:ReshapedArray{T,1,
461+
<:Union{BandedMatrix{T}, SubArray{T,2,<:BandedMatrix{T},
462+
<:NTuple{2, AbstractUnitRange{Int}}}}}, <:Tuple{BandSlice{<:Integer}}},
463+
SubArray{T, 1, <:BandedMatrix{T}, <:Tuple{BandSlice{<:CartesianIndex{2}}}}}
462464

463465

464466
band(V::BandedMatrixBand) = first(parentindices(V)).band.i
@@ -482,11 +484,7 @@ julia> A = BandedMatrix(0=>1:4, 1=>5:7, -1=>8:10)
482484
⋅ 9 3 7
483485
⋅ ⋅ 10 4
484486
485-
julia> v = view(A, band(1))
486-
3-element view(reshape(::BandedMatrix{Int64, Matrix{Int64}, Base.OneTo{Int64}}, 16), BandSlice(Band(1), 5:5:15)) with eltype Int64:
487-
5
488-
6
489-
7
487+
julia> v = view(A, band(1));
490488
491489
julia> BandedMatrices.dataview(v)
492490
3-element view(::Matrix{Int64}, 1, 2:4) with eltype Int64:

src/generic/Band.jl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ checkbandmatch(A::AbstractMatrix, V::AbstractMatrix, ::Colon, ::Colon) =
184184
checkbandmatch(A, V, 1:size(A,1), 1:size(A,2))
185185

186186
"""
187-
BandSlice(band::Band, indices::StepRange{Int,Int}) <: OrdinalRange{Int,Int}
187+
BandSlice(band::Band, indices)
188188
189-
Represent a `StepRange` of indices corresponding to a band.
189+
Represent a range of indices corresponding to a band.
190190
191191
Upon calling `to_indices`, `Band`s are converted to `BandSlice` objects to represent
192192
the indices over which the `Band` spans.
@@ -197,13 +197,20 @@ This mimics the relationship between `Colon` and `Base.Slice`.
197197
```jldoctest
198198
julia> B = BandedMatrix(0 => 1:4, 1=>1:3);
199199
200-
julia> to_indices(B, (Band(1),))[1]
201-
BandSlice(Band(1), 5:5:15)
200+
julia> bs = to_indices(B, (Band(1),))[1];
201+
202+
julia> bs isa BandedMatrices.BandSlice
203+
true
204+
205+
julia> using LinearAlgebra
206+
207+
julia> bs == diagind(B, 1)
208+
true
202209
```
203210
"""
204-
struct BandSlice <: OrdinalRange{Int,Int}
211+
struct BandSlice{T, R<:AbstractRange{T}} <: AbstractRange{T}
205212
band::Band
206-
indices::StepRange{Int,Int}
213+
indices::R
207214
end
208215

209216
for f in (:indices, :unsafe_indices, :axes1, :first, :last, :size, :length,
@@ -212,7 +219,7 @@ for f in (:indices, :unsafe_indices, :axes1, :first, :last, :size, :length,
212219
end
213220

214221
@propagate_inbounds getindex(S::BandSlice, i::Union{Int, AbstractRange}) = getindex(S.indices, i)
215-
show(io::IO, r::BandSlice) = print(io, "BandSlice(", r.band, ", ", r.indices, ")")
222+
show(io::IO, r::BandSlice) = print(io, BandSlice, "(", r.band, ", ", r.indices, ")")
216223

217224
to_index(::Band) = throw(ArgumentError("Block must be converted by to_indices(...)"))
218225

test/test_miscs.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ import BandedMatrices: _BandedMatrix, DefaultBandedMatrix
9292
B = BandedMatrix(-1=>1:4, 0=>1:5, 1=>1:4)
9393
Bshowstr = sprint(show, B)
9494
@test Bshowstr == "BandedMatrix($(-1=>[1:4;]), $(0=>[1:5;]), $(1=>[1:4;]))"
95+
96+
B = BandedMatrix(0=>1:3)
97+
sout = sprint(show, to_indices(B, (band(0),))[1])
98+
@test occursin(repr(diagind(B)), sout)
99+
@test occursin(repr(Band(0)), sout)
95100
end
96101
@time @testset "Issue #27" begin
97102
A=brand(1,10,0,9)

0 commit comments

Comments
 (0)