counting / maths /geometry /trig_identities.py
spagestic's picture
feat: enhance Gradio interfaces for trigonometric functions with detailed descriptions and examples
d29336a
import math
import gradio as gr
def trig_identities(angle_degrees: float, identity_name: str = "pythagorean1") -> str:
"""
Demonstrates common trigonometric identities for a given angle (in degrees).
Args:
angle_degrees: The angle in degrees to evaluate the identities for.
identity_name: Name of the identity to demonstrate.
"pythagorean1": sin^2(x) + cos^2(x) = 1
"pythagorean2": 1 + tan^2(x) = sec^2(x)
"pythagorean3": 1 + cot^2(x) = csc^2(x)
"all": Show all Pythagorean identities.
More can be added.
Returns:
A string demonstrating the identity.
"""
x_rad = math.radians(angle_degrees)
sinx = math.sin(x_rad)
cosx = math.cos(x_rad)
if abs(cosx) < 1e-9:
tanx = float('inf') if sinx > 0 else float('-inf') if sinx < 0 else 0
secx = float('inf') if cosx >= 0 else float('-inf')
else:
tanx = sinx / cosx
secx = 1 / cosx
if abs(sinx) < 1e-9:
cotx = float('inf') if cosx > 0 else float('-inf') if cosx < 0 else 0
cscx = float('inf') if sinx >= 0 else float('-inf')
else:
cotx = cosx / sinx
cscx = 1 / sinx
results = []
name = identity_name.lower()
if name == "pythagorean1" or name == "all":
lhs = sinx**2 + cosx**2
results.append(f"Pythagorean Identity 1: sin^2({angle_degrees}) + cos^2({angle_degrees}) = {sinx**2:.4f} + {cosx**2:.4f} = {lhs:.4f} (Expected: 1)")
if name == "pythagorean2" or name == "all":
if abs(cosx) < 1e-9:
results.append(f"Pythagorean Identity 2 (1 + tan^2(x) = sec^2(x)): Not well-defined for x={angle_degrees} degrees as cos(x) is near zero (tan(x) and sec(x) are undefined or infinite).")
else:
lhs = 1 + tanx**2
rhs = secx**2
results.append(f"Pythagorean Identity 2: 1 + tan^2({angle_degrees}) = 1 + {tanx**2:.4f} = {lhs:.4f}. sec^2({angle_degrees}) = {secx**2:.4f}. (Expected LHS = RHS)")
if name == "pythagorean3" or name == "all":
if abs(sinx) < 1e-9:
results.append(f"Pythagorean Identity 3 (1 + cot^2(x) = csc^2(x)): Not well-defined for x={angle_degrees} degrees as sin(x) is near zero (cot(x) and csc(x) are undefined or infinite).")
else:
lhs = 1 + cotx**2
rhs = cscx**2
results.append(f"Pythagorean Identity 3: 1 + cot^2({angle_degrees}) = 1 + {cotx**2:.4f} = {lhs:.4f}. csc^2({angle_degrees}) = {cscx**2:.4f}. (Expected LHS = RHS)")
if not results:
return f"Unknown identity: {identity_name}. Available: pythagorean1, pythagorean2, pythagorean3, all."
return "\n".join(results)
trig_identities_interface = gr.Interface(
fn=trig_identities,
inputs=[
gr.Number(label="Angle (degrees)"),
gr.Radio(
choices=[
("sin²(x) + cos²(x) = 1", "pythagorean1"),
("1 + tan²(x) = sec²(x)", "pythagorean2"),
("1 + cot²(x) = csc²(x)", "pythagorean3"),
("All Pythagorean", "all")
],
label="Trigonometric Identity to Demonstrate"
)
],
outputs="text",
title="Trigonometric Identities Demonstrator",
description="Show common trigonometric identities for a given angle. Select an identity and enter an angle (e.g., 45) to see the identity demonstrated with calculated values.",
examples=[
[45, "pythagorean1"],
[60, "pythagorean2"],
[30, "pythagorean3"],
[90, "all"]
]
)