14.4 Sum in C++ - Video Tutorials & Practice Problems
Video duration:
5m
Play a video:
Video transcript
<v Voiceover>With C plus plus</v> and even more so with Rcpp, there is more than one way to do things. So we are going to take a look at some of those ways to add up a vector of numbers. We're going to create a new function and to ensure it's available in R, we need to export it, so slash, slash and then double square brackets, Rcpp, colon, colon, export. When adding up a vector of numbers there could be integers; there could be decimals. So we need to be able to handle a decimal result. In C plus plus we do that by returning a double. The name of the function will be fast sum since it should be fast since it's in C plus plus. In reality, this probably won't be any faster than the base R sum function because that is written in either C or fortran and is already completely optimized. The one argument for this function will be a numeric vector named x. We enclose the body of the function in curly braces just like in R. The first thing we have to do is declare a variable to hold the result. This will be a double and we will call it result. And we will initialize it with a value of zero. 'Cause what we will do is take this value of zero and successively add in each element of the vector X. To do that, we build a for loop. For, int, I equals zero, keep going as long as I is less than X dot size and at each iteration, add one to I. Now we've looped through X and add each value to result. We do that by saying result, plus gets X sub I. We aren't iterating of a result because result is just a number that accumulates everything. We just have to keep adding in each element of X. When the loop is done, return the result. We can now source this and see what happens. Again, we source with control, shift, S or on a Mac, command, shift, S. Let's call the function. We get 5,050 as expected. That's one way to do it. C plus plus also offers an accumulate function where it can automatically iterate over a vector or an array and add up the numbers. Let's see how that's done. We create another function and we export it. And again, this will be a double. And we call fast sum two, that takes a numeric vector. Here we will declare the resulting variable and define it at the same time. The accumulate function is in the standard library. So we access it by saying STD, colon, colon, accumulate. The first argument is where to start and we want to start at the beginning of X so we say X dot begin. The second argument is where to stop. And we stop at X dot end. This takes advantage of iterators to iterate over the entire vector. And we initialize it with the value of zero. What we are saying here is, iterate over X starting at zero and add each element of X to that number. We then simply say return, result. And we can source this and check the answer. Fast sum two, one through 100, we get back 5,050 as expected. Thanks to Rcpp, there's an even easier way. Again we need to export the function if we want to use it. It returns a double and we'll call it fast sum three. It takes the numeric vector X. And all we need to do is say return, sum of X. And Rcpp will take care of the rest. Source the file. And check the results. There we have it; thanks to Rcpp it becomes incredibly easy to integrate C plus plus of R code and we can even write the C plus plus code in a fashion that looks a lot like R.