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

Mirror and rotate #22

Closed
dicom opened this issue Sep 20, 2012 · 3 comments
Closed

Mirror and rotate #22

dicom opened this issue Sep 20, 2012 · 3 comments

Comments

@dicom
Copy link

dicom commented Sep 20, 2012

Hello

NArray is a wonderful tool in Ruby. However, one thing that I miss with it is a convenient way of rotating or mirroring an array. It might be that I have missed something in the documentation of course, but as far as I can see, there's no (super-easy) way of doing it. I guess I would like to have convenient NArray#rotate and NArray#mirror methods. Then you could do something like this:


Proposed method 1:
NArray#mirror(dim=0)
This would be principal method (it would work just fine on arrays of any dimension).

a = NArray.int(3,3).indgen!
=> NArray.int(3,3):
[ [ 0, 1, 2 ],
  [ 3, 4, 5 ],
  [ 6, 7, 8 ] ]

a.mirror
=> NArray.int(3,3):
[ [ 2, 1, 0 ],
  [ 5, 4, 3 ],
  [ 8, 7, 6 ] ]

a.mirror(1)
=> NArray.int(3,3):
[ [ 6, 7, 8 ],
  [ 3, 4, 5 ],
  [ 0, 1, 2 ] ]

Proposed method 2:
NArray#rotate(steps)
This would be harder to make principal (it's primarily wanted for two dimensional arrays ('images'), and gets more complicated with vectors or multi-dimensional arrays)

a = NArray.int(3,3).indgen!
=> NArray.int(3,3):
[ [ 0, 1, 2 ],
  [ 3, 4, 5 ],
  [ 6, 7, 8 ] ]

a.rotate(1)
=> NArray.int(3,3):
[ [ 6, 3, 0 ],
  [ 7, 4, 1 ],
  [ 8, 5, 2 ] ]

a.rotate(-1)
=> NArray.int(3,3):
[ [ 2, 5, 8 ],
  [ 1, 4, 7 ],
  [ 0, 3, 6 ] ]

NArray.int(3).indgen!.rotate(1)
=> exception?!

NArray.int(3,3,3).indgen!.rotate(1)
=> exception?!

Thanks for your consideration.

Regards,
Chris

@masa16
Copy link
Owner

masa16 commented Sep 20, 2012

I wrote it as new "reverse" and "rot90" methods.
dae4028#lib/narray_ext.rb

"reverse" is named after Array#reverse, "rot90" is named after Numpy method

$a = NArray.int(3,3).indgen!   
#=> NArray.int(3,3): 
[ [ 0, 1, 2 ], 
  [ 3, 4, 5 ], 
  [ 6, 7, 8 ] ]
$a.reverse                     
#=> NArray.int(3,3): 
[ [ 8, 7, 6 ], 
  [ 5, 4, 3 ], 
  [ 2, 1, 0 ] ]
$a.reverse(0)                  
#=> NArray.int(3,3): 
[ [ 2, 1, 0 ], 
  [ 5, 4, 3 ], 
  [ 8, 7, 6 ] ]
$a.reverse(1)                  
#=> NArray.int(3,3): 
[ [ 6, 7, 8 ], 
  [ 3, 4, 5 ], 
  [ 0, 1, 2 ] ]
$a.rot90                       
#=> NArray.int(3,3): 
[ [ 6, 3, 0 ], 
  [ 7, 4, 1 ], 
  [ 8, 5, 2 ] ]
$a.rot90(2)                    
#=> NArray.int(3,3): 
[ [ 8, 7, 6 ], 
  [ 5, 4, 3 ], 
  [ 2, 1, 0 ] ]
$a.rot90(-1)                   
#=> NArray.int(3,3): 
[ [ 2, 5, 8 ], 
  [ 1, 4, 7 ], 
  [ 0, 3, 6 ] ]

@dicom
Copy link
Author

dicom commented Sep 20, 2012

Thank you Masahiro,
that was extremely responsive!

One minor comment:
In your commit, for the exception in #rot90, you have:

raise "must be >2 dimensional array"

I believe it should be:

raise "must be >=2 dimensional array"

@dicom
Copy link
Author

dicom commented Jan 26, 2013

Closing this issue. Thanks for the implementation.

By the way, it would be nice if you could push the updated documentation file to:
http://narray.rubyforge.org/SPEC.en

@dicom dicom closed this as completed Jan 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants