From 67993267128fb2c6d2bdf9ec92100dc904da5e24 Mon Sep 17 00:00:00 2001 From: Donald Calloway Date: Fri, 3 Oct 2025 14:13:05 -0700 Subject: [PATCH] Adding modules to compute determinates of 2z2 and 3x3 arrays --- determinate2x2.py | 8 ++++++++ determinate3x3.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 determinate2x2.py create mode 100644 determinate3x3.py diff --git a/determinate2x2.py b/determinate2x2.py new file mode 100644 index 0000000..a5fa58d --- /dev/null +++ b/determinate2x2.py @@ -0,0 +1,8 @@ +from typing import List + +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 + diff --git a/determinate3x3.py b/determinate3x3.py new file mode 100644 index 0000000..87d0bda --- /dev/null +++ b/determinate3x3.py @@ -0,0 +1,37 @@ +from typing import List +import determinate2x2 +import copy + + +def neg_odd_vals(lst: List[int])-> List[int]: + return [-val if i % 2 == 1 else val for i, val in enumerate(lst)] + +def det3x3(M: List[List[int]])->int: + assert len(M) == 3 and len(M[0]) == 3, "Matrix is not 3x3" + # Matrix M with row 0 and col 0 removed + N = copy.deepcopy(M) + N[1].remove(N[1][0]) + N[2].remove(N[2][0]) + A = [N[1], N[2]] + + #Matrix M with row 0 and col 1 removed + N = copy.deepcopy(M) + N[1].remove(N[1][1]) + N[2].remove(N[2][1]) + B = [N[1], N[2]] + + #Matrix M with row 0 and col 2 removed + N = copy.deepcopy(M) + N[1].remove(N[1][2]) + N[2].remove(N[2][2]) + C = [N[1], N[2]] + + # List M[0] with every odd index value set to its negative + N0 = neg_odd_vals(N[0]) + + # Sum of determinates A, B, C times their respective multipliers + det0 = N0[0]*determinate2x2.det2x2(A) + det1 = N0[1]*determinate2x2.det2x2(B) + det2 = N0[2]*determinate2x2.det2x2(C) + return det0 + det1 + det2 +