Brief notes and practical examples with the summation operator.
import numpy as np
From the Greek alphabet, the capital Sigma letter, represented by the symbol $\large \Sigma$, is mathematical notation for summation. This operator has as function the sum or addition of multiples elements of a sequence. It is commonly associated to another mathematical symbol (greek capital Pi, $\large \Pi$), but its functionality is easily memorized by the sound of the word "sum": Sssum = Sssigma.
$$ \large \sum_{i=m}^n x_i = x_m + x_{m+1} + x_{m+2} + \cdots + x_{n-2} + x_{n-1} + x_{n} $$where:
For example, given the explicit sequence [1, 3, 7, 9, 13], the summation of all 5 elements is denoted as:
$$ \large \begin{aligned} \sum_{i=1}^5 x_i &= x_1 + x_2 + x_3 + x_4 + x_5 \\ &= 1 + 3 + 7 + 9 + 13 \\ &= 33 \end{aligned} $$seq = [1, 3, 7, 9, 13]
result = 0
for x in seq:
result += x
print(result)
33
Notice that in many algorithms the lower and the upper bounds are represented by the interval $[0,N-1]$, which denote the index bound of a digital array. Although, the main idea is still the same.
$$ \large \sum_{k=0}^{N-1} = \sum_{k}^N $$x = [1, 3, 7, 9, 13]
result = x[0] + x[1] + x[2] + x[3] + x[4]
print(result)
33
The library Numpy has a very convenient method that makes the summing process easier and much more readable.
x = np.array([1, 3, 7, 9, 13])
result = x.sum() # or np.sum(x)
print(result)
33
This operator can be nested:
$$ \large \begin{aligned} \sum_{i=2}^4 \sum_{j=5}^6 (i \cdot j) &= (2 \cdot 5 + 2 \cdot 6) + (3 \cdot 5 + 3 \cdot 6) + (4 \cdot 5 + 4 \cdot 6) \\ &= (10 + 12) + (15 + 18) + (20 + 24) \\ &= 22 + 33 + 44 \\ &= 99 \end{aligned} $$result = 0
for i in range(2, 4+1):
for j in range(5, 6+1):
result += i*j
print(result)
99
Nested summations are very commonly used to notate multidimensional isotropic or anisotropic objects. For example, given the matrix:
$$ \large M = \begin{bmatrix} 1 & 2 \\ 2 & 3 \\ 3 & 4 \end{bmatrix} $$The sum of all elements can be expressed as:
$$ \large \begin{aligned} \sum_i^M \sum_j^N M_{ij} &= 1 + 2 + 2 + 3 + 3 + 4 \\ &= 15 \end{aligned} $$matrix = np.array([
[1, 2],
[2, 3],
[3, 4]
])
M = len(matrix)
N = len(matrix[0])
result = 0
for i in range(M):
for j in range(N):
result += matrix[i][j]
print(result, np.sum(matrix))
15 15
N = 10
c = 2
print(np.sum([c for _ in range(N)]))
print(N*c)
print(np.sum([c]*N))
20 20 20
x = np.array([1, 20, 3, 40])
y = np.array([10, 2, 30, 4])
print(np.sum(x) - np.sum(y))
print(np.sum(x - y))
18 18
K = np.arange(1e6) + 1
result = np.sum(1/K**2)
print(result, np.pi**2/6)
1.6449330668487268 1.6449340668482264
K = np.arange(100) + 1
result = np.sum((-1)**(K+1)/K**2)
print(result)
0.8224175333741284