Contents
1. Introduction2. Keywords
3. Problem
4. Solution
5. Discussion
5.1 Cumulative sum
6. Practice: Cumulative Sum from 1 to 10 with LINQ
7. Conclusions
8. Literature & Links
1. Introduction
In this new LINQ recipe we are going to learn how to find the cumulative sum of a sequence of numbers. This will be accomplished, basically, using the generator function Range and the statistical function Sum. As we will see soon, this will allow us to compute partial sums of the growth percentage, year to year, in organization business process.
2. Keywords
- Cumulative Sum
- Generator function
- LINQ
- Partial sum
- Statistical function
3. Problem
Find a way to find the cumulative of a sequence of numbers.
4. Solution
In the first place, it is required to have a sequence of values -this can be obtained from the Range generator function; and then with this new generated sequence, partial sums are computed by applying the Sum statistical function.
5. Discussion
5.1 Cumulative sum
According to "Cumulative Sum" (2016), a cumulative sum consists of a sequence of partial sums of a given succession. In formal mathematical notation, this concept is expressed as:
Each element of this sequence consists of a partial sum. A partial sum is basically the sum of each predecessor with the current element. The first element a does not have any predecessor element, so it is equals to itself.
A particular example can clarifies this concept better. Suppose we have the sequence of the values of the sales from January to June:
{500000, 900000, 600000, 750000, 880000, 820000}
now the cumulative sum is
{500000, 500000 + 900000, 500000 + 900000 + 600000, 500000 + 900000 + 600000 + 750000, 500000 + 900000 + 600000 + 750000 + 880000, 500000 + 900000 + 600000 + 750000 + 880000 + 820000}
which is equals to
{500000, 1400000, 2000000, 2750000, 3630000, 4450000}
6. Practice: Cumulative Sum from 1 to 10 with LINQ
This example shows us how to compute the cumulative sum using LINQ; in particular with these kinds of functions:
- Range (generator function),
- Sum (statistical function)
The range generated will represent the sequence: each element of this sequence will be summed up with its predecessor inside of a ForEach loop via the Sum function.
This is the LINQ code to perform this task:
// Represents the sequence of cumulative sums:
Listint, int>> cumulativeSums =
new Listint, int>>();
// Sequence of numbers from 1 to 10:
var range = Enumerable.Range(1, 10);
// Compute the cumulative sum:
range.ToList().ForEach(
value => cumulativeSums.Add(
new KeyValuePair<int, int>(value, range.Take(value).Sum())
)
);
// Result:
cumulativeSums.Dump("Partial Sums at Each Level");
7. Conclusions
We have understood how we can compute a cumulative sum -a sequence of partial sums- via LINQ standard query operators. LINQ expressivity allows us to elaborate complicated or extensive algorithms in a simpler way.
The coming up LINQ recipes are focused in recursive patterns: a pattern that can be expressed using a recurrence relation.
8. Literature & Links
Mukherjee, S (2014). Thinking in LINQ Harnessing the Power of Functional Programming in .NET Applications. United States: Apress.Cumulative Sum -- from Wolfram MathWorld (2016, June 17). Retrieved from: http://mathworld.wolfram.com/CumulativeSum.html
V
No hay comentarios:
Publicar un comentario
Envíe sus comentarios, dudas, sugerencias, críticas. Gracias.