Program
PF Project Matrix Manipulator
Project:
In Project Matrix Manipulator You are required to create a program that provides near-complete functionality overthe matrix space. Users should be given a simple and smooth work flow thatimplements following details according to specs:
Frontend:
- User can enter matrix personally or though files of their choosing
- User can get output on console or through files of their choosing
- Program should not end until user wishes to end their task
- User can enter Matrix of any size
- In case of file data entry each file will contain single matrix and you must useauto- grow techniques to read data
Functional Requirements:
- Sum of Matrix (sum of all values)
- Product of Matrix (product of all values)
- Average or whole Matrix
- Scalar Matrix Addition
- Addition of two Matrices
- Scalar Matrix Subtraction
- Subtraction of two Matrices
- Scalar Matrix Multiplication
- Multiplication of two Matrices
- Matrix transpose
- Matrix determinant
Back end:
- Use separate function for every task listed above that is controlled by menu function which guides users through the various options
- Use separate functions for helping tasks such as file input/output
- Use structs to represent Matrices
- Handle any and all sanity checks such as “size < 1”
- In case there are any operations the program cannot perform, show a proper error message on screen to user to clearly state what is wrong with the input
- Code should be thoroughly commented with appropriate details
- There should be no memory leakage at all throughout the program
- All code must be 100% generic.
How This Code Work?
Code:
import numpy as np
class Matrix:
def __init__(self, rows, columns):
self.rows = rows
self.columns = columns
self.matrix = np.zeros((rows, columns))
def set_element(self, row, column, value):
self.matrix[row][column] = value
def get_element(self, row, column):
return self.matrix[row][column]
def display(self):
print("Matrix:")
print(self.matrix)
def save_to_file(self, file_path):
np.savetxt(file_path, self.matrix)
def transpose(self):
return np.transpose(self.matrix)
def determinant(self):
return np.linalg.det(self.matrix)
class MatrixManipulator:
def __init__(self):
self.matrices = []
def create_matrix(self, rows, columns):
matrix = Matrix(rows, columns)
self.matrices.append(matrix)
return matrix
def enter_matrix_manually(self, matrix):
print("Enter matrix elements:")
for i in range(matrix.rows):
for j in range(matrix.columns):
element = float(input(f"Enter element for row {i+1}, column {j+1}: "))
matrix.set_element(i, j, element)
def enter_matrix_from_file(self, file_path):
matrix = np.loadtxt(file_path)
rows, columns = matrix.shape
matrix_obj = self.create_matrix(rows, columns)
matrix_obj.matrix = matrix
def display_matrix(self, matrix):
matrix.display()
def save_matrix_to_file(self, matrix, file_path):
matrix.save_to_file(file_path)
def sum_of_matrix(self, matrix):
return np.sum(matrix.matrix)
def product_of_matrix(self, matrix):
return np.product(matrix.matrix)
def average_of_matrix(self, matrix):
return np.mean(matrix.matrix)
def scalar_matrix_addition(self, matrix, scalar):
result = Matrix(matrix.rows, matrix.columns)
for i in range(matrix.rows):
for j in range(matrix.columns):
result.set_element(i, j, matrix.get_element(i, j) + scalar)
return result
def addition_of_two_matrices(self, matrix1, matrix2):
result = Matrix(matrix1.rows, matrix1.columns)
for i in range(matrix1.rows):
for j in range(matrix1.columns):
result.set_element(i, j, matrix1.get_element(i, j) + matrix2.get_element(i, j))
return result
def scalar_matrix_subtraction(self, matrix, scalar):
result = Matrix(matrix.rows, matrix.columns)
for i in range(matrix.rows):
for j in range(matrix.columns):
result.set_element(i, j, matrix.get_element(i, j) - scalar)
return result
def subtraction_of_two_matrices(self, matrix1, matrix2):
result = Matrix(matrix1.rows, matrix1.columns)
for i in range(matrix1.rows):
for j in range(matrix1.columns):
result.set_element(i, j, matrix1.get_element(i, j) - matrix2.get_element(i, j))
return result
def scalar_matrix_multiplication(self, matrix, scalar):
result = Matrix(matrix.rows, matrix.columns)
for i in range(matrix.rows):
for j in range(matrix.columns):
result.set_element(i, j, matrix.get_element(i, j) * scalar)
return result
def multiplication_of_two_matrices(self, matrix1, matrix2):
result = Matrix(matrix1.rows, matrix2.columns)
for i in range(matrix1.rows):
for j in range(matrix2.columns):
element = 0
for k in range(matrix1.columns):
element += matrix1.get_element(i, k) * matrix2.get_element(k, j)
result.set_element(i, j, element)
return result
def matrix_transpose(self, matrix):
return matrix.transpose()
def matrix_determinant(self, matrix):
return matrix.determinant()
def print_menu():
print("================= Matrix Manipulator =================")
print("1. Create a matrix")
print("2. Enter matrix manually")
print("3. Enter matrix from file")
print("4. Display matrix")
print("5. Save matrix to file")
print("6. Sum of matrix")
print("7. Product of matrix")
print("8. Average of matrix")
print("9. Scalar matrix addition")
print("10. Addition of two matrices")
print("11. Scalar matrix subtraction")
print("12. Subtraction of two matrices")
print("13. Scalar matrix multiplication")
print("14. Multiplication of two matrices")
print("15. Matrix transpose")
print("16. Matrix determinant")
print("17. Exit")
print("====================================================")
def get_matrix_by_id(matrices, matrix_id):
for matrix in matrices:
if id(matrix) == matrix_id:
return matrix
return None
def main():
manipulator = MatrixManipulator()
while True:
print_menu()
choice = int(input("Enter your choice: "))
if choice == 1:
rows = int(input("Enter the number of rows: "))
columns = int(input("Enter the number of columns: "))
matrix = manipulator.create_matrix(rows, columns)
print("Matrix created.")
print("Matrix ID:", id(matrix))
elif choice == 2:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
manipulator.enter_matrix_manually(matrix)
else:
print("Invalid matrix ID.")
elif choice == 3:
file_path = input("Enter the file path: ")
manipulator.enter_matrix_from_file(file_path)
elif choice == 4:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
manipulator.display_matrix(matrix)
else:
print("Invalid matrix ID.")
elif choice == 5:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
file_path = input("Enter the file path to save the matrix: ")
manipulator.save_matrix_to_file(matrix, file_path)
print("Matrix saved to file.")
else:
print("Invalid matrix ID.")
elif choice == 6:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
print("Sum of matrix:", manipulator.sum_of_matrix(matrix))
else:
print("Invalid matrix ID.")
elif choice == 7:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
print("Product of matrix:", manipulator.product_of_matrix(matrix))
else:
print("Invalid matrix ID.")
elif choice == 8:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
print("Average of matrix:", manipulator.average_of_matrix(matrix))
else:
print("Invalid matrix ID.")
elif choice == 9:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
scalar = float(input("Enter the scalar value: "))
result = manipulator.scalar_matrix_addition(matrix, scalar)
print("Scalar matrix addition result:")
manipulator.display_matrix(result)
else:
print("Invalid matrix ID.")
elif choice == 10:
matrix1_id = int(input("Enter the first matrix ID: "))
matrix2_id = int(input("Enter the second matrix ID: "))
matrix1 = get_matrix_by_id(manipulator.matrices, matrix1_id)
matrix2 = get_matrix_by_id(manipulator.matrices, matrix2_id)
if matrix1 and matrix2:
result = manipulator.addition_of_two_matrices(matrix1, matrix2)
print("Addition of two matrices result:")
manipulator.display_matrix(result)
else:
print("Invalid matrix ID.")
elif choice == 11:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
scalar = float(input("Enter the scalar value: "))
result = manipulator.scalar_matrix_subtraction(matrix, scalar)
print("Scalar matrix subtraction result:")
manipulator.display_matrix(result)
else:
print("Invalid matrix ID.")
elif choice == 12:
matrix1_id = int(input("Enter the first matrix ID: "))
matrix2_id = int(input("Enter the second matrix ID: "))
matrix1 = get_matrix_by_id(manipulator.matrices, matrix1_id)
matrix2 = get_matrix_by_id(manipulator.matrices, matrix2_id)
if matrix1 and matrix2:
result = manipulator.subtraction_of_two_matrices(matrix1, matrix2)
print("Subtraction of two matrices result:")
manipulator.display_matrix(result)
else:
print("Invalid matrix ID.")
elif choice == 13:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
scalar = float(input("Enter the scalar value: "))
result = manipulator.scalar_matrix_multiplication(matrix, scalar)
print("Scalar matrix multiplication result:")
manipulator.display_matrix(result)
else:
print("Invalid matrix ID.")
elif choice == 14:
matrix1_id = int(input("Enter the first matrix ID: "))
matrix2_id = int(input("Enter the second matrix ID: "))
matrix1 = get_matrix_by_id(manipulator.matrices, matrix1_id)
matrix2 = get_matrix_by_id(manipulator.matrices, matrix2_id)
if matrix1 and matrix2:
result = manipulator.multiplication_of_two_matrices(matrix1, matrix2)
print("Multiplication of two matrices result:")
manipulator.display_matrix(result)
else:
print("Invalid matrix ID.")
elif choice == 15:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
result = manipulator.matrix_transpose(matrix)
print("Matrix transpose:")
manipulator.display_matrix(result)
else:
print("Invalid matrix ID.")
elif choice == 16:
matrix_id = int(input("Enter the matrix ID: "))
matrix = get_matrix_by_id(manipulator.matrices, matrix_id)
if matrix:
result = manipulator.matrix_determinant(matrix)
print("Matrix determinant:", result)
else:
print("Invalid matrix ID.")
elif choice == 17:
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Copy the Above Code And Run in Python Environment or Online on https://www.programiz.com/python-programming/online-compiler/
Explanation:
This code provides a menu-based user interface to perform various matrix operations. Users can create matrices, enter matrix elements manually or from files, display matrices, save matrices to files, and perform operations such as sum, product, average, scalar addition, matrix addition, scalar subtraction, matrix subtraction, scalar multiplication, matrix multiplication, matrix transpose, and matrix determinant.
The code is implemented using classes to represent matrices and the matrix manipulator, providing encapsulation and separation of concerns. The menu and user input handling are done in the main() function, which interacts with the matrix manipulator object.
The code is designed to be generic, allowing the user to create matrices of any size and perform operations on them. Memory management is handled by Python’s garbage collector, so there should be no memory leaks.
Please note that this code uses the NumPy library for matrix operations, as it provides efficient and convenient functions for matrix calculations. Make sure you have NumPy installed (pip install numpy) before running the code.
Feel free to modify the code as per your specific requirements and add further comments for clarity.
Top of Form Regard RkUsman
PFProjectMatrixManipulator
View Project 1 Matrix Manipulator.docx from CS 101 at Punjab University College Of Information Technology. University of Central Punjab Faculty of