ellphi.grad
Differentiable tangency distances for gradient-based optimisation and persistent-homology backprop.
Gradients are derived via the envelope theorem: the derivative through the optimal tangent point \(\bm{x}^*\) vanishes, so only the explicit dependence of the pencil on the input coefficients survives. This makes the gradient cheap — no second solver pass is required.
from ellphi.grad import tangency_grad, pdist_tangency_grad
# Single pair
g = tangency_grad(p, q)
# g.t, g.dt_dp, g.dt_dq
# Batch pairwise with VJP
dists, vjp = pdist_tangency_grad(coefs)
grad_coefs = vjp(upstream_grad) # ∂loss/∂coefs
See the Differentiable Tangency guide for full examples.
ellphi.grad
Differentiable tangency distances and geometry helpers.
Gradient-based optimisation support for ellphi.
This module exposes tangency_grad (single-pair gradient),
pdist_tangency_grad (batch pairwise, with VJP pullback), and
coef_from_cov_grad (differentiable coefficient computation).
TangencyGrad
dataclass
Tangency distance and its gradients with respect to both ellipsoids.
Attributes:
| Name | Type | Description |
|---|---|---|
t |
float
|
Tangency distance (same as |
dt_dp |
ndarray
|
Gradient of |
dt_dq |
ndarray
|
Gradient of |
Source code in src/ellphi/grad.py
coef_from_cov_grad(X, cov, /, *, scale=1.0)
Converts centers and covariances to packed conic coefficients, with VJP.
This is the differentiable version of coef_from_cov. It returns the same
coefficient array plus a VJP (vector-Jacobian product) pullback function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
ndarray
|
Centers array, shape |
required |
cov |
ndarray
|
Covariance matrices, shape |
required |
scale |
float
|
Optional scaling factor for covariance matrices. |
1.0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
A tuple |
Callable[[ndarray], tuple[ndarray, ndarray]]
|
|
tuple[ndarray, Callable[[ndarray], tuple[ndarray, ndarray]]]
|
|
Source code in src/ellphi/grad.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
pdist_tangency_grad(coefs)
Pairwise tangency distances and a VJP (pullback) for all pairs.
Computes the same condensed distance array as
pdist_tangency
and additionally returns a VJP function that maps upstream gradient vectors
back to per-ellipsoid coefficient gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coefs |
ndarray
|
Array of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
A tuple |
Callable[[ndarray], ndarray]
|
|
tuple[ndarray, Callable[[ndarray], ndarray]]
|
|
Examples:
>>> import numpy as np
>>> from ellphi import ellipse_cloud
>>> from ellphi.grad import pdist_tangency_grad
>>> rng = np.random.default_rng(0)
>>> cloud = ellipse_cloud(rng.standard_normal((6, 2)), k=3)
>>> dists, vjp = pdist_tangency_grad(cloud.coef)
>>> dists.shape # N*(N-1)//2 = 15
(15,)
>>> vjp(np.ones(15)).shape # (N, m) = (6, 6)
(6, 6)
Source code in src/ellphi/grad.py
tangency_grad(p, q, **solver_kwargs)
Return the tangency distance and its gradients w.r.t. p and q.
Uses the envelope theorem: the chain through the optimal center vanishes,
so only the explicit dependence of the pencil coefficients on p/q
contributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p |
ndarray
|
Coefficient vector of the first ellipsoid. |
required |
q |
ndarray
|
Coefficient vector of the second ellipsoid. |
required |
**solver_kwargs |
Forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
TangencyGrad
|
A |
TangencyGrad
|
|
Raises:
| Type | Description |
|---|---|
ZeroDivisionError
|
When the pencil derivative |