matMul(DP).ipynb (3380B)
1 { 2 "cells": [ 3 { 4 "cell_type": "code", 5 "execution_count": 169, 6 "metadata": {}, 7 "outputs": [], 8 "source": [ 9 "import numpy as np\n", 10 "\n", 11 "# No side effects\n", 12 "def naiveVectorDotProduct(arr1, arr2):\n", 13 " ret = 0\n", 14 " for i in range(0, len(arr1)):\n", 15 " ret += arr1[i] * arr2[i]\n", 16 " return ret\n", 17 "\n", 18 "# x is matrix y is vector\n", 19 "def naiveMatrixVectorDot(x,y):\n", 20 " ret = np.zeros(shape=(len(x)))\n", 21 " for i in range(0,len(x)):\n", 22 " ret[i] = naiveVectorDotProduct(x[i], y)\n", 23 " return ret\n", 24 "\n", 25 "# mat mul\n", 26 "def naiveMatMul(x,y):\n", 27 " ret = np.zeros(shape=(len(x), len(y[0])))\n", 28 "\n", 29 " for z in range(0,len(x)):\n", 30 " for i in range(0, len(y[0])):\n", 31 " ret[z][i] = naiveVectorDotProduct(x[z], y[:,i])\n", 32 " return ret" 33 ] 34 }, 35 { 36 "cell_type": "code", 37 "execution_count": 170, 38 "metadata": {}, 39 "outputs": [ 40 { 41 "name": "stdout", 42 "output_type": "stream", 43 "text": [ 44 "168\n", 45 "168\n" 46 ] 47 } 48 ], 49 "source": [ 50 "x = np.array([1,3,4])\n", 51 "y = np.array([10,10,32])\n", 52 "print(x@y)\n", 53 "print(naiveVectorDotProduct(x,y))" 54 ] 55 }, 56 { 57 "cell_type": "code", 58 "execution_count": 171, 59 "metadata": {}, 60 "outputs": [ 61 { 62 "name": "stdout", 63 "output_type": "stream", 64 "text": [ 65 "[148 238 42 17]\n", 66 "[148 238 42 17]\n" 67 ] 68 } 69 ], 70 "source": [ 71 "x = np.array([\n", 72 " [10, 21, 2],\n", 73 " [23, 1, 2],\n", 74 " [3, 3, 2],\n", 75 " [1 , 2, 1]\n", 76 "])\n", 77 "y = np.array([10,2,3])\n", 78 "print(x @ y)\n", 79 "print(naiveMatrixVectorDot(x,y).astype(np.uint))" 80 ] 81 }, 82 { 83 "cell_type": "code", 84 "execution_count": 172, 85 "metadata": {}, 86 "outputs": [ 87 { 88 "name": "stdout", 89 "output_type": "stream", 90 "text": [ 91 "First: \n", 92 "[[ 7 12 12]\n", 93 " [13 20 22]]\n", 94 "Second: \n", 95 "[[ 7. 12. 12.]\n", 96 " [13. 20. 22.]]\n" 97 ] 98 } 99 ], 100 "source": [ 101 "X = np.array([\n", 102 " [0,1,3],\n", 103 " [1, 2, 4]\n", 104 "])\n", 105 "\n", 106 "y = np.array([\n", 107 " [3,2, 4],\n", 108 " [1,3, 3],\n", 109 " [2,3, 3], \n", 110 "])\n", 111 "\n", 112 "print(\"First: \\n\" + str(X@y))\n", 113 "print(\"Second: \\n\" + str(naiveMatMul(X,y)))" 114 ] 115 }, 116 { 117 "cell_type": "code", 118 "execution_count": 190, 119 "metadata": {}, 120 "outputs": [ 121 { 122 "data": { 123 "text/plain": [ 124 "True" 125 ] 126 }, 127 "execution_count": 190, 128 "metadata": {}, 129 "output_type": "execute_result" 130 } 131 ], 132 "source": [ 133 "x = np.random.random((5,30))\n", 134 "y = np.random.random((30,2))\n", 135 "\n", 136 "prod = (x @ y).astype(np.uint)\n", 137 "product = naiveMatMul(x,y).astype(np.uint)\n", 138 "res = product == prod\n", 139 "res.all()" 140 ] 141 } 142 ], 143 "metadata": { 144 "kernelspec": { 145 "display_name": ".venv", 146 "language": "python", 147 "name": "python3" 148 }, 149 "language_info": { 150 "codemirror_mode": { 151 "name": "ipython", 152 "version": 3 153 }, 154 "file_extension": ".py", 155 "mimetype": "text/x-python", 156 "name": "python", 157 "nbconvert_exporter": "python", 158 "pygments_lexer": "ipython3", 159 "version": "3.11.2" 160 } 161 }, 162 "nbformat": 4, 163 "nbformat_minor": 2 164 }