Question: Please use the code provided in Python and use the following set of instructions. This will not work unless it is done in Python. Matrix

Please use the code provided in Python and use the following set of instructions. This will not work unless it is done in Python.

Please use the code provided in Python and use the following set

Matrix Code:

# Write your code below:

def main():

m1 = Matrix(3, 3, 2)

m2 = Matrix(3, 3, 4)

print(m1 + m2)

m1 = Matrix(4, 4, 2)

m2 = Matrix(4, 4, 4)

print(m2 - m1)

m1 = Matrix(2, 3, 2)

m2 = Matrix(3, 2, 4)

print(m1 * m2)

if __name__ == "__main__": main()

Arrays Code:

class Array(object):

"""Represents an array."""

def __init__(self, capacity, fillValue = None):

"""Capacity is the static size of the array.

fillValue is placed at each position."""

self.items = list()

for count in range(capacity):

self.items.append(fillValue)

def __len__(self):

"""-> The capacity of the array."""

return len(self.items)

def __str__(self):

"""-> The string representation of the array."""

return str(self.items)

def __iter__(self):

"""Supports traversal with a for loop."""

return iter(self.items)

def __getitem__(self, index):

"""Subscript operator for access at index."""

return self.items[index]

def __setitem__(self, index, newItem):

"""Subscript operator for replacement at index."""

self.items[index] = newItem

Grid Code:

from arrays import Array

class Grid(object):

"""Represents a two-dimensional array."""

def __init__(self, rows, columns, fillValue = None):

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(self.data[0])

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(self.data[row][col]) + " "

result += " "

return result

def main():

g = Grid(10, 10, 1)

print(g)

if __name__ == "__main__":

main()

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. The Matrix class should extend the Grid class. If two matrices cannot be multiplied, raise an exception . A main() has been provided to test the implementation of Matrix

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!