Spaces:
Runtime error
Runtime error
feat: implement Gradio interfaces for matrix operations and utilities; remove old matrix interface
Browse files
maths/matrices/matrices_tab.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
from maths.matrices.
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
6 |
|
7 |
matrices_interfaces_list = [
|
8 |
matrix_add_interface, matrix_subtract_interface,
|
|
|
1 |
import gradio as gr
|
2 |
+
from maths.matrices.matrix_add import matrix_add_interface
|
3 |
+
from maths.matrices.matrix_subtract import matrix_subtract_interface
|
4 |
+
from maths.matrices.matrix_multiply import matrix_multiply_interface
|
5 |
+
from maths.matrices.matrix_determinant import matrix_determinant_interface
|
6 |
+
from maths.matrices.matrix_inverse import matrix_inverse_interface
|
7 |
|
8 |
matrices_interfaces_list = [
|
9 |
matrix_add_interface, matrix_subtract_interface,
|
maths/matrices/matrix_add.py
CHANGED
@@ -3,6 +3,9 @@ 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 |
|
@@ -12,3 +15,14 @@ def matrix_add(matrix1: Matrix, matrix2: Matrix) -> np.ndarray:
|
|
12 |
if m1.shape != m2.shape:
|
13 |
raise ValueError("Matrices must have the same dimensions for addition.")
|
14 |
return np.add(m1, m2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
from maths.matrices.utils import parse_matrix, format_output
|
9 |
|
10 |
Matrix = Union[List[List[float]], np.ndarray]
|
11 |
|
|
|
15 |
if m1.shape != m2.shape:
|
16 |
raise ValueError("Matrices must have the same dimensions for addition.")
|
17 |
return np.add(m1, m2)
|
18 |
+
|
19 |
+
matrix_add_interface = gr.Interface(
|
20 |
+
fn=lambda m1_str, m2_str: format_output(matrix_add(parse_matrix(m1_str), parse_matrix(m2_str))),
|
21 |
+
inputs=[
|
22 |
+
gr.Textbox(label="Matrix 1 (JSON or CSV format)", placeholder="e.g., [[1,2],[3,4]] or 1,2;3,4"),
|
23 |
+
gr.Textbox(label="Matrix 2 (JSON or CSV format)", placeholder="e.g., [[5,6],[7,8]] or 5,6;7,8")
|
24 |
+
],
|
25 |
+
outputs=gr.Textbox(label="Resulting Matrix"),
|
26 |
+
title="Matrix Addition",
|
27 |
+
description="Adds two matrices. Ensure they have the same dimensions."
|
28 |
+
)
|
maths/matrices/matrix_determinant.py
CHANGED
@@ -3,6 +3,9 @@ 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 |
|
@@ -11,3 +14,11 @@ def matrix_determinant(matrix: Matrix) -> float:
|
|
11 |
if m.shape[0] != m.shape[1]:
|
12 |
raise ValueError("Matrix must be square to compute determinant.")
|
13 |
return np.linalg.det(m)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
from maths.matrices.utils import parse_matrix, format_output
|
9 |
|
10 |
Matrix = Union[List[List[float]], np.ndarray]
|
11 |
|
|
|
14 |
if m.shape[0] != m.shape[1]:
|
15 |
raise ValueError("Matrix must be square to compute determinant.")
|
16 |
return np.linalg.det(m)
|
17 |
+
|
18 |
+
matrix_determinant_interface = gr.Interface(
|
19 |
+
fn=lambda m_str: format_output(matrix_determinant(parse_matrix(m_str))),
|
20 |
+
inputs=gr.Textbox(label="Matrix (must be square)", placeholder="e.g., [[1,2],[3,4]]"),
|
21 |
+
outputs=gr.Textbox(label="Determinant"),
|
22 |
+
title="Matrix Determinant",
|
23 |
+
description="Calculates the determinant of a square matrix."
|
24 |
+
)
|
maths/matrices/matrix_inverse.py
CHANGED
@@ -3,6 +3,9 @@ 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 |
|
@@ -11,3 +14,11 @@ def matrix_inverse(matrix: Matrix) -> np.ndarray:
|
|
11 |
if m.shape[0] != m.shape[1]:
|
12 |
raise ValueError("Matrix must be square to compute inverse.")
|
13 |
return np.linalg.inv(m)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
from maths.matrices.utils import parse_matrix, format_output
|
9 |
|
10 |
Matrix = Union[List[List[float]], np.ndarray]
|
11 |
|
|
|
14 |
if m.shape[0] != m.shape[1]:
|
15 |
raise ValueError("Matrix must be square to compute inverse.")
|
16 |
return np.linalg.inv(m)
|
17 |
+
|
18 |
+
matrix_inverse_interface = gr.Interface(
|
19 |
+
fn=lambda m_str: format_output(matrix_inverse(parse_matrix(m_str))),
|
20 |
+
inputs=gr.Textbox(label="Matrix (must be square and invertible)", placeholder="e.g., [[1,2],[3,7]]"),
|
21 |
+
outputs=gr.Textbox(label="Inverse Matrix"),
|
22 |
+
title="Matrix Inverse",
|
23 |
+
description="Calculates the inverse of a square matrix. Matrix must be invertible (non-singular)."
|
24 |
+
)
|
maths/matrices/matrix_multiply.py
CHANGED
@@ -3,6 +3,9 @@ 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 |
|
@@ -12,3 +15,14 @@ def matrix_multiply(matrix1: Matrix, matrix2: Matrix) -> np.ndarray:
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
from maths.matrices.utils import parse_matrix, format_output
|
9 |
|
10 |
Matrix = Union[List[List[float]], np.ndarray]
|
11 |
|
|
|
15 |
if m1.shape[1] != m2.shape[0]:
|
16 |
raise ValueError("Number of columns in the first matrix must equal number of rows in the second for multiplication.")
|
17 |
return np.dot(m1, m2)
|
18 |
+
|
19 |
+
matrix_multiply_interface = gr.Interface(
|
20 |
+
fn=lambda m1_str, m2_str: format_output(matrix_multiply(parse_matrix(m1_str), parse_matrix(m2_str))),
|
21 |
+
inputs=[
|
22 |
+
gr.Textbox(label="Matrix 1", placeholder="e.g., [[1,2],[3,4]]"),
|
23 |
+
gr.Textbox(label="Matrix 2", placeholder="e.g., [[5,6],[7,8]]")
|
24 |
+
],
|
25 |
+
outputs=gr.Textbox(label="Resulting Matrix"),
|
26 |
+
title="Matrix Multiplication",
|
27 |
+
description="Multiplies two matrices. Columns of Matrix 1 must equal rows of Matrix 2."
|
28 |
+
)
|
maths/matrices/matrix_subtract.py
CHANGED
@@ -3,6 +3,9 @@ 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 |
|
@@ -12,3 +15,14 @@ def matrix_subtract(matrix1: Matrix, matrix2: Matrix) -> np.ndarray:
|
|
12 |
if m1.shape != m2.shape:
|
13 |
raise ValueError("Matrices must have the same dimensions for subtraction.")
|
14 |
return np.subtract(m1, m2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
"""
|
4 |
import numpy as np
|
5 |
from typing import List, Union
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
from maths.matrices.utils import parse_matrix, format_output
|
9 |
|
10 |
Matrix = Union[List[List[float]], np.ndarray]
|
11 |
|
|
|
15 |
if m1.shape != m2.shape:
|
16 |
raise ValueError("Matrices must have the same dimensions for subtraction.")
|
17 |
return np.subtract(m1, m2)
|
18 |
+
|
19 |
+
matrix_subtract_interface = gr.Interface(
|
20 |
+
fn=lambda m1_str, m2_str: format_output(matrix_subtract(parse_matrix(m1_str), parse_matrix(m2_str))),
|
21 |
+
inputs=[
|
22 |
+
gr.Textbox(label="Matrix 1 (Minuend)", placeholder="e.g., [[5,6],[7,8]]"),
|
23 |
+
gr.Textbox(label="Matrix 2 (Subtrahend)", placeholder="e.g., [[1,2],[3,4]]")
|
24 |
+
],
|
25 |
+
outputs=gr.Textbox(label="Resulting Matrix"),
|
26 |
+
title="Matrix Subtraction",
|
27 |
+
description="Subtracts the second matrix from the first. Ensure they have the same dimensions."
|
28 |
+
)
|
maths/matrices/{matrices_interface.py → utils.py}
RENAMED
@@ -1,15 +1,10 @@
|
|
1 |
"""
|
2 |
-
|
3 |
"""
|
4 |
import gradio as gr
|
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:
|
@@ -48,52 +43,3 @@ def format_output(data: Union[np.ndarray, float, str]) -> str:
|
|
48 |
elif isinstance(data, (float, int, str)):
|
49 |
return str(data)
|
50 |
return "Output type not recognized."
|
51 |
-
|
52 |
-
matrix_add_interface = gr.Interface(
|
53 |
-
fn=lambda m1_str, m2_str: format_output(matrix_add(parse_matrix(m1_str), parse_matrix(m2_str))),
|
54 |
-
inputs=[
|
55 |
-
gr.Textbox(label="Matrix 1 (JSON or CSV format)", placeholder="e.g., [[1,2],[3,4]] or 1,2;3,4"),
|
56 |
-
gr.Textbox(label="Matrix 2 (JSON or CSV format)", placeholder="e.g., [[5,6],[7,8]] or 5,6;7,8")
|
57 |
-
],
|
58 |
-
outputs=gr.Textbox(label="Resulting Matrix"),
|
59 |
-
title="Matrix Addition",
|
60 |
-
description="Adds two matrices. Ensure they have the same dimensions."
|
61 |
-
)
|
62 |
-
|
63 |
-
matrix_subtract_interface = gr.Interface(
|
64 |
-
fn=lambda m1_str, m2_str: format_output(matrix_subtract(parse_matrix(m1_str), parse_matrix(m2_str))),
|
65 |
-
inputs=[
|
66 |
-
gr.Textbox(label="Matrix 1 (Minuend)", placeholder="e.g., [[5,6],[7,8]]"),
|
67 |
-
gr.Textbox(label="Matrix 2 (Subtrahend)", placeholder="e.g., [[1,2],[3,4]]")
|
68 |
-
],
|
69 |
-
outputs=gr.Textbox(label="Resulting Matrix"),
|
70 |
-
title="Matrix Subtraction",
|
71 |
-
description="Subtracts the second matrix from the first. Ensure they have the same dimensions."
|
72 |
-
)
|
73 |
-
|
74 |
-
matrix_multiply_interface = gr.Interface(
|
75 |
-
fn=lambda m1_str, m2_str: format_output(matrix_multiply(parse_matrix(m1_str), parse_matrix(m2_str))),
|
76 |
-
inputs=[
|
77 |
-
gr.Textbox(label="Matrix 1", placeholder="e.g., [[1,2],[3,4]]"),
|
78 |
-
gr.Textbox(label="Matrix 2", placeholder="e.g., [[5,6],[7,8]]")
|
79 |
-
],
|
80 |
-
outputs=gr.Textbox(label="Resulting Matrix"),
|
81 |
-
title="Matrix Multiplication",
|
82 |
-
description="Multiplies two matrices. Columns of Matrix 1 must equal rows of Matrix 2."
|
83 |
-
)
|
84 |
-
|
85 |
-
matrix_determinant_interface = gr.Interface(
|
86 |
-
fn=lambda m_str: format_output(matrix_determinant(parse_matrix(m_str))),
|
87 |
-
inputs=gr.Textbox(label="Matrix (must be square)", placeholder="e.g., [[1,2],[3,4]]"),
|
88 |
-
outputs=gr.Textbox(label="Determinant"),
|
89 |
-
title="Matrix Determinant",
|
90 |
-
description="Calculates the determinant of a square matrix."
|
91 |
-
)
|
92 |
-
|
93 |
-
matrix_inverse_interface = gr.Interface(
|
94 |
-
fn=lambda m_str: format_output(matrix_inverse(parse_matrix(m_str))),
|
95 |
-
inputs=gr.Textbox(label="Matrix (must be square and invertible)", placeholder="e.g., [[1,2],[3,7]]"),
|
96 |
-
outputs=gr.Textbox(label="Inverse Matrix"),
|
97 |
-
title="Matrix Inverse",
|
98 |
-
description="Calculates the inverse of a square matrix. Matrix must be invertible (non-singular)."
|
99 |
-
)
|
|
|
1 |
"""
|
2 |
+
Utility functions for matrix parsing and formatting in Gradio applications.
|
3 |
"""
|
4 |
import gradio as gr
|
5 |
import numpy as np
|
6 |
import json
|
7 |
from typing import Union
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def parse_matrix(matrix_str: str, allow_empty_rows_cols=False) -> np.ndarray:
|
10 |
try:
|
|
|
43 |
elif isinstance(data, (float, int, str)):
|
44 |
return str(data)
|
45 |
return "Output type not recognized."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|