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:
objectBase class for manifolds: a parameter set with a retraction
plus.The solvers take derivatives with respect to a tangent vector
deltaatdelta = 0and apply each step asx <- plus(x, step), as Ceres Solver’sManifolddoes. Subclasses setambient_shapeand implementtangent_size()andplus();minus()is optional.plusmust be differentiable in forward mode (torch.func.jacfwd()). Points may be passed flattened or inambient_shape, andplusreturns the shape it was given.- 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
deltaatx.
- plus(x, delta)[source]¶
Move
xby a tangent vector (the retraction); a zero vector leaves it unchanged.- Parameters:
x (
torch.Tensor) – a point on the manifold, flattened or inambient_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
xthat reachesy: the inverse ofplus().plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Wherepluswraps around (rotations by more than pi),minusreturns the shortest such vector.- Parameters:
y (
torch.Tensor) – the target point.x (
torch.Tensor) – the base point.
- Returns:
Tensor – a tangent vector of shape
(tangent_size(x),).
- entry_scale(x)[source]¶
The size each entry of the point
xis 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 abouteps, 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:
ManifoldPlain vector space, with
plus(x, delta) = x + delta.This is what the solvers do without a manifold.
- Parameters:
shape (
intor sequence ofint, optional) – shape of a point. Only needed inside apiazzi.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
deltaatx.
- plus(x, delta)[source]¶
Move
xby a tangent vector (the retraction); a zero vector leaves it unchanged.- Parameters:
x (
torch.Tensor) – a point on the manifold, flattened or inambient_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
xthat reachesy: the inverse ofplus().plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Wherepluswraps around (rotations by more than pi),minusreturns the shortest such vector.- Parameters:
y (
torch.Tensor) – the target point.x (
torch.Tensor) – the base point.
- Returns:
Tensor – a tangent vector of shape
(tangent_size(x),).
- class piazzi.Subset(size, constant)[source]¶
Bases:
ManifoldA vector with some entries held fixed, like Ceres Solver’s
SubsetManifold.The tangent vector holds the free entries, in order. Inside a
piazzi.Productthis fixes part of a block, for example one camera intrinsic.- Parameters:
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
deltaatx.
- plus(x, delta)[source]¶
Move
xby a tangent vector (the retraction); a zero vector leaves it unchanged.- Parameters:
x (
torch.Tensor) – a point on the manifold, flattened or inambient_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
xthat reachesy: the inverse ofplus().plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Wherepluswraps around (rotations by more than pi),minusreturns the shortest such vector.- Parameters:
y (
torch.Tensor) – the target point.x (
torch.Tensor) – the base point.
- Returns:
Tensor – a tangent vector of shape
(tangent_size(x),).
- class piazzi.SO3(side='right')[source]¶
Bases:
ManifoldRotations, stored as
(3, 3)matrices, with 3 degrees of freedom.- Parameters:
side (
str, optional) –"right"givesplus(R, w) = R @ so3_exp(w), a rotation in the body frame."left"givesso3_exp(w) @ R, a rotation in the world frame (the convention of Ceres Solver’sQuaternionManifoldand 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
deltaatx.
- plus(x, delta)[source]¶
Move
xby a tangent vector (the retraction); a zero vector leaves it unchanged.- Parameters:
x (
torch.Tensor) – a point on the manifold, flattened or inambient_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
xthat reachesy: the inverse ofplus().plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Wherepluswraps around (rotations by more than pi),minusreturns the shortest such vector.- Parameters:
y (
torch.Tensor) – the target point.x (
torch.Tensor) – the base point.
- Returns:
Tensor – a tangent vector of shape
(tangent_size(x),).
- entry_scale(x)[source]¶
The size each entry of the point
xis 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 abouteps, 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:
ManifoldRigid 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 incrementD = [[so3_exp(omega), u], [0, 1]].- Parameters:
side (
str, optional) –"right"composesT @ D: the increment is expressed in the body frame (the frameTmaps from)."left"composesD @ T, in the world frame. Default:"right"retraction (
str, optional) –"decoupled"usesu = 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 atdelta = 0and the same Gauss-Newton step; they differ only at second order. Default:"decoupled"pivot (
torch.Tensoror sequence offloat, optional) – a pointcof shape(3,)thatomegarotates about, in the frame of the increment (the body frame forside="right"). The increment becomesC @ D @ C^-1, withCthe translation byc. 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
deltaatx.
- plus(x, delta)[source]¶
Move
xby a tangent vector (the retraction); a zero vector leaves it unchanged.- Parameters:
x (
torch.Tensor) – a point on the manifold, flattened or inambient_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
xthat reachesy: the inverse ofplus().plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Wherepluswraps around (rotations by more than pi),minusreturns the shortest such vector.- Parameters:
y (
torch.Tensor) – the target point.x (
torch.Tensor) – the base point.
- Returns:
Tensor – a tangent vector of shape
(tangent_size(x),).
- entry_scale(x)[source]¶
The size each entry of the point
xis 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 abouteps, 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:
ManifoldCartesian product of manifolds, stored as one flat vector with the blocks concatenated.
Use
join()to buildx0andsplit()inside the objective to unpackx, for example a pose plus camera intrinsics.- Parameters:
*manifolds (
piazzi.Manifold) – the factors. Each needs a fixedambient_shape(givepiazzi.Euclideana 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
deltaatx.
- split(x)[source]¶
Unpack a flat point into its blocks.
- Parameters:
x (
torch.Tensor) – a point of shapeambient_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
xby a tangent vector (the retraction); a zero vector leaves it unchanged.- Parameters:
x (
torch.Tensor) – a point on the manifold, flattened or inambient_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
xthat reachesy: the inverse ofplus().plus(x, minus(y, x)) == y. Optional: the solvers do not use it, but it measures errors between points. Wherepluswraps around (rotations by more than pi),minusreturns the shortest such vector.- Parameters:
y (
torch.Tensor) – the target point.x (
torch.Tensor) – the base point.
- Returns:
Tensor – a tangent vector of shape
(tangent_size(x),).
- entry_scale(x)[source]¶
The size each entry of the point
xis 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 abouteps, 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) / 2for moderate angles, and, for angles above2 pi / 3(wheresinloses 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 translationV(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.