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.

1 reply on “Complex Numbers”

I definitely wanted to make a small message in order to appreciate you for these remarkable tactics you are writing on this site. My considerable internet search has at the end of the day been compensated with extremely good concept to exchange with my classmates and friends. I would admit that most of us visitors actually are truly lucky to dwell in a wonderful community with very many perfect professionals with helpful opinions. I feel somewhat lucky to have discovered your website and look forward to really more exciting minutes reading here. Thank you again for all the details.

Leave a Reply

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