Skip to content

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

\[ E = \{ \bm{x} : (\bm{x} - \bm{x}_0)^\top \Sigma^{-1} (\bm{x} - \bm{x}_0) \le 1 \} \]

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):

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
def axes_from_cov(cov: numpy.ndarray, /, *, scale: float = 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.

    Args:
        cov: A 2x2 covariance matrix.
        scale: A scaling factor for the axes.

    Returns:
        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)
    """
    if len(cov.shape) <= 2:
        cov = cov[None, :, :]
    eigvals, eigvecs = numpy.linalg.eigh(cov)
    lam0, lam1 = eigvals[:, 0], eigvals[:, 1]  # ascending order: lam0 <= lam1
    v1 = eigvecs[:, 1]
    theta = numpy.arctan2(v1[:, 1], v1[:, 0])
    # Major axis, minor axis, major axis angle
    return (numpy.sqrt(lam1) * scale, numpy.sqrt(lam0) * scale, theta)

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
def coef_from_axes(X: float, r0: float, r1: float, theta: float) -> numpy.ndarray:
    """Computes the conic coefficient array from the center and axes.

    Args:
        X: The center of the conic.
        r0: The major axis.
        r1: The minor axis.
        theta: The angle of the major axis in radians.

    Returns:
        A NumPy array of shape (6,) representing the conic coefficients.
    """
    return _coef_core(X, r0, r1, numpy.cos(theta), numpy.sin(theta))

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 (n, d) or a single center with shape (d,). Single centers are promoted to (1, d).

required
cov FloatArray

An array of covariance matrices with shape (n, d, d) or a single covariance with shape (d, d).

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 (n, m), where m = (d + 1)(d + 2) / 2.

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
def coef_from_cov(
    X: numpy.ndarray,
    cov: numpy.ndarray,
    /,
    *,
    scale: float = 1.0,
) -> numpy.ndarray:
    """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.

    Args:
        X: An array of centers with shape `(n, d)` or a single center with
            shape `(d,)`. Single centers are promoted to `(1, d)`.
        cov: An array of covariance matrices with shape `(n, d, d)` or a
            single covariance with shape `(d, d)`.
        scale: An optional scaling factor for the covariance matrices.

    Returns:
        A NumPy array of packed conic coefficients with shape `(n, m)`, where
            `m = (d + 1)(d + 2) / 2`.

    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)
    """
    centers = numpy.asarray(X, dtype=float)
    cov = numpy.asarray(cov, dtype=float)

    if centers.ndim == 1:
        centers = centers[numpy.newaxis, :]
    if cov.ndim == 2:
        cov = cov[numpy.newaxis, :, :]

    if centers.shape[0] != cov.shape[0]:
        raise ValueError("Mismatch between number of centres and covariance matrices")
    if cov.shape[-1] != cov.shape[-2]:
        raise ValueError("Covariance matrices must be square")

    n_dim = centers.shape[-1]
    if cov.shape[-1] != n_dim:
        raise ValueError("Centre dimensionality and covariance size must agree")

    try:
        inv_cov = numpy.linalg.inv(cov)
    except numpy.linalg.LinAlgError:
        # If the covariance matrix is singular,
        # we cannot compute the conic coefficients.
        # We return an array of NaNs to indicate that the computation failed,
        # allowing the caller (e.g., the solver) to handle this case gracefully.
        m = (n_dim + 1) * (n_dim + 2) // 2
        return numpy.full(centers.shape[:-1] + (m,), numpy.nan, dtype=float)

    matrices = inv_cov / (scale**2)
    b = -(matrices @ centers[..., None])[..., 0]
    c = numpy.einsum("...i,...ij,...j->...", centers, matrices, centers)
    packed = pack_conic(matrices, b, c)
    return packed

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 n.

Raises:

Type Description
ValueError

If the length does not correspond to a valid dimension.

Source code in src/ellphi/geometry.py
def infer_dim_from_coef_length(length: int) -> int:
    """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`.

    Args:
        length: The length of the flattened coefficient vector.

    Returns:
        The inferred ambient dimension `n`.

    Raises:
        ValueError: If the length does not correspond to a valid dimension.
    """
    if length < 6:
        raise ValueError("Coefficient vector too short to represent a conic")
    disc = 1 + 8 * length
    sqrt_disc = int(round(numpy.sqrt(disc)))
    if sqrt_disc * sqrt_disc != disc:
        raise ValueError(
            f"Coefficient length {length} does not correspond to a symmetric "
            "quadratic form"
        )
    n = (sqrt_disc - 3) // 2
    if n < 2 or ((n + 1) * (n + 2)) // 2 != length:  # pragma: no cover
        raise ValueError(
            f"Coefficient length {length} does not correspond to a valid " "dimension"
        )
    return int(n)

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 A with shape (..., n, n).

required
linear FloatArray

A NumPy array of linear coefficients b with shape (..., n).

required
constant FloatArray

A NumPy array of the constant term c with shape (...).

required

Returns:

Type Description
FloatArray

A NumPy array of shape (..., m) where m = (n + 1)(n + 2) / 2, representing the packed conic coefficients.

Source code in src/ellphi/geometry.py
def pack_conic(
    matrices: numpy.ndarray, linear: numpy.ndarray, constant: numpy.ndarray
) -> numpy.ndarray:
    """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.

    Args:
        matrices: A NumPy array of symmetric positive-definite matrices `A` with
            shape `(..., n, n)`.
        linear: A NumPy array of linear coefficients `b` with shape `(..., n)`.
        constant: A NumPy array of the constant term `c` with shape `(...)`.

    Returns:
        A NumPy array of shape `(..., m)` where `m = (n + 1)(n + 2) / 2`,
            representing the packed conic coefficients.
    """
    matrices = numpy.asarray(matrices, dtype=float)
    linear = numpy.asarray(linear, dtype=float)
    constant = numpy.asarray(constant, dtype=float)

    if matrices.ndim < 2 or matrices.shape[-1] != matrices.shape[-2]:
        raise ValueError("Quadratic matrices must have shape (..., n, n)")
    n = matrices.shape[-1]
    if linear.shape[-1] != n:
        raise ValueError("Linear term incompatible with quadratic matrix dimension")

    quad_shape = matrices.shape[:-2]
    linear_shape = linear.shape[:-1]
    const_shape = constant.shape
    broadcast_shape = _broadcast_shapes(quad_shape, linear_shape, const_shape)

    matrices = numpy.broadcast_to(matrices, broadcast_shape + (n, n))
    linear = numpy.broadcast_to(linear, broadcast_shape + (n,))
    constant = numpy.broadcast_to(constant, broadcast_shape)

    tri_i, tri_j = numpy.triu_indices(n)
    quad_entries = matrices[..., tri_i, tri_j]
    packed = numpy.concatenate(
        [quad_entries, linear, constant[..., numpy.newaxis]], axis=-1
    )
    return packed

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
def unit_vector(theta: float) -> numpy.ndarray:
    """Computes the unit vector for a given angle.

    Args:
        theta: The angle in radians.

    Returns:
        The unit vector (cosθ, sinθ) as a NumPy array.
    """
    return numpy.transpose([numpy.cos(theta), numpy.sin(theta)])

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 A, the linear coefficients b, and the constant term c.

Source code in src/ellphi/geometry.py
def unpack_conic(
    coef: numpy.ndarray,
) -> tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]:
    """Unpacks a flattened coefficient vector into a conic's components.

    This function is the inverse of
    [`pack_conic`][ellphi.geometry.pack_conic]. It takes a flattened
    coefficient vector and returns the quadratic, linear, and constant terms
    of the conic's equation.

    Args:
        coef: A NumPy array of packed conic coefficients.

    Returns:
        A tuple containing the quadratic matrix `A`, the linear coefficients
            `b`, and the constant term `c`.
    """
    coef = numpy.asarray(coef, dtype=float)
    squeeze = False
    if coef.ndim == 1:
        coef = coef[numpy.newaxis, :]
        squeeze = True
    if coef.ndim != 2:
        raise ValueError("Coefficient array must be one- or two-dimensional")

    length = coef.shape[-1]
    n = infer_dim_from_coef_length(length)
    tri_i, tri_j = numpy.triu_indices(n)
    n_quad = tri_i.size

    quad_entries = coef[:, :n_quad]
    linear = coef[:, n_quad : n_quad + n]
    constant = coef[:, n_quad + n]

    matrices = numpy.zeros((coef.shape[0], n, n), dtype=float)
    matrices[:, tri_i, tri_j] = quad_entries
    matrices[:, tri_j, tri_i] = quad_entries

    if squeeze:
        return matrices[0], linear[0], constant[0]
    return matrices, linear, constant

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 A, the linear coefficients b, and the constant term c.

Source code in src/ellphi/geometry.py
def unpack_single_conic(
    coef: numpy.ndarray,
) -> tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]:
    """Unpacks a single flattened coefficient vector into a conic's components.

    This function is a convenience wrapper around
    [`unpack_conic`][ellphi.geometry.unpack_conic] for a
    single conic. It accepts a coefficient array of shape `(m,)` or `(1, m)`
    and returns the conic's components.

    Args:
        coef: A NumPy array of packed conic coefficients for a single conic.

    Returns:
        A tuple containing the quadratic matrix `A`, the linear coefficients
            `b`, and the constant term `c`.
    """
    matrices, linear, constant = unpack_conic(coef)
    if matrices.ndim == 3:
        if matrices.shape[0] != 1:
            raise ValueError(
                "Expected coefficients for a single conic, "
                f"received {matrices.shape[0]} entries"
            )
        matrices = matrices[0]
        linear = linear[0]
        constant = constant[0]
    return matrices, linear, constant