Sigma Notation
Sigma notation is the shorthand for long sums: for "add these up". This reference lesson covers the notation, its algebra rules, the closed forms for sums of powers and telescoping sums. You need them for Riemann sums and series, and every loop that accumulates a total is one.
The notation
is the index (a dummy variable: any letter works), and are the lower and upper limits, and is the term. Some examples:
As a program, is literally sum(a(i) for i in range(m, n + 1)). Watch the off-by-one: Python's range excludes the upper end, sigma notation includes it.
Rules
| Rule | Statement |
|---|---|
| Constant multiple | |
| Sum and difference | |
| Constant term | |
| Index shift | |
| Split the range |
There is no product rule: is not , and is not .
Closed forms for power sums
Each is proved by induction. For example, for the sum of squares: it holds for (), and if it holds for then
which is the formula with in place of . SymPy finds these closed forms symbolically:
import sympy as sp
i, n = sp.symbols('i n', integer=True, positive=True)
for term in [i, i**2, i**3, 2*i - 1]:
print(f"sum of {str(term):6} for i=1..n =", sp.factor(sp.summation(term, (i, 1, n))))
print("check n = 100:", sum(k**2 for k in range(1, 101)), 100 * 101 * 201 // 6)sum of i for i=1..n = n*(n + 1)/2
sum of i**2 for i=1..n = n*(n + 1)*(2*n + 1)/6
sum of i**3 for i=1..n = n**2*(n + 1)**2/4
sum of 2*i - 1 for i=1..n = n**2
check n = 100: 338350 338350
Notice the last one: the first odd numbers always add up to a perfect square.
Telescoping sums
When each term is a difference , almost everything cancels:
This is the discrete version of the Fundamental Theorem of Calculus, and the trick behind several series in Series.
Sums and areas
The power sums let you compute a Riemann sum exactly and then take the limit. With equal strips under on and right endpoints :
That is exactly how areas are first defined in Areas and Distances. The gentle survey of sums is Riemann Sums.
from fractions import Fraction
def right_sum(n):
return sum(Fraction(i, n)**2 * Fraction(1, n) for i in range(1, n + 1))
for n in [4, 10, 100, 1000]:
exact = Fraction(n * (n + 1) * (2 * n + 1), 6 * n**3)
print(n, right_sum(n) == exact, float(exact))4 True 0.46875
10 True 0.385
100 True 0.33835
1000 True 0.3338335
In software
- Loops are sums. A nested loop
for i in range(n): for j in range(i): ...runs times, which is why it is . Insertion sort's worst case and comparing every pair in a list both give this count. A triple loop over gives roughly . - Closed forms beat loops. Summing 1..n with a formula is ; a loop is . Interview problems ("find the missing number in 1..n") use .
- Geometric sums give the cost of doubling arrays (, so appends are amortised) and the total of a geometric decay schedule.
Exercises
Exercise 1: Expand and evaluate
Evaluate .
Answer
Solution
. With the rules: .
Exercise 2: Use the formulas
Find a closed form for and evaluate it for .
Answer
, which is 1770 for .
Solution
import sympy as sp
i, n = sp.symbols('i n', integer=True, positive=True)
s = sp.summation((2*i + 1)**2, (i, 1, n))
print(sp.factor(s), s.subs(n, 10), sum((2*k + 1)**2 for k in range(1, 11)))n*(4*n**2 + 12*n + 11)/3 1770 1770
Exercise 3: Telescope
Evaluate and .
Answer
; and .
Solution
The first telescopes to . For the second, ; the sum keeps the first two positive terms and the last two negative ones:
Check in code:
from fractions import Fraction
s = sum(Fraction(1, i * i - 1) for i in range(2, 100))
print(s, float(s))14651/19800 0.7399494949494949
Exercise 4: Count loop iterations
How many times does the innermost line run in for i in range(n): for j in range(i, n): count += 1? Give the formula and check it for .
Answer
, which is 1275 for .
Solution
For each the inner loop runs times: .
n, count = 50, 0
for i in range(n):
for j in range(i, n):
count += 1
print(count, n * (n + 1) // 2)1275 1275
Test yourself
What is ?
- 5000
- 5050
- 10100
- 4950
Which equation is FALSE in general?
A loop compares every pair with in a list of items. How many comparisons?