WEBVTT

00:00.120 --> 00:03.930
Hello again! In this video, we are going to look at further numeric algorithms.

00:04.860 --> 00:10.350
We have already looked at some of the algorithms in the numeric header, like iota(), and generate(). The

00:10.350 --> 00:15.210
ones in this video are really going to be of interest, mainly, to people who do numeric computing.

00:15.870 --> 00:21.330
And if you are one of these people, you might be interested in C++ 17, which provides a number of

00:21.390 --> 00:22.410
special functions.

00:23.040 --> 00:27.540
So things like Bessel functions, Legendre polynomials, elliptic integral and so on.

00:28.440 --> 00:29.500
Those are in the <cmath> header.

00:30.150 --> 00:31.830
I will leave you to investigate those on your own.

00:32.070 --> 00:34.080
It's a bit too specialized for this course,

00:34.080 --> 00:34.440
really!

00:36.420 --> 00:39.240
The first algorithm we are going to look at is partial_sum().

00:39.720 --> 00:44.990
This will take an iterator range, and it will go through the iterator range, and it will calculate the running

00:45.000 --> 00:46.140
total of the elements.

00:46.890 --> 00:53.430
So the first element has a running total of 'a', the second test has a running total of a + b,

00:53.430 --> 00:54.510
a + b + c, and so on.

00:55.050 --> 01:00.510
And it will write each of these running totals to the corresponding element in a destination.

01:03.900 --> 01:08.910
So if we have a vector with elements 1, 2, 3, 4, 5, the elements in the destination

01:08.910 --> 01:12.390
will be 1,  1 + 2,  1 + 2 + 3 and so on.

01:13.620 --> 01:17.190
And this is in fact, how you can perform numerical integration.

01:17.730 --> 01:23.070
If this vector contains the value of a function at some points, then this vector will contain

01:23.070 --> 01:26.160
the values of the integral of that function at the same points.

01:28.970 --> 01:30.500
Okay, so let's try this out.

01:30.830 --> 01:33.740
So we have our vector, with elements 1, 2, 3, 4, 5.

01:34.310 --> 01:38.270
We have our destination. And we have our call to partial_sum().

01:39.650 --> 01:40.460
Let's see what we get.

01:42.830 --> 01:49.570
Okay, so 1 is 1, 1 + 2 is 3, 1 + 2 + 3 is 6, and so on.

01:55.460 --> 01:57.680
The next algorithm  adjacent_difference().

01:58.010 --> 02:02.540
This will go through the iterator range, and this time it will write the difference between each element.

02:03.170 --> 02:09.200
So 'a' minus nothing is 'a', b - a and c - b, and so on.

02:09.770 --> 02:12.500
And these differences will get written to the destination.

02:12.770 --> 02:17.300
So the destination has elements a, b - a, c - b, and so on.

02:19.430 --> 02:25.550
If we give the results of partial_sum(), so this is the vector we got when we ran the partial_sum()

02:25.880 --> 02:31.940
calculation. If we give this as the argument to adjacent difference, then 3 - 1 is

02:31.940 --> 02:33.980
2, 6 - 3 is 3, and so on.

02:34.400 --> 02:35.510
And we get back the original

02:35.510 --> 02:35.990
vector.

02:36.590 --> 02:43.700
So adjacent_difference() is the inverse of partial_sum(), and the inverse of numeric integration is numeric

02:43.700 --> 02:44.510
differentiation.

02:49.230 --> 02:51.480
So let's see if we get the original vector back.

02:51.510 --> 02:57.000
So we have the results of the partial_sum() and then we pass that to adjacent_difference().

03:00.100 --> 03:02.290
And there we are, we do get back the original vector.

03:07.600 --> 03:14.020
Finally, there is inner_product(). This takes 2 iterator ranges. It goes through both ranges

03:14.020 --> 03:19.930
and it multiplies together the corresponding elements, so a1 * b1, a2 * b2 and

03:19.930 --> 03:20.350
so on.

03:20.920 --> 03:26.320
And then it adds them all up. And that will calculate the scalar product of two vectors.

03:28.940 --> 03:33.650
So this is quite similar to the accumulate() algorithm, if you remember that, except we are doing a

03:33.650 --> 03:39.920
preliminary step where we multiply together the elements. When we call the accumulate() algorithm, we

03:39.920 --> 03:45.140
have to provide an initial value for the sum and we need to do that for inner_product() as well.

03:46.520 --> 03:51.980
So we pass it the two iterator ranges, and the return value will be the results of multiplying

03:51.980 --> 03:57.740
together pairs of elements from these iterators, and adding them together, and adding on this initial

03:57.740 --> 03:58.100
value.

04:02.300 --> 04:05.900
So let's try this out. We have the two vectors we have used so far.

04:07.430 --> 04:10.670
We passed these as the iterator ranges for inner_product().

04:11.510 --> 04:18.020
We have the initial range of the sum as zero. And then the result of this will be the result of

04:18.020 --> 04:21.680
1 * 1 plus 3 * 2 plus 6 * 3, and so on.

04:23.420 --> 04:25.520
And, as I am sure you all know, that is 140!

04:27.710 --> 04:29.420
Okay, so that's it for this video.

04:29.780 --> 04:30.620
I will see you next time.

04:30.860 --> 04:32.660
Until then, keep coding!
