How can I display all Float64 multidimensional arrays as vectors?

How can I display all Float64 multidimensional arrays as vectors? I tried the following but it stackoverflows:

Base.show(io::IO, x::Array{Float64}) = print(io, vec(x))

Probably it is running into a deadlock.

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

Something like this?

julia> a = rand(2,3,5)
2Ă—3Ă—5 Array{Float64,3}:
[:, :, 1] =
 0.593246  0.122574  0.77908
 0.959088  0.218372  0.0891396

[:, :, 2] =
 0.853314  0.171189  0.313323
 0.195373  0.342458  0.642057

[:, :, 3] =
 0.731841  0.951687  0.316416
 0.640172  0.230144  0.701338

[:, :, 4] =
 0.307028  0.172853  0.990212
 0.872871  0.79285   0.214958

[:, :, 5] =
 0.908459    0.441589  0.0691264
 0.00724457  0.371763  0.968289

julia> a[:]
30-element Array{Float64,1}:
 0.5932464540468072
 0.9590878214304634
 0.12257439090533429
 0.21837219429910104
 0.7790802711397888
 0.08913955642713955
 0.8533144973503481
 0.19537331243008405
 0.1711887305635067
 0.3424581504859132
 0.31332337994899184
 0.6420571681929719
 0.7318410592897324
 â‹®
 0.30702789473558245
 0.8728713274277637
 0.17285287148135597
 0.7928495776848725
 0.9902119213201215
 0.2149577614800653
 0.9084586968143415
 0.007244565168584183
 0.44158904493286766
 0.3717633735726962
 0.06912640765172728
 0.9682886566736622
1 Like

It is not generally recommendable to rewrite the show method for arrays or other basic types (or any other method of functions from Base for types that you didn’t define – see the advice against “type piracy” in the manual). But let aside that, the specific problem in this case is that Array{Float64} includes Array{Float64, 1} (i.e. Vector{Float64}), so the method that you defined calls itself forever.

To make it work, you should also define:

Base.show(io::IO, x::Vector{Float64}) = print(io, x)
2 Likes

I wasn’t aware of the advice against “type piracy”. I guess that the proper way is to make my own print function. Thank you very much.

1 Like