K i~dZddlmZddlmZddlmZddlmZ m Z m Z mZmZddZddZdd Z dd Z dd Zdd d dZy)z,Functions returning normal forms of matrices)ZZ)Poly) DomainMatrix)smith_normal_formis_smith_normal_formsmith_normal_decompinvariant_factorshermite_normal_formNct|dd}|jd}tj|}|xs|}||j |}|S)zConvert Matrix to DomainMatrixringNcFt|tr|jS|SN) isinstanceras_expr)es `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/matrices/normalforms.pyz_to_domain..sZ4-@aiika)getattr applyfuncr from_Matrix convert_to)mdomainr dMs r _to_domainrsS 1fd #D GHA  ! !! $B ^tF  ]]6 " IrcLt||}t|jS)a Return the Smith Normal Form of a matrix `m` over the ring `domain`. This will only work if the ring is a principal ideal domain. Examples ======== >>> from sympy import Matrix, ZZ >>> from sympy.matrices.normalforms import smith_normal_form >>> m = Matrix([[12, 6, 4], [3, 9, 6], [2, 16, 14]]) >>> print(smith_normal_form(m, domain=ZZ)) Matrix([[1, 0, 0], [0, 10, 0], [0, 0, 30]]) )r_snf to_Matrixrrrs rrrs# Av B 8   rc0t||}t|S)z8 Checks that the matrix is in Smith Normal Form )r_is_snfr s rrr0s Av B 2;rct||}t|\}}}|j|j|jfS)a Return the Smith Normal Decomposition of a matrix `m` over the ring `domain`. This will only work if the ring is a principal ideal domain. Examples ======== >>> from sympy import Matrix, ZZ >>> from sympy.matrices.normalforms import smith_normal_decomp >>> m = Matrix([[12, 6, 4], [3, 9, 6], [2, 16, 14]]) >>> a, s, t = smith_normal_decomp(m, domain=ZZ) >>> assert a == s * m * t )r_sndr)rrrasts rrr8s> Av B2hGAq! ;;=!++- 66rct||t}tfd|D}t|dr;|jj r%|jfdtfd|D}|S)a9 Return the tuple of abelian invariants for a matrix `m` (as in the Smith-Normal form) References ========== .. [1] https://en.wikipedia.org/wiki/Smith_normal_form#Algorithm .. [2] https://web.archive.org/web/20200331143852/https://sierra.nmsu.edu/morandi/notes/SmithNormalForm.pdf c3TK|]}jj|!ywr)rto_sympy).0frs r z$invariant_factors..Ys ;aBII&&q);s%(r cHt|jjS)N)r)rsymbolsr)r,Ks rrz#invariant_factors..^sQ !(( Crc3.K|] }|ywr)r+r,to_polys rr-z$invariant_factors.._s81GAJ8s)r_invftuplehasattrr is_PolynomialRing)rrfactorsr0rr3s @@@rr r Ksd Av BBiG;7;;Gq& 66 # #ACG888G NrFD check_rankc|)tj|stt|}t|j||j S)a Compute the Hermite Normal Form of a Matrix *A* of integers. Examples ======== >>> from sympy import Matrix >>> from sympy.matrices.normalforms import hermite_normal_form >>> m = Matrix([[12, 6, 4], [3, 9, 6], [2, 16, 14]]) >>> print(hermite_normal_form(m)) Matrix([[10, 0, 2], [0, 15, 3], [0, 0, 2]]) Parameters ========== A : $m \times n$ ``Matrix`` of integers. D : int, optional Let $W$ be the HNF of *A*. If known in advance, a positive integer *D* being any multiple of $\det(W)$ may be provided. In this case, if *A* also has rank $m$, then we may use an alternative algorithm that works mod *D* in order to prevent coefficient explosion. check_rank : boolean, optional (default=False) The basic assumption is that, if you pass a value for *D*, then you already believe that *A* has rank $m$, so we do not waste time checking it for you. If you do want this to be checked (and the ordinary, non-modulo *D* algorithm to be used if the check fails), then set *check_rank* to ``True``. Returns ======= ``Matrix`` The HNF of matrix *A*. Raises ====== DMDomainError If the domain of the matrix is not :ref:`ZZ`. DMShapeError If the mod *D* algorithm is used but the matrix has more rows than columns. References ========== .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.* (See Algorithms 2.4.5 and 2.4.8.) r9)rof_typeint_hnf_repr)Ar:r;s rr r cs=n }RZZ] s1vJ ! 3 = = ??rr)__doc__sympy.polys.domains.integerringrsympy.polys.polytoolsrsympy.polys.matricesr sympy.polys.matrices.normalformsrrrr"rr$r r4r r?rr2rrrGsA2.&-  &7&0!%9@r