Skip to content

Fix vector show #79

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 8 commits into from
Mar 10, 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
4 changes: 2 additions & 2 deletions 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.3.4"
version = "0.3.6"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down Expand Up @@ -43,7 +43,7 @@ LabelledNumbers = "0.1.0"
LinearAlgebra = "1.10"
MacroTools = "0.5.13"
MapBroadcast = "0.1.5"
SparseArraysBase = "0.4"
SparseArraysBase = "0.5"
SplitApplyCombine = "1.2.3"
TensorAlgebra = "0.1.0, 0.2"
Test = "1.10"
Expand Down
25 changes: 10 additions & 15 deletions src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<:AbstractBlockSparseArray{T,N},<:WrappedAbstractBlockSparseArray{T,N}
}

const AnyAbstractBlockSparseVector{T} = AnyAbstractBlockSparseArray{T,1}
const AnyAbstractBlockSparseMatrix{T} = AnyAbstractBlockSparseArray{T,2}
const AnyAbstractBlockSparseVecOrMat{T,N} = Union{
AnyAbstractBlockSparseVector{T},AnyAbstractBlockSparseMatrix{T}
}

function DerivableInterfaces.interface(::Type{<:AnyAbstractBlockSparseArray})
return BlockSparseArrayInterface()
end
Expand Down Expand Up @@ -338,26 +344,15 @@
end

function SparseArraysBase.isstored(
a::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}
) where {N}
bI = BlockIndex(findblockindex.(axes(a), I))
blocks_a = blocks(a)
return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...)
end

# This circumvents issues passing certain kinds of SubArrays
# to the more generic block sparse `isstored` definition,
# for example `blocks(a)` is broken for certain slices.
function SparseArraysBase.isstored(
a::SubArray{<:Any,N,<:AbstractBlockSparseArray}, I::Vararg{Int,N}
a::AbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}
) where {N}
return @interface DefaultArrayInterface() isstored(a, I...)
return @interface interface(a) isstored(a, I...)

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

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L349

Added line #L349 was not covered by tests
end

function Base.replace_in_print_matrix(
A::AnyAbstractBlockSparseArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString
a::AnyAbstractBlockSparseVecOrMat, i::Integer, j::Integer, s::AbstractString
)
return isstored(A, i, j) ? s : Base.replace_with_centered_mark(s)
return isstored(a, i, j) ? s : Base.replace_with_centered_mark(s)

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

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L355

Added line #L355 was not covered by tests
end

# attempt to catch things that wrap GPU arrays
Expand Down
6 changes: 6 additions & 0 deletions src/blocksparsearray/blocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@
return BlockSparseArray{T,N,Array{T,N}}(undef, axes)
end

function BlockSparseArray{T,N}(

Check warning on line 177 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L177

Added line #L177 was not covered by tests
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}}
) where {T,N}
return throw(ArgumentError("Length of axes doesn't match number of dimensions."))

Check warning on line 180 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L180

Added line #L180 was not covered by tests
end

function BlockSparseArray{T,N}(
::UndefInitializer, axes::Vararg{AbstractUnitRange{<:Integer},N}
) where {T,N}
Expand Down
7 changes: 7 additions & 0 deletions src/blocksparsearrayinterface/blocksparsearrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
@interface ::AbstractBlockSparseArrayInterface BlockArrays.blocks(a::AbstractArray) =
error("Not implemented")

@interface ::AbstractBlockSparseArrayInterface function SparseArraysBase.isstored(

Check warning on line 103 in src/blocksparsearrayinterface/blocksparsearrayinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearrayinterface/blocksparsearrayinterface.jl#L103

Added line #L103 was not covered by tests
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
) where {N}
bI = BlockIndex(findblockindex.(axes(a), I))
return isstored(blocks(a), bI.I...) && isstored(blocks(a)[bI.I...], bI.α...)

Check warning on line 107 in src/blocksparsearrayinterface/blocksparsearrayinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearrayinterface/blocksparsearrayinterface.jl#L106-L107

Added lines #L106 - L107 were not covered by tests
end

@interface ::AbstractBlockSparseArrayInterface function Base.getindex(
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
) where {N}
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ LinearAlgebra = "1"
Pkg = "1"
Random = "1"
SafeTestsets = "0.1"
SparseArraysBase = "0.4"
SparseArraysBase = "0.5"
Suppressor = "0.2"
SymmetrySectors = "0.1"
TensorAlgebra = "0.2"
Expand Down
18 changes: 18 additions & 0 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ arrayts = (Array, JLArray)
@test iszero(storedlength(a))
end
end

for dims in (
([2, 2], [2, 2]),
(([2, 2], [2, 2]),),
blockedrange.(([2, 2], [2, 2])),
(blockedrange.(([2, 2], [2, 2])),),
)
@test_throws ArgumentError BlockSparseVector{elt}(undef, dims...)
end
end
@testset "blockstype, blocktype" begin
a = arrayt(randn(elt, 2, 2))
Expand Down Expand Up @@ -1170,6 +1179,15 @@ arrayts = (Array, JLArray)
# Not testing other element types since they change the
# spacing so it isn't easy to make the test general.

a′ = BlockSparseVector{elt,arrayt{elt,1}}(undef, [2, 2])
@allowscalar a′[1] = 1
a = a′
@test sprint(show, "text/plain", a) ==
"$(summary(a)):\n $(eltype(a)(1))\n $(zero(eltype(a)))\n ───\n ⋅ \n ⋅ "
a = @view a′[:]
@test sprint(show, "text/plain", a) ==
"$(summary(a)):\n $(eltype(a)(1))\n $(zero(eltype(a)))\n ⋅ \n ⋅ "

a′ = BlockSparseMatrix{elt,arrayt{elt,2}}(undef, [2, 2], [2, 2])
@allowscalar a′[1, 2] = 12
for a in (a′, @view(a′[:, :]))
Expand Down
Loading