From 29b45a5d33f08a3d1444cbcfa028e41434c110c4 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 12 Nov 2025 11:34:12 -0500 Subject: [PATCH 1/4] [NDTensors] More compact tensor printing --- NDTensors/Project.toml | 2 +- NDTensors/src/tensor/tensor.jl | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/NDTensors/Project.toml b/NDTensors/Project.toml index 138c276bfc..392b203c4b 100644 --- a/NDTensors/Project.toml +++ b/NDTensors/Project.toml @@ -1,7 +1,7 @@ name = "NDTensors" uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" authors = ["Matthew Fishman "] -version = "0.4.15" +version = "0.4.16" [deps] Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" diff --git a/NDTensors/src/tensor/tensor.jl b/NDTensors/src/tensor/tensor.jl index 80b6ece502..f0af4a8d7e 100644 --- a/NDTensors/src/tensor/tensor.jl +++ b/NDTensors/src/tensor/tensor.jl @@ -495,5 +495,9 @@ end # Printing # -print_tensor(io::IO, T::Tensor) = Base.print_array(io, expose(T)) -print_tensor(io::IO, T::Tensor{<:Number, 1}) = Base.print_array(io, reshape(T, (dim(T), 1))) +function print_tensor(io::IO, T::Tensor) + return Base.print_array(IOContext(io, :limit => true), expose(T)) +end +function print_tensor(io::IO, T::Tensor{<:Number, 1}) + return Base.print_array(IOContext(io, :limit => true), reshape(T, (dim(T), 1))) +end From 1724f803f1d9922c39a05bc93ef26b53805ea2c4 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 12 Nov 2025 11:41:08 -0500 Subject: [PATCH 2/4] Missing println --- NDTensors/src/dense/densetensor.jl | 4 +++- NDTensors/src/diag/diagtensor.jl | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/NDTensors/src/dense/densetensor.jl b/NDTensors/src/dense/densetensor.jl index 5cb547b4c3..fd248ce8b8 100644 --- a/NDTensors/src/dense/densetensor.jl +++ b/NDTensors/src/dense/densetensor.jl @@ -319,5 +319,7 @@ end function Base.show(io::IO, mime::MIME"text/plain", T::DenseTensor) summary(io, T) - return print_tensor(io, T) + print_tensor(io, T) + println(io) + return nothing end diff --git a/NDTensors/src/diag/diagtensor.jl b/NDTensors/src/diag/diagtensor.jl index de9db5b3e9..46c4603c1e 100644 --- a/NDTensors/src/diag/diagtensor.jl +++ b/NDTensors/src/diag/diagtensor.jl @@ -217,5 +217,6 @@ end function Base.show(io::IO, mime::MIME"text/plain", T::DiagTensor) summary(io, T) print_tensor(io, T) + println(io) return nothing end From d0ed5585aff1b92c1df8edb5fbacdf06c60ce4f9 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 12 Nov 2025 12:29:53 -0500 Subject: [PATCH 3/4] Compact printing when ITensors are printed from within arrays --- src/itensor.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/itensor.jl b/src/itensor.jl index a41cfe6a31..ab36251e91 100644 --- a/src/itensor.jl +++ b/src/itensor.jl @@ -2025,8 +2025,16 @@ end # TODO: make a specialized printing from Diag # that emphasizes the missing elements function show(io::IO, T::ITensor) - println(io, "ITensor ord=$(order(T))") - return show(io, MIME"text/plain"(), tensor(T)) + if get(io, :compact, false) + # Just show the indices in compact view, used in some + # cases when printing arrays of ITensors (similar to + # printing of MPS in ITensorMPS.jl). + show(io, inds(T)) + else + println(io, "ITensor ord=$(order(T))") + show(io, MIME"text/plain"(), tensor(T)) + end + return nothing end function show(io::IO, mime::MIME"text/plain", T::ITensor) From 8e508dc0c3c001a753edd515513923ff1e3f1d61 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 13 Nov 2025 11:49:23 -0500 Subject: [PATCH 4/4] Format --- Project.toml | 2 +- test/base/test_itensor.jl | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f889c20e9c..ace3300ab9 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ITensors" uuid = "9136182c-28ba-11e9-034c-db9fb085ebd5" authors = ["Matthew Fishman ", "Miles Stoudenmire "] -version = "0.9.14" +version = "0.9.15" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/test/base/test_itensor.jl b/test/base/test_itensor.jl index 648cfc2641..40bb8fce1f 100644 --- a/test/base/test_itensor.jl +++ b/test/base/test_itensor.jl @@ -7,6 +7,7 @@ using ITensors: ITensor, Order, QN, + TagSet, ⊕, δ, addtags, @@ -102,6 +103,24 @@ end @test storage(A) isa NDTensors.EmptyStorage{NDTensors.EmptyNumber} end + @testset "show" begin + i = Index{Int}(1, 2, ITensors.Neither, TagSet(), 0) + a = ITensor([1, 2], i) + function res_show(prefix = "") + return "ITensor ord=1\nDim 1: (dim=2|id=1)\n" * + "$(prefix)Dense{Float64, Vector{Float64}}\n" * + " 2-element\n 1.0\n 2.0\n" + end + @test sprint(show, a) in (res_show(), res_show("NDTensors.")) + @test sprint(show, a; context = :compact => true) == "((dim=2|id=1),)" + function res_show_text(prefix = "") + return "ITensor ord=1 (dim=2|id=1)\n" * + "$(prefix)Dense{Float64, Vector{Float64}}" + end + @test sprint(show, MIME"text/plain"(), a) in + (res_show_text(), res_show_text("NDTensors.")) + end + @testset "diag" for ElType in (Float32, Float64, ComplexF32, ComplexF64) i, j = Index.(2, ("i", "j")) A = random_itensor(ElType, i, j)