Skip to content

Commit c7fe62d

Browse files
authored
Faster band indexing for BandedMatrix (#331)
* Faster band indexing for BandedMatrix * only fill elements inrange * recursive in setdocsmeta * docstest setup * remove duplicate doctests * remove jldoctest setup
1 parent 88343ab commit c7fe62d

File tree

7 files changed

+57
-45
lines changed

7 files changed

+57
-45
lines changed

.github/workflows/docs.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ jobs:
1717
- run: |
1818
julia --project=docs -e '
1919
using Pkg
20-
Pkg.develop(PackageSpec(path=pwd()))
20+
Pkg.develop(PackageSpec(; path=pwd()))
2121
Pkg.instantiate()'
22-
- run: |
23-
julia --project=docs -e '
24-
using Documenter: doctest
25-
using BandedMatrices
26-
doctest(BandedMatrices)'
2722
- run: julia --project=docs docs/make.jl
2823
env:
2924
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

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 = "0.17.14"
3+
version = "0.17.15"
44

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

docs/make.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
using Documenter, BandedMatrices
22

3+
DocMeta.setdocmeta!(BandedMatrices, :DocTestSetup, :(using BandedMatrices); recursive=true)
34
makedocs(;
45
modules = [BandedMatrices],
5-
format = Documenter.HTML(
6-
canonical = "https://JuliaMatrices.github.io/BandedMatrices.jl/stable/",
7-
),
86
pages = [
97
"Home" => "index.md",
108
],
11-
repo = "https://github.com/JuliaMatrices/BandedMatrices.jl/blob/{commit}{path}#L{line}",
129
sitename = "BandedMatrices.jl",
1310
authors = "Sheehan Olver, Mikael Slevinsky, and contributors.",
1411
)
1512

1613

1714
deploydocs(;
18-
repo = "github.com/JuliaMatrices/BandedMatrices.jl.git"
15+
repo = "github.com/JuliaLinearAlgebra/BandedMatrices.jl.git"
1916
)

src/BandedMatrices.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Base: axes, axes1, getproperty, getindex, setindex!, *, +, -, ==, <, <=,
77
>=, /, ^, \, adjoint, transpose, showerror, convert, size, view,
88
unsafe_indices, first, last, size, length, unsafe_length, step, to_indices,
99
to_index, show, fill!, similar, copy, promote_rule, IndexStyle, real, imag,
10-
copyto!
10+
copyto!, Array
1111

1212
using Base.Broadcast: AbstractArrayStyle, DefaultArrayStyle, Broadcasted
1313
import Base.Broadcast: BroadcastStyle, broadcasted, materialize, materialize!

src/banded/BandedMatrix.jl

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -395,51 +395,70 @@ end
395395
# we reduce it to converting a View
396396

397397

398-
# type to represent a view of a band
399-
const BandedMatrixBand{T} = SubArray{T, 1, ReshapedArray{T,1,BandedMatrix{T},
400-
Tuple{MultiplicativeInverses.SignedMultiplicativeInverse{Int}}}, Tuple{BandSlice}, false}
398+
"""
399+
BandedMatrixBand
400+
401+
Type to represent a view of a band of a `BandedMatrix`
402+
403+
# Examples
404+
```jldoctest
405+
julia> B = BandedMatrix(0=>1:3);
406+
407+
julia> view(B, band(0)) isa BandedMatrices.BandedMatrixBand
408+
true
409+
```
410+
"""
411+
const BandedMatrixBand{T} = SubArray{T, 1, <:ReshapedArray{T,1,<:BandedMatrix{T}}, Tuple{BandSlice}}
401412

402413

403414
band(V::BandedMatrixBand) = first(parentindices(V)).band.i
404415

405-
# gives a view of the parent's data matrix
416+
417+
"""
418+
dataview(V::BandedMatrices.BandedMatrixBand)
419+
420+
Forward a view of a band of a `BandedMatrix` to the parent's data matrix.
421+
422+
# Examples
423+
```jldoctest
424+
julia> A = BandedMatrix(0=>1:4, 1=>5:7, -1=>8:10)
425+
4×4 BandedMatrix{Int64} with bandwidths (1, 1):
426+
1 5 ⋅ ⋅
427+
8 2 6 ⋅
428+
⋅ 9 3 7
429+
⋅ ⋅ 10 4
430+
431+
julia> v = view(A, band(1))
432+
3-element view(reshape(::BandedMatrix{Int64, Matrix{Int64}, Base.OneTo{Int64}}, 16), BandSlice(Band(1), 5:5:15)) with eltype Int64:
433+
5
434+
6
435+
7
436+
437+
julia> BandedMatrices.dataview(v)
438+
3-element view(::Matrix{Int64}, 1, 2:4) with eltype Int64:
439+
5
440+
6
441+
7
442+
```
443+
"""
406444
function dataview(V::BandedMatrixBand)
407445
A = parent(parent(V))
408446
b = band(V)
409447
m,n = size(A)
410-
if b > 0
411-
view(A.data, A.u - b + 1, b+1:min(n,m+b))
412-
elseif b == 0
413-
view(A.data, A.u - b + 1, 1:min(n,m))
414-
else # b < 0
415-
view(A.data, A.u - b + 1, 1:min(n,m+b))
416-
end
448+
view(A.data, A.u - b + 1, max(b,0)+1:min(n,m+b))
417449
end
418450

419-
function convert(::Type{Vector{T}}, V::BandedMatrixBand) where T
420-
A = parent(parent(V))
421-
if -A.l band(V) A.u
422-
Vector{T}(dataview(V))
451+
function copyto!(v::Vector, B::BandedMatrixBand)
452+
A = parent(parent(B))
453+
if -A.l band(B) A.u
454+
copyto!(v, dataview(B))
423455
else
424-
zeros(T, length(V))
456+
Binds = axes(B,1)
457+
v[Binds] .= 0
425458
end
459+
return v
426460
end
427461

428-
convert(::Type{Array{T}}, A::BandedMatrixBand) where T = convert(Vector{T}, A)
429-
convert(::Type{Array}, A::BandedMatrixBand) = convert(Vector{eltype(A)}, A)
430-
convert(::Type{Vector}, A::BandedMatrixBand)= convert(Vector{eltype(A)}, A)
431-
432-
433-
convert(::Type{AbstractArray{T}}, A::BandedMatrixBand{T}) where T = A
434-
convert(::Type{AbstractVector{T}}, A::BandedMatrixBand{T}) where T = A
435-
convert(::Type{AbstractArray}, A::BandedMatrixBand{T}) where T = A
436-
convert(::Type{AbstractVector}, A::BandedMatrixBand{T}) where T = A
437-
438-
convert(::Type{AbstractArray{T}}, A::BandedMatrixBand) where T = convert(Vector{T}, A)
439-
convert(::Type{AbstractVector{T}}, A::BandedMatrixBand) where T = convert(Vector{T}, A)
440-
441-
442-
443462
# ~ indexing along a row
444463

445464

src/generic/Band.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ the indices over which the `Band` spans.
194194
This mimics the relationship between `Colon` and `Base.Slice`.
195195
196196
# Example
197-
```jldoctest; setup=:(using BandedMatrices)
197+
```jldoctest
198198
julia> B = BandedMatrix(0 => 1:4, 1=>1:3);
199199
200200
julia> to_indices(B, (Band(1),))[1]

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using Aqua
99
end
1010

1111
using Documenter
12+
DocMeta.setdocmeta!(BandedMatrices, :DocTestSetup, :(using BandedMatrices))
1213
@testset "doctests" begin
1314
doctest(BandedMatrices)
1415
end

0 commit comments

Comments
 (0)