How to print arrays inside functions?


#1

It is useful to peak at an array when debugging a function. However, the macro @show prints arrays like vectors. How to print arrays in the correct REPL way?

zeros(5, 5)
#>5×5 Array{Float64,2}:
#> 0.0  0.0  0.0  0.0  0.0
#> 0.0  0.0  0.0  0.0  0.0
#> 0.0  0.0  0.0  0.0  0.0
#> 0.0  0.0  0.0  0.0  0.0
#> 0.0  0.0  0.0  0.0  0.0
function f(x)
   @show x
   return nothing
end
f(zeros(5, 5))
#> x = [0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0]

Overload `show` for array of custom types
#2

I also was looking for it at some point, it’s the Base.showarray function.

julia> Base.showarray(STDOUT,rand(3,3),false)
3×3 Array{Float64,2}:
 0.935581  0.591996  0.532917
 0.130619  0.385825  0.580087
 0.486295  0.323777  0.920276

#3

Thanks! Do you know why it’s not the default — i.e. why we don’t have

show(io::IO, X::AbstractArray) = showarray(io, X, false)

instead of this line?


#4

When a type supports both single-line output (for things like println) and a more-verbose multi-line format, then the former is via show(io, x) and the latter is via show(io, "text/plain", x) … see: https://docs.julialang.org/en/latest/manual/types/#Custom-pretty-printing-1

So, in this case you can do:

julia> show(STDOUT, "text/plain", rand(3,3))
3×3 Array{Float64,2}:
 0.559556  0.520583  0.64307 
 0.300074  0.833891  0.478954
 0.266339  0.558122  0.912018

instead of using the undocumented internal Base.showarray function.

Note that show(io, "text/plain", x) is a shortcut for show(io, MIME("text/plain"), x).


#5

I always get confused by all the printing functions, but in the end I do display(A) in my tests, is this not the way to go?


#6

There’s nothing wrong with calling the display function in your program. But you shouldn’t call it in show(io, x), because show is different:

  • show has to write specifically to io, whereas display outputs to whatever display handler is registered.

  • show(io, x) should always output in text/plain format, whereas the display backend may choose a richer format to display if available.


#7

OK. It’s worth mentioning that, when I search for “julia print arrays” on google, a ton of suggestions come up (show, println, print, show, showall, showcompact and @show), none of which do what I want (pretty-printing an array as the REPL does).

Coupled with the lack of a debugger, this makes for a very frustrating experience for a casual user that wants to port their script from matlab and wants to print an array inside a loop to check it’s correct. So, if someone finds this topic by searching on google for “julia print arrays”, here’s the answer you’re looking for: display.


#8

If you look in the manual for “pretty printing,” you’ll find the section explaining the difference between show(io, x) (terse one-line display, used e.g. in print) and show(io, "text/plain", x) (multiline verbose pretty printing, used e.g. in REPL output and display).

The three-argument show may be preferable to display if you specifically want text output (display can use e.g. graphical output for some types if the UI supports it, though you can also supply a MIME type to display) or if you want to output to a specific file/stream/buffer (not STDOUT).