Skip to content

Commit

Permalink
Add missing banded overloads (#402)
Browse files Browse the repository at this point in the history
* Add missing banded sub_materailize

* Move over _copyto! and BroadcastStyle overloads

* add test, disable unneeded(?) code
  • Loading branch information
dlfivefifty authored May 21, 2024
1 parent bb3bf1b commit fd96cb5
Show file tree
Hide file tree
Showing 3 changed files with 89 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 = "BlockArrays"
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
version = "1.0.0"
version = "1.0.1"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
73 changes: 70 additions & 3 deletions ext/BlockArraysBandedMatricesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module BlockArraysBandedMatricesExt

using BandedMatrices, BlockArrays
using BlockArrays.ArrayLayouts
import BandedMatrices: isbanded, AbstractBandedLayout, bandeddata, bandwidths
import BlockArrays: blockcolsupport, blockrowsupport, AbstractBlockedUnitRange
import ArrayLayouts: sub_materialize
using BlockArrays.LinearAlgebra
import BandedMatrices: isbanded, AbstractBandedLayout, BandedColumns, bandeddata, bandwidths
import BlockArrays: blockcolsupport, blockrowsupport, AbstractBlockedUnitRange, BlockLayout, BlockSlice1
import ArrayLayouts: sub_materialize, _copyto!
import Base: BroadcastStyle


bandeddata(P::BlockedMatrix) = bandeddata(P.blocks)
Expand All @@ -25,6 +27,71 @@ end
# ambiguity
sub_materialize(::AbstractBandedLayout, V, ::Tuple{AbstractBlockedUnitRange,Base.OneTo{Int}}) = BandedMatrix(V)
sub_materialize(::AbstractBandedLayout, V, ::Tuple{Base.OneTo{Int},AbstractBlockedUnitRange}) = BandedMatrix(V)
sub_materialize(::AbstractBandedLayout, V, ::Tuple{AbstractBlockedUnitRange,AbstractBlockedUnitRange}) = BandedMatrix(V)


# _copyto!
# disabled as not clear its needed and used undefined colblockbandwidths

# function _copyto!(_, ::BlockLayout{<:BandedColumns}, dest::AbstractMatrix, src::AbstractMatrix)
# if !blockisequal(axes(dest), axes(src))
# copyto!(BlockedArray(dest, axes(src)), src)
# return dest
# end

# srcB = blocks(src)
# srcD = bandeddata(srcB)

# dl, du = colblockbandwidths(dest)
# sl, su = bandwidths(srcB)
# M,N = size(srcB)
# # Source matrix must fit within bands of destination matrix
# all(dl .≥ min(sl,M-1)) && all(du .≥ min(su,N-1)) || throw(BandError(dest))

# for J = 1:N
# for K = max(1,J-du[J]):min(J-su-1,M)
# zero!(view(dest,Block(K),Block(J)))
# end
# for K = max(1,J-su):min(J+sl,M)
# copyto!(view(dest,Block(K),Block(J)), srcD[K-J+su+1,J])
# end
# for K = max(1,J+sl+1):min(J+dl[J],M)
# zero!(view(dest,Block(K),Block(J)))
# end
# end
# dest
# end

# function _copyto!(_, ::BlockLayout{<:AbstractBandedLayout}, dest::AbstractMatrix, src::AbstractMatrix)
# if !blockisequal(axes(dest), axes(src))
# copyto!(BlockedArray(dest, axes(src)), src)
# return dest
# end

# srcB = blocks(src)

# dl, du = colblockbandwidths(dest)
# sl, su = bandwidths(srcB)
# M,N = size(srcB)
# # Source matrix must fit within bands of destination matrix
# all(dl .≥ min(sl,M-1)) && all(du .≥ min(su,N-1)) || throw(BandError(dest))

# for J = 1:N
# for K = max(1,J-du[J]):min(J-su-1,M)
# zero!(view(dest,Block(K),Block(J)))
# end
# for K = max(1,J-su):min(J+sl,M)
# copyto!(view(dest,Block(K),Block(J)), inbands_getindex(srcB, K, J))
# end
# for K = max(1,J+sl+1):min(J+dl[J],M)
# zero!(view(dest,Block(K),Block(J)))
# end
# end
# dest
# end

## WARNING: type piracy
# BroadcastStyle(::Type{<:SubArray{<:Any,2,<:BlockedMatrix{<:Any,<:Diagonal}, <:Tuple{<:BlockSlice1,<:BlockSlice1}}}) = BandedStyle()


end
19 changes: 18 additions & 1 deletion test/test_blockbanded.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TestBlockArraysBandedMatrices

using BlockArrays, LinearAlgebra, BandedMatrices, Test
using BlockArrays: BlockDiagonal, BlockBidiagonal, BlockTridiagonal, blockcolsupport, blockrowsupport
using BlockArrays: BlockDiagonal, BlockBidiagonal, BlockTridiagonal, blockcolsupport, blockrowsupport, BlockLayout
using BandedMatrices: _BandedMatrix


Expand Down Expand Up @@ -70,6 +70,23 @@ using BandedMatrices: _BandedMatrix
A = BlockedArray(brand(5,4,1,2), [3,2], [2,2])
@test bandwidths(A) == (1,2)
@test BandedMatrix(A) == A
@test copyto!(similar(A), A) == A
end

@testset "block banded" begin
B = BandedMatrix{Matrix{Float64}}(undef, (3, 4), (1,2))
for k = axes(B,1), j = max(1,k-1):min(4,k+2)
B[k,j] = randn(2,2)
end
A = BlockArrays._BlockArray(B, (blockedrange(fill(2,3)), blockedrange(fill(2,4))))
@test copyto!(zeros(size(A)), A) == Matrix(A)
end

@testset "Blocked Diagonal" begin
A = BlockedMatrix(Diagonal(1:5), [2,3], [2,2,1])
@test A[Block(2,2)] isa BandedMatrix
@test bandwidths(A[Block(2,2)]) == (0,0)
@test A[Block.(1:2), Block.(2:3)] isa BandedMatrix
end
end

Expand Down

2 comments on commit fd96cb5

@dlfivefifty
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/107315

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.0.1 -m "<description of version>" fd96cb5361cc342801b150734229326dea76477c
git push origin v1.0.1

Please sign in to comment.