changeset 1009:d5e6c4e9d64e feature/lazy_tensors/pretty_printing

Add pretty printing for IdentityTensor
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 21 Mar 2022 08:04:14 +0100
parents 8920f237caf7
children 7acd417b780d
files src/LazyTensors/tensor_types.jl test/LazyTensors/tensor_types_test.jl
diffstat 2 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/LazyTensors/tensor_types.jl	Sun Mar 20 22:58:25 2022 +0100
+++ b/src/LazyTensors/tensor_types.jl	Mon Mar 21 08:04:14 2022 +0100
@@ -18,6 +18,17 @@
 apply(tmi::IdentityTensor{T,D}, v::AbstractArray{<:Any,D}, I::Vararg{Any,D}) where {T,D} = v[I...]
 apply_transpose(tmi::IdentityTensor{T,D}, v::AbstractArray{<:Any,D}, I::Vararg{Any,D}) where {T,D} = v[I...]
 
+function Base.show(io::IO, ::MIME"text/plain", tm::IdentityTensor{T}) where T
+    if (:comact => true) in io
+        print(io, "I")
+    else
+        print(io, "IdentityTensor{$T}")
+    end
+    print(io, "(")
+    join(io, tm.size, ",")
+    print(io, ")")
+end
+
 
 """
     ScalingTensor{T,D} <: LazyTensor{T,D,D}
--- a/test/LazyTensors/tensor_types_test.jl	Sun Mar 20 22:58:25 2022 +0100
+++ b/test/LazyTensors/tensor_types_test.jl	Mon Mar 21 08:04:14 2022 +0100
@@ -41,6 +41,14 @@
     @test_throws DomainSizeMismatch I1∘A
     @test_throws DomainSizeMismatch A∘I2
     @test_throws DomainSizeMismatch I1∘I2
+
+    @testset "Pretty printing" begin
+        @test repr(MIME("text/plain"), IdentityTensor{Float64}(5)) == "IdentityTensor{Float64}(5)"
+        @test repr(MIME("text/plain"), IdentityTensor{Int}(4,5)) == "IdentityTensor{Int64}(4,5)"
+
+        @test repr(MIME("text/plain"), IdentityTensor{Float64}(5), context=:comact=>true) == "I(5)"
+        @test repr(MIME("text/plain"), IdentityTensor{Int}(4,5), context=:comact=>true) == "I(4,5)"
+    end
 end