vector_analysis_functions¶
shortfx.fxNumeric.vector_analysis_functions
¶
Vector analysis and multivariable calculus.
Gradient, divergence, curl, Laplacian, Jacobian, Hessian, and related operations from Spiegel's Mathematical Handbook of Formulas and Tables.
Functions¶
curl_numerical(f_components: List[Callable[..., float]], point: List[float], h: float = 1e-07) -> List[float]
¶
Computes the numerical curl of a 3-D vector field F.
curl(F) = (dF3/dy - dF2/dz, dF1/dz - dF3/dx, dF2/dx - dF1/dy).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_components
|
List[Callable[..., float]]
|
List of 3 functions [F_x, F_y, F_z]. |
required |
point
|
List[float]
|
3-D evaluation point [x, y, z]. |
required |
h
|
float
|
Step size. |
1e-07
|
Returns:
| Type | Description |
|---|---|
List[float]
|
Curl vector [curl_x, curl_y, curl_z]. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If not 3-D. |
Example
curl = curl_numerical( ... [lambda x, y, z: -y, lambda x, y, z: x, lambda x, y, z: 0], ... [0.0, 0.0, 0.0], ... ) [round(c, 4) for c in curl][0.0, 0.0, 2.0]
Complexity: O(12) function evaluations.
Source code in shortfx/fxNumeric/vector_analysis_functions.py
directional_derivative(f: Callable[..., float], point: List[float], direction: List[float], h: float = 1e-07) -> float
¶
Computes the directional derivative of f in the given direction.
D_u f = grad(f) · u_hat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[..., float]
|
Scalar-valued function. |
required |
point
|
List[float]
|
Evaluation point. |
required |
direction
|
List[float]
|
Direction vector (not necessarily unit). |
required |
h
|
float
|
Step size for gradient. |
1e-07
|
Returns:
| Type | Description |
|---|---|
float
|
Directional derivative. |
Example
round(directional_derivative( ... lambda x, y: x2 + y2, [1.0, 1.0], [1.0, 0.0] ... ), 4) 2.0
Complexity: O(2n)
Source code in shortfx/fxNumeric/vector_analysis_functions.py
divergence_numerical(f_components: List[Callable[..., float]], point: List[float], h: float = 1e-07) -> float
¶
Computes the numerical divergence of a vector field F.
div(F) = sum_i dF_i/dx_i.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_components
|
List[Callable[..., float]]
|
List of scalar functions [F_1, F_2, ..., F_n], each accepting the same number of arguments as len(point). |
required |
point
|
List[float]
|
Evaluation point. |
required |
h
|
float
|
Step size. |
1e-07
|
Returns:
| Type | Description |
|---|---|
float
|
Divergence scalar. |
Example
div = divergence_numerical( ... [lambda x, y: x, lambda x, y: y], ... [1.0, 1.0], ... ) round(div, 4) 2.0
Complexity: O(2n) function evaluations.
Source code in shortfx/fxNumeric/vector_analysis_functions.py
divergence_theorem_verify(field: Tuple[Callable[[float, float, float], float], Callable[[float, float, float], float], Callable[[float, float, float], float]], r_surface: Callable[[float, float], Tuple[float, float, float]], u_range: Tuple[float, float], v_range: Tuple[float, float], x_range: Tuple[float, float], y_range: Tuple[float, float], z_range: Tuple[float, float], n_s: int = 50, n_v_pts: int = 20) -> Tuple[float, float]
¶
Verifies the divergence theorem: ∬_S F·dS = ∭_V ∇·F dV.
Computes both the surface flux integral and the volume integral of the divergence, returning both for comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Tuple[Callable[[float, float, float], float], Callable[[float, float, float], float], Callable[[float, float, float], float]]
|
Tuple of 3 functions (Fx, Fy, Fz). |
required |
r_surface
|
Callable[[float, float], Tuple[float, float, float]]
|
Parametric surface r(u,v) → (x,y,z). |
required |
u_range
|
Tuple[float, float]
|
Parameter range for surface. |
required |
v_range
|
Tuple[float, float]
|
Parameter range for surface. |
required |
x_range
|
Tuple[float, float]
|
Volume bounds in x. |
required |
y_range
|
Tuple[float, float]
|
Volume bounds in y. |
required |
z_range
|
Tuple[float, float]
|
Volume bounds in z. |
required |
n_s
|
int
|
Surface quadrature subdivisions. |
50
|
n_v_pts
|
int
|
Volume quadrature points per dimension. |
20
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple (surface_flux, volume_divergence). |
Example
isinstance(divergence_theorem_verify( ... (lambda x,y,z: x, lambda x,y,z: y, lambda x,y,z: z), ... lambda u,v: (import('math').sin(u)import('math').cos(v), import('math').sin(u)import('math').sin(v), import('math').cos(u)), ... (0, 3.14159), (0, 6.28318), (-1,1), (-1,1), (-1,1), 30, 10 ... ), tuple) True
Complexity: O(n_s^2 + n_v_pts^3)
Source code in shortfx/fxNumeric/vector_analysis_functions.py
flux_integral_numerical(field: Tuple[Callable[[float, float, float], float], Callable[[float, float, float], float], Callable[[float, float, float], float]], r: Callable[[float, float], Tuple[float, float, float]], u_range: Tuple[float, float], v_range: Tuple[float, float], n_u: int = 50, n_v: int = 50) -> float
¶
Computes a vector flux integral ∬_S F · dS numerically.
F · dS = F · (∂r/∂u × ∂r/∂v) du dv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Tuple[Callable[[float, float, float], float], Callable[[float, float, float], float], Callable[[float, float, float], float]]
|
Tuple of 3 functions (Fx, Fy, Fz) giving the vector field. |
required |
r
|
Callable[[float, float], Tuple[float, float, float]]
|
Parametric surface function r(u, v) → (x, y, z). |
required |
u_range
|
Tuple[float, float]
|
Tuple (u_min, u_max). |
required |
v_range
|
Tuple[float, float]
|
Tuple (v_min, v_max). |
required |
n_u
|
int
|
Subdivisions in u. |
50
|
n_v
|
int
|
Subdivisions in v. |
50
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate flux integral. |
Example
import math
Flux of radial field F=(x,y,z) through unit sphere = 4π¶
def sphere(u, v): return (math.sin(u)math.cos(v), math.sin(u)math.sin(v), math.cos(u)) round(flux_integral_numerical((lambda x,y,z: x, lambda x,y,z: y, lambda x,y,z: z), sphere, (0, math.pi), (0, 2*math.pi), 100, 100), 1) 12.6
Complexity: O(n_u * n_v)
Source code in shortfx/fxNumeric/vector_analysis_functions.py
gradient_numerical(f: Callable[..., float], point: List[float], h: float = 1e-07) -> List[float]
¶
Computes the numerical gradient (nabla f) at a point.
Uses central differences: df/dx_i ≈ (f(x+h e_i) - f(x-h e_i)) / (2h).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[..., float]
|
Scalar-valued function f(x1, x2, ..., xn). |
required |
point
|
List[float]
|
Evaluation point. |
required |
h
|
float
|
Step size. |
1e-07
|
Returns:
| Type | Description |
|---|---|
List[float]
|
Gradient vector. |
Example
grad = gradient_numerical(lambda x, y: x2 + y2, [1.0, 2.0]) [round(g, 4) for g in grad][2.0, 4.0]
Complexity: O(2n) function evaluations.
Source code in shortfx/fxNumeric/vector_analysis_functions.py
hessian_numerical(f: Callable[..., float], point: List[float], h: float = 1e-05) -> List[List[float]]
¶
Computes the numerical Hessian matrix of a scalar function f.
H[i][j] = d^2f / (dx_i dx_j).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[..., float]
|
Scalar-valued function. |
required |
point
|
List[float]
|
Evaluation point. |
required |
h
|
float
|
Step size. |
1e-05
|
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
n × n Hessian matrix. |
Example
H = hessian_numerical(lambda x, y: x2 + y2, [1.0, 1.0]) [[round(v, 2) for v in row] for row in H][[2.0, 0.0], [0.0, 2.0]]
Complexity: O(n^2) function evaluations.
Source code in shortfx/fxNumeric/vector_analysis_functions.py
jacobian_numerical(f_components: List[Callable[..., float]], point: List[float], h: float = 1e-07) -> List[List[float]]
¶
Computes the numerical Jacobian matrix of a vector-valued function F.
J[i][j] = dF_i / dx_j.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_components
|
List[Callable[..., float]]
|
List of m scalar functions. |
required |
point
|
List[float]
|
n-dimensional evaluation point. |
required |
h
|
float
|
Step size. |
1e-07
|
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
m × n Jacobian matrix. |
Example
J = jacobian_numerical( ... [lambda x, y: x*y, lambda x, y: x+y], ... [2.0, 3.0], ... ) [[round(v, 2) for v in row] for row in J][[3.0, 2.0], [1.0, 1.0]]
Complexity: O(2 * m * n)
Source code in shortfx/fxNumeric/vector_analysis_functions.py
laplacian_numerical(f: Callable[..., float], point: List[float], h: float = 1e-05) -> float
¶
Computes the numerical Laplacian of a scalar field f.
nabla^2 f = sum_i d^2f/dx_i^2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[..., float]
|
Scalar-valued function. |
required |
point
|
List[float]
|
Evaluation point. |
required |
h
|
float
|
Step size. |
1e-05
|
Returns:
| Type | Description |
|---|---|
float
|
Laplacian scalar. |
Example
round(laplacian_numerical(lambda x, y: x2 + y2, [1.0, 1.0]), 2) 4.0
Complexity: O(2n) function evaluations.
Source code in shortfx/fxNumeric/vector_analysis_functions.py
line_integral_numerical(f_components: List[Callable[..., float]], path_points: List[List[float]]) -> float
¶
Numerically approximates a line integral of F along a piecewise-linear path.
integral F · dr ≈ sum F(midpoint) · delta_r.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_components
|
List[Callable[..., float]]
|
Vector field [F_1, ..., F_n]. |
required |
path_points
|
List[List[float]]
|
Ordered list of points defining the path. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Approximate line integral. |
Example
line_integral_numerical( ... [lambda x, y: 1, lambda x, y: 0], ... [[0, 0], [1, 0], [1, 1]], ... ) 1.0
Complexity: O(m * n) where m = number of segments.
Source code in shortfx/fxNumeric/vector_analysis_functions.py
scalar_triple_product(u: List[float], v: List[float], w: List[float]) -> float
¶
Scalar triple product u · (v × w) = det([u; v; w]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u, v, w
|
3-D vectors as lists of length 3. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Scalar value. |
Example
scalar_triple_product([1, 0, 0], [0, 1, 0], [0, 0, 1]) 1.0
Complexity: O(1)
Source code in shortfx/fxNumeric/vector_analysis_functions.py
surface_integral_numerical(f: Callable[[float, float, float], float], r: Callable[[float, float], Tuple[float, float, float]], u_range: Tuple[float, float], v_range: Tuple[float, float], n_u: int = 50, n_v: int = 50) -> float
¶
Computes a scalar surface integral ∬_S f(x,y,z) dS numerically.
dS = |∂r/∂u × ∂r/∂v| du dv for a parametric surface r(u,v).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float, float, float], float]
|
Scalar function f(x, y, z). |
required |
r
|
Callable[[float, float], Tuple[float, float, float]]
|
Parametric surface function r(u, v) → (x, y, z). |
required |
u_range
|
Tuple[float, float]
|
Tuple (u_min, u_max). |
required |
v_range
|
Tuple[float, float]
|
Tuple (v_min, v_max). |
required |
n_u
|
int
|
Subdivisions in u. |
50
|
n_v
|
int
|
Subdivisions in v. |
50
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate surface integral. |
Example
import math
Sphere r=1: surface area = 4π¶
def sphere(u, v): return (math.sin(u)math.cos(v), math.sin(u)math.sin(v), math.cos(u)) round(surface_integral_numerical(lambda x,y,z: 1, sphere, (0, math.pi), (0, 2*math.pi), 100, 100), 1) 12.6
Complexity: O(n_u * n_v)
Source code in shortfx/fxNumeric/vector_analysis_functions.py
vector_projection(u: List[float], v: List[float]) -> List[float]
¶
Projection of vector u onto vector v: proj_v(u) = (u·v / v·v) v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
List[float]
|
Vector to project. |
required |
v
|
List[float]
|
Vector to project onto (must be non-zero). |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
Projection vector. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If v is a zero vector. |
Example
vector_projection([3, 4], [1, 0]) [3.0, 0.0]
Complexity: O(n) for n-dimensional vectors.
Source code in shortfx/fxNumeric/vector_analysis_functions.py
vector_triple_product(u: List[float], v: List[float], w: List[float]) -> List[float]
¶
Vector triple product u × (v × w) = v(u·w) - w(u·v).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u, v, w
|
3-D vectors. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
Resulting 3-D vector. |
Example
vector_triple_product([1, 0, 0], [0, 1, 0], [0, 0, 1]) [0.0, 0.0, 0.0]
Complexity: O(1)