Skip to content

Commit f9b971b

Browse files
authored
Convenient constructors like blocksparse and blocksparsezeros (#141)
1 parent 621935b commit f9b971b

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BlockSparseArrays"
22
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.7.6"
4+
version = "0.7.7"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/blocksparsearray/blocksparsearray.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,27 @@ function BlockSparseArray{T}(
230230
return BlockSparseArray{T}(undef, axes)
231231
end
232232

233+
# Convenient constructors.
234+
function blocksparsezeros(elt::Type, axes...)
235+
return BlockSparseArray{elt}(undef, axes...)
236+
end
237+
function blocksparsezeros(::BlockType{A}, axes...) where {A<:AbstractArray}
238+
# TODO: Use:
239+
# ```julia
240+
# B = similartype(A, Type{eltype(A)}, Tuple{blockaxistype.(axes)...})
241+
# BlockSparseArray{eltype(A),length(axes),B}(undef, axes...)
242+
# ```
243+
# to make a bit more generic.
244+
return BlockSparseArray{eltype(A),ndims(A),A}(undef, axes...)
245+
end
246+
function blocksparse(d::Dict{<:Block,<:AbstractArray}, axes...)
247+
a = blocksparsezeros(BlockType(valtype(d)), axes...)
248+
for I in eachindex(d)
249+
a[I] = d[I]
250+
end
251+
return a
252+
end
253+
233254
# Base `AbstractArray` interface
234255
Base.axes(a::BlockSparseArray) = a.axes
235256

test/test_basics.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ using BlockSparseArrays:
2121
BlockSparseArray,
2222
BlockSparseMatrix,
2323
BlockSparseVector,
24+
BlockType,
2425
BlockView,
2526
blockdiagindices,
2627
blockreshape,
28+
blocksparse,
29+
blocksparsezeros,
2730
blockstoredlength,
2831
blockstype,
2932
blocktype,
@@ -131,6 +134,37 @@ arrayts = (Array, JLArray)
131134
)
132135
@test_throws ArgumentError BlockSparseVector{elt}(undef, dims...)
133136
end
137+
138+
# Convenient constructors.
139+
a = blocksparsezeros(elt, [2, 3], [2, 3])
140+
@test iszero(a)
141+
@test iszero(blockstoredlength(a))
142+
@test a isa BlockSparseMatrix{elt,Matrix{elt}}
143+
@test blocktype(a) == Matrix{elt}
144+
@test blockstype(a) <: SparseMatrixDOK{Matrix{elt}}
145+
@test blocksize(a) == (2, 2)
146+
@test blocksizes(a) == [(2, 2) (2, 3); (3, 2) (3, 3)]
147+
148+
a = blocksparsezeros(BlockType(arrayt{elt,2}), [2, 3], [2, 3])
149+
@test iszero(a)
150+
@test iszero(blockstoredlength(a))
151+
@test a isa BlockSparseMatrix{elt,arrayt{elt,2}}
152+
@test blocktype(a) == arrayt{elt,2}
153+
@test blockstype(a) <: SparseMatrixDOK{arrayt{elt,2}}
154+
@test blocksize(a) == (2, 2)
155+
@test blocksizes(a) == [(2, 2) (2, 3); (3, 2) (3, 3)]
156+
157+
d = Dict(Block(1, 1) => dev(randn(elt, 2, 2)), Block(2, 2) => dev(randn(elt, 3, 3)))
158+
a = blocksparse(d, [2, 3], [2, 3])
159+
@test !iszero(a)
160+
@test a[Block(1, 1)] == d[Block(1, 1)]
161+
@test a[Block(2, 2)] == d[Block(2, 2)]
162+
@test blockstoredlength(a) == 2
163+
@test a isa BlockSparseMatrix{elt,arrayt{elt,2}}
164+
@test blocktype(a) == arrayt{elt,2}
165+
@test blockstype(a) <: SparseMatrixDOK{arrayt{elt,2}}
166+
@test blocksize(a) == (2, 2)
167+
@test blocksizes(a) == [(2, 2) (2, 3); (3, 2) (3, 3)]
134168
end
135169
@testset "blockstype, blocktype" begin
136170
a = arrayt(randn(elt, 2, 2))

0 commit comments

Comments
 (0)