The Challenge of R

Here's a coding challenge you.

The R language has the following nifty features for working with arrays. To
what degree can you get Ruby to do the same?

The solution that is closest/nicest gets forever embedded in my growing
library of Ruby enhancements, and it's author's name wrapped in shiny
asterisks and number signs! Joy, joy! :slight_smile:

Listing 1. Elementwise operations in R

a = 1:10 # Range of numbers 1 through 10
b = a+5 # Add 5 to each element
b # Show b vector

[1] 6 7 8 9 10 11 12 13 14 15

Or you can operate selectively only on elements with certain indices, by using
an "index array":

Listing 2. Using index arrays to select elements

c = b # Make a (lazy) copy of b
pos = c(1,2,3,5,7) # Change the prime indices
c[pos] = c[pos]*10 # Reassign to indexed positions
c # Show c vector

[1] 60 70 80 9 100 11 120 13 14 15

Or, maybe best of all, you can use a syntax much akin to list comprehensions
in Haskell or Python, and only operate on elements that have a desired
property:

Listing 3. Using predicates to select elements

d = c
d[d %% 2 == 0] = -1 # Reassign -1 to all even elements
d

[1] -1 -1 -1 9 -1 11 -1 13 -1 15

路路路

---

Note, I've gotten pretty far on my own solution, but can't seem to get passed
a certain point --the Functor class I presented a week or so ago has proven
useful.

Finally special thanks to Martin DeMello who got me hell bent on this :wink:

T.

trans. (T. Onoma) ha scritto:

Here's a coding challenge you.

The R language has the following nifty features for working with arrays. To what degree can you get Ruby to do the same?

Interesting to see people playing with array programming in ruby..
Sometimes I wish I had F-Script like array operations..

btw, nothing ot say about this problem, but notice that there is access to the R library from ruby: http://raa.ruby-lang.org/project/ruby-rmathlib/

i did not write it, but narray does all that you want, and more, including
being extremely fast, doing simply display of data in X, and working
seamlessly with memory mapping:

   jib:~ > cat a.rb
   require 'narray'

   a = NArray[1..10]
   b = a + 5
   p b

   c = b
   pos = 0,1,2,4,6 # adjust R's 1 based idx to ruby's 0 based
   c[pos] = c[pos] * 10
   p c

   d = c
   d[(d % 2).eq(0).where] = -1
   p d

   jib:~ > ruby a.rb
   NArray.int(10):
   [ 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
   NArray.int(10):
   [ 60, 70, 80, 9, 100, 11, 120, 13, 14, 15 ]
   NArray.int(10):
   [ -1, -1, -1, 9, -1, 11, -1, 13, -1, 15 ]

you can thank Masahiro Tanaka for such a great work.

http://www.ir.isas.ac.jp/~masa/ruby/index-e.html

kind regards.

-a

路路路

On Wed, 13 Oct 2004, trans. (T. Onoma) wrote:

Here's a coding challenge you.

The R language has the following nifty features for working with arrays. To
what degree can you get Ruby to do the same?

The solution that is closest/nicest gets forever embedded in my growing
library of Ruby enhancements, and it's author's name wrapped in shiny
asterisks and number signs! Joy, joy! :slight_smile:

Listing 1. Elementwise operations in R

a = 1:10 # Range of numbers 1 through 10
b = a+5 # Add 5 to each element
b # Show b vector

[1] 6 7 8 9 10 11 12 13 14 15

Or you can operate selectively only on elements with certain indices, by using
an "index array":

Listing 2. Using index arrays to select elements

c = b # Make a (lazy) copy of b
pos = c(1,2,3,5,7) # Change the prime indices
c[pos] = c[pos]*10 # Reassign to indexed positions
c # Show c vector

[1] 60 70 80 9 100 11 120 13 14 15

Or, maybe best of all, you can use a syntax much akin to list comprehensions
in Haskell or Python, and only operate on elements that have a desired
property:

Listing 3. Using predicates to select elements

d = c
d[d %% 2 == 0] = -1 # Reassign -1 to all even elements
d

[1] -1 -1 -1 9 -1 11 -1 13 -1 15

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
When you do something, you should burn yourself completely, like a good
bonfire, leaving no trace of yourself. --Shunryu Suzuki

===============================================================================

trans. (T. Onoma) wrote:

Here's a coding challenge you.

The R language has the following nifty features for working with arrays. To what degree can you get Ruby to do the same?

Here's my take. I haven't wrapped it up in a nice module, but it solves the sample problems you gave:

class Elements
  include Enumerable
  
  attr_reader :array
  
  def initialize(array, indices = nil)
    @array = array
    @indices = indices
    @indices = (0...@array.size).to_a unless @indices
  end
  
  def method_missing(symbol, *args)
    res =
    @indices.each {|i|
      res << @array[i].send(symbol, *args)
    }
    return Elements.new(res)
  end
  
  def to_s
    res = ""
    @indices.each {|i|
      res << @array[i].to_s << " "
    }
    return res
  end
  
  def indices(x)
    res =
    if x.respond_to?(:call)
      @indices.each {|i| res << i if x.call(@array[i])}
    elsif x.respond_to?(:each)
      x.each {|i| res << @indices[i] }
    else
      res << x
    end
    return res
  end
  
  def (x)
    return Elements.new(@array, indices(x))
  end
  
  def = (x,y)
    ind = indices(x)
    if y.respond_to?(:each_with_index)
      y.each_with_index {|v, i| @array[ ind[i] ] = v }
    else
      ind.each {|i| @array[i] = y}
    end
  end
  
  def each
    @indices.each {|i| yield @array[i]}
  end
end

class Array
  def elements
    return Elements.new(self)
  end
end

a = (1..10).to_a
a = a.elements

b = a + 5
puts b

c = b
pos = [0, 1, 2, 4, 6]
c[pos] = c[pos] * 10
puts c

d = c
d[ proc {|x| x % 2 == 0} ] = -1
puts d

trans. (T. Onoma) ha scritto:
> Here's a coding challenge you.
>
> The R language has the following nifty features for working with arrays.
> To what degree can you get Ruby to do the same?

Interesting to see people playing with array programming in ruby..
Sometimes I wish I had F-Script like array operations..

Care to elaborate?

btw, nothing ot say about this problem, but notice that there is access
to the R library from ruby: http://raa.ruby-lang.org/project/ruby-rmathlib/

And I discovered R for Ruby! Nice.

Thanks,
T.

路路路

On Wednesday 13 October 2004 03:59 am, gabriele renzi wrote:

i did not write it, but narray does all that you want, and more, including
being extremely fast, doing simply display of data in X, and working
seamlessly with memory mapping:

I did not know NArray worked elementwise. Nice. For numbers at least, that's
one hell of a solution Ara :wink:

   jib:~ > cat a.rb
   require 'narray'

   a = NArray[1..10]
   b = a + 5
   p b

   c = b
   pos = 0,1,2,4,6 # adjust R's 1 based idx to ruby's 0 based
   c[pos] = c[pos] * 10
   p c

   d = c
   d[(d % 2).eq(0).where] = -1
   p d

   jib:~ > ruby a.rb
   NArray.int(10):
   [ 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
   NArray.int(10):
   [ 60, 70, 80, 9, 100, 11, 120, 13, 14, 15 ]
   NArray.int(10):
   [ -1, -1, -1, 9, -1, 11, -1, 13, -1, 15 ]

you can thank Masahiro Tanaka for such a great work.

http://www.ir.isas.ac.jp/~masa/ruby/index-e.html

Double thanks!

T.

路路路

On Wednesday 13 October 2004 04:24 am, Ara.T.Howard@noaa.gov wrote:

Niklas,

I'm still working on how to incorporate your code into my lib, but you get the
glorious prize :wink: Here's your star spangled banner!

路路路

#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
# SPECIAL THANKS TO #
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
# #
# * * * Niklas Frykholm * * * #
# * * * #
# * * * #
# * #
# #
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#

Nice work!
T.

it also works with any objects:

   jib:~ > cat b.rb
   require 'narray'
   na = NArray::to_na ['4', '7', '9', '2']
   puts(na[(na < '5').where].to_a.join)

   jib:~ > ruby b.rb
   42

cheers.

-a

路路路

On Wed, 13 Oct 2004, trans. (T. Onoma) wrote:

I did not know NArray worked elementwise. Nice. For numbers at least, that's
one hell of a solution Ara :wink:

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
When you do something, you should burn yourself completely, like a good
bonfire, leaving no trace of yourself. --Shunryu Suzuki

===============================================================================

trans. (T. Onoma) ha scritto:

路路路

On Wednesday 13 October 2004 03:59 am, gabriele renzi wrote:
> trans. (T. Onoma) ha scritto:
> > Here's a coding challenge you.
> >
> > The R language has the following nifty features for working with arrays.
> > To what degree can you get Ruby to do the same?
>
> Interesting to see people playing with array programming in ruby..
> Sometimes I wish I had F-Script like array operations..

Care to elaborate?

you can read what I mean here (better than what I can explain):
http://www.fscript.org/download/OOPAL.pdf

maybe on ruby-suby there is space for some debate about it, but notice that I just said "sometimes" :wink:

Oh, even better. Then what does N stand for? I thought it was Numeric.

Moreover, what are the important differences between Array and NArray. Why
does Ruby choose Array over NArray?

Thanks,
T.

路路路

On Wednesday 13 October 2004 12:05 pm, Ara.T.Howard@noaa.gov wrote:

On Wed, 13 Oct 2004, trans. (T. Onoma) wrote:
> I did not know NArray worked elementwise. Nice. For numbers at least,
> that's one hell of a solution Ara :wink:

it also works with any objects:

   jib:~ > cat b.rb
   require 'narray'
   na = NArray::to_na ['4', '7', '9', '2']
   puts(na[(na < '5').where].to_a.join)

   jib:~ > ruby b.rb
   42

Debate? Don't think so. It's suby-ruby, btw.

Thanks for the link though. I'll have a look.
T.

路路路

On Wednesday 13 October 2004 06:39 pm, gabriele renzi wrote:

trans. (T. Onoma) ha scritto:
> On Wednesday 13 October 2004 03:59 am, gabriele renzi wrote:
> > trans. (T. Onoma) ha scritto:
> > > Here's a coding challenge you.
> > >
> > > The R language has the following nifty features for working with
> > > arrays. To what degree can you get Ruby to do the same?
> >
> > Interesting to see people playing with array programming in ruby..
> > Sometimes I wish I had F-Script like array operations..
>
> Care to elaborate?

you can read what I mean here (better than what I can explain):
http://www.fscript.org/download/OOPAL.pdf

maybe on ruby-suby there is space for some debate about it, but notice
that I just said "sometimes" :wink: