Recommended Posts
- Get link
- X
- Other Apps
This example demonstrates basic linear algebra operations – vector addition, scalar multiplication, dot product, and matrix multiplication – using NumPy and a simple graphical user interface (GUI) built with Tkinter. The GUI allows users to input vector/matrix elements and perform these operations.
linear_algebra_gui.py
import tkinter as tk
from tkinter import messagebox
import numpy as np
def perform_vector_addition():
try:
vector1_str = vector1_entry.get()
vector2_str = vector2_entry.get()
vector1 = np.array([float(x) for x in vector1_str.split(',')])
vector2 = np.array([float(x) for x in vector2_str.split(',')])
if vector1.shape != vector2.shape:
raise ValueError("Vectors must have the same dimensions.")
result = vector1 + vector2
result_label.config(text="Vector Addition Result: " + str(result))
except ValueError as e:
messagebox.showerror("Error", str(e))
def perform_scalar_multiplication():
try:
vector_str = vector_entry.get()
scalar_str = scalar_entry.get()
vector = np.array([float(x) for x in vector_str.split(',')])
scalar = float(scalar_str)
result = scalar * vector
result_label.config(text="Scalar Multiplication Result: " + str(result))
except ValueError as e:
messagebox.showerror("Error", str(e))
def perform_dot_product():
try:
vector1_str = vector1_entry.get()
vector2_str = vector2_entry.get()
vector1 = np.array([float(x) for x in vector1_str.split(',')])
vector2 = np.array([float(x) for x in vector2_str.split(',')])
if vector1.shape != vector2.shape:
raise ValueError("Vectors must have the same dimensions.")
result = np.dot(vector1, vector2)
result_label.config(text="Dot Product Result: " + str(result))
except ValueError as e:
messagebox.showerror("Error", str(e))
def perform_matrix_multiplication():
try:
matrix1_str = matrix1_entry.get()
matrix2_str = matrix2_entry.get()
matrix1 = np.array([[float(x) for x in row.split(',')] for row in matrix1_str.split(';')])
matrix2 = np.array([[float(x) for x in row.split(',')] for row in matrix2_str.split(';')])
if matrix1.shape[1] != matrix2.shape[0]:
raise ValueError("Matrix dimensions are incompatible for multiplication.")
result = np.dot(matrix1, matrix2)
result_label.config(text="Matrix Multiplication Result:\n" + np.array_str(result))
except ValueError as e:
messagebox.showerror("Error", str(e))
root = tk.Tk()
root.title("Linear Algebra Operations")
vector1_label = tk.Label(root, text="Vector 1 (e.g., 1,2,3):")
vector1_label.pack()
vector1_entry = tk.Entry(root)
vector1_entry.pack()
vector2_label = tk.Label(root, text="Vector 2 (e.g., 4,5,6):")
vector2_label.pack()
vector2_entry = tk.Entry(root)
vector2_entry.pack()
vector_label = tk.Label(root, text="Vector (e.g., 1,2,3):")
vector_label.pack()
vector_entry = tk.Entry(root)
vector_entry.pack()
scalar_label = tk.Label(root, text="Scalar (e.g., 2):")
scalar_label.pack()
scalar_entry = tk.Entry(root)
scalar_entry.pack()
matrix1_label = tk.Label(root, text="Matrix 1 (e.g., 1,2;3,4):")
matrix1_label.pack()
matrix1_entry = tk.Entry(root)
matrix1_entry.pack()
matrix2_label = tk.Label(root, text="Matrix 2 (e.g., 5,6;7,8):")
matrix2_label.pack()
matrix2_entry = tk.Entry(root)
matrix2_entry.pack()
vector_addition_button = tk.Button(root, text="Vector Addition", command=perform_vector_addition)
vector_addition_button.pack(pady=5)
scalar_multiplication_button = tk.Button(root, text="Scalar Multiplication", command=perform_scalar_multiplication)
scalar_multiplication_button.pack(pady=5)
dot_product_button = tk.Button(root, text="Dot Product", command=perform_dot_product)
dot_product_button.pack(pady=5)
matrix_multiplication_button = tk.Button(root, text="Matrix Multiplication", command=perform_matrix_multiplication)
matrix_multiplication_button.pack(pady=5)
result_label = tk.Label(root, text="")
result_label.pack()
root.mainloop()
-
How to Run the Code:
- Install Python: Ensure you have Python installed on your system (version 3.6 or higher is recommended).
- Install Libraries: Open a terminal or command prompt and install the necessary libraries using pip:
python linear_algebra_gui.py
-
- Save the Code: Save the code as a Python file (e.g., linear_algebra_gui.py).
- Run the Script: Execute the script from your terminal:
python linear_algebra_gui.py
-A window will appear with input fields for vectors and matrices, along with buttons to perform the linear algebra operations. Enter the values in the specified format (e.g., "1,2,3" for a vector, "1,2;3,4" for a matrix) and click the corresponding button to see the result displayed below. Error messages will appear in a pop-up window if the input is invalid or the operation is not possible.
Comments
Post a Comment