Skip to content

Convenient constructors like blocksparse and blocksparsezeros #141

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

Merged
merged 1 commit into from
Jun 12, 2025
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,7 +1,7 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.7.6"
version = "0.7.7"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
21 changes: 21 additions & 0 deletions src/blocksparsearray/blocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ function BlockSparseArray{T}(
return BlockSparseArray{T}(undef, axes)
end

# Convenient constructors.
function blocksparsezeros(elt::Type, axes...)
return BlockSparseArray{elt}(undef, axes...)
end
function blocksparsezeros(::BlockType{A}, axes...) where {A<:AbstractArray}
# TODO: Use:
# ```julia
# B = similartype(A, Type{eltype(A)}, Tuple{blockaxistype.(axes)...})
# BlockSparseArray{eltype(A),length(axes),B}(undef, axes...)
# ```
# to make a bit more generic.
return BlockSparseArray{eltype(A),ndims(A),A}(undef, axes...)
end
function blocksparse(d::Dict{<:Block,<:AbstractArray}, axes...)
a = blocksparsezeros(BlockType(valtype(d)), axes...)
for I in eachindex(d)
a[I] = d[I]
end
return a
end

# Base `AbstractArray` interface
Base.axes(a::BlockSparseArray) = a.axes

Expand Down
34 changes: 34 additions & 0 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ using BlockSparseArrays:
BlockSparseArray,
BlockSparseMatrix,
BlockSparseVector,
BlockType,
BlockView,
blockdiagindices,
blockreshape,
blocksparse,
blocksparsezeros,
blockstoredlength,
blockstype,
blocktype,
Expand Down Expand Up @@ -131,6 +134,37 @@ arrayts = (Array, JLArray)
)
@test_throws ArgumentError BlockSparseVector{elt}(undef, dims...)
end

# Convenient constructors.
a = blocksparsezeros(elt, [2, 3], [2, 3])
@test iszero(a)
@test iszero(blockstoredlength(a))
@test a isa BlockSparseMatrix{elt,Matrix{elt}}
@test blocktype(a) == Matrix{elt}
@test blockstype(a) <: SparseMatrixDOK{Matrix{elt}}
@test blocksize(a) == (2, 2)
@test blocksizes(a) == [(2, 2) (2, 3); (3, 2) (3, 3)]

a = blocksparsezeros(BlockType(arrayt{elt,2}), [2, 3], [2, 3])
@test iszero(a)
@test iszero(blockstoredlength(a))
@test a isa BlockSparseMatrix{elt,arrayt{elt,2}}
@test blocktype(a) == arrayt{elt,2}
@test blockstype(a) <: SparseMatrixDOK{arrayt{elt,2}}
@test blocksize(a) == (2, 2)
@test blocksizes(a) == [(2, 2) (2, 3); (3, 2) (3, 3)]

d = Dict(Block(1, 1) => dev(randn(elt, 2, 2)), Block(2, 2) => dev(randn(elt, 3, 3)))
a = blocksparse(d, [2, 3], [2, 3])
@test !iszero(a)
@test a[Block(1, 1)] == d[Block(1, 1)]
@test a[Block(2, 2)] == d[Block(2, 2)]
@test blockstoredlength(a) == 2
@test a isa BlockSparseMatrix{elt,arrayt{elt,2}}
@test blocktype(a) == arrayt{elt,2}
@test blockstype(a) <: SparseMatrixDOK{arrayt{elt,2}}
@test blocksize(a) == (2, 2)
@test blocksizes(a) == [(2, 2) (2, 3); (3, 2) (3, 3)]
end
@testset "blockstype, blocktype" begin
a = arrayt(randn(elt, 2, 2))
Expand Down
Loading