{ "cells": [ { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A:\n", " [[ 43 -10 -39 51]\n", " [ -7 3 -2 -2]\n", " [126 -63 75 105]\n", " [ 29 -9 -11 13]] \n", "Rank(A): 3 \n", "\n", "B:\n", " [[ 12 -52 108 94]\n", " [154 104 36 172]\n", " [ 33 70 -68 -25]\n", " [160 176 -80 88]] \n", "Rank(B): 3\n", "\n", "Basis for N(A) [n]:\n", " [[0.34055298]\n", " [0.92435809]\n", " [0.17027649]\n", " [0.02432521]]\n", "\n", "B@A@n (zeros) = \n", " [[ 9.09494702e-13]\n", " [ 1.81898940e-12]\n", " [-4.54747351e-13]\n", " [ 0.00000000e+00]]\n", "\n", "A@B@n = \n", " [[ 5412.91932134]\n", " [ 100.90098257]\n", " [13433.13657348]\n", " [ -133.13188949]]\n", "\n", "Matrix multiplication not commutative.\n" ] } ], "source": [ "# D. Scott\n", "# Linear Algebra: TCI\n", "# Code Challenge 8.1\n", "# Find null space vector and test commutativity\n", "\n", "import random\n", "import numpy as np\n", "from scipy.linalg import null_space\n", "\n", "# generate and populate random matrices with same inner value\n", "num1 = []\n", "num2 = []\n", "for i in range(0,12):\n", " num1.append(random.randint(-6,12))\n", "for i in range(0,12):\n", " num2.append(random.randint(-6,12))\n", "A1 = np.array(num1).reshape(4,3)\n", "A2 = np.array(num2).reshape(3,4)\n", "\n", "A = A1@A2\n", "\n", "num1 = []\n", "num2 = []\n", "for i in range(0,12):\n", " num1.append(random.randint(-6,12))\n", "for i in range(0,12):\n", " num2.append(random.randint(-6,12))\n", "B1 = np.array(num1).reshape(4,3)\n", "B2 = np.array(num2).reshape(3,4)\n", "\n", "B = B1@B2\n", "\n", "print(\"A:\\n\",A,\"\\nRank(A): \",np.linalg.matrix_rank(A),\n", " \"\\n\\nB:\\n\",B,\"\\nRank(B):\",np.linalg.matrix_rank(B))\n", "\n", "n = null_space(A)\n", "print(\"\\nBasis for N(A) [n]:\\n\",n)\n", "print(\"\\nB@A@n (zeros) = \\n\",B@A@n)\n", "print(\"\\nA@B@n = \\n\",A@B@n)\n", "print(\"\\nMatrix multiplication not commutative.\")" ] } ], "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.9.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }