Nooby question : multidimensional arrays

On 01/08/11 13:33, Larry E. wrote:

There’s a book:
arr.ev = [1, s0, s0s1, s0s1s3,…, s0s1*…*sn]

elements by calculating the offset from the initial element
using the dot product:

i0*arr.ev[0]+i1*arr.ev[1]+...+in*arr.ev[n]

[snip]

To illustrate, the attached produces output:

44
12
[0,0,0]
0
[1,0,0]
1
[0,1,0]
2
[1,1,0]
3
[1,1,2]
11
[ [ [0, 1]
, [2, 3]
]
, [ [4, 5]
, [6, 7]
]
, [ [8, 9]
, [44, 11]
]
]

The last several lines (starting with [ [ [)
show the output to the to_s and were designed
to show (hopefully) the nested subarray’s.

-regards,
Larry

Larry,

Thanks for references and detailed review. I will go over them to
understand them better. Just to reiterate, my implementation is pretty
rudimentary at this point and more importantly, it caters for the cases
where the number of elements in every dimension is assumed to be the
same (IOW, you can’t simply represent a 2x3 matrix is it).

-Kedar

If you need that you could do something like
https://gist.github.com/772827

Cheers

robert

Interesting. Thanks, Robert.
But using hash internally to represent arrays is slightly
counterintuitive. But I agree, it will work well for “sparse”
multi-dimensional arrays. I realize that I could do something similar
and still retain the one-dimensional flattening, in my implementation.
And yes, expanding it to do non-cube entities is on my list.

-Kedar

Thank you for your feedback, again!

I simply started with flattening out the elements into a one-dimensional
array internally (which is quite obvious). I am not sure about its
applications yet. Also, dc[3] does not make sense for this
implementation, unless the DataCube itself is one-dimensional (in which
case it just degenerates to normal array).

So, if you have p dimensions, then the array has to be accessed (or
indexed) using an index like: [x, y, z, …] where the length of this
array itself is p.

That said, I haven’t yet considering “expanding the array” dynamically.
It’s like, you fix the # and size of dimensions upfront and freeze them.
I will address that officially, as you suggest.

I will admit, I haven’t checked the current literature of Data Cube when
implementing it. Maybe I should do that.

My modest expectation was that to address a point in multidimensional
space A[d1,d2,d3 … dn] was a more natural notation than
A[d1][d2][d3]…[dn], because the former is how we would represent it on
paper. And Ruby’s flexibility allowed me to do just that!

Regards,
Kedar

On Mon, Jan 10, 2011 at 4:14 PM, Kedar M.
[email protected] wrote:

If you need that you could do something like
https://gist.github.com/772827

Interesting. Thanks, Robert.
But using hash internally to represent arrays is slightly
counterintuitive.

Well, you don’t see it from the outside. For users of the class it
doesn’t really matter how it internally works (unless they hit some
limitations).

But I agree, it will work well for “sparse”
multi-dimensional arrays.

Exactly that was the use case I had in mind.

I realize that I could do something similar
and still retain the one-dimensional flattening, in my implementation.

How exactly do you want to do that?

And yes, expanding it to do non-cube entities is on my list.

:slight_smile: Actually “cube” is just a special case of the more general one.
I’d rather implement the special case and add a custom constructor for
the cube case:

Cheers

robert

On Mon, Jan 10, 2011 at 2:06 PM, Kedar M.
[email protected] wrote:

Thanks for references and detailed review. I will go over them to
understand them better. Just to reiterate, my implementation is pretty
rudimentary at this point and more importantly, it caters for the cases
where the number of elements in every dimension is assumed to be the
same (IOW, you can’t simply represent a 2x3 matrix is it).

If you need that you could do something like

Cheers

robert

On 01/10/11 08:33, Robert K. wrote:
[snip]

If you need that you could do something like https://gist.github.com/772827

[snip]
Are you using a ruby newer than 1.8.7. I’m getting an error as shown
here:

~/prog_dev/ruby/gist772827-61376303a967918b698bc0ddb50af26d6368438c $
ruby mda.rb
mda.rb:20: syntax error, unexpected tIDENTIFIER, expecting tAMPER or ‘&’
def []=(*idx, val)
^
mda.rb:41: syntax error, unexpected kEND, expecting $end
~/prog_dev/ruby/gist772827-61376303a967918b698bc0ddb50af26d6368438c $
ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]

On Mon, Jan 10, 2011 at 9:15 PM, Larry E. [email protected]
wrote:

def []=(*idx, val)
^
mda.rb:41: syntax error, unexpected kEND, expecting $end
~/prog_dev/ruby/gist772827-61376303a967918b698bc0ddb50af26d6368438c $
ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]

Yes, you need 1.9ish Ruby for this to work. Reason is that 1.9 has
extended pattern matching. With 1.8 you need to do it yourself:

09:14:49 ~$ irb
Ruby version 1.8.7
irb(main):001:0> class X
irb(main):002:1> def []=(*a)
irb(main):003:2> v = a.pop
irb(main):004:2> p a, v
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> X.new[1,2,3]=4
[1, 2, 3]
4
=> 4
irb(main):008:0>

Sorry for not mentioning it.

Kind regards

robert

Robert K. wrote in post #973678:

On Mon, Jan 10, 2011 at 4:14 PM, Kedar M.
[email protected] wrote:

If you need that you could do something like
https://gist.github.com/772827

Interesting. Thanks, Robert.
But using hash internally to represent arrays is slightly
counterintuitive.

Well, you don’t see it from the outside. For users of the class it
doesn’t really matter how it internally works (unless they hit some
limitations).

I meant for a programmer to implement it. And yes, it is a damn good
idea.

But I agree, it will work well for “sparse”
multi-dimensional arrays.

Exactly that was the use case I had in mind.

I realize that I could do something similar
and still retain the one-dimensional flattening, in my implementation.

How exactly do you want to do that?

Grrr. I was not thinking enough. I could make the current implementation
better in that I can lazily expand the array which will fill the
intervening elements with nil’s but yeah, it’s nowhere near the fast
lookup of hashtables.

And yes, expanding it to do non-cube entities is on my list.

:slight_smile: Actually “cube” is just a special case of the more general one.
I’d rather implement the special case and add a custom constructor for
the cube case:

https://gist.github.com/772827

Cheers

robert

On 01/10/11 07:06, Kedar M. wrote:

Larry,

Thanks for references and detailed review. I will go over them to
understand them better. Just to reiterate, my implementation is pretty
rudimentary at this point and more importantly, it caters for the cases
where the number of elements in every dimension is assumed to be the
same (IOW, you can’t simply represent a 2x3 matrix is it).

-Kedar

Kadar,

I was looking through my computers ruby packages and found one:

libnarray-ruby1.9.1

claiming it:

has numerical n-dimensional array class

Googling found:

https://github.com/princelab/narray/wiki/Tentative-NArray-Tutorial

So, I tried it and, it works:

/home/evansl/prog_dev/ruby $ irb1.9.1
irb(main):001:0> require ‘narray’
=> true
irb(main):002:0> a = NArray.int(2,3,4)
=> NArray.int(2,3,4):
[ [ [ 0, 0 ],
[ 0, 0 ],
[ 0, 0 ] ],
[ [ 0, 0 ],
[ 0, 0 ],
[ 0, 0 ] ],
[ [ 0, 0 ],
[ 0, 0 ],
[ 0, 0 ] ],
[ [ 0, 0 ],

irb(main):003:0>

HTH.

-Larry

I know this is an old thread, but I have to respond since the venerable
JEGII didn’t point out the obvious to you guys.

Matrix.build(row_count, col_count) do |row, col|

end

See
http://www.ruby-doc.org/stdlib/libdoc/matrix/rdoc/classes/Matrix.html