Skip to content

Commit 3761112

Browse files
feat: Added deteminant calculation
1 parent 31eb0a2 commit 3761112

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

math/Matrix-Calculator/Matrix-Calculator.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
print("🎮 Matrix Calculator 🎮")
2-
print("Easily add, subtract, multiply, or transpose matrices! \n")
2+
print("Easily add, subtract, multiply, transpose, and calculate determinant of matrices! \n")
33

44
while True:
5-
choice = input("🎯 Choose Operation: Add (A), Subtract (S), Multiply (M), Transpose (T), or Quit (Q): ").upper()
5+
choice = input("🎯 Choose Operation: Add (A), Subtract (S), Multiply (M), Transpose (T), Determinant (D), or Quit (Q): ").upper()
66

77
if choice in ["Q", "QUIT"]:
88
break
@@ -100,6 +100,34 @@
100100
except Exception:
101101
print("❌ Error: Please enter valid numbers.\n")
102102

103+
104+
elif choice == "D":
105+
try:
106+
n = int(input("➡️ Enter size of square matrix (n x n): "))
107+
108+
print(f"\n📝 Enter elements for Matrix ({n}x{n}) row by row (space separated):")
109+
matrix = []
110+
for _ in range(n):
111+
row = list(map(float, input("🔢 ").split()))
112+
matrix.append(row)
113+
114+
def determinant(m):
115+
if len(m) == 1:
116+
return m[0][0]
117+
if len(m) == 2:
118+
return m[0][0] * m[1][1] - m[0][1] * m[1][0]
119+
det = 0
120+
for c in range(len(m)):
121+
det += ((-1)**c) * m[0][c] * determinant([row[:c] + row[c+1:] for row in m[1:]])
122+
return det
123+
124+
det_value = determinant(matrix)
125+
print(f"\n📊 Determinant: {det_value}")
126+
print("✅ Calculation successful!\n")
127+
128+
except Exception:
129+
print("❌ Error: Please enter valid numbers or ensure it's a square matrix.\n")
130+
103131
else:
104132
print("⚠️ Invalid input\n")
105133

0 commit comments

Comments
 (0)