FAQ
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!

--

Search Discussions

  • Minux at Dec 18, 2012 at 5:44 pm

    On Wed, Dec 19, 2012 at 1:35 AM, Ali Ali 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]) ).

    --
  • Ali Ali at Dec 18, 2012 at 6:41 pm

    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 <javascript:>>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.

    --
  • Minux at Dec 18, 2012 at 6:48 pm

    On Wed, Dec 19, 2012 at 2:41 AM, Ali Ali 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 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?

    --
  • Ali Ali at Dec 18, 2012 at 7:32 pm

    On Tuesday, December 18, 2012 12:48:14 PM UTC-6, minux wrote:

    On Wed, Dec 19, 2012 at 2:41 AM, Ali Ali <alj...@gmail.com <javascript:>>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 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?
    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).

    --
  • Svip at Dec 18, 2012 at 7:58 pm

    On 18 December 2012 20:31, Ali Ali 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.

    --
  • Ali Ali at Dec 18, 2012 at 6:47 pm

    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 <javascript:>>wrote:
    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]) ).
    Thank you, this worked.

    --
  • Ali Ali at Dec 18, 2012 at 7:28 pm

    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 <javascript:>>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"


    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)
    --
  • Dan Kortschak at Dec 18, 2012 at 9:19 pm
    http://play.golang.org/p/sSFonPTvxl
    On 19/12/2012, at 6:02 AM, "Ali Ali" wrote:

    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"
    --
  • Dan Kortschak at Dec 18, 2012 at 9:22 pm
    And because the question seems to be at odds with the snippet:

    http://play.golang.org/p/FiUzmh3NdV
    On 19/12/2012, at 7:49 AM, "Dan Kortschak" wrote:

    http://play.golang.org/p/sSFonPTvxl
    On 19/12/2012, at 6:02 AM, "Ali Ali" wrote:

    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"
    --
    --
  • Minux at Dec 18, 2012 at 9:25 pm

    On Wed, Dec 19, 2012 at 5:19 AM, Dan Kortschak 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,
    http://play.golang.org/p/vYeoPg_E9v
    or copy the elements.

    --
  • Dan Kortschak at Dec 18, 2012 at 10:37 pm
    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
    On Wed, 2012-12-19 at 05:24 +0800, minux wrote:
    either you just use the index,
    http://play.golang.org/p/vYeoPg_E9v

    --
  • John Asmuth at Dec 18, 2012 at 5:51 pm

    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 at Dec 18, 2012 at 6:43 pm

    On Tuesday, December 18, 2012 11:51:26 AM UTC-6, John Asmuth wrote:
    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.
    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 at Dec 18, 2012 at 6:46 pm

    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 at Dec 18, 2012 at 7:33 pm

    On Tuesday, December 18, 2012 12:46:32 PM UTC-6, John Asmuth wrote:
    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.
    It is just error prone and unnatural.

    --
  • Minux at Dec 18, 2012 at 6:56 pm

    On Wed, Dec 19, 2012 at 2:43 AM, Ali Ali wrote:
    On Tuesday, December 18, 2012 11:51:26 AM UTC-6, John Asmuth wrote:
    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.
    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.
    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 at Dec 18, 2012 at 7:16 pm
    Maybe you can do what NumPy does, which is a flat array and a flexible
    indexing scheme (stride ...)
    On Tuesday, December 18, 2012 9:35:17 AM UTC-8, Ali Ali wrote:

    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!
    --
  • Job van der Zwan at Dec 19, 2012 at 12:08 am

    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?
    http://golang.org/pkg/image/


    --
  • Ali Ali at Dec 18, 2012 at 7:28 pm

    On Tuesday, December 18, 2012 11:35:17 AM UTC-6, Ali Ali wrote:
    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!
    --
  • David DENG at Dec 18, 2012 at 11:29 pm
    Is this what you want for the second question?

    http://play.golang.org/p/stFCBwfm_t

    David
    On Wednesday, December 19, 2012 1:35:17 AM UTC+8, Ali Ali wrote:

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 18, '12 at 5:35p
activeDec 19, '12 at 12:08a
posts21
users8
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase