WHSoft

 

Linear Algebra Library

 

These are the vector and matrix classes that I wrote to enable all of my 3D math.  These classes were written in pure C++.  They do not depend on any other platform specific libraries.

 

Vector class:

This class represents vectors in R3.  The class will automatically keep up with both rectangular and spherical coordinates.  To increase speed, these values are only computed when requested.  Each vector object keeps track of whether rectangular, spherical, or both coordinate systems are current.  If it is currently rectangular and you ask for a spherical component, all three spherical values are computed.  The same is true for the opposite direction.  If you only need a vector in R2, simply ignore the z and phi values.

The class supports operator overloading to enable readable code.  Several external "helper" functions have also been implemented. 

For example:

dot(x,y) computes the dot product of two vectors.
pi is a predefined constant (2*acos(0)) to allow the most precise measurement of pi that the machine will allow.
degrees(r) will convert from radians to degrees.
radians(d) will convert from degrees to radians.
cross(x,y) will compute the cross product of two vectors.
angle(x,y) will compute the angle between two vectors in radians.

Important internal functions:

set_rectangular(x,y,z) sets the vector equal to <x,y,z> in rectangular coordinates.
set_spherical(r,theta,phi) sets the vector equal to <r,theta,phi> in spherical coordinates.
get_x() returns the x value of the vector, updating it to rectangular coordinates if necessary.
get_y() returns the y value of the vector, updating it to rectangular coordinates if necessary.
get_z() returns the z value of the vector, updating it to rectangular coordinates if necessary.
get_r() returns the r value of the vector, updating it to spherical coordinates if necessary.
get_t() returns the theat value of the vector, updating it to spherical coordinates if necessary.
get_p() returns the phi value of the vector, updating it to spherical coordinates if necessary.
reflect(norm) reflects the vector off of a plane with unit normal vector of norm.
reflection(norm) is the same as reflect(), except it returns the reflected value instead of changing the original vector.
norm() returns the length of the vector.  This function compensates for negative r.  This does not force a conversion to spherical.
make_unit() normalizes the vector. This does not force a conversion to spherical.

 

Matrix class:

This class allows matrices of virtually any dimension to be created.  The matrices are simply represented as a one dimensional array of doubles.  This makes memory allocation and deallocation much simpler on my part.  There are several functions included to simplify matrix/vector operations.  Also, operator overloading has been implemented to ease the creation of readable code.

I have had one primary difficulty with getting this class to work properly.  Due to hardware limitations, the matrix inverse functions are not 100% accurate.  In most cases, matrices will invert properly.  However, sometimes the difference between two theoretically identical numbers does not compute to zero, but it does compute to a very small number.  This will wreak havoc on a matrix inversion.  Since the only thing I use the matrix inversion for is conversion between bases, I have two highly similar implementations of this class.  One of the two is the code I wrote by hand, which is 100% external library independent.  The other uses the publicly available LAPACK for conversion between bases.  This is included in Apple's vecLib framework.  Eventually, I will convert the matrix inversion to this as well (my recent attempts have failed).  So, if you are using this on Mac OS X, just use the vecLib version of the file and include the vecLib framework in your project.

Important external functions:

convertToBasis(vector origin,vector i,vector j,vector k,vector theVector)
      returns a vector defined with respect to the given basis equivalent to theVector
convertFromBasis(vector origin,vector i,vector j,vector k,vector theVector)
      returns a vector defined with respect to the standard basis equivalent to the Vector

Important internal functions:

get(r,c) gets the matrix value at row r and column c.
set(r,c,v) sets the matrix value at row r and column c to be value v.
setdim(r,c) changes the dimensions to r by c, creating an identity matrix for these dimensions.
getdimr() returns the row dimension.
getdimc() returns the column dimension.
transpose() returns the transposed matrix.
inverse() returns the inverse matrix.
invert() inverts the matrix.
makeidentity() doesn't change the dimensions; puts 1's on the diagonal, 0's elsewhere.

see the header file for information about vector translation functions.

 

 

Note:  this code is all provided as is.  If you find/fix any bugs, please notify me.  Otherwise, you can do whatever you want with the code.  There are no related fees or royalties.  The resulting code is not required to be open source.  However, if you do use this code for any publicly released product, I do ask that you give me credit where credit is due and that you notify me (so that I know who thinks my code is reliable and worthy of use).