Multi-dimensional (like 2) arrays in Ruby

Does Ruby support other than 1 dimensional arrays?

A Ruby array element can contain any Ruby object, so you can have an
Array of Arrays.

foo = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]

p foo[0][0] # => 1
p foo[2][2] # => 2

-austin

– Austin Ziegler, austin@halostatue.ca on 2002.11.08 at 17.53.23

···

On Sat, 9 Nov 2002 07:42:15 +0900, Ted wrote:

Does Ruby support other than 1 dimensional arrays?

  • Original Message -----
···

From: “Ted” ted@datacomm.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, November 08, 2002 2:42 PM
Subject: Multi-dimensional (like 2) arrays in Ruby

Does Ruby support other than 1 dimensional arrays?

A Ruby array may contain any objects, other arrays among them. Those arrays
may contain still other arrays, and so on.

ary = [ [1, 2], [3, 4] ]
ary[0][0] → 1
ary[0][1] → 2
ary[1][0] → 3
ary[1][1] → 4

Like this?

a = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

p a[0][0] → 1

Massimiliano

···

On Sat, Nov 09, 2002 at 07:42:15AM +0900, Ted wrote:

Does Ruby support other than 1 dimensional arrays?

class Array
def Array.Array dims, default=nil, accum=
if dims.size == 1
return Array.new(dims[0], default)
else
return accum = Array.new(dims.shift, Array.Array(dims, default, accum))
end
end
end

m = Array.Array [4,3,2], 42

p m >>

[[[42, 42], [42, 42], [42, 42]], [[42, 42], [42, 42], [42, 42]], [[42, 42], [42, 42], [42, 42]], [[42, 42], [42, 42], [42, 42]]]

-a

···

On Sat, 9 Nov 2002, Ted wrote:

Does Ruby support other than 1 dimensional arrays?

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

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

and then

m[0][0][0] = 0
p m >>

[[[0, 42], [0, 42], [0, 42]], [[0, 42], [0, 42], [0, 42]], [[0, 42],
[0, 42], [0, 42]], [[0, 42], [0, 42], [0, 42]]]

Please see the other posts to this thread for ways to improve the
code.

Regards,
Pit

···

On 9 Nov 2002 at 11:15, ahoward wrote:

On Sat, 9 Nov 2002, Ted wrote:

Does Ruby support other than 1 dimensional arrays?

class Array
def Array.Array dims, default=nil, accum=
if dims.size == 1
return Array.new(dims[0], default)
else
return accum = Array.new(dims.shift, Array.Array(dims, default,
accum))
end
end
end

m = Array.Array [4,3,2], 42

p m >>

[[[42, 42], [42, 42], [42, 42]], [[42, 42], [42, 42], [42, 42]], [[42,
42], [42, 42], [42, 42]], [[42, 42], [42, 42], [42, 42]]]

Austin Ziegler wrote:

···

On Sat, 9 Nov 2002 07:42:15 +0900, Ted wrote:

Does Ruby support other than 1 dimensional arrays?

A Ruby array element can contain any Ruby object, so you can have an
Array of Arrays.

foo = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]

p foo[0][0] # => 1
p foo[2][2] # => 2

-austin

– Austin Ziegler, austin@halostatue.ca on 2002.11.08 at 17.53.23

It does, but that’s a slow implementation, though flexible. I have been
trying to figure out NArray, and bouncing. This is less flexible, but
much faster. (However it depends on knowing the size of all but the
last dimension of the array at indexing time – like the way that
Fortran does it, only a bit more dynamic.)

I implemented a fixed-size n-dimensional array class in Ruby once
(mostly because I wanted something that supported multidimensional
slices), but it was terribly slow (to the point of uselessness). I might
try it again as a way to learn about C extensions some day.

martin

···

Charles Hixson charleshixsn@earthlink.net wrote:

It does, but that’s a slow implementation, though flexible. I have been
trying to figure out NArray, and bouncing. This is less flexible, but
much faster. (However it depends on knowing the size of all but the
last dimension of the array at indexing time – like the way that
Fortran does it, only a bit more dynamic.)

[snip]

Please see the other posts to this thread for ways to improve the
code.

whoops! my bad. here’s a fix

class Array
def Array.Array dims, default=nil
dim = dims.shift
if dims.size == 0
return Array.new dim, default
else
a = Array.new dim
a.collect!{ Array.Array dims.clone, default }
end
end
end

if $0 == FILE
m = Array.Array [3,2,1], 0
p m # >> [[[0], [0]], [[0], [0]], [[0], [0]]]
m[0][0][0] = 42
m[1][0] = [42]
m[2] = [[42],[0]]
p m # >> [[[42], [0]], [[42], [0]], [[42], [0]]]
end

note that this works with dims.size == n

-a

···

On Sun, 10 Nov 2002, Pit Capitain wrote:

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

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

if one were to wrap some scientifc libraries

GSL - GNU Scientific Library - GNU Project - Free Software Foundation

would be a good way to go. IMHO.

something i plan to get to, next week, or maybe next month, or maybe…

-ara

···

On Sat, 9 Nov 2002 martindemello@yahoo.com wrote:

I implemented a fixed-size n-dimensional array class in Ruby once
(mostly because I wanted something that supported multidimensional
slices), but it was terribly slow (to the point of uselessness). I might
try it again as a way to learn about C extensions some day.

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

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================