-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (30 loc) · 1.12 KB
/
utils.py
File metadata and controls
35 lines (30 loc) · 1.12 KB
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
"""
Helper functions for input handling and matrix validation.
"""
import numpy as np
def input_matrix(name):
"""Take matrix input from the user"""
rows = int(input(f"Enter number of rows for {name}: "))
cols = int(input(f"Enter number of columns for {name}: "))
print(f"Enter the elements of {name} row by row, separated by space:")
matrix = []
for i in range(rows):
row = list(map(float, input(f"Row {i+1}: ").split()))
if len(row) != cols:
print("Incorrect number of elements. Please enter the row again.")
return input_matrix(name)
matrix.append(row)
return np.array(matrix)
def display_matrix(matrix, name="Result"):
"""Display the matrix in a structured format"""
print(f"\n{name}:")
print(matrix)
print("-" * 30)
def validate_square(matrix):
"""Check if the matrix is square"""
if matrix.shape[0] != matrix.shape[1]:
raise ValueError("Matrix must be square.")
def validate_shape(A, B):
"""Check if two matrices have the same shape"""
if A.shape != B.shape:
raise ValueError("Matrices must have the same shape.")