Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make #pretty_print actually pretty #52

Closed
agarie opened this issue Dec 9, 2012 · 7 comments
Closed

Make #pretty_print actually pretty #52

agarie opened this issue Dec 9, 2012 · 7 comments

Comments

@agarie
Copy link
Member

agarie commented Dec 9, 2012

I'm trying to come up with some examples of how to use NMatrix and, obviously, #pretty_print/#pp is used lots of times.

I wanted to throw this discussion to see how you guys think NMatrix/NVector should be printed. Any ideas? I'll try to come up with something and implement it, but I don't want to make this kind of change without listening to others.

@ghost ghost assigned agarie Mar 29, 2013
@translunar
Copy link
Member

I cleaned up the code in a recent commit. Have you tried it with pry (which uses coderay)? It looks a little better.

@agarie
Copy link
Member Author

agarie commented Apr 26, 2013

Some experiments in IRB:

> n = NMatrix.random(4)
> n.pp
[[0.6299860224178115, 0.2626926192712231, 0.9008651790526774, 0.7025077672785742]
[0.6629325603072921, 0.657471929860773, 0.9601643345978037, 0.7900181731280271]
[0.22735456931808107, 0.5035327724154458, 0.6728627567703459, 0.9629883418502257]
[0.36004767980910257, 0.6542623614657115, 0.9829866207607914, 0.6299860224178115]]
 => nil 

> u = NVector.random(3)
> u.pretty_print
[0.6652767626356697
0.1188537203789306
0.8368488043457466]
 => nil
> u.pp
 => "#<NVector:0x007fad838f6668 orientation:column shape:[3,1] dtype:float64 stype:dense>" 

The result from pretty_print is correct and acceptable, but obviously pp is wrong. This is probably due to it being an alias to NMatrix#pretty_print; as NVectors have dimension 1, it calls inspect directly. Just aliasing it again in NVector solves the problem:

> class NVector
>   alias :pp :pretty_print
> end
 => nil 
> u.pp
[0.6652767626356697
0.1188537203789306
0.8368488043457466]
 => nil 

I can send a PR with this. But there's one more question: why isn't there a to_s method defined? We could simply use puts in this case. Also, what about displaying it such that the indices appear next to the values? Like:

> n = NMatrix.seq(3)
> u = NVector.random(3)
> puts n
     0   1   2
0   0   1   2
1   3   4   5
2   6   7   8
> puts u
0   0.6652767626356697
1   0.1188537203789306
2   0.8368488043457466

I think this can be useful. If you agree with me, I can implement it during the weekend.

@translunar
Copy link
Member

I don't think to_s should produce multi-line output. I haven't seen any others that do.

@agarie
Copy link
Member Author

agarie commented Apr 28, 2013

Why not? I agree that I've never seen others do that, but in this case it's much better (and useful) than #<NMatrix:0x007fff418a3b38>. Besides, if someone unfamiliar with Ruby (e.g. a scientist trying to use SciRuby tools for his work) wants to see how the operations work in IRB, he might not know the pp library or that NMatrix has the pp method.

In any case, I'll ask other Rubyists' opinions on this :)

@translunar
Copy link
Member

Why not? Again, mainly because I haven't seen any other objects that do multiline to_s output (unless with assistance from pry).

@v0dro
Copy link
Member

v0dro commented Jun 7, 2013

Can we try something like awesome_print?

@translunar
Copy link
Member

No. I think we don't want to complicate NMatrix with additional dependencies, if possible.

@agarie agarie closed this as completed May 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants