The Challenge of R

T

trans. (T. Onoma)

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! :)


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 ;)

T.
 
G

gabriele renzi

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/
 
A

Ara.T.Howard

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! :)

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

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
--
===============================================================================
| 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
===============================================================================
 
N

Niklas Frykholm

trans. (T. Onoma) said:
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.send(symbol, *args)
}
return Elements.new(res)
end

def to_s
res = ""
@indices.each {|i|
res << @array.to_s << " "
}
return res
end

def indices(x)
res = []
if x.respond_to?:)call)
@indices.each {|i| res << i if x.call(@array)}
elsif x.respond_to?:)each)
x.each {|i| res << @indices }
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 ] = v }
else
ind.each {|i| @array = y}
end
end

def each
@indices.each {|i| yield @array}
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
 
T

trans. (T. Onoma)

| > 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.
 
T

trans. (T. Onoma)

On Wednesday 13 October 2004 04:24 am, (e-mail address removed) 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 ;)

| 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.
 
A

Ara.T.Howard

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

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
--
===============================================================================
| 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
===============================================================================
 
T

trans. (T. Onoma)

| > I did not know NArray worked elementwise. Nice. For numbers at least,
| > that's one hell of a solution Ara ;)
|
| 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

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.
 
G

gabriele renzi

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" ;)
 
T

trans. (T. Onoma)

| > 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" ;)

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

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

trans. (T. Onoma)

Niklas,

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

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

Nice work!
T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,981
Members
47,582
Latest member
brooksmith

Latest Threads

Top