Categories
Linear Maths in C++

Matrices

Stroud Chapter 4 is Determinants. Chapter 5 is on Matrices. Chapter 4 only really deals with 2 x 2 and 3 x 3 matrices. This is a little taster for eigenvalues of larger matrices. First, Chapter 5 tells us how to add, subtract, multiply and divide matrices. We can apply these operations with a scalar – in that case, every element does the operation with the scalar. As a example, multiplying a 2 x 2 matrix by 5 is simply M0,0 x 5, M0,1 x 5, M0,1 x 5 and M1,1 x 5 .

Operations with 2 matrices takes row-wise from the left and column-wise from the right. This means the two matrices must have the right number of elements. This is property is called compliant.

So I ask myself what type is my matrix ? It could be anything, and that in C++ and Java means a template. Disregarding best advice and best practice, I wrote 2 x 2 and 3 x 3 determinants as templated functions. They were so simple I can write a unit test later. I carried on and wrote a templated Matrix class. A C++ class has a header file defining the class and implementation in the cpp file. Now this compiles I can write unit tests. My unit tests fail on linking.

The take-away here is that template functions and classes are not regular code. They are instructions on how to produce code, or meta-code. There is another style of programming called template meta-programming. Simply put, the template is instantiated at compile time. You can do real computing in the compiler, sometimes called static programming.

I moved my template code into inline (.inl) files. These are #included like header files. There were a few other tweaks to get the types to match exactly. The unit tests all execute.

The Git Hub content is still wrong but I loaded the code into a local Git repository. I have effective change control locally. I need to move my local repository to the Web Git Hub repo. TO DO.

Leave a Reply

Your email address will not be published. Required fields are marked *