Re: [Numpy-discussion] [AstroPy] Rotating and Transforming Vectors--Flight Path of a Celestial Body
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? Anne Archibald wrote:
2009/12/17 Wayne Watson <sierra_mtnview@sbcglobal.net>:
I'm just getting used to the math and numpy library, and have begun working on a problem of the following sort.
Two observing stations are equidistant, 1/2 degree, on either side of a line of longitude of 90 deg west, and both are at 45 deg latitude. Given the earth's circumference as 25020 miles, a meteor is 60 miles above the point between the two sites. That is, if you were standing at long 90deg and lat 45 deg, the meteor would be that high above you. 70 miles along the long line is 1 degree, so the stations are 70 miles apart. I want to know the az and el of the meteor from each station. With some geometry and trig, I've managed to get that first point; however, I can see moving the meteor say, 1/2 deg, along its circular path towards the north pole is going to require more pen and pencil work to get the az/el for it.
Long ago in a faraway time, I used to do this stuff. It should be easy to rotate the vector to the first point 1/2 deg northward, and find the vector there, then compute the new az and el from each station. Maybe. I'm just beginning to look at the matrix and vector facilities in numpy. Maybe someone can comment on how this should be done, and steer me towards what I need to know in numpy.
You may find that the problem grows drastically easier if you work as much as possible in so-called earth-centered earth-fixed coordinates (sometimes called XYZ) coordinates. These are a rectilinear coordinate system that rotates with the earth, with the Z axis through the north pole and the X axis through the equator at the Greenwich meridian. It's kind of horrible for getting altitudes, since the Earth is sort of pear-shaped, but it makes the 3D geometry much simpler.
If you don't go this route, I'd recommend picking one station and defining a rectilinear coordinate system based on its north and vertical vectors. The north and vertical vectors of the other station will be at somewhat funny angles (unless you can get away with treating the Earth as flat between the two), but whatever rectilinear coordinates you choose, a dot product lets you calculate vector lengths and angles between them, and a cross product lets you build vectors orthogonal to a given pair. So, for example, if your station has north vector N and up vector U, you can get its east vector as E=cross(N,U) (then normalize it); if you want to convert an absolute north to a local north (i.e. one that is horizontal) you can do N=cross(E,U) (and normalize it). Then you can get the azimuth of a vector V using dot(N,V)/sqrt(dot(V,V)) and dot(E,V)/sqrt(dot(V,V)).
Anne
-- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet "... humans'innate skills with numbers isn't much better than that of rats and dolphins." -- Stanislas Dehaene, neurosurgeon Web Page: <www.speckledwithstars.net/>
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.linalg.svd(numpy.ones((3,2))) 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 multiplication. Anne 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
Fyinfo, http://code.google.com/p/geometry-simple has classes "Point","Line","Plane","Movement", with methods points moved distance_to angle_to midpoint_to ... It's not all you want (~ 350 lines, uses straight numpy, would benefit from an expert eye (dot = inner ? math. ?)) but has a clean api. @Anne, excellent description; can you find time to work up your notes to a say 5-page intro ? Would sell like hotcakes cheers -- denis
On Thu, Dec 17, 2009 at 11:14 PM, Anne Archibald <peridot.faceted@gmail.com>wrote:
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.linalg.svd(numpy.ones((3,2)))
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 multiplication.
Anne
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
Two reflections in two (hyper)planes gives a rotation of twice the angle between their normals and leaves their intersection invariant. It's hard to visualize in more than three dimensions ;) It's also a good trick for rotating vector values in place using only exchanges. Chuck
participants (4)
-
Anne Archibald -
Charles R Harris -
denis -
Wayne Watson