{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Linear algebra" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For matrix and vector usage in python, we will import the `numpy` package." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Basic matrix operators\n", "import numpy as np\n", "\n", "# Import the pseudo inverse function\n", "from numpy.linalg import pinv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix\n", "\n", "Denoting the matrix in python:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3],\n", " [ 4, 5, 6],\n", " [ 7, 8, 9],\n", " [10, 11, 12]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.array([\n", " [1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9],\n", " [10, 11, 12],\n", "])\n", "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Items in python are the same, except for 0-indexed" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[3, 1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vector\n", "\n", "Denoting the vector in python:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [2],\n", " [3]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = np.array([\n", " [1],\n", " [2],\n", " [3],\n", "])\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Items in python (again, same but 0-indexed):" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y[1, 0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix-vector multiplication" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[486. ],\n", " [314. ],\n", " [343.5],\n", " [173. ]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_matrix = np.array([\n", " [1, 2104],\n", " [1, 1416],\n", " [1, 1534],\n", " [1, 852],\n", "])\n", "\n", "parameters = np.array([\n", " [-40],\n", " [0.25],\n", "])\n", "\n", "data_matrix @ parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix-matrix multiplication" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[486. , 410.4, 691.6],\n", " [314. , 341.6, 416.4],\n", " [343.5, 353.4, 463.6],\n", " [173. , 285.2, 190.8]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_matrix = np.array([\n", " [1, 2104],\n", " [1, 1416],\n", " [1, 1534],\n", " [1, 852],\n", "])\n", "\n", "parameters = np.array([\n", " [-40, 200, -150],\n", " [0.25, 0.1, 0.4],\n", "])\n", "\n", "data_matrix @ parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Identity matrix" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "A = np.array([\n", " [1, 2],\n", " [4, 5]\n", "])\n", "\n", "I = np.identity(2)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 2.],\n", " [4., 5.]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "I @ A" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 2.],\n", " [4., 5.]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A @ I" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix inverse" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 3, 4],\n", " [ 2, 16]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.array([\n", " [3, 4],\n", " [2, 16],\n", "])\n", "A" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.4 , -0.1 ],\n", " [-0.05 , 0.075]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "I = pinv(A)\n", "I" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1.00000000e+00, 1.11022302e-16],\n", " [-3.33066907e-16, 1.00000000e+00]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A @ I" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix transpose" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 0],\n", " [3, 5, 9]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.array([\n", " [1, 2, 0],\n", " [3, 5, 9],\n", "])\n", "A" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 3],\n", " [2, 5],\n", " [0, 9]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.transpose(A)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.8.3" }, "toc-autonumbering": false, "toc-showcode": false, "toc-showmarkdowntxt": false }, "nbformat": 4, "nbformat_minor": 4 }