Getters and Setters

Definition:

get_entry(i: int, j:int) -> int|float
get_row(i: int) -> list[int|float]
get_column(j:int) -> list[int|float]
set_entry(value: int|float, i: int, j:int) -> None

For a given matrix \(A = \begin{pmatrix} a_{1,1} & a_{1,2} & \cdots & a_{1,p} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,p} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n,1} & a_{n,2} & \cdots & a_{n,p} \end{pmatrix}\), we access the entry \(a_{i,j}\) using the get_entry(i, j) method. The indexation of the entries is according to the mathematical convention, ie starts at 1.

from basic_deep_learning import*

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

print(A.get_entry(1,3))
3

We can also retrieve a row or column as a list:

print(A.get_row(2))
print(A.get_column(1))
[4, 5, 6]
[1, 4]

To modify a certain entry from the matrix, we can use the set_entry(value, i, j) method:

A.set_entry(-1, 2, 2)   #Set the a_{2,2} entry to -1
print(A)
matrix([
        [1, 2, 3],
        [4, -1, 6]
])

In all of those methods, if a wrong index is passed, a IndexError will be raised.

from basic_deep_learning import*

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

print(A.get_entry(3,2))
IndexError: Index 3 is out of the expected range [1,2].