polynomial_solver.py 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """Module containing the functionalities for computing the real roots of polynomial equation."""
  18. import math
  19. import torch
  20. from kornia.core import Tensor, cos, ones_like, stack, zeros, zeros_like
  21. from kornia.core.check import KORNIA_CHECK_SHAPE
  22. # Reference : https://github.com/opencv/opencv/blob/4.x/modules/calib3d/src/polynom_solver.cpp
  23. def solve_quadratic(coeffs: Tensor) -> Tensor:
  24. r"""Solve given quadratic equation.
  25. The function takes the coefficients of quadratic equation and returns the real roots.
  26. .. math:: coeffs[0]x^2 + coeffs[1]x + coeffs[2] = 0
  27. Args:
  28. coeffs : The coefficients of quadratic equation :`(B, 3)`
  29. Returns:
  30. A tensor of shape `(B, 2)` containing the real roots to the quadratic equation.
  31. Example:
  32. >>> coeffs = torch.tensor([[1., 4., 4.]])
  33. >>> roots = solve_quadratic(coeffs)
  34. .. note::
  35. In cases where a quadratic polynomial has only one real root, the output will be in the format
  36. [real_root, 0]. And for the complex roots should be represented as 0. This is done to maintain
  37. a consistent output shape for all cases.
  38. """
  39. KORNIA_CHECK_SHAPE(coeffs, ["B", "3"])
  40. # Coefficients of quadratic equation
  41. a = coeffs[:, 0] # coefficient of x^2
  42. b = coeffs[:, 1] # coefficient of x
  43. c = coeffs[:, 2] # constant term
  44. # Calculate discriminant
  45. delta = b * b - 4 * a * c
  46. # Create masks for negative and zero discriminant
  47. mask_negative = delta < 0
  48. mask_zero = delta == 0
  49. # Calculate 1/(2*a) for efficient computation
  50. inv_2a = 0.5 / a
  51. # Initialize solutions tensor
  52. solutions = zeros((coeffs.shape[0], 2), device=coeffs.device, dtype=coeffs.dtype)
  53. # Handle cases with zero discriminant
  54. if torch.any(mask_zero):
  55. solutions[mask_zero, 0] = -b[mask_zero] * inv_2a[mask_zero]
  56. solutions[mask_zero, 1] = solutions[mask_zero, 0]
  57. # Negative discriminant cases are automatically handled since solutions is initialized with zeros.
  58. sqrt_delta = torch.sqrt(delta)
  59. # Handle cases with non-negative discriminant
  60. mask = torch.bitwise_and(~mask_negative, ~mask_zero)
  61. if torch.any(mask):
  62. solutions[mask, 0] = (-b[mask] + sqrt_delta[mask]) * inv_2a[mask]
  63. solutions[mask, 1] = (-b[mask] - sqrt_delta[mask]) * inv_2a[mask]
  64. return solutions
  65. def solve_cubic(coeffs: Tensor) -> Tensor:
  66. r"""Solve given cubic equation.
  67. The function takes the coefficients of cubic equation and returns
  68. the real roots.
  69. .. math:: coeffs[0]x^3 + coeffs[1]x^2 + coeffs[2]x + coeffs[3] = 0
  70. Args:
  71. coeffs : The coefficients cubic equation : `(B, 4)`
  72. Returns:
  73. A tensor of shape `(B, 3)` containing the real roots to the cubic equation.
  74. Example:
  75. >>> coeffs = torch.tensor([[32., 3., -11., -6.]])
  76. >>> roots = solve_cubic(coeffs)
  77. .. note::
  78. In cases where a cubic polynomial has only one or two real roots, the output for the non-real
  79. roots should be represented as 0. Thus, the output for a single real root should be in the
  80. format [real_root, 0, 0], and for two real roots, it should be [real_root_1, real_root_2, 0].
  81. """
  82. KORNIA_CHECK_SHAPE(coeffs, ["B", "4"])
  83. _PI = torch.tensor(math.pi, device=coeffs.device, dtype=coeffs.dtype)
  84. # Coefficients of cubic equation
  85. a = coeffs[:, 0] # coefficient of x^3
  86. b = coeffs[:, 1] # coefficient of x^2
  87. c = coeffs[:, 2] # coefficient of x
  88. d = coeffs[:, 3] # constant term
  89. solutions = zeros((len(coeffs), 3), device=a.device, dtype=a.dtype)
  90. mask_a_zero = a == 0
  91. mask_b_zero = b == 0
  92. mask_c_zero = c == 0
  93. # Zero order cases are automatically handled since solutions is initialized with zeros.
  94. # No need for explicit handling of mask_zero_order as solutions already contains zeros by default.
  95. mask_first_order = mask_a_zero & mask_b_zero & ~mask_c_zero
  96. mask_second_order = mask_a_zero & ~mask_b_zero & ~mask_c_zero
  97. if torch.any(mask_second_order):
  98. solutions[mask_second_order, 0:2] = solve_quadratic(coeffs[mask_second_order, 1:])
  99. if torch.any(mask_first_order):
  100. solutions[mask_first_order, 0] = torch.tensor(1.0, device=a.device, dtype=a.dtype)
  101. # Normalized form x^3 + a2 * x^2 + a1 * x + a0 = 0
  102. inv_a = 1.0 / a[~mask_a_zero]
  103. b_a = inv_a * b[~mask_a_zero]
  104. b_a2 = b_a * b_a
  105. c_a = inv_a * c[~mask_a_zero]
  106. d_a = inv_a * d[~mask_a_zero]
  107. # Solve the cubic equation
  108. Q = (3 * c_a - b_a2) / 9
  109. R = (9 * b_a * c_a - 27 * d_a - 2 * b_a * b_a2) / 54
  110. Q3 = Q * Q * Q
  111. D = Q3 + R * R
  112. b_a_3 = (1.0 / 3.0) * b_a
  113. a_Q_zero = ones_like(a)
  114. a_R_zero = ones_like(a)
  115. a_D_zero = ones_like(a)
  116. a_Q_zero[~mask_a_zero] = Q
  117. a_R_zero[~mask_a_zero] = R
  118. a_D_zero[~mask_a_zero] = D
  119. # Q == 0
  120. mask_Q_zero = (Q == 0) & (R != 0)
  121. mask_Q_zero_solutions = (a_Q_zero == 0) & (a_R_zero != 0)
  122. if torch.any(mask_Q_zero):
  123. x0_Q_zero = torch.pow(2 * R[mask_Q_zero], 1 / 3) - b_a_3[mask_Q_zero]
  124. solutions[mask_Q_zero_solutions, 0] = x0_Q_zero
  125. mask_QR_zero = (Q == 0) & (R == 0)
  126. mask_QR_zero_solutions = (a_Q_zero == 0) & (a_R_zero == 0)
  127. if torch.any(mask_QR_zero):
  128. solutions[mask_QR_zero_solutions] = stack(
  129. [-b_a_3[mask_QR_zero], -b_a_3[mask_QR_zero], -b_a_3[mask_QR_zero]], dim=1
  130. )
  131. # D <= 0
  132. mask_D_zero = (D <= 0) & (Q != 0)
  133. mask_D_zero_solutions = (a_D_zero <= 0) & (a_Q_zero != 0)
  134. if torch.any(mask_D_zero):
  135. theta_D_zero = torch.acos(R[mask_D_zero] / torch.sqrt(-Q3[mask_D_zero]))
  136. sqrt_Q_D_zero = torch.sqrt(-Q[mask_D_zero])
  137. x0_D_zero = 2 * sqrt_Q_D_zero * cos(theta_D_zero / 3.0) - b_a_3[mask_D_zero]
  138. x1_D_zero = 2 * sqrt_Q_D_zero * cos((theta_D_zero + 2 * _PI) / 3.0) - b_a_3[mask_D_zero]
  139. x2_D_zero = 2 * sqrt_Q_D_zero * cos((theta_D_zero + 4 * _PI) / 3.0) - b_a_3[mask_D_zero]
  140. solutions[mask_D_zero_solutions] = stack([x0_D_zero, x1_D_zero, x2_D_zero], dim=1)
  141. a_D_positive = zeros_like(a)
  142. a_D_positive[~mask_a_zero] = D
  143. # D > 0
  144. mask_D_positive_solution = (a_D_positive > 0) & (a_Q_zero != 0)
  145. mask_D_positive = (D > 0) & (Q != 0)
  146. if torch.any(mask_D_positive):
  147. AD = zeros_like(R)
  148. BD = zeros_like(R)
  149. R_abs = torch.abs(R)
  150. mask_R_positive = R_abs > 1e-16
  151. if torch.any(mask_R_positive):
  152. AD[mask_R_positive] = torch.pow(R_abs[mask_R_positive] + torch.sqrt(D[mask_R_positive]), 1 / 3)
  153. mask_R_positive_ = R < 0
  154. if torch.any(mask_R_positive_):
  155. AD[mask_R_positive_] = -AD[mask_R_positive_]
  156. BD[mask_R_positive] = -Q[mask_R_positive] / AD[mask_R_positive]
  157. x0_D_positive = AD[mask_D_positive] + BD[mask_D_positive] - b_a_3[mask_D_positive]
  158. solutions[mask_D_positive_solution, 0] = x0_D_positive
  159. return solutions
  160. # def solve_quartic(coeffs: Tensor) -> Tensor:
  161. # TODO: Quartic equation solver
  162. # return solutions
  163. # Reference
  164. # https://github.com/danini/graph-cut-ransac/blob/master/src/pygcransac/include/
  165. # estimators/solver_essential_matrix_five_point_nister.h#L108
  166. T_deg1 = torch.zeros(16, 10)
  167. T_deg1[0, 0] = 1 # x * x → x^2
  168. T_deg1[1, 1] = 1 # x * y
  169. T_deg1[4, 1] = 1 # y * x
  170. T_deg1[2, 2] = 1 # x * z
  171. T_deg1[8, 2] = 1 # z * x
  172. T_deg1[3, 3] = 1 # x * 1
  173. T_deg1[12, 3] = 1 # 1 * x
  174. T_deg1[5, 4] = 1 # y * y
  175. T_deg1[6, 5] = 1 # y * z
  176. T_deg1[9, 5] = 1 # z * y
  177. T_deg1[7, 6] = 1 # y * 1
  178. T_deg1[13, 6] = 1 # 1 * y
  179. T_deg1[10, 7] = 1 # z * z
  180. T_deg1[11, 8] = 1 # z * 1
  181. T_deg1[14, 8] = 1 # 1 * z
  182. T_deg1[15, 9] = 1 # 1 * 1
  183. def multiply_deg_one_poly(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
  184. r"""Multiply two polynomials of the first order [@nister2004efficient].
  185. Args:
  186. a: a first order polynomial for variables :math:`(x,y,z,1)`.
  187. b: a first order polynomial for variables :math:`(x,y,z,1)`.
  188. Returns:
  189. degree 2 poly with the order :math:`(x^2, x*y, x*z, x, y^2, y*z, y, z^2, z, 1)`.
  190. """
  191. global T_deg1 # noqa: PLW0603
  192. if T_deg1.device != a.device or T_deg1.dtype != a.dtype:
  193. T_deg1 = T_deg1.to(device=a.device, dtype=a.dtype)
  194. return (a.unsqueeze(2) * b.unsqueeze(1)).flatten(start_dim=-2) @ T_deg1
  195. # Reference
  196. # https://github.com/danini/graph-cut-ransac/blob/aae1f40c2e10e31fd2191bac601c53a189673f60/src/pygcransac/
  197. # include/estimators/solver_essential_matrix_five_point_nister.h#L156
  198. T_deg2 = torch.zeros(40, 20)
  199. T_deg2[0, 0] = 1 # (0*4+0)
  200. T_deg2[17, 1] = 1 # (4*4+1)
  201. T_deg2[1, 2] = 1 # (0*4+1)
  202. T_deg2[4, 2] = 1 # (1*4+0)
  203. T_deg2[5, 3] = 1 # (1*4+1)
  204. T_deg2[16, 3] = 1 # (4*4+0)
  205. T_deg2[2, 4] = 1 # (0*4+2)
  206. T_deg2[8, 4] = 1 # (2*4+0)
  207. T_deg2[3, 5] = 1 # (0*4+3)
  208. T_deg2[12, 5] = 1 # (3*4+0)
  209. T_deg2[18, 6] = 1 # (4*4+2)
  210. T_deg2[21, 6] = 1 # (5*4+1)
  211. T_deg2[19, 7] = 1 # (4*4+3)
  212. T_deg2[25, 7] = 1 # (6*4+1)
  213. T_deg2[6, 8] = 1 # (1*4+2)
  214. T_deg2[9, 8] = 1 # (2*4+1)
  215. T_deg2[20, 8] = 1 # (5*4+0)
  216. T_deg2[7, 9] = 1 # (1*4+3)
  217. T_deg2[13, 9] = 1 # (3*4+1)
  218. T_deg2[24, 9] = 1 # (6*4+0)
  219. T_deg2[10, 10] = 1 # (2*4+2)
  220. T_deg2[28, 10] = 1 # (7*4+0)
  221. T_deg2[11, 11] = 1 # (2*4+3)
  222. T_deg2[14, 11] = 1 # (3*4+2)
  223. T_deg2[32, 11] = 1 # (8*4+0)
  224. T_deg2[15, 12] = 1 # (3*4+3)
  225. T_deg2[36, 12] = 1 # (9*4+0)
  226. T_deg2[22, 13] = 1 # (5*4+2)
  227. T_deg2[29, 13] = 1 # (7*4+1)
  228. T_deg2[23, 14] = 1 # (5*4+3)
  229. T_deg2[26, 14] = 1 # (6*4+2)
  230. T_deg2[33, 14] = 1 # (8*4+1)
  231. T_deg2[27, 15] = 1 # (6*4+3)
  232. T_deg2[37, 15] = 1 # (9*4+1)
  233. T_deg2[30, 16] = 1 # (7*4+2)
  234. T_deg2[31, 17] = 1 # (7*4+3)
  235. T_deg2[34, 17] = 1 # (8*4+2)
  236. T_deg2[35, 18] = 1 # (8*4+3)
  237. T_deg2[38, 18] = 1 # (9*4+2)
  238. T_deg2[39, 19] = 1 # (9*4+3)
  239. def multiply_deg_two_one_poly(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
  240. r"""Multiply two polynomials a and b of degrees two and one [@nister2004efficient].
  241. Args:
  242. a: a second degree poly for variables :math:`(x^2, x*y, x*z, x, y^2, y*z, y, z^2, z, 1)`.
  243. b: a first degree poly for variables :math:`(x y z 1)`.
  244. Returns:
  245. a third degree poly for variables,
  246. :math:`(x^3, y^3, x^2*y, x*y^2, x^2*z, x^2, y^2*z, y^2,
  247. x*y*z, x*y, x*z^2, x*z, x, y*z^2, y*z, y, z^3, z^2, z, 1)`.
  248. """
  249. global T_deg2 # noqa: PLW0603
  250. if T_deg2.device != a.device or T_deg2.dtype != a.dtype:
  251. T_deg2 = T_deg2.to(device=a.device, dtype=a.dtype)
  252. product_basis = a.unsqueeze(2) * b.unsqueeze(1)
  253. product_vector = product_basis.flatten(start_dim=-2)
  254. return product_vector @ T_deg2
  255. # Compute degree 10 poly representing determinant (equation 14 in the paper)
  256. # https://github.com/danini/graph-cut-ransac/blob/aae1f40c2e10e31fd2191bac601c53a189673f60/src/pygcransac/
  257. # include/estimators/solver_essential_matrix_five_point_nister.h#L368C5-L368C82
  258. multiplication_indices = torch.tensor(
  259. [
  260. [12, 16, 33],
  261. [12, 20, 29],
  262. [3, 33, 25],
  263. [7, 29, 25],
  264. [3, 20, 38],
  265. [7, 16, 38],
  266. [11, 16, 33],
  267. [11, 20, 29],
  268. [12, 15, 33],
  269. [12, 16, 32],
  270. [12, 19, 29],
  271. [12, 20, 28],
  272. [2, 33, 25],
  273. [3, 32, 25],
  274. [3, 33, 24],
  275. [6, 29, 25],
  276. [7, 28, 25],
  277. [7, 29, 24],
  278. [2, 20, 38],
  279. [3, 19, 38],
  280. [3, 20, 37],
  281. [6, 16, 38],
  282. [7, 15, 38],
  283. [7, 16, 37],
  284. [10, 16, 33],
  285. [10, 20, 29],
  286. [11, 15, 33],
  287. [11, 16, 32],
  288. [11, 19, 29],
  289. [11, 20, 28],
  290. [14, 12, 33],
  291. [12, 15, 32],
  292. [12, 16, 31],
  293. [12, 18, 29],
  294. [12, 19, 28],
  295. [12, 20, 27],
  296. [1, 33, 25],
  297. [2, 32, 25],
  298. [2, 33, 24],
  299. [3, 31, 25],
  300. [3, 32, 24],
  301. [3, 33, 23],
  302. [5, 29, 25],
  303. [6, 28, 25],
  304. [6, 29, 24],
  305. [7, 27, 25],
  306. [7, 28, 24],
  307. [7, 29, 23],
  308. [1, 20, 38],
  309. [2, 19, 38],
  310. [2, 20, 37],
  311. [3, 18, 38],
  312. [3, 19, 37],
  313. [3, 20, 36],
  314. [5, 16, 38],
  315. [6, 15, 38],
  316. [6, 16, 37],
  317. [7, 14, 38],
  318. [7, 15, 37],
  319. [7, 16, 36],
  320. [3, 20, 35],
  321. [3, 22, 33],
  322. [7, 16, 35],
  323. [7, 22, 29],
  324. [9, 16, 33],
  325. [9, 20, 29],
  326. [10, 15, 33],
  327. [10, 16, 32],
  328. [10, 19, 29],
  329. [10, 20, 28],
  330. [13, 12, 33],
  331. [11, 14, 33],
  332. [11, 15, 32],
  333. [11, 16, 31],
  334. [11, 18, 29],
  335. [11, 19, 28],
  336. [11, 20, 27],
  337. [14, 12, 32],
  338. [12, 15, 31],
  339. [12, 16, 30],
  340. [12, 17, 29],
  341. [12, 18, 28],
  342. [12, 19, 27],
  343. [12, 20, 26],
  344. [0, 33, 25],
  345. [1, 32, 25],
  346. [1, 33, 24],
  347. [2, 31, 25],
  348. [2, 32, 24],
  349. [2, 33, 23],
  350. [3, 30, 25],
  351. [3, 31, 24],
  352. [3, 32, 23],
  353. [4, 29, 25],
  354. [5, 28, 25],
  355. [5, 29, 24],
  356. [6, 27, 25],
  357. [6, 28, 24],
  358. [6, 29, 23],
  359. [7, 26, 25],
  360. [7, 27, 24],
  361. [7, 28, 23],
  362. [0, 20, 38],
  363. [1, 19, 38],
  364. [1, 20, 37],
  365. [2, 18, 38],
  366. [2, 19, 37],
  367. [2, 20, 36],
  368. [3, 17, 38],
  369. [3, 18, 37],
  370. [3, 19, 36],
  371. [4, 16, 38],
  372. [5, 15, 38],
  373. [5, 16, 37],
  374. [6, 14, 38],
  375. [6, 15, 37],
  376. [6, 16, 36],
  377. [7, 13, 38],
  378. [7, 14, 37],
  379. [7, 15, 36],
  380. [2, 20, 35],
  381. [2, 22, 33],
  382. [3, 19, 35],
  383. [3, 20, 34],
  384. [3, 21, 33],
  385. [3, 22, 32],
  386. [6, 16, 35],
  387. [6, 22, 29],
  388. [7, 15, 35],
  389. [7, 16, 34],
  390. [7, 21, 29],
  391. [7, 22, 28],
  392. [8, 16, 33],
  393. [8, 20, 29],
  394. [9, 15, 33],
  395. [9, 16, 32],
  396. [9, 19, 29],
  397. [9, 20, 28],
  398. [10, 14, 33],
  399. [10, 15, 32],
  400. [10, 16, 31],
  401. [10, 18, 29],
  402. [10, 19, 28],
  403. [10, 20, 27],
  404. [13, 11, 33],
  405. [13, 12, 32],
  406. [11, 14, 32],
  407. [11, 15, 31],
  408. [11, 16, 30],
  409. [11, 17, 29],
  410. [11, 18, 28],
  411. [11, 19, 27],
  412. [11, 20, 26],
  413. [14, 12, 31],
  414. [12, 15, 30],
  415. [12, 17, 28],
  416. [12, 18, 27],
  417. [12, 19, 26],
  418. [0, 32, 25],
  419. [0, 33, 24],
  420. [1, 31, 25],
  421. [1, 32, 24],
  422. [1, 33, 23],
  423. [2, 30, 25],
  424. [2, 31, 24],
  425. [2, 32, 23],
  426. [3, 30, 24],
  427. [3, 31, 23],
  428. [4, 28, 25],
  429. [4, 29, 24],
  430. [5, 27, 25],
  431. [5, 28, 24],
  432. [5, 29, 23],
  433. [6, 26, 25],
  434. [6, 27, 24],
  435. [6, 28, 23],
  436. [7, 26, 24],
  437. [7, 27, 23],
  438. [0, 19, 38],
  439. [0, 20, 37],
  440. [1, 18, 38],
  441. [1, 19, 37],
  442. [1, 20, 36],
  443. [2, 17, 38],
  444. [2, 18, 37],
  445. [2, 19, 36],
  446. [3, 17, 37],
  447. [3, 18, 36],
  448. [4, 15, 38],
  449. [4, 16, 37],
  450. [5, 14, 38],
  451. [5, 15, 37],
  452. [5, 16, 36],
  453. [6, 13, 38],
  454. [6, 14, 37],
  455. [6, 15, 36],
  456. [7, 13, 37],
  457. [7, 14, 36],
  458. [1, 20, 35],
  459. [1, 22, 33],
  460. [2, 19, 35],
  461. [2, 20, 34],
  462. [2, 21, 33],
  463. [2, 22, 32],
  464. [3, 18, 35],
  465. [3, 19, 34],
  466. [3, 21, 32],
  467. [3, 22, 31],
  468. [5, 16, 35],
  469. [5, 22, 29],
  470. [6, 15, 35],
  471. [6, 16, 34],
  472. [6, 21, 29],
  473. [6, 22, 28],
  474. [7, 14, 35],
  475. [7, 15, 34],
  476. [7, 21, 28],
  477. [7, 22, 27],
  478. [8, 15, 33],
  479. [8, 16, 32],
  480. [8, 19, 29],
  481. [8, 20, 28],
  482. [9, 14, 33],
  483. [9, 15, 32],
  484. [9, 16, 31],
  485. [9, 18, 29],
  486. [9, 19, 28],
  487. [9, 20, 27],
  488. [10, 13, 33],
  489. [10, 14, 32],
  490. [10, 15, 31],
  491. [10, 16, 30],
  492. [10, 17, 29],
  493. [10, 18, 28],
  494. [10, 19, 27],
  495. [10, 20, 26],
  496. [13, 11, 32],
  497. [13, 12, 31],
  498. [11, 14, 31],
  499. [11, 15, 30],
  500. [11, 17, 28],
  501. [11, 18, 27],
  502. [11, 19, 26],
  503. [14, 12, 30],
  504. [12, 17, 27],
  505. [12, 18, 26],
  506. [0, 31, 25],
  507. [0, 32, 24],
  508. [0, 33, 23],
  509. [1, 30, 25],
  510. [1, 31, 24],
  511. [1, 32, 23],
  512. [2, 30, 24],
  513. [2, 31, 23],
  514. [3, 30, 23],
  515. [4, 27, 25],
  516. [4, 28, 24],
  517. [4, 29, 23],
  518. [5, 26, 25],
  519. [5, 27, 24],
  520. [5, 28, 23],
  521. [6, 26, 24],
  522. [6, 27, 23],
  523. [7, 26, 23],
  524. [0, 18, 38],
  525. [0, 19, 37],
  526. [0, 20, 36],
  527. [1, 17, 38],
  528. [1, 18, 37],
  529. [1, 19, 36],
  530. [2, 17, 37],
  531. [2, 18, 36],
  532. [3, 17, 36],
  533. [4, 14, 38],
  534. [4, 15, 37],
  535. [4, 16, 36],
  536. [5, 13, 38],
  537. [5, 14, 37],
  538. [5, 15, 36],
  539. [6, 13, 37],
  540. [6, 14, 36],
  541. [7, 13, 36],
  542. [0, 20, 35],
  543. [0, 22, 33],
  544. [1, 19, 35],
  545. [1, 20, 34],
  546. [1, 21, 33],
  547. [1, 22, 32],
  548. [2, 18, 35],
  549. [2, 19, 34],
  550. [2, 21, 32],
  551. [2, 22, 31],
  552. [3, 17, 35],
  553. [3, 18, 34],
  554. [3, 21, 31],
  555. [3, 22, 30],
  556. [4, 16, 35],
  557. [4, 22, 29],
  558. [5, 15, 35],
  559. [5, 16, 34],
  560. [5, 21, 29],
  561. [5, 22, 28],
  562. [6, 14, 35],
  563. [6, 15, 34],
  564. [6, 21, 28],
  565. [6, 22, 27],
  566. [7, 13, 35],
  567. [7, 14, 34],
  568. [7, 21, 27],
  569. [7, 22, 26],
  570. [8, 14, 33],
  571. [8, 15, 32],
  572. [8, 16, 31],
  573. [8, 18, 29],
  574. [8, 19, 28],
  575. [8, 20, 27],
  576. [9, 13, 33],
  577. [9, 14, 32],
  578. [9, 15, 31],
  579. [9, 16, 30],
  580. [9, 17, 29],
  581. [9, 18, 28],
  582. [9, 19, 27],
  583. [9, 20, 26],
  584. [10, 13, 32],
  585. [10, 14, 31],
  586. [10, 15, 30],
  587. [10, 17, 28],
  588. [10, 18, 27],
  589. [10, 19, 26],
  590. [13, 11, 31],
  591. [13, 12, 30],
  592. [11, 14, 30],
  593. [11, 17, 27],
  594. [11, 18, 26],
  595. [12, 17, 26],
  596. [0, 30, 25],
  597. [0, 31, 24],
  598. [0, 32, 23],
  599. [1, 30, 24],
  600. [1, 31, 23],
  601. [2, 30, 23],
  602. [4, 26, 25],
  603. [4, 27, 24],
  604. [4, 28, 23],
  605. [5, 26, 24],
  606. [5, 27, 23],
  607. [6, 26, 23],
  608. [0, 17, 38],
  609. [0, 18, 37],
  610. [0, 19, 36],
  611. [1, 17, 37],
  612. [1, 18, 36],
  613. [2, 17, 36],
  614. [4, 13, 38],
  615. [4, 14, 37],
  616. [4, 15, 36],
  617. [5, 13, 37],
  618. [5, 14, 36],
  619. [6, 13, 36],
  620. [0, 19, 35],
  621. [0, 20, 34],
  622. [0, 21, 33],
  623. [0, 22, 32],
  624. [1, 18, 35],
  625. [1, 19, 34],
  626. [1, 21, 32],
  627. [1, 22, 31],
  628. [2, 17, 35],
  629. [2, 18, 34],
  630. [2, 21, 31],
  631. [2, 22, 30],
  632. [3, 17, 34],
  633. [3, 21, 30],
  634. [4, 15, 35],
  635. [4, 16, 34],
  636. [4, 21, 29],
  637. [4, 22, 28],
  638. [5, 14, 35],
  639. [5, 15, 34],
  640. [5, 21, 28],
  641. [5, 22, 27],
  642. [6, 13, 35],
  643. [6, 14, 34],
  644. [6, 21, 27],
  645. [6, 22, 26],
  646. [7, 13, 34],
  647. [7, 21, 26],
  648. [8, 13, 33],
  649. [8, 14, 32],
  650. [8, 15, 31],
  651. [8, 16, 30],
  652. [8, 17, 29],
  653. [8, 18, 28],
  654. [8, 19, 27],
  655. [8, 20, 26],
  656. [9, 13, 32],
  657. [9, 14, 31],
  658. [9, 15, 30],
  659. [9, 17, 28],
  660. [9, 18, 27],
  661. [9, 19, 26],
  662. [10, 13, 31],
  663. [10, 14, 30],
  664. [10, 17, 27],
  665. [10, 18, 26],
  666. [13, 11, 30],
  667. [11, 17, 26],
  668. [0, 30, 24],
  669. [0, 31, 23],
  670. [1, 30, 23],
  671. [4, 26, 24],
  672. [4, 27, 23],
  673. [5, 26, 23],
  674. [0, 17, 37],
  675. [0, 18, 36],
  676. [1, 17, 36],
  677. [4, 13, 37],
  678. [4, 14, 36],
  679. [5, 13, 36],
  680. [0, 18, 35],
  681. [0, 19, 34],
  682. [0, 21, 32],
  683. [0, 22, 31],
  684. [1, 17, 35],
  685. [1, 18, 34],
  686. [1, 21, 31],
  687. [1, 22, 30],
  688. [2, 17, 34],
  689. [2, 21, 30],
  690. [4, 14, 35],
  691. [4, 15, 34],
  692. [4, 21, 28],
  693. [4, 22, 27],
  694. [5, 13, 35],
  695. [5, 14, 34],
  696. [5, 21, 27],
  697. [5, 22, 26],
  698. [6, 13, 34],
  699. [6, 21, 26],
  700. [8, 13, 32],
  701. [8, 14, 31],
  702. [8, 15, 30],
  703. [8, 17, 28],
  704. [8, 18, 27],
  705. [8, 19, 26],
  706. [9, 13, 31],
  707. [9, 14, 30],
  708. [9, 17, 27],
  709. [9, 18, 26],
  710. [10, 13, 30],
  711. [10, 17, 26],
  712. [0, 30, 23],
  713. [4, 26, 23],
  714. [0, 17, 36],
  715. [4, 13, 36],
  716. [0, 17, 35],
  717. [0, 18, 34],
  718. [0, 21, 31],
  719. [0, 22, 30],
  720. [1, 17, 34],
  721. [1, 21, 30],
  722. [4, 13, 35],
  723. [4, 14, 34],
  724. [4, 21, 27],
  725. [4, 22, 26],
  726. [5, 13, 34],
  727. [5, 21, 26],
  728. [8, 13, 31],
  729. [8, 14, 30],
  730. [8, 17, 27],
  731. [8, 18, 26],
  732. [9, 13, 30],
  733. [9, 17, 26],
  734. [0, 17, 34],
  735. [0, 21, 30],
  736. [4, 13, 34],
  737. [4, 21, 26],
  738. [8, 13, 30],
  739. [8, 17, 26],
  740. ],
  741. dtype=torch.int64,
  742. )
  743. signs = torch.tensor(
  744. [
  745. 1.0,
  746. -1.0,
  747. -1.0,
  748. 1.0,
  749. 1.0,
  750. -1.0,
  751. 1.0,
  752. -1.0,
  753. 1.0,
  754. 1.0,
  755. -1.0,
  756. -1.0,
  757. -1.0,
  758. -1.0,
  759. -1.0,
  760. 1.0,
  761. 1.0,
  762. 1.0,
  763. 1.0,
  764. 1.0,
  765. 1.0,
  766. -1.0,
  767. -1.0,
  768. -1.0,
  769. 1.0,
  770. -1.0,
  771. 1.0,
  772. 1.0,
  773. -1.0,
  774. -1.0,
  775. 1.0,
  776. 1.0,
  777. 1.0,
  778. -1.0,
  779. -1.0,
  780. -1.0,
  781. -1.0,
  782. -1.0,
  783. -1.0,
  784. -1.0,
  785. -1.0,
  786. -1.0,
  787. 1.0,
  788. 1.0,
  789. 1.0,
  790. 1.0,
  791. 1.0,
  792. 1.0,
  793. 1.0,
  794. 1.0,
  795. 1.0,
  796. 1.0,
  797. 1.0,
  798. 1.0,
  799. -1.0,
  800. -1.0,
  801. -1.0,
  802. -1.0,
  803. -1.0,
  804. -1.0,
  805. 1.0,
  806. -1.0,
  807. -1.0,
  808. 1.0,
  809. 1.0,
  810. -1.0,
  811. 1.0,
  812. 1.0,
  813. -1.0,
  814. -1.0,
  815. 1.0,
  816. 1.0,
  817. 1.0,
  818. 1.0,
  819. -1.0,
  820. -1.0,
  821. -1.0,
  822. 1.0,
  823. 1.0,
  824. 1.0,
  825. -1.0,
  826. -1.0,
  827. -1.0,
  828. -1.0,
  829. -1.0,
  830. -1.0,
  831. -1.0,
  832. -1.0,
  833. -1.0,
  834. -1.0,
  835. -1.0,
  836. -1.0,
  837. -1.0,
  838. 1.0,
  839. 1.0,
  840. 1.0,
  841. 1.0,
  842. 1.0,
  843. 1.0,
  844. 1.0,
  845. 1.0,
  846. 1.0,
  847. 1.0,
  848. 1.0,
  849. 1.0,
  850. 1.0,
  851. 1.0,
  852. 1.0,
  853. 1.0,
  854. 1.0,
  855. 1.0,
  856. -1.0,
  857. -1.0,
  858. -1.0,
  859. -1.0,
  860. -1.0,
  861. -1.0,
  862. -1.0,
  863. -1.0,
  864. -1.0,
  865. 1.0,
  866. -1.0,
  867. 1.0,
  868. 1.0,
  869. -1.0,
  870. -1.0,
  871. -1.0,
  872. 1.0,
  873. -1.0,
  874. -1.0,
  875. 1.0,
  876. 1.0,
  877. 1.0,
  878. -1.0,
  879. 1.0,
  880. 1.0,
  881. -1.0,
  882. -1.0,
  883. 1.0,
  884. 1.0,
  885. 1.0,
  886. -1.0,
  887. -1.0,
  888. -1.0,
  889. 1.0,
  890. 1.0,
  891. 1.0,
  892. 1.0,
  893. 1.0,
  894. -1.0,
  895. -1.0,
  896. -1.0,
  897. -1.0,
  898. 1.0,
  899. 1.0,
  900. -1.0,
  901. -1.0,
  902. -1.0,
  903. -1.0,
  904. -1.0,
  905. -1.0,
  906. -1.0,
  907. -1.0,
  908. -1.0,
  909. -1.0,
  910. -1.0,
  911. -1.0,
  912. -1.0,
  913. 1.0,
  914. 1.0,
  915. 1.0,
  916. 1.0,
  917. 1.0,
  918. 1.0,
  919. 1.0,
  920. 1.0,
  921. 1.0,
  922. 1.0,
  923. 1.0,
  924. 1.0,
  925. 1.0,
  926. 1.0,
  927. 1.0,
  928. 1.0,
  929. 1.0,
  930. 1.0,
  931. 1.0,
  932. 1.0,
  933. -1.0,
  934. -1.0,
  935. -1.0,
  936. -1.0,
  937. -1.0,
  938. -1.0,
  939. -1.0,
  940. -1.0,
  941. -1.0,
  942. -1.0,
  943. 1.0,
  944. -1.0,
  945. 1.0,
  946. 1.0,
  947. -1.0,
  948. -1.0,
  949. 1.0,
  950. 1.0,
  951. -1.0,
  952. -1.0,
  953. -1.0,
  954. 1.0,
  955. -1.0,
  956. -1.0,
  957. 1.0,
  958. 1.0,
  959. -1.0,
  960. -1.0,
  961. 1.0,
  962. 1.0,
  963. 1.0,
  964. 1.0,
  965. -1.0,
  966. -1.0,
  967. 1.0,
  968. 1.0,
  969. 1.0,
  970. -1.0,
  971. -1.0,
  972. -1.0,
  973. 1.0,
  974. 1.0,
  975. 1.0,
  976. 1.0,
  977. -1.0,
  978. -1.0,
  979. -1.0,
  980. -1.0,
  981. 1.0,
  982. 1.0,
  983. 1.0,
  984. 1.0,
  985. -1.0,
  986. -1.0,
  987. -1.0,
  988. 1.0,
  989. -1.0,
  990. -1.0,
  991. -1.0,
  992. -1.0,
  993. -1.0,
  994. -1.0,
  995. -1.0,
  996. -1.0,
  997. -1.0,
  998. -1.0,
  999. -1.0,
  1000. 1.0,
  1001. 1.0,
  1002. 1.0,
  1003. 1.0,
  1004. 1.0,
  1005. 1.0,
  1006. 1.0,
  1007. 1.0,
  1008. 1.0,
  1009. 1.0,
  1010. 1.0,
  1011. 1.0,
  1012. 1.0,
  1013. 1.0,
  1014. 1.0,
  1015. 1.0,
  1016. 1.0,
  1017. 1.0,
  1018. -1.0,
  1019. -1.0,
  1020. -1.0,
  1021. -1.0,
  1022. -1.0,
  1023. -1.0,
  1024. -1.0,
  1025. -1.0,
  1026. -1.0,
  1027. 1.0,
  1028. -1.0,
  1029. 1.0,
  1030. 1.0,
  1031. -1.0,
  1032. -1.0,
  1033. 1.0,
  1034. 1.0,
  1035. -1.0,
  1036. -1.0,
  1037. 1.0,
  1038. 1.0,
  1039. -1.0,
  1040. -1.0,
  1041. -1.0,
  1042. 1.0,
  1043. -1.0,
  1044. -1.0,
  1045. 1.0,
  1046. 1.0,
  1047. -1.0,
  1048. -1.0,
  1049. 1.0,
  1050. 1.0,
  1051. -1.0,
  1052. -1.0,
  1053. 1.0,
  1054. 1.0,
  1055. 1.0,
  1056. 1.0,
  1057. 1.0,
  1058. -1.0,
  1059. -1.0,
  1060. -1.0,
  1061. 1.0,
  1062. 1.0,
  1063. 1.0,
  1064. 1.0,
  1065. -1.0,
  1066. -1.0,
  1067. -1.0,
  1068. -1.0,
  1069. 1.0,
  1070. 1.0,
  1071. 1.0,
  1072. -1.0,
  1073. -1.0,
  1074. -1.0,
  1075. 1.0,
  1076. 1.0,
  1077. 1.0,
  1078. -1.0,
  1079. -1.0,
  1080. -1.0,
  1081. -1.0,
  1082. -1.0,
  1083. -1.0,
  1084. -1.0,
  1085. -1.0,
  1086. -1.0,
  1087. 1.0,
  1088. 1.0,
  1089. 1.0,
  1090. 1.0,
  1091. 1.0,
  1092. 1.0,
  1093. 1.0,
  1094. 1.0,
  1095. 1.0,
  1096. 1.0,
  1097. 1.0,
  1098. 1.0,
  1099. -1.0,
  1100. -1.0,
  1101. -1.0,
  1102. -1.0,
  1103. -1.0,
  1104. -1.0,
  1105. 1.0,
  1106. 1.0,
  1107. -1.0,
  1108. -1.0,
  1109. 1.0,
  1110. 1.0,
  1111. -1.0,
  1112. -1.0,
  1113. 1.0,
  1114. 1.0,
  1115. -1.0,
  1116. -1.0,
  1117. 1.0,
  1118. -1.0,
  1119. -1.0,
  1120. -1.0,
  1121. 1.0,
  1122. 1.0,
  1123. -1.0,
  1124. -1.0,
  1125. 1.0,
  1126. 1.0,
  1127. -1.0,
  1128. -1.0,
  1129. 1.0,
  1130. 1.0,
  1131. -1.0,
  1132. 1.0,
  1133. 1.0,
  1134. 1.0,
  1135. 1.0,
  1136. 1.0,
  1137. -1.0,
  1138. -1.0,
  1139. -1.0,
  1140. -1.0,
  1141. 1.0,
  1142. 1.0,
  1143. 1.0,
  1144. -1.0,
  1145. -1.0,
  1146. -1.0,
  1147. 1.0,
  1148. 1.0,
  1149. -1.0,
  1150. -1.0,
  1151. 1.0,
  1152. -1.0,
  1153. -1.0,
  1154. -1.0,
  1155. -1.0,
  1156. 1.0,
  1157. 1.0,
  1158. 1.0,
  1159. 1.0,
  1160. 1.0,
  1161. 1.0,
  1162. -1.0,
  1163. -1.0,
  1164. -1.0,
  1165. 1.0,
  1166. 1.0,
  1167. -1.0,
  1168. -1.0,
  1169. 1.0,
  1170. 1.0,
  1171. -1.0,
  1172. -1.0,
  1173. 1.0,
  1174. -1.0,
  1175. -1.0,
  1176. -1.0,
  1177. 1.0,
  1178. 1.0,
  1179. -1.0,
  1180. -1.0,
  1181. 1.0,
  1182. 1.0,
  1183. -1.0,
  1184. 1.0,
  1185. 1.0,
  1186. 1.0,
  1187. 1.0,
  1188. -1.0,
  1189. -1.0,
  1190. -1.0,
  1191. 1.0,
  1192. 1.0,
  1193. -1.0,
  1194. -1.0,
  1195. 1.0,
  1196. -1.0,
  1197. -1.0,
  1198. 1.0,
  1199. 1.0,
  1200. -1.0,
  1201. 1.0,
  1202. 1.0,
  1203. -1.0,
  1204. -1.0,
  1205. 1.0,
  1206. -1.0,
  1207. -1.0,
  1208. -1.0,
  1209. 1.0,
  1210. 1.0,
  1211. -1.0,
  1212. 1.0,
  1213. 1.0,
  1214. 1.0,
  1215. -1.0,
  1216. -1.0,
  1217. 1.0,
  1218. -1.0,
  1219. 1.0,
  1220. -1.0,
  1221. -1.0,
  1222. 1.0,
  1223. 1.0,
  1224. -1.0,
  1225. ],
  1226. dtype=torch.float32,
  1227. )
  1228. coefficient_map = torch.tensor(
  1229. [
  1230. 0,
  1231. 0,
  1232. 0,
  1233. 0,
  1234. 0,
  1235. 0,
  1236. 1,
  1237. 1,
  1238. 1,
  1239. 1,
  1240. 1,
  1241. 1,
  1242. 1,
  1243. 1,
  1244. 1,
  1245. 1,
  1246. 1,
  1247. 1,
  1248. 1,
  1249. 1,
  1250. 1,
  1251. 1,
  1252. 1,
  1253. 1,
  1254. 2,
  1255. 2,
  1256. 2,
  1257. 2,
  1258. 2,
  1259. 2,
  1260. 2,
  1261. 2,
  1262. 2,
  1263. 2,
  1264. 2,
  1265. 2,
  1266. 2,
  1267. 2,
  1268. 2,
  1269. 2,
  1270. 2,
  1271. 2,
  1272. 2,
  1273. 2,
  1274. 2,
  1275. 2,
  1276. 2,
  1277. 2,
  1278. 2,
  1279. 2,
  1280. 2,
  1281. 2,
  1282. 2,
  1283. 2,
  1284. 2,
  1285. 2,
  1286. 2,
  1287. 2,
  1288. 2,
  1289. 2,
  1290. 3,
  1291. 3,
  1292. 3,
  1293. 3,
  1294. 3,
  1295. 3,
  1296. 3,
  1297. 3,
  1298. 3,
  1299. 3,
  1300. 3,
  1301. 3,
  1302. 3,
  1303. 3,
  1304. 3,
  1305. 3,
  1306. 3,
  1307. 3,
  1308. 3,
  1309. 3,
  1310. 3,
  1311. 3,
  1312. 3,
  1313. 3,
  1314. 3,
  1315. 3,
  1316. 3,
  1317. 3,
  1318. 3,
  1319. 3,
  1320. 3,
  1321. 3,
  1322. 3,
  1323. 3,
  1324. 3,
  1325. 3,
  1326. 3,
  1327. 3,
  1328. 3,
  1329. 3,
  1330. 3,
  1331. 3,
  1332. 3,
  1333. 3,
  1334. 3,
  1335. 3,
  1336. 3,
  1337. 3,
  1338. 3,
  1339. 3,
  1340. 3,
  1341. 3,
  1342. 3,
  1343. 3,
  1344. 3,
  1345. 3,
  1346. 3,
  1347. 3,
  1348. 3,
  1349. 3,
  1350. 4,
  1351. 4,
  1352. 4,
  1353. 4,
  1354. 4,
  1355. 4,
  1356. 4,
  1357. 4,
  1358. 4,
  1359. 4,
  1360. 4,
  1361. 4,
  1362. 4,
  1363. 4,
  1364. 4,
  1365. 4,
  1366. 4,
  1367. 4,
  1368. 4,
  1369. 4,
  1370. 4,
  1371. 4,
  1372. 4,
  1373. 4,
  1374. 4,
  1375. 4,
  1376. 4,
  1377. 4,
  1378. 4,
  1379. 4,
  1380. 4,
  1381. 4,
  1382. 4,
  1383. 4,
  1384. 4,
  1385. 4,
  1386. 4,
  1387. 4,
  1388. 4,
  1389. 4,
  1390. 4,
  1391. 4,
  1392. 4,
  1393. 4,
  1394. 4,
  1395. 4,
  1396. 4,
  1397. 4,
  1398. 4,
  1399. 4,
  1400. 4,
  1401. 4,
  1402. 4,
  1403. 4,
  1404. 4,
  1405. 4,
  1406. 4,
  1407. 4,
  1408. 4,
  1409. 4,
  1410. 4,
  1411. 4,
  1412. 4,
  1413. 4,
  1414. 4,
  1415. 4,
  1416. 4,
  1417. 4,
  1418. 4,
  1419. 4,
  1420. 4,
  1421. 4,
  1422. 4,
  1423. 4,
  1424. 4,
  1425. 4,
  1426. 4,
  1427. 4,
  1428. 5,
  1429. 5,
  1430. 5,
  1431. 5,
  1432. 5,
  1433. 5,
  1434. 5,
  1435. 5,
  1436. 5,
  1437. 5,
  1438. 5,
  1439. 5,
  1440. 5,
  1441. 5,
  1442. 5,
  1443. 5,
  1444. 5,
  1445. 5,
  1446. 5,
  1447. 5,
  1448. 5,
  1449. 5,
  1450. 5,
  1451. 5,
  1452. 5,
  1453. 5,
  1454. 5,
  1455. 5,
  1456. 5,
  1457. 5,
  1458. 5,
  1459. 5,
  1460. 5,
  1461. 5,
  1462. 5,
  1463. 5,
  1464. 5,
  1465. 5,
  1466. 5,
  1467. 5,
  1468. 5,
  1469. 5,
  1470. 5,
  1471. 5,
  1472. 5,
  1473. 5,
  1474. 5,
  1475. 5,
  1476. 5,
  1477. 5,
  1478. 5,
  1479. 5,
  1480. 5,
  1481. 5,
  1482. 5,
  1483. 5,
  1484. 5,
  1485. 5,
  1486. 5,
  1487. 5,
  1488. 5,
  1489. 5,
  1490. 5,
  1491. 5,
  1492. 5,
  1493. 5,
  1494. 5,
  1495. 5,
  1496. 5,
  1497. 5,
  1498. 5,
  1499. 5,
  1500. 5,
  1501. 5,
  1502. 5,
  1503. 5,
  1504. 5,
  1505. 5,
  1506. 5,
  1507. 5,
  1508. 5,
  1509. 5,
  1510. 5,
  1511. 5,
  1512. 6,
  1513. 6,
  1514. 6,
  1515. 6,
  1516. 6,
  1517. 6,
  1518. 6,
  1519. 6,
  1520. 6,
  1521. 6,
  1522. 6,
  1523. 6,
  1524. 6,
  1525. 6,
  1526. 6,
  1527. 6,
  1528. 6,
  1529. 6,
  1530. 6,
  1531. 6,
  1532. 6,
  1533. 6,
  1534. 6,
  1535. 6,
  1536. 6,
  1537. 6,
  1538. 6,
  1539. 6,
  1540. 6,
  1541. 6,
  1542. 6,
  1543. 6,
  1544. 6,
  1545. 6,
  1546. 6,
  1547. 6,
  1548. 6,
  1549. 6,
  1550. 6,
  1551. 6,
  1552. 6,
  1553. 6,
  1554. 6,
  1555. 6,
  1556. 6,
  1557. 6,
  1558. 6,
  1559. 6,
  1560. 6,
  1561. 6,
  1562. 6,
  1563. 6,
  1564. 6,
  1565. 6,
  1566. 6,
  1567. 6,
  1568. 6,
  1569. 6,
  1570. 6,
  1571. 6,
  1572. 6,
  1573. 6,
  1574. 6,
  1575. 6,
  1576. 6,
  1577. 6,
  1578. 6,
  1579. 6,
  1580. 6,
  1581. 6,
  1582. 6,
  1583. 6,
  1584. 6,
  1585. 6,
  1586. 6,
  1587. 6,
  1588. 6,
  1589. 6,
  1590. 7,
  1591. 7,
  1592. 7,
  1593. 7,
  1594. 7,
  1595. 7,
  1596. 7,
  1597. 7,
  1598. 7,
  1599. 7,
  1600. 7,
  1601. 7,
  1602. 7,
  1603. 7,
  1604. 7,
  1605. 7,
  1606. 7,
  1607. 7,
  1608. 7,
  1609. 7,
  1610. 7,
  1611. 7,
  1612. 7,
  1613. 7,
  1614. 7,
  1615. 7,
  1616. 7,
  1617. 7,
  1618. 7,
  1619. 7,
  1620. 7,
  1621. 7,
  1622. 7,
  1623. 7,
  1624. 7,
  1625. 7,
  1626. 7,
  1627. 7,
  1628. 7,
  1629. 7,
  1630. 7,
  1631. 7,
  1632. 7,
  1633. 7,
  1634. 7,
  1635. 7,
  1636. 7,
  1637. 7,
  1638. 7,
  1639. 7,
  1640. 7,
  1641. 7,
  1642. 7,
  1643. 7,
  1644. 7,
  1645. 7,
  1646. 7,
  1647. 7,
  1648. 7,
  1649. 7,
  1650. 8,
  1651. 8,
  1652. 8,
  1653. 8,
  1654. 8,
  1655. 8,
  1656. 8,
  1657. 8,
  1658. 8,
  1659. 8,
  1660. 8,
  1661. 8,
  1662. 8,
  1663. 8,
  1664. 8,
  1665. 8,
  1666. 8,
  1667. 8,
  1668. 8,
  1669. 8,
  1670. 8,
  1671. 8,
  1672. 8,
  1673. 8,
  1674. 8,
  1675. 8,
  1676. 8,
  1677. 8,
  1678. 8,
  1679. 8,
  1680. 8,
  1681. 8,
  1682. 8,
  1683. 8,
  1684. 8,
  1685. 8,
  1686. 9,
  1687. 9,
  1688. 9,
  1689. 9,
  1690. 9,
  1691. 9,
  1692. 9,
  1693. 9,
  1694. 9,
  1695. 9,
  1696. 9,
  1697. 9,
  1698. 9,
  1699. 9,
  1700. 9,
  1701. 9,
  1702. 9,
  1703. 9,
  1704. 10,
  1705. 10,
  1706. 10,
  1707. 10,
  1708. 10,
  1709. 10,
  1710. ],
  1711. dtype=torch.int64,
  1712. )
  1713. def determinant_to_polynomial(
  1714. A: Tensor,
  1715. ) -> Tensor:
  1716. r"""Represent the determinant by the 10th polynomial, used for 5PC solver [@nister2004efficient].
  1717. Args:
  1718. A: Tensor :math:`(*, 3, 13)`.
  1719. Returns:
  1720. a degree 10 poly, representing determinant (Eqn. 14 in the paper).
  1721. """
  1722. B, device, dtype = A.shape[0], A.device, A.dtype
  1723. global multiplication_indices, signs, coefficient_map # noqa: PLW0603
  1724. multiplication_indices = multiplication_indices.to(device)
  1725. signs = signs.to(device, dtype)
  1726. coefficient_map = coefficient_map.to(device)
  1727. A_flat = A.view(B, -1)
  1728. gathered_values = A_flat[:, multiplication_indices]
  1729. products = torch.prod(gathered_values, dim=-1)
  1730. signed_products = products * signs
  1731. cs = torch.zeros(B, 11, device=device, dtype=dtype)
  1732. batch_coefficient_map = coefficient_map.repeat(B, 1)
  1733. cs.scatter_add_(dim=1, index=batch_coefficient_map, src=signed_products)
  1734. return cs