Skip to content

More general truncation and slicing #159

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 9 commits into
base: main
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
@@ -1,7 +1,7 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.7.22"
version = "0.7.23"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
1 change: 1 addition & 0 deletions src/BlockArraysExtensions/BlockArraysExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ const GenericBlockIndexVectorSlices = BlockIndices{
<:BlockVector{<:GenericBlockIndex{1},<:Vector{<:BlockIndexVector}}
}
const SubBlockSliceCollection = Union{
Base.Slice,
BlockIndexRangeSlice,
BlockIndexRangeSlices,
BlockIndexVectorSlices,
Expand Down
9 changes: 9 additions & 0 deletions src/BlockArraysExtensions/blockedunitrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ BlockArrays.Block(b::BlockIndexVector) = b.block

Base.copy(a::BlockIndexVector) = BlockIndexVector(a.block, copy.(a.indices))

# Copied from BlockArrays.BlockIndexRange.
function Base.show(io::IO, B::BlockIndexVector)
show(io, Block(B))
print(io, "[")
print_tuple_elements(io, B.indices)
print(io, "]")
end
Base.show(io::IO, ::MIME"text/plain", B::BlockIndexVector) = show(io, B)

function Base.getindex(b::AbstractBlockedUnitRange, Kkr::BlockIndexVector{1})
return b[block(Kkr)][Kkr.indices...]
end
Expand Down
6 changes: 6 additions & 0 deletions src/BlockArraysExtensions/blockrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ function Base.getindex(r::BlockUnitRange, I::Block{1})
return eachblockaxis(r)[Int(I)] .+ (first(r.r[I]) - 1)
end

using BlockArrays: BlockedOneTo
const BlockOneTo{T<:Integer,B,CS,R<:BlockedOneTo{T,CS}} = BlockUnitRange{T,B,CS,R}
Base.axes(S::Base.Slice{<:BlockOneTo}) = (S.indices,)
Base.axes1(S::Base.Slice{<:BlockOneTo}) = S.indices
Base.unsafe_indices(S::Base.Slice{<:BlockOneTo}) = (S.indices,)

function BlockArrays.combine_blockaxes(r1::BlockUnitRange, r2::BlockUnitRange)
if eachblockaxis(r1) ≠ eachblockaxis(r2)
return throw(ArgumentError("BlockUnitRanges must have the same block axes"))
Expand Down
25 changes: 25 additions & 0 deletions src/abstractblocksparsearray/views.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ function Base.view(
) where {T,N}
return viewblock(a, block...)
end
# Fix ambiguity error with BlockArrays.jl.
function Base.view(
a::SubArray{
T,
N,
<:AbstractBlockSparseArray{T,N},
<:Tuple{Vararg{Union{Base.Slice,BlockSlice{Union{},<:Integer}},N}},
},
block::Block{N},
) where {T,N}
return viewblock(a, block)
end

# XXX: TODO: Distinguish if a sub-view of the block needs to be taken!
# Define a new `SubBlockSlice` which is used in:
Expand Down Expand Up @@ -302,6 +314,14 @@ end

blockedslice_blocks(x::BlockSlice) = x.block
blockedslice_blocks(x::BlockIndices) = x.blocks
# Reinterpret the slice blockwise.
function blockedslice_blocks(x::Base.Slice)
return mortar(
map(BlockRange(x.indices)) do b
return BlockIndexRange(b, Base.Slice(Base.axes1(x.indices[b])))
end,
)
end

# TODO: Define `@interface interface(a) viewblock`.
function BlockArrays.viewblock(
Expand All @@ -319,6 +339,11 @@ function BlockArrays.viewblock(
end
return @view parent(a)[brs...]
end

## function BlockArrays.viewblock
## a::SubArray{
## T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{Union{Base.Slice, BlockArrays.BlockSlice{Union{}, T} where T<:Integer, BlockSparseArrays.BlockIndices{Union{}, T} where T<:Integer},N}}, ::Vararg{Block{1}, N}) where {T, N}

# TODO: Define `@interface interface(a) viewblock`.
function BlockArrays.viewblock(
a::SubArray{
Expand Down
Loading