Spaces:
Runtime error
Runtime error
""" | |
Multiply two matrices using NumPy. | |
""" | |
import numpy as np | |
from typing import List, Union | |
import gradio as gr | |
from maths.matrices.utils import parse_matrix, format_output | |
Matrix = Union[List[List[float]], np.ndarray] | |
def matrix_multiply(matrix1: Matrix, matrix2: Matrix) -> np.ndarray: | |
m1 = np.array(matrix1) | |
m2 = np.array(matrix2) | |
if m1.shape[1] != m2.shape[0]: | |
raise ValueError("Number of columns in the first matrix must equal number of rows in the second for multiplication.") | |
return np.dot(m1, m2) | |
matrix_multiply_interface = gr.Interface( | |
fn=lambda m1_str, m2_str: format_output(matrix_multiply(parse_matrix(m1_str), parse_matrix(m2_str))), | |
inputs=[ | |
gr.Textbox(label="Matrix 1", placeholder="e.g., [[1,2],[3,4]]"), | |
gr.Textbox(label="Matrix 2", placeholder="e.g., [[5,6],[7,8]]") | |
], | |
outputs=gr.Textbox(label="Resulting Matrix"), | |
title="Matrix Multiplication", | |
description="Multiplies two matrices. The number of columns in Matrix 1 must equal the number of rows in Matrix 2. Example: Matrix 1: [[1,2],[3,4]], Matrix 2: [[5,6],[7,8]] => Result: [[19,22],[43,50]]", | |
examples=[ | |
["[[1,2],[3,4]]", "[[5,6],[7,8]]"], | |
["1,2;3,4", "5,6;7,8"] | |
] | |
) | |