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++.