Initialisation

Definition:

class Matrix(matrix: list[list[int|float]]) -> Matrix

We pass to the Matrix class constructor a list of rows (as lists) of a given matrix. For example, the matrix \(M = \begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}\) will be implemented as such:

from basic_deep_learning import*

M = Matrix([[1, 0], [0, 1]])

Use print() to display the matrix.

from basic_deep_learning import*

M = Matrix([[1, 0], [0, 1]])

print(M)
matrix([
        [1, 0],
        [0, 1]
])

Each Matrix instance has two attributes; format and matrix. The format is a tuple of two elements, the first being the number of rows and the latter being the number of columns.

from basic_deep_learning import*

A = Matrix(
    [
        [1, 2, 3],
        [4, 5, 6]
    ]
)

print(A.matrix)
print(A.format)
[[1, 2, 3], [4, 5, 6]]
(2, 3)

The Matrix constructor accepts “uneven” matrix formats and automatically fills the missing entries with zeros:

from basic_deep_learning import*

A = Matrix(
    [
        [1, 2, 3],
        [4]
    ]
)

print(A)
print(A.matrix)
print(A.format)
matrix([
        [1, 2, 3],
        [4, 0, 0]
])
[[1, 2, 3], [4, 0, 0]]
(2, 3)