Skip to content

Commit 56f7aea

Browse files
authored
Fix vector show (#79)
1 parent 10f6ebf commit 56f7aea

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
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.3.4"
4+
version = "0.3.6"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -43,7 +43,7 @@ LabelledNumbers = "0.1.0"
4343
LinearAlgebra = "1.10"
4444
MacroTools = "0.5.13"
4545
MapBroadcast = "0.1.5"
46-
SparseArraysBase = "0.4"
46+
SparseArraysBase = "0.5"
4747
SplitApplyCombine = "1.2.3"
4848
TensorAlgebra = "0.1.0, 0.2"
4949
Test = "1.10"

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const AnyAbstractBlockSparseArray{T,N} = Union{
2323
<:AbstractBlockSparseArray{T,N},<:WrappedAbstractBlockSparseArray{T,N}
2424
}
2525

26+
const AnyAbstractBlockSparseVector{T} = AnyAbstractBlockSparseArray{T,1}
27+
const AnyAbstractBlockSparseMatrix{T} = AnyAbstractBlockSparseArray{T,2}
28+
const AnyAbstractBlockSparseVecOrMat{T,N} = Union{
29+
AnyAbstractBlockSparseVector{T},AnyAbstractBlockSparseMatrix{T}
30+
}
31+
2632
function DerivableInterfaces.interface(::Type{<:AnyAbstractBlockSparseArray})
2733
return BlockSparseArrayInterface()
2834
end
@@ -338,26 +344,15 @@ function Base.Array(a::AnyAbstractBlockSparseArray)
338344
end
339345

340346
function SparseArraysBase.isstored(
341-
a::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}
342-
) where {N}
343-
bI = BlockIndex(findblockindex.(axes(a), I))
344-
blocks_a = blocks(a)
345-
return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...)
346-
end
347-
348-
# This circumvents issues passing certain kinds of SubArrays
349-
# to the more generic block sparse `isstored` definition,
350-
# for example `blocks(a)` is broken for certain slices.
351-
function SparseArraysBase.isstored(
352-
a::SubArray{<:Any,N,<:AbstractBlockSparseArray}, I::Vararg{Int,N}
347+
a::AbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}
353348
) where {N}
354-
return @interface DefaultArrayInterface() isstored(a, I...)
349+
return @interface interface(a) isstored(a, I...)
355350
end
356351

357352
function Base.replace_in_print_matrix(
358-
A::AnyAbstractBlockSparseArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString
353+
a::AnyAbstractBlockSparseVecOrMat, i::Integer, j::Integer, s::AbstractString
359354
)
360-
return isstored(A, i, j) ? s : Base.replace_with_centered_mark(s)
355+
return isstored(a, i, j) ? s : Base.replace_with_centered_mark(s)
361356
end
362357

363358
# attempt to catch things that wrap GPU arrays

src/blocksparsearray/blocksparsearray.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ function BlockSparseArray{T,N}(
174174
return BlockSparseArray{T,N,Array{T,N}}(undef, axes)
175175
end
176176

177+
function BlockSparseArray{T,N}(
178+
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}}
179+
) where {T,N}
180+
return throw(ArgumentError("Length of axes doesn't match number of dimensions."))
181+
end
182+
177183
function BlockSparseArray{T,N}(
178184
::UndefInitializer, axes::Vararg{AbstractUnitRange{<:Integer},N}
179185
) where {T,N}

src/blocksparsearrayinterface/blocksparsearrayinterface.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ struct BlockSparseArrayInterface <: AbstractBlockSparseArrayInterface end
100100
@interface ::AbstractBlockSparseArrayInterface BlockArrays.blocks(a::AbstractArray) =
101101
error("Not implemented")
102102

103+
@interface ::AbstractBlockSparseArrayInterface function SparseArraysBase.isstored(
104+
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
105+
) where {N}
106+
bI = BlockIndex(findblockindex.(axes(a), I))
107+
return isstored(blocks(a), bI.I...) && isstored(blocks(a)[bI.I...], bI.α...)
108+
end
109+
103110
@interface ::AbstractBlockSparseArrayInterface function Base.getindex(
104111
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
105112
) where {N}

test/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ LinearAlgebra = "1"
3636
Pkg = "1"
3737
Random = "1"
3838
SafeTestsets = "0.1"
39-
SparseArraysBase = "0.4"
39+
SparseArraysBase = "0.5"
4040
Suppressor = "0.2"
4141
SymmetrySectors = "0.1"
4242
TensorAlgebra = "0.2"

test/test_basics.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ arrayts = (Array, JLArray)
130130
@test iszero(storedlength(a))
131131
end
132132
end
133+
134+
for dims in (
135+
([2, 2], [2, 2]),
136+
(([2, 2], [2, 2]),),
137+
blockedrange.(([2, 2], [2, 2])),
138+
(blockedrange.(([2, 2], [2, 2])),),
139+
)
140+
@test_throws ArgumentError BlockSparseVector{elt}(undef, dims...)
141+
end
133142
end
134143
@testset "blockstype, blocktype" begin
135144
a = arrayt(randn(elt, 2, 2))
@@ -1170,6 +1179,15 @@ arrayts = (Array, JLArray)
11701179
# Not testing other element types since they change the
11711180
# spacing so it isn't easy to make the test general.
11721181

1182+
a′ = BlockSparseVector{elt,arrayt{elt,1}}(undef, [2, 2])
1183+
@allowscalar a′[1] = 1
1184+
a = a′
1185+
@test sprint(show, "text/plain", a) ==
1186+
"$(summary(a)):\n $(eltype(a)(1))\n $(zero(eltype(a)))\n ───\n\n"
1187+
a = @view a′[:]
1188+
@test sprint(show, "text/plain", a) ==
1189+
"$(summary(a)):\n $(eltype(a)(1))\n $(zero(eltype(a)))\n\n"
1190+
11731191
a′ = BlockSparseMatrix{elt,arrayt{elt,2}}(undef, [2, 2], [2, 2])
11741192
@allowscalar a′[1, 2] = 12
11751193
for a in (a′, @view(a′[:, :]))

0 commit comments

Comments
 (0)