ArticleslgStudy

mathematics

Prefix sum

Prefix sum is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Prefix sum rather than just read about it. In short: In computer science, the prefix sum, cumulative sum, inclusive scan, or simply scan of a sequence of numbers x0, x1, x2, ... is a second sequence of numbers y0, y1, y2, ..., the sums of prefixes (running totals) of the input sequence: y0 = x0 y1 = x0 + x1 y2 = x0 + x1+ x2 ... For instance, the prefix sums of the natural numbers are the triangular numbers: Prefix sums are trivial to compute in sequential models of co…

Prefix sum — main illustration
Prefix sum — illustration

Key takeaways

  • Prefix sum belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Prefix sum to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Prefix sum from memory before moving on to harder problems.

Reference excerpt

In computer science, the prefix sum, cumulative sum, inclusive scan, or simply scan of a sequence of numbers x0, x1, x2, ... is a second sequence of numbers y0, y1, y2, ..., the sums of prefixes (running totals) of the input sequence:

y0 = x0 y1 = x0 + x1 y2 = x0 + x1+ x2 ... For instance, the prefix sums of the natural numbers are the triangular numbers:

Prefix sums are trivial to compute in sequential models of computation, by using the formula yi = yi − 1 + xi to compute each output value in sequence order. However, despite their ease of computation, prefix sums are a useful primitive in certain algorithms such as counting sort, and they form the basis of the scan higher-order function in functional programming languages. Prefix sums have also been much studied in parallel algorithms, both as a test problem to be solved and as a useful primitive to be used as a subroutine in other parallel algorithms. Abstractly, a prefix sum requires only a binary associative operator ⊕, making it useful for many applications from calculating well-separated pair decompositions of points to string processing. Mathematically, the operation of taking prefix sums can be generalized from finite to infinite sequences; in that context, a prefix sum is known as a partial sum of a series. Prefix summation or partial summation form linear operators on the vector spaces of finite or infinite sequences; their inverses are finite difference operators.

Scan higher order function In functional programming terms, the prefix sum may be generalized to any binary operation (not just the addition operation); the higher order function resulting from this generalization is called a scan, and it is closely related to the fold operation. Both the scan and the fold operations apply the given binary operation to the same sequence of values, but differ in that the scan returns the whole sequence of results from the binary operation, whereas the fold returns only the final result. For instance, the sequence of factorial numbers may be generated by a scan of the natural numbers using multiplication instead of addition:

Inclusive and exclusive scans

Programming language and library implementations of scan may be either inclusive or exclusive. An inclusive scan includes input xi when computing output yi (i.e., y i = ⨁ j = 0 i x j {\textstyle y_{i}=\bigoplus _{j=0}^{i}x_{j}} ) while an exclusive scan does not (i.e., y i = ⨁ j = 0 i − 1 x j {\textstyle y_{i}=\bigoplus _{j=0}^{i-1}x_{j}} ). In the latter case, implementations either leave y0 undefined or accept a separate "x−1" value with which to seed the scan. Either type of scan can be transformed into the other: an inclusive scan can be transformed into an exclusive scan by shifting the array produced by the scan right by one element and inserting the identity value at the left of the array. Conversely, an exclusive scan be transformed into an inclusive scan by shifting the array produced by the scan left and inserting the sum of the last element of the scan and the last element of the input array at the right of the array. The following table lists examples of the inclusive and exclusive scan functions provided by a few programming languages and libraries:

The directive-based OpenMP parallel programming model supports both inclusive and exclusive scan support beginning with Version 5.0.

Parallel algorithms There are two key algorithms for computing a prefix sum in parallel. The first offers a shorter span and more parallelism but is not work-efficient. The second is work-efficient but requires double the span and offers less parallelism. These are presented in turn below.

Algorithm 1: Shorter span, more parallel

Hillis and Steele present the following parallel prefix sum algorithm:

for i <- 0 to log2(n) do for j <- 0 to n - 1 do in parallel if j < 2i then xi+1j <- xij else xi+1j <- xij + xij - 2i

In the above, the notation x j i {\displaystyle x_{j}^{i}} means the value of the jth element of array x in timestep i. With a single processor this algorithm would run in O(n log n) time. However, if the machine has at least n processors to perform the inner loop in parallel, the algorithm as a whole runs in O(log n) time, the number of iterations of the outer loop.

Algorithm 2: Work-efficient

A work-efficient parallel prefix sum can be computed by the following steps.

Compute the sums of consecutive pairs of items in which the first item of the pair has an even index: z0 = x0 + x1, z1 = x2 + x3, etc. Recursively compute the prefix sum w0, w1, w2, ... of the sequence z0, z1, z2, ... Express each term of the final sequence y0, y1, y2, ... as the sum of up to two terms of these intermediate sequences: y0 = x0, y1 = z0, y2 = z0 + x2, y3 = w1, etc. After the first value, each successive number yi is either copied from a position half as far through the w sequence, or is the previous value added to one value in the x sequence. If the input sequence has n steps, then the recursion continues to a depth of O(log n), which is also the bound on the parallel running time of this algorithm. The number of steps of the algorithm is O(n), and it can be implemented on a parallel random access machine with O(n/log n) processors without any asymptotic slowdown by assigning multiple indices to each processor in rounds of the algorithm for which there are more elements than processors.

… excerpt ends here. Continue reading the full article.

Illustrations

Prefix sum: Circuit representation of a work-efficient 16-input parallel prefix sum
Circuit representation of a work-efficient 16-input parallel prefix sum
Prefix sum: Different hypercubes for varying number of nodes
Different hypercubes for varying number of nodes
Prefix sum: Information exchange between processing elements during upward (blue) and downward (red) phase in the Pipelined Binary Tree Prefix Sum algorithm
Information exchange between processing elements during upward (blue) and downward (red) phase in the Pipelined Binary Tree Prefix Sum algorithm

Worked examples

Example 1 — a first encounter with Prefix sum

Start with the simplest possible case. Write down what Prefix sum claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Prefix sum before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Prefix sum ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Prefix sum

In research
Prefix sum appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Prefix sum in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Prefix sum is common in secondary-school and first-year university syllabi. It links to neighbouring topics Concurrent algorithms, Higher-order functions, so understanding it makes those chapters shorter.
In everyday life
Look for Prefix sum outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Prefix sum” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Prefix sum in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Prefix sum means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Prefix sum out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Prefix sum in simple terms?

In computer science, the prefix sum, cumulative sum, inclusive scan, or simply scan of a sequence of numbers x0, x1, x2, ... is a second sequence of numbers y0, y1, y2, ..., the sums of prefixes (running totals) of the input sequence: y0 = x0 y1 = x0 + x1 y2 = x0 + x1+ x2 ... For instance, the pref…

Why does Prefix sum matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Prefix sum?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Prefix sum.

Tags

  • Concurrent algorithms
  • Higher-order functions

Keep exploring