Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing banded overloads #402

Merged
merged 3 commits into from
May 21, 2024
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
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 @@

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 @@
# 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)

Check warning on line 30 in ext/BlockArraysBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BlockArraysBandedMatricesExt.jl#L30

Added line #L30 was not covered by tests


# _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
Loading