[Numpy-discussion] Matrices and arrays of vectors

Bob Dowling rjd4+numpy@cam.ac...
Fri Feb 24 06:11:39 CST 2012

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)

rotated_data = rotation*data

More information about the NumPy-Discussion mailing list