Manifolds and Lie groups

Pass a manifold as manifold= to keep the parameters on a constrained set, such as rotations or rigid transforms. See the manifolds guide.

Manifolds

class piazzi.Manifold[source]

Bases: object

Base class for manifolds: a parameter set with a retraction plus.

The solvers take derivatives with respect to a tangent vector delta at delta = 0 and apply each step as x <- plus(x, step), as Ceres Solver’s Manifold does. Subclasses set ambient_shape and implement tangent_size() and plus(); minus() is optional. plus must be differentiable in forward mode (torch.func.jacfwd()). Points may be passed flattened or in ambient_shape, and plus returns the shape it was given.

ambient_shape

the shape a point is stored in, or None for any shape.

Type:

tuple of int or None

tangent_size(x)[source]

Number of degrees of freedom at x.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

int – the length of a tangent vector delta at x.

plus(x, delta)[source]

Move x by a tangent vector (the retraction); a zero vector leaves it unchanged.

Parameters:
  • x (torch.Tensor) – a point on the manifold, flattened or in ambient_shape.

  • delta (torch.Tensor) – a tangent vector of shape (tangent_size(x),).

Returns:

Tensor – the new point, with the shape of x.

minus(y, x)[source]

Tangent vector at x that reaches y: the inverse of plus().

plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Where plus wraps around (rotations by more than pi), minus returns the shortest such vector.

Parameters:
Returns:

Tensor – a tangent vector of shape (tangent_size(x),).

entry_scale(x)[source]

The size each entry of the point x is computed at.

The solvers treat a change of an entry by a few units in the last place of this size as rounding noise, when they decide that a Newton-like step no longer moves x. The default, |x|, suits independent coordinates. Structured points override it: the entries of a rotation matrix all carry an absolute error of about eps, since its rows have norm 1. Optional: override it only for such points.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

Tensor – non-negative sizes, shaped like x.

class piazzi.Euclidean(shape=None)[source]

Bases: Manifold

Plain vector space, with plus(x, delta) = x + delta.

This is what the solvers do without a manifold.

Parameters:

shape (int or sequence of int, optional) – shape of a point. Only needed inside a piazzi.Product, where each block must have a known size. Default: None (any shape)

tangent_size(x)[source]

Number of degrees of freedom at x.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

int – the length of a tangent vector delta at x.

plus(x, delta)[source]

Move x by a tangent vector (the retraction); a zero vector leaves it unchanged.

Parameters:
  • x (torch.Tensor) – a point on the manifold, flattened or in ambient_shape.

  • delta (torch.Tensor) – a tangent vector of shape (tangent_size(x),).

Returns:

Tensor – the new point, with the shape of x.

minus(y, x)[source]

Tangent vector at x that reaches y: the inverse of plus().

plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Where plus wraps around (rotations by more than pi), minus returns the shortest such vector.

Parameters:
Returns:

Tensor – a tangent vector of shape (tangent_size(x),).

class piazzi.Subset(size, constant)[source]

Bases: Manifold

A vector with some entries held fixed, like Ceres Solver’s SubsetManifold.

The tangent vector holds the free entries, in order. Inside a piazzi.Product this fixes part of a block, for example one camera intrinsic.

Parameters:
  • size (int) – number of entries of a point, of shape (size,).

  • constant (sequence of int) – indices of the entries held fixed.

Example:

>>> M = piazzi.Subset(3, constant=[1])
>>> M.plus(torch.zeros(3), torch.tensor([1.0, 2.0]))
tensor([1., 0., 2.])
tangent_size(x)[source]

Number of degrees of freedom at x.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

int – the length of a tangent vector delta at x.

plus(x, delta)[source]

Move x by a tangent vector (the retraction); a zero vector leaves it unchanged.

Parameters:
  • x (torch.Tensor) – a point on the manifold, flattened or in ambient_shape.

  • delta (torch.Tensor) – a tangent vector of shape (tangent_size(x),).

Returns:

Tensor – the new point, with the shape of x.

minus(y, x)[source]

Tangent vector at x that reaches y: the inverse of plus().

plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Where plus wraps around (rotations by more than pi), minus returns the shortest such vector.

Parameters:
Returns:

Tensor – a tangent vector of shape (tangent_size(x),).

class piazzi.SO3(side='right')[source]

Bases: Manifold

Rotations, stored as (3, 3) matrices, with 3 degrees of freedom.

Parameters:

side (str, optional) – "right" gives plus(R, w) = R @ so3_exp(w), a rotation in the body frame. "left" gives so3_exp(w) @ R, a rotation in the world frame (the convention of Ceres Solver’s QuaternionManifold and PyPose). Default: "right"

tangent_size(x)[source]

Number of degrees of freedom at x.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

