| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823 |
- # LICENSE HEADER MANAGED BY add-license-header
- #
- # Copyright 2018 Kornia Team
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- #
- """Module containing the functionalities for computing the real roots of polynomial equation."""
- import math
- import torch
- from kornia.core import Tensor, cos, ones_like, stack, zeros, zeros_like
- from kornia.core.check import KORNIA_CHECK_SHAPE
- # Reference : https://github.com/opencv/opencv/blob/4.x/modules/calib3d/src/polynom_solver.cpp
- def solve_quadratic(coeffs: Tensor) -> Tensor:
- r"""Solve given quadratic equation.
- The function takes the coefficients of quadratic equation and returns the real roots.
- .. math:: coeffs[0]x^2 + coeffs[1]x + coeffs[2] = 0
- Args:
- coeffs : The coefficients of quadratic equation :`(B, 3)`
- Returns:
- A 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 complex roots should be represented as 0. This is done to maintain
- a consistent output shape for all cases.
- """
- KORNIA_CHECK_SHAPE(coeffs, ["B", "3"])
- # Coefficients of quadratic equation
- a = coeffs[:, 0] # coefficient of x^2
- b = coeffs[:, 1] # coefficient of x
- c = coeffs[:, 2] # constant term
- # Calculate discriminant
- delta = b * b - 4 * a * c
- # Create masks for negative and zero discriminant
- mask_negative = delta < 0
- mask_zero = delta == 0
- # Calculate 1/(2*a) for efficient computation
- inv_2a = 0.5 / a
- # Initialize solutions tensor
- solutions = zeros((coeffs.shape[0], 2), device=coeffs.device, dtype=coeffs.dtype)
- # Handle cases with zero discriminant
- if torch.any(mask_zero):
- solutions[mask_zero, 0] = -b[mask_zero] * inv_2a[mask_zero]
- solutions[mask_zero, 1] = solutions[mask_zero, 0]
- # Negative discriminant cases are automatically handled since solutions is initialized with zeros.
- sqrt_delta = torch.sqrt(delta)
- # Handle cases with non-negative discriminant
- mask = torch.bitwise_and(~mask_negative, ~mask_zero)
- if torch.any(mask):
- solutions[mask, 0] = (-b[mask] + sqrt_delta[mask]) * inv_2a[mask]
- solutions[mask, 1] = (-b[mask] - sqrt_delta[mask]) * inv_2a[mask]
- return solutions
- def solve_cubic(coeffs: Tensor) -> Tensor:
- r"""Solve given cubic equation.
- The function takes the coefficients of cubic equation and returns
- the real roots.
- .. math:: coeffs[0]x^3 + coeffs[1]x^2 + coeffs[2]x + coeffs[3] = 0
- Args:
- coeffs : The coefficients cubic equation : `(B, 4)`
- Returns:
- A 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_CHECK_SHAPE(coeffs, ["B", "4"])
- _PI = torch.tensor(math.pi, device=coeffs.device, dtype=coeffs.dtype)
- # Coefficients of cubic equation
- a = coeffs[:, 0] # coefficient of x^3
- b = coeffs[:, 1] # coefficient of x^2
- c = coeffs[:, 2] # coefficient of x
- d = coeffs[:, 3] # constant term
- solutions = zeros((len(coeffs), 3), device=a.device, dtype=a.dtype)
- mask_a_zero = a == 0
- mask_b_zero = b == 0
- mask_c_zero = c == 0
- # Zero order cases are automatically handled since solutions is initialized with zeros.
- # No need for explicit handling of mask_zero_order as solutions already contains zeros by default.
- mask_first_order = mask_a_zero & mask_b_zero & ~mask_c_zero
- mask_second_order = mask_a_zero & ~mask_b_zero & ~mask_c_zero
- if torch.any(mask_second_order):
- solutions[mask_second_order, 0:2] = solve_quadratic(coeffs[mask_second_order, 1:])
- if torch.any(mask_first_order):
- solutions[mask_first_order, 0] = torch.tensor(1.0, device=a.device, dtype=a.dtype)
- # Normalized form x^3 + a2 * x^2 + a1 * x + a0 = 0
- inv_a = 1.0 / a[~mask_a_zero]
- b_a = inv_a * b[~mask_a_zero]
- b_a2 = b_a * b_a
- c_a = inv_a * c[~mask_a_zero]
- d_a = inv_a * d[~mask_a_zero]
- # Solve the cubic equation
- Q = (3 * c_a - b_a2) / 9
- R = (9 * b_a * c_a - 27 * d_a - 2 * b_a * b_a2) / 54
- Q3 = Q * Q * Q
- D = Q3 + R * R
- b_a_3 = (1.0 / 3.0) * b_a
- a_Q_zero = ones_like(a)
- a_R_zero = ones_like(a)
- a_D_zero = ones_like(a)
- a_Q_zero[~mask_a_zero] = Q
- a_R_zero[~mask_a_zero] = R
- a_D_zero[~mask_a_zero] = D
- # Q == 0
- mask_Q_zero = (Q == 0) & (R != 0)
- mask_Q_zero_solutions = (a_Q_zero == 0) & (a_R_zero != 0)
- if torch.any(mask_Q_zero):
- x0_Q_zero = torch.pow(2 * R[mask_Q_zero], 1 / 3) - b_a_3[mask_Q_zero]
- solutions[mask_Q_zero_solutions, 0] = x0_Q_zero
- mask_QR_zero = (Q == 0) & (R == 0)
- mask_QR_zero_solutions = (a_Q_zero == 0) & (a_R_zero == 0)
- if torch.any(mask_QR_zero):
- solutions[mask_QR_zero_solutions] = stack(
- [-b_a_3[mask_QR_zero], -b_a_3[mask_QR_zero], -b_a_3[mask_QR_zero]], dim=1
- )
- # D <= 0
- mask_D_zero = (D <= 0) & (Q != 0)
- mask_D_zero_solutions = (a_D_zero <= 0) & (a_Q_zero != 0)
- if torch.any(mask_D_zero):
- theta_D_zero = torch.acos(R[mask_D_zero] / torch.sqrt(-Q3[mask_D_zero]))
- sqrt_Q_D_zero = torch.sqrt(-Q[mask_D_zero])
- x0_D_zero = 2 * sqrt_Q_D_zero * cos(theta_D_zero / 3.0) - b_a_3[mask_D_zero]
- x1_D_zero = 2 * sqrt_Q_D_zero * cos((theta_D_zero + 2 * _PI) / 3.0) - b_a_3[mask_D_zero]
- x2_D_zero = 2 * sqrt_Q_D_zero * cos((theta_D_zero + 4 * _PI) / 3.0) - b_a_3[mask_D_zero]
- solutions[mask_D_zero_solutions] = stack([x0_D_zero, x1_D_zero, x2_D_zero], dim=1)
- a_D_positive = zeros_like(a)
- a_D_positive[~mask_a_zero] = D
- # D > 0
- mask_D_positive_solution = (a_D_positive > 0) & (a_Q_zero != 0)
- mask_D_positive = (D > 0) & (Q != 0)
- if torch.any(mask_D_positive):
- AD = zeros_like(R)
- BD = zeros_like(R)
- R_abs = torch.abs(R)
- mask_R_positive = R_abs > 1e-16
- if torch.any(mask_R_positive):
- AD[mask_R_positive] = torch.pow(R_abs[mask_R_positive] + torch.sqrt(D[mask_R_positive]), 1 / 3)
- mask_R_positive_ = R < 0
- if torch.any(mask_R_positive_):
- AD[mask_R_positive_] = -AD[mask_R_positive_]
- BD[mask_R_positive] = -Q[mask_R_positive] / AD[mask_R_positive]
- x0_D_positive = AD[mask_D_positive] + BD[mask_D_positive] - b_a_3[mask_D_positive]
- solutions[mask_D_positive_solution, 0] = x0_D_positive
- return solutions
- # 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
- T_deg1 = torch.zeros(16, 10)
- T_deg1[0, 0] = 1 # x * x → x^2
- T_deg1[1, 1] = 1 # x * y
- T_deg1[4, 1] = 1 # y * x
- T_deg1[2, 2] = 1 # x * z
- T_deg1[8, 2] = 1 # z * x
- T_deg1[3, 3] = 1 # x * 1
- T_deg1[12, 3] = 1 # 1 * x
- T_deg1[5, 4] = 1 # y * y
- T_deg1[6, 5] = 1 # y * z
- T_deg1[9, 5] = 1 # z * y
- T_deg1[7, 6] = 1 # y * 1
- T_deg1[13, 6] = 1 # 1 * y
- T_deg1[10, 7] = 1 # z * z
- T_deg1[11, 8] = 1 # z * 1
- T_deg1[14, 8] = 1 # 1 * z
- T_deg1[15, 9] = 1 # 1 * 1
- 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)`.
- """
- global T_deg1 # noqa: PLW0603
- if T_deg1.device != a.device or T_deg1.dtype != a.dtype:
- T_deg1 = T_deg1.to(device=a.device, dtype=a.dtype)
- return (a.unsqueeze(2) * b.unsqueeze(1)).flatten(start_dim=-2) @ T_deg1
- # Reference
- # https://github.com/danini/graph-cut-ransac/blob/aae1f40c2e10e31fd2191bac601c53a189673f60/src/pygcransac/
- # include/estimators/solver_essential_matrix_five_point_nister.h#L156
- T_deg2 = torch.zeros(40, 20)
- T_deg2[0, 0] = 1 # (0*4+0)
- T_deg2[17, 1] = 1 # (4*4+1)
- T_deg2[1, 2] = 1 # (0*4+1)
- T_deg2[4, 2] = 1 # (1*4+0)
- T_deg2[5, 3] = 1 # (1*4+1)
- T_deg2[16, 3] = 1 # (4*4+0)
- T_deg2[2, 4] = 1 # (0*4+2)
- T_deg2[8, 4] = 1 # (2*4+0)
- T_deg2[3, 5] = 1 # (0*4+3)
- T_deg2[12, 5] = 1 # (3*4+0)
- T_deg2[18, 6] = 1 # (4*4+2)
- T_deg2[21, 6] = 1 # (5*4+1)
- T_deg2[19, 7] = 1 # (4*4+3)
- T_deg2[25, 7] = 1 # (6*4+1)
- T_deg2[6, 8] = 1 # (1*4+2)
- T_deg2[9, 8] = 1 # (2*4+1)
- T_deg2[20, 8] = 1 # (5*4+0)
- T_deg2[7, 9] = 1 # (1*4+3)
- T_deg2[13, 9] = 1 # (3*4+1)
- T_deg2[24, 9] = 1 # (6*4+0)
- T_deg2[10, 10] = 1 # (2*4+2)
- T_deg2[28, 10] = 1 # (7*4+0)
- T_deg2[11, 11] = 1 # (2*4+3)
- T_deg2[14, 11] = 1 # (3*4+2)
- T_deg2[32, 11] = 1 # (8*4+0)
- T_deg2[15, 12] = 1 # (3*4+3)
- T_deg2[36, 12] = 1 # (9*4+0)
- T_deg2[22, 13] = 1 # (5*4+2)
- T_deg2[29, 13] = 1 # (7*4+1)
- T_deg2[23, 14] = 1 # (5*4+3)
- T_deg2[26, 14] = 1 # (6*4+2)
- T_deg2[33, 14] = 1 # (8*4+1)
- T_deg2[27, 15] = 1 # (6*4+3)
- T_deg2[37, 15] = 1 # (9*4+1)
- T_deg2[30, 16] = 1 # (7*4+2)
- T_deg2[31, 17] = 1 # (7*4+3)
- T_deg2[34, 17] = 1 # (8*4+2)
- T_deg2[35, 18] = 1 # (8*4+3)
- T_deg2[38, 18] = 1 # (9*4+2)
- T_deg2[39, 19] = 1 # (9*4+3)
- 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)`.
- """
- global T_deg2 # noqa: PLW0603
- if T_deg2.device != a.device or T_deg2.dtype != a.dtype:
- T_deg2 = T_deg2.to(device=a.device, dtype=a.dtype)
- product_basis = a.unsqueeze(2) * b.unsqueeze(1)
- product_vector = product_basis.flatten(start_dim=-2)
- return product_vector @ T_deg2
- # 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
- multiplication_indices = torch.tensor(
- [
- [12, 16, 33],
- [12, 20, 29],
- [3, 33, 25],
- [7, 29, 25],
- [3, 20, 38],
- [7, 16, 38],
- [11, 16, 33],
- [11, 20, 29],
- [12, 15, 33],
- [12, 16, 32],
- [12, 19, 29],
- [12, 20, 28],
- [2, 33, 25],
- [3, 32, 25],
- [3, 33, 24],
- [6, 29, 25],
- [7, 28, 25],
- [7, 29, 24],
- [2, 20, 38],
- [3, 19, 38],
- [3, 20, 37],
- [6, 16, 38],
- [7, 15, 38],
- [7, 16, 37],
- [10, 16, 33],
- [10, 20, 29],
- [11, 15, 33],
- [11, 16, 32],
- [11, 19, 29],
- [11, 20, 28],
- [14, 12, 33],
- [12, 15, 32],
- [12, 16, 31],
- [12, 18, 29],
- [12, 19, 28],
- [12, 20, 27],
- [1, 33, 25],
- [2, 32, 25],
- [2, 33, 24],
- [3, 31, 25],
- [3, 32, 24],
- [3, 33, 23],
- [5, 29, 25],
- [6, 28, 25],
- [6, 29, 24],
- [7, 27, 25],
- [7, 28, 24],
- [7, 29, 23],
- [1, 20, 38],
- [2, 19, 38],
- [2, 20, 37],
- [3, 18, 38],
- [3, 19, 37],
- [3, 20, 36],
- [5, 16, 38],
- [6, 15, 38],
- [6, 16, 37],
- [7, 14, 38],
- [7, 15, 37],
- [7, 16, 36],
- [3, 20, 35],
- [3, 22, 33],
- [7, 16, 35],
- [7, 22, 29],
- [9, 16, 33],
- [9, 20, 29],
- [10, 15, 33],
- [10, 16, 32],
- [10, 19, 29],
- [10, 20, 28],
- [13, 12, 33],
- [11, 14, 33],
- [11, 15, 32],
- [11, 16, 31],
- [11, 18, 29],
- [11, 19, 28],
- [11, 20, 27],
- [14, 12, 32],
- [12, 15, 31],
- [12, 16, 30],
- [12, 17, 29],
- [12, 18, 28],
- [12, 19, 27],
- [12, 20, 26],
- [0, 33, 25],
- [1, 32, 25],
- [1, 33, 24],
- [2, 31, 25],
- [2, 32, 24],
- [2, 33, 23],
- [3, 30, 25],
- [3, 31, 24],
- [3, 32, 23],
- [4, 29, 25],
- [5, 28, 25],
- [5, 29, 24],
- [6, 27, 25],
- [6, 28, 24],
- [6, 29, 23],
- [7, 26, 25],
- [7, 27, 24],
- [7, 28, 23],
- [0, 20, 38],
- [1, 19, 38],
- [1, 20, 37],
- [2, 18, 38],
- [2, 19, 37],
- [2, 20, 36],
- [3, 17, 38],
- [3, 18, 37],
- [3, 19, 36],
- [4, 16, 38],
- [5, 15, 38],
- [5, 16, 37],
- [6, 14, 38],
- [6, 15, 37],
- [6, 16, 36],
- [7, 13, 38],
- [7, 14, 37],
- [7, 15, 36],
- [2, 20, 35],
- [2, 22, 33],
- [3, 19, 35],
- [3, 20, 34],
- [3, 21, 33],
- [3, 22, 32],
- [6, 16, 35],
- [6, 22, 29],
- [7, 15, 35],
- [7, 16, 34],
- [7, 21, 29],
- [7, 22, 28],
- [8, 16, 33],
- [8, 20, 29],
- [9, 15, 33],
- [9, 16, 32],
- [9, 19, 29],
- [9, 20, 28],
- [10, 14, 33],
- [10, 15, 32],
- [10, 16, 31],
- [10, 18, 29],
- [10, 19, 28],
- [10, 20, 27],
- [13, 11, 33],
- [13, 12, 32],
- [11, 14, 32],
- [11, 15, 31],
- [11, 16, 30],
- [11, 17, 29],
- [11, 18, 28],
- [11, 19, 27],
- [11, 20, 26],
- [14, 12, 31],
- [12, 15, 30],
- [12, 17, 28],
- [12, 18, 27],
- [12, 19, 26],
- [0, 32, 25],
- [0, 33, 24],
- [1, 31, 25],
- [1, 32, 24],
- [1, 33, 23],
- [2, 30, 25],
- [2, 31, 24],
- [2, 32, 23],
- [3, 30, 24],
- [3, 31, 23],
- [4, 28, 25],
- [4, 29, 24],
- [5, 27, 25],
- [5, 28, 24],
- [5, 29, 23],
- [6, 26, 25],
- [6, 27, 24],
- [6, 28, 23],
- [7, 26, 24],
- [7, 27, 23],
- [0, 19, 38],
- [0, 20, 37],
- [1, 18, 38],
- [1, 19, 37],
- [1, 20, 36],
- [2, 17, 38],
- [2, 18, 37],
- [2, 19, 36],
- [3, 17, 37],
- [3, 18, 36],
- [4, 15, 38],
- [4, 16, 37],
- [5, 14, 38],
- [5, 15, 37],
- [5, 16, 36],
- [6, 13, 38],
- [6, 14, 37],
- [6, 15, 36],
- [7, 13, 37],
- [7, 14, 36],
- [1, 20, 35],
- [1, 22, 33],
- [2, 19, 35],
- [2, 20, 34],
- [2, 21, 33],
- [2, 22, 32],
- [3, 18, 35],
- [3, 19, 34],
- [3, 21, 32],
- [3, 22, 31],
- [5, 16, 35],
- [5, 22, 29],
- [6, 15, 35],
- [6, 16, 34],
- [6, 21, 29],
- [6, 22, 28],
- [7, 14, 35],
- [7, 15, 34],
- [7, 21, 28],
- [7, 22, 27],
- [8, 15, 33],
- [8, 16, 32],
- [8, 19, 29],
- [8, 20, 28],
- [9, 14, 33],
- [9, 15, 32],
- [9, 16, 31],
- [9, 18, 29],
- [9, 19, 28],
- [9, 20, 27],
- [10, 13, 33],
- [10, 14, 32],
- [10, 15, 31],
- [10, 16, 30],
- [10, 17, 29],
- [10, 18, 28],
- [10, 19, 27],
- [10, 20, 26],
- [13, 11, 32],
- [13, 12, 31],
- [11, 14, 31],
- [11, 15, 30],
- [11, 17, 28],
- [11, 18, 27],
- [11, 19, 26],
- [14, 12, 30],
- [12, 17, 27],
- [12, 18, 26],
- [0, 31, 25],
- [0, 32, 24],
- [0, 33, 23],
- [1, 30, 25],
- [1, 31, 24],
- [1, 32, 23],
- [2, 30, 24],
- [2, 31, 23],
- [3, 30, 23],
- [4, 27, 25],
- [4, 28, 24],
- [4, 29, 23],
- [5, 26, 25],
- [5, 27, 24],
- [5, 28, 23],
- [6, 26, 24],
- [6, 27, 23],
- [7, 26, 23],
- [0, 18, 38],
- [0, 19, 37],
- [0, 20, 36],
- [1, 17, 38],
- [1, 18, 37],
- [1, 19, 36],
- [2, 17, 37],
- [2, 18, 36],
- [3, 17, 36],
- [4, 14, 38],
- [4, 15, 37],
- [4, 16, 36],
- [5, 13, 38],
- [5, 14, 37],
- [5, 15, 36],
- [6, 13, 37],
- [6, 14, 36],
- [7, 13, 36],
- [0, 20, 35],
- [0, 22, 33],
- [1, 19, 35],
- [1, 20, 34],
- [1, 21, 33],
- [1, 22, 32],
- [2, 18, 35],
- [2, 19, 34],
- [2, 21, 32],
- [2, 22, 31],
- [3, 17, 35],
- [3, 18, 34],
- [3, 21, 31],
- [3, 22, 30],
- [4, 16, 35],
- [4, 22, 29],
- [5, 15, 35],
- [5, 16, 34],
- [5, 21, 29],
- [5, 22, 28],
- [6, 14, 35],
- [6, 15, 34],
- [6, 21, 28],
- [6, 22, 27],
- [7, 13, 35],
- [7, 14, 34],
- [7, 21, 27],
- [7, 22, 26],
- [8, 14, 33],
- [8, 15, 32],
- [8, 16, 31],
- [8, 18, 29],
- [8, 19, 28],
- [8, 20, 27],
- [9, 13, 33],
- [9, 14, 32],
- [9, 15, 31],
- [9, 16, 30],
- [9, 17, 29],
- [9, 18, 28],
- [9, 19, 27],
- [9, 20, 26],
- [10, 13, 32],
- [10, 14, 31],
- [10, 15, 30],
- [10, 17, 28],
- [10, 18, 27],
- [10, 19, 26],
- [13, 11, 31],
- [13, 12, 30],
- [11, 14, 30],
- [11, 17, 27],
- [11, 18, 26],
- [12, 17, 26],
- [0, 30, 25],
- [0, 31, 24],
- [0, 32, 23],
- [1, 30, 24],
- [1, 31, 23],
- [2, 30, 23],
- [4, 26, 25],
- [4, 27, 24],
- [4, 28, 23],
- [5, 26, 24],
- [5, 27, 23],
- [6, 26, 23],
- [0, 17, 38],
- [0, 18, 37],
- [0, 19, 36],
- [1, 17, 37],
- [1, 18, 36],
- [2, 17, 36],
- [4, 13, 38],
- [4, 14, 37],
- [4, 15, 36],
- [5, 13, 37],
- [5, 14, 36],
- [6, 13, 36],
- [0, 19, 35],
- [0, 20, 34],
- [0, 21, 33],
- [0, 22, 32],
- [1, 18, 35],
- [1, 19, 34],
- [1, 21, 32],
- [1, 22, 31],
- [2, 17, 35],
- [2, 18, 34],
- [2, 21, 31],
- [2, 22, 30],
- [3, 17, 34],
- [3, 21, 30],
- [4, 15, 35],
- [4, 16, 34],
- [4, 21, 29],
- [4, 22, 28],
- [5, 14, 35],
- [5, 15, 34],
- [5, 21, 28],
- [5, 22, 27],
- [6, 13, 35],
- [6, 14, 34],
- [6, 21, 27],
- [6, 22, 26],
- [7, 13, 34],
- [7, 21, 26],
- [8, 13, 33],
- [8, 14, 32],
- [8, 15, 31],
- [8, 16, 30],
- [8, 17, 29],
- [8, 18, 28],
- [8, 19, 27],
- [8, 20, 26],
- [9, 13, 32],
- [9, 14, 31],
- [9, 15, 30],
- [9, 17, 28],
- [9, 18, 27],
- [9, 19, 26],
- [10, 13, 31],
- [10, 14, 30],
- [10, 17, 27],
- [10, 18, 26],
- [13, 11, 30],
- [11, 17, 26],
- [0, 30, 24],
- [0, 31, 23],
- [1, 30, 23],
- [4, 26, 24],
- [4, 27, 23],
- [5, 26, 23],
- [0, 17, 37],
- [0, 18, 36],
- [1, 17, 36],
- [4, 13, 37],
- [4, 14, 36],
- [5, 13, 36],
- [0, 18, 35],
- [0, 19, 34],
- [0, 21, 32],
- [0, 22, 31],
- [1, 17, 35],
- [1, 18, 34],
- [1, 21, 31],
- [1, 22, 30],
- [2, 17, 34],
- [2, 21, 30],
- [4, 14, 35],
- [4, 15, 34],
- [4, 21, 28],
- [4, 22, 27],
- [5, 13, 35],
- [5, 14, 34],
- [5, 21, 27],
- [5, 22, 26],
- [6, 13, 34],
- [6, 21, 26],
- [8, 13, 32],
- [8, 14, 31],
- [8, 15, 30],
- [8, 17, 28],
- [8, 18, 27],
- [8, 19, 26],
- [9, 13, 31],
- [9, 14, 30],
- [9, 17, 27],
- [9, 18, 26],
- [10, 13, 30],
- [10, 17, 26],
- [0, 30, 23],
- [4, 26, 23],
- [0, 17, 36],
- [4, 13, 36],
- [0, 17, 35],
- [0, 18, 34],
- [0, 21, 31],
- [0, 22, 30],
- [1, 17, 34],
- [1, 21, 30],
- [4, 13, 35],
- [4, 14, 34],
- [4, 21, 27],
- [4, 22, 26],
- [5, 13, 34],
- [5, 21, 26],
- [8, 13, 31],
- [8, 14, 30],
- [8, 17, 27],
- [8, 18, 26],
- [9, 13, 30],
- [9, 17, 26],
- [0, 17, 34],
- [0, 21, 30],
- [4, 13, 34],
- [4, 21, 26],
- [8, 13, 30],
- [8, 17, 26],
- ],
- dtype=torch.int64,
- )
- signs = torch.tensor(
- [
- 1.0,
- -1.0,
- -1.0,
- 1.0,
- 1.0,
- -1.0,
- 1.0,
- -1.0,
- 1.0,
- 1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- -1.0,
- -1.0,
- -1.0,
- 1.0,
- -1.0,
- 1.0,
- 1.0,
- -1.0,
- -1.0,
- 1.0,
- 1.0,
- 1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- 1.0,
- -1.0,
- -1.0,
- 1.0,
- 1.0,
- -1.0,
- 1.0,
- 1.0,
- -1.0,
- -1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- -1.0,
- -1.0,
- -1.0,
- 1.0,
- 1.0,
- 1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- -1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.0,
- 1.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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).
- """
- B, device, dtype = A.shape[0], A.device, A.dtype
- global multiplication_indices, signs, coefficient_map # noqa: PLW0603
- multiplication_indices = multiplication_indices.to(device)
- signs = signs.to(device, dtype)
- coefficient_map = coefficient_map.to(device)
- A_flat = A.view(B, -1)
- gathered_values = A_flat[:, multiplication_indices]
- products = torch.prod(gathered_values, dim=-1)
- signed_products = products * signs
- cs = torch.zeros(B, 11, device=device, dtype=dtype)
- batch_coefficient_map = coefficient_map.repeat(B, 1)
- cs.scatter_add_(dim=1, index=batch_coefficient_map, src=signed_products)
- return cs
|