kornia.geometry.solvers

Module containing various geometrical solvers/optimizers.

Homogeneous Solvers

kornia.geometry.solvers.null_vector_3x4(A)[source]

Return the null vector of a rank-3 matrix of shape \((*, 3, 4)\).

The null vector \(\mathbf{v} \in \mathbb{R}^4\) satisfies \(A\,\mathbf{v} = \mathbf{0}\). For a matrix of rank 3 this solution is unique up to scale.

The computation uses the 4-D cross-product (cofactor expansion): each component of \(\mathbf{v}\) is a \(3 \times 3\) determinant of the submatrix obtained by dropping the corresponding column of \(A\). This is equivalent to computing the last right singular vector of \(A\) via SVD but replaces the SVD with 48 scalar multiplications and 20 additions — no LAPACK or cuSOLVER call is made.

\[v_j = (-1)^j \det\!\bigl(A_{[0,1,2],\,\widehat{j}}\bigr), \quad j = 0, 1, 2, 3\]

where \(A_{[0,1,2],\,\widehat{j}}\) denotes the \(3 \times 3\) submatrix formed by deleting column \(j\).

Note

The returned vector is not normalised. Divide by its norm if a unit null vector is required.

Note

The function is only correct when \(A\) has rank exactly 3. For rank-deficient inputs (rank < 3) the result is the zero vector.

Parameters:

A (Tensor) – matrix of shape \((*, 3, 4)\).

Return type:

Tensor

Returns:

Null vector of shape \((*, 4)\).

Raises:
  • TypeCheckError – if A is not a torch.Tensor.

  • ShapeError – if the last two dimensions of A are not (3, 4).

Example

>>> A = torch.tensor([[[1., 0., 0., 0.],
...                    [0., 1., 0., 0.],
...                    [0., 0., 1., 0.]]])   # null vector is [0,0,0,1]
>>> v = null_vector_3x4(A)                   # shape (1, 4)
>>> (A @ v.unsqueeze(-1)).squeeze(-1)         # should be near zero
tensor([[0., 0., 0.]])

Polynomial Solvers

kornia.geometry.solvers.solve_quadratic(coeffs)[source]

Solve given quadratic equation.

The function takes the coefficients of quadratic equation and returns the real roots.

\[coeffs[0]x^2 + coeffs[1]x + coeffs[2] = 0\]
Parameters:

coeffs (Tensor) – The coefficients of quadratic equation :(B, 3)

Return type:

Tensor

Returns:

A torch.Tensor of shape (B, 2) containing the real roots to the quadratic equation.

Example

>>> coeffs = torch.tensor([[1., 4., 4.]])
>>> roots = solve_quadratic(coeffs)

Note

In cases where a quadratic polynomial has only one real root, the output will be in the format [real_root, 0]. And for the torch.complex roots should be represented as 0. This is done to maintain a consistent output shape for all cases.

kornia.geometry.solvers.solve_cubic(coeffs)[source]

Solve given cubic equation.

The function takes the coefficients of cubic equation and returns the real roots.

\[coeffs[0]x^3 + coeffs[1]x^2 + coeffs[2]x + coeffs[3] = 0\]
Parameters:

coeffs (Tensor) – The coefficients cubic equation : (B, 4)

Return type:

Tensor

Returns:

A torch.Tensor of shape (B, 3) containing the real roots to the cubic equation.

Example

>>> coeffs = torch.tensor([[32., 3., -11., -6.]])
>>> roots = solve_cubic(coeffs)

Note

In cases where a cubic polynomial has only one or two real roots, the output for the non-real roots should be represented as 0. Thus, the output for a single real root should be in the format [real_root, 0, 0], and for two real roots, it should be [real_root_1, real_root_2, 0].

kornia.geometry.solvers.multiply_deg_one_poly(a, b)[source]

Multiply two polynomials of the first order [@nister2004efficient].

Parameters:
  • a (Tensor) – a first order polynomial for variables \((x,y,z,1)\).

  • b (Tensor) – a first order polynomial for variables \((x,y,z,1)\).

Return type:

Tensor

Returns:

degree 2 poly with the order \((x^2, x*y, x*z, x, y^2, y*z, y, z^2, z, 1)\).

kornia.geometry.solvers.multiply_deg_two_one_poly(a, b)[source]

Multiply two polynomials a and b of degrees two and one [@nister2004efficient].

Parameters:
  • a (Tensor) – a second degree poly for variables \((x^2, x*y, x*z, x, y^2, y*z, y, z^2, z, 1)\).

  • b (Tensor) – a first degree poly for variables \((x y z 1)\).

Return type:

Tensor

Returns:

a third degree poly for variables, \((x^3, y^3, x^2*y, x*y^2, x^2*z, x^2, y^2*z, y^2, x*y*z, x*y, x*z^2, x*z, x, y*z^2, y*z, y, z^3, z^2, z, 1)\).

kornia.geometry.solvers.determinant_to_polynomial(A)[source]

Represent the determinant by the 10th polynomial, used for 5PC solver [@nister2004efficient].

Parameters:

A (Tensor) – torch.Tensor \((*, 3, 13)\).

Return type:

Tensor

Returns:

a degree 10 poly, representing determinant (Eqn. 14 in the paper).