counting / maths /vectors /vector_add.py
spagestic's picture
feat: update examples in Gradio interfaces for vector operations to improve clarity
fcfbd59
"""
Add two vectors of the same dimension using NumPy.
"""
import gradio as gr
import numpy as np
from typing import List, Union
from maths.vectors.utils import parse_vector, format_output
Vector = Union[List[float], np.ndarray]
def vector_add(vector1: Vector, vector2: Vector) -> np.ndarray:
v1 = np.array(vector1)
v2 = np.array(vector2)
if v1.shape != v2.shape:
raise ValueError("Vectors must have the same dimensions for addition.")
return np.add(v1, v2)
vector_add_interface = gr.Interface(
fn=lambda v1_str, v2_str: format_output(vector_add(parse_vector(v1_str), parse_vector(v2_str))),
inputs=[
gr.Textbox(label="Vector 1 (JSON or CSV format)", placeholder="e.g., [1,2,3] or 1,2,3"),
gr.Textbox(label="Vector 2 (JSON or CSV format)", placeholder="e.g., [4,5,6] or 4,5,6")
],
outputs=gr.Textbox(label="Resulting Vector"),
title="Vector Addition",
description="Adds two vectors of the same dimension.\n\nExample:\nInput: [1,2,3] and [4,5,6]\nOutput: [5.0, 7.0, 9.0]",
examples=[["[1,2,3]", "[4,5,6]"], ["1,2,3", "4,5,6"]],
)