Skip to main content

math

C++: phynexis::utils::math Python: phynexis.utils.math Header: src/utils/math/math.hpp

Mathematical constants, vector operations, and quaternion utilities.

Constants

NameValueDescription
PI3.14159...Circle constant
EPSILON2.22e-16Machine epsilon for doubles
TOL1e-6Default numerical tolerance
HUGE_VALUE1e20Large sentinel value
SQRT_21.41421...Square root of 2
SQRT_31.73205...Square root of 3

Vector Operations

dot(a, b)

Compute dot product of two vectors.

Parameters:

ParameterTypeDescription
aVec3dFirst vector
bVec3dSecond vector

Returns: float

cross(a, b)

Compute cross product of two vectors.

Returns: Vec3d

norm_l2(v)

Compute L2 norm (magnitude) of a vector.

Returns: float

normalize(v)

Normalize a vector in-place.

to_int64(val)

Convert a 32-bit integer to a 64-bit integer.

Parameters:

ParameterTypeDescription
valint32-bit integer value

Returns: int — 64-bit integer

Quaternion

Module phynexis.utils.math.quaternion provides quaternion operations.

from_rodrigues(angle, axis)

Create a quaternion from angle-axis (Rodrigues) representation.

Parameters:

ParameterTypeDescription
anglefloatRotation angle in radians
axisVec3dRotation axis

Returns: Vec4d

to_rodrigues(q) / to_matrix(q) / from_matrix(m)

Convert quaternion to/from Rodrigues, rotation matrix.

multiply(a, b) / conjugate(q) / normalize(q) / add(a, b)

Quaternion arithmetic.

Example

import phynexis

# Constants
print("PI:", phynexis.utils.math.PI)
print("EPSILON:", phynexis.utils.math.EPSILON)

# Vector ops
a = phynexis.utils.Vec3d(1.0, 0.0, 0.0)
b = phynexis.utils.Vec3d(0.0, 1.0, 0.0)
print("dot:", phynexis.utils.math.dot(a, b))
print("cross:", phynexis.utils.math.cross(a, b))
print("norm:", phynexis.utils.math.norm_l2(a))

# Quaternion
q = phynexis.utils.math.quaternion.from_rodrigues(
phynexis.utils.math.PI / 2,
phynexis.utils.Vec3d(0.0, 0.0, 1.0)
)
print("quaternion:", q)

Output:

PI: 3.141592653589793
EPSILON: 2.220446049250313e-16
dot: 0.0
cross: Vec3d(0, 0, 1)
norm: 1.0
quaternion: Vec4d(0, 0, 0.707107, 0.707107)

Spherical Harmonics

Spherical harmonics evaluation functions used internally by SphericalHarmonics shape.

legendre_p(n, m, x)

Associated Legendre polynomial P(n, m, x).

Parameters:

ParameterTypeDescription
nintDegree
mintOrder
xfloatInput value

Returns: float

normalization_factor(n, m)

Compute spherical harmonic normalization factor.

Parameters:

ParameterTypeDescription
nintDegree
mintOrder

Returns: float

calculate_ynm_fast(theta, phi, deg)

Evaluate spherical harmonics at given angles up to specified degree. Accepts scalar or array inputs.

Parameters:

ParameterTypeDescription
thetafloat or list[float]Polar angle(s)
phifloat or list[float]Azimuthal angle(s)
degintMaximum degree

Returns: list[float] — Ynm coefficients

Example:

import phynexis

# Associated Legendre polynomial
p = phynexis.utils.math.legendre_p(2, 1, 0.5)
print("legendre P(2,1,0.5):", p)

# Normalization factor
nf = phynexis.utils.math.normalization_factor(2, 1)
print("normalization factor:", nf)

Output:

legendre P(2,1,0.5): -1.299038105676658
normalization factor: 2.958039891549808

Unexposed C++ API

  • flat_array3d utilities (internal 3D array layout, not exposed via pybind)