matrix.matrix_operation

Functions for 2D matrix operations

Functions

_check_not_integer(→ bool)

_shape(→ tuple[int, int])

_verify_matrix_sizes(→ tuple[tuple[int, int], ...)

add(→ list[list[int]])

determinant(→ Any)

identity(→ list[list[int]])

inverse(→ list[list[float]] | None)

main(→ None)

minor(→ list[list[int]])

multiply(→ list[list[int]])

scalar_multiply(→ list[list[float]])

subtract(→ list[list[int]])

transpose(→ list[list[int]] | map[list[int]])

Module Contents

matrix.matrix_operation._check_not_integer(matrix: list[list[int]]) bool
matrix.matrix_operation._shape(matrix: list[list[int]]) tuple[int, int]
matrix.matrix_operation._verify_matrix_sizes(matrix_a: list[list[int]], matrix_b: list[list[int]]) tuple[tuple[int, int], tuple[int, int]]
matrix.matrix_operation.add(*matrix_s: list[list[int]]) list[list[int]]
>>> add([[1,2],[3,4]],[[2,3],[4,5]])
[[3, 5], [7, 9]]
>>> add([[1.2,2.4],[3,4]],[[2,3],[4,5]])
[[3.2, 5.4], [7, 9]]
>>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]])
[[7, 14], [12, 16]]
>>> add([3], [4, 5])
Traceback (most recent call last):
  ...
TypeError: Expected a matrix, got int/list instead
matrix.matrix_operation.determinant(matrix: list[list[int]]) Any
>>> determinant([[1, 2], [3, 4]])
-2
>>> determinant([[1.5, 2.5], [3, 4]])
-1.5
matrix.matrix_operation.identity(n: int) list[list[int]]
Parameters:

n (int) – dimension for nxn matrix

Returns:

Identity matrix of shape [n, n]

>>> identity(3)
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
matrix.matrix_operation.inverse(matrix: list[list[int]]) list[list[float]] | None
>>> inverse([[1, 2], [3, 4]])
[[-2.0, 1.0], [1.5, -0.5]]
>>> inverse([[1, 1], [1, 1]])
matrix.matrix_operation.main() None
matrix.matrix_operation.minor(matrix: list[list[int]], row: int, column: int) list[list[int]]
>>> minor([[1, 2], [3, 4]], 1, 1)
[[1]]
matrix.matrix_operation.multiply(matrix_a: list[list[int]], matrix_b: list[list[int]]) list[list[int]]
>>> multiply([[1,2],[3,4]],[[5,5],[7,5]])
[[19, 15], [43, 35]]
>>> multiply([[1,2.5],[3,4.5]],[[5,5],[7,5]])
[[22.5, 17.5], [46.5, 37.5]]
>>> multiply([[1, 2, 3]], [[2], [3], [4]])
[[20]]
matrix.matrix_operation.scalar_multiply(matrix: list[list[int]], n: float) list[list[float]]
>>> scalar_multiply([[1,2],[3,4]],5)
[[5, 10], [15, 20]]
>>> scalar_multiply([[1.4,2.3],[3,4]],5)
[[7.0, 11.5], [15, 20]]
matrix.matrix_operation.subtract(matrix_a: list[list[int]], matrix_b: list[list[int]]) list[list[int]]
>>> subtract([[1,2],[3,4]],[[2,3],[4,5]])
[[-1, -1], [-1, -1]]
>>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]])
[[-1, -0.5], [-1, -1.5]]
>>> subtract([3], [4, 5])
Traceback (most recent call last):
  ...
TypeError: Expected a matrix, got int/list instead
matrix.matrix_operation.transpose(matrix: list[list[int]], return_map: bool = True) list[list[int]] | map[list[int]]
>>> transpose([[1,2],[3,4]]) 
<map object at ...
>>> transpose([[1,2],[3,4]], return_map=False)
[[1, 3], [2, 4]]
>>> transpose([1, [2, 3]])
Traceback (most recent call last):
  ...
TypeError: Expected a matrix, got int/list instead