Not sure to understand how matrices are displayed

Lecrapouille
Thu, 15 Apr 2021 21:32:23 +0000
General Usage

Hi !

At least, since Julia 1.3, still now with Julia 1.6 I did not know how to fix misaligned simple matrices.

julia> A=[45 "m" 6; 4.5 5 't']
2×3 Matrix{Any}:
 45     "m"  6
  4.5  5      't'

This is an issue for my package. Here an abstract:

using Printf

struct MP{T <: Real} <: Real
    λ::T
end

global mpstyle = 2

function Base.show(io::IO, x::MP{T}) where T
    if mpstyle == 0
        show(io, x.λ)
    elseif x.λ == -Inf
        (mpstyle == 1 || mpstyle == 2) ? (@printf io ".") : (@printf io "ε")
    elseif x.λ == zero(T)
        (mpstyle == 1 || mpstyle == 3) ? (@printf io "0") : (@printf io "e")
    elseif x.λ == trunc(x.λ)
        (@printf io "%d" x.λ)
    else
        show(io, x.λ)
    end
end

will display:

julia> mpstyle = 2;

julia> [MP(-Inf) MP(0); MP(0) MP(-Inf)]
2×2 Matrix{MP}:
 .  e
 e  .

julia> mpstyle = 1;

julia> [MP(-Inf) MP(0); MP(0) MP(-Inf)]
2×2 Matrix{MP}:
  .  0
 0    .

julia> mpstyle = 4;

julia> [MP(-Inf) MP(0); MP(0) MP(-Inf)]
2×2 Matrix{MP}:
 ε    e
  e  ε

How to fix my show() function to fix misalignment ?

Thanks !

9 posts - 2 participants