These are individual homework. While you may discuss the ideas and algorithms or share the test cases with others, at no time may you read, possess, or submit the solution code of anyone else (including people outside this course), submit anyone else's solution code to your account on the gradebot, or allow anyone else to read or possess your source code. We will detect plagiarism using automated tools and will prosecute all violations to the fullest extent of the university regulations, including failing this course, academic probation, and expulsion from the university.
- Matrix calcluation
- Class
- Standard operators, e.g.,
+ - Recursion
Matrices are widely used both in mathematics and computer sciences, such as game thoery and economics. In homework 2 and 3, you will implement a module for matrix calculation.
- Matrix Wiki
- Prefix notation (Polish notation)
- Postfix notation (Reverse notation)
- Invertible matrix
- Determinant
Matrixis defined in Backus-Naur Form, see BNF Wiki.- In case you don't know matrix or linear algebra well, search on internet,it is not too diffcult to understand. Programs are used to solve real-world problems. However, to understand the problems, you may have to learn some domain knowledge related to the problems.
Matrix::= "[" Rows "]" | "[" "]"
Rows::= Row | Row ";" Rows
Row::= element | element "," Row
elment::= int | float | complex
- Implement a class
Matrixfor representing matrix and implement the following operations in the Matrix class. Instance object of Matrix is created byx=Matrix(s), wheresis a string in Matrix syntax allowing white space" ". If the stringsdoes not follow the Matrix syntax, raise the exceptionMatrixSyntaxError - Operations:
+,-,*,/,**,==,isIdentity,isSquare,determinant,inverse,index,slice,__str__,transposition+, -, *, /, **, ==are standard operators, like forint, float.*supports both Scalar multiplicationx * cand Matrix Multiplicationx * y. For division and pow, we only condsiderx/candx**c. Note thatx,yare matrices andcis a number (int, float, complex). The operands of+,-,==are matrices. Raise the exceptionMatrixSyntaxErrorif the operands of+,-,*,**do not meet the requirment for the corresponding operation, e.g., adding a2-by-3matrix with a2-by-4matrix should raise the exceptionisIdentityreturnsTrueif the matrix is an identity matrix, otherwiseFalse, empty matrix is regarded as an identity matrixisSquarereturnsTrueif the matrix is a square matrix, otherwiseFalse, empty matrix is regarded as a square matrixindexsimilar to list indexx[i]returns thei-th row (starting at0) which also is a matrixx[i,j]returns the element at thei-th row andj-th columnx[i,j]=1replaces the elementx[i,j]by1inxx[i]=Matrix(..)replaces the rowx[i]by the rowMatrix(..)inxif the lengths of the rowx[i]and the rowMatrix(..)are identical, otherwise raise the exceptionMatrixSyntaxError
slicesimilar to list slicingx[start1:stop1:step1,start2:stop2:step2]returns the matrixy, such thaty[i,j]is the elementx[start1+i*step1,start2+j*step2]if it exists andstart1+i*step1<stop1,start2+i*step2<stop2x[start1:stop1:step1,start2:stop2:step2]=Matrix(..)replaces the matrixx[start1+i*step1,start2+j*step2]byMatrix(..)inxif the number of rows (as well as columns) ofx[start1+i*step1,start2+j*step2]areMatrix(..)are identical, otherwise raise the exceptionMatrixSyntaxError`
__str__returns a string denoting the matrix in Matrix syntax without white space and follows that:joutputs1j- for any value
x, ifx==0, outputs0 - for any complex
a + bj, ifa==0andb==0(see Item 2); ifa==0andb!=0outputsbj; ifa!=0andb==0, outputsa; otherwise outputsa+bjrather than(a+bj) - for float
x, ifx==int(x), outputsint(x), otherwise outputsxwithout any round
determinantreturns the determinant of the matrix, if the matrix is not a square matrix, raise the exceptionMatrixSyntaxErrorinversereturns the inverse of the matrix,if the matrix is not a square matrix or invertible, raise the exceptionMatrixSyntaxError
- Implementation of
determinantandinversefor2-by-2, 3-by-3matrices is mandatory with the exceptionMatrixSyntaxErrorfor non-square matrix - Optional: comlete the implementation of
determinantandinverseforn-by-nmatrices withn>=4is optional, as extra credits, (testcases 58 and 59)
>>> x = Matrix("[1,2,3; 4,5,6; 7,8,9]") # can have white space
>>> print(x)
[1,2,3;4,5,6;7,8,9] # the output does not contain any white space
>>> y = Matrix("[0,1,2; 3,4,5; 6,7,8]")
>>> z = x + y # z is an matrix instance object
>>> print(z)
[1,3,5;7,9,11;13,15,17]
>>> z = x-y
>>> print(z)
[1,1,1;1,1,1;1,1,1]
>>> z = x * 2
>>> print(z)
[2,4,6;8,10,12;14,16,18]
>>> z = x * y
>>> print(z)
[24,30,36;51,66,81;78,102,126]
>>> print(z/2)
[12,15,18;25.5,33,40.5;39,51,63]
>>> z = x**3 #e.g., x***3 = x*x*x
>>> print(z)
[468,576,684;1062,1305,1548;1656,2034,2412]
>>> x == y
False
>>> x == Matrix("[1,2,3; 4,5,6;7,8,9]")
True
>>> x.isIdentity()
False
>>> Matrix("[1,0,0,0; 0,1,0,0; 0,0,1,0; 0,0,0,1]").isIdentity()
True
>>> x.isSquare()
True
>>> Matrix("[1,2,3,4; 0,1,4,0; 0,0,1,0; 0,0,0,1]").isSquare()
True
>>> z = x[2]
>>> print(z)
[7,8,9]
>>> x[2,1]
8
>>> z = x[1:3:1,0:3:2]
>>> print(z)
[4,6;7,9]
>>> x[2] = Matrix("[17,18,19]")
>>> print(x)
[1,2,3;4,5,6;17,18,19]
>>> x[1,2] = 0
>>> print(x)
[1,2,3;4,5,0;17,18,19]
>>> x[1:3:1,0:3:2] = Matrix("[14,16;7,9]")
>>> print(x)
[1,2,3;14,5,16;7,18,9]
>>> x = Matrix("[2,-2;-1,5]")
>>> x.determinant()
8
>>> x = Matrix("[2,4;1,0]")
>>> z = x.inverse()
>>> print(z)
[0,1;0.25,-0.5]- check in
Matrix.pyfile into github
- Some students may want to practise programming skills, here we select some projects. These projects may have been implemented. But, you can still choose some and solve them using Python or Rust. These projects are open ended and you will not get any extra credit
- Text analysis: Sample
- Weather Analysis: Sample
- Binary decision diagram: Sample
- Electronic Design Automation: Sample