Skip to content

[NDTensors] More compact tensor printing#1680

Merged
mtfishman merged 4 commits into
mainfrom
NDTensors/mf/limit_show
Nov 13, 2025
Merged

[NDTensors] More compact tensor printing#1680
mtfishman merged 4 commits into
mainfrom
NDTensors/mf/limit_show

Conversation

@mtfishman

@mtfishman mtfishman commented Nov 12, 2025

Copy link
Copy Markdown
Member

This enables more compact printing of ITensors, such as:

using ITensors: Index, random_itensor
d = 100
i, j = Index.((d, d))
a = random_itensor(i, j)
julia> show(a)
ITensor ord=2
Dim 1: (dim=100|id=386)
Dim 2: (dim=100|id=806)
NDTensors.Dense{Float64, Vector{Float64}}
 100×100
  1.1065511218675492    0.25558343779457593    -0.7563918255048953
 -0.19087995131196725   0.10936665763661539     -0.9511317856005679
 -0.1339119679780238   -0.9824888504596919      -0.8297798433753355
 -0.3380376950048613   -0.22502109940735232      1.238562637784077
                                              
  0.569878151816781     0.9722889212769971       0.15625262885769123
 -0.6617584401784992    1.1133126735067624       1.1441043736020045
 -1.2613730025434786   -0.3896145241325943       0.5647941511581129

which is achieved by setting limit = true when printing the tensor data as an array (see https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext-Tuple{IO,%20Pair}).

Previously, all of the elements of the tensor were being printing, which filled the terminal with output for larger tensors. This was an issue I came across in the ITensor CCQ school.

@mtfishman

Copy link
Copy Markdown
Member Author

In the latest I also made some printing of ITensors from within arrays more compact by just printing the indices in those cases:

julia> [a a; a a]
2×2 Matrix{ITensor}:
 ((dim=100|id=386), (dim=100|id=806))    ((dim=100|id=386), (dim=100|id=806))
 ((dim=100|id=386), (dim=100|id=806))     ((dim=100|id=386), (dim=100|id=806))

@emstoudenmire

Copy link
Copy Markdown
Collaborator

This looks great. I had not thought of the idea of just showing some of the tensor data when it’s long, though of course some of the Julia array types do that.

@mtfishman mtfishman merged commit 655be5b into main Nov 13, 2025
13 of 14 checks passed
@mtfishman mtfishman deleted the NDTensors/mf/limit_show branch November 13, 2025 18:54
kmp5VT pushed a commit to kmp5VT/ITensors.jl that referenced this pull request Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants