ITensors.jl icon indicating copy to clipboard operation
ITensors.jl copied to clipboard

Compact MPS printing

Open MasonProtter opened this issue 4 years ago • 14 comments

Fixes #771

Okay, so there's a few ways to go about this, and I'm trying one way here but I'm open to changing direction. This makes it so when a MPS is printed or shown from some sort of compact enviornment, it defaults to a more compact representation of the MPS.

Generally speaking, in something like the inside of an array, types avoid putting newlines into their show methods because it can cause some weird printing stuff.

Therefore, I made this basically just like the the current non-compact printing of an MPS except I omit the newlines and only show the first and last 5 elements of the MPS:

julia> [randomMPS(siteinds("S=1/2", 50)) for _ ∈ 1:1]
1-element Vector{MPS}:
 MPS [1] ((dim=2|id=394|"S=1/2,Site,n=1"), (dim=1|id=885|"Link,l=1")), [2] ((dim=1|id=885|"Link,l=1"), (dim=2|id=156|"S=1/2,Site,n=2"), (dim=1|id=6|"Link,l=2")), [3] ((dim=1|id=6|"Link,l=2"), (dim=2|id=734|"S=1/2,Site,n=3"), (dim=1|id=193|"Link,l=3")), [4] ((dim=1|id=193|"Link,l=3"), (dim=2|id=788|"S=1/2,Site,n=4"), (dim=1|id=934|"Link,l=4")), ... , [45] ((dim=1|id=906|"Link,l=44"), (dim=2|id=342|"S=1/2,Site,n=45"), (dim=1|id=635|"Link,l=45")), [46] ((dim=1|id=635|"Link,l=45"), (dim=2|id=955|"S=1/2,Site,n=46"), (dim=1|id=626|"Link,l=46")), [47] ((dim=1|id=626|"Link,l=46"), (dim=2|id=252|"S=1/2,Site,n=47"), (dim=1|id=262|"Link,l=47")), [48] ((dim=1|id=262|"Link,l=47"), (dim=2|id=59|"S=1/2,Site,n=48"), (dim=1|id=203|"Link,l=48")), [49] ((dim=1|id=203|"Link,l=48"), (dim=2|id=771|"S=1/2,Site,n=49"), (dim=1|id=31|"Link,l=49")), [50] ((dim=1|id=31|"Link,l=49"), (dim=2|id=635|"S=1/2,Site,n=50"))

This is adjustable through a const Ref:

julia> ITensors.compact_show_cutoff[]
10

julia> ITensors.compact_show_cutoff[] = 4
4

julia> [randomMPS(siteinds("S=1/2", 50)) for _ ∈ 1:1]
1-element Vector{MPS}:
 MPS [1] ((dim=2|id=24|"S=1/2,Site,n=1"), (dim=1|id=360|"Link,l=1")), ... , [48] ((dim=1|id=828|"Link,l=47"), (dim=2|id=79|"S=1/2,Site,n=48"), (dim=1|id=404|"Link,l=48")), [49] ((dim=1|id=404|"Link,l=48"), (dim=2|id=407|"S=1/2,Site,n=49"), (dim=1|id=112|"Link,l=49")), [50] ((dim=1|id=112|"Link,l=49"), (dim=2|id=731|"S=1/2,Site,n=50"))

Regular toplevel printing is not effected:

