-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeterminantCalculator.py
160 lines (132 loc) · 4.71 KB
/
DeterminantCalculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import tkinter as tk
from tkinter import messagebox
import sympy as sp
# Function to calculate determinant
def calculate_determinant():
try:
# Read matrix dimensions
rows = int(row_entry.get())
cols = int(col_entry.get())
if rows != cols:
messagebox.showerror("Error", "Matrix must be square!")
return
# Build the matrix from user input
matrix = []
for i in range(rows):
row = []
for j in range(cols):
value = entries[i][j].get()
row.append(sp.Rational(value)) # Parse input as fraction
matrix.append(row)
# Convert to sympy Matrix and calculate determinant
sympy_matrix = sp.Matrix(matrix)
determinant = sympy_matrix.det()
# Display the result
result_label.config(text=f"Determinant = {determinant}")
except Exception as e:
messagebox.showerror("Error", f"Invalid input: {e}")
# Function to create matrix inputs
def create_matrix_inputs():
# Clear any previous inputs
for widget in matrix_frame.winfo_children():
widget.destroy()
try:
rows = int(row_entry.get())
cols = int(col_entry.get())
# Ensure the matrix is square and size is reasonable
if rows > 10 or cols > 10:
messagebox.showerror("Error", "Maximum matrix size is 10x10!")
return
if rows != cols:
messagebox.showerror("Error", "Matrix must be square!")
return
global entries
entries = []
for i in range(rows):
row_entries = []
for j in range(cols):
entry = tk.Entry(matrix_frame, width=8, font=("Arial", 16), justify="center")
entry.grid(row=i, column=j, padx=10, pady=10)
# Bind arrow keys for navigation
entry.bind("<Up>", lambda e, x=i, y=j: navigate_to_cell(x - 1, y))
entry.bind("<Down>", lambda e, x=i, y=j: navigate_to_cell(x + 1, y))
entry.bind("<Left>", lambda e, x=i, y=j: navigate_to_cell(x, y - 1))
entry.bind("<Right>", lambda e, x=i, y=j: navigate_to_cell(x, y + 1))
row_entries.append(entry)
entries.append(row_entries)
except ValueError:
messagebox.showerror("Error", "Please enter valid numbers for rows and columns.")
# Function to navigate between cells
def navigate_to_cell(row, col):
if 0 <= row < len(entries) and 0 <= col < len(entries[0]):
target_cell = entries[row][col]
target_cell.focus_set()
# Main window
root = tk.Tk()
root.title("Determinant Calculator")
# Custom styles
root.configure(bg="#2c3e50") # Dark gray-blue background
root.geometry("900x700") # Expanded window size
# Top frame for welcome message
top_frame = tk.Frame(root, bg="#1abc9c", height=80) # Teal green background
top_frame.pack(fill="x")
welcome_label = tk.Label(
top_frame,
text="Welcome To Determinant Calculator!",
bg="#1abc9c",
fg="#000000",
font=("Arial", 24, "bold"),
)
welcome_label.pack(pady=10)
subtitle_label = tk.Label(
top_frame,
text="Using Numpy",
bg="#1abc9c",
fg="black",
font=("Arial", 20, "italic"),
)
subtitle_label.pack(pady=(0, 10))
# Input frame for matrix size
size_frame = tk.Frame(root, bg="#2c3e50")
size_frame.pack(pady=20)
tk.Label(size_frame, text="ROWS", bg="#2c3e50", fg="white", font=("Arial", 16, "bold")).grid(row=0, column=0, padx=10)
row_entry = tk.Entry(size_frame, width=8, font=("Arial", 16), justify="center")
row_entry.grid(row=0, column=1, padx=10)
tk.Label(size_frame, text="COLUMNS:", bg="#2c3e50", fg="white", font=("Arial", 16, "bold")).grid(row=0, column=2, padx=10)
col_entry = tk.Entry(size_frame, width=8, font=("Arial", 16), justify="center")
col_entry.grid(row=0, column=3, padx=10)
generate_button = tk.Button(
size_frame,
text="Generate Matrix",
command=create_matrix_inputs,
bg="#16a085", # Teal green
fg="#000000",
font=("Arial", 16, "bold"),
width=15,
height=2,
relief="raised",
bd=3,
)
generate_button.grid(row=0, column=4, padx=20)
# Frame for matrix input
matrix_frame = tk.Frame(root, bg="#34495e") # Slightly lighter gray-blue
matrix_frame.pack(pady=20)
# Button to calculate determinant
calculate_button = tk.Button(
root,
text="Calculate Determinant",
command=calculate_determinant,
bg="#d3d3d3",
fg="red",
font=("Arial", 16, "bold"),
width=20,
height=2,
relief="raised",
bd=3,
)
calculate_button.pack(pady=20)
# Label to display the result
result_label = tk.Label(root, text="Determinant: ", bg="#2c3e50", fg="white", font=("Arial", 18, "bold"))
result_label.pack(pady=20)
# Run the application
root.mainloop()