Skip to content

Commit

Permalink
Feat: added scalar_multiply for Gussian Elemin
Browse files Browse the repository at this point in the history
  • Loading branch information
MagnusS0 committed Dec 3, 2023
1 parent 058a6b2 commit 1527317
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion submission/snumpy.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Sub-Numpy\n","We will create our own implementation of a few functionalities supported by the NumPy\n","Library. We will call our implementation SNumPy (for Sub-NumPy). SNumPy will be the name of the\n","class you implement, and we will refer to it by the shorthand “snp” from here on"]},{"cell_type":"code","execution_count":63,"metadata":{},"outputs":[],"source":["# Create SNumPy class that will hold our methods for data manipulation methods, not using numpy\n","# Including snp.ones(Int), snp.zeros(Int), snp.reshape(array, (row, column)), snp.shape(array), snp.append(array1, array2)\n","# snp.get(array, (row, column)), snp.add(array1, array1), snp.subtract(array1, array1), snp.dotproduct(array1, array1)\n","\n","class SNumPy:\n"," \"\"\"\n"," SNumPy class for basic array manipulations.\n"," \"\"\"\n","\n"," @staticmethod\n"," def ones(n, m=None):\n"," \"\"\"\n"," Return an array of ones in given shape (n,m).\n"," \"\"\"\n"," if m == None:\n"," return [1 for i in range(n)]\n"," else:\n"," return [[1 for i in range(m)] for j in range(n)]\n","\n","\n"," @staticmethod\n"," def zeros(n, m=None):\n"," \"\"\"\n"," Return an array of zeros in given shape (n,m).\n"," \"\"\"\n"," if m == None:\n"," return [0 for i in range(n)]\n"," else:\n"," return [[0 for i in range(m)] for j in range(n)]\n","\n"," @staticmethod\n"," def reshape(array, shape):\n"," \"\"\"\n"," Return an array containing the same data with a new shape.\n"," \"\"\"\n"," column, row = shape\n"," new_array = []\n"," for i in range(row):\n"," new_array.append(array[i*column:(i+1)*column])\n"," return new_array\n","\n"," @staticmethod\n"," def shape(array):\n"," \"\"\"\n"," Return the shape of an array.\n"," \"\"\"\n"," # Check if it's a vector (1D array)\n"," if not array or not isinstance(array[0], list):\n"," return (len(array),)\n"," # Else, it's a matrix (2D array)\n"," else:\n"," return (len(array), len(array[0]))\n","\n"," @staticmethod\n"," def append(array1, array2):\n"," \"\"\"\n"," Return an array containing the same data with a new shape.\n"," \"\"\"\n"," return array1 + array2\n","\n"," @staticmethod\n"," def get(array, index):\n"," \"\"\"\n"," Return the element at the given index.\n"," \"\"\"\n"," column, row = index # Unpack the index tuple\n"," return array[row][column]\n","\n"," @staticmethod\n"," def add(array1, array2):\n"," \"\"\"\n"," Return the element-wise sum of two arrays.\n"," \"\"\"\n"," try:\n"," if SNumPy.shape(array1) != SNumPy.shape(array2):\n"," raise ValueError(\"Arrays must be the same size\")\n"," except ValueError as e:\n"," print(e)\n"," return None\n","\n"," # Using zip() will parrallelize the addition of the arrays\n"," return [\n"," [\n"," cell1 + cell2 \n"," for cell1, cell2 in zip(row1, row2)\n"," ] \n"," for row1, row2 in zip(array1, array2)\n"," ]\n","\n"," @staticmethod\n"," def subtract(array1, array2):\n"," \"\"\"\n"," Return the element-wise difference of two arrays.\n"," \"\"\"\n"," try:\n"," if SNumPy.shape(array1) != SNumPy.shape(array2):\n"," raise ValueError(\"Arrays must be the same size\")\n"," except ValueError as e:\n"," print(e)\n"," return None\n"," \n"," # Using zip() will parrallelize the subtraction of the arrays\n"," return [\n"," [\n"," cell1 - cell2 \n"," for cell1, cell2 in zip(row1, row2)\n"," ] \n"," for row1, row2 in zip(array1, array2)\n"," ]\n","\n"," @staticmethod\n"," def dotproduct(array1, array2):\n"," \"\"\"\n"," Check if its vectors or matrices, then return the dot product.\n"," \"\"\"\n"," # Check if its vectors\n"," if len(SNumPy.shape(array1)) == 1 and len(SNumPy.shape(array2)) == 1:\n"," # Check if the vectors are the same size\n"," try:\n"," if SNumPy.shape(array1) != SNumPy.shape(array2):\n"," raise ValueError(\"Vectors must be the same size\")\n"," except ValueError as e:\n"," print(e)\n"," return None\n","\n"," return sum([array1[i] * array2[i] for i in range(len(array1))])\n"," \n"," # Else do matrix multiplication\n"," else:\n"," # Check if the columns of array1 are the same size as the rows of array2\n"," try:\n"," if SNumPy.shape(array1)[1] != SNumPy.shape(array2)[0]:\n"," raise ValueError(f\"Array1's columns '{SNumPy.shape(array1)[1]}' must be the same size as Array2's rows '{SNumPy.shape(array2)[0]}'\")\n"," except ValueError as e:\n"," print(e)\n"," return None\n","\n"," return [\n"," [\n"," sum(\n"," [array1[i][k] * array2[k][j] \n"," for k in range(len(array1[0]))]\n"," ) \n"," for j in range(len(array2[0]))\n"," ] \n"," for i in range(len(array1))\n"," ]\n","\n","\n","\n"," \n"," \n"," \n"," "]},{"cell_type":"code","execution_count":65,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[1, 1, 1, 1, 1]\n","[0, 0, 0, 0, 0]\n","[[1, 2], [3, 4], [5, 6]]\n","(2, 3)\n","[1, 2, 3, 4, 5, 6]\n","5\n","[[2, 4, 6], [8, 10, 12]]\n","[[0, 0, 0], [0, 0, 0]]\n","[[27, 33, 39], [63, 78, 93]]\n"]}],"source":["# Test cases for SNumPy class\n","\n","snp = SNumPy()\n","print(snp.ones(5))\n","print(snp.zeros(5))\n","print(snp.reshape([1, 2, 3, 4, 5, 6], (2, 3)))\n","print(snp.shape([[1, 2, 3], [4, 5, 6]]))\n","print(snp.append([1, 2, 3], [4, 5, 6]))\n","print(snp.get([[1, 2, 3], [4, 5, 6]], (1, 1)))\n","print(snp.add([[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]))\n","print(snp.subtract([[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]))\n","print(snp.dotproduct([[1, 2, 3], [4,5,6]], [[4, 5, 6], [1, 2, 3], [7, 8, 9]]))"]},{"cell_type":"code","execution_count":44,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[1. 1. 1. 1. 1.]\n"]}],"source":["import numpy as np\n","\n","print(np.ones((5, )))"]}],"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.10.8"}},"nbformat":4,"nbformat_minor":2}
{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Sub-Numpy\n","We will create our own implementation of a few functionalities supported by the NumPy\n","Library. We will call our implementation SNumPy (for Sub-NumPy). SNumPy will be the name of the\n","class you implement, and we will refer to it by the shorthand “snp” from here on"]},{"cell_type":"code","execution_count":73,"metadata":{},"outputs":[],"source":["# Create SNumPy class that will hold our methods for data manipulation methods, not using numpy\n","# Including snp.ones(Int), snp.zeros(Int), snp.reshape(array, (row, column)), snp.shape(array), snp.append(array1, array2)\n","# snp.get(array, (row, column)), snp.add(array1, array1), snp.subtract(array1, array1), snp.dotproduct(array1, array1)\n","\n","class SNumPy:\n"," \"\"\"\n"," SNumPy class for basic array manipulations.\n"," \"\"\"\n","\n"," @staticmethod\n"," def ones(n, m=None):\n"," \"\"\"\n"," Return an array of ones in given shape (n,m).\n"," \"\"\"\n"," if m == None:\n"," return [1 for i in range(n)]\n"," else:\n"," return [[1 for i in range(m)] for j in range(n)]\n","\n","\n"," @staticmethod\n"," def zeros(n, m=None):\n"," \"\"\"\n"," Return an array of zeros in given shape (n,m).\n"," \"\"\"\n"," if m == None:\n"," return [0 for i in range(n)]\n"," else:\n"," return [[0 for i in range(m)] for j in range(n)]\n","\n"," @staticmethod\n"," def reshape(array, shape):\n"," \"\"\"\n"," Return an array containing the same data with a new shape.\n"," \"\"\"\n"," column, row = shape\n"," new_array = []\n"," for i in range(row):\n"," new_array.append(array[i*column:(i+1)*column])\n"," return new_array\n","\n"," @staticmethod\n"," def shape(array):\n"," \"\"\"\n"," Return the shape of an array.\n"," \"\"\"\n"," # Check if it's a vector (1D array)\n"," if not array or not isinstance(array[0], list):\n"," return (len(array),)\n"," # Else, it's a matrix (2D array)\n"," else:\n"," return (len(array), len(array[0]))\n","\n"," @staticmethod\n"," def append(array1, array2):\n"," \"\"\"\n"," Return an array containing the same data with a new shape.\n"," \"\"\"\n"," return array1 + array2\n","\n"," @staticmethod\n"," def get(array, index):\n"," \"\"\"\n"," Return the element at the given index.\n"," \"\"\"\n"," column, row = index # Unpack the index tuple\n"," return array[row][column]\n","\n"," @staticmethod\n"," def add(array1, array2):\n"," \"\"\"\n"," Return the element-wise sum of two arrays.\n"," \"\"\"\n"," try:\n"," if SNumPy.shape(array1) != SNumPy.shape(array2):\n"," raise ValueError(\"Arrays must be the same size\")\n"," except ValueError as e:\n"," print(e)\n"," return None\n","\n"," # Using zip() will parrallelize the addition of the arrays\n"," return [\n"," [\n"," cell1 + cell2 \n"," for cell1, cell2 in zip(row1, row2)\n"," ] \n"," for row1, row2 in zip(array1, array2)\n"," ]\n","\n"," @staticmethod\n"," def subtract(array1, array2):\n"," \"\"\"\n"," Return the element-wise difference of two arrays.\n"," \"\"\"\n"," try:\n"," if SNumPy.shape(array1) != SNumPy.shape(array2):\n"," raise ValueError(\"Arrays must be the same size\")\n"," except ValueError as e:\n"," print(e)\n"," return None\n"," \n"," # Using zip() will parrallelize the subtraction of the arrays\n"," return [\n"," [\n"," cell1 - cell2 \n"," for cell1, cell2 in zip(row1, row2)\n"," ] \n"," for row1, row2 in zip(array1, array2)\n"," ]\n","\n"," @staticmethod\n"," def dotproduct(array1, array2):\n"," \"\"\"\n"," Check if its vectors or matrices, then return the dot product.\n"," \"\"\"\n"," # Check if its vectors\n"," if len(SNumPy.shape(array1)) == 1 and len(SNumPy.shape(array2)) == 1:\n"," # Check if the vectors are the same size\n"," try:\n"," if SNumPy.shape(array1) != SNumPy.shape(array2):\n"," raise ValueError(\"Vectors must be the same size\")\n"," except ValueError as e:\n"," print(e)\n"," return None\n","\n"," return sum([array1[i] * array2[i] for i in range(len(array1))])\n"," \n"," # Else do matrix multiplication\n"," else:\n"," # Check if the columns of array1 are the same size as the rows of array2\n"," try:\n"," if SNumPy.shape(array1)[1] != SNumPy.shape(array2)[0]:\n"," raise ValueError(f\"Array1's columns '{SNumPy.shape(array1)[1]}' must be the same size as Array2's rows '{SNumPy.shape(array2)[0]}'\")\n"," except ValueError as e:\n"," print(e)\n"," return None\n","\n"," return [\n"," [\n"," sum(\n"," [array1[i][k] * array2[k][j] \n"," for k in range(len(array1[0]))]\n"," ) \n"," for j in range(len(array2[0]))\n"," ] \n"," for i in range(len(array1))\n"," ]\n"," \n"," @staticmethod\n"," def scalar_multiply(array, scalar):\n"," \"\"\"\n"," Return the scalar product of an array.\n"," \"\"\"\n"," return [\n"," [\n"," cell * scalar\n"," for cell in row\n"," ]\n"," for row in array\n"," ]\n"," \n"," "]},{"cell_type":"code","execution_count":78,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[1, 1, 1, 1, 1]\n","[0, 0, 0, 0, 0]\n","[[1, 2], [3, 4], [5, 6]]\n","(2, 3)\n","[1, 2, 3, 4, 5, 6]\n","5\n","[[2, 4, 6], [8, 10, 12]]\n","[[0, 0, 0], [0, 0, 0]]\n","[[27, 33, 39]]\n","[[2, 4, 6], [8, 10, 12]]\n"]}],"source":["# Test cases for SNumPy class\n","\n","snp = SNumPy()\n","print(snp.ones(5))\n","print(snp.zeros(5))\n","print(snp.reshape([1, 2, 3, 4, 5, 6], (2, 3)))\n","print(snp.shape([[1, 2, 3], [4, 5, 6]]))\n","print(snp.append([1, 2, 3], [4, 5, 6]))\n","print(snp.get([[1, 2, 3], [4, 5, 6]], (1, 1)))\n","print(snp.add([[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]))\n","print(snp.subtract([[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]))\n","print(snp.dotproduct([[1, 2, 3]], [[4, 5, 6], [1, 2, 3], [7, 8, 9]]))\n","print(snp.scalar_multiply([[1, 2, 3], [4, 5, 6]], 2))"]}],"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.10.8"}},"nbformat":4,"nbformat_minor":2}

0 comments on commit 1527317

Please sign in to comment.