ellphi.geometry
Utilities for encoding and decoding ellipsoid geometry as packed conic coefficient vectors.
A d-dimensional ellipsoid centred at \(\bm{x}_0\) with covariance \(\Sigma\) is represented by the quadratic form
The coefficient vector packs the upper-triangular entries of \(A = \Sigma^{-1}\), followed by the linear term \(\bm{b} = -A\bm{x}_0\) and the scalar \(c = \bm{x}_0^\top A \bm{x}_0\), into a flat array of length \(m = (d+1)(d+2)/2\). The internal evaluator applies a factor of 2 to \(\bm{b}\), so the effective evaluation is \((\bm{x} - \bm{x}_0)^\top A (\bm{x} - \bm{x}_0)\), which equals 1 on the ellipsoid boundary and 0 at its center.
ellphi.geometry
Geometry helpers shared across the ellphi package.
This module historically provided 2D-only utilities for working with ellipses represented as quadratic forms. For the n-dimensional extension the public façade is kept backwards compatible while the core routines now work with generic symmetric positive-definite matrices.
Key API (all return numpy.float64 arrays):
unit_vectoraxes_from_covcoef_from_axescoef_from_covpack_conicunpack_conicunpack_single_conicinfer_dim_from_coef_length
axes_from_cov(cov, /, *, scale=1.0)
Computes the axes and orientation of an ellipse from a covariance matrix.
This function takes a 2x2 covariance matrix and returns the major axis, minor axis, and angle of the major axis. The axes are scaled by the provided scale factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cov |
FloatArray
|
A 2x2 covariance matrix. |
required |
scale |
float
|
A scaling factor for the axes. |
1.0
|
Returns:
| Type | Description |
|---|---|
|
A tuple containing the major axis, minor axis, and angle of the major axis in radians. The major axis is always greater than or equal to the minor axis. |
Examples:
>>> import numpy as np
>>> from ellphi.geometry import axes_from_cov
>>> cov = np.array([[4.0, 0.0], [0.0, 1.0]])
>>> r_major, r_minor, theta = axes_from_cov(cov)
>>> float(r_major), float(r_minor)
(2.0, 1.0)
Source code in src/ellphi/geometry.py
coef_from_axes(X, r0, r1, theta)
Computes the conic coefficient array from the center and axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
float
|
The center of the conic. |
required |
r0 |
float
|
The major axis. |
required |
r1 |
float
|
The minor axis. |
required |
theta |
float
|
The angle of the major axis in radians. |
required |
Returns:
| Type | Description |
|---|---|
FloatArray
|
A NumPy array of shape (6,) representing the conic coefficients. |
Source code in src/ellphi/geometry.py
coef_from_cov(X, cov, /, *, scale=1.0)
Converts centers and covariances to packed conic coefficients.
This function takes an array of centers and covariance matrices and converts them into packed conic coefficients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
FloatArray
|
An array of centers with shape |
required |
cov |
FloatArray
|
An array of covariance matrices with shape |
required |
scale |
float
|
An optional scaling factor for the covariance matrices. |
1.0
|
Returns:
| Type | Description |
|---|---|
FloatArray
|
A NumPy array of packed conic coefficients with shape |
Examples:
>>> import numpy as np
>>> from ellphi.geometry import coef_from_cov
>>> center = np.array([1.0, 0.0])
>>> cov = np.array([[2.0, 0.0], [0.0, 1.0]])
>>> coef = coef_from_cov(center, cov)
>>> coef.shape
(1, 6)
Source code in src/ellphi/geometry.py
infer_dim_from_coef_length(length)
Infers the ambient dimension n from the flattened coefficient length.
The coefficient vector stores the upper triangular part of the quadratic
matrix, followed by the linear term and the constant term. Its length is
therefore m = (n + 1)(n + 2) / 2. The function validates that the
supplied length matches this formula and returns n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length |
int
|
The length of the flattened coefficient vector. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The inferred ambient dimension |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the length does not correspond to a valid dimension. |
Source code in src/ellphi/geometry.py
pack_conic(matrices, linear, constant)
Packs a conic's components into a flattened coefficient vector.
This function takes the quadratic, linear, and constant terms of a conic's equation and packs them into a single, flattened NumPy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrices |
FloatArray
|
A NumPy array of symmetric positive-definite matrices |
required |
linear |
FloatArray
|
A NumPy array of linear coefficients |
required |
constant |
FloatArray
|
A NumPy array of the constant term |
required |
Returns:
| Type | Description |
|---|---|
FloatArray
|
A NumPy array of shape |
Source code in src/ellphi/geometry.py
unit_vector(theta)
Computes the unit vector for a given angle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta |
float
|
The angle in radians. |
required |
Returns:
| Type | Description |
|---|---|
FloatArray
|
The unit vector (cosθ, sinθ) as a NumPy array. |
Source code in src/ellphi/geometry.py
unpack_conic(coef)
Unpacks a flattened coefficient vector into a conic's components.
This function is the inverse of
pack_conic. It takes a flattened
coefficient vector and returns the quadratic, linear, and constant terms
of the conic's equation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coef |
FloatArray
|
A NumPy array of packed conic coefficients. |
required |
Returns:
| Type | Description |
|---|---|
tuple[FloatArray, FloatArray, FloatArray]
|
A tuple containing the quadratic matrix |
Source code in src/ellphi/geometry.py
unpack_single_conic(coef)
Unpacks a single flattened coefficient vector into a conic's components.
This function is a convenience wrapper around
unpack_conic for a
single conic. It accepts a coefficient array of shape (m,) or (1, m)
and returns the conic's components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coef |
FloatArray
|
A NumPy array of packed conic coefficients for a single conic. |
required |
Returns:
| Type | Description |
|---|---|
tuple[FloatArray, FloatArray, FloatArray]
|
A tuple containing the quadratic matrix |