File size: 1,138 Bytes
79e637a
 
 
 
 
727d361
 
 
79e637a
 
 
 
 
 
 
 
 
727d361
 
 
 
 
 
 
 
 
c735b62
fcfbd59
727d361
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
"""
Compute the cross product of two 3-dimensional vectors using NumPy.
"""
import numpy as np
from typing import List, Union
import gradio as gr

from maths.vectors.utils import parse_vector, format_output

Vector = Union[List[float], np.ndarray]

def vector_cross_product(vector1: Vector, vector2: Vector) -> np.ndarray:
    v1 = np.array(vector1)
    v2 = np.array(vector2)
    if v1.shape != (3,) or v2.shape != (3,):
        raise ValueError("Both vectors must be 3-dimensional for cross product.")
    return np.cross(v1, v2)

vector_cross_product_interface = gr.Interface(
    fn=lambda v1_str, v2_str: format_output(vector_cross_product(parse_vector(v1_str), parse_vector(v2_str))),
    inputs=[
        gr.Textbox(label="Vector 1 (3D)", placeholder="e.g., [1,2,3]"),
        gr.Textbox(label="Vector 2 (3D)", placeholder="e.g., [4,5,6]")
    ],
    outputs=gr.Textbox(label="Resulting Vector (3D)"),
    title="Vector Cross Product",
    description="Calculates the cross product of two 3D vectors.\n\nExample:\nInput: [1,2,3] and [4,5,6]\nOutput: [-3.0, 6.0, -3.0]",
    examples=[["[1,2,3]", "[4,5,6]"], ["1,2,3", "4,5,6"]],
)