Skip to content

Commit aab5fd4

Browse files
authored
Support banded matrix with block structure (#109)
* Support banded matrix with block structure * DiagonalBlock subarrays are BandedBlockBandedMatrix * Update Project.toml * v0.10.4
1 parent a02a94f commit aab5fd4

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

Project.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockBandedMatrices"
22
uuid = "ffab5731-97b5-5995-9138-79e8c1846df0"
3-
version = "0.10.3"
3+
version = "0.10.4"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
@@ -13,9 +13,9 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1313
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1414

1515
[compat]
16-
ArrayLayouts = "0.5, 0.6"
17-
BandedMatrices = "0.16"
18-
BlockArrays = "0.14, 0.15"
16+
ArrayLayouts = "0.6.3"
17+
BandedMatrices = "0.16.8"
18+
BlockArrays = "0.15"
1919
FillArrays = "0.11"
2020
MatrixFactorizations = "0.7.1, 0.8"
2121
julia = "1.5"

src/BandedBlockBandedMatrix.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,14 @@ bandwidths given by `subblockbandwidths(A)`.
294294
"""
295295
subblockbandwidths(A::BandedBlockBandedMatrix) = (A.λ, A.μ)
296296

297-
# default is to use bandwidth
298-
subblockbandwidths(A::AbstractMatrix) = bandwidths(A)
297+
# default is to use whole block
298+
_subblockbandwidths(A::AbstractMatrix, ::NTuple{2,OneTo{Int}}) = bandwidths(A)
299+
function _subblockbandwidths(A::AbstractMatrix, _)
300+
M,N = map(maximum, blocksizes(A))
301+
M-1,N-1
302+
end
303+
304+
subblockbandwidths(A::AbstractMatrix) = _subblockbandwidths(A, axes(A))
299305

300306
"""
301307
subblockbandwidth(A, i)
@@ -438,6 +444,9 @@ sub_materialize(::AbstractBandedBlockBandedLayout, V, ::Tuple{<:AbstractUnitRang
438444
sub_materialize(::AbstractBandedBlockBandedLayout, V, ::Tuple{<:BlockedUnitRange,<:AbstractUnitRange}) = PseudoBlockArray(V)
439445

440446

447+
sub_materialize(::AbstractBandedLayout, V, ::Tuple{<:BlockedUnitRange,<:BlockedUnitRange}) = BandedMatrix(V)
448+
449+
441450
isbanded(A::SubArray{<:Any,2,<:BandedBlockBandedMatrix}) = MemoryLayout(A) isa AbstractBandedLayout
442451
isbandedblockbanded(A::SubArray{<:Any,2,<:BandedBlockBandedMatrix}) = MemoryLayout(A) isa AbstractBandedBlockBandedLayout
443452

src/interfaceimpl.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function blockbandwidths(P::PseudoBlockMatrix{<:Any,<:Diagonal})
1212
(0,0)
1313
end
1414

15+
blockbandwidths(::Diagonal) = (0,0)
1516
bandeddata(P::PseudoBlockMatrix) = bandeddata(P.blocks)
1617
bandwidths(P::PseudoBlockMatrix) = bandwidths(P.blocks)
1718

@@ -123,4 +124,13 @@ end
123124
###
124125

125126
blockbandwidths(::Zeros) = (-1,-1)
126-
subblockbandwidths(::Zeros) = (-1,-1)
127+
subblockbandwidths(::Zeros) = (-1,-1)
128+
129+
130+
###
131+
# DiagonalBlockMatrix
132+
###
133+
134+
sublayout(::DiagonalLayout{L}, inds::Type{<:NTuple{2,BS}}) where {L,BS<:BlockSlice{<:BlockRange1}} = bandedblockbandedcolumns(sublayout(L(),Tuple{BS}))
135+
subblockbandwidths(::Diagonal) = (0,0)
136+
bandedblockbandeddata(D::Diagonal) = permutedims(D.diag)

test/test_misc.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,23 @@ end
105105
@test A-I == mortar(Tridiagonal(fill([1 2; 1 2],3), fill([2 4; 3 3],4), fill([4 5; 4 5],3))) == Matrix(A) - I
106106
@test I-A == mortar(Tridiagonal(fill(-[1 2; 1 2],3), fill([-2 -4; -3 -3],4), fill(-[4 5; 4 5],3))) == I - Matrix(A)
107107
end
108+
109+
@testset "DiagonalBlock" begin
110+
D = Diagonal(PseudoBlockVector(randn(6), 1:3))
111+
= BandedBlockBandedMatrix(D)
112+
@test== D
113+
@test blockbandwidths(D) == blockbandwidths(D̃) == subblockbandwidths(D) == subblockbandwidths(D̃) == (0,0)
114+
115+
@test @inferred(D[Block(2,2)]) isa BandedMatrix
116+
@test D[Block(2,3)] isa BandedMatrix
117+
@test bandwidths(D[Block(2,2)]) == (0,0)
118+
@test bandwidths(D[Block(2,3)]) == (-720,-720)
119+
@test D[Block(2,2)] == D[2:3,2:3]
120+
@test D[Block(2,3)] == zeros(2,3)
121+
122+
@test D[Block.(1:2),Block.(2:3)] isa BandedBlockBandedMatrix
123+
@test D[Block.(1:2),Block.(2:3)] == D[1:3,2:6]
124+
@test blockbandwidths(D[Block.(1:2),Block.(2:3)]) == (1,-1)
125+
@test subblockbandwidths(D[Block.(1:2),Block.(2:3)]) == (0,0)
126+
end
108127
end

0 commit comments

Comments
 (0)