julia> randomMPS(siteinds("S=1/2", 20))
MPS
[1] ((dim=2|id=710|"S=1/2,Site,n=1"), (dim=1|id=864|"Link,l=1"))
[2] ((dim=1|id=864|"Link,l=1"), (dim=2|id=750|"S=1/2,Site,n=2"), (dim=1|id=189|"Link,l=2"))
[3] ((dim=1|id=189|"Link,l=2"), (dim=2|id=353|"S=1/2,Site,n=3"), (dim=1|id=910|"Link,l=3"))
[4] ((dim=1|id=910|"Link,l=3"), (dim=2|id=552|"S=1/2,Site,n=4"), (dim=1|id=318|"Link,l=4"))
[5] ((dim=1|id=318|"Link,l=4"), (dim=2|id=826|"S=1/2,Site,n=5"), (dim=1|id=717|"Link,l=5"))
[6] ((dim=1|id=717|"Link,l=5"), (dim=2|id=149|"S=1/2,Site,n=6"), (dim=1|id=360|"Link,l=6"))
[7] ((dim=1|id=360|"Link,l=6"), (dim=2|id=91|"S=1/2,Site,n=7"), (dim=1|id=216|"Link,l=7"))
[8] ((dim=1|id=216|"Link,l=7"), (dim=2|id=293|"S=1/2,Site,n=8"), (dim=1|id=182|"Link,l=8"))
[9] ((dim=1|id=182|"Link,l=8"), (dim=2|id=741|"S=1/2,Site,n=9"), (dim=1|id=245|"Link,l=9"))
[10] ((dim=1|id=245|"Link,l=9"), (dim=2|id=367|"S=1/2,Site,n=10"), (dim=1|id=295|"Link,l=10"))
[11] ((dim=1|id=295|"Link,l=10"), (dim=2|id=901|"S=1/2,Site,n=11"), (dim=1|id=823|"Link,l=11"))
[12] ((dim=1|id=823|"Link,l=11"), (dim=2|id=604|"S=1/2,Site,n=12"), (dim=1|id=69|"Link,l=12"))
[13] ((dim=1|id=69|"Link,l=12"), (dim=2|id=73|"S=1/2,Site,n=13"), (dim=1|id=569|"Link,l=13"))
[14] ((dim=1|id=569|"Link,l=13"), (dim=2|id=27|"S=1/2,Site,n=14"), (dim=1|id=470|"Link,l=14"))
[15] ((dim=1|id=470|"Link,l=14"), (dim=2|id=946|"S=1/2,Site,n=15"), (dim=1|id=697|"Link,l=15"))
[16] ((dim=1|id=697|"Link,l=15"), (dim=2|id=124|"S=1/2,Site,n=16"), (dim=1|id=776|"Link,l=16"))
[17] ((dim=1|id=776|"Link,l=16"), (dim=2|id=484|"S=1/2,Site,n=17"), (dim=1|id=86|"Link,l=17"))
[18] ((dim=1|id=86|"Link,l=17"), (dim=2|id=306|"S=1/2,Site,n=18"), (dim=1|id=108|"Link,l=18"))
[19] ((dim=1|id=108|"Link,l=18"), (dim=2|id=914|"S=1/2,Site,n=19"), (dim=1|id=405|"Link,l=19"))
[20] ((dim=1|id=405|"Link,l=19"), (dim=2|id=333|"S=1/2,Site,n=20"))

What do you think about this implementation? I find the way I've done it here a bit noisy and not very helpful honestly, and it makes me thing we should show even less information. Perhaps something more like just showing that it's an MPS and showing what sort of sites it's on? E.g. something like

MPS on sites S=1/2,Site,n=1", "S=1/2,Site,n=2", "S=1/2,Site,n=3",  ... "S=1/2,Site,n=48", "S=1/2,Site,n=49", "S=1/2,Site,n=50"]

Suggestions welcome!

MasonProtter avatar Nov 09 '21 23:11 MasonProtter

Codecov Report

Merging #772 (ae3ac1f) into main (a1b6f02) will decrease coverage by 37.48%. The diff coverage is 0.99%.

:exclamation: Current head ae3ac1f differs from pull request most recent head f64ff36. Consider uploading reports for the commit f64ff36 to get more accurate results Impacted file tree graph

@@             Coverage Diff             @@
##             main     #772       +/-   ##
===========================================
- Coverage   67.93%   30.44%   -37.49%     
===========================================
  Files          87       91        +4     
  Lines       10291    10460      +169     
===========================================
- Hits         6991     3185     -3806     
- Misses       3300     7275     +3975     
Impacted Files Coverage Δ
src/ITensors.jl 30.00% <ø> (ø)
src/LazyApply/LazyApply.jl 0.00% <0.00%> (ø)
src/Ops/Ops.jl 0.00% <0.00%> (ø)
src/Ops/ops_itensor.jl 0.00% <0.00%> (ø)
src/Ops/ops_mpo.jl 0.00% <0.00%> (ø)
src/Ops/trotter.jl 0.00% <0.00%> (ø)
src/mps/abstractmps.jl 12.16% <0.00%> (-61.75%) :arrow_down:
src/itensor.jl 26.60% <40.00%> (-46.58%) :arrow_down:
src/physics/sitetype.jl 48.48% <100.00%> (-34.05%) :arrow_down:
src/physics/site_types/boson.jl 0.00% <0.00%> (-100.00%) :arrow_down:
... and 70 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update a1b6f02...f64ff36. Read the comment docs.

