[Numpy-discussion] Matrices and arrays of vectors

gary ruben gruben at bigpond.net.au
Fri Feb 24 08:11:59 EST 2012


I haven't checked correctness, but how about

np.tensordot(rotation, data, axes=1)

Gary R

On 24 February 2012 23:11, Bob Dowling <rjd4+numpy at cam.ac.uk> wrote:
> Conceptually, I have a 2-d grid of 2-d vectors.  I am representing this
> as an ndarray of shape (2,M,N).  I want to apply a 2x2 matrix
> individually to each vector in the grid.
>
> However, I can't work out the appropriate syntax for getting the matrix
> multiplication to broadcast over the grid.  All attempts at
> multiplication seem to want to cast the 3-d array into a matricx with
> the inevitable error message "ValueError: shape too large to be a matrix."
>
> Does such a method exist?
>
> This is a dummy script to show the sort of thing I'm trying to do:
>
> import math
> import numpy
> import numpy.random
>
> # Rotation angle
> theta = math.pi/6.0
>
> # Grid shape
> M = 10
> N = 10
>
> # Establish the rotation matrix
> c = math.cos(theta)
> s = math.sin(theta)
> rotation = numpy.matrix([[c, s], [-1.0*s, c]])
>
> # Fudge some data to work with
> data = numpy.random.uniform(-1.0, 1.0, 2*M*N).reshape(2,M,N)
>
> # THIS DOES NOT WORK.
> # BUT WHAT DOES?
> rotated_data = rotation*data
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list