int – the length of a tangent vector delta at x.

plus(x, delta)[source]

Move x by a tangent vector (the retraction); a zero vector leaves it unchanged.

Parameters:
  • x (torch.Tensor) – a point on the manifold, flattened or in ambient_shape.

  • delta (torch.Tensor) – a tangent vector of shape (tangent_size(x),).

Returns:

Tensor – the new point, with the shape of x.

minus(y, x)[source]

Tangent vector at x that reaches y: the inverse of plus().

plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Where plus wraps around (rotations by more than pi), minus returns the shortest such vector.

Parameters:
Returns:

Tensor – a tangent vector of shape (tangent_size(x),).

entry_scale(x)[source]

The size each entry of the point x is computed at.

The solvers treat a change of an entry by a few units in the last place of this size as rounding noise, when they decide that a Newton-like step no longer moves x. The default, |x|, suits independent coordinates. Structured points override it: the entries of a rotation matrix all carry an absolute error of about eps, since its rows have norm 1. Optional: override it only for such points.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

Tensor – non-negative sizes, shaped like x.

class piazzi.SE3(side='right', retraction='decoupled', pivot=None)[source]

Bases: Manifold

Rigid transforms, stored as (4, 4) matrices [[R, t], [0, 1]], with 6 degrees of freedom.

The tangent vector is delta = (rho, omega): translation first, then a rotation vector, as in Sophus and PyPose. It builds an increment D = [[so3_exp(omega), u], [0, 1]].

Parameters:
  • side (str, optional) – "right" composes T @ D: the increment is expressed in the body frame (the frame T maps from). "left" composes D @ T, in the world frame. Default: "right"

  • retraction (str, optional) – "decoupled" uses u = rho, so rotation and translation update independently. "exp" uses the true SE(3) exponential, u = V(omega) @ rho (a screw motion). Both give the same Jacobian at delta = 0 and the same Gauss-Newton step; they differ only at second order. Default: "decoupled"

  • pivot (torch.Tensor or sequence of float, optional) – a point c of shape (3,) that omega rotates about, in the frame of the increment (the body frame for side="right"). The increment becomes C @ D @ C^-1, with C the translation by c. For registration, put it at the centre of the object (the isocentre): rotating about a far-away origin couples rotation and translation and slows convergence. Default: None (the origin)

Example:

>>> P = torch.randn(10, 3, dtype=torch.float64, generator=torch.Generator().manual_seed(0))
>>> T_true = piazzi.se3_exp(torch.tensor([0.5, -0.2, 0.1, 0.1, 0.2, -0.3], dtype=torch.float64))
>>> Q = P @ T_true[:3, :3].T + T_true[:3, 3]
>>> res = piazzi.least_squares(
...     lambda T: P @ T[:3, :3].T + T[:3, 3] - Q,
...     torch.eye(4, dtype=torch.float64),
...     manifold=piazzi.SE3(pivot=P.mean(0)),
... )
>>> torch.allclose(res.x, T_true)
True
tangent_size(x)[source]

Number of degrees of freedom at x.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

int – the length of a tangent vector delta at x.

plus(x, delta)[source]

Move x by a tangent vector (the retraction); a zero vector leaves it unchanged.

Parameters:
  • x (torch.Tensor) – a point on the manifold, flattened or in ambient_shape.

  • delta (torch.Tensor) – a tangent vector of shape (tangent_size(x),).

Returns:

Tensor – the new point, with the shape of x.

minus(y, x)[source]

Tangent vector at x that reaches y: the inverse of plus().

plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Where plus wraps around (rotations by more than pi), minus returns the shortest such vector.

Parameters:
Returns:

Tensor – a tangent vector of shape (tangent_size(x),).

entry_scale(x)[source]

The size each entry of the point x is computed at.

The solvers treat a change of an entry by a few units in the last place of this size as rounding noise, when they decide that a Newton-like step no longer moves x. The default, |x|, suits independent coordinates. Structured points override it: the entries of a rotation matrix all carry an absolute error of about eps, since its rows have norm 1. Optional: override it only for such points.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

Tensor – non-negative sizes, shaped like x.

class piazzi.Product(*manifolds)[source]

Bases: Manifold

Cartesian product of manifolds, stored as one flat vector with the blocks concatenated.

Use join() to build x0 and split() inside the objective to unpack x, for example a pose plus camera intrinsics.

Parameters:

*manifolds (piazzi.Manifold) – the factors. Each needs a fixed ambient_shape (give piazzi.Euclidean a shape).

Example:

>>> M = piazzi.Product(piazzi.SE3(), piazzi.Euclidean(1))
>>> x0 = M.join(torch.eye(4), torch.ones(1))
>>> x0.shape
torch.Size([17])
>>> pose, scale = M.split(x0)
>>> pose.shape, scale.shape
(torch.Size([4, 4]), torch.Size([1]))
tangent_size(x)[source]

