diff --git a/Project.toml b/Project.toml index fd5a46e..deb5964 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BlockSparseArrays" uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] -version = "0.4.2" +version = "0.4.3" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/BlockArraysExtensions/BlockArraysExtensions.jl b/src/BlockArraysExtensions/BlockArraysExtensions.jl index 535bf52..81b5f47 100644 --- a/src/BlockArraysExtensions/BlockArraysExtensions.jl +++ b/src/BlockArraysExtensions/BlockArraysExtensions.jl @@ -263,17 +263,20 @@ function blocks_to_cartesianindices(d::Dictionary{<:Block}) return Dictionary(blocks_to_cartesianindices(eachindex(d)), d) end -function blockreshape(a::AbstractArray, dims::Tuple{Vararg{Vector{Int}}}) +function blockreshape(a::AbstractArray, dims::Tuple{Vector{Int},Vararg{Vector{Int}}}) return blockreshape(a, blockedrange.(dims)) end - -function blockreshape(a::AbstractArray, dims::Vararg{Vector{Int}}) - return blockreshape(a, dims) +function blockreshape(a::AbstractArray, dim1::Vector{Int}, dim_rest::Vararg{Vector{Int}}) + return blockreshape(a, (dim1, dim_rest...)) +end +# Fix ambiguity error. +function blockreshape(a::AbstractArray) + return blockreshape(a, ()) end tuple_oneto(n) = ntuple(identity, n) -function blockreshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}}) +function _blockreshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}}) reshaped_blocks_a = reshape(blocks(a), blocklength.(axes)) reshaped_a = similar(a, axes) for I in eachstoredindex(reshaped_blocks_a) @@ -284,8 +287,20 @@ function blockreshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}}) return reshaped_a end -function blockreshape(a::AbstractArray, axes::Vararg{AbstractUnitRange}) - return blockreshape(a, axes) +function blockreshape( + a::AbstractArray, axes::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}} +) + return _blockreshape(a, axes) +end +# Fix ambiguity error. +function blockreshape(a::AbstractArray, axes::Tuple{}) + return _blockreshape(a, axes) +end + +function blockreshape( + a::AbstractArray, axis1::AbstractUnitRange, axes_rest::Vararg{AbstractUnitRange} +) + return blockreshape(a, (axis1, axes_rest...)) end function cartesianindices(axes::Tuple, b::Block) diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index 0e577f1..0403b78 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -232,6 +232,18 @@ end ) return blocksparse_similar(a, elt, axes) end +# Fix ambiguity error when non-blocked ranges are passed. +@interface ::AbstractBlockSparseArrayInterface function Base.similar( + a::AbstractArray, elt::Type, axes::Tuple{Base.OneTo,Vararg{Base.OneTo}} +) + return blocksparse_similar(a, elt, axes) +end +# Fix ambiguity error when empty axes are passed. +@interface ::AbstractBlockSparseArrayInterface function Base.similar( + a::AbstractArray, elt::Type, axes::Tuple{} +) + return blocksparse_similar(a, elt, axes) +end @interface ::AbstractBlockSparseArrayInterface function Base.similar( a::Type{<:AbstractArray}, elt::Type, axes::Tuple{Vararg{Int}} ) diff --git a/test/test_basics.jl b/test/test_basics.jl index fda304f..ee87ddc 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -405,6 +405,13 @@ arrayts = (Array, JLArray) @test size(b) == size(a) @test blocksize(b) == blocksize(a) + # Regression test for https://github.com/ITensor/BlockSparseArrays.jl/issues/98 + a = dev(BlockSparseArray{elt}(undef)) + b = similar(a, Float64, (Base.OneTo(1),)) + @test b isa BlockSparseVector{Float64} + @test size(b) == (1,) + @test blocksize(b) == (1,) + a = dev(BlockSparseArray{elt}(undef, [2, 3], [3, 4])) b = @view a[[Block(2), Block(1)], [Block(2), Block(1)]] c = @view b[Block(1, 1)] @@ -1126,6 +1133,15 @@ arrayts = (Array, JLArray) @test reshape(a[Block(2, 1)], 8) == b[Block(2)] @test blockstoredlength(b) == 2 @test storedlength(b) == 17 + + # Zero-dimensional limit (check for ambiguity errors). + # Regression test for https://github.com/ITensor/BlockSparseArrays.jl/issues/98. + a = dev(BlockSparseArray{elt}(undef, ())) + a[Block()] = dev(randn(elt, ())) + b = blockreshape(a) + @test a[Block()] == b[Block()] + @test blockstoredlength(b) == 1 + @test storedlength(b) == 1 end @testset "show" begin vectort_elt = arrayt{elt,1} diff --git a/test/test_tensoralgebraext.jl b/test/test_tensoralgebraext.jl index f4c053e..7caaffd 100644 --- a/test/test_tensoralgebraext.jl +++ b/test/test_tensoralgebraext.jl @@ -59,14 +59,12 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) @test a_dest ≈ a_dest_dense # vector vector - @test_broken a_dest, dimnames_dest = contract(a3, (1, 2), a3, (2, 1)) - #= + a_dest, dimnames_dest = contract(a3, (1, 2), a3, (2, 1)) a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (2, 1)) @test dimnames_dest == dimnames_dest_dense @test size(a_dest) == size(a_dest_dense) @test a_dest isa BlockSparseArray{elt,0} @test a_dest ≈ a_dest_dense - =# # outer product a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (3, 4))