Skip to content

Fix generic show for block sparse arrays #65

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 3 commits into from
Mar 2, 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.2.22"
version = "0.2.23"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
53 changes: 39 additions & 14 deletions src/abstractblocksparsearray/abstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,52 @@
return nothing
end

# Copied from `BlockArrays.jl`.
block2string(b, s) = string(join(map(string, b), '×'), "-blocked ", Base.dims2string(s))
# Copy of `Base.dims2string` defined in `show.jl`.
function dims_to_string(d)
isempty(d) && return "0-dimensional"
length(d) == 1 && return "$(d[1])-element"
return join(map(string, d), '×')
end

function summary_blocksparse(io::IO, a::AbstractArray)
print(io, block2string(blocksize(a), size(a)))
print(io, ' ')
show_typeof_blocksparse(io, a)
return nothing
# Copy of `BlockArrays.block2string` from `BlockArrays.jl`.
block_to_string(b, s) = string(join(map(string, b), '×'), "-blocked ", dims_to_string(s))

using TypeParameterAccessors: type_parameters, unspecify_type_parameters
function concretetype_to_string_truncated(type::Type; param_truncation_length=typemax(Int))
isconcretetype(type) || throw(ArgumentError("Type must be concrete."))
alias = Base.make_typealias(type)
base_type, params = if isnothing(alias)
unspecify_type_parameters(type), type_parameters(type)
else
base_type_globalref, params_svec = alias
base_type_globalref.name, params_svec
end
str = string(base_type)
if isempty(params)
return str

Check warning on line 117 in src/abstractblocksparsearray/abstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/abstractblocksparsearray.jl#L117

Added line #L117 was not covered by tests
end
str *= '{'
param_strings = map(params) do param
param_string = string(param)
if length(param_string) > param_truncation_length
return "…"
end
return param_string
end
str *= join(param_strings, ", ")
str *= '}'
return str
end

function Base.summary(io::IO, a::AbstractBlockSparseArray)
summary_blocksparse(io, a)
print(io, block_to_string(blocksize(a), size(a)))
print(io, ' ')
print(io, concretetype_to_string_truncated(typeof(a); param_truncation_length=40))
return nothing
end

function Base.showarg(io::IO, a::AbstractBlockSparseArray, toplevel::Bool)
if toplevel
show_typeof_blocksparse(io, a)
else
print(io, "::")
show_typeof_blocksparse(io, a)
end
!toplevel && print(io, "::")
print(io, concretetype_to_string_truncated(typeof(a); param_truncation_length=40))

Check warning on line 141 in src/abstractblocksparsearray/abstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/abstractblocksparsearray.jl#L140-L141

Added lines #L140 - L141 were not covered by tests
return nothing
end
28 changes: 0 additions & 28 deletions src/blocksparsearray/blocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,31 +208,3 @@ TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blocktype)) =
function TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blockstype))
return Position(4)
end

# TODO: Make this generic to `AbstractBlockSparseVector` using
# TypeParameterAccessors.jl, for example using:
# `set_ndims(unspecify_type_parameters(typeof(a)), 1)`.
function show_typeof_blocksparse(io::IO, a::BlockSparseVector)
print(io, "BlockSparseVector")
print(io, '{')
show(io, eltype(a))
print(io, ", ")
show(io, blocktype(a))
print(io, ", …")
print(io, '}')
return nothing
end

# TODO: Make this generic to `AbstractBlockSparseMatrix` using
# TypeParameterAccessors.jl, for example using:
# `set_ndims(unspecify_type_parameters(typeof(a)), 2)`.
function show_typeof_blocksparse(io::IO, a::BlockSparseMatrix)
print(io, "BlockSparseMatrix")
print(io, '{')
show(io, eltype(a))
print(io, ", ")
show(io, blocktype(a))
print(io, ", …")
print(io, '}')
return nothing
end
40 changes: 18 additions & 22 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1108,34 +1108,30 @@ arrayts = (Array, JLArray)
arrayt_elt = arrayt{elt,3}

a = BlockSparseVector{elt,arrayt{elt,1}}([2, 2])
res = sprint(summary, a)
function ref_vec(elt, arrayt, prefix="")
return "2-blocked 4-element $(prefix)BlockSparseVector{$(elt), $(arrayt), …, …}"
end
# Either option is possible depending on namespacing.
@test (
sprint(summary, a) ==
"2-blocked 4-element BlockSparseVector{$(elt), $(vectort_elt), …}"
) || (
sprint(summary, a) ==
"2-blocked 4-element BlockSparseArrays.BlockSparseVector{$(elt), $(vectort_elt), …}"
)
@test (res == ref_vec(elt, vectort_elt)) ||
(res == ref_vec(elt, vectort_elt, "BlockSparseArrays."))

a = BlockSparseMatrix{elt,arrayt{elt,2}}([2, 2], [2, 2])
res = sprint(summary, a)
function ref_mat(elt, arrayt, prefix="")
return "2×2-blocked 4×4 $(prefix)BlockSparseMatrix{$(elt), $(arrayt), …, …}"
end
# Either option is possible depending on namespacing.
@test (
sprint(summary, a) == "2×2-blocked 4×4 BlockSparseMatrix{$(elt), $(matrixt_elt), …}"
) || (
sprint(summary, a) ==
"2×2-blocked 4×4 BlockSparseArrays.BlockSparseMatrix{$(elt), $(matrixt_elt), …}"
)
@test (res == ref_mat(elt, matrixt_elt)) ||
(res == ref_mat(elt, matrixt_elt, "BlockSparseArrays."))

a = BlockSparseArray{elt,3,arrayt{elt,3}}([2, 2], [2, 2], [2, 2])

# Either option is possible depending on namespacing.
@test (
sprint(summary, a) ==
"2×2×2-blocked 4×4×4 BlockSparseArray{$(elt), 3, $(arrayt_elt), …}"
) || (
sprint(summary, a) ==
"2×2×2-blocked 4×4×4 BlockSparseArrays.BlockSparseArray{$(elt), 3, $(arrayt_elt), …}"
)
res = sprint(summary, a)
function ref_arr(elt, arrayt, prefix="")
return "2×2×2-blocked 4×4×4 $(prefix)BlockSparseArray{$(elt), 3, $(arrayt), …, …}"
end
@test (res == ref_arr(elt, arrayt_elt)) ||
(res == ref_arr(elt, arrayt_elt, "BlockSparseArrays."))

if elt === Float64
# Not testing other element types since they change the
Expand Down
Loading