The Incomplete Works of Josh English

Writings and Ramblings about almost anything...

Vector.py

I have been playing with Poser and POV-Ray and using Python to translate between the two. To do this I needed a Vector object, so I’ve written one.

Download vector.py here.

Download vectortest.py here.

The Vector class may not always work as mathematicians want it to, because I designed it to work with POV-Ray, so adding a two dimensional vector to a three dimensional vector promotes the two dimensional to a three dimensional vector permanently to complete the operation. I also have some unit tests written to make sure that I don’t destroy the code as I work on it.

The Vector class object is created by passing several arguments, a list, or a tuple:


>>> from vector import Vector
>>> a=Vector(1,2)
>>> b=Vector([1,2,3.5])
>>> c=Vector((1,4,5,3))

Here are the methods of the Vector class:

string()
Returns a string representation of the Vector using < and >.
mag()
Returns the magnitude of the vector.
norm()
Returns a normalized equivalent of the vector.
normalize()
Normalizes the vector, changing it’s values.
vmul(other)
Does component-wise multiplication. This method does not promote either vector
dot(other)
Finds the dot product bewteen two vectors. This method does not promote either vector
cross(other)
Finds the cross product. Raises an error if either vectors don’t have a dimension of 3.
angle(other)
Returns the angle bewteen two vectors
scalar(other)
Finds the scalar projection of u onto v.
project(other)
Finds the vector projection of u onto v.
cmin(other)
Returns a new vector after component wise minimum comparision.
cmax(other)
Returns a new vector after component wise minimum comparision.
povstring()
Returns a string representation of self fit for POV-Ray.