Back in March I had a class in
linear algebra which unfortunately I didn't feel was taught all that well. Since I felt the homework wasn't doing a good job of getting me to learn the techniques we were being taught, I decided to write a C++ library that implemented several of the methods this class covered.
For my linear algebra class, I used
OpenOffice Calc to do my matrix calculations. This made it easy to check my steps and eliminate arithmetic mistakes (which I seem to make a lot). One thing I noticed was that OpenOffice often ended up with tinny rounding errors. I would get results like 1E-15 (1
-15) that I had to manually replace with 0. The reality was, all the matrix math I was doing used integer numbers. Since all the matrix operations I did were just addition, subtraction, multiplication, and division, everything could be done using rational numbers. So my first task was to write a rational number template class.
A
rational number is a number that can be expressed as a quotient, so in the form
a/
b or "
a over
b". Rational numbers (set
Q) are made of two integer number (set
Z),
a and
b. In C/C++, there are several integer types, so I decided a template class would work the best so the programmer could select the integer type they needed. In addition, as long as the type supported basic arithmetic (subtraction, multiplication, and division), any class that overloaded these operators could be used. This allows the use of arbitrary precision libraries such as the
GNU Multiple Precision Arithmetic Library.
The matrix class is also a template. The elements of the matrix can be of any type (integer, floating-point, rational, ext.) I implemented the following functions:
addition/subtraction,
transpose,
row echelon reduction,
determinants,
dot product,
matrix product,
inversion, and
LU decomposition.
The libraries are available
here, and linked on the
projects page.