Multidimensional arrays in Go, how to ravel?

1,742 views
Skip to first unread message

Ali Ali

unread,
Dec 18, 2012, 12:35:17 PM12/18/12
to golan...@googlegroups.com
Hi All,

Does Go have multidimensional arrays/slices at all, or these are just lists of lists? I mean, lf I have a slice a[][]float64, pointing to an underlying array a0[3][4]float64, will it be a continuous memory, in say row-major order, like in C, or an [3] array of pointers to [4]float64 arrays like in Java?

Next question, is there a way to ravel/resize arrays/slices the way Python's Numpy does? Basicallym I want to be able to make a[x][y] to be aflat[x*y], and vice versa.

How does one get dimensions of a multi-dimensional slice? Simple len(a) returns only first dimension. How does one determine how many are there, and their values?

a := [][]int{ {1,2,3}, {4,5,6} }
fmt.Printf(" a: %T %v %v %v\n", a, a, len(a) )

returns a:    [][]int    [[1 2 3] [4 5 6]]    2

Thanks a lot in advance!

minux

unread,
Dec 18, 2012, 12:43:57 PM12/18/12
to Ali Ali, golan...@googlegroups.com
On Wed, Dec 19, 2012 at 1:35 AM, Ali Ali <alj...@gmail.com> wrote:
Does Go have multidimensional arrays/slices at all, or these are just lists of lists? I mean, lf I have a slice a[][]float64, pointing to an underlying array a0[3][4]float64, will it be a continuous memory, in say row-major order, like in C, or an [3] array of pointers to [4]float64 arrays like in Java?
it seems you confused array with slice in Go's sense.

you can have static multidimensional C-like array with this [5][4]float64.
if you [][]float64, then it's a java like slice of slice of float64s
(however, as it's up to you to allocate the underlying storage for the slice, you can simulate
row-major multidimensional C-like array) 

Next question, is there a way to ravel/resize arrays/slices the way Python's Numpy does? Basicallym I want to be able to make a[x][y] to be aflat[x*y], and vice versa.
no, you can't. are you sure you want to multiply the two indices?  

How does one get dimensions of a multi-dimensional slice? Simple len(a) returns only first dimension. How does one determine how many are there, and their values?
if len(a) > 0, then len(a[0]) will be the length of the 2nd dimension (assume it's created as a regular multidimension
slice, that is, for 0 <= i <= j < len(a), len(a[i]) should be equal to len(a[j]) ).

John Asmuth

unread,
Dec 18, 2012, 12:51:26 PM12/18/12
to golan...@googlegroups.com
On Tuesday, December 18, 2012 12:35:17 PM UTC-5, Ali Ali wrote:
Next question, is there a way to ravel/resize arrays/slices the way Python's Numpy does? Basicallym I want to be able to make a[x][y] to be aflat[x*y], and vice versa.

Usually it would be a[x][y] = aflat[x+y*width], since x*y has the same value for different (x,y) pairs. 

Ali Ali

unread,
Dec 18, 2012, 1:41:13 PM12/18/12
to golan...@googlegroups.com, Ali Ali

On Tuesday, December 18, 2012 11:43:57 AM UTC-6, minux wrote:

On Wed, Dec 19, 2012 at 1:35 AM, Ali Ali <alj...@gmail.com> wrote:
Does Go have multidimensional arrays/slices at all, or these are just lists of lists? I mean, lf I have a slice a[][]float64, pointing to an underlying array a0[3][4]float64, will it be a continuous memory, in say row-major order, like in C, or an [3] array of pointers to [4]float64 arrays like in Java?
it seems you confused array with slice in Go's sense.

It suddenly seems that Go's design has a confusing implications for multidimension arrays/slices. Which are not clearly thought about or documented.

Next question, is there a way to ravel/resize arrays/slices the way Python's Numpy does? Basicallym I want to be able to make a[x][y] to be aflat[x*y], and vice versa.
no, you can't. are you sure you want to multiply the two indices?  


I am sure that I want to multiply two dimensions, not indexes.

Ali Ali

unread,
Dec 18, 2012, 1:43:22 PM12/18/12
to golan...@googlegroups.com

Of course I meant dimensions, not indexes. But so far it seems that the answer is "No, Go doesnt have multidimentional arrays (called slices) but has lists of lists like Java" . Which is kind of,  limiting.

John Asmuth

unread,
Dec 18, 2012, 1:46:32 PM12/18/12
to golan...@googlegroups.com
On Tuesday, December 18, 2012 1:43:22 PM UTC-5, Ali Ali wrote:
Of course I meant dimensions, not indexes. But so far it seems that the answer is "No, Go doesnt have multidimentional arrays (called slices) but has lists of lists like Java" . Which is kind of,  limiting.

I'm not sure "of course" is the right term here... I have no idea what you mean with "dimensions". Also, it's not particularly limiting. It's quite straightforward to use row-major flatten arrays for multidimensional stuff.
 

Ali Ali

unread,
Dec 18, 2012, 1:47:31 PM12/18/12
to golan...@googlegroups.com, Ali Ali


On Tuesday, December 18, 2012 11:43:57 AM UTC-6, minux wrote:

Thank you, this worked. 

minux

unread,
Dec 18, 2012, 1:48:14 PM12/18/12
to Ali Ali, golan...@googlegroups.com
On Wed, Dec 19, 2012 at 2:41 AM, Ali Ali <alj...@gmail.com> wrote:
On Tuesday, December 18, 2012 11:43:57 AM UTC-6, minux wrote:
On Wed, Dec 19, 2012 at 1:35 AM, Ali Ali <alj...@gmail.com> wrote:
Does Go have multidimensional arrays/slices at all, or these are just lists of lists? I mean, lf I have a slice a[][]float64, pointing to an underlying array a0[3][4]float64, will it be a continuous memory, in say row-major order, like in C, or an [3] array of pointers to [4]float64 arrays like in Java?
it seems you confused array with slice in Go's sense.

