Sequences and Summations
It is important that we are comfortable with reading and manipulating mathematical formulas that involve sequences and sums, because these topics appear frequently in the world of scientific computing and applied mathematics. This section will introduce the notation that we will be using for sequences and summations and how we work with these objects in Python.
Sequence Notation
Before we go into sequences and summations, it is important that we understand the basics of sequences. Very simply, a sequence is an ordered collection of numbers. For example,
If we define the sequence
The notation
Summation Notation
In order to more efficiently denote sums, especially sums over long sequences, we can use summation notation, also known as sigma notation due to the use of the Greek letter “sigma”:
There are several elements involved in summation notation. First, consider a sequence of
One way to think about this is in terms of a for-loop. This summation concept is equivalent to a for-loop over all integers in the range from the lower bound to the upper bound, indexed into the sequence
total = 0
x = [1, 3, -2, 0, 10]
n = len(x)
for i in range(n): # i = 0, 1, ..., n-1
total += x[i]
# this is equivalent to
total = sum(x)
The value of total
in either case will be
It is probably easiest to build an intuition by looking through a few more examples of summation notation.
Example 1:
>>> n = 5
>>> sum(i**2 for i in range(1, n + 1))
55
Example 2:
>>> x = [1, 7, 4, 1, 6, 9, 5, 2, 7]
>>> sum(x[i]**2 for i in range(3, 7))
143
Example 3:
>>> sum((i**2 - i) for i in range(1, 6))
40
We can also sum over several different sequences. Consider the sequences
>>> import numpy as np
>>> A = np.random.rand(5)
>>> B = np.random.rand(7)
>>> sum(A[i]*B[j] for i in range(5) for j in range(7)) == sum(A)*sum(B)
True
Note that the following does not hold
because this would treat the index
>>> import numpy as np
>>> A = np.random.rand(5)
>>> sum(A[i]*A[i] for i in range(5)) == sum(A)*sum(A)
False
Mathematicians are lazy. This means writing as little as possible to communicate, so note that typically when someone writes
Reading Comprehension: Arithmetic Mean
Using the introduced summation notation, write the mean of a sequence
Next, write a Python function called mean
, which takes in a collection of numbers (a list, tuple, set, etc.) called seq
and returns the mean of the sequence using a for-loop. Use this method to calculate the mean of the tuple (1, 5, 6, 9, 2, 5, 8, 1)
.
Kronecker Delta
To make notation for working with summations even simpler (especially those involving matrices), we can use the Kronecker delta function, named after Prussian mathematician Leopold Kronecker. We use
See that a Kronecker delta can “collapse” a sum. If
See also that the identity matrix,
Reading Comprehension: Kronecker Delta
Write a function named kronecker
. It should accept two input argument, named i
and j
, which will be integers. Have the function return
Then, use this function to collapse a sum. Make 2 lists of 5 elements each. The first list, called a
, should contain the numbers [3, 6, 7, 1, 8]
and the other list, called b
, should contain [4, 7, 1, 3, 8]
. Use the kronecker
function to compute and print the sum
Reading Comprehension Exercise Solutions
Arithmetic Mean: Solution
The equation for computing the mean of a collection of numbers, using summation notation, is
def mean(seq):
"""
Parameters
----------
seq : Iterable
Returns
-------
float
The mean of the given sequence
"""
sum_ = 0
for num in seq:
sum_ += num
return sum_ / len(seq)
>>> mean((1, 5, 6, 9, 2, 5, 8, 1))
4.625
Kronecker Delta: Solution
def kronecker(i, j):
"""
Parameters
----------
i : int
j : int
Returns
-------
int
returns 1 if `i` and `j` are equal and returns 0 otherwise
"""
# Recall that `int(True)` returns 1 and `int(False)` returns 0.
return int(i == j)
a = [3, 6, 7, 1, 8]
b = [4, 7, 1, 3, 8]
print(sum(a[i] * b[j] * kronecker(i, j) for i in range(5) for j in range(5)))