Assignment Background
In this assignment you will create a Matrix class and its methods. Note that in mathematics and most engineering fields, matrix indices begin at 1, not 0 as implemented in Python (and most computer languages).
As noted below you are not allowed to import any modules that implement matrix operations.
Assignment Specifications
First you will need to implement the “matrix” class.
Matrix__init__(self,num_rows,num_colums):
Required Attributes – for testing you must use these exact names
a.num_rows: number of rows, default value is 2
b.num_cols: number of columns, default value is 2
c.array: A list of lists. It must be initialized as a list of lengthnum_rows, each index should have a list of lengthnum_cols, initialized withzeros. Warning: be careful that you don’t create multiple copies of the same row; instead you want multiple copies of rows with the same value.
(Hint:deepcopyis your friend, but is not required. Loops work just as well.)
Ifeither the number of rows or columns isnota positive integer, aValueErrorshould be
raised with a “Matrix: Error, the dimensions must be positive integers!” message using:raise ValueError("Matrix: Error, the dimensions must be positive integers!")
(Hint: note the use of “if” in this specification.)
__str__(self):
returns a string representation of the matrix. For instance, for an identity matrix with degree 2 this function returns a string:
[[1 0]
[0 1]]
Note that every Matrix row is printed in separate row and that each new line starts with a space so the left brackets line up. (Hint: one trick is to add something to every value or line and then remove it using slicing for the last one.)
__repr__(self):
Returns the same string as__str__(Hint: simply call__str__).__getitem__(self,indexes):
This function allows us to get the values from the matrix. The indexes parameter can be either an integer with the ith row of a matrix or a tuple with the ith row and jth column of the matrix. For instance A[i,j] will return theith rowjth column element. Additionally, the user can use A[i] to get theith row.
Ifindexesis not of typeintortuple of integers, then aValueErrorshould be raised with raise “Matrix: Error, the indices must be a positive integer or a tuple of integers!”. If the indexes are not positive integers, then anIndexErrorshould be raised with a “Matrix: Error, bad indexing!” message.
If theith orjth indexes are above the number of rows and columns or are not positive values, then anIndexErrorshould be raised with a “Matrix: Error, index out of range!” message.
This is similar to the raising of an error described above in__init__().
Hint: to create a Boolean for multiple items that you don’t want, try something like this:
if not(what you want):
Note that the legal range for the indexes are 1 tonum_rowsornum_cols. However, the indexing in python starts from 0. Also, for A[i,j] thei,jargument will be passed as a tuple(i,j)whereas for A[i] the argument will be passed simply asi, that is, not a tuple. Here we are making use of the Python feature thati,jis a tuple, that is, it is identical to the tuple(i,j). (For the curious: we use A[i,j] rather than A[i][j] for Matrix calls because it is easier for you to implement for this project.)
__setitem__(self,indexes,value):
This function allows us to set the values from the matrix. It is mostly a copy of__getitem()__For instance, A[i,j]=vwill set theithjth element to be equal tov.
If the value is not a float or integer, aValueErrorshould be raised with a “Matrix: Error, You can only assign a float or int to a matrix!” message.
Ifindexesis not of typetuple of integers, then aValueErrorshould be raised with raise “Matrix: Error, the indices must be a tuple of integers!”.
If the indexes are not positive integers, then anIndexErrorshould be raised with a “Matrix: Error, bad indexing!” message. If theith orjth indexes are above the number of rows and
columns, then anIndexErrorshould be raised with a “Matrix: Error, index out of range!” message.
Note that the legal range for the indexes are 1 tonum_rowsornum_cols. However, the indexing in Python starts from 0.
__add__(self,B):
This function performs a matrix addition. For instance, A+B will return a new matrix C with theith rowjth column element equal to the addition of thisith rowjth column element in the two matrices. That is, C[1,1] is the sum of A[1,1] and B[1,1]. If B is not a matrix, then aValueErrorshould be raised with a “Matrix: Error, you can only add a matrix to another matrix!” message. If the dimension of the matrices A and B are not the same, then aValueErrorshould be raised with a “Matrix: Error, matrices dimensions must agree in addition!” message. Ensure that the item returned is of type Matrix. (Hint: be careful with indexing!)
dot_product(self,L1,L2):
Return the dot product of two lists of numbers as indicated below. If the length of L1 and L2 are not the same then aValueErrorshould be raised with a"Dot Product: must be same length"message.
Here is one step of the multiplication (known as the dot product) from the mathisfun page.
__mul__(self,B):
This function performs the multiplication of two matrices. For instance A*B will return a new matrix C.You MUST use the dot_product function defined above as part of your solution. Note to use the method from theMatrixClass you have to callMatrix.dot_product().You can read about matrix multiplication online athttp://en.wikipedia.org/wiki/Matrix_multiplicationor a simplified version at:https://www.mathsisfun.com/algebra/matrix-multiplying.html.
If B is not aMatrixthen aValueErrorshould be raised with a “Matrix: Error, you can only multiply a matrix to another matrix!” message. If the dimension of the matrices do not agree then aValueErrorshould be raised with a “Matrix: Error, matrices dimensions must agree in multiplication!” message.
Important: the rules for matching dimensions for matrix multiplication are different than for matrix addition.To multiply a matrix that hasmrows andncolumns(m×n)by a matrix that haskrows andpcolumns(k×p),thenandkmust be the same, and the result is anm×pmatrix.Make sure that you return an object of type Matrix.
transpose(self):
This function returns the transpose of the matrix. For instance A.transpose() will return a new matrix. You can read about matrix transposition here:http://en.wikipedia.org/wiki/Transpose. Basically, you are swapping rows and columns.
__eq__(self,B):
This function returnsTrueif corresponding values are equal, andFalseotherwise. Note if the dimensions of the matrices are different,Falsewill be returned. (Hint: check that B is of type Matrix then check if the dimensions are the same before checking if corresponding values are the same.).
__rmul__(self,i):
This function performs a scalar multiplication, i.e. the multiplication of a scalar (int in our case) and a Matrix. For example, C= 2*A where each element of A is multiplied by 2. If parameteriis not anintraise aValueErrorwith the message"Matrix Error: scaler must be an int."Make sure you return an object of typeMatrix. (Hint, don’t overthink this function, it is much easier than matrix multiplication)