[Numpy-discussion] [AstroPy] Rotating and Transforming Vectors--Flight Path of a Celestial Body

Anne Archibald peridot.faceted@gmail....
Fri Dec 18 00:14:18 CST 2009

2009/12/18 Wayne Watson <sierra_mtnview@sbcglobal.net>:
> It's starting to come back to me. I found a few old graphics books that
> get into transformation matrices and such. Yes, earth centered. I ground
> out some code with geometry and trig that at least gets the first point
> on the path right. I think I can probably apply a rotation around the
> x-axis several times with a 1/2 degree rotation for each step towards
> the north to get the job done.
> I'm still fairly fresh with Python, and found a little bit of info in a
> Tentative numpy tutorial. I found this on getting started with matrices:
> from numpy import matrix
> Apparently matrix is a class in numpy, and there are many others, linalg
> I think is one. How
> does one find all the classes, and are there rules that keep them apart.
> It was tempting to add
> import numpy in addition to the above, but how is what is in numpy
> different that the classes?
> Is numpy solely class driven? That is, if one does a from as above to
> get all the classes, does
> it give all the capabilities that just using import numpy does?

Many things in python are classes; a class is a way of attaching
relevant functions to a collection of data (more precisely, a class is
a *type*, defining the interpretation of data; usually they also carry
a collection of functions to operate on that data). So the central
feature of numpy is a class, ndarray, that represents a collection of
values of homogeneous type. This may be one, two, or many-dimensional,
and there are various operations, including linear algebra, on them
available in numpy.

The matrix class is a special kind of ndarray in which a number of
modifications have been made. In particular, the * operator no longer
does element-wise operations, it does matrix multiplication. There are
also various rules designed to ensure that matrix objects are always
two-dimensional. I avoid matrix objects like the plague, but some
people find them useful.

numpy.linalg is an entirely different beast. It is a *module*, a
collection of functions (and potentially objects and classes). It is
like sys or os: you import it and the functions, objects and classes
it contains become available. This is a basic feature of python. What
is unusual (but not unique) is that rather than having to explicitly
import it like:

import numpy
import numpy.linalg


numpy automatically imports it for you, every time. This is done for
historical reasons and won't change, but is a wart.

For your purposes, I recommend simply using numpy arrays -
three-element arrays for vectors, three-by-three for matrices - and
using the linear algebra functions numpy provides to act on them. For
example, dot does matrix-matrix, matrix-vector, and vector-vector


P.S. you can usually write out a rotation explicitly, e.g. as
[[cos(t), sin(t), 0],
 [-sin(t), cos(t), 0],
 [0, 0, 1]]
but if you want a more general one I believe there's a clever way to
make it using two reflections. -A

More information about the NumPy-Discussion mailing list