From: Larry Evans Date: 2011-01-09T04:35:26+09:00 Subject: Re: Nooby question : multidimensional arrays. On 01/07/11 21:57, Larry Evans wrote: > On 01/07/11 17:56, Kedar Mhaswade wrote: >> Is this an okay job of implementing a multidimensional array? >> >> https://github.com/kedarmhaswade/datacube/blob/master/data_cube.rb >> >> Thank you for any feedback. >> >> -Kedar >> > There's a book: > > http://web.engr.oregonstate.edu/~budd/Books/aplc/ > > which describes an expansion vector which is a scan of > the array sizes. IOW, for: > > arr = Array.new(s0,s1,...sn) > > the expansion vector for this arr is: > > arr.ev = [1, s0, s0*s1, s0*s1*s3,..., s0*s1*...*sn] > The expansion vector for DataCube.new(n,m) would be: [m**0,m**1,m**2,...,m**n] IOW, the length of this expansion vector is n+1. [snip] > > arr[i0,i1,..., ij, ... in] > > is located: > > i0*arr.ev[0]+i1*arr.ev[1]+...+in*arr.ev[n] > > elements from the 1st element: > > arr[0,0,...,0] > > Thus, given the sizes, you can create a member variable > which is the expansion vector, then use that to access the > 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] > However, after looking at: https://github.com/kedarmhaswade/datacube/blob/master/data_cube.rb#L48 it's not clear that to_index calculates the same offset because coefficients[i] corresponds to one of the i0,i1,...,in-1 in the above formulat, and coefficients[i] is not multiplied by anything. It's only added to mega_index*n, and mega_index*n involves multiplication by the dimension, n, instead of ths uniform size, m. HTH -Larry