Skip to content
Open

lab4 #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Exams/Final/Finalsolutions.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Exams/Mid-term/solutions to Exam (1).ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Lab-2-solutions.ipynb.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Labs/Lab-1/_Lab-1.ipynb solutions.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Labs/Lab-2/Lab-2-solutions.ipynb.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Labs/Lab-3/Lab-3-solutions.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Labs/Lab-5/Lab-5solutions.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Labs/Lab-6/Lab-6solutions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"nbformat":4,"nbformat_minor":0,"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.1"},"colab":{"name":"Lab-6solutions.ipynb","provenance":[{"file_id":"https://github.com/justinkeas/DATA1401-Spring-2020/blob/master/Labs/Lab-6/Lab-6.ipynb","timestamp":1587756361221}],"collapsed_sections":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"wxey2xn3VgMN","colab_type":"text"},"source":["# Lab 6\n"]},{"cell_type":"markdown","metadata":{"id":"2kFyeYy-VgMQ","colab_type":"text"},"source":["Matrix Representation: In this lab you will be creating a simple linear algebra system. In memory, we will represent matrices as nested python lists as we have done in lecture. \n","\n","1. Create a `matrix` class with the following properties:\n"," * It can be initialized in 2 ways:\n"," 1. with arguments `n` and `m`, the size of the matrix. A newly instanciated matrix will contain all zeros.\n"," 2. with a list of lists of values. Note that since we are using lists of lists to implement matrices, it is possible that not all rows have the same number of columns. Test explicitly that the matrix is properly specified.\n"," * Matrix instances `M` can be indexed with `M[i][j]` and `M[i,j]`.\n"," * Matrix assignment works in 2 ways:\n"," 1. If `M_1` and `M_2` are `matrix` instances `M_1=M_2` sets the values of `M_1` to those of `M_2`, if they are the same size. Error otherwise.\n"," 2. In example above `M_2` can be a list of lists of correct size.\n"]},{"cell_type":"code","metadata":{"id":"Fi7UNfO_ZyQr","colab_type":"code","colab":{}},"source":["class matrix:\n"," def __init__(self, m, n):\n"," self.__m=m\n"," self.__n=n\n","\n"," def zero_matrix(self):\n"," out=list()\n"," for i in range(self.__m):\n"," row=list()\n"," for j in range(self.__n):\n"," row.append(0.)\n"," out.append(row)\n"," return out\n"," \n"," def eye_matrix(self):\n"," M=zero_matrix(self.__n,self.__n)\n"," for i in range(self.__n):\n"," M[i][i]=1.\n"," return M\n"," \n"," def is_matrix(M):\n"," if isinstance(M,list):\n"," row_length=len(M[0])\n"," for row in M:\n"," if not row_length==len(row):\n"," return False\n"," else:\n"," False\n"," return True"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"0UuG9YZh8HnR","colab_type":"code","outputId":"af4c7054-4b57-4767-88f4-0eec3850f080","executionInfo":{"status":"ok","timestamp":1587744561588,"user_tz":300,"elapsed":635,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["matrix(5,5)"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["<__main__.matrix at 0x7f574ccb4128>"]},"metadata":{"tags":[]},"execution_count":4}]},{"cell_type":"markdown","metadata":{"id":"NMtW996iVgMR","colab_type":"text"},"source":["2. Add the following methods:\n"," * `shape()`: returns a tuple `(n,m)` of the shape of the matrix.\n"," * `transpose()`: returns a new matrix instance which is the transpose of the matrix.\n"," * `row(n)` and `column(n)`: that return the nth row or column of the matrix M as a new appropriately shaped matrix object.\n"," * `to_list()`: which returns the matrix as a list of lists.\n"," * `block(n_0,n_1,m_0,m_1)` that returns a smaller matrix located at the n_0 to n_1 columns and m_0 to m_1 rows. \n"," * (Extra credit) Modify `__getitem__` implemented above to support slicing.\n"," "]},{"cell_type":"code","metadata":{"id":"sW1cg6h0854X","colab_type":"code","colab":{}},"source":["class matrix:\n"," def __init__(self, m, n):\n"," self.__m=m\n"," self.__n=n\n","\n"," def zero_matrix(self):\n"," out=list()\n"," for i in range(self.__m):\n"," row=list()\n"," for j in range(self.__n):\n"," row.append(0.)\n"," out.append(row)\n"," return out\n"," \n"," def eye_matrix(self):\n"," M=zero_matrix(self.__n,self.__n)\n"," for i in range(self.__n):\n"," M[i][i]=1.\n"," return M\n"," \n"," def is_matrix(M):\n"," if isinstance(M,list):\n"," row_length=len(M[0])\n"," for row in M:\n"," if not row_length==len(row):\n"," return False\n"," else:\n"," False\n"," return True\n"," \n"," def matrix_shape(M):\n"," if is_matrix(M):\n"," self.__m=len(M)\n"," self.__n=len(M[0])\n"," return self.__m,self.__n\n"," else:\n"," 0,0\n"," \n"," def matrix_transpose(M):\n"," self.__m,self.__n=matrix_shape(M)\n"," M_out=zero_matrix(self.__n,self.__m)\n"," for i in range(self.__m):\n"," for j in range(self.__n):\n"," M_out[j][i]=M[i][j]\n"," return M_out\n","\n"," def matrix_row(self):\n"," return M[i]\n","\n"," def matrix_column(self):\n"," return M[j]\n","\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"HBSwpZIQ864F","colab_type":"code","outputId":"42fdef86-e4d0-45ac-8b11-7f58f9734472","executionInfo":{"status":"error","timestamp":1587756210077,"user_tz":300,"elapsed":693,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":337}},"source":["my_matrix=matrix(4,5)\n","my_matrix.is_matrix()\n","my_matrix.matrix_transpose()"],"execution_count":0,"outputs":[{"output_type":"error","ename":"NameError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)","\u001b[0;32m<ipython-input-33-10e50703137a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmy_matrix\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmatrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmy_matrix\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mmy_matrix\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatrix_transpose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;32m<ipython-input-32-583796988854>\u001b[0m in \u001b[0;36mmatrix_transpose\u001b[0;34m(M)\u001b[0m\n\u001b[1;32m 38\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmatrix_transpose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 40\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__m\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__n\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmatrix_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 41\u001b[0m \u001b[0mM_out\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mzero_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__n\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 42\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m<ipython-input-1-f340e0872b8d>\u001b[0m in \u001b[0;36mmatrix_shape\u001b[0;34m(M)\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmatrix_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 33\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mis_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 34\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mNameError\u001b[0m: name 'is_matrix' is not defined"]}]},{"cell_type":"markdown","metadata":{"id":"dOHIVUnkVgMS","colab_type":"text"},"source":["3. Write functions that create special matrices (note these are standalone functions, not member functions of your `matrix` class):\n"," * `constant(n,m,c)`: returns a `n` by `m` matrix filled with floats of value `c`.\n"," * `zeros(n,m)` and `ones(n,m)`: return `n` by `m` matrices filled with floats of value `0` and `1`, respectively.\n"," * `eye(n)`: returns the n by n identity matrix."]},{"cell_type":"code","metadata":{"id":"HYJP_9oQE3pt","colab_type":"code","colab":{}},"source":["def constant_matrix(n,m,c):\n"," out=list()\n"," for i in range(n):\n"," row=list()\n"," for j in range(m):\n"," row.append(c)\n"," out.append(row)\n"," return out\n","\n","def zero_matrix(n,m):\n"," out=list()\n"," for i in range(n):\n"," row=list()\n"," for j in range(m):\n"," row.append(0.)\n"," out.append(row)\n"," return out\n","\n","def ones_matrix(n,m):\n"," out=list()\n"," for i in range(n):\n"," row=list()\n"," for j in range(m):\n"," row.append(1)\n"," out.append(row)\n"," return out\n","\n","def eye_matrix(n):\n"," M=zero_matrix(n,n)\n"," for i in range(n):\n"," M[i][i]=1.\n"," return M"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"lNs_vJpLE2xi","colab_type":"code","outputId":"491c8ccf-37d2-4f9a-f177-ab1631061249","executionInfo":{"status":"ok","timestamp":1587747132273,"user_tz":300,"elapsed":518,"user":{"displayName":"","photoUrl":"","userId":""}},"colab":{"base_uri":"https://localhost:8080/","height":101}},"source":["constant_matrix(5,5,5)"],"execution_count":0,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[[5, 5, 5, 5, 5],\n"," [5, 5, 5, 5, 5],\n"," [5, 5, 5, 5, 5],\n"," [5, 5, 5, 5, 5],\n"," [5, 5, 5, 5, 5]]"]},"metadata":{"tags":[]},"execution_count":30}]},{"cell_type":"markdown","metadata":{"id":"jPW6VKbaVgMT","colab_type":"text"},"source":["4. Add the following member functions to your class. Make sure to appropriately test the dimensions of the matrices to make sure the operations are correct.\n"," * `M.scalarmul(c)`: a matrix that is scalar product $cM$, where every element of $M$ is multiplied by $c$.\n"," * `M.add(N)`: adds two matrices $M$ and $N$. Don’t forget to test that the sizes of the matrices are compatible for this and all other operations.\n"," * `M.sub(N)`: subtracts two matrices $M$ and $N$.\n"," * `M.mat_mult(N)`: returns a matrix that is the matrix product of two matrices $M$ and $N$.\n"," * `M.element_mult(N)`: returns a matrix that is the element-wise product of two matrices $M$ and $N$.\n"," * `M.equals(N)`: returns true/false if $M==N$."]},{"cell_type":"markdown","metadata":{"id":"RWoVn-J2VgMU","colab_type":"text"},"source":["5. Overload python operators to appropriately use your functions in 4 and allow expressions like:\n"," * 2*M\n"," * M*2\n"," * M+N\n"," * M-N\n"," * M*N\n"," * M==N\n"," * M=N\n"]},{"cell_type":"markdown","metadata":{"id":"fmQpLjeGVgMV","colab_type":"text"},"source":["6. Demonstrate the basic properties of matrices with your matrix class by creating two 2 by 2 example matrices using your Matrix class and illustrating the following:\n","\n","$$\n","(AB)C=A(BC)\n","$$\n","$$\n","A(B+C)=AB+AC\n","$$\n","$$\n","AB\\neq BA\n","$$\n","$$\n","AI=A\n","$$"]},{"cell_type":"code","metadata":{"id":"IpZpvU8bVgMW","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]}
1 change: 1 addition & 0 deletions Labs/Lab-7/Lab-7solutions.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions _Lab-1.ipynb solutions.ipynb

Large diffs are not rendered by default.