Skip to content

Commit 446c351

Browse files
committed
rework tensor show again
1 parent f92c704 commit 446c351

File tree

3 files changed

+56
-31
lines changed

3 files changed

+56
-31
lines changed

src/tensors/abstracttensor.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,33 @@ function Base.convert(::Type{Array}, t::AbstractTensorMap)
503503
return A
504504
end
505505
end
506+
507+
# Show and friends
508+
# ----------------
509+
Base.dims2string(V::HomSpace) = join(dim.(codomain(V)), '×') * "" * join(dim.(domain(V)), '×')
510+
511+
Base.summary(io::IO, t::AbstractTensorMap) = tensor_summary(io, t, space(t))
512+
513+
function tensor_summary(io::IO, t, V)
514+
print(io, Base.dims2string(V), " ")
515+
Base.showarg(io, t, true)
516+
return nothing
517+
end
518+
519+
# Human-readable:
520+
function Base.show(io::IO, ::MIME"text/plain", t::AbstractTensorMap)
521+
# 1) show summary: typically d₁×d₂×… ← d₃×d₄×… $(typeof(t)):
522+
summary(io, t)
523+
println(io, ":")
524+
525+
# 2) show spaces
526+
# println(io, " space(t):")
527+
print(io, " ", space(t))
528+
529+
# 3) [optional]: show data
530+
get(io, :compact, true) && return nothing
531+
ioc = IOContext(io, :typeinfo => sectortype(t))
532+
println(io, "\n\n blocks(t):")
533+
show_blocks(io, MIME"text/plain"(), blocks(t))
534+
return nothing
535+
end

src/tensors/blockiterator.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,21 @@ function foreachblock(f, t::AbstractTensorMap; scheduler = nothing)
4444
end
4545
return nothing
4646
end
47+
48+
function show_blocks(io, mime::MIME"text/plain", iter)
49+
first = true
50+
for (c, b) in iter
51+
first || print(io, "\n\n")
52+
print(io, " * ", c, " => ")
53+
show(io, mime, b)
54+
first = false
55+
end
56+
return nothing
57+
end
58+
59+
function show_blocks(io, iter)
60+
print(io, "(")
61+
join(io, iter, ", ")
62+
print(io, ")")
63+
return nothing
64+
end

src/tensors/tensor.jl

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -566,42 +566,19 @@ end
566566

567567
# Show
568568
#------
569-
function Base.summary(io::IO, t::TensorMap)
570-
return print(io, "TensorMap(", space(t), ")")
569+
function type_repr(::Type{TensorMap{T, S, N₁, N₂, A}}) where {T, S, N₁, N₂, A}
570+
return "TensorMap{$T, $(type_repr(S)), $N₁, $N₂, $A}"
571571
end
572-
Base.show(io::IO, t::TensorMap) = summary(io, t)
573572

574-
function Base.show(io::IO, ::MIME"text/plain", t::TensorMap)
575-
if get(io, :compact, false)
576-
print(io, "TensorMap(", space(t), ")")
577-
return
578-
end
579-
println(io, "TensorMap(", space(t), "):")
580-
581-
for (c, b) in blocks(t)
582-
print(io, "* block for charge ", c, ":")
583-
summary(io, b)
584-
println(io)
585-
end
586-
# if sectortype(t) == Trivial
587-
# Base.print_array(io, t[])
588-
# println(io)
589-
# elseif FusionStyle(sectortype(t)) isa UniqueFusion
590-
# for (f₁, f₂) in fusiontrees(t)
591-
# println(io, "* Data for sector ", f₁.uncoupled, " ← ", f₂.uncoupled, ":")
592-
# Base.print_array(io, t[f₁, f₂])
593-
# println(io)
594-
# end
595-
# else
596-
# for (f₁, f₂) in fusiontrees(t)
597-
# println(io, "* Data for fusiontree ", f₁, " ← ", f₂, ":")
598-
# Base.print_array(io, t[f₁, f₂])
599-
# println(io)
600-
# end
601-
# end
573+
function Base.showarg(io::IO, t::TensorMap, toplevel::Bool)
574+
!toplevel && print(io, "::")
575+
print(io, type_repr(typeof(t)))
602576
return nothing
603577
end
604578

579+
Base.show(io::IO, t::TensorMap) =
580+
print(io, type_repr(typeof(t)), "(", t.data, ", ", space(t), ")")
581+
605582
# Complex, real and imaginary parts
606583
#-----------------------------------
607584
for f in (:real, :imag, :complex)

0 commit comments

Comments
 (0)