Spaces:
Runtime error
Runtime error
File size: 1,227 Bytes
288b0ef ff0d3b3 288b0ef ff0d3b3 a561f92 ff0d3b3 |
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 |
"""
Add 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_add(matrix1: Matrix, matrix2: Matrix) -> np.ndarray:
m1 = np.array(matrix1)
m2 = np.array(matrix2)
if m1.shape != m2.shape:
raise ValueError("Matrices must have the same dimensions for addition.")
return np.add(m1, m2)
matrix_add_interface = gr.Interface(
fn=lambda m1_str, m2_str: format_output(matrix_add(parse_matrix(m1_str), parse_matrix(m2_str))),
inputs=[
gr.Textbox(label="Matrix 1 (JSON or CSV format)", placeholder="e.g., [[1,2],[3,4]] or 1,2;3,4"),
gr.Textbox(label="Matrix 2 (JSON or CSV format)", placeholder="e.g., [[5,6],[7,8]] or 5,6;7,8")
],
outputs=gr.Textbox(label="Resulting Matrix"),
title="Matrix Addition",
description="Adds two matrices of the same dimensions. Input can be JSON (e.g., [[1,2],[3,4]]) or CSV (e.g., 1,2;3,4). Example: Matrix 1: [[1,2],[3,4]], Matrix 2: [[5,6],[7,8]] => Result: [[6,8],[10,12]]",
examples=[
["[[1,2],[3,4]]", "[[5,6],[7,8]]"],
["1,2;3,4", "5,6;7,8"]
]
)
|