Categories
Linear Maths in C++

..and this happened

At the end of October I got to see my friend Andrea Giommi play with his band The Glass Key. I didn’t know much about anything – the venue, the artists or the main band. They opened OSPs Psyche Festival at The Victoria, Dalston a few weeks earlier. I got there a bit a late and missed then, so I was glad to have to opportunity to see them.

Even more striking was Agathe Max on violin. I got to see her with her band Custy Gamut (Track 1 andTrack 2). She was on Radio 3’s Late Junction on International Women’s Day.

The main band was World Sanguine Report. That’s Andrew Plummer on guitar and vocals and Ruth Goller on bass. Ruth’s solo album Skylluminia get’s a 4-star review in The Guardian.

Andrea plays at Two Palms, Hackney tomorrow (14th Mar) with his band Sons of Viljems. Tickets on DICE to make things easy.

Categories
Linear Maths in C++

Building for Linux from Windows

I have been a fan of Linux for a very long time. Windows was always been dated, so that you knew when you had to upgrade. That certainty breaks down with Windows 10 continual updates. There’s a Microsoft Reactor session later called “Commercial Marketplace Roadmap”. The latest upgrade gave us WSL2, that supports graphics in Linux. As a commercial programmer I was struggling with modern Windows GUIs. MFC is really old, WPF only runs in .NET, and Qt can only build UWP apps. I’m looking for something else.

I’m getting ahead of myself. Visual Studio 2017 (still dated!) added cross-platform development. The outline is to connect to a remote system using the secure shell (ssh). That remote system provides build and debug facilities. Rather than repeating it, the Microsoft blog describes Linux development.

Having installed as per instructions and restarted, under the menu “File->New->Project, there are Linux options. The most basic is “Console Application (Linux)”. This generates a “main” that looks like this

int main()
{
    printf("hello from ConsoleAppLinux!\n");
    return 0;
}

It might not look like much but it’s quite powerful. Building sends the code to the remote machine and compiles natively. I’m using WSL so I had to install g++ and gdb. Secure shell connection is managed in Visual Studio from Tools->Options and then “Cross Platform”->”Connection Manager.

Vanilla-flavoured Unix, as my professor called it, is not very interesting. WSL2 needs a major Windows update but will allow graphics. Another option under new project is for Raspberry Pi. The generated project “main” is

int main(void)
{
	wiringPiSetupSys();
	pinMode(LED, OUTPUT);

	while (true)
	{
		digitalWrite(LED, HIGH);  // On
		delay(500); // ms
		digitalWrite(LED, LOW);	  // Off
		delay(500);
	}
	return 0;
}

As you can it’s just normal code. The function calls are to a library that mimics Arduino GPIO.

Another development option to investigate is Native Android development. Google have an NDK and Android Studio has a C++ new project template.

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.

Categories
Linear Maths in C++

Complex Numbers

eπi+1 = 0

Euler’s identity was the first beautiful piece of maths. The short YouTube video made it really clear.

The i symbol is the square root of -1. There is no rational number that when multiplied by itself is -1. So they made one up and called it i (or maybe j).

Arithmetic

Solutions to quadratic equations might lead to requesting the square root of -1. So now we have a value that has 2 parts – the real part and the imaginary part. Conventional maths applies. We can easily apply arithmetic operations with a scalar, it just applies to both part. Operation with complex numbers is

Addition
add real parts and add imaginary parts
Subtraction
subtract real parts and subtract imaginary parts
Multiplication
somewhat like multiplying out the bracket
Division
can’t be done directly, requires the complex conjugate

a+bj * c+dj = ac +adj +bjc + bdj2
but bdj2 would be -bd, so
a+bj * c+dj = ac – bd + (ad +bc)j

Division requires the complex conjugate. Suppose
a+bj is multiplied by a-bj, then the imaginary part goes away. To divide complex numbers, multiply top and bottom by the complex conjugate of the divisor (bottom) and the the operation is a simple scalar division.

Other forms

If we imagine the horizontal axis represents real numbers and vertical axis represents imaginary numbers, we can construct a triangle. One side is the real axis, another is imaginary axis and the hypotenuse runs from the origin to the (r, i) coordinates. An alternative representation is the length of the hypotenuse and the angle wrt to the data. This is called the polar form.

Code

In my GitHub there are 4 files

  • ComplexNumber.h
  • ComplexNumber.cpp
  • Angle.h
  • Angle.cpp

The conversion to polar form requires multiplication by sines and cosines. The standard library functions use radians rather than degrees. We understand 180° easier than π radians. The class Angle is supposed to convert between radians and degrees. It also provides arithmetic primitives. Full conversion include minutes and seconds is a TO DO.

Testing

I’m a big fan of Unit Testing. In fact, for the complex number class, I wrote those tests first. Conversion to polar form was failing because radians, with fractional parts were compared. These are not the values in Stroud’s book. After some time I realized the factional parts in Stroud’s value are minutes. This had a natural solution of the Angle class, again with it Unit test. I will add these Unit Tests to my Git Hub repository.

Categories
Linear Maths in C++

Start

I have registered my name as my domain. I had to see what WordPress was first and it’s just content management. I’m sure I’ll get better at this over time.

I was reading K A Stroud’s Engineering Mathematics over the last couple of years. Way back in the 90’s I had books on neural nets – ahm, Artificial Neural Nets (ANN). That approach was strictly object-orientated.

The brain is a network of switches. That switch is technically known as an synapse. When it fires is dependent largely on the amount that has flowed into it. In the human brain that stuff is sodium ions, which is why it’s important to eat salt. The input flows through a nerve fibre called an axon and output through a dendrite. The channels become wider if used a lot, or narrower if not used, causing changes in the signal level.

The contemporary approach only considers the synapse weight and stores these values in a matrix. Tensor Flow and PyTorch are Python based tool-kits that use underlying NumPy libraries for very efficient matrix manipulation. I jumped straight in and tried to read Elegant SciPy but it was too difficult. The last book on Python was Fluent Python by Luciano Ramalho. In it he claims 16 out the 23 Design Patterns in the GoF book are obsolete. The whirligig of time moves on and we get better at building software.

After many, many years of designing object-orienting software, I’m a skilled C++ programmer. Back to K A Stroud’s book – actually I have the 3rd edition; the current one at Amazon is 7th edition. I coded the actions for a Vector and Matrix and added to my GitHub repo. I think I have to study to do to use GitHub properly.

I have done some of HackerRank‘s exercises in C++ and Python.I have gold badge for C++.