{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 2: Vectors, matrices and multidimensional arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Robert Johansson\n", "\n", "Source code listings for [Numerical Python - Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib](https://www.apress.com/us/book/9781484242452) (ISBN 978-1-484242-45-2)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The NumPy array object" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data = np.array([[1, 2], [3, 4], [5, 6]])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(data)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4],\n", " [5, 6]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.ndim" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 2)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.size" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.dtype" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "48" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.nbytes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data types" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1, 2, 3], dtype=np.int)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3.])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1, 2, 3], dtype=np.float)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.+0.j, 2.+0.j, 3.+0.j])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1, 2, 3], dtype=np.complex)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "data = np.array([1, 2, 3], dtype=np.float)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3.])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.dtype" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "data = np.array([1, 2, 3], dtype=np.int)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.dtype" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "data = np.array([1, 2, 3], dtype=np.float)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3.])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.astype(np.int)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "d1 = np.array([1, 2, 3], dtype=float)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "d2 = np.array([1, 2, 3], dtype=complex)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2.+0.j, 4.+0.j, 6.+0.j])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1 + d2" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('complex128')" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(d1 + d2).dtype" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/rob/miniconda/envs/py3.6-npm-e2/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in sqrt\n", " \"\"\"Entry point for launching an IPython kernel.\n" ] }, { "data": { "text/plain": [ "array([ nan, 0., 1.])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sqrt(np.array([-1, 0, 1]))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.+1.j, 0.+0.j, 1.+0.j])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sqrt(np.array([-1, 0, 1], dtype=complex))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Real and imaginary parts" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "data = np.array([1, 2, 3], dtype=complex)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.+0.j, 2.+0.j, 3.+0.j])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3.])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.real" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0., 0., 0.])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.imag" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arrays created from lists and other array-like objects" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1, 2, 3, 4])" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.ndim" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[1, 2], [3, 4]])" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.ndim" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arrays filled with constant values" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0.],\n", " [ 0., 0., 0.]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((2, 3))" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 1., 1., 1.])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones(4)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "data = np.ones(4)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.dtype" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "data = np.ones(4, dtype=np.int64)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.dtype" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "x1 = 5.4 * np.ones(10)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "x2 = np.full(10, 5.4)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "x1 = np.empty(5)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "x1.fill(3.0)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 3., 3., 3., 3., 3.])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x1" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "x2 = np.full(5, 3.0)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 3., 3., 3., 3., 3.])" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arrays filled with incremental sequences" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(0.0, 10, 1)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linspace(0, 10, 11)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arrays filled with logarithmic sequences" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1. , 3.16227766, 10. , 31.6227766 , 100. ])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.logspace(0, 2, 5) # 5 data points between 10**0=1 to 10**2=100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mesh-grid arrays" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "x = np.array([-1, 0, 1])" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "y = np.array([-2, 0, 2])" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "X, Y = np.meshgrid(x, y)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1, 0, 1],\n", " [-1, 0, 1],\n", " [-1, 0, 1]])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-2, -2, -2],\n", " [ 0, 0, 0],\n", " [ 2, 2, 2]])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "Z = (X + Y) ** 2" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[9, 4, 1],\n", " [1, 0, 1],\n", " [1, 4, 9]])" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating uninitialized arrays" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3.])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.empty(3, dtype=np.float)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating arrays with properties of other arrays" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "def f(x):\n", " y = np.ones_like(x)\n", " # compute with x and y\n", " return y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating matrix arrays" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., 0., 0.],\n", " [ 0., 1., 0., 0.],\n", " [ 0., 0., 1., 0.],\n", " [ 0., 0., 0., 1.]])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.identity(4)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 1., 0.],\n", " [ 0., 0., 1.],\n", " [ 0., 0., 0.]])" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(3, k=1)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0.],\n", " [ 1., 0., 0.],\n", " [ 0., 1., 0.]])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(3, k=-1)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 0, 0, 0],\n", " [ 0, 5, 0, 0],\n", " [ 0, 0, 10, 0],\n", " [ 0, 0, 0, 15]])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.diag(np.arange(0, 20, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Index and slicing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### One-dimensional arrays" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "a = np.arange(0, 11)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0] # the first element" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-1] # the last element" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[4] # the fifth element, at index 4" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1:-1]" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 3, 5, 7, 9])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1:-1:2]" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:5]" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 6, 7, 8, 9, 10])" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-5:]" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 8, 6, 4, 2, 0])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[::-2]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multidimensional arrays" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [], "source": [ "f = lambda m, n: n + 10 * m" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "A = np.fromfunction(f, (6, 6), dtype=int)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 5],\n", " [10, 11, 12, 13, 14, 15],\n", " [20, 21, 22, 23, 24, 25],\n", " [30, 31, 32, 33, 34, 35],\n", " [40, 41, 42, 43, 44, 45],\n", " [50, 51, 52, 53, 54, 55]])" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 11, 21, 31, 41, 51])" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:, 1] # the second column" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 11, 12, 13, 14, 15])" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1, :] # the second row" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2],\n", " [10, 11, 12],\n", " [20, 21, 22]])" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:3, :3] # upper half diagonal block matrix" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[30, 31, 32],\n", " [40, 41, 42],\n", " [50, 51, 52]])" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[3:, :3] # lower left off-diagonal block matrix" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4],\n", " [20, 22, 24],\n", " [40, 42, 44]])" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[::2, ::2] # every second element starting from 0, 0" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 14],\n", " [31, 34],\n", " [51, 54]])" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1::2, 1::3] # every second element starting from 1, 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Views" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "B = A[1:5, 1:5]" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 12, 13, 14],\n", " [21, 22, 23, 24],\n", " [31, 32, 33, 34],\n", " [41, 42, 43, 44]])" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "B[:, :] = 0" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 5],\n", " [10, 0, 0, 0, 0, 15],\n", " [20, 0, 0, 0, 0, 25],\n", " [30, 0, 0, 0, 0, 35],\n", " [40, 0, 0, 0, 0, 45],\n", " [50, 51, 52, 53, 54, 55]])" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "C = B[1:3, 1:3].copy()" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 0],\n", " [0, 0]])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [], "source": [ "C[:, :] = 1 # this does not affect B since C is a copy of the view B[1:3, 1:3]" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [1, 1]])" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 0, 0],\n", " [0, 0, 0, 0],\n", " [0, 0, 0, 0],\n", " [0, 0, 0, 0]])" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fancy indexing and Boolean-valued indexing" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "A = np.linspace(0, 1, 11)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.2, 0.4])" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[np.array([0, 2, 4])]" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.2, 0.4])" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[[0, 2, 4]]" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, False, False, True, True, True,\n", " True, True], dtype=bool)" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A > 0.5 " ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.6, 0.7, 0.8, 0.9, 1. ])" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[A > 0.5]" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "A = np.arange(10)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "indices = [2, 4, 6]" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "B = A[indices]" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "B[0] = -1 # this does not affect A" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "A[indices] = -1" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, -1, 3, -1, 5, -1, 7, 8, 9])" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [], "source": [ "A = np.arange(10)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "B = A[A > 5]" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [], "source": [ "B[0] = -1 # this does not affect A" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [], "source": [ "A[A > 5] = -1" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, -1, -1, -1, -1])" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reshaping and resizing" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "data = np.array([[1, 2], [3, 4]])" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3, 4]])" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.reshape(data, (1, 4))" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.reshape(4)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [], "source": [ "data = np.array([[1, 2], [3, 4]])" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.flatten()" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4,)" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.flatten().shape" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [], "source": [ "data = np.arange(0, 5)" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [], "source": [ "column = data[:, np.newaxis]" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0],\n", " [1],\n", " [2],\n", " [3],\n", " [4]])" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "column" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "row = data[np.newaxis, :]" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3, 4]])" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "row" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [], "source": [ "data = np.arange(5)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4])" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4]])" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.vstack((data, data, data))" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [], "source": [ "data = np.arange(5)" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4])" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4])" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hstack((data, data, data))" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [], "source": [ "data = data[:, np.newaxis]" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 0],\n", " [1, 1, 1],\n", " [2, 2, 2],\n", " [3, 3, 3],\n", " [4, 4, 4]])" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hstack((data, data, data))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vectorized expressions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arithmetic operations" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [], "source": [ "x = np.array([[1, 2], [3, 4]]) " ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [], "source": [ "y = np.array([[5, 6], [7, 8]])" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 6, 8],\n", " [10, 12]])" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + y" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 4],\n", " [4, 4]])" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y - x" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 12],\n", " [21, 32]])" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x * y" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 5. , 3. ],\n", " [ 2.33333333, 2. ]])" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y / x" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 4],\n", " [6, 8]])" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x * 2" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2, 4],\n", " [ 8, 16]])" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** x" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2.5, 3. ],\n", " [ 3.5, 4. ]])" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y / 2" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(y / 2).dtype" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [], "source": [ "x = np.array([1, 2, 3, 4]).reshape(2,2)" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "z = np.array([1, 2, 3, 4])" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "operands could not be broadcast together with shapes (2,2) (4,) ", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,2) (4,) " ] } ], "source": [ "x / z" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [], "source": [ "z = np.array([[2, 4]])" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 4]])" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.shape" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.5, 0.5],\n", " [ 1.5, 1. ]])" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x / z" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [], "source": [ "zz = np.concatenate([z, z], axis=0)" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 4],\n", " [2, 4]])" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zz" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.5, 0.5],\n", " [ 1.5, 1. ]])" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x / zz" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [], "source": [ "z = np.array([[2], [4]])" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.shape" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.5 , 1. ],\n", " [ 0.75, 1. ]])" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x / z" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [], "source": [ "zz = np.concatenate([z, z], axis=1)" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 2],\n", " [4, 4]])" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zz" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.5 , 1. ],\n", " [ 0.75, 1. ]])" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x / zz" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 6, 9],\n", " [ 9, 12]])" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 3], [2, 4]])\n", "x = x + y\n", "x" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 6, 9],\n", " [ 9, 12]])" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1, 3], [2, 4]])\n", "x += y\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Elementwise functions" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [], "source": [ "x = np.linspace(-1, 1, 11)" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1. , -0.8, -0.6, -0.4, -0.2, 0. , 0.2, 0.4, 0.6, 0.8, 1. ])" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [], "source": [ "y = np.sin(np.pi * x)" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0. , -0.5878, -0.9511, -0.9511, -0.5878, 0. , 0.5878,\n", " 0.9511, 0.9511, 0.5878, 0. ])" ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.round(y, decimals=4)" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.add(np.sin(x) ** 2, np.cos(x) ** 2)" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sin(x) ** 2 + np.cos(x) ** 2" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "def heaviside(x):\n", " return 1 if x > 0 else 0" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heaviside(-1)" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heaviside(1.5)" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [], "source": [ "x = np.linspace(-5, 5, 11)" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mheaviside\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mheaviside\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mheaviside\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" ] } ], "source": [ "heaviside(x)" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [], "source": [ "heaviside = np.vectorize(heaviside)" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heaviside(x)" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [], "source": [ "def heaviside(x):\n", " return 1.0 * (x > 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Aggregate functions" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [], "source": [ "data = np.random.normal(size=(15,15))" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.090472669644641304" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(data)" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.090472669644641304" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.mean()" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [], "source": [ "data = np.random.normal(size=(5, 10, 15))" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10, 15)" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sum(axis=0).shape" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10,)" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sum(axis=(0, 2)).shape" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25.869033203350682" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sum()" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [], "source": [ "data = np.arange(1,10).reshape(3,3)" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "45" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sum()" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([12, 15, 18])" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sum(axis=0)" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 6, 15, 24])" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sum(axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean arrays and conditional expressions" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [], "source": [ "a = np.array([1, 2, 3, 4])" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [], "source": [ "b = np.array([4, 3, 2, 1])" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ True, True, False, False], dtype=bool)" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a < b" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.all(a < b)" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.any(a < b)" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Some elements in a are smaller than their corresponding elemment in b\n" ] } ], "source": [ "if np.all(a < b):\n", " print(\"All elements in a are smaller than their corresponding element in b\")\n", "elif np.any(a < b):\n", " print(\"Some elements in a are smaller than their corresponding elemment in b\")\n", "else:\n", " print(\"All elements in b are smaller than their corresponding element in a\")" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [], "source": [ "x = np.array([-2, -1, 0, 1, 2])" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, True, True], dtype=bool)" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x > 0" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 1, 1])" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 * (x > 0)" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 1, 2])" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x * (x > 0)" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [], "source": [ "def pulse(x, position, height, width):\n", " return height * (x >= position) * (x <= (position + width))" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [], "source": [ "x = np.linspace(-5, 5, 11)" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0])" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pulse(x, position=-2, height=1, width=5)" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pulse(x, position=1, height=1, width=5)" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [], "source": [ "def pulse(x, position, height, width):\n", " return height * np.logical_and(x >= position, x <= (position + width))" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [], "source": [ "x = np.linspace(-4, 4, 9)" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 16., 9., 4., 1., 0., 1., 8., 27., 64.])" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.where(x < 0, x**2, x**3)" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 16., 9., 4., -1., 0., 1., 16., 81., 256.])" ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.select([x < -1, x < 2, x >= 2],\n", " [x**2 , x**3 , x**4])" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 16., 9., 4., -1., 0., 1., 16., 81., 256.])" ] }, "execution_count": 206, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.choose([0, 0, 0, 1, 1, 1, 2, 2, 2], \n", " [x**2, x**3, x**4])" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-4., -3., 3., 4.])" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[abs(x) > 2]" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([0, 1, 7, 8]),)" ] }, "execution_count": 208, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.nonzero(abs(x) > 2)" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-4., -3., 3., 4.])" ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[np.nonzero(abs(x) > 2)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Set operations" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [], "source": [ "a = np.unique([1,2,3,3])" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [], "source": [ "b = np.unique([2,3,4,4,5,6,5])" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, True, True], dtype=bool)" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.in1d(a, b)" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in a" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in b" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.all(np.in1d(a, b))" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 216, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.union1d(a, b)" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 3])" ] }, "execution_count": 217, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.intersect1d(a, b)" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1])" ] }, "execution_count": 218, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.setdiff1d(a, b)" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4, 5, 6])" ] }, "execution_count": 219, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.setdiff1d(b, a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operations on arrays" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [], "source": [ "data = np.arange(9).reshape(3, 3)" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2],\n", " [3, 4, 5],\n", " [6, 7, 8]])" ] }, "execution_count": 221, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 3, 6],\n", " [1, 4, 7],\n", " [2, 5, 8]])" ] }, "execution_count": 222, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.transpose(data)" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [], "source": [ "data = np.random.randn(1, 2, 3, 4, 5)" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 4, 5)" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5, 4, 3, 2, 1)" ] }, "execution_count": 225, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.T.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix and vector operations" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [], "source": [ "A = np.arange(1, 7).reshape(2, 3)" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 227, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [], "source": [ "B = np.arange(1, 7).reshape(3, 2)" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4],\n", " [5, 6]])" ] }, "execution_count": 229, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[22, 28],\n", " [49, 64]])" ] }, "execution_count": 230, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(A, B)" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 9, 12, 15],\n", " [19, 26, 33],\n", " [29, 40, 51]])" ] }, "execution_count": 231, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(B, A)" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [], "source": [ "A = np.arange(9).reshape(3, 3)" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2],\n", " [3, 4, 5],\n", " [6, 7, 8]])" ] }, "execution_count": 233, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [], "source": [ "x = np.arange(3)" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2])" ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 5, 14, 23])" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(A, x)" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 5, 14, 23])" ] }, "execution_count": 237, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.dot(x)" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [], "source": [ "A = np.random.rand(3,3)\n", "B = np.random.rand(3,3)" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.15628289, 1.01950542, 0.54368897],\n", " [ 0.1421288 , 0.60925824, 0.90360847],\n", " [ 0.03321325, 0.63335306, 0.47040536]])" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Ap = B @ A @ np.linalg.inv(B)\n", "Ap" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.15628289, 1.01950542, 0.54368897],\n", " [ 0.1421288 , 0.60925824, 0.90360847],\n", " [ 0.03321325, 0.63335306, 0.47040536]])" ] }, "execution_count": 240, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Ap = np.dot(B, np.dot(A, np.linalg.inv(B)))\n", "Ap" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.15628289, 1.01950542, 0.54368897],\n", " [ 0.1421288 , 0.60925824, 0.90360847],\n", " [ 0.03321325, 0.63335306, 0.47040536]])" ] }, "execution_count": 241, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Ap = B.dot(A.dot(np.linalg.inv(B)))\n", "Ap" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [], "source": [ "A = np.matrix(A)" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [], "source": [ "B = np.matrix(B)" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [], "source": [ "Ap = B * A * B.I" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [], "source": [ "A = np.asmatrix(A)" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [], "source": [ "B = np.asmatrix(B)" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [], "source": [ "Ap = B * A * B.I" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [], "source": [ "Ap = np.asarray(Ap)" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.15628289, 1.01950542, 0.54368897],\n", " [ 0.1421288 , 0.60925824, 0.90360847],\n", " [ 0.03321325, 0.63335306, 0.47040536]])" ] }, "execution_count": 249, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Ap" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 250, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.inner(x, x)" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 251, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(x, x)" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [], "source": [ "y = x[:, np.newaxis]" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0],\n", " [1],\n", " [2]])" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[5]])" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(y.T, y)" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [], "source": [ "x = np.array([1, 2, 3])" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [2, 4, 6],\n", " [3, 6, 9]])" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.outer(x, x) " ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 2, 4, 6, 3, 6, 9])" ] }, "execution_count": 257, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.kron(x, x) " ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [2, 4, 6],\n", " [3, 6, 9]])" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.kron(x[:, np.newaxis], x[np.newaxis, :])" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., 1., 0.],\n", " [ 0., 1., 0., 1.],\n", " [ 1., 0., 1., 0.],\n", " [ 0., 1., 0., 1.]])" ] }, "execution_count": 259, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.kron(np.ones((2,2)), np.identity(2))" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 0., 0.],\n", " [ 1., 1., 0., 0.],\n", " [ 0., 0., 1., 1.],\n", " [ 0., 0., 1., 1.]])" ] }, "execution_count": 260, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.kron(np.identity(2), np.ones((2,2)))" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [], "source": [ "x = np.array([1, 2, 3, 4])" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [], "source": [ "y = np.array([5, 6, 7, 8])" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "70" ] }, "execution_count": 263, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.einsum(\"n,n\", x, y)" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "70" ] }, "execution_count": 264, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.inner(x, y)" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [], "source": [ "A = np.arange(9).reshape(3, 3)" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [], "source": [ "B = A.T" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 14, 23],\n", " [ 14, 50, 86],\n", " [ 23, 86, 149]])" ] }, "execution_count": 267, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.einsum(\"mk,kn\", A, B)" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 268, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.alltrue(np.einsum(\"mk,kn\", A, B) == np.dot(A, B))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Versions" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
SoftwareVersion
Python3.6.6 64bit [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
IPython6.4.0
OSDarwin 17.3.0 x86_64 i386 64bit
numpy1.13.3
Thu Sep 27 22:38:24 2018 JST
" ], "text/latex": [ "\\begin{tabular}{|l|l|}\\hline\n", "{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n", "Python & 3.6.6 64bit [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE\\_401/final)] \\\\ \\hline\n", "IPython & 6.4.0 \\\\ \\hline\n", "OS & Darwin 17.3.0 x86\\_64 i386 64bit \\\\ \\hline\n", "numpy & 1.13.3 \\\\ \\hline\n", "\\hline \\multicolumn{2}{|l|}{Thu Sep 27 22:38:24 2018 JST} \\\\ \\hline\n", "\\end{tabular}\n" ], "text/plain": [ "Software versions\n", "Python 3.6.6 64bit [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]\n", "IPython 6.4.0\n", "OS Darwin 17.3.0 x86_64 i386 64bit\n", "numpy 1.13.3\n", "Thu Sep 27 22:38:24 2018 JST" ] }, "execution_count": 269, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%reload_ext version_information\n", "%version_information numpy" ] } ], "metadata": { "kernelspec": { "display_name": "py3.6", "language": "python", "name": "py3.6" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }