Projects/determinate2x2.py

26 lines
748 B
Python

from typing import List
"""
Calculates the determinant of a 2x2 matrix.
Args:
llist (List[List[int]]): A 2x2 matrix represented as a list of two lists, each containing two integers.
Returns:
int: The determinant of the 2x2 matrix.
Raises:
AssertionError: If the input is not a 2x2 matrix.
Example:
>>> det2x2([[1, 2], [3, 4]])
-2
NOTE: You call the module with determinate2x2.det2x2(M) where M is the 2x2 matrix. If M is not a 2x2 matrix,
you will receive an AssertionError: "Matrix is not 2x2"
"""
def det2x2(llist: List[List[int]])->int:
assert len(llist) == 2 and len(llist[0]) == 2, "Matrix is not 2x2"
z = list(zip(llist[0], llist[1][::-1]))
det = z[0][0]*z[0][1] - z[1][0]*z[1][1]
return det