import numpy as np
print( np.arange(10) )[0 1 2 3 4 5 6 7 8 9]
A sequence is an ordered list of terms. It can be finite or infinite. Some famous sequences include:
| Sequence | Description |
|---|---|
| \(1, 4, 9, 16, 25, \dots\) | The square numbers. The sequence of \(n^2\) for \(n \geq 1\). |
| \(1, 3, 6, 10, 15, 21, \dots\) | The triangular numbers. A sequence representing sums of natural numbers, with the formula \(T_n=\frac{n(n+1)}{2}\). |
| \(2, 3, 5, 7, 11, 13, 17, \dots\) | The prime numbers, which are natural numbers which are not the product of two smaller natural numbers. |
arangeThe NumPy library has a useful function called arange for generating sequences as an array over a particular range.
import numpy as np
print( np.arange(10) )[0 1 2 3 4 5 6 7 8 9]
By default, arange starts at 0 and the stopping number (the 10 above) is excluded. We can pass arange more information about how we want to build the seqeuence in the form: \[\text{np.arange(start, stop, step)}\] which specifies the start, the (excluded) stop, and the step size. The numbers 1 through 10 are generated with:
np.arange(1, 11, 1)array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
The even numbers from 20 through 30:
np.arange(20, 31, 2)array([20, 22, 24, 26, 28, 30])
Here are examples of sequences with NumPy:
| Sequence | Python |
|---|---|
| \(1,2,3,4,5\) | np.arange(1,6) |
| \(1,3,5,7,\dots,99\) | np.arange(1,100,2) |
| \(10,20,30,\dots,100\) | np.arange(10,101,10) |
| \(10,9,8,7,\dots,1\) | np.arange(10,0,-1) |
linspaceAnother useful NumPy function is linspace, which generates a sequence with linearly spaced values. The syntax for linspace is: \[\text{np.linspace(start, stop, num)}\] where \(\text{num}\) is the number of elements in the sequence (as opposed to the step size). The line of code:
np.linspace(0,10,50)array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653,
1.02040816, 1.2244898 , 1.42857143, 1.63265306, 1.83673469,
2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286,
3.06122449, 3.26530612, 3.46938776, 3.67346939, 3.87755102,
4.08163265, 4.28571429, 4.48979592, 4.69387755, 4.89795918,
5.10204082, 5.30612245, 5.51020408, 5.71428571, 5.91836735,
6.12244898, 6.32653061, 6.53061224, 6.73469388, 6.93877551,
7.14285714, 7.34693878, 7.55102041, 7.75510204, 7.95918367,
8.16326531, 8.36734694, 8.57142857, 8.7755102 , 8.97959184,
9.18367347, 9.3877551 , 9.59183673, 9.79591837, 10. ])
creates a numpy array that starts at 0, ends at 10, and has a total of 50 equally spaced elements in the array. The linspace function will be particularly useful for graphing functions in Section 7.1.
Given a sequence as a NumPy array, we can perform math operations which apply to every element in the sequence.
x = np.array([1, 2, 3, 4, 5])
x + 4 # [5, 6, 7, 8, 9, 10]
x - 1 # [0, 1, 2, 3, 4]
5 * x # [5, 10, 15, 20, 25]
x / 4 # [0.25, 0.5, 0.75, 1., 1.25]
x**2 # [1, 4, 9, 16, 25]Given two sequences, it is important to know that the standard operations are performed element-wise, meaning that the operation is performed between matching pairs of elements.
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
x + y # [5, 7, 9]
x - y # [-3, -3, -3]
x * y # [4, 10, 18]
x / y # [0.25, 0.4, 0.5]A famous math problem asks for the sum of the numbers from 1 to 100, \[\sum_{n=1}^{100}n\] which we can find quickly with:
np.sum( np.arange(1,101,1) )5050
Given a sum with a more complex expression, we can find that sum by initializing the iteration variable and then writing the expression. For example,
\[\sum_{k=1}^{20}k^2+3k+2\] can be found with:
k = np.arange(1,21,1) # Creates sequence k = 1 through 20
np.sum( k**2 + 3*k + 2 ) # Sums k^2+3k+2 for each element in k 3540
Here are some more examples:
| Series | Python |
|---|---|
| \[\sum_{n=0}^{10}\frac{1}{2^{n}}\] | n = np.arange(11)np.sum( 1/2**n ) |
| \[\sum_{m=5}^{15}\frac{m+m^3}{3+m}\] | m = np.arange(5,16)np.sum( (m+m^3) / (3+m) ) |
| \[\sum_{x=1}^{20}3x-5\] | x = np.arange(1,21)np.sum( 3*x-5 ) |
Generate two NumPy arrays, each representing one million rolls of a die. Create a histogram of their sum. Be sure to specify the correct number of bins and label the plot.
Create each of the following arithmetic sequences using a single line of code for each:
Write the most concise NumPy expressions you can to evaluate the summations of the following sequences:
Find the value of the following sums using NumPy:
| \[\sum_{k=1}^{5}k^2-3\] | \[\sum_{n=1}^{5}(4-n^2)\] | \[\sum_{m=1}^{10}\left(4m^2-2\right)\] |
| \[\frac{7}{4}+\frac{9}{5}+\frac{11}{6}+\dots+\frac{107}{54}\] | \[\sum_{h=4}^{9}h(h+5)\] | \[\sum_{m=4}^{9}\frac{m^2+1}{m}\] |
| \[\sum_{x=-4}^{4}x^2+4x+5\] | \[\sum_{n=1}^{100}\frac{1}{n}\] | \[\frac{10}{1}+\frac{20}{2}+\frac{30}{3}+\dots+\frac{100}{10}\] |