normalforms.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. '''Functions returning normal forms of matrices'''
  2. from collections import defaultdict
  3. from .domainmatrix import DomainMatrix
  4. from .exceptions import DMDomainError, DMShapeError
  5. from sympy.ntheory.modular import symmetric_residue
  6. from sympy.polys.domains import QQ, ZZ
  7. # TODO (future work):
  8. # There are faster algorithms for Smith and Hermite normal forms, which
  9. # we should implement. See e.g. the Kannan-Bachem algorithm:
  10. # <https://www.researchgate.net/publication/220617516_Polynomial_Algorithms_for_Computing_the_Smith_and_Hermite_Normal_Forms_of_an_Integer_Matrix>
  11. def smith_normal_form(m):
  12. '''
  13. Return the Smith Normal Form of a matrix `m` over the ring `domain`.
  14. This will only work if the ring is a principal ideal domain.
  15. Examples
  16. ========
  17. >>> from sympy import ZZ
  18. >>> from sympy.polys.matrices import DomainMatrix
  19. >>> from sympy.polys.matrices.normalforms import smith_normal_form
  20. >>> m = DomainMatrix([[ZZ(12), ZZ(6), ZZ(4)],
  21. ... [ZZ(3), ZZ(9), ZZ(6)],
  22. ... [ZZ(2), ZZ(16), ZZ(14)]], (3, 3), ZZ)
  23. >>> print(smith_normal_form(m).to_Matrix())
  24. Matrix([[1, 0, 0], [0, 10, 0], [0, 0, 30]])
  25. '''
  26. invs = invariant_factors(m)
  27. smf = DomainMatrix.diag(invs, m.domain, m.shape)
  28. return smf
  29. def is_smith_normal_form(m):
  30. '''
  31. Checks that the matrix is in Smith Normal Form
  32. '''
  33. domain = m.domain
  34. shape = m.shape
  35. zero = domain.zero
  36. m = m.to_list()
  37. for i in range(shape[0]):
  38. for j in range(shape[1]):
  39. if i == j:
  40. continue
  41. if not m[i][j] == zero:
  42. return False
  43. upper = min(shape[0], shape[1])
  44. for i in range(1, upper):
  45. if m[i-1][i-1] == zero:
  46. if m[i][i] != zero:
  47. return False
  48. else:
  49. r = domain.div(m[i][i], m[i-1][i-1])[1]
  50. if r != zero:
  51. return False
  52. return True
  53. def add_columns(m, i, j, a, b, c, d):
  54. # replace m[:, i] by a*m[:, i] + b*m[:, j]
  55. # and m[:, j] by c*m[:, i] + d*m[:, j]
  56. for k in range(len(m)):
  57. e = m[k][i]
  58. m[k][i] = a*e + b*m[k][j]
  59. m[k][j] = c*e + d*m[k][j]
  60. def invariant_factors(m):
  61. '''
  62. Return the tuple of abelian invariants for a matrix `m`
  63. (as in the Smith-Normal form)
  64. References
  65. ==========
  66. [1] https://en.wikipedia.org/wiki/Smith_normal_form#Algorithm
  67. [2] https://web.archive.org/web/20200331143852/https://sierra.nmsu.edu/morandi/notes/SmithNormalForm.pdf
  68. '''
  69. domain = m.domain
  70. shape = m.shape
  71. m = m.to_list()
  72. return _smith_normal_decomp(m, domain, shape=shape, full=False)
  73. def smith_normal_decomp(m):
  74. '''
  75. Return the Smith-Normal form decomposition of matrix `m`.
  76. Examples
  77. ========
  78. >>> from sympy import ZZ
  79. >>> from sympy.polys.matrices import DomainMatrix
  80. >>> from sympy.polys.matrices.normalforms import smith_normal_decomp
  81. >>> m = DomainMatrix([[ZZ(12), ZZ(6), ZZ(4)],
  82. ... [ZZ(3), ZZ(9), ZZ(6)],
  83. ... [ZZ(2), ZZ(16), ZZ(14)]], (3, 3), ZZ)
  84. >>> a, s, t = smith_normal_decomp(m)
  85. >>> assert a == s * m * t
  86. '''
  87. domain = m.domain
  88. rows, cols = shape = m.shape
  89. m = m.to_list()
  90. invs, s, t = _smith_normal_decomp(m, domain, shape=shape, full=True)
  91. smf = DomainMatrix.diag(invs, domain, shape).to_dense()
  92. s = DomainMatrix(s, domain=domain, shape=(rows, rows))
  93. t = DomainMatrix(t, domain=domain, shape=(cols, cols))
  94. return smf, s, t
  95. def _smith_normal_decomp(m, domain, shape, full):
  96. '''
  97. Return the tuple of abelian invariants for a matrix `m`
  98. (as in the Smith-Normal form). If `full=True` then invertible matrices
  99. ``s, t`` such that the product ``s, m, t`` is the Smith Normal Form
  100. are also returned.
  101. '''
  102. if not domain.is_PID:
  103. msg = f"The matrix entries must be over a principal ideal domain, but got {domain}"
  104. raise ValueError(msg)
  105. rows, cols = shape
  106. zero = domain.zero
  107. one = domain.one
  108. def eye(n):
  109. return [[one if i == j else zero for i in range(n)] for j in range(n)]
  110. if 0 in shape:
  111. if full:
  112. return (), eye(rows), eye(cols)
  113. else:
  114. return ()
  115. if full:
  116. s = eye(rows)
  117. t = eye(cols)
  118. def add_rows(m, i, j, a, b, c, d):
  119. # replace m[i, :] by a*m[i, :] + b*m[j, :]
  120. # and m[j, :] by c*m[i, :] + d*m[j, :]
  121. for k in range(len(m[0])):
  122. e = m[i][k]
  123. m[i][k] = a*e + b*m[j][k]
  124. m[j][k] = c*e + d*m[j][k]
  125. def clear_column():
  126. # make m[1:, 0] zero by row and column operations
  127. pivot = m[0][0]
  128. for j in range(1, rows):
  129. if m[j][0] == zero:
  130. continue
  131. d, r = domain.div(m[j][0], pivot)
  132. if r == zero:
  133. add_rows(m, 0, j, 1, 0, -d, 1)
  134. if full:
  135. add_rows(s, 0, j, 1, 0, -d, 1)
  136. else:
  137. a, b, g = domain.gcdex(pivot, m[j][0])
  138. d_0 = domain.exquo(m[j][0], g)
  139. d_j = domain.exquo(pivot, g)
  140. add_rows(m, 0, j, a, b, d_0, -d_j)
  141. if full:
  142. add_rows(s, 0, j, a, b, d_0, -d_j)
  143. pivot = g
  144. def clear_row():
  145. # make m[0, 1:] zero by row and column operations
  146. pivot = m[0][0]
  147. for j in range(1, cols):
  148. if m[0][j] == zero:
  149. continue
  150. d, r = domain.div(m[0][j], pivot)
  151. if r == zero:
  152. add_columns(m, 0, j, 1, 0, -d, 1)
  153. if full:
  154. add_columns(t, 0, j, 1, 0, -d, 1)
  155. else:
  156. a, b, g = domain.gcdex(pivot, m[0][j])
  157. d_0 = domain.exquo(m[0][j], g)
  158. d_j = domain.exquo(pivot, g)
  159. add_columns(m, 0, j, a, b, d_0, -d_j)
  160. if full:
  161. add_columns(t, 0, j, a, b, d_0, -d_j)
  162. pivot = g
  163. # permute the rows and columns until m[0,0] is non-zero if possible
  164. ind = [i for i in range(rows) if m[i][0] != zero]
  165. if ind and ind[0] != zero:
  166. m[0], m[ind[0]] = m[ind[0]], m[0]
  167. if full:
  168. s[0], s[ind[0]] = s[ind[0]], s[0]
  169. else:
  170. ind = [j for j in range(cols) if m[0][j] != zero]
  171. if ind and ind[0] != zero:
  172. for row in m:
  173. row[0], row[ind[0]] = row[ind[0]], row[0]
  174. if full:
  175. for row in t:
  176. row[0], row[ind[0]] = row[ind[0]], row[0]
  177. # make the first row and column except m[0,0] zero
  178. while (any(m[0][i] != zero for i in range(1,cols)) or
  179. any(m[i][0] != zero for i in range(1,rows))):
  180. clear_column()
  181. clear_row()
  182. def to_domain_matrix(m):
  183. return DomainMatrix(m, shape=(len(m), len(m[0])), domain=domain)
  184. if m[0][0] != 0:
  185. c = domain.canonical_unit(m[0][0])
  186. if domain.is_Field:
  187. c = 1 / m[0][0]
  188. if c != domain.one:
  189. m[0][0] *= c
  190. if full:
  191. s[0] = [elem * c for elem in s[0]]
  192. if 1 in shape:
  193. invs = ()
  194. else:
  195. lower_right = [r[1:] for r in m[1:]]
  196. ret = _smith_normal_decomp(lower_right, domain,
  197. shape=(rows - 1, cols - 1), full=full)
  198. if full:
  199. invs, s_small, t_small = ret
  200. s2 = [[1] + [0]*(rows-1)] + [[0] + row for row in s_small]
  201. t2 = [[1] + [0]*(cols-1)] + [[0] + row for row in t_small]
  202. s, s2, t, t2 = list(map(to_domain_matrix, [s, s2, t, t2]))
  203. s = s2 * s
  204. t = t * t2
  205. s = s.to_list()
  206. t = t.to_list()
  207. else:
  208. invs = ret
  209. if m[0][0]:
  210. result = [m[0][0]]
  211. result.extend(invs)
  212. # in case m[0] doesn't divide the invariants of the rest of the matrix
  213. for i in range(len(result)-1):
  214. a, b = result[i], result[i+1]
  215. if b and domain.div(b, a)[1] != zero:
  216. if full:
  217. x, y, d = domain.gcdex(a, b)
  218. else:
  219. d = domain.gcd(a, b)
  220. alpha = domain.div(a, d)[0]
  221. if full:
  222. beta = domain.div(b, d)[0]
  223. add_rows(s, i, i + 1, 1, 0, x, 1)
  224. add_columns(t, i, i + 1, 1, y, 0, 1)
  225. add_rows(s, i, i + 1, 1, -alpha, 0, 1)
  226. add_columns(t, i, i + 1, 1, 0, -beta, 1)
  227. add_rows(s, i, i + 1, 0, 1, -1, 0)
  228. result[i+1] = b * alpha
  229. result[i] = d
  230. else:
  231. break
  232. else:
  233. if full:
  234. if rows > 1:
  235. s = s[1:] + [s[0]]
  236. if cols > 1:
  237. t = [row[1:] + [row[0]] for row in t]
  238. result = invs + (m[0][0],)
  239. if full:
  240. return tuple(result), s, t
  241. else:
  242. return tuple(result)
  243. def _gcdex(a, b):
  244. r"""
  245. This supports the functions that compute Hermite Normal Form.
  246. Explanation
  247. ===========
  248. Let x, y be the coefficients returned by the extended Euclidean
  249. Algorithm, so that x*a + y*b = g. In the algorithms for computing HNF,
  250. it is critical that x, y not only satisfy the condition of being small
  251. in magnitude -- namely that |x| <= |b|/g, |y| <- |a|/g -- but also that
  252. y == 0 when a | b.
  253. """
  254. x, y, g = ZZ.gcdex(a, b)
  255. if a != 0 and b % a == 0:
  256. y = 0
  257. x = -1 if a < 0 else 1
  258. return x, y, g
  259. def _hermite_normal_form(A):
  260. r"""
  261. Compute the Hermite Normal Form of DomainMatrix *A* over :ref:`ZZ`.
  262. Parameters
  263. ==========
  264. A : :py:class:`~.DomainMatrix` over domain :ref:`ZZ`.
  265. Returns
  266. =======
  267. :py:class:`~.DomainMatrix`
  268. The HNF of matrix *A*.
  269. Raises
  270. ======
  271. DMDomainError
  272. If the domain of the matrix is not :ref:`ZZ`.
  273. References
  274. ==========
  275. .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
  276. (See Algorithm 2.4.5.)
  277. """
  278. if not A.domain.is_ZZ:
  279. raise DMDomainError('Matrix must be over domain ZZ.')
  280. # We work one row at a time, starting from the bottom row, and working our
  281. # way up.
  282. m, n = A.shape
  283. A = A.to_ddm().copy()
  284. # Our goal is to put pivot entries in the rightmost columns.
  285. # Invariant: Before processing each row, k should be the index of the
  286. # leftmost column in which we have so far put a pivot.
  287. k = n
  288. for i in range(m - 1, -1, -1):
  289. if k == 0:
  290. # This case can arise when n < m and we've already found n pivots.
  291. # We don't need to consider any more rows, because this is already
  292. # the maximum possible number of pivots.
  293. break
  294. k -= 1
  295. # k now points to the column in which we want to put a pivot.
  296. # We want zeros in all entries to the left of the pivot column.
  297. for j in range(k - 1, -1, -1):
  298. if A[i][j] != 0:
  299. # Replace cols j, k by lin combs of these cols such that, in row i,
  300. # col j has 0, while col k has the gcd of their row i entries. Note
  301. # that this ensures a nonzero entry in col k.
  302. u, v, d = _gcdex(A[i][k], A[i][j])
  303. r, s = A[i][k] // d, A[i][j] // d
  304. add_columns(A, k, j, u, v, -s, r)
  305. b = A[i][k]
  306. # Do not want the pivot entry to be negative.
  307. if b < 0:
  308. add_columns(A, k, k, -1, 0, -1, 0)
  309. b = -b
  310. # The pivot entry will be 0 iff the row was 0 from the pivot col all the
  311. # way to the left. In this case, we are still working on the same pivot
  312. # col for the next row. Therefore:
  313. if b == 0:
  314. k += 1
  315. # If the pivot entry is nonzero, then we want to reduce all entries to its
  316. # right in the sense of the division algorithm, i.e. make them all remainders
  317. # w.r.t. the pivot as divisor.
  318. else:
  319. for j in range(k + 1, n):
  320. q = A[i][j] // b
  321. add_columns(A, j, k, 1, -q, 0, 1)
  322. # Finally, the HNF consists of those columns of A in which we succeeded in making
  323. # a nonzero pivot.
  324. return DomainMatrix.from_rep(A.to_dfm_or_ddm())[:, k:]
  325. def _hermite_normal_form_modulo_D(A, D):
  326. r"""
  327. Perform the mod *D* Hermite Normal Form reduction algorithm on
  328. :py:class:`~.DomainMatrix` *A*.
  329. Explanation
  330. ===========
  331. If *A* is an $m \times n$ matrix of rank $m$, having Hermite Normal Form
  332. $W$, and if *D* is any positive integer known in advance to be a multiple
  333. of $\det(W)$, then the HNF of *A* can be computed by an algorithm that
  334. works mod *D* in order to prevent coefficient explosion.
  335. Parameters
  336. ==========
  337. A : :py:class:`~.DomainMatrix` over :ref:`ZZ`
  338. $m \times n$ matrix, having rank $m$.
  339. D : :ref:`ZZ`
  340. Positive integer, known to be a multiple of the determinant of the
  341. HNF of *A*.
  342. Returns
  343. =======
  344. :py:class:`~.DomainMatrix`
  345. The HNF of matrix *A*.
  346. Raises
  347. ======
  348. DMDomainError
  349. If the domain of the matrix is not :ref:`ZZ`, or
  350. if *D* is given but is not in :ref:`ZZ`.
  351. DMShapeError
  352. If the matrix has more rows than columns.
  353. References
  354. ==========
  355. .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
  356. (See Algorithm 2.4.8.)
  357. """
  358. if not A.domain.is_ZZ:
  359. raise DMDomainError('Matrix must be over domain ZZ.')
  360. if not ZZ.of_type(D) or D < 1:
  361. raise DMDomainError('Modulus D must be positive element of domain ZZ.')
  362. def add_columns_mod_R(m, R, i, j, a, b, c, d):
  363. # replace m[:, i] by (a*m[:, i] + b*m[:, j]) % R
  364. # and m[:, j] by (c*m[:, i] + d*m[:, j]) % R
  365. for k in range(len(m)):
  366. e = m[k][i]
  367. m[k][i] = symmetric_residue((a * e + b * m[k][j]) % R, R)
  368. m[k][j] = symmetric_residue((c * e + d * m[k][j]) % R, R)
  369. W = defaultdict(dict)
  370. m, n = A.shape
  371. if n < m:
  372. raise DMShapeError('Matrix must have at least as many columns as rows.')
  373. A = A.to_list()
  374. k = n
  375. R = D
  376. for i in range(m - 1, -1, -1):
  377. k -= 1
  378. for j in range(k - 1, -1, -1):
  379. if A[i][j] != 0:
  380. u, v, d = _gcdex(A[i][k], A[i][j])
  381. r, s = A[i][k] // d, A[i][j] // d
  382. add_columns_mod_R(A, R, k, j, u, v, -s, r)
  383. b = A[i][k]
  384. if b == 0:
  385. A[i][k] = b = R
  386. u, v, d = _gcdex(b, R)
  387. for ii in range(m):
  388. W[ii][i] = u*A[ii][k] % R
  389. if W[i][i] == 0:
  390. W[i][i] = R
  391. for j in range(i + 1, m):
  392. q = W[i][j] // W[i][i]
  393. add_columns(W, j, i, 1, -q, 0, 1)
  394. R //= d
  395. return DomainMatrix(W, (m, m), ZZ).to_dense()
  396. def hermite_normal_form(A, *, D=None, check_rank=False):
  397. r"""
  398. Compute the Hermite Normal Form of :py:class:`~.DomainMatrix` *A* over
  399. :ref:`ZZ`.
  400. Examples
  401. ========
  402. >>> from sympy import ZZ
  403. >>> from sympy.polys.matrices import DomainMatrix
  404. >>> from sympy.polys.matrices.normalforms import hermite_normal_form
  405. >>> m = DomainMatrix([[ZZ(12), ZZ(6), ZZ(4)],
  406. ... [ZZ(3), ZZ(9), ZZ(6)],
  407. ... [ZZ(2), ZZ(16), ZZ(14)]], (3, 3), ZZ)
  408. >>> print(hermite_normal_form(m).to_Matrix())
  409. Matrix([[10, 0, 2], [0, 15, 3], [0, 0, 2]])
  410. Parameters
  411. ==========
  412. A : $m \times n$ ``DomainMatrix`` over :ref:`ZZ`.
  413. D : :ref:`ZZ`, optional
  414. Let $W$ be the HNF of *A*. If known in advance, a positive integer *D*
  415. being any multiple of $\det(W)$ may be provided. In this case, if *A*
  416. also has rank $m$, then we may use an alternative algorithm that works
  417. mod *D* in order to prevent coefficient explosion.
  418. check_rank : boolean, optional (default=False)
  419. The basic assumption is that, if you pass a value for *D*, then
  420. you already believe that *A* has rank $m$, so we do not waste time
  421. checking it for you. If you do want this to be checked (and the
  422. ordinary, non-modulo *D* algorithm to be used if the check fails), then
  423. set *check_rank* to ``True``.
  424. Returns
  425. =======
  426. :py:class:`~.DomainMatrix`
  427. The HNF of matrix *A*.
  428. Raises
  429. ======
  430. DMDomainError
  431. If the domain of the matrix is not :ref:`ZZ`, or
  432. if *D* is given but is not in :ref:`ZZ`.
  433. DMShapeError
  434. If the mod *D* algorithm is used but the matrix has more rows than
  435. columns.
  436. References
  437. ==========
  438. .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
  439. (See Algorithms 2.4.5 and 2.4.8.)
  440. """
  441. if not A.domain.is_ZZ:
  442. raise DMDomainError('Matrix must be over domain ZZ.')
  443. if D is not None and (not check_rank or A.convert_to(QQ).rank() == A.shape[0]):
  444. return _hermite_normal_form_modulo_D(A, D)
  445. else:
  446. return _hermite_normal_form(A)