codecov-commenter avatar Nov 09 '21 23:11 codecov-commenter

Thanks for looking into this @MasonProtter, printing of ITensor objects could definitely use some improvement.

Maybe we should go even farther with this and just print MPS(N) where N is the length of the MPS? So for example:

julia> [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> using ITensors

julia> [MPS(10), MPS(20), MPS(30)]
3-element Vector{MPS}:
 MPS(10)
 MPS(20)
 MPS(30)

It seems like if you really want more detailed information about the MPS you should investigate them individually.

mtfishman avatar Nov 10 '21 13:11 mtfishman

I think one of the reasons why we haven't done this before (besides laziness) is because I like to use @show to debug code, in which case it is useful to get a more verbose output. But with this PR, @show will call the less compact show and will be less useful. But I think the solution for that is to define our own more verbose version of @show that calls show(::IO, ::MIME"text/plain", ...), maybe @show_verbose or something like that.

mtfishman avatar Nov 10 '21 14:11 mtfishman

yeah that sounds good to me. I also use @show lot while debugging, so yeah @show_verbose sounds good to me

MasonProtter avatar Nov 11 '21 17:11 MasonProtter

Here is the new printing and @show_verbose

julia> let m1 = MPS(3), m2 = MPS(4)
           ITensors.@show_verbose m1 m2
           [m1, m2]
       end
m1 = MPS
#undef
#undef
#undef

m2 = MPS
#undef
#undef
#undef
#undef

2-element Vector{MPS}:
 MPS(3)
 MPS(4)

MasonProtter avatar Nov 11 '21 17:11 MasonProtter

Here is a summary:

julia> using ITensors

julia> using ITensors: @show_verbose

julia> s = siteinds("S=1/2", 8);

julia> M = [randomMPS(s; linkdims=n) for n in 1:10];

julia> M
10-element Vector{MPS}:
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)

julia> @show M;
M = MPS[MPS(8), MPS(8), MPS(8), MPS(8), MPS(8), MPS(8), MPS(8), MPS(8), MPS(8), MPS(8)]

julia> @show_verbose M;
M = 10-element Vector{MPS}:
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)
 MPS(8)

julia> M[3]
MPS
[1] ((dim=2|id=433|"S=1/2,Site,n=1"), (dim=3|id=855|"Link,l=1"))
[2] ((dim=3|id=855|"Link,l=1"), (dim=2|id=945|"S=1/2,Site,n=2"), (dim=3|id=68|"Link,l=2"))
[3] ((dim=3|id=68|"Link,l=2"), (dim=2|id=92|"S=1/2,Site,n=3"), (dim=3|id=806|"Link,l=3"))
[4] ((dim=3|id=806|"Link,l=3"), (dim=2|id=7|"S=1/2,Site,n=4"), (dim=3|id=535|"Link,l=4"))
[5] ((dim=3|id=535|"Link,l=4"), (dim=2|id=198|"S=1/2,Site,n=5"), (dim=3|id=365|"Link,l=5"))
[6] ((dim=3|id=365|"Link,l=5"), (dim=2|id=608|"S=1/2,Site,n=6"), (dim=3|id=518|"Link,l=6"))
[7] ((dim=3|id=518|"Link,l=6"), (dim=2|id=95|"S=1/2,Site,n=7"), (dim=2|id=108|"Link,l=7"))
[8] ((dim=2|id=108|"Link,l=7"), (dim=2|id=894|"S=1/2,Site,n=8"))


julia> @show M[3];
M[3] = MPS(8)

julia> @show_verbose M[3];
M[3] = MPS
[1] ((dim=2|id=433|"S=1/2,Site,n=1"), (dim=3|id=855|"Link,l=1"))
[2] ((dim=3|id=855|"Link,l=1"), (dim=2|id=945|"S=1/2,Site,n=2"), (dim=3|id=68|"Link,l=2"))
[3] ((dim=3|id=68|"Link,l=2"), (dim=2|id=92|"S=1/2,Site,n=3"), (dim=3|id=806|"Link,l=3"))
[4] ((dim=3|id=806|"Link,l=3"), (dim=2|id=7|"S=1/2,Site,n=4"), (dim=3|id=535|"Link,l=4"))
[5] ((dim=3|id=535|"Link,l=4"), (dim=2|id=198|"S=1/2,Site,n=5"), (dim=3|id=365|"Link,l=5"))
[6] ((dim=3|id=365|"Link,l=5"), (dim=2|id=608|"S=1/2,Site,n=6"), (dim=3|id=518|"Link,l=6"))
[7] ((dim=3|id=518|"Link,l=6"), (dim=2|id=95|"S=1/2,Site,n=7"), (dim=2|id=108|"Link,l=7"))
[8] ((dim=2|id=108|"Link,l=7"), (dim=2|id=894|"S=1/2,Site,n=8"))


