Mercurial > repos > public > sbplib_julia
changeset 1110:c0bff9f6e0fb feature/lazy_tensors/pretty_printing
Add show(io,x) methods
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 23 May 2022 07:20:27 +0200 |
parents | 84820d4780fa |
children | |
files | Notes.md src/LazyTensors/tensor_types.jl test/LazyTensors/tensor_types_test.jl |
diffstat | 3 files changed, 42 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/Notes.md Thu May 12 22:24:51 2022 +0200 +++ b/Notes.md Mon May 23 07:20:27 2022 +0200 @@ -71,6 +71,14 @@ dictionary-structure containing stencils, tuples, scalars and other types ready for input to the methods creating the operators. +## Pretty printing +From the documentation of `show` it seems that the correct way to setup +printing of instances is to let `show(x)` print valid julia code for creating +the instance if possible. With a specified MIME type +`show(::IO, ::MIME"text/plain", x)` should print a human readable, possible +more verbose version. Compare to what the standard library does for regular +vectors. + ## Variable second derivative 2020-12-08 after discussion with Vidar:
--- a/src/LazyTensors/tensor_types.jl Thu May 12 22:24:51 2022 +0200 +++ b/src/LazyTensors/tensor_types.jl Mon May 23 07:20:27 2022 +0200 @@ -18,7 +18,7 @@ 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 +function Base.show(io::IO, tm::IdentityTensor{T}) where T if get(io, :compact, false) print(io, "I") else @@ -46,13 +46,13 @@ LazyTensors.range_size(m::ScalingTensor) = m.size LazyTensors.domain_size(m::ScalingTensor) = m.size -function Base.show(io::IO, ::MIME"text/plain", tm::ScalingTensor{T}) where T +function Base.show(io::IO, tm::ScalingTensor{T}) where T if get(io, :compact, false) print(io, "$(tm.λ)*I(") join(io, tm.size, ",") print(io, ")") else - print(io, "ScalingTensor{$T}(") + print(io, "ScalingTensor(") print(io, tm.λ, ", ") print(io, tm.size) print(io, ")") @@ -75,12 +75,18 @@ LazyTensors.apply(tm::DiagonalTensor{T,D}, v::AbstractArray{<:Any,D}, I::Vararg{Any,D}) where {T,D} = tm.diagonal[I...]*v[I...] LazyTensors.apply_transpose(tm::DiagonalTensor{T,D}, v::AbstractArray{<:Any,D}, I::Vararg{Any,D}) where {T,D} = tm.diagonal[I...]*v[I...] -function Base.show(io::IO, ::MIME"text/plain", tm::DiagonalTensor) +function Base.show(io::IO, tm::DiagonalTensor) print(io, "DiagonalTensor(") print(io, tm.diagonal) print(io, ")") end +function Base.show(io::IO, ::MIME"text/plain", tm::DiagonalTensor{T,D}) where {T,D} + println(io, "DiagonalTensor{$T, $D}:") + Base.print_array(io, tm.diagonal) + print(io, "\n") +end + """ DenseTensor{T,R,D,...}(A, range_indicies, domain_indicies)
--- a/test/LazyTensors/tensor_types_test.jl Thu May 12 22:24:51 2022 +0200 +++ b/test/LazyTensors/tensor_types_test.jl Mon May 23 07:20:27 2022 +0200 @@ -44,6 +44,9 @@ @test_throws DomainSizeMismatch I1∘I2 @testset "Pretty printing" begin + @test repr(IdentityTensor{Float64}(5)) == "IdentityTensor{Float64}(5)" + @test repr(IdentityTensor{Int}(4,5)) == "IdentityTensor{Int64}(4,5)" + @test repr(MIME("text/plain"), IdentityTensor{Float64}(5)) == "IdentityTensor{Float64}(5)" @test repr(MIME("text/plain"), IdentityTensor{Int}(4,5)) == "IdentityTensor{Int64}(4,5)" @@ -67,8 +70,11 @@ @inferred (st'*v)[2,2] @testset "Pretty printing" begin - @test repr(MIME("text/plain"), ScalingTensor(2., (5,))) == "ScalingTensor{Float64}(2.0, (5,))" # TODO: Can we make this nicer? - @test repr(MIME("text/plain"), ScalingTensor(3, (4,5))) == "ScalingTensor{Int64}(3, (4, 5))" + @test repr(ScalingTensor(2., (5,))) == "ScalingTensor(2.0, (5,))" + @test repr(ScalingTensor(3, (4,5))) == "ScalingTensor(3, (4, 5))" + + @test repr(MIME("text/plain"), ScalingTensor(2., (5,))) == "ScalingTensor(2.0, (5,))" + @test repr(MIME("text/plain"), ScalingTensor(3, (4,5))) == "ScalingTensor(3, (4, 5))" @test repr(MIME("text/plain"), ScalingTensor(4., (5,)), context=:compact=>true) == "4.0*I(5)" @test repr(MIME("text/plain"), ScalingTensor(2, (4,5)), context=:compact=>true) == "2*I(4,5)" @@ -117,8 +123,22 @@ @testset "Pretty printing" begin - @test repr(MIME("text/plain"), DiagonalTensor([1,2,3,4])) == "DiagonalTensor([1, 2, 3, 4])" - @test repr(MIME("text/plain"), DiagonalTensor([1.,1.,1.])) == "DiagonalTensor([1.0, 1.0, 1.0])" + @test repr(DiagonalTensor([1,2,3,4])) == "DiagonalTensor([1, 2, 3, 4])" + @test repr(DiagonalTensor([1.,1.,1.])) == "DiagonalTensor([1.0, 1.0, 1.0])" + + @test repr(MIME("text/plain"), DiagonalTensor([1,2,3,4])) == """ + DiagonalTensor{Int64, 1}: + 1 + 2 + 3 + 4 + """ + @test repr(MIME("text/plain"), DiagonalTensor([1.,1.,1.])) == """ + DiagonalTensor{Float64, 1}: + 1.0 + 1.0 + 1.0 + """ end end