Skip to content

Commit

Permalink
Fix indexing for offset views of BandedMatrixes (#432)
Browse files Browse the repository at this point in the history
* Fix indexing for offset views of BandedMatrixes

* Fix starting index in colstart
  • Loading branch information
jishnub authored Mar 18, 2024
1 parent 3e408f5 commit 797a574
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "BandedMatrices"
uuid = "aae01518-5342-5314-be14-df237901396f"
version = "1.6.0"
version = "1.6.1"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
8 changes: 4 additions & 4 deletions src/generic/AbstractBandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ julia> BandedMatrices.colstart(A, 4)
3
```
"""
@inline colstart(A, i::Integer) = max(i-bandwidth(A,2), 1)
@inline colstart(A, i::Integer) = max(i-bandwidth(A,2), 1) + firstindex(A,1)-1

"""
colstop(A, i::Integer)
Expand All @@ -84,7 +84,7 @@ julia> BandedMatrices.colstop(A, 4)
4
```
"""
@inline colstop(A, i::Integer) = max(min(i+bandwidth(A,1), size(A, 1)), 0)
@inline colstop(A, i::Integer) = clamp(i+bandwidth(A,1), 0:size(A, 1)) + firstindex(A,1)-1

"""
rowstart(A, i::Integer)
Expand All @@ -108,7 +108,7 @@ julia> BandedMatrices.rowstart(A, 3)
3
```
"""
@inline rowstart(A, i::Integer) = max(i-bandwidth(A,1), 1)
@inline rowstart(A, i::Integer) = max(i-bandwidth(A,1), 1) + firstindex(A,2)-1

"""
rowstop(A, i::Integer)
Expand All @@ -132,7 +132,7 @@ julia> BandedMatrices.rowstop(A, 4)
4
```
"""
@inline rowstop(A, i::Integer) = max(min(i+bandwidth(A,2), size(A, 2)), 0)
@inline rowstop(A, i::Integer) = clamp(i+bandwidth(A,2), 0:size(A, 2)) + firstindex(A,2)-1

"""
colrange(A, i::Integer)
Expand Down
9 changes: 9 additions & 0 deletions test/test_banded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,13 @@ Base.similar(::MyMatrix, ::Type{T}, m::Int, n::Int) where T = MyMatrix{T}(undef,
copyto!(view(B, :, :), S)
@test B == 2B2
end

@testset "offset views" begin
B = BandedMatrix(0=>1:4)
A = view(B, Base.IdentityUnitRange(2:4), 1:4)
@test !any(in(axes(A,1)), BandedMatrices.colrange(A, 1))
@test BandedMatrices.colrange(A, 2) == 2:2
@test BandedMatrices.colrange(A, 3) == 3:3
@test BandedMatrices.colrange(A, 4) == 4:4
end
end

2 comments on commit 797a574

@jishnub
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/103097

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.6.1 -m "<description of version>" 797a574d64a3c69a1a6d86644cb7997c9ed989dd
git push origin v1.6.1

Please sign in to comment.