Python · Curved Metrics

Curved Metrics

import numkong 8.0.0-rc.1 · b1f866ab

Curved-space kernels use an extra metric tensor or inverse covariance and should not be mixed into the Euclidean section.

import numpy as np
import numkong as nk

# Complex bilinear form: aᴴ M b
a = (np.ones(16) + 1j * np.zeros(16)).astype(np.complex64)
b = (np.zeros(16) + 1j * np.ones(16)).astype(np.complex64)
m = np.eye(16, dtype=np.complex64)
bilinear = nk.bilinear(a, b, m)

# Real Mahalanobis distance: √((a−b)ᵀ M⁻¹ (a−b))
x = np.ones(32, dtype=np.float32)
y = np.full(32, 2.0, dtype=np.float32)
inv_cov = np.eye(32, dtype=np.float32)
mahalanobis = nk.mahalanobis(x, y, inv_cov)

API

Functions

bilinearCompute the bilinear form aᵀ × M × b of two vectors under a metric tensor M.
__init__.pyi:1233 ↗
def bilinear(
    a: _BufferType,
    b: _BufferType,
    metric_tensor: _BufferType,
    /,
    dtype: _FloatTypeName | _MiniFloatType | None = None,
    *,
    capabilities: Capability | int | None = ...,
    stream: int | None = ...,
) -> float

Similar to numpy.dot(a, metric_tensor @ b).

Parameters

a_BufferTypeFirst vector.
b_BufferTypeSecond vector.
metric_tensor_BufferTypeMatrix M defining the bilinear form.
dtype_FloatTypeName | _MiniFloatType | NoneInput dtype to read every buffer as, overriding the one they report.
capabilitiesCapability | int | NoneCapabilities to run within: every CPU one by default, or for packed operands the mask that packed them.
streamint | NoneA GPU stream pointer, left None on the CPU.
mahalanobisCompute the Mahalanobis distance between two vectors given an inverse covariance matrix.
__init__.pyi:1253 ↗
def mahalanobis(
    a: _BufferType,
    b: _BufferType,
    inverse_covariance: _BufferType,
    /,
    dtype: _FloatTypeName | _MiniFloatType | None = None,
    *,
    capabilities: Capability | int | None = ...,
    stream: int | None = ...,
) -> float

Parameters

a_BufferTypeFirst vector.
b_BufferTypeSecond vector.
inverse_covariance_BufferTypeInverse of the covariance matrix.
dtype_FloatTypeName | _MiniFloatType | NoneInput dtype to read every buffer as, overriding the one they report.
capabilitiesCapability | int | NoneCapabilities to run within: every CPU one by default, or for packed operands the mask that packed them.
streamint | NoneA GPU stream pointer, left None on the CPU.