counting / maths /matrices /matrix_determinant.py
spagestic's picture
feat: enhance Gradio interfaces for matrix operations with detailed descriptions and examples
a561f92
raw
history blame contribute delete
984 Bytes
"""
Compute the determinant of a matrix 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_determinant(matrix: Matrix) -> float:
m = np.array(matrix)
if m.shape[0] != m.shape[1]:
raise ValueError("Matrix must be square to compute determinant.")
return np.linalg.det(m)
matrix_determinant_interface = gr.Interface(
fn=lambda m_str: format_output(matrix_determinant(parse_matrix(m_str))),
inputs=gr.Textbox(label="Matrix (must be square)", placeholder="e.g., [[1,2],[3,4]]"),
outputs=gr.Textbox(label="Determinant"),
title="Matrix Determinant",
description="Calculates the determinant of a square matrix. Input can be JSON (e.g., [[1,2],[3,4]]) or CSV (e.g., 1,2;3,4). Example: Matrix: [[1,2],[3,4]] => Result: -2.0",
examples=[
["[[1,2],[3,4]]"],
["1,2;3,4"]
]
)