For decades, Jack has wanted a language that would let him do arithmetic with vectors and matrices. Now he has it. Here's a summation of his work on vector and matrix classes.
By Jack W. Crenshaw
Matrix math functions
In the end, adding the math operations to the matrix class is the easiest part of the job. I had similar functions in Vectors.cpp, so setting up the ones in the matrix class was pretty easy. You'll find the code on our web site. In Listing 1, I show the typical usage of each function.
Understand, these functions by no means exhaust the possible functions we can provide in this class. As I mentioned earlier, the math for matrices is a rich one indeed, and even moreso when you combine matrices with vectors. We could go on adding functions for the rest of eternity.
Listing 1 Matrix math functions.
B = T(A); // transpose a matrix
A.Negate( ); // replace A by its negative
B = -A; // unary minus
A += B; // add in place
A += 1.0; // add scalar matrix in place
A -= B; // subtract in place
A -= 1.0; // subtract scalar matrix in place
C = A+B; // matrix add
C = A+s; // add matrix and scalar (square only)
C = A-B; // matrix subtract
C = A-s; // subtract scalar from matrix (square only)
C = A*B; // matrix product
C *= 2.0; // scale matrix in place
A = 2.0*A; // multiply matrix by scalar
A = A*2.0; // same result
A.Invert( ); // matrix inverse
C = B/A; // multiply matrix by inverse: C=A-1B
y = A*x; // multiply vector by matrix
y = x/A; // multiply vector by inverse: y=A-1x
I can think of many more operations I wish I had. I'd like to be able to construct a matrix from a set of vectors. I'd like to be able to extract rows and columns from a matrix. I'd like to be able to build a matrix from a set of smaller ones, and I'd like to be able to subdivide them again. I'd like to create a diagonal matrix whose diagonal elements are defined by a vector. I'd like to be able to extract the diagonal into a vector.
I want at least two nontrivial functions that I've not written so far. One is to compute the determinant of a matrix. That determinant is computed within the matrix inverter, mInv( ), and returned from that function. But I have no "compute determinant function" otherwise.
The other function that's often essential for work in control theory, is computing the eigenvalues and eigenvectors of a square matrix (if you don't know what these are, you probably don't need the function). The problem with that one is that the results are often complex numbers. So far, I've not included complex arithmetic in these classes, so that one is going to be a special case.
So there's no doubt that the class Matrix needs more functions. I'll be adding them to the class over time and will show them to you as I do so. In the meantime, however, the class as I've presented it here, and its related files, are nice, serviceable classes in their own right. So I'm going to invoke Plauger's dictum and leave the rest until someone asks for them. Use the libraries in good health.
Jack Crenshaw is a systems engineer and the author of Math Toolkit for Real-Time Programming. He holds a PhD in physics from Auburn University. E-mail him at jcrens@earthlink.net. For more information about Jack click here