Vector, matrix, normalize, rotate. What package?

greg greg at cosc.canterbury.ac.nz
Thu Mar 1 03:53:02 EST 2007


Mattias Brändström wrote:

> 1. Create 3d vectors.
> 2. Normalize those vectors.
> 3. Create a 3x3 rotation matrix from a unit 3-d vector and an angle in
> radians.
> 4. Perform matrix multiplication.
> 
> Meybe someone knows a way to use numpy for 2 and 3?

Here's some code I wrote recently to do normalisation
of vectors using Numeric:

   from Numeric import add, sqrt

   def dots(u, v):
     """Return array of dot products of arrays of vectors."""
     return add.reduce(u * v, -1)

   def units(v):
     """Array of unit vectors from array of vectors."""
     ds = 1.0 / sqrt(dots(v, v))
     return ds * v

These work best if you give them multiple vectors to
work on at once, otherwise you don't get much advantage
from using Numeric.

I don't have anything to hand for rotation about a
vector, but if you can find a formula, you should be
able to use similar techniques to "vectorize" it
using Numeric.

--
Greg



More information about the Python-list mailing list