Question
Code in Python!!!! A Matrix class can be used to perform some of the operations found in linear algebra, such as matrix arithmetic. Develop a
Code in Python!!!! A Matrix class can be used to perform some of the operations found in linear algebra, such as matrix arithmetic.
Develop a Matrix class that uses the built-in operators for arithmetic.
In the matrix.py file, complete the following:
Define the Matrix class and extend the Grid class.
Complete the implementation of the following methods:
__init__
__add__, adds two matrices together.
Returns the sum of self and other.
__sub__, subtracts two matrices.
Returns the difference of self and other.
__mul__, multiplies two matrices together.
Returns the product of self and other.
If two matrices cannot be multiplied, raise an Exception.
To test your program run the main() method in the matrix.py file.
Your program's output should look like the following:
6 6 6 6 6 6 6 6 6 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 24 24 24 24
grid.py matrix.py File: grid.py from arrays import Array class Grid(object):1 " Represents a two-dimensional array." self.data = Array(rows) for row in range(rows): self.data [ row ]= Array(columns, fillValue) def getHeight (self): " "Returns the number of rows."\| return len(self.data) def getWidth ( self ) : "Returns the number of columns." return len (selfdata[]) def _-getitem_--(self, index ) : "Supports two-dimensional indexing with [][]. return self.data [index ] def __str__(self): " Returns a string representation of the grid."\| result = " for row in range(self.getHeight ()): for col in range(self.getwidth( )): result +=str(selfdata[row][col])+ result += " " return result def main( ): g=Grid(10,10,1) print (g) if __-name ___==" "main__-" : main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started