Categories
Linear Maths in C++

Polymorphic operators

Some time ago the C++ bool operator==(Class c) returned the wrong result for some comparisons. We couldn’t see why looking at the code and to hit the deadline, we made work-around and ignored it. This happens at work but it stayed with me, and many years later a CppCon video explained that as a class member, the argument is the same type or can be coerced to that type. If coercion fails, the function fails.

The safe solution is to put the operator== outside the class. The function then takes two args, and can be a friend if you need access to private data. So I put my Matrix operators as functions in the LinearMath namespace.

Modern C++ allows us to make the coercion explicit. Is this enough to protect against spurious calls ? That experience with a failing comparison operator was not replicated.

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.

Categories
Linear Maths in C++

Software Engineering

When you only have a hammer everything looks like a nail. We need the right tools for the job. In some contexts this is referred to as a tool-chain. For my Windows laptop, I’m using Visual Studio Community Edition 2017. To look after my code I have a local Git repository. VS has a nice integration with Git, explain by Benjamin Perkin’s blog.

I also created Unit Tests. There’s loads of stuff on YouTube about Test Driven Development. I generally try to write the tests first. Kent Beck almost invented TDD. His book Test Driven Development by Example is a great read. He is quite pragmatic. When you don’t know, take small steps. When you’re more certain, you can take big steps.

Now I have to be honest and admit it’s gone wrong. Before I described Complex Numbers. I was feeling really confident so I wrote code and tests for a templated matrix. The Unit Test is not instantiating the template when it builds the test DLL. I haven’t set up Git Hub properly, hence I’m doing it locally first before it goes on the interweb.

It’s the second week of Corona Virus emergency. Life is up-ended. I’m lacking a UML design tool, maybe Star UML (cloud storage). IBM offers Rhapsody (not used it). I have used Microsoft Visio. It looks like a cloud-based freebie or a cash investment in the future.