Skip to content

Return a BandedMatrix from a view of a BandedBlockBandedMatrix #223

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Expand Up @@ -19,7 +19,7 @@ BlockBandedMatricesSparseArraysExt = "SparseArrays"
[compat]
Aqua = "0.8"
ArrayLayouts = "1"
BandedMatrices = "1"
BandedMatrices = "1.9.4"
BlockArrays = "1"
Documenter = "1"
FillArrays = "1"
Expand Down
39 changes: 36 additions & 3 deletions src/BandedBlockBandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
end
end

const DefaultBandedBlockBandedMatrix{T} = BandedBlockBandedMatrix{T, BlockedMatrix{T, Matrix{T}, NTuple{2,DefaultBlockAxis}}, DefaultBlockAxis}
const DefaultBandedBlockBandedMatrix{T} = BandedBlockBandedMatrix{T, <:BlockedMatrix{T, Matrix{T}}}

@inline _BandedBlockBandedMatrix(data::AbstractMatrix, axes::NTuple{2,AbstractUnitRange{Int}}, lu::NTuple{2,Int}, λμ::NTuple{2,Int}) =
_BandedBlockBandedMatrix(BlockedArray(data,(blockedrange(Fill(sum(λμ)+1,sum(lu)+1)),axes[2])), axes[1], lu, λμ)
Expand Down Expand Up @@ -167,7 +167,7 @@
BandedBlockBandedMatrix{T}(m::Union{AbstractMatrix, UniformScaling},
axes::NTuple{2,AbstractUnitRange{Int}},
lu::NTuple{2,Int}, λμ::NTuple{2,Int}) where T =
DefaultBandedBlockBandedMatrix{T}(m, axes, lu, λμ)
BandedBlockBandedMatrix{T,BlockedMatrix{T,Matrix{T},typeof(_bbb_data_axes(axes[2],lu,λμ))},typeof(axes[1])}(m, axes, lu, λμ)

BandedBlockBandedMatrix{T,B,R}(m::Union{AbstractMatrix, UniformScaling},
rdims::AbstractVector{Int}, cdims::AbstractVector{Int},
Expand Down Expand Up @@ -334,6 +334,39 @@
Base.size(arr::BandedBlockBandedMatrix) =
@inbounds return map(length,axes(arr))

@inline function inbands_viewblock(A::BandedBlockBandedMatrix, KJ::Block{2})
l,u = blockbandwidths(A)
K,J = KJ.n
_BandedMatrix(view(A.data, Block(K-J+u+1, J)), length(axes(A,1)[Block(K)]), subblockbandwidths(A)...)

Check warning on line 340 in src/BandedBlockBandedMatrix.jl

View check run for this annotation

Codecov / codecov/patch

src/BandedBlockBandedMatrix.jl#L337-L340

Added lines #L337 - L340 were not covered by tests
end

@inline inbands_viewblock(A::BandedBlockBandedMatrix, K::Block{1}, J::Block{1}) = inbands_viewblock(A, Block(Int(K), Int(J)))

Check warning on line 343 in src/BandedBlockBandedMatrix.jl

View check run for this annotation

Codecov / codecov/patch

src/BandedBlockBandedMatrix.jl#L343

Added line #L343 was not covered by tests

emptyview(A::BlockedMatrix, J) = view(A.blocks, 1:0, axes(A,2)[J])
function emptyview(A, J)
V = view(A, Block(1), J)
view(V, 1:0, 1:size(V,2))

Check warning on line 348 in src/BandedBlockBandedMatrix.jl

View check run for this annotation

Codecov / codecov/patch

src/BandedBlockBandedMatrix.jl#L345-L348

Added lines #L345 - L348 were not covered by tests
end

@inline function outbands_viewblock(A::BandedBlockBandedMatrix, KJ::Block{2})
l,u = blockbandwidths(A)
K,J = KJ.n

Check warning on line 353 in src/BandedBlockBandedMatrix.jl

View check run for this annotation

Codecov / codecov/patch

src/BandedBlockBandedMatrix.jl#L351-L353

Added lines #L351 - L353 were not covered by tests
# Need to make an empty banded matrix in a type-stable way
_BandedMatrix(emptyview(A.data, Block(J)), length(axes(A,1)[Block(K)]), -720,-720)

Check warning on line 355 in src/BandedBlockBandedMatrix.jl

View check run for this annotation

Codecov / codecov/patch

src/BandedBlockBandedMatrix.jl#L355

Added line #L355 was not covered by tests
end



@inline function viewblock(A::BandedBlockBandedMatrix, KJ::Block{2})
@boundscheck blockcheckbounds(A, KJ)
K,J = KJ.n
if -A.u ≤ K-J ≤ A.l
inbands_viewblock(A, KJ)

Check warning on line 364 in src/BandedBlockBandedMatrix.jl

View check run for this annotation

Codecov / codecov/patch

src/BandedBlockBandedMatrix.jl#L360-L364

Added lines #L360 - L364 were not covered by tests
else
outbands_viewblock(A, KJ)

Check warning on line 366 in src/BandedBlockBandedMatrix.jl

View check run for this annotation

Codecov / codecov/patch

src/BandedBlockBandedMatrix.jl#L366

Added line #L366 was not covered by tests
end
end


@inline function getindex(A::BandedBlockBandedMatrix{T}, i::Int, j::Int) where T
@boundscheck checkbounds(A, i, j)
Expand All @@ -346,7 +379,7 @@
@boundscheck checkbounds(A, i, j)
BI,BJ = findblockindex.(axes(A), (i,j))
if -A.l ≤ Int(block(BJ)-block(BI)) ≤ A.u
V = view(A, block(BI),block(BJ))
V = inbands_viewblock(A, block(BI),block(BJ))

Check warning on line 382 in src/BandedBlockBandedMatrix.jl

View check run for this annotation

Codecov / codecov/patch

src/BandedBlockBandedMatrix.jl#L382

Added line #L382 was not covered by tests
@inbounds V[blockindex(BI),blockindex(BJ)] = convert(T, v)::T
elseif !iszero(v)
throw(BandError(A))
Expand Down
2 changes: 1 addition & 1 deletion src/BlockBandedMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import BlockArrays: AbstractBlockLayout, AbstractBlockedUnitRange, Block, BlockI
_blockkron, _blocklengths2blocklasts, block,
blockcheckbounds, blockcolstart, blockcolstop, blockcolsupport, blockindex, blockisequal,
blockrowstart, blockrowstop, blockrowsupport, blocks, blocksize, checksquareblocks,
hasmatchingblocks, sizes_from_blocks
hasmatchingblocks, sizes_from_blocks, viewblock

import FillArrays: Fill, Ones, Zeros

Expand Down
Loading