Looking at the above, I could see an advantage of printing a bit more in the simple MPS show method, especially since @show_verbose M is not very useful now. I think we need to strike a balance between making @show_verbose M more useful but making sure @show M doesn't become too messy and incomprehensible (for example try @show [randn(10, 10) for _ in 1:10]). @emstoudenmire what do you think?

mtfishman avatar Nov 11 '21 18:11 mtfishman

Agreed. I was hoping we aren’t going to change the default behavior of @show for a single MPS too much with this PR. Mainly I was supportive of the change of putting “…” if the MPS was longer than some amount, say more than 20 sites.

I do like the idea of just showing an MPS as MPS(8) when it is contained inside something else. Is it easy to define overloads of @show which distinguish these two cases (single MPS versus inside another data structure)?

emstoudenmire avatar Nov 11 '21 18:11 emstoudenmire

Not that I know of. By default in Julia, both @show and showing the elements of an Array would call show(::IO, ::MPS). I don't see how @show could be customized, but maybe the way the elements of an Array get shown could be customized?

mtfishman avatar Nov 11 '21 18:11 mtfishman

Actually, there is a compact attribute in the IO object, maybe that could be used for this? https://discourse.julialang.org/t/show-and-showcompact-on-custom-types/8493 https://github.com/JuliaLang/julia/blob/9028130434c5fb55e00c65e73aaf13cf0afde380/base/complex.jl#L181-L196

mtfishman avatar Nov 11 '21 18:11 mtfishman

The problem is that containers like Array don't set their internal printing to compact. Compare:

julia> println(IOContext(stdout, :compact => false), 1.12341234)
1.12341234

julia> println(IOContext(stdout, :compact => true),  1.12341234)
1.12341

julia> [1.12341234]
1-element Vector{Float64}:
 1.12341234

MasonProtter avatar Nov 11 '21 19:11 MasonProtter

Thanks, that rules out that option.

mtfishman avatar Nov 11 '21 21:11 mtfishman

@emstoudenmire, from what I can tell there is currently no way to have a compact show for elements of Array{MPS} and a non-compact show used in @show (since they both call show(::IO, ::MPS)), unless we customize show for Array{MPS} which I think is impractical (there is a lot of complicated code that goes into formatting Array outputs, such as multidimensional Arrays). I wish there was more customization available there, I think Julia should have a way to customize which show is used when printing elements of an Array, but 🤷. Also I think it is unfortunate that the compact version of show is called from @show, also 🤷 (try @show randn(100, 100), not the best).

Personally I think the simplest way to go is make our own macro like @show_verbose arg as @MasonProtter did here (or maybe something shorter, like @showv) which calls show(::IO, ::MIME"text/plain", arg). Then we can just follow the standard approach of having one compact version of printing our types defined by show(::IO, arg) and a verbose printing defined by show(::IO, ::MIME"text/plain", arg). Perhaps I am missing an alternative solution, open to suggestions.

In fact, looking back at things, I think we have the definitions backwards, for example for ITensor we have:

julia> i = Index(2);

