Skip to content

Fix ambiguities in similar and blockreshape #99

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 4 commits into from
Apr 9, 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.4.2"
version = "0.4.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
29 changes: 22 additions & 7 deletions src/BlockArraysExtensions/BlockArraysExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,20 @@
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}}})

Check warning on line 266 in src/BlockArraysExtensions/BlockArraysExtensions.jl

View check run for this annotation

Codecov / codecov/patch

src/BlockArraysExtensions/BlockArraysExtensions.jl#L266

Added line #L266 was not covered by tests
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...))

Check warning on line 270 in src/BlockArraysExtensions/BlockArraysExtensions.jl

View check run for this annotation

Codecov / codecov/patch

src/BlockArraysExtensions/BlockArraysExtensions.jl#L269-L270

Added lines #L269 - L270 were not covered by tests
end
# Fix ambiguity error.
function blockreshape(a::AbstractArray)
return blockreshape(a, ())

Check warning on line 274 in src/BlockArraysExtensions/BlockArraysExtensions.jl

View check run for this annotation

Codecov / codecov/patch

src/BlockArraysExtensions/BlockArraysExtensions.jl#L273-L274

Added lines #L273 - L274 were not covered by tests
end

tuple_oneto(n) = ntuple(identity, n)

function blockreshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}})
function _blockreshape(a::AbstractArray, axes::Tuple{Vararg{AbstractUnitRange}})

Check warning on line 279 in src/BlockArraysExtensions/BlockArraysExtensions.jl

View check run for this annotation

Codecov / codecov/patch

src/BlockArraysExtensions/BlockArraysExtensions.jl#L279

Added line #L279 was not covered by tests
reshaped_blocks_a = reshape(blocks(a), blocklength.(axes))
reshaped_a = similar(a, axes)
for I in eachstoredindex(reshaped_blocks_a)
Expand All @@ -284,8 +287,20 @@
return reshaped_a
end

function blockreshape(a::AbstractArray, axes::Vararg{AbstractUnitRange})
return blockreshape(a, axes)
function blockreshape(

Check warning on line 290 in src/BlockArraysExtensions/BlockArraysExtensions.jl

View check run for this annotation

Codecov / codecov/patch

src/BlockArraysExtensions/BlockArraysExtensions.jl#L290

Added line #L290 was not covered by tests
a::AbstractArray, axes::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}
)
return _blockreshape(a, axes)

Check warning on line 293 in src/BlockArraysExtensions/BlockArraysExtensions.jl

View check run for this annotation

Codecov / codecov/patch

src/BlockArraysExtensions/BlockArraysExtensions.jl#L293

Added line #L293 was not covered by tests
end
# Fix ambiguity error.
function blockreshape(a::AbstractArray, axes::Tuple{})
return _blockreshape(a, axes)

Check warning on line 297 in src/BlockArraysExtensions/BlockArraysExtensions.jl

View check run for this annotation

Codecov / codecov/patch

src/BlockArraysExtensions/BlockArraysExtensions.jl#L296-L297

Added lines #L296 - L297 were not covered by tests
end

function blockreshape(

Check warning on line 300 in src/BlockArraysExtensions/BlockArraysExtensions.jl

View check run for this annotation

Codecov / codecov/patch

src/BlockArraysExtensions/BlockArraysExtensions.jl#L300

Added line #L300 was not covered by tests
a::AbstractArray, axis1::AbstractUnitRange, axes_rest::Vararg{AbstractUnitRange}
)
return blockreshape(a, (axis1, axes_rest...))

Check warning on line 303 in src/BlockArraysExtensions/BlockArraysExtensions.jl

View check run for this annotation

Codecov / codecov/patch

src/BlockArraysExtensions/BlockArraysExtensions.jl#L303

Added line #L303 was not covered by tests
end

function cartesianindices(axes::Tuple, b::Block)
Expand Down
12 changes: 12 additions & 0 deletions src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@
)
return blocksparse_similar(a, elt, axes)
end
# Fix ambiguity error when non-blocked ranges are passed.
@interface ::AbstractBlockSparseArrayInterface function Base.similar(

Check warning on line 236 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L236

Added line #L236 was not covered by tests
a::AbstractArray, elt::Type, axes::Tuple{Base.OneTo,Vararg{Base.OneTo}}
)
return blocksparse_similar(a, elt, axes)

Check warning on line 239 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L239

Added line #L239 was not covered by tests
end
# Fix ambiguity error when empty axes are passed.
@interface ::AbstractBlockSparseArrayInterface function Base.similar(

Check warning on line 242 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L242

Added line #L242 was not covered by tests
a::AbstractArray, elt::Type, axes::Tuple{}
)
return blocksparse_similar(a, elt, axes)

Check warning on line 245 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L245

Added line #L245 was not covered by tests
end
@interface ::AbstractBlockSparseArrayInterface function Base.similar(
a::Type{<:AbstractArray}, elt::Type, axes::Tuple{Vararg{Int}}
)
Expand Down
16 changes: 16 additions & 0 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 1 addition & 3 deletions test/test_tensoralgebraext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading