Hello again! In this video, we are going to look at further numeric algorithms.
We have already looked at some of the algorithms in the numeric header, like iota(), and generate(). The
ones in this video are really going to be of interest, mainly, to people who do numeric computing.
And if you are one of these people, you might be interested in C++ 17, which provides a number of
special functions.
So things like Bessel functions, Legendre polynomials, elliptic integral and so on.
Those are in the header.
I will leave you to investigate those on your own.
It's a bit too specialized for this course,
really!
The first algorithm we are going to look at is partial_sum().
This will take an iterator range, and it will go through the iterator range, and it will calculate the running
total of the elements.
So the first element has a running total of 'a', the second test has a running total of a + b,
a + b + c, and so on.
And it will write each of these running totals to the corresponding element in a destination.
So if we have a vector with elements 1, 2, 3, 4, 5, the elements in the destination
will be 1, 1 + 2, 1 + 2 + 3 and so on.
And this is in fact, how you can perform numerical integration.
If this vector contains the value of a function at some points, then this vector will contain
the values of the integral of that function at the same points.
Okay, so let's try this out.
So we have our vector, with elements 1, 2, 3, 4, 5.
We have our destination. And we have our call to partial_sum().
Let's see what we get.
Okay, so 1 is 1, 1 + 2 is 3, 1 + 2 + 3 is 6, and so on.
The next algorithm adjacent_difference().
This will go through the iterator range, and this time it will write the difference between each element.
So 'a' minus nothing is 'a', b - a and c - b, and so on.
And these differences will get written to the destination.
So the destination has elements a, b - a, c - b, and so on.
If we give the results of partial_sum(), so this is the vector we got when we ran the partial_sum()
calculation. If we give this as the argument to adjacent difference, then 3 - 1 is
2, 6 - 3 is 3, and so on.
And we get back the original
vector.
So adjacent_difference() is the inverse of partial_sum(), and the inverse of numeric integration is numeric
differentiation.
So let's see if we get the original vector back.
So we have the results of the partial_sum() and then we pass that to adjacent_difference().
And there we are, we do get back the original vector.
Finally, there is inner_product(). This takes 2 iterator ranges. It goes through both ranges
and it multiplies together the corresponding elements, so a1 * b1, a2 * b2 and
so on.
And then it adds them all up. And that will calculate the scalar product of two vectors.
So this is quite similar to the accumulate() algorithm, if you remember that, except we are doing a
preliminary step where we multiply together the elements. When we call the accumulate() algorithm, we
have to provide an initial value for the sum and we need to do that for inner_product() as well.
So we pass it the two iterator ranges, and the return value will be the results of multiplying
together pairs of elements from these iterators, and adding them together, and adding on this initial
value.
So let's try this out. We have the two vectors we have used so far.
We passed these as the iterator ranges for inner_product().
We have the initial range of the sum as zero. And then the result of this will be the result of
1 * 1 plus 3 * 2 plus 6 * 3, and so on.
And, as I am sure you all know, that is 140!
Okay, so that's it for this video.
I will see you next time.
Until then, keep coding!