julia> show(stdout, "text/plain", randomITensor(i', i));
ITensor ord=2 (dim=2|id=240)' (dim=2|id=240)
ITensors.NDTensors.Dense{Float64, Vector{Float64}}
julia> show(stdout, randomITensor(i', i));
ITensor ord=2
Dim 1: (dim=2|id=240)'
Dim 2: (dim=2|id=240)
ITensors.NDTensors.Dense{Float64, Vector{Float64}}
 2×2
 -1.0167055049492282  -0.3264027798780254
  0.7315881188879073  -1.3839747548593588

I think this is rooted in wanting @show to look better, but we are expecting it to do something it was not designed to do so we should come up with our own macro.

A consequence of the current design is that it makes printing Array{ITensor} pretty unwieldy:

julia> i = Index(100)
(dim=100|id=113)

julia> [randomITensor(i'', i', i) for _ in 1:10]
10-element Vector{ITensor}:
 ITensor ord=3
Dim 1: (dim=100|id=113)''
Dim 2: (dim=100|id=113)'
Dim 3: (dim=100|id=113)
ITensors.NDTensors.Dense{Float64, Vector{Float64}}
 100×100×100
[:, :, 1] =
  0.539633123096535    -0.39585097404039954  …  -0.6653048829066069
  2.467129315039132    -0.11907674241487644      0.6150783441288017
  0.5890669114325943    0.8852914458347289      -0.06104485688777041
  0.3420606266681132    1.6242056330053272      -0.4557301534845767
 -0.4447110614829224    0.8586034110240498       0.27088404068446403
 -0.36447847578000053   2.5166228952202587   …   0.4108373245763029
  0.7025203772414333   -0.11848342152097505      0.3363418214466386
 -1.591680062967237    -0.2493791999699872       0.4356769316088687
 -0.6108766935741851   -1.37732926122349         0.2646098532762197
 -0.5251721298462694    0.28204434980836335     -0.17177243113899693
  ⋮                                          ⋱  
  1.1575002803997825   -0.21728324859124357      0.29811679976978833
 -1.6280740632206077    0.26961201155464015     -1.3995187925110153
  0.14911698400052414  -0.44303396156814784      1.3812790457611002
  0.7072242698607226   -1.5662515696856727      -1.0689936893147756
  0.5776240510723901   -1.7104474848534277   …   0.8005075933223866
  1.7271457624164888    1.8456175194151043       2.1075833679724973
  1.2096724216026749    1.0721335559453296       1.335226395252451
  0.2948465988832223    0.7903423603900087       0.8790353893444962
 -0.09654516312661886   0.9194408425342692      -1.0240237913059465

[:, :, 2] =
  0.03532920276122561    0.2568583933078362   …   0.5563310530531189
  0.4927967751377661    -1.0508767412676976       0.2613358184181657
  1.8533705792509587     0.9759450560272133       0.22078272393777634
  1.9490540622166719     1.8040875109179475       0.9513053945677078
  1.1043949885820912     0.8683197751242245      -0.2992615516508142
  1.9096312812897733     0.3232543003511035   …   1.6907174672995815
  1.11512180172116      -0.13566607224998706      0.09964895765738835
 -0.619662968397387     -1.813412632659377       -0.9417145634232201
  0.038038061866559336   1.2912010407861025      -0.6531562925518497
 -1.2308387100808884     1.6741194885353488      -0.3167112789298043
  ⋮                                           ⋱  
  0.4205850324690859     0.2190922633369162      -0.43856543975828477
 -0.30515928595515923    0.301996040631815        0.2934715515248511
 -1.780909659712542      0.4208735905687667       0.07283638814705169
 -0.3038644148931843    -0.19619209406124585     -0.23610321726812766
 -1.396958790287576     -0.07911792192043726  …  -1.1741962871558371
  1.3981841422495815     0.8483025947056868       1.275117190040007
  0.877154937920254      0.02119421909713376     -0.729830834192325
 -0.5536905914335546    -2.2964004633935717      -0.9661364539172445
 -1.2910189990931833     0.7432376940340646       0.11610979997859004

[:, :, 3] =
  0.05012203865659773    0.05229286045271644   …   0.15237944775134152
 -1.0180473076090213    -0.04073518760687088      -0.7554137598536583
 -0.9507210406202723     0.009221994216641514      0.16301129730290534
 -0.1499805637109642    -0.8417072647342031       -1.9215649580783987
 -0.7261221928121334     0.5141117300353082       -0.622149877652235
  1.133301018437103     -1.3307318895070503    …   0.753828128455448
 -0.28379673953168133   -0.16081843612105362      -0.12794650958423384
  1.37165870685902       0.7777989190194529        0.8940667756087681
  1.871227855766476      1.3861978736115397       -0.5757721214794905
 -1.0898224233916365     2.08947817450898          0.3593443935512408
  ⋮                                            ⋱  
  0.6089238086321251     0.18778514048340117      -0.6311911709420768
 -0.7391862458587849    -0.49434159689015283       0.9368683308150981
 -0.5084918211622681    -1.1261830526960663       -1.6301916732947754
  1.528959063401851      0.01063679265320535      -0.27257197712911296
 -0.002976372107062703   0.45836207524107836   …  -0.13761703381789583
  0.3108107068912703     1.504981311698514         1.3829754409778099
  0.6081371733056671     0.3188665207350987       -0.1923312048848557
  0.32810794173650804   -0.20803268811467376       3.1421718328510804
  0.20919366297357556    1.939776993960456        -0.976775797318182

;;; … 

[:, :, 98] =
  0.8610551009867918    0.5312560712095616   …   0.8788176059472618
 -0.1469753164168656    0.2762272226941387      -1.3180203372039212
 -0.11342898499358328   0.19555556587661682     -0.17654676481345313
 -0.7112756175646048   -0.2608109197053975      -0.6342950208514937
 -0.2675753854968734    0.35397079332454323     -0.19321942270058012
 -1.31581994244343      1.3270473519241572   …  -0.7182868475100256
 -1.1138378780510758   -1.521966306421771        0.9485640068956396
  0.10485696456018977  -0.8680533586469985      -0.977507934732912
 -1.635272665183603    -0.23441577578698286      0.1943368058643454
 -1.8146606989523457    1.571724042753342       -0.022912939063868043
  ⋮                                          ⋱  
  0.06532995448543515  -0.21521507605955195     -0.7967413881325854
  0.14647281051531966  -1.2197865381838935       1.1064223569862797
  0.2044987413202758    0.5895421716178367       0.009013804850249685
  0.6239908855885419    1.2430292161008991      -1.5886523295853936
  1.0178075707816536   -0.2854288911068872   …  -1.2983719935581246
  0.6551287027749337    0.8769114938915802      -0.0755676297216932
  1.673646601260157    -0.39190638093838637     -0.5785791560708948
  0.3577567140872468   -0.4960380924585519       0.4580384297681727
 -1.2143752990531902    0.7094318989448364       0.5492340668704934

[:, :, 99] =
 -1.248198806335628    -0.7181658539678007   …   1.492359463416025
  0.01773791366277253   1.4568394502863644       1.2247437386795015
  0.5176290763948882    0.3152929038599073       0.27272162416972967
 -1.7905830009197605   -0.20176722786940615      0.750352009561684
 -0.5106502894448403    0.48594235368524674     -1.0613900781133658
 -1.0620737732557601   -0.15744104349388469  …  -0.8274121459920815
 -2.0794135296121024   -0.07757450365275195      0.6460220432761192
  1.0620436795411454   -0.08541225282423255     -0.042407651962400504
  0.4049344286723456    0.2514364917286498      -0.045989126013834286
  0.16911709612346115   0.6780202748713829      -0.18651568691863588
  ⋮                                          ⋱  
 -0.20896049354345653  -0.12329471841071048     -0.04780573710224975
 -0.202776143235553     0.4457863514534772      -0.10146579072809178
  0.7146250656132853   -1.9222412337935195      -1.8075946984327451
 -0.4738454028557653   -0.6548036816975925      -0.6262416860209056
  0.7570631225204479    1.0096389114100228   …   0.445883854024118
  0.3584280695333485    1.02513418226397         0.49868537646727706
  0.9698112099338226    0.3896097571560104       0.2829077550691685
 -0.7970000207132792    0.7538748647765107      -0.2987006698645591
  0.7029733133812078    0.6312505444200068      -0.5849025280986312

[:, :, 100] =
 -1.0851844102535242   -1.194139088625669    …   0.8484290048959561
 -0.13858921878587993  -0.9856202833839869      -0.31142109131250506
 -0.7588825599683944    1.121828712479744        0.4188495128700498
  1.5874686738801176    0.4449948987592607       1.0273177190498963
 -0.4082696519530168    0.06936768623361894     -0.019318066262004913
 -0.3336781882392999   -1.1799001166274197   …  -0.5291346955778713
  1.6163656104114097    1.8378890193984345       0.8676798368075215
 -0.7264450751262298    0.6382206287107742      -0.10142406627787337
  0.23161725083356266  -0.4908595393665819      -0.04286757564209445
 -0.16038163427535188  -0.9792591252919431      -2.114289054878378
  ⋮                                          ⋱  
  0.4211705028518652   -0.7575070759970429      -0.49919811257236657
  0.38601306960797876  -0.3886107090727237      -0.14261246903507518
 -0.45002919528518986   1.0269587632134365      -0.35156512354662556
  0.6793098192824187   -0.5616932523345731       0.9126077119877442
  0.9111272428694285   -1.5012864953012979   …   0.46800727901931577
 -0.739443249142166    -0.20296984426657616      0.8730783330317115
  1.0302329153220342    1.8688527866589952       1.168549652260097
 -1.8409833941930773   -1.9394407105330085      -0.07320498521283635
 -0.4165944040719091   -1.5003732436445631      -0.25126583702099864
 ITensor ord=3
Dim 1: (dim=100|id=113)''
Dim 2: (dim=100|id=113)'
Dim 3: (dim=100|id=113)
ITensors.NDTensors.Dense{Float64, Vector{Float64}}
 100×100×100
[:, :, 1] =
 -0.2696239438660804    0.4546084127697769    …   0.43437057134241275
 -0.855138954515774    -1.3184458065877132       -1.3791196411540758
 -0.7664310407058416    0.5949018239151038        3.3564029832951934
 -0.590930027228707     0.4693619502810474       -1.0915124738705373
[...]

I think for that use case it is best to define show(::IO, ::ITensor) properly as the compact version.

mtfishman avatar Nov 11 '21 22:11 mtfishman

@MasonProtter It looks like sometimes context properties like compact and limit do get set during Array show:

struct MyType end

function Base.show(io::IO, ::MIME"text/plain", m::MyType)
  compact = get(io, :compact, false)
  limit = get(io, :limit, false)
  print(io, "show(::IO, ::MIME\"text/plain\", ::MyType), compact=$compact, limit=$limit\n")
end
function Base.show(io::IO, m::MyType)
  compact = get(io, :compact, false)
  limit = get(io, :limit, false)
  print(io, "show(::IO, ::MyType), compact=$compact, limit=$limit\n")
end

m = MyType()
mm = [m for _ in 1:3, _ in 1:3]

println("\n\nshow(stdout, \"text/plain\", m)")
show(stdout, "text/plain", m)

println("\n\nshow(stdout, \"text/plain\", mm)")
show(stdout, "text/plain", mm)

println("\n\nshow(stdout, m)")
show(stdout, m)

println("\n\nshow(stdout, mm)")
show(stdout, mm)

println("\n\ndisplay(m)")
display(m)

println("\n\ndisplay(mm)")
display(mm)

println("\n\n@show m")
@show m

println("\n\n@show mm")
@show mm

Prints:



show(stdout, "text/plain", m)
show(::IO, ::MIME"text/plain", ::MyType), compact=false, limit=false


show(stdout, "text/plain", mm)
3×3 Matrix{MyType}:
 show(::IO, ::MyType), compact=true, limit=false
  show(::IO, ::MyType), compact=true, limit=false
  show(::IO, ::MyType), compact=true, limit=false

 show(::IO, ::MyType), compact=true, limit=false
  show(::IO, ::MyType), compact=true, limit=false
  show(::IO, ::MyType), compact=true, limit=false

 show(::IO, ::MyType), compact=true, limit=false
  show(::IO, ::MyType), compact=true, limit=false
  show(::IO, ::MyType), compact=true, limit=false


show(stdout, m)
show(::IO, ::MyType), compact=false, limit=false


show(stdout, mm)
[show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
; show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
; show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
]

display(m)
show(::IO, ::MIME"text/plain", ::MyType), compact=false, limit=true



display(mm)
3×3 Matrix{MyType}:
 show(::IO, ::MyType), compact=true, limit=true
  …  show(::IO, ::MyType), compact=true, limit=true

 show(::IO, ::MyType), compact=true, limit=true
     show(::IO, ::MyType), compact=true, limit=true

 show(::IO, ::MyType), compact=true, limit=true
     show(::IO, ::MyType), compact=true, limit=true



@show m
m = show(::IO, ::MyType), compact=false, limit=false



@show mm
mm = [show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
; show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
; show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
 show(::IO, ::MyType), compact=false, limit=false
]


mtfishman avatar Nov 15 '21 19:11 mtfishman

Also relevant docs: https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext-Tuple{IO,%20Pair}

mtfishman avatar Nov 15 '21 19:11 mtfishman