Skip to content

Fix zero-dim view! #149

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
Jun 16, 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.13"
version = "0.7.14"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
5 changes: 5 additions & 0 deletions src/BlockArraysExtensions/BlockArraysExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ function view!(a::AbstractArray{<:Any,N}, index::Vararg{Block{1},N}) where {N}
blocks(a)[Int.(index)...] = blocks(a)[Int.(index)...]
return blocks(a)[Int.(index)...]
end
# Fix ambiguity error.
function view!(a::AbstractArray{<:Any,0})
blocks(a)[] = blocks(a)[]
return blocks(a)[]
end

function view!(a::AbstractArray{<:Any,N}, index::BlockIndexRange{N}) where {N}
# TODO: Is there a better code pattern for this?
Expand Down
21 changes: 20 additions & 1 deletion test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ using BlockSparseArrays:
using GPUArraysCore: @allowscalar
using JLArrays: JLArray, JLMatrix
using LinearAlgebra: Adjoint, Transpose, dot, norm, tr
using SparseArraysBase: SparseArrayDOK, SparseMatrixDOK, SparseVectorDOK, storedlength
using SparseArraysBase:
SparseArrayDOK, SparseMatrixDOK, SparseVectorDOK, isstored, storedlength
using Test: @test, @test_broken, @test_throws, @testset, @inferred
using TestExtras: @constinferred
using TypeParameterAccessors: TypeParameterAccessors, Position
Expand Down Expand Up @@ -1159,6 +1160,24 @@ arrayts = (Array, JLArray)
@test view!(a, blk...) == x
@test @view!(a[blk...]) == x
end
# 0-dim case
# Regression test for https://github.com/ITensor/BlockSparseArrays.jl/issues/148
for I in ((), (Block(),))
a = dev(BlockSparseArray{elt}(undef))
@test !isstored(a)
@test iszero(blockstoredlength(a))
@test isempty(eachblockstoredindex(a))
@test iszero(a)
b = @view! a[I...]
@test isstored(a)
@test isone(blockstoredlength(a))
@test issetequal(eachblockstoredindex(a), [Block()])
@test iszero(adapt(Array)(a))
@test b isa arrayt{elt,0}
@test size(b) == ()
# Converting to `Array` works around a bug in `iszero(JLArray{Float64}(undef))`.
@test iszero(adapt(Array)(b))
end
end
@testset "LinearAlgebra" begin
a1 = dev(BlockSparseArray{elt}(undef, [2, 3], [2, 3]))
Expand Down
Loading