Spaces:
Runtime error
Runtime error
modularized
Browse files
maths/matrices/matrices.py
CHANGED
@@ -38,3 +38,5 @@ def matrix_inverse(matrix: Matrix) -> np.ndarray:
|
|
38 |
if m.shape[0] != m.shape[1]:
|
39 |
raise ValueError("Matrix must be square to compute inverse.")
|
40 |
return np.linalg.inv(m)
|
|
|
|
|
|
38 |
if m.shape[0] != m.shape[1]:
|
39 |
raise ValueError("Matrix must be square to compute inverse.")
|
40 |
return np.linalg.inv(m)
|
41 |
+
|
42 |
+
# Functions moved to separate files: matrix_add.py, matrix_subtract.py, matrix_multiply.py, matrix_determinant.py, matrix_inverse.py
|
maths/matrices/matrices_interface.py
CHANGED
@@ -5,10 +5,11 @@ import gradio as gr
|
|
5 |
import numpy as np
|
6 |
import json
|
7 |
from typing import Union
|
8 |
-
from maths.matrices.
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
def parse_matrix(matrix_str: str, allow_empty_rows_cols=False) -> np.ndarray:
|
14 |
try:
|
|
|
5 |
import numpy as np
|
6 |
import json
|
7 |
from typing import Union
|
8 |
+
from maths.matrices.matrix_add import matrix_add
|
9 |
+
from maths.matrices.matrix_subtract import matrix_subtract
|
10 |
+
from maths.matrices.matrix_multiply import matrix_multiply
|
11 |
+
from maths.matrices.matrix_determinant import matrix_determinant
|
12 |
+
from maths.matrices.matrix_inverse import matrix_inverse
|
13 |
|
14 |
def parse_matrix(matrix_str: str, allow_empty_rows_cols=False) -> np.ndarray:
|
15 |
try:
|
maths/matrices/matrix_add.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Add two matrices using NumPy.
|
3 |
+
"""
|
4 |
+
import numpy as np
|
5 |
+
from typing import List, Union
|
6 |
+
|
7 |
+
Matrix = Union[List[List[float]], np.ndarray]
|
8 |
+
|
9 |
+
def matrix_add(matrix1: Matrix, matrix2: Matrix) -> np.ndarray:
|
10 |
+
m1 = np.array(matrix1)
|
11 |
+
m2 = np.array(matrix2)
|
12 |
+
if m1.shape != m2.shape:
|
13 |
+
raise ValueError("Matrices must have the same dimensions for addition.")
|
14 |
+
return np.add(m1, m2)
|
maths/matrices/matrix_determinant.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Compute the determinant of a matrix using NumPy.
|
3 |
+
"""
|
4 |
+
import numpy as np
|
5 |
+
from typing import List, Union
|
6 |
+
|
7 |
+
Matrix = Union[List[List[float]], np.ndarray]
|
8 |
+
|
9 |
+
def matrix_determinant(matrix: Matrix) -> float:
|
10 |
+
m = np.array(matrix)
|
11 |
+
if m.shape[0] != m.shape[1]:
|
12 |
+
raise ValueError("Matrix must be square to compute determinant.")
|
13 |
+
return np.linalg.det(m)
|
maths/matrices/matrix_inverse.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Compute the inverse of a matrix using NumPy.
|
3 |
+
"""
|
4 |
+
import numpy as np
|
5 |
+
from typing import List, Union
|
6 |
+
|
7 |
+
Matrix = Union[List[List[float]], np.ndarray]
|
8 |
+
|
9 |
+
def matrix_inverse(matrix: Matrix) -> np.ndarray:
|
10 |
+
m = np.array(matrix)
|
11 |
+
if m.shape[0] != m.shape[1]:
|
12 |
+
raise ValueError("Matrix must be square to compute inverse.")
|
13 |
+
return np.linalg.inv(m)
|
maths/matrices/matrix_multiply.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Multiply two matrices using NumPy.
|
3 |
+
"""
|
4 |
+
import numpy as np
|
5 |
+
from typing import List, Union
|
6 |
+
|
7 |
+
Matrix = Union[List[List[float]], np.ndarray]
|
8 |
+
|
9 |
+
def matrix_multiply(matrix1: Matrix, matrix2: Matrix) -> np.ndarray:
|
10 |
+
m1 = np.array(matrix1)
|
11 |
+
m2 = np.array(matrix2)
|
12 |
+
if m1.shape[1] != m2.shape[0]:
|
13 |
+
raise ValueError("Number of columns in the first matrix must equal number of rows in the second for multiplication.")
|
14 |
+
return np.dot(m1, m2)
|
maths/matrices/matrix_subtract.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Subtract two matrices using NumPy.
|
3 |
+
"""
|
4 |
+
import numpy as np
|
5 |
+
from typing import List, Union
|
6 |
+
|
7 |
+
Matrix = Union[List[List[float]], np.ndarray]
|
8 |
+
|
9 |
+
def matrix_subtract(matrix1: Matrix, matrix2: Matrix) -> np.ndarray:
|
10 |
+
m1 = np.array(matrix1)
|
11 |
+
m2 = np.array(matrix2)
|
12 |
+
if m1.shape != m2.shape:
|
13 |
+
raise ValueError("Matrices must have the same dimensions for subtraction.")
|
14 |
+
return np.subtract(m1, m2)
|