如何用类实现矩阵

时间:2018-06-25 12:05:55

标签: python class matrix

我需要用N个单元实现一个矩阵,这些单元取决于K参数,一些固定变量和其他变量。我想通过使用类来完成所有这些工作。 有人可以向我解释基本程序吗?

1 个答案:

答案 0 :(得分:0)

我知道,我不应该回答这个问题,因为这是一项“作业”,但是我无法抗拒矩阵类!

基本上,矩阵只不过是一个列表列表。将其实现为类意味着您应该添加一些基本功能(方法),例如,获取特定行/列的值,打印矩阵等...

以下是您可以开始使用的简单示例:

class Matrix2D:
    def __init__(self):
        '''
        Init an empty matrix.
        '''
        self.matrix = []
        self.rows = 0
        self.columns = 0
        self.name = 'Unnamed'

    def __str__(self):
        return self.matrix

    def generate(self, rows, columns, verbose=False):
        '''
        Return a list of lists containing the indices of the matrix (row, col)
        and prints it by row.
        int, int -> [[(int, int), ...], ...]
        '''
        self.rows = rows
        self.columns = columns
        self.matrix = [[(row, col) for col in range(columns)] for row in range(rows)]
        if verbose ==  True:
            print(f'Generated a {self.rows} row/s by {self.columns} column/s matrix')
            print('--------' * columns)
            self.printme()
            print('--------' * columns)
        return self.matrix

    def printme(self, verbose=False):
        '''
        Print the matrix by row.
        '''
        if verbose == True:
            print(f'I am a {self.rows} row/s by {self.columns} column/s matrix')
        for row in self.matrix:
            print(row)

    def get_row(self, n, verbose=False):
        '''
        Return the row n of the matrix.
        '''
        if verbose ==  True:
            print(f'matrix[row={n}]...')
            print(self.matrix[n])
        return self.matrix[n]

    def get_col(self, n, verbose=False):
        '''
        Return the column n of the matrix.
        '''
        column_items = []
        i = 0
        while i < self.rows:
            column_items.append(self.matrix[i][n])
            i += 1
        if verbose ==  True:
            print(f'matrix[col={n}]...')
            for item in column_items:
                print(item)
        return column_items

    def get_cell(self, row, col, verbose=False):
        '''
        Return a specific cell of the matrix.
        '''
        if verbose ==  True:
            print(f'cell[row={row}, col={col}]...')
            print(self.matrix[row][col])
        return self.matrix[row][col]

    def write_cell(self, row, col, data, verbose=False):
        '''
        Assign some data to a specific cell of the matrix.
        '''
        self.matrix[row][col] = data
        if verbose ==  True:
            print('Data wrote into cell[row={row}, col={col}]...')
            print(self.matrix[row][col])                            
        return self.matrix[row][col]

测试:

print('#########')
m = Matrix2D()
m.generate(4,5,True)
m.printme()
m.get_row(1,True)
m.get_col(1,True)
m.get_cell(3,2,True)
m.write_cell(3,2, (9,9,9),True)

输出:

#########
Generated a 4 row/s by 5 column/s matrix
----------------------------------------
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]
[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]
[(3, 0), (3, 1), (3, 2), (3, 3), (3, 4)]
----------------------------------------
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]
[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]
[(3, 0), (3, 1), (3, 2), (3, 3), (3, 4)]
matrix[row=1]...
[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
matrix[col=1]...
(0, 1)
(1, 1)
(2, 1)
(3, 1)
cell[row=3, col=2]...
(3, 2)
Data wrote into cell[row={row}, col={col}]...
(9, 9, 9)

接下来,您可以添加一些额外的方法来进行更多奇特的操作,例如矩阵乘法,转置等...玩得开心!