default visual display of vectorz matrices

38 views
Skip to first unread message

Mars0i

unread,
Jan 12, 2017, 10:37:52 AM1/12/17
to Numerical Clojure
This is not an important issue, but it keeps bugging me a little as I work with core.matrix vectorz matrices in the repl, so I was curious what others might think about it.  When a vectorz matrix is displayed in the repl, the first row is printed after a type identifier, and when subsequent rows are printed, they're lined up with the left margin. 

user=> (matrix :vectorz (matrix [(range 10) (range 10) (range 10)]))
#vectorz/matrix [[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0],
[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0],
[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]]

This means that columns aren't even close to being lined up, while the display nevertheless takes up several lines.  By contrast, persistent-vector, ndarray, and clatrix matrices display all on one line, which doesn't show you relationships between columns, but at least takes up less vertical space on the screen.

user=> (matrix :persistent-vector (matrix [(range 10) (range 10) (range 10)]))
[[0 1 2 3 4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8 9]]

user=> (matrix :ndarray (matrix [(range 10) (range 10) (range 10)]))
#object[clojure.core.matrix.impl.ndarray_object.NDArray 0x4b6c134 "[[0 1 2 3 4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8 9]]"]

user=> (matrix :clatrix (matrix [(range 10) (range 10) (range 10)]))
((0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0) (0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0) (0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))

I know that many matrices are too large to be usefully displayed in the repl, that the point of working with core.matrix is not to have convenient visual representations, in general (aljabar matrices don't display their contents at all), and that the 'pm' function, as well as .toString, are available to generate nicer representations.  Still, with small matrices it can be nice to have a convenient default representation, especially when the matrices are embedded in other data structures that one is examining while experimenting at the repl.  (Of course I could defined a custom printer for any data structure containing matrices that I define, too.)

Printing all vectorz rows on one line is an option.  Another would be to insert a newline immediately after the type identifier, so that all of the data is flush left and columns kind of line up.

I don't know whether anyone else will care about this one way or another, but I'd be curious about others' thoughts.




Mike Anderson

unread,
Jan 13, 2017, 2:47:01 AM1/13/17
to Numerical Clojure
I would normally use c.c.m/pm if I wanted a readable view of a core.matrix array:

(pm [[1.5 2.3] [3.1 -4.0]])

[[1.500  2.300]
 [3.100 -4.000]]

This does nice things like automatically lining up the columns, and you can use custom formatters if you wish

Mars0i

unread,
Jan 13, 2017, 2:59:13 PM1/13/17
to Numerical Clojure

On Friday, January 13, 2017 at 1:47:01 AM UTC-6, Mike Anderson wrote:
I would normally use c.c.m/pm if I wanted a readable view of a core.matrix array:

(pm [[1.5 2.3] [3.1 -4.0]])

[[1.500  2.300]
 [3.100 -4.000]]

This does nice things like automatically lining up the columns, and you can use custom formatters if you wish

I use pm a lot, but it's not helpful for quick browsing of embedded matrices (nor is it intended to for this purpose):

user=> (pm curr-stage)
free_agent.level.Level@f637ab04

user=> curr-stage
#free_agent.level.Level
{:covar #vectorz/matrix [[0.22369828709166198,-8.627576234682892E-6],
[-8.627576234682892E-6,0.2238517460813706]]
 :covar-dt 1.0E-4
 :error #vectorz/matrix [[0.0026006209555344746],
[-0.005571554206934956]]
 :error-dt 0.01
 :gen #<Fn@54221c06 free_agent.snipe_levels/gen>
 :gen' #<Fn@5e8009d2 free_agent.snipe_levels/gen_SINGLEQUOTE_>
 :hypoth #vectorz/matrix [[3.212844081328286],
[0.13557772951257596]]
 :hypoth-dt 1.0E-4
 :learn-adj #vectorz/matrix [[1.0339743028176713,0.03397430281767125],
[-0.47207480759353676,0.5279251924064624]]
 :learn-adj-dt 0.01}


I agree that I can make a custom formatter for such data structures, but there may be many different data structures that one wants to browse at different times.   One of the great things about the Clojure repl is that you can get away with not defining custom print routines very often.  (Compare defrecords to many class instances in Java!) 

I guess I would say that the current display of vectorz matrices is not ideally useful for any purpose, since it's not all on one line, and it's not lined up roughly in columns.

Again, this is not a big deal, but a minor discomfort, though a recurring one.
Reply all
Reply to author
Forward
0 new messages