1 Apr 2009 02:20

Numpy Positional Array

Hello,
I'm trying to make an array of size n*n*2.  It should be of the form:
[[[0,0],[1,0],[2,0],[3,0],[4,0], ... ,[n,0]],
[[0,1],[1,1],[2,1],[3,1],[4,1], ... ,[n,1]],
[[0,2],[1,2],[2,2],[3,2],[4,2], ... ,[n,2]],
[[0,3],[1,3],[2,3],[3,3],[4,3], ... ,[n,3]],
[[0,4],[1,4],[2,4],[3,4],[4,4], ... ,[n,4]],
...   ...   ...   ...   ...   ...   ...
[[0,n],[1,n],[2,n],[3,n],[4,n], ... ,[n,n]]]
Each vec2 represents the x,y position of the vec in the array itself.  I'm completely stuck on implementing this setup in numpy.  Any pointers?

Thanks,
Ian

```_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
```
1 Apr 2009 02:32

Re: Numpy Positional Array

```2009/3/31 Ian Mallett <geometrian <at> gmail.com>:
> Hello,
> I'm trying to make an array of size n*n*2.  It should be of the form:
> [[[0,0],[1,0],[2,0],[3,0],[4,0], ... ,[n,0]],
>  [[0,1],[1,1],[2,1],[3,1],[4,1], ... ,[n,1]],
>  [[0,2],[1,2],[2,2],[3,2],[4,2], ... ,[n,2]],
>  [[0,3],[1,3],[2,3],[3,3],[4,3], ... ,[n,3]],
>  [[0,4],[1,4],[2,4],[3,4],[4,4], ... ,[n,4]],
>    ...   ...   ...   ...   ...   ...   ...
>  [[0,n],[1,n],[2,n],[3,n],[4,n], ... ,[n,n]]]
> Each vec2 represents the x,y position of the vec in the array itself.  I'm
> completely stuck on implementing this setup in numpy.  Any pointers?

How do you want to fill in the array? If you are typing it in
literally into your code, you would do basically the above, without
the ...'s, and wrap it in numpy.array(...).

Otherwise, you can create empty arrays with numpy.empty((n,n,2)), or
filled in versions using zeros() and ones().

--

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
```
1 Apr 2009 02:35

Re: Numpy Positional Array

On Tue, Mar 31, 2009 at 5:32 PM, Robert Kern wrote:

How do you want to fill in the array? If you are typing it in
literally into your code, you would do basically the above, without
the ...'s, and wrap it in numpy.array(...).
I know that, but in some cases, n will be quite large, perhaps 1000 on a side.  I'm trying to generate an array of that form in numpy entirely for speed and aesthetic reasons.

Ian
```_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
```
1 Apr 2009 02:39

Re: Numpy Positional Array

```2009/3/31 Ian Mallett <geometrian <at> gmail.com>:
> On Tue, Mar 31, 2009 at 5:32 PM, Robert Kern <robert.kern <at> gmail.com> wrote:
>>
>> How do you want to fill in the array? If you are typing it in
>> literally into your code, you would do basically the above, without
>> the ...'s, and wrap it in numpy.array(...).
>
> I know that, but in some cases, n will be quite large, perhaps 1000 on a
> side.  I'm trying to generate an array of that form in numpy entirely for
> speed and aesthetic reasons.

Again: How do you want to fill in the array? What is the process that
generates the data?

--

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
```
1 Apr 2009 02:42

Re: Numpy Positional Array

The array follows a pattern: each array of length 2 represents the x,y index of that array within the larger array.

```_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
```
1 Apr 2009 02:48

Re: Numpy Positional Array

```2009/3/31 Ian Mallett <geometrian <at> gmail.com>:
> The array follows a pattern: each array of length 2 represents the x,y index
> of that array within the larger array.

Ah, right. Use dstack(mgrid[0:n,0:n]).

--

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
```
1 Apr 2009 02:51

Re: Numpy Positional Array

Thanks!

```_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
```
1 Apr 2009 02:45

Re: Numpy Positional Array

```
> The array follows a pattern: each array of length 2 represents the x,y
index of that array within the larger array.

Is this what you are after?

>>> numpy.array(list(numpy.ndindex(n,n))).reshape(n,n,2)

--

This message and any attachments are confidential, proprietary, and may be privileged. If this message
was misdirected, Barclays Global Investors (BGI) does not waive any confidentiality or privilege. If
you are not the intended recipient, please notify us immediately and destroy the message without
disclosing its contents to anyone. Any distribution, use or copying of this e-mail or the information it
contains by other than an intended recipient is unauthorized. The views and opinions expressed in this
e-mail message are the author's own and may not reflect the views and opinions of BGI, unless the author is
authorized by BGI to express such views or opinions on its behalf. All email sent to or from this address is
subject to electronic storage and review by BGI. Although BGI operates ant
i-virus programs, it does not accept responsibility for any damage whatsoever caused by viruses being passed.
```
1 Apr 2009 03:02

Re: Numpy Positional Array

Same.  Thanks, too.

```_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
```
1 Apr 2009 03:40

Re: array of matrices

```Robert Kern <robert.kern <at> gmail.com> writes:
> On Sat, Mar 28, 2009 at 23:15, Anne Archibald <peridot.faceted <at> gmail.com>
wrote:
> > 2009/3/28 Geoffrey Irving <irving <at> naml.us>:
> >> On Sat, Mar 28, 2009 at 12:47 AM, Robert Kern <robert.kern <at> gmail.com>
wrote:
> >>> 2009/3/27 Charles R Harris <charlesr.harris <at> gmail.com>:
> >>>>
> >>>> On Fri, Mar 27, 2009 at 4:43 PM, Robert Kern <robert.kern <at> gmail.com>
wrote:
> >>>>>
> >>>>> On Fri, Mar 27, 2009 at 17:38, Bryan Cole <bryan <at> cole.uklinux.net>
wrote:
> >>>>> > I have a number of arrays of shape (N,4,4). I need to perform a
> >>>>> > vectorised matrix-multiplication between pairs of them I.e.
> >>>>> > matrix-multiplication rules for the last two dimensions, usual
> >>>>> > element-wise rule for the 1st dimension (of length N).
> >>>>> >
(...)
> >>
> >> It'd be great if this operation existed as a primitive.
(...)
> >
> > The infrastructure to support such generalized ufuncs has been added
> > to numpy, but as far as I know no functions yet make use of it.
>
> I don't think there is a way to do it in general with dot(). Some
> cases are ambiguous. I think you will need separate matrix-matrix,
> matrix-vector, and vector-vector gufuncs, to coin a term.

By the way, matrix multiplication is one of the testcases for the generalized
ufuncs in numpy 1.3 -- this makes playing around with it easy:

In [1]: N = 10; a = randn(N, 4, 4); b = randn(N, 4, 4)

In [2]: import numpy.core.umath_tests

In [3]: (numpy.core.umath_tests.matrix_multiply(a, b) == [dot(ai, bi) for (ai,
bi) in zip(a, b)]).all()
Out[3]: True

Best, Hansres

_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
```

Gmane