TAGS :Viewed: 6 - Published at: a few seconds ago

[ how to create a between matrix calculations class in python? ]

I am trying to create a class that will be able to make all possible calculations between matrix. the input is a matrix (doesn't matter the size), and on that matrix the class should do any calculations - either multiply, combine or substract. The different functions are as follow: I need to have in this class:

__init__(self, data)

get_width(self)

get_height(self)

add(self, m)

scalar_multiply(self, a)

subtract(self, m)

multiply(self, m)

compare(self, m)

Thanks alot, it also needs to be with simple actions.
This is my code till now:

class Matrix:
    def __init__(self, data):
        self.data = data

    def get_width(self):
        return len(self.data[0])

    def get_height(self):
        return len(self.data)

    def add(self, m):
        lines = []
        for j in range(len(self.data)):
            line = []
            for i in range(len(self.data[j])):
                line.append(self.data[j][i] + m[j][i])
            lines.append(line)
        return lines

    def scalar_multiply(self, a):
        res = []
        for j in range(len(self.data)):
            line = []
            for i in range(len(self.data)):
                line.append(self.data[j][i]*a)
            res.append(line)
        return res

    def subtract(self, m):
        lines = []
        for j in range(len(self.data)):
            line = []
            for i in range(len(self.data)):
                line.append(self.data[j][i] - m[j][i])
            lines.append(line)
        return lines

    def multiply(self, m):
        lines = []
        for j in range(len(self.data-1)):
            line = []
            for i in range(len(m[0])-1):
                schum = 0
                for k in range(len(self.data[0]-1)):
                    schum = self.data[j][k]*m[k][i]
                line.append(schum)
            lines.append(line)
        return lines

    def compare(self, m):
        for j in range(len(self.data)):
            for i in range(len(self.data)[j]):
                while self.data[j][i] == m[j][i]:
                    return True
                else:
                    return False

Answer 1


Do you need to create this yourself? If not, there is already an excellent implementation of this in numpy