Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add differentiable 5PC (five point algorithm) for essential matrix estimation (#2580)

Wei Tongb4cfa130899d
diff --git a/docs/source/geometry.epipolar.rst b/docs/source/geometry.epipolar.rst
index 04db58b40..cc142b4f4 100644
--- a/docs/source/geometry.epipolar.rst
+++ b/docs/source/geometry.epipolar.rst
@@ -10,7 +10,7 @@ Module with useful functionalities for epipolar geometry used by Structure from
Essential
---------
-
+.. autofunction:: find_essential
.. autofunction:: essential_from_fundamental
.. autofunction:: essential_from_Rt
.. autofunction:: decompose_essential_matrix
@@ -53,6 +53,7 @@ Numeric
.. autofunction:: cross_product_matrix
+
Triangulation
-------------
diff --git a/docs/source/geometry.solvers.rst b/docs/source/geometry.solvers.rst
index bad03b22c..313497051 100644
--- a/docs/source/geometry.solvers.rst
+++ b/docs/source/geometry.solvers.rst
@@ -10,3 +10,6 @@ Polynomial Solvers
.. autofunction:: solve_quadratic
.. autofunction:: solve_cubic
+.. autofunction:: multiply_deg_one_poly
+.. autofunction:: multiply_deg_two_one_poly
+.. autofunction:: determinant_to_polynomial
diff --git a/docs/source/references.bib b/docs/source/references.bib
index d3b14ba03..35283e61d 100644
--- a/docs/source/references.bib
+++ b/docs/source/references.bib
@@ -350,3 +350,27 @@ month = {mar}
archivePrefix={arXiv},
primaryClass={eess.IV}
}
+
+@article{nister2004efficient,
+ author={Nist{\'e}r, David},
+ journal={IEEE Transactions on Pattern Analysis and Machine Intelligence},
+ title={An efficient solution to the five-point relative pose problem},
+ year={2004},
+ volume={26},
+ number={6},
+ pages={756-770}
+}
+
+@inproceedings{barath2020magsac++,
+ title={MAGSAC++, a fast, reliable and accurate robust estimator},
+ author={Barath, Daniel and Noskova, Jana and Ivashechkin, Maksym and Matas, Jiri},
+ booktitle={CVPR},
+ year={2020}
+}
+
+@inproceedings{wei2023generalized,
+ author = {Wei, Tong and Patel, Yash and Shekhovtsov, Alexander and Matas, Jiri and Barath, Daniel},
+ title = {Generalized Differentiable RANSAC},
+ booktitle = {ICCV},
+ year = {2023}
+}
diff --git a/kornia/geometry/epipolar/__init__.py b/kornia/geometry/epipolar/__init__.py
index a521a9c6a..886367716 100644
--- a/kornia/geometry/epipolar/__init__.py
+++ b/kornia/geometry/epipolar/__init__.py
@@ -8,6 +8,7 @@ from .essential import (
decompose_essential_matrix,
essential_from_fundamental,
essential_from_Rt,
+ find_essential,
motion_from_essential,
motion_from_essential_choose_solution,
relative_camera_motion,
@@ -64,4 +65,5 @@ __all__ = [
"triangulate_points",
"get_perpendicular",
"get_closest_point_on_epipolar_line",
+ "find_essential",
]
diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.py
index 348ef925c..b9b84e0e7 100644
--- a/kornia/geometry/epipolar/essential.py
+++ b/kornia/geometry/epipolar/essential.py
@@ -3,6 +3,9 @@ from typing import Optional, Tuple
import torch
+import kornia.geometry.epipolar as epi
+from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SAME_SHAPE, KORNIA_CHECK_SHAPE
+from kornia.geometry import solvers
from kornia.utils import eye_like, vec_like
from kornia.utils.helpers import _torch_svd_cast
@@ -11,6 +14,7 @@ from .projection import depth_from_point, projection_from_KRt
from .triangulation import triangulate_points
__all__ = [
+ "find_essential",
"essential_from_fundamental",
"decompose_essential_matrix",
"essential_from_Rt",
@@ -20,6 +24,184 @@ __all__ = [
]
+def run_5point(points1: torch.Tensor, points2: torch.Tensor, weights: Optional[torch.Tensor] = None) -> torch.Tensor:
+ r"""Compute the essential matrix using the 5-point algorithm from Nister.
+
+ The linear system is solved by Nister's 5-point algorithm [@nister2004efficient],
+ and the solver implemented referred to [@barath2020magsac++][@wei2023generalized].
+
+ Args:
+ points1: A set of carlibrated points in the first image with a tensor shape :math:`(B, N, 2), N>=8`.
+ points2: A set of points in the second image with a tensor shape :math:`(B, N, 2), N>=8`.
+ weights: Tensor containing the weights per point correspondence with a shape of :math:`(B, N)`.
+
+ Returns:
+ the computed essential matrix with shape :math:`(B, 3, 3)`.
+ """
+ KORNIA_CHECK_SHAPE(points1, ['B', 'N', '2'])
+ KORNIA_CHECK_SAME_SHAPE(points1, points2)
+ KORNIA_CHECK(points1.shape[1] >= 5, "Number of points should be >=5")
+ if weights is not None:
+ KORNIA_CHECK_SAME_SHAPE(points1[:, :, 0], weights)
+
+ batch_size, _, _ = points1.shape
+ x1, y1 = torch.chunk(points1, dim=-1, chunks=2) # Bx1xN
+ x2, y2 = torch.chunk(points2, dim=-1, chunks=2) # Bx1xN
+ ones = torch.ones_like(x1)
+
+ # build equations system and find null space.
+ # https://www.cc.gatech.edu/~afb/classes/CS4495-Fall2013/slides/CS4495-09-TwoViews-2.pdf
+ # [x * x', x * y', x, y * x', y * y', y, x', y', 1]
+ # BxNx9
+ X = torch.cat([x1 * x2, x1 * y2, x1, y1 * x2, y1 * y2, y1, x2, y2, ones], dim=-1)
+
+ # apply the weights to the linear system
+ if weights is None:
+ X = X.transpose(-2, -1) @ X
+ else:
+ w_diag = torch.diag_embed(weights)
+ X = X.transpose(-2, -1) @ w_diag @ X
+ # compute eigevectors and retrieve the one with the smallest eigenvalue, using SVD
+ # turn off the grad check due to the unstable gradients from SVD.
+ # several close to zero values of eigenvalues.
+ _, _, V = _torch_svd_cast(X) # torch.svd
+ null_ = V[:, :, -4:] # the last four rows
+ nullSpace = V.transpose(-1, -2)[:, -4:, :]
+
+ coeffs = torch.zeros(batch_size, 10, 20, device=null_.device, dtype=null_.dtype)
+ d = torch.zeros(batch_size, 60, device=null_.device, dtype=null_.dtype)
+
+ def fun(i: int, j: int) -> torch.Tensor:
+ return null_[:, 3 * j + i]
+
+ # Determinant constraint
+ coeffs[:, 9] = (
+ solvers.multiply_deg_two_one_poly(
+ solvers.multiply_deg_one_poly(fun(0, 1), fun(1, 2)) - solvers.multiply_deg_one_poly(fun(0, 2), fun(1, 1)),
+ fun(2, 0),
+ )
+ + solvers.multiply_deg_two_one_poly(
+ solvers.multiply_deg_one_poly(fun(0, 2), fun(1, 0)) - solvers.multiply_deg_one_poly(fun(0, 0), fun(1, 2)),
+ fun(2, 1),
+ )
+ + solvers.multiply_deg_two_one_poly(
+ solvers.multiply_deg_one_poly(fun(0, 0), fun(1, 1)) - solvers.multiply_deg_one_poly(fun(0, 1), fun(1, 0)),
+ fun(2, 2),
+ )
+ )
+
+ indices = torch.tensor([[0, 10, 20], [10, 40, 30], [20, 30, 50]])
+
+ # Compute EE^T (Eqn. 20 in the paper)
+ for i in range(3):
+ for j in range(3):
+ d[:, indices[i, j] : indices[i, j] + 10] = (
+ solvers.multiply_deg_one_poly(fun(i, 0), fun(j, 0))
+ + solvers.multiply_deg_one_poly(fun(i, 1), fun(j, 1))
+ + solvers.multiply_deg_one_poly(fun(i, 2), fun(j, 2))
+ )
+
+ for i in range(10):
+ t = 0.5 * (d[:, indices[0, 0] + i] + d[:, indices[1, 1] + i] + d[:, indices[2, 2] + i])
+ d[:, indices[0, 0] + i] -= t
+ d[:, indices[1, 1] + i] -= t
+ d[:, indices[2, 2] + i] -= t
+
+ cnt = 0
+ for i in range(3):
+ for j in range(3):
+ row = (
+ solvers.multiply_deg_two_one_poly(d[:, indices[i, 0] : indices[i, 0] + 10], fun(0, j))
+ + solvers.multiply_deg_two_one_poly(d[:, indices[i, 1] : indices[i, 1] + 10], fun(1, j))
+ + solvers.multiply_deg_two_one_poly(d[:, indices[i, 2] : indices[i, 2] + 10], fun(2, j))
+ )
+ coeffs[:, cnt] = row
+ cnt += 1
+
+ b = coeffs[:, :, 10:]
+ singular_filter = torch.linalg.matrix_rank(coeffs[:, :, :10]) >= torch.max(
+ torch.linalg.matrix_rank(coeffs), torch.ones_like(torch.linalg.matrix_rank(coeffs[:, :, :10])) * 10
+ )
+
+ eliminated_mat = torch.linalg.solve(coeffs[singular_filter, :, :10], b[singular_filter])
+
+ coeffs_ = torch.cat((coeffs[singular_filter, :, :10], eliminated_mat), dim=-1)
+
+ A = torch.zeros(coeffs_.shape[0], 3, 13, device=coeffs_.device, dtype=coeffs_.dtype)
+
+ for i in range(3):
+ A[:, i, 0] = 0.0
+ A[:, i : i + 1, 1:4] = coeffs_[:, 4 + 2 * i : 5 + 2 * i, 10:13]
+ A[:, i : i + 1, 0:3] -= coeffs_[:, 5 + 2 * i : 6 + 2 * i, 10:13]
+ A[:, i, 4] = 0.0
+ A[:, i : i + 1, 5:8] = coeffs_[:, 4 + 2 * i : 5 + 2 * i, 13:16]
+ A[:, i : i + 1, 4:7] -= coeffs_[:, 5 + 2 * i : 6 + 2 * i, 13:16]
+ A[:, i, 8] = 0.0
+ A[:, i : i + 1, 9:13] = coeffs_[:, 4 + 2 * i : 5 + 2 * i, 16:20]
+ A[:, i : i + 1, 8:12] -= coeffs_[:, 5 + 2 * i : 6 + 2 * i, 16:20]
+
+ cs = solvers.determinant_to_polynomial(A)
+ E_models = []
+
+ # for loop because of different numbers of solutions
+ for bi in range(A.shape[0]):
+ A_i = A[bi]
+ null_i = nullSpace[bi]
+
+ # companion matrix solver for polynomial
+ C = torch.zeros((10, 10), device=cs.device, dtype=cs.dtype)
+ C[0:-1, 1:] = torch.eye(C[0:-1, 0:-1].shape[0], device=cs.device, dtype=cs.dtype)
+ C[-1, :] = -cs[bi][:-1] / cs[bi][-1]
+
+ roots = torch.real(torch.linalg.eigvals(C))
+
+ if roots is None:
+ continue
+ n_sols = roots.size()
+ if n_sols == 0:
+ continue
+ Bs = torch.stack(
+ (
+ A_i[:3, :1] * (roots**3) + A_i[:3, 1:2] * roots.square() + A_i[0:3, 2:3] * (roots) + A_i[0:3, 3:4],
+ A_i[0:3, 4:5] * (roots**3) + A_i[0:3, 5:6] * roots.square() + A_i[0:3, 6:7] * (roots) + A_i[0:3, 7:8],
+ ),
+ dim=0,
+ ).transpose(0, -1)
+
+ bs = (
+ A_i[0:3, 8:9] * (roots**4)
+ + A_i[0:3, 9:10] * (roots**3)
+ + A_i[0:3, 10:11] * roots.square()
+ + A_i[0:3, 11:12] * roots
+ + A_i[0:3, 12:13]
+ ).T.unsqueeze(-1)
+
+ # We try to solve using top two rows,
+ xzs = Bs[:, 0:2, 0:2].inverse() @ (bs[:, 0:2])
+
+ mask = (abs(Bs[:, 2].unsqueeze(1) @ xzs - bs[:, 2].unsqueeze(1)) > 1e-3).flatten()
+ if torch.sum(mask) != 0:
+ q, r = torch.linalg.qr(Bs[mask].clone()) #
+ xzs[mask] = torch.linalg.solve(r, q.transpose(-1, -2) @ bs[mask]) # [mask]
+
+ # models
+ Es = null_i[0] * (-xzs[:, 0]) + null_i[1] * (-xzs[:, 1]) + null_i[2] * roots.unsqueeze(-1) + null_i[3]
+
+ # Since the rows of N are orthogonal unit vectors, we can normalize the coefficients instead
+ inv = 1.0 / torch.sqrt((-xzs[:, 0]) ** 2 + (-xzs[:, 1]) ** 2 + roots.unsqueeze(-1) ** 2 + 1.0)
+ Es *= inv
+ if Es.shape[0] < 10:
+ Es = torch.cat(
+ (Es.clone(), torch.eye(3, device=Es.device, dtype=Es.dtype).repeat(10 - Es.shape[0], 1).reshape(-1, 9))
+ )
+ E_models.append(Es)
+
+ # if not E_models:
+ # return torch.eye(3, device=cs.device, dtype=cs.dtype).unsqueeze(0)
+ # else:
+ return torch.cat(E_models).view(-1, 3, 3).transpose(-1, -2)
+
+
def essential_from_fundamental(F_mat: torch.Tensor, K1: torch.Tensor, K2: torch.Tensor) -> torch.Tensor:
r"""Get Essential matrix from Fundamental and Camera matrices.
@@ -292,3 +474,38 @@ def relative_camera_motion(
t = t2 - R @ t1
return (R, t)
+
+
+def find_essential(
+ points1: torch.Tensor, points2: torch.Tensor, weights: Optional[torch.Tensor] = None
+) -> torch.Tensor:
+ r"""
+ Args:
+ points1: A set of points in the first image with a tensor shape :math:`(B, N, 2), N>=5`.
+ points2: A set of points in the second image with a tensor shape :math:`(B, N, 2), N>=5`.
+ weights: Tensor containing the weights per point correspondence with a shape of :math:`(5, N)`.
+
+ Returns:
+ the computed essential matrix with shape :math:`(B, 3, 3)`,
+ one model for each batch selected out of ten solutions by Sampson distances.
+
+ """
+ E = run_5point(points1, points2, weights).to(points1.dtype)
+
+ # select one out of 10 possible solutions from 5PC Nister solver.
+ solution_num = 10
+ batch_size = points1.shape[0]
+
+ error = torch.zeros((batch_size, solution_num))
+
+ for b in range(batch_size):
+ error[b] = epi.sampson_epipolar_distance(points1[b], points2[b], E.view(batch_size, solution_num, 3, 3)[b]).sum(
+ -1
+ )
+
+ KORNIA_CHECK_SHAPE(error, ['f{batch_size}', '10'])
+
+ chosen_indices = torch.argmin(error, dim=-1)
+ result = torch.stack([(E.view(-1, solution_num, 3, 3))[i, chosen_indices[i], :] for i in range(batch_size)])
+
+ return result
diff --git a/kornia/geometry/solvers/__init__.py b/kornia/geometry/solvers/__init__.py
index ad2a5bbad..ffa142157 100644
--- a/kornia/geometry/solvers/__init__.py
+++ b/kornia/geometry/solvers/__init__.py
@@ -1,3 +1,15 @@
-from .polynomial_solver import solve_cubic, solve_quadratic
+from .polynomial_solver import (
+ determinant_to_polynomial,
+ multiply_deg_one_poly,
+ multiply_deg_two_one_poly,
+ solve_cubic,
+ solve_quadratic,
+)
-__all__ = ["solve_quadratic", "solve_cubic"]
+__all__ = [
+ "multiply_deg_one_poly",
+ "multiply_deg_two_one_poly",
+ "determinant_to_polynomial",
+ "solve_cubic",
+ "solve_quadratic",
+]
diff --git a/kornia/geometry/solvers/polynomial_solver.py b/kornia/geometry/solvers/polynomial_solver.py
index 4d4caee0f..f346fc605 100644
--- a/kornia/geometry/solvers/polynomial_solver.py
+++ b/kornia/geometry/solvers/polynomial_solver.py
@@ -198,3 +198,611 @@ def solve_cubic(coeffs: Tensor) -> Tensor:
# def solve_quartic(coeffs: Tensor) -> Tensor:
# TODO: Quartic equation solver
# return solutions
+
+
+# Reference
+# https://github.com/danini/graph-cut-ransac/blob/master/src/pygcransac/include/
+# estimators/solver_essential_matrix_five_point_nister.h#L108
+
+
+def multiply_deg_one_poly(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
+ r"""Multiply two polynomials of the first order [@nister2004efficient].
+
+ Args:
+ a: a first order polynomial for variables :math:`(x,y,z,1)`.
+ b: a first order polynomial for variables :math:`(x,y,z,1)`.
+
+ Returns:
+ degree 2 poly with the order :math:`(x^2, x*y, x*z, x, y^2, y*z, y, z^2, z, 1)`.
+ """
+
+ return torch.stack(
+ [
+ a[:, 0] * b[:, 0],
+ a[:, 0] * b[:, 1] + a[:, 1] * b[:, 0],
+ a[:, 0] * b[:, 2] + a[:, 2] * b[:, 0],
+ a[:, 0] * b[:, 3] + a[:, 3] * b[:, 0],
+ a[:, 1] * b[:, 1],
+ a[:, 1] * b[:, 2] + a[:, 2] * b[:, 1],
+ a[:, 1] * b[:, 3] + a[:, 3] * b[:, 1],
+ a[:, 2] * b[:, 2],
+ a[:, 2] * b[:, 3] + a[:, 3] * b[:, 2],
+ a[:, 3] * b[:, 3],
+ ],
+ dim=-1,
+ )
+
+
+# Reference
+# https://github.com/danini/graph-cut-ransac/blob/aae1f40c2e10e31fd2191bac601c53a189673f60/src/pygcransac/
+# include/estimators/solver_essential_matrix_five_point_nister.h#L156
+
+
+def multiply_deg_two_one_poly(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
+ r"""Multiply two polynomials a and b of degrees two and one [@nister2004efficient].
+
+ Args:
+ a: a second degree poly for variables :math:`(x^2, x*y, x*z, x, y^2, y*z, y, z^2, z, 1)`.
+ b: a first degree poly for variables :math:`(x y z 1)`.
+
+ Returns:
+ a third degree poly for variables,
+ :math:`(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)`.
+ """
+
+ return torch.stack(
+ [
+ a[:, 0] * b[:, 0],
+ a[:, 4] * b[:, 1],
+ a[:, 0] * b[:, 1] + a[:, 1] * b[:, 0],
+ a[:, 1] * b[:, 1] + a[:, 4] * b[:, 0],
+ a[:, 0] * b[:, 2] + a[:, 2] * b[:, 0],
+ a[:, 0] * b[:, 3] + a[:, 3] * b[:, 0],
+ a[:, 4] * b[:, 2] + a[:, 5] * b[:, 1],
+ a[:, 4] * b[:, 3] + a[:, 6] * b[:, 1],
+ a[:, 1] * b[:, 2] + a[:, 2] * b[:, 1] + a[:, 5] * b[:, 0],
+ a[:, 1] * b[:, 3] + a[:, 3] * b[:, 1] + a[:, 6] * b[:, 0],
+ a[:, 2] * b[:, 2] + a[:, 7] * b[:, 0],
+ a[:, 2] * b[:, 3] + a[:, 3] * b[:, 2] + a[:, 8] * b[:, 0],
+ a[:, 3] * b[:, 3] + a[:, 9] * b[:, 0],
+ a[:, 5] * b[:, 2] + a[:, 7] * b[:, 1],
+ a[:, 5] * b[:, 3] + a[:, 6] * b[:, 2] + a[:, 8] * b[:, 1],
+ a[:, 6] * b[:, 3] + a[:, 9] * b[:, 1],
+ a[:, 7] * b[:, 2],
+ a[:, 7] * b[:, 3] + a[:, 8] * b[:, 2],
+ a[:, 8] * b[:, 3] + a[:, 9] * b[:, 2],
+ a[:, 9] * b[:, 3],
+ ],
+ dim=-1,
+ )
+
+
+# Compute degree 10 poly representing determinant (equation 14 in the paper)
+# https://github.com/danini/graph-cut-ransac/blob/aae1f40c2e10e31fd2191bac601c53a189673f60/src/pygcransac/
+# include/estimators/solver_essential_matrix_five_point_nister.h#L368C5-L368C82
+def determinant_to_polynomial(A: Tensor) -> Tensor:
+ r"""Represent the determinant by the 10th polynomial, used for 5PC solver [@nister2004efficient].
+
+ Args:
+ A: Tensor :math:`(*, 3, 13)`.
+
+ Returns:
+ a degree 10 poly, representing determinant (Eqn. 14 in the paper).
+ """
+
+ cs = torch.zeros(A.shape[0], 11, device=A.device, dtype=A.dtype)
+ cs[:, 0] = (
+ A[:, 0, 12] * A[:, 1, 3] * A[:, 2, 7]
+ - A[:, 0, 12] * A[:, 1, 7] * A[:, 2, 3]
+ - A[:, 0, 3] * A[:, 2, 7] * A[:, 1, 12]
+ + A[:, 0, 7] * A[:, 2, 3] * A[:, 1, 12]
+ + A[:, 0, 3] * A[:, 1, 7] * A[:, 2, 12]
+ - A[:, 0, 7] * A[:, 1, 3] * A[:, 2, 12]
+ )
+
+ cs[:, 1] = (
+ A[:, 0, 11] * A[:, 1, 3] * A[:, 2, 7]
+ - A[:, 0, 11] * A[:, 1, 7] * A[:, 2, 3]
+ + A[:, 0, 12] * A[:, 1, 2] * A[:, 2, 7]
+ + A[:, 0, 12] * A[:, 1, 3] * A[:, 2, 6]
+ - A[:, 0, 12] * A[:, 1, 6] * A[:, 2, 3]
+ - A[:, 0, 12] * A[:, 1, 7] * A[:, 2, 2]
+ - A[:, 0, 2] * A[:, 2, 7] * A[:, 1, 12]
+ - A[:, 0, 3] * A[:, 2, 6] * A[:, 1, 12]
+ - A[:, 0, 3] * A[:, 2, 7] * A[:, 1, 11]
+ + A[:, 0, 6] * A[:, 2, 3] * A[:, 1, 12]
+ + A[:, 0, 7] * A[:, 2, 2] * A[:, 1, 12]
+ + A[:, 0, 7] * A[:, 2, 3] * A[:, 1, 11]
+ + A[:, 0, 2] * A[:, 1, 7] * A[:, 2, 12]
+ + A[:, 0, 3] * A[:, 1, 6] * A[:, 2, 12]
+ + A[:, 0, 3] * A[:, 1, 7] * A[:, 2, 11]
+ - A[:, 0, 6] * A[:, 1, 3] * A[:, 2, 12]
+ - A[:, 0, 7] * A[:, 1, 2] * A[:, 2, 12]
+ - A[:, 0, 7] * A[:, 1, 3] * A[:, 2, 11]
+ )
+
+ cs[:, 2] = (
+ A[:, 0, 10] * A[:, 1, 3] * A[:, 2, 7]
+ - A[:, 0, 10] * A[:, 1, 7] * A[:, 2, 3]
+ + A[:, 0, 11] * A[:, 1, 2] * A[:, 2, 7]
+ + A[:, 0, 11] * A[:, 1, 3] * A[:, 2, 6]
+ - A[:, 0, 11] * A[:, 1, 6] * A[:, 2, 3]
+ - A[:, 0, 11] * A[:, 1, 7] * A[:, 2, 2]
+ + A[:, 1, 1] * A[:, 0, 12] * A[:, 2, 7]
+ + A[:, 0, 12] * A[:, 1, 2] * A[:, 2, 6]
+ + A[:, 0, 12] * A[:, 1, 3] * A[:, 2, 5]
+ - A[:, 0, 12] * A[:, 1, 5] * A[:, 2, 3]
+ - A[:, 0, 12] * A[:, 1, 6] * A[:, 2, 2]
+ - A[:, 0, 12] * A[:, 1, 7] * A[:, 2, 1]
+ - A[:, 0, 1] * A[:, 2, 7] * A[:, 1, 12]
+ - A[:, 0, 2] * A[:, 2, 6] * A[:, 1, 12]
+ - A[:, 0, 2] * A[:, 2, 7] * A[:, 1, 11]
+ - A[:, 0, 3] * A[:, 2, 5] * A[:, 1, 12]
+ - A[:, 0, 3] * A[:, 2, 6] * A[:, 1, 11]
+ - A[:, 0, 3] * A[:, 2, 7] * A[:, 1, 10]
+ + A[:, 0, 5] * A[:, 2, 3] * A[:, 1, 12]
+ + A[:, 0, 6] * A[:, 2, 2] * A[:, 1, 12]
+ + A[:, 0, 6] * A[:, 2, 3] * A[:, 1, 11]
+ + A[:, 0, 7] * A[:, 2, 1] * A[:, 1, 12]
+ + A[:, 0, 7] * A[:, 2, 2] * A[:, 1, 11]
+ + A[:, 0, 7] * A[:, 2, 3] * A[:, 1, 10]
+ + A[:, 0, 1] * A[:, 1, 7] * A[:, 2, 12]
+ + A[:, 0, 2] * A[:, 1, 6] * A[:, 2, 12]
+ + A[:, 0, 2] * A[:, 1, 7] * A[:, 2, 11]
+ + A[:, 0, 3] * A[:, 1, 5] * A[:, 2, 12]
+ + A[:, 0, 3] * A[:, 1, 6] * A[:, 2, 11]
+ + A[:, 0, 3] * A[:, 1, 7] * A[:, 2, 10]
+ - A[:, 0, 5] * A[:, 1, 3] * A[:, 2, 12]
+ - A[:, 0, 6] * A[:, 1, 2] * A[:, 2, 12]
+ - A[:, 0, 6] * A[:, 1, 3] * A[:, 2, 11]
+ - A[:, 0, 7] * A[:, 1, 1] * A[:, 2, 12]
+ - A[:, 0, 7] * A[:, 1, 2] * A[:, 2, 11]
+ - A[:, 0, 7] * A[:, 1, 3] * A[:, 2, 10]
+ )
+
+ cs[:, 3] = (
+ A[:, 0, 3] * A[:, 1, 7] * A[:, 2, 9]
+ - A[:, 0, 3] * A[:, 1, 9] * A[:, 2, 7]
+ - A[:, 0, 7] * A[:, 1, 3] * A[:, 2, 9]
+ + A[:, 0, 7] * A[:, 1, 9] * A[:, 2, 3]
+ + A[:, 0, 9] * A[:, 1, 3] * A[:, 2, 7]
+ - A[:, 0, 9] * A[:, 1, 7] * A[:, 2, 3]
+ + A[:, 0, 10] * A[:, 1, 2] * A[:, 2, 7]
+ + A[:, 0, 10] * A[:, 1, 3] * A[:, 2, 6]
+ - A[:, 0, 10] * A[:, 1, 6] * A[:, 2, 3]
+ - A[:, 0, 10] * A[:, 1, 7] * A[:, 2, 2]
+ + A[:, 1, 0] * A[:, 0, 12] * A[:, 2, 7]
+ + A[:, 0, 11] * A[:, 1, 1] * A[:, 2, 7]
+ + A[:, 0, 11] * A[:, 1, 2] * A[:, 2, 6]
+ + A[:, 0, 11] * A[:, 1, 3] * A[:, 2, 5]
+ - A[:, 0, 11] * A[:, 1, 5] * A[:, 2, 3]
+ - A[:, 0, 11] * A[:, 1, 6] * A[:, 2, 2]
+ - A[:, 0, 11] * A[:, 1, 7] * A[:, 2, 1]
+ + A[:, 1, 1] * A[:, 0, 12] * A[:, 2, 6]
+ + A[:, 0, 12] * A[:, 1, 2] * A[:, 2, 5]
+ + A[:, 0, 12] * A[:, 1, 3] * A[:, 2, 4]
+ - A[:, 0, 12] * A[:, 1, 4] * A[:, 2, 3]
+ - A[:, 0, 12] * A[:, 1, 5] * A[:, 2, 2]
+ - A[:, 0, 12] * A[:, 1, 6] * A[:, 2, 1]
+ - A[:, 0, 12] * A[:, 1, 7] * A[:, 2, 0]
+ - A[:, 0, 0] * A[:, 2, 7] * A[:, 1, 12]
+ - A[:, 0, 1] * A[:, 2, 6] * A[:, 1, 12]
+ - A[:, 0, 1] * A[:, 2, 7] * A[:, 1, 11]
+ - A[:, 0, 2] * A[:, 2, 5] * A[:, 1, 12]
+ - A[:, 0, 2] * A[:, 2, 6] * A[:, 1, 11]
+ - A[:, 0, 2] * A[:, 2, 7] * A[:, 1, 10]
+ - A[:, 0, 3] * A[:, 2, 4] * A[:, 1, 12]
+ - A[:, 0, 3] * A[:, 2, 5] * A[:, 1, 11]
+ - A[:, 0, 3] * A[:, 2, 6] * A[:, 1, 10]
+ + A[:, 0, 4] * A[:, 2, 3] * A[:, 1, 12]
+ + A[:, 0, 5] * A[:, 2, 2] * A[:, 1, 12]
+ + A[:, 0, 5] * A[:, 2, 3] * A[:, 1, 11]
+ + A[:, 0, 6] * A[:, 2, 1] * A[:, 1, 12]
+ + A[:, 0, 6] * A[:, 2, 2] * A[:, 1, 11]
+ + A[:, 0, 6] * A[:, 2, 3] * A[:, 1, 10]
+ + A[:, 0, 7] * A[:, 2, 0] * A[:, 1, 12]
+ + A[:, 0, 7] * A[:, 2, 1] * A[:, 1, 11]
+ + A[:, 0, 7] * A[:, 2, 2] * A[:, 1, 10]
+ + A[:, 0, 0] * A[:, 1, 7] * A[:, 2, 12]
+ + A[:, 0, 1] * A[:, 1, 6] * A[:, 2, 12]
+ + A[:, 0, 1] * A[:, 1, 7] * A[:, 2, 11]
+ + A[:, 0, 2] * A[:, 1, 5] * A[:, 2, 12]
+ + A[:, 0, 2] * A[:, 1, 6] * A[:, 2, 11]
+ + A[:, 0, 2] * A[:, 1, 7] * A[:, 2, 10]
+ + A[:, 0, 3] * A[:, 1, 4] * A[:, 2, 12]
+ + A[:, 0, 3] * A[:, 1, 5] * A[:, 2, 11]
+ + A[:, 0, 3] * A[:, 1, 6] * A[:, 2, 10]
+ - A[:, 0, 4] * A[:, 1, 3] * A[:, 2, 12]
+ - A[:, 0, 5] * A[:, 1, 2] * A[:, 2, 12]
+ - A[:, 0, 5] * A[:, 1, 3] * A[:, 2, 11]
+ - A[:, 0, 6] * A[:, 1, 1] * A[:, 2, 12]
+ - A[:, 0, 6] * A[:, 1, 2] * A[:, 2, 11]
+ - A[:, 0, 6] * A[:, 1, 3] * A[:, 2, 10]
+ - A[:, 0, 7] * A[:, 1, 0] * A[:, 2, 12]
+ - A[:, 0, 7] * A[:, 1, 1] * A[:, 2, 11]
+ - A[:, 0, 7] * A[:, 1, 2] * A[:, 2, 10]
+ )
+
+ cs[:, 4] = (
+ A[:, 0, 2] * A[:, 1, 7] * A[:, 2, 9]
+ - A[:, 0, 2] * A[:, 1, 9] * A[:, 2, 7]
+ + A[:, 0, 3] * A[:, 1, 6] * A[:, 2, 9]
+ + A[:, 0, 3] * A[:, 1, 7] * A[:, 2, 8]
+ - A[:, 0, 3] * A[:, 1, 8] * A[:, 2, 7]
+ - A[:, 0, 3] * A[:, 1, 9] * A[:, 2, 6]
+ - A[:, 0, 6] * A[:, 1, 3] * A[:, 2, 9]
+ + A[:, 0, 6] * A[:, 1, 9] * A[:, 2, 3]
+ - A[:, 0, 7] * A[:, 1, 2] * A[:, 2, 9]
+ - A[:, 0, 7] * A[:, 1, 3] * A[:, 2, 8]
+ + A[:, 0, 7] * A[:, 1, 8] * A[:, 2, 3]
+ + A[:, 0, 7] * A[:, 1, 9] * A[:, 2, 2]
+ + A[:, 0, 8] * A[:, 1, 3] * A[:, 2, 7]
+ - A[:, 0, 8] * A[:, 1, 7] * A[:, 2, 3]
+ + A[:, 0, 9] * A[:, 1, 2] * A[:, 2, 7]
+ + A[:, 0, 9] * A[:, 1, 3] * A[:, 2, 6]
+ - A[:, 0, 9] * A[:, 1, 6] * A[:, 2, 3]
+ - A[:, 0, 9] * A[:, 1, 7] * A[:, 2, 2]
+ + A[:, 0, 10] * A[:, 1, 1] * A[:, 2, 7]
+ + A[:, 0, 10] * A[:, 1, 2] * A[:, 2, 6]
+ + A[:, 0, 10] * A[:, 1, 3] * A[:, 2, 5]
+ - A[:, 0, 10] * A[:, 1, 5] * A[:, 2, 3]
+ - A[:, 0, 10] * A[:, 1, 6] * A[:, 2, 2]
+ - A[:, 0, 10] * A[:, 1, 7] * A[:, 2, 1]
+ + A[:, 1, 0] * A[:, 0, 11] * A[:, 2, 7]
+ + A[:, 1, 0] * A[:, 0, 12] * A[:, 2, 6]
+ + A[:, 0, 11] * A[:, 1, 1] * A[:, 2, 6]
+ + A[:, 0, 11] * A[:, 1, 2] * A[:, 2, 5]
+ + A[:, 0, 11] * A[:, 1, 3] * A[:, 2, 4]
+ - A[:, 0, 11] * A[:, 1, 4] * A[:, 2, 3]
+ - A[:, 0, 11] * A[:, 1, 5] * A[:, 2, 2]
+ - A[:, 0, 11] * A[:, 1, 6] * A[:, 2, 1]
+ - A[:, 0, 11] * A[:, 1, 7] * A[:, 2, 0]
+ + A[:, 1, 1] * A[:, 0, 12] * A[:, 2, 5]
+ + A[:, 0, 12] * A[:, 1, 2] * A[:, 2, 4]
+ - A[:, 0, 12] * A[:, 1, 4] * A[:, 2, 2]
+ - A[:, 0, 12] * A[:, 1, 5] * A[:, 2, 1]
+ - A[:, 0, 12] * A[:, 1, 6] * A[:, 2, 0]
+ - A[:, 0, 0] * A[:, 2, 6] * A[:, 1, 12]
+ - A[:, 0, 0] * A[:, 2, 7] * A[:, 1, 11]
+ - A[:, 0, 1] * A[:, 2, 5] * A[:, 1, 12]
+ - A[:, 0, 1] * A[:, 2, 6] * A[:, 1, 11]
+ - A[:, 0, 1] * A[:, 2, 7] * A[:, 1, 10]
+ - A[:, 0, 2] * A[:, 2, 4] * A[:, 1, 12]
+ - A[:, 0, 2] * A[:, 2, 5] * A[:, 1, 11]
+ - A[:, 0, 2] * A[:, 2, 6] * A[:, 1, 10]
+ - A[:, 0, 3] * A[:, 2, 4] * A[:, 1, 11]
+ - A[:, 0, 3] * A[:, 2, 5] * A[:, 1, 10]
+ + A[:, 0, 4] * A[:, 2, 2] * A[:, 1, 12]
+ + A[:, 0, 4] * A[:, 2, 3] * A[:, 1, 11]
+ + A[:, 0, 5] * A[:, 2, 1] * A[:, 1, 12]
+ + A[:, 0, 5] * A[:, 2, 2] * A[:, 1, 11]
+ + A[:, 0, 5] * A[:, 2, 3] * A[:, 1, 10]
+ + A[:, 0, 6] * A[:, 2, 0] * A[:, 1, 12]
+ + A[:, 0, 6] * A[:, 2, 1] * A[:, 1, 11]
+ + A[:, 0, 6] * A[:, 2, 2] * A[:, 1, 10]
+ + A[:, 0, 7] * A[:, 2, 0] * A[:, 1, 11]
+ + A[:, 0, 7] * A[:, 2, 1] * A[:, 1, 10]
+ + A[:, 0, 0] * A[:, 1, 6] * A[:, 2, 12]
+ + A[:, 0, 0] * A[:, 1, 7] * A[:, 2, 11]
+ + A[:, 0, 1] * A[:, 1, 5] * A[:, 2, 12]
+ + A[:, 0, 1] * A[:, 1, 6] * A[:, 2, 11]
+ + A[:, 0, 1] * A[:, 1, 7] * A[:, 2, 10]
+ + A[:, 0, 2] * A[:, 1, 4] * A[:, 2, 12]
+ + A[:, 0, 2] * A[:, 1, 5] * A[:, 2, 11]
+ + A[:, 0, 2] * A[:, 1, 6] * A[:, 2, 10]
+ + A[:, 0, 3] * A[:, 1, 4] * A[:, 2, 11]
+ + A[:, 0, 3] * A[:, 1, 5] * A[:, 2, 10]
+ - A[:, 0, 4] * A[:, 1, 2] * A[:, 2, 12]
+ - A[:, 0, 4] * A[:, 1, 3] * A[:, 2, 11]
+ - A[:, 0, 5] * A[:, 1, 1] * A[:, 2, 12]
+ - A[:, 0, 5] * A[:, 1, 2] * A[:, 2, 11]
+ - A[:, 0, 5] * A[:, 1, 3] * A[:, 2, 10]
+ - A[:, 0, 6] * A[:, 1, 0] * A[:, 2, 12]
+ - A[:, 0, 6] * A[:, 1, 1] * A[:, 2, 11]
+ - A[:, 0, 6] * A[:, 1, 2] * A[:, 2, 10]
+ - A[:, 0, 7] * A[:, 1, 0] * A[:, 2, 11]
+ - A[:, 0, 7] * A[:, 1, 1] * A[:, 2, 10]
+ )
+
+ cs[:, 5] = (
+ A[:, 0, 1] * A[:, 1, 7] * A[:, 2, 9]
+ - A[:, 0, 1] * A[:, 1, 9] * A[:, 2, 7]
+ + A[:, 0, 2] * A[:, 1, 6] * A[:, 2, 9]
+ + A[:, 0, 2] * A[:, 1, 7] * A[:, 2, 8]
+ - A[:, 0, 2] * A[:, 1, 8] * A[:, 2, 7]
+ - A[:, 0, 2] * A[:, 1, 9] * A[:, 2, 6]
+ + A[:, 0, 3] * A[:, 1, 5] * A[:, 2, 9]
+ + A[:, 0, 3] * A[:, 1, 6] * A[:, 2, 8]
+ - A[:, 0, 3] * A[:, 1, 8] * A[:, 2, 6]
+ - A[:, 0, 3] * A[:, 1, 9] * A[:, 2, 5]
+ - A[:, 0, 5] * A[:, 1, 3] * A[:, 2, 9]
+ + A[:, 0, 5] * A[:, 1, 9] * A[:, 2, 3]
+ - A[:, 0, 6] * A[:, 1, 2] * A[:, 2, 9]
+ - A[:, 0, 6] * A[:, 1, 3] * A[:, 2, 8]
+ + A[:, 0, 6] * A[:, 1, 8] * A[:, 2, 3]
+ + A[:, 0, 6] * A[:, 1, 9] * A[:, 2, 2]
+ - A[:, 0, 7] * A[:, 1, 1] * A[:, 2, 9]
+ - A[:, 0, 7] * A[:, 1, 2] * A[:, 2, 8]
+ + A[:, 0, 7] * A[:, 1, 8] * A[:, 2, 2]
+ + A[:, 0, 7] * A[:, 1, 9] * A[:, 2, 1]
+ + A[:, 0, 8] * A[:, 1, 2] * A[:, 2, 7]
+ + A[:, 0, 8] * A[:, 1, 3] * A[:, 2, 6]
+ - A[:, 0, 8] * A[:, 1, 6] * A[:, 2, 3]
+ - A[:, 0, 8] * A[:, 1, 7] * A[:, 2, 2]
+ + A[:, 0, 9] * A[:, 1, 1] * A[:, 2, 7]
+ + A[:, 0, 9] * A[:, 1, 2] * A[:, 2, 6]
+ + A[:, 0, 9] * A[:, 1, 3] * A[:, 2, 5]
+ - A[:, 0, 9] * A[:, 1, 5] * A[:, 2, 3]
+ - A[:, 0, 9] * A[:, 1, 6] * A[:, 2, 2]
+ - A[:, 0, 9] * A[:, 1, 7] * A[:, 2, 1]
+ + A[:, 0, 10] * A[:, 1, 0] * A[:, 2, 7]
+ + A[:, 0, 10] * A[:, 1, 1] * A[:, 2, 6]
+ + A[:, 0, 10] * A[:, 1, 2] * A[:, 2, 5]
+ + A[:, 0, 10] * A[:, 1, 3] * A[:, 2, 4]
+ - A[:, 0, 10] * A[:, 1, 4] * A[:, 2, 3]
+ - A[:, 0, 10] * A[:, 1, 5] * A[:, 2, 2]
+ - A[:, 0, 10] * A[:, 1, 6] * A[:, 2, 1]
+ - A[:, 0, 10] * A[:, 1, 7] * A[:, 2, 0]
+ + A[:, 1, 0] * A[:, 0, 11] * A[:, 2, 6]
+ + A[:, 1, 0] * A[:, 0, 12] * A[:, 2, 5]
+ + A[:, 0, 11] * A[:, 1, 1] * A[:, 2, 5]
+ + A[:, 0, 11] * A[:, 1, 2] * A[:, 2, 4]
+ - A[:, 0, 11] * A[:, 1, 4] * A[:, 2, 2]
+ - A[:, 0, 11] * A[:, 1, 5] * A[:, 2, 1]
+ - A[:, 0, 11] * A[:, 1, 6] * A[:, 2, 0]
+ + A[:, 1, 1] * A[:, 0, 12] * A[:, 2, 4]
+ - A[:, 0, 12] * A[:, 1, 4] * A[:, 2, 1]
+ - A[:, 0, 12] * A[:, 1, 5] * A[:, 2, 0]
+ - A[:, 0, 0] * A[:, 2, 5] * A[:, 1, 12]
+ - A[:, 0, 0] * A[:, 2, 6] * A[:, 1, 11]
+ - A[:, 0, 0] * A[:, 2, 7] * A[:, 1, 10]
+ - A[:, 0, 1] * A[:, 2, 4] * A[:, 1, 12]
+ - A[:, 0, 1] * A[:, 2, 5] * A[:, 1, 11]
+ - A[:, 0, 1] * A[:, 2, 6] * A[:, 1, 10]
+ - A[:, 0, 2] * A[:, 2, 4] * A[:, 1, 11]
+ - A[:, 0, 2] * A[:, 2, 5] * A[:, 1, 10]
+ - A[:, 0, 3] * A[:, 2, 4] * A[:, 1, 10]
+ + A[:, 0, 4] * A[:, 2, 1] * A[:, 1, 12]
+ + A[:, 0, 4] * A[:, 2, 2] * A[:, 1, 11]
+ + A[:, 0, 4] * A[:, 2, 3] * A[:, 1, 10]
+ + A[:, 0, 5] * A[:, 2, 0] * A[:, 1, 12]
+ + A[:, 0, 5] * A[:, 2, 1] * A[:, 1, 11]
+ + A[:, 0, 5] * A[:, 2, 2] * A[:, 1, 10]
+ + A[:, 0, 6] * A[:, 2, 0] * A[:, 1, 11]
+ + A[:, 0, 6] * A[:, 2, 1] * A[:, 1, 10]
+ + A[:, 0, 7] * A[:, 2, 0] * A[:, 1, 10]
+ + A[:, 0, 0] * A[:, 1, 5] * A[:, 2, 12]
+ + A[:, 0, 0] * A[:, 1, 6] * A[:, 2, 11]
+ + A[:, 0, 0] * A[:, 1, 7] * A[:, 2, 10]
+ + A[:, 0, 1] * A[:, 1, 4] * A[:, 2, 12]
+ + A[:, 0, 1] * A[:, 1, 5] * A[:, 2, 11]
+ + A[:, 0, 1] * A[:, 1, 6] * A[:, 2, 10]
+ + A[:, 0, 2] * A[:, 1, 4] * A[:, 2, 11]
+ + A[:, 0, 2] * A[:, 1, 5] * A[:, 2, 10]
+ + A[:, 0, 3] * A[:, 1, 4] * A[:, 2, 10]
+ - A[:, 0, 4] * A[:, 1, 1] * A[:, 2, 12]
+ - A[:, 0, 4] * A[:, 1, 2] * A[:, 2, 11]
+ - A[:, 0, 4] * A[:, 1, 3] * A[:, 2, 10]
+ - A[:, 0, 5] * A[:, 1, 0] * A[:, 2, 12]
+ - A[:, 0, 5] * A[:, 1, 1] * A[:, 2, 11]
+ - A[:, 0, 5] * A[:, 1, 2] * A[:, 2, 10]
+ - A[:, 0, 6] * A[:, 1, 0] * A[:, 2, 11]
+ - A[:, 0, 6] * A[:, 1, 1] * A[:, 2, 10]
+ - A[:, 0, 7] * A[:, 1, 0] * A[:, 2, 10]
+ )
+
+ cs[:, 6] = (
+ A[:, 0, 0] * A[:, 1, 7] * A[:, 2, 9]
+ - A[:, 0, 0] * A[:, 1, 9] * A[:, 2, 7]
+ + A[:, 0, 1] * A[:, 1, 6] * A[:, 2, 9]
+ + A[:, 0, 1] * A[:, 1, 7] * A[:, 2, 8]
+ - A[:, 0, 1] * A[:, 1, 8] * A[:, 2, 7]
+ - A[:, 0, 1] * A[:, 1, 9] * A[:, 2, 6]
+ + A[:, 0, 2] * A[:, 1, 5] * A[:, 2, 9]
+ + A[:, 0, 2] * A[:, 1, 6] * A[:, 2, 8]
+ - A[:, 0, 2] * A[:, 1, 8] * A[:, 2, 6]
+ - A[:, 0, 2] * A[:, 1, 9] * A[:, 2, 5]
+ + A[:, 0, 3] * A[:, 1, 4] * A[:, 2, 9]
+ + A[:, 0, 3] * A[:, 1, 5] * A[:, 2, 8]
+ - A[:, 0, 3] * A[:, 1, 8] * A[:, 2, 5]
+ - A[:, 0, 3] * A[:, 1, 9] * A[:, 2, 4]
+ - A[:, 0, 4] * A[:, 1, 3] * A[:, 2, 9]
+ + A[:, 0, 4] * A[:, 1, 9] * A[:, 2, 3]
+ - A[:, 0, 5] * A[:, 1, 2] * A[:, 2, 9]
+ - A[:, 0, 5] * A[:, 1, 3] * A[:, 2, 8]
+ + A[:, 0, 5] * A[:, 1, 8] * A[:, 2, 3]
+ + A[:, 0, 5] * A[:, 1, 9] * A[:, 2, 2]
+ - A[:, 0, 6] * A[:, 1, 1] * A[:, 2, 9]
+ - A[:, 0, 6] * A[:, 1, 2] * A[:, 2, 8]
+ + A[:, 0, 6] * A[:, 1, 8] * A[:, 2, 2]
+ + A[:, 0, 6] * A[:, 1, 9] * A[:, 2, 1]
+ - A[:, 0, 7] * A[:, 1, 0] * A[:, 2, 9]
+ - A[:, 0, 7] * A[:, 1, 1] * A[:, 2, 8]
+ + A[:, 0, 7] * A[:, 1, 8] * A[:, 2, 1]
+ + A[:, 0, 7] * A[:, 1, 9] * A[:, 2, 0]
+ + A[:, 0, 8] * A[:, 1, 1] * A[:, 2, 7]
+ + A[:, 0, 8] * A[:, 1, 2] * A[:, 2, 6]
+ + A[:, 0, 8] * A[:, 1, 3] * A[:, 2, 5]
+ - A[:, 0, 8] * A[:, 1, 5] * A[:, 2, 3]
+ - A[:, 0, 8] * A[:, 1, 6] * A[:, 2, 2]
+ - A[:, 0, 8] * A[:, 1, 7] * A[:, 2, 1]
+ + A[:, 0, 9] * A[:, 1, 0] * A[:, 2, 7]
+ + A[:, 0, 9] * A[:, 1, 1] * A[:, 2, 6]
+ + A[:, 0, 9] * A[:, 1, 2] * A[:, 2, 5]
+ + A[:, 0, 9] * A[:, 1, 3] * A[:, 2, 4]
+ - A[:, 0, 9] * A[:, 1, 4] * A[:, 2, 3]
+ - A[:, 0, 9] * A[:, 1, 5] * A[:, 2, 2]
+ - A[:, 0, 9] * A[:, 1, 6] * A[:, 2, 1]
+ - A[:, 0, 9] * A[:, 1, 7] * A[:, 2, 0]
+ + A[:, 0, 10] * A[:, 1, 0] * A[:, 2, 6]
+ + A[:, 0, 10] * A[:, 1, 1] * A[:, 2, 5]
+ + A[:, 0, 10] * A[:, 1, 2] * A[:, 2, 4]
+ - A[:, 0, 10] * A[:, 1, 4] * A[:, 2, 2]
+ - A[:, 0, 10] * A[:, 1, 5] * A[:, 2, 1]
+ - A[:, 0, 10] * A[:, 1, 6] * A[:, 2, 0]
+ + A[:, 1, 0] * A[:, 0, 11] * A[:, 2, 5]
+ + A[:, 1, 0] * A[:, 0, 12] * A[:, 2, 4]
+ + A[:, 0, 11] * A[:, 1, 1] * A[:, 2, 4]
+ - A[:, 0, 11] * A[:, 1, 4] * A[:, 2, 1]
+ - A[:, 0, 11] * A[:, 1, 5] * A[:, 2, 0]
+ - A[:, 0, 12] * A[:, 1, 4] * A[:, 2, 0]
+ - A[:, 0, 0] * A[:, 2, 4] * A[:, 1, 12]
+ - A[:, 0, 0] * A[:, 2, 5] * A[:, 1, 11]
+ - A[:, 0, 0] * A[:, 2, 6] * A[:, 1, 10]
+ - A[:, 0, 1] * A[:, 2, 4] * A[:, 1, 11]
+ - A[:, 0, 1] * A[:, 2, 5] * A[:, 1, 10]
+ - A[:, 0, 2] * A[:, 2, 4] * A[:, 1, 10]
+ + A[:, 0, 4] * A[:, 2, 0] * A[:, 1, 12]
+ + A[:, 0, 4] * A[:, 2, 1] * A[:, 1, 11]
+ + A[:, 0, 4] * A[:, 2, 2] * A[:, 1, 10]
+ + A[:, 0, 5] * A[:, 2, 0] * A[:, 1, 11]
+ + A[:, 0, 5] * A[:, 2, 1] * A[:, 1, 10]
+ + A[:, 0, 6] * A[:, 2, 0] * A[:, 1, 10]
+ + A[:, 0, 0] * A[:, 1, 4] * A[:, 2, 12]
+ + A[:, 0, 0] * A[:, 1, 5] * A[:, 2, 11]
+ + A[:, 0, 0] * A[:, 1, 6] * A[:, 2, 10]
+ + A[:, 0, 1] * A[:, 1, 4] * A[:, 2, 11]
+ + A[:, 0, 1] * A[:, 1, 5] * A[:, 2, 10]
+ + A[:, 0, 2] * A[:, 1, 4] * A[:, 2, 10]
+ - A[:, 0, 4] * A[:, 1, 0] * A[:, 2, 12]
+ - A[:, 0, 4] * A[:, 1, 1] * A[:, 2, 11]
+ - A[:, 0, 4] * A[:, 1, 2] * A[:, 2, 10]
+ - A[:, 0, 5] * A[:, 1, 0] * A[:, 2, 11]
+ - A[:, 0, 5] * A[:, 1, 1] * A[:, 2, 10]
+ - A[:, 0, 6] * A[:, 1, 0] * A[:, 2, 10]
+ )
+
+ cs[:, 7] = (
+ A[:, 0, 0] * A[:, 1, 6] * A[:, 2, 9]
+ + A[:, 0, 0] * A[:, 1, 7] * A[:, 2, 8]
+ - A[:, 0, 0] * A[:, 1, 8] * A[:, 2, 7]
+ - A[:, 0, 0] * A[:, 1, 9] * A[:, 2, 6]
+ + A[:, 0, 1] * A[:, 1, 5] * A[:, 2, 9]
+ + A[:, 0, 1] * A[:, 1, 6] * A[:, 2, 8]
+ - A[:, 0, 1] * A[:, 1, 8] * A[:, 2, 6]
+ - A[:, 0, 1] * A[:, 1, 9] * A[:, 2, 5]
+ + A[:, 0, 2] * A[:, 1, 4] * A[:, 2, 9]
+ + A[:, 0, 2] * A[:, 1, 5] * A[:, 2, 8]
+ - A[:, 0, 2] * A[:, 1, 8] * A[:, 2, 5]
+ - A[:, 0, 2] * A[:, 1, 9] * A[:, 2, 4]
+ + A[:, 0, 3] * A[:, 1, 4] * A[:, 2, 8]
+ - A[:, 0, 3] * A[:, 1, 8] * A[:, 2, 4]
+ - A[:, 0, 4] * A[:, 1, 2] * A[:, 2, 9]
+ - A[:, 0, 4] * A[:, 1, 3] * A[:, 2, 8]
+ + A[:, 0, 4] * A[:, 1, 8] * A[:, 2, 3]
+ + A[:, 0, 4] * A[:, 1, 9] * A[:, 2, 2]
+ - A[:, 0, 5] * A[:, 1, 1] * A[:, 2, 9]
+ - A[:, 0, 5] * A[:, 1, 2] * A[:, 2, 8]
+ + A[:, 0, 5] * A[:, 1, 8] * A[:, 2, 2]
+ + A[:, 0, 5] * A[:, 1, 9] * A[:, 2, 1]
+ - A[:, 0, 6] * A[:, 1, 0] * A[:, 2, 9]
+ - A[:, 0, 6] * A[:, 1, 1] * A[:, 2, 8]
+ + A[:, 0, 6] * A[:, 1, 8] * A[:, 2, 1]
+ + A[:, 0, 6] * A[:, 1, 9] * A[:, 2, 0]
+ - A[:, 0, 7] * A[:, 1, 0] * A[:, 2, 8]
+ + A[:, 0, 7] * A[:, 1, 8] * A[:, 2, 0]
+ + A[:, 0, 8] * A[:, 1, 0] * A[:, 2, 7]
+ + A[:, 0, 8] * A[:, 1, 1] * A[:, 2, 6]
+ + A[:, 0, 8] * A[:, 1, 2] * A[:, 2, 5]
+ + A[:, 0, 8] * A[:, 1, 3] * A[:, 2, 4]
+ - A[:, 0, 8] * A[:, 1, 4] * A[:, 2, 3]
+ - A[:, 0, 8] * A[:, 1, 5] * A[:, 2, 2]
+ - A[:, 0, 8] * A[:, 1, 6] * A[:, 2, 1]
+ - A[:, 0, 8] * A[:, 1, 7] * A[:, 2, 0]
+ + A[:, 0, 9] * A[:, 1, 0] * A[:, 2, 6]
+ + A[:, 0, 9] * A[:, 1, 1] * A[:, 2, 5]
+ + A[:, 0, 9] * A[:, 1, 2] * A[:, 2, 4]
+ - A[:, 0, 9] * A[:, 1, 4] * A[:, 2, 2]
+ - A[:, 0, 9] * A[:, 1, 5] * A[:, 2, 1]
+ - A[:, 0, 9] * A[:, 1, 6] * A[:, 2, 0]
+ + A[:, 0, 10] * A[:, 1, 0] * A[:, 2, 5]
+ + A[:, 0, 10] * A[:, 1, 1] * A[:, 2, 4]
+ - A[:, 0, 10] * A[:, 1, 4] * A[:, 2, 1]
+ - A[:, 0, 10] * A[:, 1, 5] * A[:, 2, 0]
+ + A[:, 1, 0] * A[:, 0, 11] * A[:, 2, 4]
+ - A[:, 0, 11] * A[:, 1, 4] * A[:, 2, 0]
+ - A[:, 0, 0] * A[:, 2, 4] * A[:, 1, 11]
+ - A[:, 0, 0] * A[:, 2, 5] * A[:, 1, 10]
+ - A[:, 0, 1] * A[:, 2, 4] * A[:, 1, 10]
+ + A[:, 0, 4] * A[:, 2, 0] * A[:, 1, 11]
+ + A[:, 0, 4] * A[:, 2, 1] * A[:, 1, 10]
+ + A[:, 0, 5] * A[:, 2, 0] * A[:, 1, 10]
+ + A[:, 0, 0] * A[:, 1, 4] * A[:, 2, 11]
+ + A[:, 0, 0] * A[:, 1, 5] * A[:, 2, 10]
+ + A[:, 0, 1] * A[:, 1, 4] * A[:, 2, 10]
+ - A[:, 0, 4] * A[:, 1, 0] * A[:, 2, 11]
+ - A[:, 0, 4] * A[:, 1, 1] * A[:, 2, 10]
+ - A[:, 0, 5] * A[:, 1, 0] * A[:, 2, 10]
+ )
+
+ cs[:, 8] = (
+ A[:, 0, 0] * A[:, 1, 5] * A[:, 2, 9]
+ + A[:, 0, 0] * A[:, 1, 6] * A[:, 2, 8]
+ - A[:, 0, 0] * A[:, 1, 8] * A[:, 2, 6]
+ - A[:, 0, 0] * A[:, 1, 9] * A[:, 2, 5]
+ + A[:, 0, 1] * A[:, 1, 4] * A[:, 2, 9]
+ + A[:, 0, 1] * A[:, 1, 5] * A[:, 2, 8]
+ - A[:, 0, 1] * A[:, 1, 8] * A[:, 2, 5]
+ - A[:, 0, 1] * A[:, 1, 9] * A[:, 2, 4]
+ + A[:, 0, 2] * A[:, 1, 4] * A[:, 2, 8]
+ - A[:, 0, 2] * A[:, 1, 8] * A[:, 2, 4]
+ - A[:, 0, 4] * A[:, 1, 1] * A[:, 2, 9]
+ - A[:, 0, 4] * A[:, 1, 2] * A[:, 2, 8]
+ + A[:, 0, 4] * A[:, 1, 8] * A[:, 2, 2]
+ + A[:, 0, 4] * A[:, 1, 9] * A[:, 2, 1]
+ - A[:, 0, 5] * A[:, 1, 0] * A[:, 2, 9]
+ - A[:, 0, 5] * A[:, 1, 1] * A[:, 2, 8]
+ + A[:, 0, 5] * A[:, 1, 8] * A[:, 2, 1]
+ + A[:, 0, 5] * A[:, 1, 9] * A[:, 2, 0]
+ - A[:, 0, 6] * A[:, 1, 0] * A[:, 2, 8]
+ + A[:, 0, 6] * A[:, 1, 8] * A[:, 2, 0]
+ + A[:, 0, 8] * A[:, 1, 0] * A[:, 2, 6]
+ + A[:, 0, 8] * A[:, 1, 1] * A[:, 2, 5]
+ + A[:, 0, 8] * A[:, 1, 2] * A[:, 2, 4]
+ - A[:, 0, 8] * A[:, 1, 4] * A[:, 2, 2]
+ - A[:, 0, 8] * A[:, 1, 5] * A[:, 2, 1]
+ - A[:, 0, 8] * A[:, 1, 6] * A[:, 2, 0]
+ + A[:, 0, 9] * A[:, 1, 0] * A[:, 2, 5]
+ + A[:, 0, 9] * A[:, 1, 1] * A[:, 2, 4]
+ - A[:, 0, 9] * A[:, 1, 4] * A[:, 2, 1]
+ - A[:, 0, 9] * A[:, 1, 5] * A[:, 2, 0]
+ + A[:, 0, 10] * A[:, 1, 0] * A[:, 2, 4]
+ - A[:, 0, 10] * A[:, 1, 4] * A[:, 2, 0]
+ - A[:, 0, 0] * A[:, 2, 4] * A[:, 1, 10]
+ + A[:, 0, 4] * A[:, 2, 0] * A[:, 1, 10]
+ + A[:, 0, 0] * A[:, 1, 4] * A[:, 2, 10]
+ - A[:, 0, 4] * A[:, 1, 0] * A[:, 2, 10]
+ )
+
+ cs[:, 9] = (
+ A[:, 0, 0] * A[:, 1, 4] * A[:, 2, 9]
+ + A[:, 0, 0] * A[:, 1, 5] * A[:, 2, 8]
+ - A[:, 0, 0] * A[:, 1, 8] * A[:, 2, 5]
+ - A[:, 0, 0] * A[:, 1, 9] * A[:, 2, 4]
+ + A[:, 0, 1] * A[:, 1, 4] * A[:, 2, 8]
+ - A[:, 0, 1] * A[:, 1, 8] * A[:, 2, 4]
+ - A[:, 0, 4] * A[:, 1, 0] * A[:, 2, 9]
+ - A[:, 0, 4] * A[:, 1, 1] * A[:, 2, 8]
+ + A[:, 0, 4] * A[:, 1, 8] * A[:, 2, 1]
+ + A[:, 0, 4] * A[:, 1, 9] * A[:, 2, 0]
+ - A[:, 0, 5] * A[:, 1, 0] * A[:, 2, 8]
+ + A[:, 0, 5] * A[:, 1, 8] * A[:, 2, 0]
+ + A[:, 0, 8] * A[:, 1, 0] * A[:, 2, 5]
+ + A[:, 0, 8] * A[:, 1, 1] * A[:, 2, 4]
+ - A[:, 0, 8] * A[:, 1, 4] * A[:, 2, 1]
+ - A[:, 0, 8] * A[:, 1, 5] * A[:, 2, 0]
+ + A[:, 0, 9] * A[:, 1, 0] * A[:, 2, 4]
+ - A[:, 0, 9] * A[:, 1, 4] * A[:, 2, 0]
+ )
+
+ cs[:, 10] = (
+ A[:, 0, 0] * A[:, 1, 4] * A[:, 2, 8]
+ - A[:, 0, 0] * A[:, 1, 8] * A[:, 2, 4]
+ - A[:, 0, 4] * A[:, 1, 0] * A[:, 2, 8]
+ + A[:, 0, 4] * A[:, 1, 8] * A[:, 2, 0]
+ + A[:, 0, 8] * A[:, 1, 0] * A[:, 2, 4]
+ - A[:, 0, 8] * A[:, 1, 4] * A[:, 2, 0]
+ )
+
+ return cs
diff --git a/test/geometry/epipolar/test_essential.py b/test/geometry/epipolar/test_essential.py
index f4a1dc7d4..2dbfd4dd4 100644
--- a/test/geometry/epipolar/test_essential.py
+++ b/test/geometry/epipolar/test_essential.py
@@ -8,6 +8,68 @@ import kornia.geometry.epipolar as epi
from kornia.testing import assert_close
+class TestFindEssential:
+ def test_smoke(self, device, dtype):
+ points1 = torch.rand(1, 5, 2, device=device, dtype=dtype)
+ points2 = torch.rand(1, 5, 2, device=device, dtype=dtype)
+ weights = torch.ones(1, 5, device=device, dtype=dtype)
+ E_mat = epi.essential.find_essential(points1, points2, weights)
+ assert E_mat.shape == (1, 3, 3)
+
+ @pytest.mark.parametrize("batch_size, num_points", [(1, 5), (2, 6), (3, 7)])
+ def test_shape(self, batch_size, num_points, device, dtype):
+ B, N = batch_size, num_points
+ points1 = torch.rand(B, N, 2, device=device, dtype=dtype)
+ points2 = torch.rand(B, N, 2, device=device, dtype=dtype)
+ weights = torch.ones(B, N, device=device, dtype=dtype)
+ E_mat = epi.essential.find_essential(points1, points2, weights)
+ assert E_mat.shape == (B, 3, 3)
+
+ @pytest.mark.parametrize("batch_size, num_points", [(1, 5), (2, 6), (3, 7)])
+ def test_shape_noweights(self, batch_size, num_points, device, dtype):
+ B, N = batch_size, num_points
+ points1 = torch.rand(B, N, 2, device=device, dtype=dtype)
+ points2 = torch.rand(B, N, 2, device=device, dtype=dtype)
+ weights = None
+ E_mat = epi.essential.find_essential(points1, points2, weights)
+ assert E_mat.shape == (B, 3, 3)
+
+ def test_epipolar_constraint(self, device, dtype):
+ calibrated_x1 = torch.tensor(
+ [[[0.0640, 0.7799], [-0.2011, 0.2836], [-0.1355, 0.2907], [0.0520, 1.0086], [-0.0361, 0.6533]]],
+ device=device,
+ dtype=dtype,
+ )
+ calibrated_x2 = torch.tensor(
+ [[[0.3470, -0.4274], [-0.1818, -0.1281], [-0.1766, -0.1617], [0.4066, -0.0706], [0.1137, 0.0363]]],
+ device=device,
+ dtype=dtype,
+ )
+
+ E = epi.essential.find_essential(calibrated_x1, calibrated_x2)
+ if torch.all(E != 0):
+ distance = epi.symmetrical_epipolar_distance(calibrated_x1, calibrated_x2, E)
+ mean_error = distance.mean()
+ assert_close(mean_error, torch.tensor(0.0, device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
+
+ def test_synthetic_sampson(self, device, dtype):
+ calibrated_x1 = torch.tensor(
+ [[[0.0640, 0.7799], [-0.2011, 0.2836], [-0.1355, 0.2907], [0.0520, 1.0086], [-0.0361, 0.6533]]],
+ device=device,
+ dtype=dtype,
+ )
+ calibrated_x2 = torch.tensor(
+ [[[0.3470, -0.4274], [-0.1818, -0.1281], [-0.1766, -0.1617], [0.4066, -0.0706], [0.1137, 0.0363]]],
+ device=device,
+ dtype=dtype,
+ )
+
+ weights = torch.ones_like(calibrated_x2)[..., 0]
+ E_est = epi.essential.find_essential(calibrated_x1, calibrated_x2, weights)
+ error = epi.sampson_epipolar_distance(calibrated_x1, calibrated_x2, E_est)
+ assert_close(error, torch.zeros((calibrated_x1.shape[:2]), device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
+
+
class TestEssentialFromFundamental:
def test_smoke(self, device, dtype):
F_mat = torch.rand(1, 3, 3, device=device, dtype=dtype)