WEBVTT

00:00.060 --> 00:00.570
Hello again!

00:01.080 --> 00:03.600
In this video, we are going to look at mathematical types.

00:05.190 --> 00:08.130
The first one we are going to look at, very briefly, is the valarray.

00:08.940 --> 00:10.740
This is similar to the vector.

00:11.220 --> 00:14.610
It is based on the arrays in the FORTRAN programming language.

00:15.270 --> 00:18.060
FORTRAN is one of the oldest computer programming languages.

00:18.120 --> 00:20.100
It goes back to the 1950s.

00:20.670 --> 00:24.720
It is specially designed for doing numerical work, and it is very good at it.

00:25.350 --> 00:26.850
So it is still used today.

00:28.140 --> 00:30.300
The valarray has a fixed size.

00:30.390 --> 00:36.450
The number of elements cannot change. And it has a much more limited interface than the standard vector.

00:36.990 --> 00:40.470
And it also has some extra syntax for doing numerical operations.

00:41.040 --> 00:43.890
So things like adding or dividing vectors, for example.

00:45.360 --> 00:47.220
However, it is not used very much.

00:47.760 --> 00:54.300
It is regarded as poorly designed, and compiler implementers are not really interested in optimizing it.

00:55.470 --> 01:01.050
If you want something like this, you would do better to use one of the third party libraries, and I have seen

01:01.050 --> 01:06.450
recommendations for Eigen, Blaze and Armadillo. And there are probably some other good ones as well.

01:09.100 --> 01:13.120
When we talked about overloading operators, we mentioned complex numbers.

01:13.810 --> 01:17.950
We wrote an addition operator for a very simple complex number class.

01:18.940 --> 01:26.350
C++ 11 now has a fully featured complex number class in the library, in the <complex> header. It is a

01:26.350 --> 01:31.750
template type, and the parameter can be any one of the built-in floating-point types.

01:32.290 --> 01:37.780
So we could use float, double or long double. Other types are "use at your own risk".

01:37.780 --> 01:40.390
They may not work properly or they may not even compile.

01:40.900 --> 01:42.220
So I do not recommend that.

01:44.380 --> 01:46.420
To create a complex number object,

01:46.420 --> 01:53.440
We give the type as the parameter so that is double, and then we part the real and imaginary parts as arguments

01:53.440 --> 01:54.310
to the constructor.

01:54.430 --> 02:01.210
So this creates a complex number whose real part is three and imaginary part is four. And there are member

02:01.210 --> 02:03.880
functions for getting the real and imaginary parts.

02:06.420 --> 02:09.180
We can do input and output with complex objects.

02:10.110 --> 02:13.320
The display format is the fairly obvious one.

02:13.590 --> 02:17.430
We have a pair of round brackets with the real and imaginary part inside them.

02:18.390 --> 02:22.530
We can also read in numbers from the keyboard, if they are entered in this format.

02:24.390 --> 02:25.450
So let's try that out.

02:25.470 --> 02:32.190
We are going to print out this complex number, and then we are going to try and read a complex number

02:32.190 --> 02:32.940
from the keyboard.

02:35.700 --> 02:43.140
So the complex number is printed out with real part three and imaginary part four. And let's enter one.

02:43.140 --> 02:43.620
So...

02:47.660 --> 02:48.020
See.

02:48.860 --> 02:56.090
So this has been interpreted as a complex number with real part 2.5, and imaginary part 1.9.

02:59.790 --> 03:05.400
We can do arithmetic and logical operations with complex numbers, and pretty much all of them are supported.

03:06.090 --> 03:12.150
So, for example, we can add together complex numbers, we can compare them for equality, and we can

03:12.510 --> 03:13.860
add integers introduced to them.

03:14.520 --> 03:19.440
If we do that, this will be interpreted as a complex number with real part one and imaginary part

03:19.460 --> 03:26.070
zero. So that will just affect the real part of the number. The increment and decrement operators

03:26.070 --> 03:27.150
are not supported.

03:27.930 --> 03:33.080
I suppose there is ambiguity. Does plus plus just mean adding one to the real

03:33.380 --> 03:37.770
part? Or does it mean adding one to the real part and the imaginary part?

03:38.400 --> 03:40.920
So I think they decided it would be easier not to allow

03:40.920 --> 03:41.160
that.

03:43.510 --> 03:45.160
So let's have some examples.

03:45.160 --> 03:48.430
So we are going to add together these two complex numbers.

03:49.600 --> 03:54.550
We are going to compare them for equality, and then we are going to add one to the first number.

03:58.420 --> 03:59.140
So there we are.

03:59.150 --> 04:02.170
One plus two gives four. Two plus

04:02.170 --> 04:05.230
four gives size. p and q are not equal.

04:05.950 --> 04:10.570
And if we add one to p, we get real part two, imaginary point two.

04:14.120 --> 04:19.160
If we try to call the increment operator, then we get a compiler error.

04:20.850 --> 04:23.370
"complex does not define this operator".

04:27.670 --> 04:36.550
In C++11, we have user-defined literals. In C++14, we get some literals in the library. We have this literal

04:36.960 --> 04:44.260
suffix "i", and this will return a complex object whose real part is zero and imaginary part is, whatever

04:44.260 --> 04:45.760
goes before this suffix.

04:46.990 --> 04:55.120
For example, if we have "2i", then this variable "s" will be a complex object with real part zero and imaginary

04:55.120 --> 04:59.860
part two. And we can also have "three plus 4i".

05:00.130 --> 05:04.510
So that will be a complex number with real part three and imaginary part four.

05:07.410 --> 05:11.850
So this means we can write complex numbers in the same way that we would write them in mathematics.

05:13.080 --> 05:18.140
So let's try this out, with this "2i" and this "three plus four i" variable.

05:19.680 --> 05:23.100
We also are going to do a compound addition with a literal.

05:26.150 --> 05:34.160
So there we are. "s" is real part zero, imaginary part two. "z" has real part three and imaginary part four. And

05:34.160 --> 05:41.840
adding "4i" to "p" gives real part one, imaginary part six. And then, finally, going a bit deeper into

05:41.840 --> 05:43.040
the maths side.

05:43.580 --> 05:46.520
If this is "above your pay grade", then do not worry about it.

05:47.870 --> 05:53.150
We can call abs() to get the magnitude, or the modulus, or the absolute value, whatever you want to call it.

05:53.780 --> 05:59.030
So that squares the real part and the imaginary part, adds them together, and takes the square root.

06:00.110 --> 06:06.470
We can call arg() to get the phase angle, in radians, and we can also get the complex conjugate.

06:06.950 --> 06:09.530
So that will invert the sign of the imaginary part.

06:12.270 --> 06:18.120
And we can also use exponential, power and trigonometric functions with complex objects.

06:21.170 --> 06:23.030
So let's quickly try that out.

06:27.550 --> 06:32.830
So the magnitude of "p" is five, the conjugate is three and minus four.

06:33.400 --> 06:35.410
And I will leave you to check the rest for yourself!

06:36.190 --> 06:36.490
Okay.

06:36.490 --> 06:37.570
So that is it for this video.

06:37.990 --> 06:38.800
I will see you next time.

06:38.800 --> 06:40.960
But until then, keep coding!
