<jupyter_start><jupyter_text>NumPy[NumPy](http://www.numpy.org/) is the core Python package for numerical computing. The main features of NumPy are:* $N$-dimensional array object `ndarray`* Vectorized operations and functions which broadcast across arrays for fast computationTo get started with NumPy, let's adopt the standard convention and import it using the name `np`:<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>NumPy ArraysThe fundamental object provided by the NumPy package is the `ndarray`. We can think of a 1D (1-dimensional) `ndarray` as a list, a 2D (2-dimensional) `ndarray` as a matrix, a 3D (3-dimensional) `ndarray` as a 3-tensor (or a "cube" of numbers), and so on. See the [NumPy tutorial](https://docs.scipy.org/doc/numpy/user/quickstart.html) for more about NumPy arrays.  Creating ArraysThe function `numpy.array` creates a NumPy array from a Python sequence such as a list, a tuple or a list of lists. For example, create a 1D NumPy array from a Python list:<jupyter_code>a = np.array([1,2,3,4,5])
print(a)<jupyter_output>[1 2 3 4 5]<jupyter_text>Notice that when we print a NumPy array it looks a lot like a Python list except that the entries are separated by spaces whereas entries in a Python list are separated by commas:<jupyter_code>print([1,2,3,4,5])<jupyter_output>[1, 2, 3, 4, 5]<jupyter_text>Notice also that a NumPy array is displayed slightly differently when output by a cell (as opposed to being explicitly printed to output by the `print` function):<jupyter_code>a<jupyter_output><empty_output><jupyter_text>Use the built-in function `type` to verify the type:<jupyter_code>type(a)<jupyter_output><empty_output><jupyter_text>Create a 2D NumPy array from a Python list of lists:<jupyter_code>M = np.array([[1,2,3],[4,5,6]])
print(M)
type(M)<jupyter_output><empty_output><jupyter_text>Create an $n$-dimensional NumPy array from nested Python lists. For example, the following is a 3D NumPy array:<jupyter_code>N = np.array([ [[1,2],[3,4]] , [[5,6],[7,8]] , [[9,10],[11,12]] ])
print(N)<jupyter_output>[[[ 1  2]
  [ 3  4]]

 [[ 5  6]
  [ 7  8]]

 [[ 9 10]
  [11 12]]]<jupyter_text>There are several NumPy functions for [creating arrays](https://docs.scipy.org/doc/numpy/user/quickstart.htmlarray-creation):| Function | Description || ---: | :--- || `numpy.array(a)` | Create $n$-dimensional NumPy array from sequence `a` || `numpy.linspace(a,b,N)` | Create 1D NumPy array with `N` equally spaced values from `a` to `b` (inclusively)|| `numpy.arange(a,b,step)` | Create 1D NumPy array with values from `a` to `b` (exclusively) incremented by `step`|| `numpy.zeros(N)` | Create 1D NumPy array of zeros of length $N$ || `numpy.zeros((n,m))` | Create 2D NumPy array of zeros with $n$ rows and $m$ columns || `numpy.ones(N)` | Create 1D NumPy array of ones of length $N$ || `numpy.ones((n,m))` | Create 2D NumPy array of ones with $n$ rows and $m$ columns || `numpy.eye(N)` | Create 2D NumPy array with $N$ rows and $N$ columns with ones on the diagonal (ie. the identity matrix of size $N$) | Create a 1D NumPy array with 11 equally spaced values from 0 to 1:<jupyter_code>x = np.linspace(0,1,11)
print(x)<jupyter_output>[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]<jupyter_text>Create a 1D NumPy array with values from 0 to 20 (exclusively) incremented by 2.5:<jupyter_code>y = np.arange(0,20,2.5)
print(y)<jupyter_output>[ 0.   2.5  5.   7.5 10.  12.5 15.  17.5]<jupyter_text>These are the functions that we'll use most often when creating NumPy arrays. The function `numpy.linspace` works best when we know the *number of points* we want in the array, and `numpy.arange` works best when we know *step size* between values in the array. Create a 1D NumPy array of zeros of length 5:<jupyter_code>z = np.zeros(5)
print(z)<jupyter_output>[0. 0. 0. 0. 0.]<jupyter_text>Create a 2D NumPy array of zeros with 2 rows and 5 columns:<jupyter_code>M = np.zeros((2,5))
print(M)<jupyter_output>[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]<jupyter_text>Create a 1D NumPy array of ones of length 7:<jupyter_code>w = np.ones(7)
print(w)<jupyter_output>[1. 1. 1. 1. 1. 1. 1.]<jupyter_text>Create a 2D NumPy array of ones with 3 rows and 2 columns:<jupyter_code>N = np.ones((3,2))
print(N)<jupyter_output>[[1. 1.]
 [1. 1.]
 [1. 1.]]<jupyter_text>Create the identity matrix of size 10:<jupyter_code>I = np.eye(10)
print(I)<jupyter_output>[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]<jupyter_text>Array DatatypeNumPy arrays are *homogeneous*: all entries in the array are the same datatype. We will only work with numeric arrays and our arrays will contain either integers, floats, complex numbers or booleans. There are different kinds of datatypes provided by NumPy for different applications but we'll mostly be working with the default integer type `numpy.int64` and the default float type `numpy.float64`. These are very similar to the built-in Python datatypes `int` and `float` but with some differences that we won't go into. Check out the NumPy documentation on [numeric datatypes](https://docs.scipy.org/doc/numpy/user/basics.types.html) for more information.The most important point for now is to know how to determine if a NumPy array contains integers elements or float elements. We can access the datatype of a NumPy array by its `.dtype` attribute. For example, create a 2D NumPy array from a list of lists of integers:<jupyter_code>A = np.array([[1,2,3],[4,5,6]])
print(A)<jupyter_output>[[1 2 3]
 [4 5 6]]<jupyter_text>We expect the datatype of `A` to be integers and we verify:<jupyter_code>A.dtype<jupyter_output><empty_output><jupyter_text>Most of the other NumPy functions which create arrays use the `numpy.float64` datatype by default. For example, using `numpy.linspace`:<jupyter_code>u = np.linspace(0,1,5)
print(u)
u.dtype<jupyter_output><empty_output><jupyter_text>Notice that numbers are printed with a decimal point when the datatype of the NumPy array is any kind of float.  Dimension, Shape and SizeWe can think of a 1D NumPy array as a list of numbers, a 2D NumPy array as a matrix, a 3D NumPy array as a cube of numbers, and so on. Given a NumPy array, we can find out how many dimensions it has by accessing its `.ndim` attribute. The result is a number telling us how many dimensions it has.For example, create a 2D NumPy array:<jupyter_code>A = np.array([[1,2],[3,4],[5,6]])
print(A)
A.ndim<jupyter_output><empty_output><jupyter_text>The result tells us that `A` has 2 dimensions. The first dimension corresponds to the vertical direction counting the rows and the second dimension corresponds to the horizontal direction counting the columns.We can find out how many rows and columns `A` has by accessing its `.shape` attribute:<jupyter_code>A.shape<jupyter_output><empty_output><jupyter_text>The result is a tuple `(3,2)` of length 2 which means that `A` is a 2D array with 3 rows and 2 columns.We can also find out how many entries `A` has in total by accessing its `.size` attribute:<jupyter_code>A.size<jupyter_output><empty_output><jupyter_text>This is the expected result since we know that `A` has 3 rows and 2 columns and therefore 2(3) = 6 total entries. Create a 1D NumPy array and inspect its dimension, shape and size:<jupyter_code>r = np.array([9,3,1,7])
print(r)
r.ndim
r.shape
r.size<jupyter_output><empty_output><jupyter_text>The variable `r` is assigned to a 1D NumPy array of length 4. Notice that `r.shape` is a tuple with a single entry `(4,)`.  Slicing and IndexingAccessing the entries in an array is called *indexing* and accessing rows and columns (or subarrays) is called *slicing*. See the NumPy documentation for more information about [indexing and slicing](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html). Create a 1D NumPy array:<jupyter_code>v = np.linspace(0,5,11)
print(v)<jupyter_output>[0.  0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5 5. ]<jupyter_text>Access the entries in a 1D array using the square brackets notation just like a Python list. For example, access the entry at index 3:<jupyter_code>v[3]<jupyter_output><empty_output><jupyter_text>Notice that NumPy array indices start at 0 just like Python sequences. Create a 2D array of integers:<jupyter_code>B = np.array([[6, 5, 3, 1, 1],[1, 0, 4, 0, 1],[5, 9, 2, 2, 9]])
print(B)<jupyter_output>[[6 5 3 1 1]
 [1 0 4 0 1]
 [5 9 2 2 9]]<jupyter_text>Access the entries in a 2D array using the square brackets with 2 indices. In particular, access the entry at row index 1 and column index 2:<jupyter_code>B[1,2]<jupyter_output><empty_output><jupyter_text>Access the top left entry in the array:<jupyter_code>B[0,0]<jupyter_output><empty_output><jupyter_text>Negative indices work for NumPy arrays as they do for Python sequences. Access the bottom right entry in the array:<jupyter_code>B[-1,-1]<jupyter_output><empty_output><jupyter_text>Access the row at index 2 using the colon `:` syntax:<jupyter_code>B[2,:]<jupyter_output><empty_output><jupyter_text>Access the column at index 3:<jupyter_code>B[:,3]<jupyter_output><empty_output><jupyter_text>Select the subarray of rows at index 1 and 2, and columns at index 2, 3 and 4:<jupyter_code>subB = B[1:3,2:5]
print(subB)<jupyter_output>[[4 0 1]
 [2 2 9]]<jupyter_text>Slices of NumPy arrays are again NumPy arrays but possibly of a different dimension:<jupyter_code>subB.ndim
subB.shape
type(subB)<jupyter_output><empty_output><jupyter_text>The variable `subB` is assigned to a 2D NumPy array of shape 2 by 2. Let's do the same for the column at index 2:<jupyter_code>colB = B[:,2]
print(colB)
colB.ndim
colB.shape
type(colB)<jupyter_output><empty_output><jupyter_text>The variable `colB` is assigned to a 1D NumPy array of length 3.  StackingWe can build bigger arrays out of smaller arrays by [stacking](https://docs.scipy.org/doc/numpy/user/quickstart.htmlstacking-together-different-arrays) along different dimensions using the functions `numpy.hstack` and `numpy.vstack`.Stack 3 different 1D NumPy arrays of length 3 vertically forming a 3 by 3 matrix:<jupyter_code>x = np.array([1,1,1])
y = np.array([2,2,2])
z = np.array([3,3,3])
vstacked = np.vstack((x,y,z))
print(vstacked)<jupyter_output>[[1 1 1]
 [2 2 2]
 [3 3 3]]<jupyter_text>Stack 1D NumPy arrays horizontally to create another 1D array:<jupyter_code>hstacked = np.hstack((x,y,z))
print(hstacked)<jupyter_output>[1 1 1 2 2 2 3 3 3]<jupyter_text>Use `numpy.hstack` and `numpy.vstack` to build the matrix $T$ where$$T = \begin{bmatrix}1 & 1 & 2 & 2 \\ 1 & 1 & 2 & 2 \\ 3 & 3 & 4 & 4 \\ 3 & 3 & 4 & 4\end{bmatrix}$$<jupyter_code>A = np.ones((2,2))
B = 2*np.ones((2,2))
C = 3*np.ones((2,2))
D = 4*np.ones((2,2))
A_B = np.hstack((A,B))
print(A_B)
C_D = np.hstack((C,D))
print(C_D)
T = np.vstack((A_B,C_D))
print(T)<jupyter_output>[[1. 1. 2. 2.]
 [1. 1. 2. 2.]
 [3. 3. 4. 4.]
 [3. 3. 4. 4.]]<jupyter_text>Copies versus Views*Under construction*  Operations and Functions  Array Operations[Arithmetic operators](https://docs.scipy.org/doc/numpy/user/quickstart.htmlbasic-operations) including addition `+`, subtraction `-`, multiplication `*`, division `/` and exponentiation `**` are applied to arrays *elementwise*. For addition and substraction, these are the familiar vector operations we see in linear algebra:<jupyter_code>v = np.array([1,2,3])
w = np.array([1,0,-1])
v + w
v - w<jupyter_output><empty_output><jupyter_text>In the same way, array multiplication and division are performed element by element:<jupyter_code>v * w
w / v<jupyter_output><empty_output><jupyter_text>Notice that the datatype of both `v` and `w` is `numpy.int64` however division `w / v` returns an array with datatype `numpy.float64`. The exponent operator `**` also acts element by element in the array:<jupyter_code>v ** 2<jupyter_output><empty_output><jupyter_text>Let's see these operations for 2D arrays:<jupyter_code>A = np.array([[3,1],[2,-1]])
B = np.array([[2,-2],[5,1]])
A + B
A - B
A / B
A * B
A ** 2<jupyter_output><empty_output><jupyter_text>Notice that array multiplication and exponentiation are performed elementwise.In Python 3.5+, the symbol `@` computes matrix multiplication for NumPy arrays:<jupyter_code>A @ B<jupyter_output><empty_output><jupyter_text>[Matrix powers](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.matrix_power.html) are performed by the function `numpy.linalg.matrix_power`. It's a long function name and so it's convenient to import it with a shorter name:<jupyter_code>from numpy.linalg import matrix_power as mpow<jupyter_output><empty_output><jupyter_text>Compute $A^3$:<jupyter_code>mpow(A,3)<jupyter_output><empty_output><jupyter_text>Equivalently, use the `@` operator to compute $A^3$:<jupyter_code>A @ A @ A<jupyter_output><empty_output><jupyter_text>BroadcastingWe know from linear algebra that we can only add matrices of the same size. [Braodcasting](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) is a set of NumPy rules which relaxes this constraint and allows us to combine a smaller array with a bigger when it makes sense.For example, suppose we want to create a 1D NumPy array of $y$ values for $x=0.0,0.25,0.5,0.75,1.0$ for the function $y = x^2 + 1$. From what we've seen so far, it makes sense to create `x`, then `x**2` and then add an array of ones `[1. 1. 1. 1. 1.]`:<jupyter_code>x = np.array([0,0.25,0.5,0.75,1.0])
y = x**2 + np.array([1,1,1,1,1])
print(y)<jupyter_output>[1.     1.0625 1.25   1.5625 2.    ]<jupyter_text>An example of broadcasting in NumPy is the following equivalent operation:<jupyter_code>x = np.array([0,0.25,0.5,0.75,1.0])
y = x**2 + 1
print(y)<jupyter_output>[1.     1.0625 1.25   1.5625 2.    ]<jupyter_text>The number 1 is a scalar and we are adding it to a 1D NumPy array of length 5. The broadcasting rule in this case is to broadcast the scalar value 1 across the larger array. The result is a simpler syntax for a very comman operation.Let's try another example. What happens when we try to add a 1D NumPy array of length 4 to a 2D NumPy array of size 3 by 4?<jupyter_code>u = np.array([1,2,3,4])
print(u)
A = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3]])
print(A)
result = A + u
print(result)<jupyter_output>[[2 3 4 5]
 [3 4 5 6]
 [4 5 6 7]]<jupyter_text>The 1D NumPy array is broadcast across the 2D array because the length of the first dimension in each array are equal!  Array FunctionsThere are *many* [array functions](https://docs.scipy.org/doc/numpy/reference/routines.html) we can use to compute with NumPy arrays. The following is a partial list and we'll look closer at mathematical functions in the next section.| | | || :---: | :---: | :---: || `numpy.sum` | `numpy.prod` | `numpy.mean` || `numpy.max` | `numpy.min` | `numpy.std` || `numpy.argmax` | `numpy.argmin` | `numpy.var` | Create a 1D NumPy array with random values and compute:<jupyter_code>arr = np.array([8,-2,4,7,-3])
print(arr)<jupyter_output>[ 8 -2  4  7 -3]<jupyter_text>Compute the mean of the values in the array:<jupyter_code>np.mean(arr)<jupyter_output><empty_output><jupyter_text>Verify the mean once more:<jupyter_code>m = np.sum(arr) / arr.size
print(m)<jupyter_output>2.8<jupyter_text>Find the index of the maximum element in the array:<jupyter_code>max_i = np.argmax(arr)
print(max_i)<jupyter_output>0<jupyter_text>Verify the maximum value in the array:<jupyter_code>np.max(arr)
arr[max_i]<jupyter_output><empty_output><jupyter_text>Array functions apply to 2D arrays as well (and $N$-dimensional arrays in general) with the added feature that we can choose to apply array functions to the entire array, down the columns or across the rows (or any axis).Create a 2D NumPy array with random values and compute the sum of all the entries:<jupyter_code>M = np.array([[2,4,2],[2,1,1],[3,2,0],[0,6,2]])
print(M)
np.sum(M)<jupyter_output><empty_output><jupyter_text>The function `numpy.sum` also takes a keyword argument `axis` which determines along which dimension to compute the sum:<jupyter_code>np.sum(M,axis=0) # Sum of the columns
np.sum(M,axis=1) # Sum of the rows<jupyter_output><empty_output><jupyter_text>Mathematical Functions[Mathematical functions](http://docs.scipy.org/doc/numpy/reference/routines.math.html) in NumPy are called [universal functions](https://docs.scipy.org/doc/numpy/user/quickstart.htmluniversal-functions) and are *vectorized*. Vectorized functions operate *elementwise* on arrays producing arrays as output and are built to compute values across arrays *very* quickly. The following is a partial list of mathematical functions:| | | || :---: | :---: | :---: || `numpy.sin` | `numpy.cos` | `numpy.tan` || `numpy.exp` | `numpy.log` | `numpy.log10` || `numpy.arcsin` | `numpy.arccos` | `numpy.arctan` |Compute the values $\sin(2 \pi x)$ for $x = 0,0.25,0.5\dots,1.75$:<jupyter_code>x = np.arange(0,1.25,0.25)
print(x)
np.sin(2*np.pi*x)<jupyter_output><empty_output><jupyter_text>We expect the array `[0. 1. 0. -1. 0.]` however there is (as always with floating point numbers) some rounding errors in the result. In numerical computing, we can interpret a number such as $10^{-16}$ as $0$. Compute the values $\log_{10}(x)$ for $x = 1,10,100,1000,10000$:<jupyter_code>x = np.array([1,10,100,1000,10000])
print(x)
np.log10(x)<jupyter_output><empty_output><jupyter_text>Note that we can also evaluate mathematical functions with scalar values:<jupyter_code>np.sin(0)<jupyter_output><empty_output><jupyter_text>NumPy also provides familiar mathematical constants such as $\pi$ and $e$:<jupyter_code>np.pi
np.e<jupyter_output><empty_output><jupyter_text>For example, verify the limit$$\lim_{x \to \infty} \arctan(x) = \frac{\pi}{2}$$by evaluating $\arctan(x)$ for some (arbitrary) large value $x$:<jupyter_code>np.arctan(10000)
np.pi/2<jupyter_output><empty_output><jupyter_text>Random Number GeneratorsThe subpackage `numpy.random` contains functions to generate NumPy arrays of [random numbers](https://docs.scipy.org/doc/numpy/reference/routines.random.html) sampled from different distributions. The following is a partial list of distributions:| Function | Description || :--- | :--- || `numpy.random.rand(d1,...,dn)` | Create a NumPy array (with shape `(d1,...,dn)`) with entries sampled uniformly from `[0,1)` || `numpy.random.randn(d1,...,dn)` | Create a NumPy array (with shape `(d1,...,dn)`) with entries sampled from the standard normal distribution || `numpy.random.randint(a,b,size)` | Create a NumPy array (with shape `size`) with integer entries from `low` (inclusive) to `high` (exclusive) | Sample a random number from the [uniform distribution](https://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29):<jupyter_code>np.random.rand()<jupyter_output><empty_output><jupyter_text>Sample 3 random numbers:<jupyter_code>np.random.rand(3)<jupyter_output><empty_output><jupyter_text>Create 2D NumPy array of random samples:<jupyter_code>np.random.rand(2,4)<jupyter_output><empty_output><jupyter_text>Random samples from the [standard normal distribution](https://en.wikipedia.org/wiki/Normal_distribution):<jupyter_code>np.random.randn()
np.random.randn(3)
np.random.randn(3,1)<jupyter_output><empty_output><jupyter_text>Random integers sampled uniformly from various intervals:<jupyter_code>np.random.randint(-10,10)
np.random.randint(0,2,(4,8))
np.random.randint(-9,10,(5,2))<jupyter_output><empty_output><jupyter_text>Examples  Brute Force OptimizationFind the [absolute maximum and minimum](https://en.wikipedia.org/wiki/Maxima_and_minima) values of the function$$f(x) = x\sin(x)+\cos(4x)$$on the interval $[0,2\pi]$. We know that the maximum and minimum values must occur at either the endpoints $x=0,2\pi$ or at critical points where $f'(x)=0$. However, the derivative is given by$$f'(x) = \sin(x) + x \cos(x) - 4\sin(4x)$$and the equation $f'(x) = 0$ is impossible to solve explicitly.Instead, create a 1D NumPy array of $x$ values from $0$ to $2\pi$ of length $N$ (for some arbitrarily large value $N$) and use the functions `numpy.min` and `numpy.max` to find maximum and minimum $y$ values, and the functions `numpy.argmin` and `numpy.argmax` to find the indices of the corresponding $x$ values.<jupyter_code>N = 10000
x = np.linspace(0,2*np.pi,N)
y = x * np.sin(x) + np.cos(4*x)
y_max = np.max(y)
y_min = np.min(y)
x_max = x[np.argmax(y)]
x_min = x[np.argmin(y)]
print('Absolute maximum value is y =',y_max,'at x =',x_max)
print('Absolute minimum value is y =',y_min,'at x =',x_min)<jupyter_output>Absolute maximum value is y = 2.5992726072887007 at x = 1.628136126702901
Absolute minimum value is y = -5.129752039182 at x = 5.34187001663503<jupyter_text>Riemann SumsWrite a function called `exp_int` which takes input parameters $b$ and $N$ and returns the (left) [Riemann sum](https://en.wikipedia.org/wiki/Riemann_sum)$$\int_0^b e^{-x^2} dx \approx \sum_{k=0}^{N-1} e^{-x_k^2} \Delta x$$for $\Delta x = b/N$ and the partition $x_k=k \, \Delta x$, $k=0,\dots,N$.<jupyter_code>def exp_int(b,N):
    "Compute left Riemann sum of exp(-x^2) from 0 to b with N subintervals."
    x = np.linspace(0,b,N+1)
    x_left_endpoints = x[:-1]
    Delta_x = b/N
    I = Delta_x * np.sum(np.exp(-x_left_endpoints**2))
    return I<jupyter_output><empty_output><jupyter_text>The infinite integral satisfies the [beautiful identity](https://en.wikipedia.org/wiki/Gaussian_integral)$$\int_0^{\infty} e^{-x^2} dx = \frac{\sqrt{\pi}}{2}$$Compute the integral with large values of $b$ and $N$:<jupyter_code>exp_int(100,100000)<jupyter_output><empty_output><jupyter_text>Compare to the true value:<jupyter_code>np.pi**0.5/2<jupyter_output><empty_output><jupyter_text>Infinite ProductsThe cosine function has the following infinite product representation$$\cos x = \prod_{k = 1}^{\infty} \left(1 - \frac{4 x^2}{\pi^2 (2k - 1)^2} \right)$$Write a function called `cos_product` which takes input parameters $x$ and $N$ and returns the $N$th partial product$$\prod_{k = 1}^{N} \left(1 - \frac{4 x^2}{\pi^2 (2k - 1)^2} \right)$$<jupyter_code>def cos_product(x,N):
    "Compute the product \prod_{k=1}^N (1 - 4x^2/(pi^2 (2k - 1)^2)."
    k = np.arange(1,N+1)
    terms = 1 - 4*x**2 / (np.pi**2 * (2*k - 1)**2)
    return np.prod(terms)<jupyter_output><empty_output><jupyter_text>Verify our function using values for which we know the result. For example, $\cos(0)=1$, $\cos(\pi)=-1$ and $\cos(\pi/4) = \frac{1}{\sqrt{2}}$.<jupyter_code>cos_product(0,10)
cos_product(np.pi,10000)
cos_product(np.pi/4,10000000)
1/2**0.5<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13729300066239788, 'fraction_numerical': 0.04173106646058733, 'mean_word_length': 5.123850730124391, 'pattern_counts': {'":': 0, '<': 253, '<?xml version=': 0, '>': 253, 'https://': 16, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '69', 'dir': 'jupyter-structured-clean-dedup', 'id': '38169', 'license': 'MIT', 'n_tokens_mistral': 8880, 'n_tokens_neox': 8218, 'n_words': 2599, 'path': 'Python/3. Computational Sciences and Mathematics/Intro To Numpy.ipynb', 'repo_name': 'okara83/Becoming-a-Data-Scientist', 'size': '22645'}