LUNAR
🌙
Dashboard
Browse
python-cheatsheet
ipynb
See Source
{ "cells": [ { "cell_type": "markdown", "id": "ff303d24", "metadata": {}, "source": [ "# Import Numpy" ] }, { "cell_type": "code", "execution_count": 1, "id": "8d685b96", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "id": "8e51a3fa", "metadata": {}, "source": [ "# Writing matrices" ] }, { "cell_type": "code", "execution_count": 2, "id": "a81aafee", "metadata": {}, "outputs": [], "source": [ "A = np.array([[1,2],[3,4]])\n", "B = np.array([[1,2],[3,4],[5,6]])\n", "C = np.array([[1,2,3],[4,5,6]])" ] }, { "cell_type": "markdown", "id": "625c0744", "metadata": {}, "source": [ "## For matrix shape and size" ] }, { "cell_type": "code", "execution_count": 3, "id": "56e34f79", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3, 2) 6\n" ] } ], "source": [ "print(B.shape, B.size)" ] }, { "cell_type": "markdown", "id": "0f8146f1", "metadata": {}, "source": [ "## To stack matrices" ] }, { "cell_type": "code", "execution_count": 4, "id": "fb45405d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 1 2 3]\n", " [3 4 4 5 6]]\n" ] } ], "source": [ "D = np.hstack([A,C])\n", "print(D)" ] }, { "cell_type": "markdown", "id": "fc01090d", "metadata": {}, "source": [ "## Powers and basic operations" ] }, { "cell_type": "code", "execution_count": 5, "id": "a0f12d73", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.25, 0.5 ],\n", " [0.75, 1. ]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Element-wise squaring\n", "A**2\n", "np.linalg.matrix_power(A,2)\n", "\n", "# Matrix square\n", "A@A\n", "\n", "# Moltiplications\n", "A@C\n", "\n", "# Division\n", "A/4" ] }, { "cell_type": "markdown", "id": "90e1b32e", "metadata": {}, "source": [ "# Inverse and Determinant" ] }, { "cell_type": "markdown", "id": "ef75870d", "metadata": {}, "source": [ "### Invertible matrix" ] }, { "cell_type": "code", "execution_count": 6, "id": "1721348c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Inverse:\n", " [[-0. -1. -1.]\n", " [ 2. -1. -2.]\n", " [-1. 1. 2.]]\n", "\n", "Proof that it is invertible using M_inv@M:\n", " [[1. 0. 0.]\n", " [0. 1. 0.]\n", " [0. 0. 1.]]\n", "\n", "Proof that it is invertible using the det:\n", " 1.0\n" ] } ], "source": [ "# Invertible matrix\n", "M = np.array([[0,1,1],[-2,-1,-2],[1,1,2]])\n", "M_inv = np.linalg.inv(M)\n", "Det_M = np.linalg.det(M)\n", "print(\"Inverse:\\n\", M_inv)\n", "print(\"\\nProof that it is invertible using M_inv@M:\\n\", M_inv@M)\n", "print(\"\\nProof that it is invertible using the det:\\n\", Det_M)" ] }, { "cell_type": "markdown", "id": "c0332cef", "metadata": {}, "source": [ "### Not invertible matrix" ] }, { "cell_type": "code", "execution_count": 7, "id": "5a0a1913", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Not invertible:\n", " [[ 3.15251974e+15 -6.30503948e+15 3.15251974e+15]\n", " [-6.30503948e+15 1.26100790e+16 -6.30503948e+15]\n", " [ 3.15251974e+15 -6.30503948e+15 3.15251974e+15]]\n", "\n", "Proof that it is not invertible using H_inv@H:\n", " [[ 12. 16. 12. ]\n", " [-10. -16. -6. ]\n", " [ 1.5 4. 2.5]]\n", "\n", "Proof that it is not invertible using the det:\n", " -9.51619735392994e-16\n" ] } ], "source": [ "H = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", "H_inv = np.linalg.inv(H)\n", "Det_H = np.linalg.det(H)\n", "print(\"Not invertible:\\n\", H_inv)\n", "print(\"\\nProof that it is not invertible using H_inv@H:\\n\", H_inv@H)\n", "print(\"\\nProof that it is not invertible using the det:\\n\", Det_H)" ] }, { "cell_type": "markdown", "id": "1d091cb9", "metadata": {}, "source": [ "## Identity matrix and diagonals" ] }, { "cell_type": "markdown", "id": "05b73e6e", "metadata": {}, "source": [ "### Identity with multiple dimensions" ] }, { "cell_type": "code", "execution_count": 8, "id": "6910ab34", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 0. 0. 0. 0.]\n", " [0. 1. 0. 0. 0.]\n", " [0. 0. 1. 0. 0.]\n", " [0. 0. 0. 1. 0.]\n", " [0. 0. 0. 0. 1.]]\n" ] } ], "source": [ "I = np.eye(5)\n", "print(I)" ] }, { "cell_type": "markdown", "id": "52abe4a6", "metadata": {}, "source": [ "### Diagonal" ] }, { "cell_type": "code", "execution_count": 9, "id": "9941235b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[5 0 0 0]\n", " [0 5 0 0]\n", " [0 0 3 0]\n", " [0 0 0 4]]\n" ] } ], "source": [ "E = np.diag([5,5,3,4])\n", "print(E)" ] }, { "cell_type": "markdown", "id": "abba9c07", "metadata": {}, "source": [ "### Zero matrix" ] }, { "cell_type": "code", "execution_count": 10, "id": "d55ab4e4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0. 0.]\n", " [0. 0. 0. 0.]\n", " [0. 0. 0. 0.]]\n" ] } ], "source": [ "Zero = np.zeros([3,4])\n", "print(Zero)" ] }, { "cell_type": "markdown", "id": "3d41274b", "metadata": {}, "source": [ "### Block command" ] }, { "cell_type": "code", "execution_count": 11, "id": "db89710c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 2. 1. 2. 3.]\n", " [3. 4. 4. 5. 6.]\n", " [1. 0. 0. 0. 0.]\n", " [0. 1. 0. 0. 0.]]\n" ] } ], "source": [ "F = np.block([[A,C], [np.eye(2), np.zeros([2,3])]])\n", "print(F)" ] }, { "cell_type": "markdown", "id": "fedfdb5d", "metadata": {}, "source": [ "# Eigenvalues and Eigenvectors" ] }, { "cell_type": "code", "execution_count": 12, "id": "c503c4a2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Eigenvalues:\n", " [2. 3.]\n", "\n", "Invertible Matrix S 'inv(S) @ A @ S = D':\n", " [[-0.89442719 -0.70710678]\n", " [-0.4472136 -0.70710678]]\n", "\n", "!! Values of S are the Eigenbasis of D !!\n" ] } ], "source": [ "A = np.array([[1,2],[-1,4]])\n", "ev, S = np.linalg.eig(A)\n", "print(\"Eigenvalues:\\n\",ev)\n", "print(\"\\nInvertible Matrix S 'inv(S) @ A @ S = D':\\n\",S)\n", "print(\"\\n!! Values of S are the Eigenbasis of D !!\")" ] }, { "cell_type": "markdown", "id": "c59fa114", "metadata": {}, "source": [ "### ev, S" ] }, { "cell_type": "code", "execution_count": 13, "id": "6c993480", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "EigResult(eigenvalues=array([2., 3.]), eigenvectors=array([[-0.89442719, -0.70710678],\n", " [-0.4472136 , -0.70710678]]))\n", "\n", "Eigenvalues:\n", " [2. 3.]\n", "\n", "Eigenvectors:\n", " [[-0.89442719 -0.70710678]\n", " [-0.4472136 -0.70710678]]\n" ] } ], "source": [ "SINGLE = np.linalg.eig(A)\n", "print(SINGLE)\n", "\n", "print(\"\\nEigenvalues:\\n\",SINGLE[0])\n", "print(\"\\nEigenvectors:\\n\",SINGLE[1])" ] }, { "cell_type": "markdown", "id": "f4adcd63", "metadata": {}, "source": [ "### Proof of eig(S)" ] }, { "cell_type": "code", "execution_count": 14, "id": "ee7d4e46", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2. 0.]\n", " [0. 3.]]\n" ] } ], "source": [ "M_S = np.linalg.inv(S)@A@S\n", "print(M_S)" ] }, { "cell_type": "code", "execution_count": 15, "id": "6d13eda1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2. 0.]\n", " [0. 3.]]\n" ] } ], "source": [ "Z = np.array([[2,1],[1,1]])\n", "Z_S = np.linalg.inv(Z)@A@Z\n", "print(Z_S)" ] }, { "cell_type": "markdown", "id": "832a2b66", "metadata": {}, "source": [ "# Norm" ] }, { "cell_type": "code", "execution_count": 16, "id": "b2bc6abb", "metadata": {}, "outputs": [], "source": [ "a,b = np.array([[1],[-5],[6]]), np.array([[0],[-2],[7]])" ] }, { "cell_type": "markdown", "id": "49e9366d", "metadata": {}, "source": [ "### Manhattan norm $||a_1||$" ] }, { "cell_type": "code", "execution_count": 17, "id": "52918d5b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12.0\n" ] } ], "source": [ "print(np.linalg.norm(a,1))" ] }, { "cell_type": "markdown", "id": "cb99b92a", "metadata": {}, "source": [ "### Euclidean norm $||a_2||$" ] }, { "cell_type": "code", "execution_count": 18, "id": "1460797d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.874007874011811\n", "7.874007874011811\n" ] } ], "source": [ "print(np.linalg.norm(a))\n", "print(np.linalg.norm(a,2))" ] }, { "cell_type": "markdown", "id": "b4db20bc", "metadata": {}, "source": [ "### Maximum norm $||a_\\text{max}||$" ] }, { "cell_type": "code", "execution_count": 19, "id": "363f654e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6.0\n" ] } ], "source": [ "print(np.linalg.norm(a,np.inf))" ] }, { "cell_type": "markdown", "id": "4f874cf5", "metadata": {}, "source": [ "# Scalar product" ] }, { "cell_type": "code", "execution_count": 20, "id": "4bb4e009", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[52]\n", "[[52]]\n" ] } ], "source": [ "print(sum(a*b))\n", "print(a.T@b)" ] }, { "cell_type": "markdown", "id": "83a96ef2", "metadata": {}, "source": [ "### Pivot positions" ] }, { "cell_type": "code", "execution_count": 21, "id": "a85a7c2d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "52\n", "1\n" ] } ], "source": [ "s = (a.T@b)[0,0]\n", "print(s)\n", "\n", "print(a[0,0])" ] }, { "cell_type": "markdown", "id": "869e2ebf", "metadata": {}, "source": [ "# Gram - Schmidt" ] }, { "cell_type": "code", "execution_count": 22, "id": "f3c44b0d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 8 0]\n", " [ 2 1 0]\n", " [ 0 -6 1]]\n" ] } ], "source": [ "u = np.array([[1],[2],[0]])\n", "v = np.array([[8],[1],[-6]])\n", "w = np.array([[0],[0],[1]])\n", "\n", "A = np.hstack([u,v,w])\n", "print(A)" ] }, { "cell_type": "markdown", "id": "2eabdd7e", "metadata": {}, "source": [ "## Orthonormal (q) and upper-triangular (r)\n" ] }, { "cell_type": "code", "execution_count": 23, "id": "1d79b905", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.4472136 0.66666667 0.59628479]\n", " [-0.89442719 -0.33333333 -0.2981424 ]\n", " [-0. -0.66666667 0.74535599]] \n", "\n", " [[-2.23606798 -4.47213595 0. ]\n", " [ 0. 9. -0.66666667]\n", " [ 0. 0. 0.74535599]]\n" ] } ], "source": [ "Q,R = np.linalg.qr(A)\n", "print(Q,\"\\n\\n\",R)" ] }, { "cell_type": "markdown", "id": "f8300f64", "metadata": {}, "source": [ "### Proof of the Transformation matrix (r)" ] }, { "cell_type": "code", "execution_count": 24, "id": "e5fd6179", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1.00000000e+00 8.00000000e+00 -7.07465566e-17]\n", " [ 2.00000000e+00 1.00000000e+00 8.13329108e-18]\n", " [ 0.00000000e+00 -6.00000000e+00 1.00000000e+00]]\n" ] } ], "source": [ "# Q@R gives the original matrix A\n", "print(Q@R)" ] }, { "cell_type": "markdown", "id": "47cceae4", "metadata": {}, "source": [ "### Norm of Q" ] }, { "cell_type": "code", "execution_count": 25, "id": "600bcc95", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9999999999999997\n" ] } ], "source": [ "P = Q[0,0]**2 + Q[1,0]**2 + Q[2,0]**2\n", "print(P)" ] }, { "cell_type": "markdown", "id": "3dfd171e", "metadata": {}, "source": [ "# Linear systems" ] }, { "cell_type": "code", "execution_count": 26, "id": "93bc6ebe", "metadata": {}, "outputs": [], "source": [ "from scipy import linalg" ] }, { "cell_type": "markdown", "id": "36ec8f34", "metadata": {}, "source": [ "## Linear system with its inverse\n", "\n", "$$\\begin{align*}\n", "A \\boldsymbol{\\cdot} \\mathbf{x} &= \\mathbf{b}\\\\\n", "A^{-1} \\boldsymbol{\\cdot} A \\boldsymbol{\\cdot} \\mathbf{x} &= A^{-1} \\boldsymbol{\\cdot} \\mathbf{b}\\\\\n", "\\mathbf{x} &= A^{-1} \\boldsymbol{\\cdot} \\mathbf{b}\n", "\\end{align*}$$" ] }, { "cell_type": "code", "execution_count": 27, "id": "54763593", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.9999999999999996\n" ] } ], "source": [ "A = np.array([[2,-3],[3,-5]])\n", "d = np.linalg.det(A)\n", "print(d)" ] }, { "cell_type": "code", "execution_count": 28, "id": "7fcac47f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[7.]\n", " [3.]]\n" ] } ], "source": [ "b = np.array([[5],[6]])\n", "x = np.linalg.inv(A)@b\n", "print(x)" ] }, { "cell_type": "markdown", "id": "e61ad1da", "metadata": {}, "source": [ "### Proof of invertible matrix x" ] }, { "cell_type": "code", "execution_count": 29, "id": "1a57ef51", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[5.]\n", " [6.]]\n" ] } ], "source": [ "print(A@x)" ] }, { "cell_type": "markdown", "id": "4adc3061", "metadata": {}, "source": [ "## Using scipy" ] }, { "cell_type": "code", "execution_count": 30, "id": "dc9c3924", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-13.0\n" ] } ], "source": [ "B = np.array([[-2,3,1],[3,-2,0],[1,0,3]])\n", "d = linalg.det(B)\n", "c = np.array([[7],[-3],[2]])\n", "print(d)" ] }, { "cell_type": "code", "execution_count": 31, "id": "e7e346f0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.84615385]\n", " [2.76923077]\n", " [0.38461538]]\n", "\n", " [[8.8817842e-16]\n", " [8.8817842e-16]\n", " [8.8817842e-16]]\n" ] } ], "source": [ "xs = linalg.inv(B)@c\n", "print(xs)\n", "\n", "print(\"\\n\",c-B@xs)" ] }, { "cell_type": "code", "execution_count": 32, "id": "82376e33", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.]\n", " [0.]\n", " [0.]]\n" ] } ], "source": [ "xsol = linalg.solve(B,c)\n", "print(c-B@xsol)" ] }, { "cell_type": "markdown", "id": "a8eb44fb", "metadata": {}, "source": [ "# Square system with singular coefficient matrix\n", "\n", "Singular = not invertible matrix, ie det = 0; 0 solutions; $\\infty$ solutions " ] }, { "cell_type": "code", "execution_count": 33, "id": "c4ed22bf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0\n" ] } ], "source": [ "A = np.array([[2,-6],[-1,3]])\n", "d = np.linalg.det(A)\n", "b = np.array([[-2],[1]])\n", "print(d)" ] }, { "cell_type": "markdown", "id": "dd422b24", "metadata": {}, "source": [ "## Rank of a matrix\n", "\n", "If $A =$ square and $\\det A = 0$,\n", "\n", "rk $A \\neq$ rk $A\\big|b \\to 0$ solutions\n", "rk $A =$ rk $A\\big|b = n \\to$ unique solution\n", "$\\small \\text{where } n \\text{ is the number of unknowns}$\n", "rk $A =$ rk $A\\big|b \\to \\infty-$ many solutions \n" ] }, { "cell_type": "code", "execution_count": 34, "id": "d630e935", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "1\n" ] } ], "source": [ "rkA = np.linalg.matrix_rank(A)\n", "print(rkA)\n", "\n", "rkAb = np.linalg.matrix_rank(np.hstack([A,b]))\n", "print(rkAb)" ] }, { "cell_type": "code", "execution_count": 35, "id": "20db97e5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.1]\n", " [ 0.3]]\n", "\n", " [[-2.]\n", " [ 1.]]\n" ] } ], "source": [ "x_a = linalg.lstsq(A,b)[0]\n", "print(x_a)\n", "\n", "xNew = linalg.lstsq(A,b)[0]\n", "print(\"\\n\",A@xNew)" ] }, { "cell_type": "markdown", "id": "d234d267", "metadata": {}, "source": [ "## Rank check\n", "### To find $\\infty$-many solutions:\n", "$$\\text{Sol} = x = x_p + \\lambda_1 n_1 + \\dots + \\lambda_k n_k$$\n", "$$\\ker A = \\operatorname{span}\\{n_1,\\dots,n_k\\}$$\n", "$$A\\boldsymbol{\\cdot} n_1 = 0 \\Longrightarrow A\\left(x_p+n_1\\right) = Ax_p + An_1 = \\boxed{Ax_p = \\mathbf{b}}$$" ] }, { "cell_type": "code", "execution_count": 36, "id": "250fbcf7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kernel:\n", " [[-0.9486833 ]\n", " [-0.31622777]]\n", "\n", "Kernel check:\n", " [[0.]\n", " [0.]]\n", "\n", "Vector b using 7*n:\n", " [[-2.]\n", " [ 1.]]\n" ] } ], "source": [ "n = linalg.null_space(A)\n", "print(\"Kernel:\\n\",n)\n", "\n", "# Proof\n", "print(\"\\nKernel check:\\n\",A@n)\n", "\n", "# Using 7 (that ofc belongs to the kernel)\n", "print(\"\\nVector b using 7*n:\\n\", A@(xNew+7*n))" ] }, { "cell_type": "markdown", "id": "dd5c868c", "metadata": {}, "source": [ "### When we have 0 solutions:" ] }, { "cell_type": "markdown", "id": "33f88777", "metadata": {}, "source": [ "$$\\mathrm{rk}\\ (A) \\neq \\mathrm{rk}\\ (A\\big|b)$$" ] }, { "cell_type": "code", "execution_count": 37, "id": "c2f2dcf9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n" ] } ], "source": [ "b = np.array([[1],[1]])\n", "\n", "print(rkA)\n", "rkAb = np.linalg.matrix_rank(np.hstack([A,b]))\n", "print(rkAb)" ] }, { "cell_type": "markdown", "id": "fedb889e", "metadata": {}, "source": [ "Proof that b $\\neq$ A@x_b" ] }, { "cell_type": "code", "execution_count": 38, "id": "fd6af162", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0.02]\n", " [-0.06]]\n", "\n", " [[ 0.4]\n", " [-0.2]]\n" ] } ], "source": [ "x_b = linalg.lstsq(A,b)[0]\n", "print(x_b)\n", "\n", "print(\"\\n\",A@x_b)" ] }, { "cell_type": "markdown", "id": "b0106f5e", "metadata": {}, "source": [ "## Non-square matrix" ] }, { "cell_type": "code", "execution_count": 39, "id": "040f13bd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "\n", " [[0.16666667]\n", " [0.16666667]\n", " [0.16666667]] \n", "\n", " [[0.5]\n", " [0.5]]\n", "\n", " [[-8.16496581e-01 -6.99362418e-17]\n", " [ 4.08248290e-01 -7.07106781e-01]\n", " [ 4.08248290e-01 7.07106781e-01]]\n" ] } ], "source": [ "A = np.array([[1,1,1],[1,1,1]])\n", "b = np.array([[0],[1]])\n", "\n", "rkA = np.linalg.matrix_rank(A)\n", "print(rkA)\n", "\n", "rkAb = np.linalg.matrix_rank(np.hstack([A,b]))\n", "print(rkAb)\n", "\n", "x_q = linalg.lstsq(A,b)[0]\n", "print(\"\\n\",x_q,\"\\n\\n\",A@x_q)\n", "\n", "print(\"\\n\",linalg.null_space(A))\n" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }