Skip to content

Commit 6b9dacd

Browse files
authored
Fix ambiguities in similar and blockreshape (#99)
1 parent 77efe55 commit 6b9dacd

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
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.4.2"
4+
version = "0.4.3"
55

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

src/BlockArraysExtensions/BlockArraysExtensions.jl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,20 @@ function blocks_to_cartesianindices(d::Dictionary{<:Block})
263263
return Dictionary(blocks_to_cartesianindices(eachindex(d)), d)
264264
end
265265

266-
function blockreshape(a::AbstractArray, dims::Tuple{Vararg{Vector{Int}}})
266+
function blockreshape(a::AbstractArray, dims::Tuple{Vector{Int},Vararg{Vector{Int}}})
267267
return blockreshape(a, blockedrange.(dims))
268268
end
269-
270-
function blockreshape(a::AbstractArray, dims::Vararg{Vector{Int}})
271-
return blockreshape(a, dims)
269+
function blockreshape(a::AbstractArray, dim1::Vector{Int}, dim_rest::Vararg{Vector{Int}})
270+
return blockreshape(a, (dim1, dim_rest...))
271+
end
272+
# Fix ambiguity error.
273+
function blockreshape(a::AbstractArray)
274+
return blockreshape(a, ())
272275
end
273276

274277
tuple_oneto(n) = ntuple(identity, n)
275278

276-
function blockreshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}})
279+
function _blockreshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}})
277280
reshaped_blocks_a = reshape(blocks(a), blocklength.(axes))
278281
reshaped_a = similar(a, axes)
279282
for I in eachstoredindex(reshaped_blocks_a)
@@ -284,8 +287,20 @@ function blockreshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}})
284287
return reshaped_a
285288
end
286289

287-
function blockreshape(a::AbstractArray, axes::Vararg{AbstractUnitRange})
288-
return blockreshape(a, axes)
290+
function blockreshape(
291+
a::AbstractArray, axes::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}
292+
)
293+
return _blockreshape(a, axes)
294+
end
295+
# Fix ambiguity error.
296+
function blockreshape(a::AbstractArray, axes::Tuple{})
297+
return _blockreshape(a, axes)
298+
end
299+
300+
function blockreshape(
301+
a::AbstractArray, axis1::AbstractUnitRange, axes_rest::Vararg{AbstractUnitRange}
302+
)
303+
return blockreshape(a, (axis1, axes_rest...))
289304
end
290305

291306
function cartesianindices(axes::Tuple, b::Block)

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ end
232232
)
233233
return blocksparse_similar(a, elt, axes)
234234
end
235+
# Fix ambiguity error when non-blocked ranges are passed.
236+
@interface ::AbstractBlockSparseArrayInterface function Base.similar(
237+
a::AbstractArray, elt::Type, axes::Tuple{Base.OneTo,Vararg{Base.OneTo}}
238+
)
239+
return blocksparse_similar(a, elt, axes)
240+
end
241+
# Fix ambiguity error when empty axes are passed.
242+
@interface ::AbstractBlockSparseArrayInterface function Base.similar(
243+
a::AbstractArray, elt::Type, axes::Tuple{}
244+
)
245+
return blocksparse_similar(a, elt, axes)
246+
end
235247
@interface ::AbstractBlockSparseArrayInterface function Base.similar(
236248
a::Type{<:AbstractArray}, elt::Type, axes::Tuple{Vararg{Int}}
237249
)

test/test_basics.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ arrayts = (Array, JLArray)
405405
@test size(b) == size(a)
406406
@test blocksize(b) == blocksize(a)
407407

408+
# Regression test for https://github.com/ITensor/BlockSparseArrays.jl/issues/98
409+
a = dev(BlockSparseArray{elt}(undef))
410+
b = similar(a, Float64, (Base.OneTo(1),))
411+
@test b isa BlockSparseVector{Float64}
412+
@test size(b) == (1,)
413+
@test blocksize(b) == (1,)
414+
408415
a = dev(BlockSparseArray{elt}(undef, [2, 3], [3, 4]))
409416
b = @view a[[Block(2), Block(1)], [Block(2), Block(1)]]
410417
c = @view b[Block(1, 1)]
@@ -1126,6 +1133,15 @@ arrayts = (Array, JLArray)
11261133
@test reshape(a[Block(2, 1)], 8) == b[Block(2)]
11271134
@test blockstoredlength(b) == 2
11281135
@test storedlength(b) == 17
1136+
1137+
# Zero-dimensional limit (check for ambiguity errors).
1138+
# Regression test for https://github.com/ITensor/BlockSparseArrays.jl/issues/98.
1139+
a = dev(BlockSparseArray{elt}(undef, ()))
1140+
a[Block()] = dev(randn(elt, ()))
1141+
b = blockreshape(a)
1142+
@test a[Block()] == b[Block()]
1143+
@test blockstoredlength(b) == 1
1144+
@test storedlength(b) == 1
11291145
end
11301146
@testset "show" begin
11311147
vectort_elt = arrayt{elt,1}

test/test_tensoralgebraext.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,12 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
5959
@test a_dest a_dest_dense
6060

6161
# vector vector
62-
@test_broken a_dest, dimnames_dest = contract(a3, (1, 2), a3, (2, 1))
63-
#=
62+
a_dest, dimnames_dest = contract(a3, (1, 2), a3, (2, 1))
6463
a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (2, 1))
6564
@test dimnames_dest == dimnames_dest_dense
6665
@test size(a_dest) == size(a_dest_dense)
6766
@test a_dest isa BlockSparseArray{elt,0}
6867
@test a_dest a_dest_dense
69-
=#
7068

7169
# outer product
7270
a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (3, 4))

0 commit comments

Comments
 (0)