It suddenly seems that Go's design has a confusing implications for multidimension arrays/slices. Which are not clearly thought about or documented.
i really don't think so. Go's slice mechanism is in fact well designed.

Next question, is there a way to ravel/resize arrays/slices the way Python's Numpy does? Basicallym I want to be able to make a[x][y] to be aflat[x*y], and vice versa.
no, you can't. are you sure you want to multiply the two indices?  

I am sure that I want to multiply two dimensions, not indexes.
what? what does "multiply two dimensions" mean? could you please explain a little?

minux

unread,
Dec 18, 2012, 1:55:57 PM12/18/12
to Ali Ali, golan...@googlegroups.com
Go has pointers and a pretty good inliner so you can write a function to simulate your multiple dimension
array like access.

Also, I don't understand why slice of slice of float64 is limiting here, the underlying storage could be
in row-major layout as in the C case.

Another thing to note is that, this is in fact pretty similar to the C world, e.g. 
when you say "int a[4][5]" in C, it's just like "var a [4][5]int" in Go; when you dynamically allocate a
multidimensional array in C, in effect, you're still allocating an array of array of some type.
If you want to avoid that indirection, you can only say, int (*a)[4][5], which is an array of [4][5], and
it's just like "var a [][4][5]int" in Go. Thus I don't really understand why you say Go is limiting in this.

Miki Tebeka

unread,
Dec 18, 2012, 2:16:10 PM12/18/12
to golan...@googlegroups.com
Maybe you can do what NumPy does, which is a flat array and a flexible indexing scheme (stride ...)

Ali Ali

unread,
Dec 18, 2012, 2:28:26 PM12/18/12
to golan...@googlegroups.com, Ali Ali


On Tuesday, December 18, 2012 11:43:57 AM UTC-6, minux wrote:

On Wed, Dec 19, 2012 at 1:35 AM, Ali Ali <alj...@gmail.com> wrote:
Does Go have multidimensional arrays/slices at all, or these are just lists of lists? I mean, lf I have a slice a[][]float64, pointing to an underlying array a0[3][4]float64, will it be a continuous memory, in say row-major order, like in C, or an [3] array of pointers to [4]float64 arrays like in Java?
it seems you confused array with slice in Go's sense.

I do confuse things, you are right.
How does one assign a [][] slice to [x][y] array?

a0 := [2][3]int{ {1,2,3}, {4,5,6} }
a := [][]int{ {1,1,1}, {2,2,2} }
a = a0[:][:]

Fails because " cannot use a0[:][:] (type [][3]int) as type [][]int in assignment"

 

Ali Ali

unread,
Dec 18, 2012, 2:28:48 PM12/18/12
to golan...@googlegroups.com

Ali Ali

unread,
Dec 18, 2012, 2:31:59 PM12/18/12
to golan...@googlegroups.com, Ali Ali

If you have a square, X height and Y width, multiplying these dimensions X*Y gives you its area.  Similarly, a linear array of X*Y holds same number of elements as a matrix with dimensions (X,Y).

Ali Ali

unread,
Dec 18, 2012, 2:33:02 PM12/18/12
to golan...@googlegroups.com

It is just error prone and unnatural.

Svip

unread,
Dec 18, 2012, 2:42:19 PM12/18/12
to Ali Ali, golan...@googlegroups.com
On 18 December 2012 20:31, Ali Ali <alj...@gmail.com> wrote:

> If you have a square, X height and Y width,

That's not a square, that's a rectangle.

> multiplying these dimensions X*Y
> gives you its area. Similarly, a linear array of X*Y holds same number of
> elements as a matrix with dimensions (X,Y).

Yes, but in most rectangles both (1,2) and (2,1) are going to be sound
coordinates. But 1*2 == 2*1, which ruins your ability to transfer the
coordinates that easily. A better solution is to use X*width+Y
instead, as that should always provide a unique result for each
coordinate.

Dan Kortschak

unread,
Dec 18, 2012, 4:19:15 PM12/18/12
to Ali Ali, golan...@googlegroups.com, Ali Ali

Dan Kortschak

unread,
Dec 18, 2012, 4:22:49 PM12/18/12
to Dan Kortschak, Ali Ali, golan...@googlegroups.com
And because the question seems to be at odds with the snippet:

http://play.golang.org/p/FiUzmh3NdV
> --
>
>

minux

unread,
Dec 18, 2012, 4:24:47 PM12/18/12
to Dan Kortschak, Ali Ali, golan...@googlegroups.com
On Wed, Dec 19, 2012 at 5:19 AM, Dan Kortschak <dan.ko...@adelaide.edu.au> wrote:
http://play.golang.org/p/sSFonPTvxl
this is slightly wrong, as you're reusing the same r at each iteration.

either you just use the index,
or copy the elements.

Dan Kortschak

unread,
Dec 18, 2012, 5:37:01 PM12/18/12
to minux, Ali Ali, golan...@googlegroups.com
Bit me again. Yes, I intended the use of index to avoid the copy (which
I didn't avoid because of the value, anyway).

thanks

David DENG

unread,
Dec 18, 2012, 6:29:29 PM12/18/12
to golan...@googlegroups.com
Is this what you want for the second question?


David

Job van der Zwan

unread,
Dec 18, 2012, 7:08:42 PM12/18/12
to golan...@googlegroups.com
On Tuesday, 18 December 2012 20:16:10 UTC+1, Miki Tebeka wrote:
Maybe you can do what NumPy does, which is a flat array and a flexible indexing scheme (stride ...)

You mean like what the image package does?
 
Reply all
Reply to author
Forward
0 new messages