Skip to content

Commit 655be5b

Browse files
authored
[NDTensors] More compact tensor printing (#1680)
1 parent 0084657 commit 655be5b

File tree

7 files changed

+41
-7
lines changed

7 files changed

+41
-7
lines changed

NDTensors/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NDTensors"
22
uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
33
authors = ["Matthew Fishman <[email protected]>"]
4-
version = "0.4.15"
4+
version = "0.4.16"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"

NDTensors/src/dense/densetensor.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,5 +319,7 @@ end
319319

320320
function Base.show(io::IO, mime::MIME"text/plain", T::DenseTensor)
321321
summary(io, T)
322-
return print_tensor(io, T)
322+
print_tensor(io, T)
323+
println(io)
324+
return nothing
323325
end

NDTensors/src/diag/diagtensor.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,6 @@ end
217217
function Base.show(io::IO, mime::MIME"text/plain", T::DiagTensor)
218218
summary(io, T)
219219
print_tensor(io, T)
220+
println(io)
220221
return nothing
221222
end

NDTensors/src/tensor/tensor.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,5 +495,9 @@ end
495495
# Printing
496496
#
497497

498-
print_tensor(io::IO, T::Tensor) = Base.print_array(io, expose(T))
499-
print_tensor(io::IO, T::Tensor{<:Number, 1}) = Base.print_array(io, reshape(T, (dim(T), 1)))
498+
function print_tensor(io::IO, T::Tensor)
499+
return Base.print_array(IOContext(io, :limit => true), expose(T))
500+
end
501+
function print_tensor(io::IO, T::Tensor{<:Number, 1})
502+
return Base.print_array(IOContext(io, :limit => true), reshape(T, (dim(T), 1)))
503+
end

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ITensors"
22
uuid = "9136182c-28ba-11e9-034c-db9fb085ebd5"
33
authors = ["Matthew Fishman <[email protected]>", "Miles Stoudenmire <[email protected]>"]
4-
version = "0.9.14"
4+
version = "0.9.15"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/itensor.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,8 +2025,16 @@ end
20252025
# TODO: make a specialized printing from Diag
20262026
# that emphasizes the missing elements
20272027
function show(io::IO, T::ITensor)
2028-
println(io, "ITensor ord=$(order(T))")
2029-
return show(io, MIME"text/plain"(), tensor(T))
2028+
if get(io, :compact, false)
2029+
# Just show the indices in compact view, used in some
2030+
# cases when printing arrays of ITensors (similar to
2031+
# printing of MPS in ITensorMPS.jl).
2032+
show(io, inds(T))
2033+
else
2034+
println(io, "ITensor ord=$(order(T))")
2035+
show(io, MIME"text/plain"(), tensor(T))
2036+
end
2037+
return nothing
20302038
end
20312039

20322040
function show(io::IO, mime::MIME"text/plain", T::ITensor)

test/base/test_itensor.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using ITensors:
77
ITensor,
88
Order,
99
QN,
10+
TagSet,
1011
,
1112
δ,
1213
addtags,
@@ -102,6 +103,24 @@ end
102103
@test storage(A) isa NDTensors.EmptyStorage{NDTensors.EmptyNumber}
103104
end
104105

106+
@testset "show" begin
107+
i = Index{Int}(1, 2, ITensors.Neither, TagSet(), 0)
108+
a = ITensor([1, 2], i)
109+
function res_show(prefix = "")
110+
return "ITensor ord=1\nDim 1: (dim=2|id=1)\n" *
111+
"$(prefix)Dense{Float64, Vector{Float64}}\n" *
112+
" 2-element\n 1.0\n 2.0\n"
113+
end
114+
@test sprint(show, a) in (res_show(), res_show("NDTensors."))
115+
@test sprint(show, a; context = :compact => true) == "((dim=2|id=1),)"
116+
function res_show_text(prefix = "")
117+
return "ITensor ord=1 (dim=2|id=1)\n" *
118+
"$(prefix)Dense{Float64, Vector{Float64}}"
119+
end
120+
@test sprint(show, MIME"text/plain"(), a) in
121+
(res_show_text(), res_show_text("NDTensors."))
122+
end
123+
105124
@testset "diag" for ElType in (Float32, Float64, ComplexF32, ComplexF64)
106125
i, j = Index.(2, ("i", "j"))
107126
A = random_itensor(ElType, i, j)

0 commit comments

Comments
 (0)