Breno

A construção de matrizes

Quando se tem matrizes, a implementação pode ser interessante quando se considera o próximo passo da conceituação. Pode ser considerado como um conjunto de vetores. Mas gosto da definição da matriz como algo que transforma o vetor. Consideremos o código.

class Matrix:
    def __init__(self, rows):
        self.rows = [list(row) for row in rows]
        self.shape = (len(self.rows), len(self.rows[0]))

    def __matmul__(self, other):
        if isinstance(other, Vector):
            return Vector([
                sum(self.rows[i][j] * other.components[j] for j in range(self.shape[1]))
                for i in range(self.shape[0])
            ])
        rows = []
        for i in range(self.shape[0]):
            row = []
            for j in range(other.shape[1]):
                row.append(sum(
                    self.rows[i][k] * other.rows[k][j]
                    for k in range(self.shape[1])
                ))
            rows.append(row)
        return Matrix(rows)

    def transpose(self):
        return Matrix([
            [self.rows[j][i] for j in range(self.shape[0])]
            for i in range(self.shape[1])
        ])

    def __repr__(self):
        return f"Matrix({self.rows})"

So, ponto que pensei foi na produção. A criação em si se dá não buscando just to copy, mas por buscar algo original depois do recurso.

Really great learning.