Number of degrees of freedom at x.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

int – the length of a tangent vector delta at x.

split(x)[source]

Unpack a flat point into its blocks.

Parameters:

x (torch.Tensor) – a point of shape ambient_shape.

Returns:

list of Tensor – one block per factor, each in its factor’s ambient_shape.

join(*blocks)[source]

Pack one block per factor into a flat point.

Parameters:

*blocks (torch.Tensor) – one block per factor, of any shape with the factor’s number of elements.

Returns:

Tensor – the flat point, of shape ambient_shape.

plus(x, delta)[source]

Move x by a tangent vector (the retraction); a zero vector leaves it unchanged.

Parameters:
  • x (torch.Tensor) – a point on the manifold, flattened or in ambient_shape.

  • delta (torch.Tensor) – a tangent vector of shape (tangent_size(x),).

Returns:

Tensor – the new point, with the shape of x.

minus(y, x)[source]

Tangent vector at x that reaches y: the inverse of plus().

plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Where plus wraps around (rotations by more than pi), minus returns the shortest such vector.

Parameters:
Returns:

Tensor – a tangent vector of shape (tangent_size(x),).

entry_scale(x)[source]

The size each entry of the point x is computed at.

The solvers treat a change of an entry by a few units in the last place of this size as rounding noise, when they decide that a Newton-like step no longer moves x. The default, |x|, suits independent coordinates. Structured points override it: the entries of a rotation matrix all carry an absolute error of about eps, since its rows have norm 1. Optional: override it only for such points.

Parameters:

x (torch.Tensor) – a point on the manifold.

Returns:

Tensor – non-negative sizes, shaped like x.

Lie-group maps

piazzi.so3_exp(omega)[source]

SO(3) exponential: the rotation matrix of a rotation vector (Rodrigues’ formula).

Values and derivatives (forward and reverse mode) are finite everywhere, including at omega = 0.

Parameters:

omega (torch.Tensor) – rotation vectors (axis times angle, in radians) of shape (..., 3).

Returns:

Tensor – rotation matrices of shape (..., 3, 3).

Example:

>>> R = piazzi.so3_exp(torch.tensor([0.0, 0.0, torch.pi / 2]))  # a quarter turn about z
>>> torch.allclose(R @ torch.tensor([1.0, 0.0, 0.0]), torch.tensor([0.0, 1.0, 0.0]), atol=1e-6)
True
piazzi.so3_log(R)[source]

SO(3) logarithm: the rotation vector of a rotation matrix; inverts piazzi.so3_exp().

Three branches, all evaluated with safe inputs so derivatives stay finite: a series near the identity, theta / sin(theta) * vee(R - R^T) / 2 for moderate angles, and, for angles above 2 pi / 3 (where sin loses precision), the axis from the symmetric part (R + R^T) / 2 - cos(theta) I = (1 - cos(theta)) a a^T.

Parameters:

R (torch.Tensor) – rotation matrices of shape (..., 3, 3).

Returns:

Tensor – rotation vectors of shape (..., 3), with angle in [0, pi].

Example:

>>> omega = torch.tensor([0.1, -0.2, 0.3], dtype=torch.float64)
>>> torch.allclose(piazzi.so3_log(piazzi.so3_exp(omega)), omega)
True
piazzi.so3_hat(omega)[source]

Skew-symmetric (cross-product) matrix of a vector, with hat(w) @ v == cross(w, v).

Parameters:

omega (torch.Tensor) – vectors of shape (..., 3).

Returns:

Tensor – skew-symmetric matrices of shape (..., 3, 3).

piazzi.se3_exp(xi)[source]

SE(3) exponential: the rigid transform of a twist.

The rotation is so3_exp(omega) and the translation V(omega) @ rho (a screw motion), as in Sophus and PyPose.

Parameters:

xi (torch.Tensor) – twists (rho, omega) of shape (..., 6), translation part first.

Returns:

Tensor – rigid transforms [[R, t], [0, 1]] of shape (..., 4, 4).

Example:

>>> xi = torch.tensor([0.1, 0.2, 0.3, 0.0, 0.0, 0.5], dtype=torch.float64)
>>> torch.allclose(piazzi.se3_log(piazzi.se3_exp(xi)), xi)
True
piazzi.se3_log(T)[source]

SE(3) logarithm: the twist of a rigid transform; inverts piazzi.se3_exp().

Parameters:

T (torch.Tensor) – rigid transforms [[R, t], [0, 1]] of shape (..., 4, 4).

Returns:

Tensor – twists (rho, omega) of shape (..., 6), translation part first.