Hello again!
In this video, we are going to look at mathematical types.
The first one we are going to look at, very briefly, is the valarray.
This is similar to the vector.
It is based on the arrays in the FORTRAN programming language.
FORTRAN is one of the oldest computer programming languages.
It goes back to the 1950s.
It is specially designed for doing numerical work, and it is very good at it.
So it is still used today.
The valarray has a fixed size.
The number of elements cannot change. And it has a much more limited interface than the standard vector.
And it also has some extra syntax for doing numerical operations.
So things like adding or dividing vectors, for example.
However, it is not used very much.
It is regarded as poorly designed, and compiler implementers are not really interested in optimizing it.
If you want something like this, you would do better to use one of the third party libraries, and I have seen
recommendations for Eigen, Blaze and Armadillo. And there are probably some other good ones as well.
When we talked about overloading operators, we mentioned complex numbers.
We wrote an addition operator for a very simple complex number class.
C++ 11 now has a fully featured complex number class in the library, in the header. It is a
template type, and the parameter can be any one of the built-in floating-point types.
So we could use float, double or long double. Other types are "use at your own risk".
They may not work properly or they may not even compile.
So I do not recommend that.
To create a complex number object,
We give the type as the parameter so that is double, and then we part the real and imaginary parts as arguments
to the constructor.
So this creates a complex number whose real part is three and imaginary part is four. And there are member
functions for getting the real and imaginary parts.
We can do input and output with complex objects.
The display format is the fairly obvious one.
We have a pair of round brackets with the real and imaginary part inside them.
We can also read in numbers from the keyboard, if they are entered in this format.
So let's try that out.
We are going to print out this complex number, and then we are going to try and read a complex number
from the keyboard.
So the complex number is printed out with real part three and imaginary part four. And let's enter one.
So...
See.
So this has been interpreted as a complex number with real part 2.5, and imaginary part 1.9.
We can do arithmetic and logical operations with complex numbers, and pretty much all of them are supported.
So, for example, we can add together complex numbers, we can compare them for equality, and we can
add integers introduced to them.
If we do that, this will be interpreted as a complex number with real part one and imaginary part
zero. So that will just affect the real part of the number. The increment and decrement operators
are not supported.
I suppose there is ambiguity. Does plus plus just mean adding one to the real
part? Or does it mean adding one to the real part and the imaginary part?
So I think they decided it would be easier not to allow
that.
So let's have some examples.
So we are going to add together these two complex numbers.
We are going to compare them for equality, and then we are going to add one to the first number.
So there we are.
One plus two gives four. Two plus
four gives size. p and q are not equal.
And if we add one to p, we get real part two, imaginary point two.
If we try to call the increment operator, then we get a compiler error.
"complex does not define this operator".
In C++11, we have user-defined literals. In C++14, we get some literals in the library. We have this literal
suffix "i", and this will return a complex object whose real part is zero and imaginary part is, whatever
goes before this suffix.
For example, if we have "2i", then this variable "s" will be a complex object with real part zero and imaginary
part two. And we can also have "three plus 4i".
So that will be a complex number with real part three and imaginary part four.
So this means we can write complex numbers in the same way that we would write them in mathematics.
So let's try this out, with this "2i" and this "three plus four i" variable.
We also are going to do a compound addition with a literal.
So there we are. "s" is real part zero, imaginary part two. "z" has real part three and imaginary part four. And
adding "4i" to "p" gives real part one, imaginary part six. And then, finally, going a bit deeper into
the maths side.
If this is "above your pay grade", then do not worry about it.
We can call abs() to get the magnitude, or the modulus, or the absolute value, whatever you want to call it.
So that squares the real part and the imaginary part, adds them together, and takes the square root.
We can call arg() to get the phase angle, in radians, and we can also get the complex conjugate.
So that will invert the sign of the imaginary part.
And we can also use exponential, power and trigonometric functions with complex objects.
So let's quickly try that out.
So the magnitude of "p" is five, the conjugate is three and minus four.
And I will leave you to check the rest for yourself!
Okay.
So that is it for this video.
I will see you next time.
But until then, keep coding!