Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

defining matrix class in python

enter image description here

Define a class that abstracts the matrix that satisfies the following examples of practice

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
class Matrix:
    def __init__(self, n, m):
        self.matrix = self.get_matrix(n, m)

    def get_matrix(self, n, m):
        num = 1
        matrix = [[None for j in range(m)] for i in range(n)]
        for i in range(len(matrix)):
            for j in range(len(matrix[i])):
                matrix[i][j] = num
                num += 1
        return matrix

    def get_readable_matrix_string(self, matrix):
        strings = []
        for row in matrix:
            strings.append(str(row))
        return '
'.join(strings)  

    def __str__(self):
        return self.get_readable_matrix_string(self.matrix)
    
    def __len__(self):
        return len(self.matrix)

    def __getitem__(self, item):
        return self.matrix[item]

    def getElement(self, i, j):
        return self.matrix[i-1][j-1]
    
    def setElement(self, i, j, element):
        self.matrix[i-1][j-1] = element
    
    def transpose(self, matrix):
        return [list(i) for i in zip(*matrix)]

    def getTranspose(self):
        return self.get_readable_matrix_string(self.transpose(self.matrix))
    
    def doTranspose(self):
        self.matrix = self.transpose(self.matrix)
    
    def multiply(self, matrix):
        result = [[0 for j in range(len(matrix[i]))] for i in range(len(self.matrix))]
        for i in range(len(self.matrix)):
            for j in range(len(matrix[0])):
                for k in range(len(matrix)):
                    result[i][j] += self.matrix[i][k] * matrix[k][j]
        return result

    def getMultiply(self, matrix):
        return self.get_readable_matrix_string(self.multiply(matrix))
    
    def __mul__(self, other):
        if isinstance(other, Matrix):
            return self.get_readable_matrix_string(self.multiply(other))
        return self.get_readable_matrix_string([[num*other for num in row] for row in self.matrix])
    
        


m1 = Matrix(2, 3)
print(m1)
# [1, 2, 3]
# [4, 5, 6]
print(m1.getElement(2,2))
# 5
m1.setElement(2,2,-10)
print(m1.getTranspose())
# [1, 4]
# [2, -10]
# [3, 6]
print(m1)
# [1, 2, 3]
# [4, -10, 6]
m1.doTranspose()
print(m1)
# [1, 4]
# [2, -10]
# [3, 6]
m2 = Matrix(2,3)
print(m2)
# [1, 2, 3]
# [4, 5, 6]
print(m2.getMultiply(m1))
# [14, 2]
# [32, 2]
print(m2 * m1)
# [14, 2]
# [32, 2]
print(m1)
# [1, 4]
# [2, -10]
# [3, 6]
print(m1 * 3)
# [3, 12]
# [6, -30]
# [9, 18]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...