Skip to content

Commit 9b3eee6

Browse files
committed
Add first fundamental form calculator to maths
This adds a script to calculate the coefficients (e, f, g) of the first fundamental form for parametric surfaces. This mathematical framework is a core concept in differential geometry, used to measure lengths, angles, and areas on a surface. The implementation uses sympy for symbolic differentiation and simplification. While standard mathematical notation dictates capital E, F, and G for these coefficients, they are implemented as lowercase to strictly adhere to PEP 8 N806 and pass the repository's required Ruff linter pre-commit hooks. Reference: https://en.wikipedia.org/wiki/First_fundamental_form Author: singhc7
1 parent 791deb4 commit 9b3eee6

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

maths/first_fundamental_form.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Calculates the First Fundamental Form of a parametric surface.
3+
4+
The first fundamental form allows the measurement of lengths, angles,
5+
and areas on a surface defined by parametric equations r(u, v).
6+
7+
Reference:
8+
- https://en.wikipedia.org/wiki/First_fundamental_form
9+
10+
Author: Chahat Sandhu
11+
GitHub: https://github.com/singhc7
12+
"""
13+
14+
import sympy as sp
15+
16+
17+
def first_fundamental_form(
18+
x_expr: str, y_expr: str, z_expr: str
19+
) -> tuple[sp.Expr, sp.Expr, sp.Expr]:
20+
"""
21+
Calculate the First Fundamental Form coefficients (E, F, G) for a surface
22+
defined by parametric equations x(u,v), y(u,v), z(u,v).
23+
24+
Args:
25+
x_expr: A string representing the x component in terms of u and v.
26+
y_expr: A string representing the y component in terms of u and v.
27+
z_expr: A string representing the z component in terms of u and v.
28+
29+
Returns:
30+
A tuple containing the sympy expressions for E, F, and G.
31+
32+
Examples:
33+
>>> # Example 1: A simple plane r(u, v) = <u, v, 0>
34+
>>> E, F, G = first_fundamental_form("u", "v", "0")
35+
>>> print(f"E: {E}, F: {F}, G: {G}")
36+
E: 1, F: 0, G: 1
37+
38+
>>> # Example 2: A paraboloid r(u, v) = <u, v, u**2 + v**2>
39+
>>> E, F, G = first_fundamental_form("u", "v", "u**2 + v**2")
40+
>>> print(f"E: {E}, F: {F}, G: {G}")
41+
E: 4*u**2 + 1, F: 4*u*v, G: 4*v**2 + 1
42+
43+
>>> # Example 3: A cylinder r(u, v) = <cos(u), sin(u), v>
44+
>>> E, F, G = first_fundamental_form("cos(u)", "sin(u)", "v")
45+
>>> print(f"E: {sp.simplify(E)}, F: {F}, G: {G}")
46+
E: 1, F: 0, G: 1
47+
"""
48+
# Define the mathematical symbols
49+
u, v = sp.symbols("u v")
50+
51+
# Parse the string expressions into sympy objects
52+
x = sp.sympify(x_expr)
53+
y = sp.sympify(y_expr)
54+
z = sp.sympify(z_expr)
55+
56+
# Define the position vector r
57+
r = sp.Matrix([x, y, z])
58+
59+
# Calculate partial derivatives (tangent vectors)
60+
r_u = sp.diff(r, u)
61+
r_v = sp.diff(r, v)
62+
63+
# Compute the coefficients E, F, G using the dot product
64+
# We use simplify to combine trigonometric terms where possible
65+
e = sp.simplify(r_u.dot(r_u))
66+
f = sp.simplify(r_u.dot(r_v))
67+
g = sp.simplify(r_v.dot(r_v))
68+
69+
return e, f, g
70+
71+
72+
if __name__ == "__main__":
73+
import doctest
74+
75+
doctest.testmod()

0 commit comments

Comments
 (0)