+
    iB                         R t ^ RIHt ^RIHt ^RIHtHt ^ RIH	t	 ^ RI
HtHt R tR tR tR	 tR
 tR tR tR tR tRRRR/R ltR# )z,Functions returning normal forms of matrices)defaultdict)DomainMatrix)DMDomainErrorDMShapeError)symmetric_residue)QQZZc                r    \        V 4      p\        P                  ! WP                  V P                  4      pV# )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 ZZ
>>> from sympy.polys.matrices import DomainMatrix
>>> from sympy.polys.matrices.normalforms import smith_normal_form
>>> m = DomainMatrix([[ZZ(12), ZZ(6), ZZ(4)],
...                   [ZZ(3), ZZ(9), ZZ(6)],
...                   [ZZ(2), ZZ(16), ZZ(14)]], (3, 3), ZZ)
>>> print(smith_normal_form(m).to_Matrix())
Matrix([[1, 0, 0], [0, 10, 0], [0, 0, 30]])

)invariant_factorsr   diagdomainshape)minvssmfs   &  ^/var/www/html/photoedit/myenv/lib/python3.14/site-packages/sympy/polys/matrices/normalforms.pysmith_normal_formr      s-    $ QD


D((AGG
4CJ    c                v   V P                   pV P                  pVP                  pV P                  4       p \	        V^ ,          4       F<  p\	        V^,          4       F#  pWE8X  d   K  W,          V,          V8X  d   K!    R# 	  K>  	  \        V^ ,          V^,          4      p\	        ^V4       F  pW^,
          ,          V^,
          ,          V8X  d   W,          V,          V8w  d    R# K>  VP                  W,          V,          W^,
          ,          V^,
          ,          4      ^,          pWs8w  g   K   R# 	  R# )z0
Checks that the matrix is in Smith Normal Form
FT)r   r   zeroto_listrangemindiv)r   r   r   r   ijupperrs   &       r   is_smith_normal_formr   (   s     XXFGGE;;D			A58_uQxAv47d?	 !  a%(#E1e_qS6!A#;$tAw$  

147AcF1Q3K03Ay  r   c                 
   \        \        V 4      4       Fj  pW,          V,          pW8,          W@V,          V,          ,          ,           W,          V&   WX,          W`V,          V,          ,          ,           W,          V&   Kl  	  R # Nr   len	r   r   r   abcdkes	   &&&&&&&  r   add_columnsr*   E   sW     3q6]DG#A$q'	/Q#A$q'	/Q r   c                n    V P                   pV P                  pV P                  4       p \        WVRR7      # )a  
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

Fr   full)r   r   r   _smith_normal_decomp)r   r   r   s   &  r   r
   r
   N   s0     XXFGGE			AUCCr   c                   V P                   pV P                  ;w  r#pV P                  4       p \        WVRR7      w  rVp\        P
                  ! WQV4      P                  4       p\	        WaW"3R7      p\	        WqW33R7      pWV3# )a  
Return the Smith-Normal form decomposition of matrix `m`.

Examples
========

>>> from sympy import ZZ
>>> from sympy.polys.matrices import DomainMatrix
>>> from sympy.polys.matrices.normalforms import smith_normal_decomp
>>> m = DomainMatrix([[ZZ(12), ZZ(6), ZZ(4)],
...                   [ZZ(3), ZZ(9), ZZ(6)],
...                   [ZZ(2), ZZ(16), ZZ(14)]], (3, 3), ZZ)
>>> a, s, t = smith_normal_decomp(m)
>>> assert a == s * m * t
Tr,   )r   r   )r   r   r   r.   r   r   to_dense)	r   r   rowscolsr   r   str   s	   &        r   smith_normal_decompr5   `   s{      XXF JD			A%au4HJDQ


D%
0
9
9
;CQd\:AQd\:A19r   c           
       a aaaa a!a"a#a$a% SP                   '       g   RS 2p\        V4      hVw  o"o SP                  o%SP                  o!V!V%3R lp^ V9   d   S'       d   RV! S"4      V! S 4      3# R# S'       d   V! S"4      o#V! S 4      o$R oVVVV V"V#V%3R lpV VVV V$V%3R lp\	        S"4       Uu. uF  pS V,          ^ ,          S%8w  g   K  VNK  	  p	pV	'       df   V	^ ,          S%8w  dX   S V	^ ,          ,          S ^ ,          uS ^ &   S V	^ ,          &   S'       d(   S#V	^ ,          ,          S#^ ,          uS#^ &   S#V	^ ,          &   M\	        S 4       U
u. uF  p
S ^ ,          V
,          S%8w  g   K  V
NK  	  p	p
V	'       ds   V	^ ,          S%8w  de   S  F(  pW^ ,          ,          V^ ,          uV^ &   W^ ,          &   K*  	  S'       d/   S$ F(  pW^ ,          ,          V^ ,          uV^ &   W^ ,          &   K*  	  \
        ;QJ d*    V V%3R l\	        ^S 4       4       F  '       g   K   RM	  RM! V V%3R l\	        ^S 4       4       4      '       gW   \
        ;QJ d*    V V%3R	 l\	        ^S"4       4       F  '       g   K   RM	  RM! V V%3R	 l\	        ^S"4       4       4      '       d   V! 4        V! 4        K  V3R
 lpS ^ ,          ^ ,          ^ 8w  d   SP                  S ^ ,          ^ ,          4      pSP                  '       d   ^S ^ ,          ^ ,          ,          pVSP                  8w  dG   S ^ ,          ^ ;;,          V,          uu&   S'       d#   S#^ ,           Uu. uF  qV,          NK  	  upS#^ &   ^V9   d   RpEMS R,           Uu. uF  pVR,          NK  	  pp\        VSS"^,
          S ^,
          3SR7      pS'       d   Vw  ppp^.^ .S"^,
          ,          ,           .V Uu. uF  p^ .V,           NK  	  up,           p^.^ .S ^,
          ,          ,           .V Uu. uF  p^ .V,           NK  	  up,           p\        \        VS#VS$V.4      4      w  o#po$pVS#,          o#S$V,          o$S#P                  4       o#S$P                  4       o$MTpS ^ ,          ^ ,          '       Edy   S ^ ,          ^ ,          .pVP                  V4       \	        \        V4      ^,
          4       EF5  pVV,          VV^,           ,          ppV'       Ed   SP                  VV4      ^,          S%8w  d   S'       d   SP                  VV4      w  pppMSP!                  VV4      pSP                  VV4      ^ ,          pS'       d   SP                  VV4      ^ ,          pS! S#W^,           ^^ X^4       \#        S$W^,           ^X^ ^4       S! S#W^,           ^V) ^ ^4       \#        S$W^,           ^^ V) ^4       S! S#W^,           ^ ^R^ 4       VV,          VV^,           &   VVV&   EK6   Mp	  MmS'       dN   S"^8  d   S#R,          S#^ ,          .,           o#S ^8  d(   S$ Uu. uF  qR,          V^ ,          .,           NK  	  upo$VS ^ ,          ^ ,          3,           pS'       d   \%        V4      S#S$3# \%        V4      # u upi u up
i u upi u upi u upi u upi u upi )z
Return the tuple of abelian invariants for a matrix `m`
(as in the Smith-Normal form). If `full=True` then invertible matrices
``s, t`` such that the product ``s, m, t`` is the Smith Normal Form
are also returned.
zBThe matrix entries must be over a principal ideal domain, but got c           	         < \        V 4       UUu. uF&  p\        V 4       Uu. uF  q"V8X  d   SMSNK  	  upNK(  	  upp# u upi u uppi r    )r   )nr   r   oner   s   &  r   eye!_smith_normal_decomp.<locals>.eye   s>    EJ1XNX%(;(QQD((;XNN;Ns   AA A Ac                    \        \        V ^ ,          4      4       Fj  pW,          V,          pW8,          W@V,          V,          ,          ,           W,          V&   WX,          W`V,          V,          ,          ,           W,          V&   Kl  	  R#     Nr!   r#   s	   &&&&&&&  r   add_rows&_smith_normal_decomp.<locals>.add_rows   s\     s1Q4y!AQAcAd1gIoADGcAd1gIoADG "r   c            
        < S^ ,          ^ ,          p \        ^S4       F  pSV,          ^ ,          S8X  d   K  S
P                  SV,          ^ ,          V 4      w  r#VS8X  d+   S	! S^ V^^ V) ^4       S'       d   S	! S^ V^^ V) ^4       Kk  Km  S
P                  V SV,          ^ ,          4      w  rEpS
P                  SV,          ^ ,          V4      pS
P                  W4      pS	! S^ WWWV) 4       S'       d   S	! S^ WWWV) 4       Tp K  	  R# r=   )r   r   gcdexexquo)pivotr   r'   r   r$   r%   gd_0d_jr?   r   r-   r   r1   r3   r   s            r   clear_column*_smith_normal_decomp.<locals>.clear_column   s    !Qq$AtAw$::ad1gu-DADyAq!QA.Q1aQB2  !,,uad1g6all1Q47A.ll5,AqQcT2Q1#6  r   c            
      *  < S^ ,          ^ ,          p \        ^S	4       F  pS^ ,          V,          S8X  d   K  S
P                  S^ ,          V,          V 4      w  r#VS8X  d1   \        S^ V^^ V) ^4       S'       d   \        S^ V^^ V) ^4       Kq  Ks  S
P                  V S^ ,          V,          4      w  rEpS
P	                  S^ ,          V,          V4      pS
P	                  W4      p\        S^ WWWV) 4       S'       d   \        S^ WWWV) 4       Tp K  	  R# r=   )r   r   r*   rB   rC   )rD   r   r'   r   r$   r%   rE   rF   rG   r2   r   r-   r   r4   r   s            r   	clear_row'_smith_normal_decomp.<locals>.clear_row   s    !Qq$AtAw$::ad1gu-DADyAq!QA2q11aAr15  !,,uad1g6all1Q47A.ll5,Aq!51aASD9  r   c              3   J   <"   T F  pS^ ,          V,          S8g  x  K  	  R# 5ir=    .0r   r   r   s   & r   	<genexpr>'_smith_normal_decomp.<locals>.<genexpr>        61qtAw$    #TFc              3   J   <"   T F  pSV,          ^ ,          S8g  x  K  	  R# 5ir=   rN   rO   s   & r   rQ   rR      rS   rT   c                 V   < \        V \        V 4      \        V ^ ,          4      3SR7      # )r>   )r   r   )r   r"   )r   r   s   &r   to_domain_matrix._smith_normal_decomp.<locals>.to_domain_matrix   s#    Ac!fc!A$i%8HHr   :   NNr,   rN   )is_PID
ValueErrorr   r9   r   anycanonical_unitis_Fieldr.   listmapr   extendr"   r   rB   gcdr*   tuple)&r   r   r   r-   msgr:   rH   rK   r   indr   rowrW   r&   elemr   r   lower_rightrets_smallt_smalls2t2resultr$   r%   xyr'   alphabetar?   r2   r9   r1   r3   r4   r   s&   ff&f                           @@@@@@@r   r.   r.   |   s    ===RSYRZ[oJD$;;D
**CO 	Ezs4y#d)++III& ( * Dk
5kQqT!W_11kC
5
s1v~CF)QqT!aAiAi1OAaD!CF)+9+Q1aDqq+93q6T>&)a&k3q6#AF C*-!f+s1v'CFCAK  36a63336a66636a63336a666I 	tAw!|!!!A$q'*???AaDGA

?aDGqLG-.qT2TTqT2!Ez&'e,equue,";ax*7%("D'7#T!V$%g(Fgs!sg(FFB#T!V$%g(Fgs!sg(FFB$4q"an EFLAr1bQABA		A		ADtAwwA$q'ds6{1}%A!9fQqSkqAqVZZ1%a(D0$ll1a0GAq!

1a(A

1a(+!::a+A.DQq5!Q151!eQ1a8Qq5!eVQ:1!eQD5!<Qq5!QA6%iqsq	) &, axbEQqTFNax3451CWAx''151a
"V}a""V}m 6 :0 3
 -
 )G(FN 6s6   2ZZZ",Z"Z':Z,Z1Z6 Z;c                ~    \         P                  ! W4      w  r#pV ^ 8w  d   W,          ^ 8X  d   ^ pV ^ 8  d   RM^pW#V3# )a  
This supports the functions that compute Hermite Normal Form.

Explanation
===========

Let x, y be the coefficients returned by the extended Euclidean
Algorithm, so that x*a + y*b = g. In the algorithms for computing HNF,
it is critical that x, y not only satisfy the condition of being small
in magnitude -- namely that |x| <= |b|/g, |y| <- |a|/g -- but also that
y == 0 when a | b.

rZ   )r   rB   )r$   r%   rp   rq   rE   s   &&   r   _gcdexru   "  s?     hhqnGA!Av!%1*a%BQ7Nr   c                   V P                   P                  '       g   \        R4      hV P                  w  rV P	                  4       P                  4       p Tp\        V^,
          RR4       EF'  pV^ 8X  d    EMV^,          p\        V^,
          RR4       F|  pW,          V,          ^ 8w  g   K  \        W,          V,          W,          V,          4      w  rgpW,          V,          V,          W,          V,          V,          r\        WWVWz) V	4       K~  	  W,          V,          pV^ 8  d   \        WVR^ R^ 4       V) pV^ 8X  d   V^,          pK  \        V^,           V4       F*  pW,          V,          V,          p\        WV^V) ^ ^4       K,  	  EK*  	  \        P                  ! V P                  4       4      RVR13,          # )a  
Compute the Hermite Normal Form of DomainMatrix *A* over :ref:`ZZ`.

Parameters
==========

A : :py:class:`~.DomainMatrix` over domain :ref:`ZZ`.

Returns
=======

:py:class:`~.DomainMatrix`
    The HNF of matrix *A*.

Raises
======

DMDomainError
    If the domain of the matrix is not :ref:`ZZ`.

References
==========

.. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
   (See Algorithm 2.4.5.)

Matrix must be over domain ZZ.:NNNNrZ   )r   is_ZZr   r   to_ddmcopyr   ru   r*   r   from_repto_dfm_or_ddm)Ar   r8   r(   r   r   uvr'   r   r3   r%   qs   &            r   _hermite_normal_formr   7  s|   8 88>>><== 77DA	
A 	
A1q5"b!6 	Q q1ub"%AtAw!| !a!$q'2atAw!|QT!W\1A!2q1 & DGq5aQA.A 6FA
 1q5!_DGqLA!QAq1 %? "H   !23AqrE::r   c                   V P                   P                  '       g   \        R4      h\        P                  ! V4      '       d   V^8  d   \        R4      hR p\        \        4      pV P                  w  rEWT8  d   \        R4      hV P                  4       p TpTp\        V^,
          RR4       EF~  pV^,          p\        V^,
          RR4       Fz  p	W,          V	,          ^ 8w  g   K  \        W,          V,          W,          V	,          4      w  rpW,          V,          V,          W,          V	,          V,          rV! WWiWV) V4       K|  	  W,          V,          pV^ 8X  d   T;W,          V&   p\        W4      w  rp\        V4       F*  pWV,          V,          ,          V,          VV,          V&   K,  	  W8,          V,          ^ 8X  d   WsV,          V&   \        V^,           V4       F7  p	W8,          V	,          W8,          V,          ,          p\        W9V^V) ^ ^4       K9  	  W|,          pEK  	  \        W4V3\        4      P                  4       # )a  
Perform the mod *D* Hermite Normal Form reduction algorithm on
:py:class:`~.DomainMatrix` *A*.

Explanation
===========

If *A* is an $m \times n$ matrix of rank $m$, having Hermite Normal Form
$W$, and if *D* is any positive integer known in advance to be a multiple
of $\det(W)$, then the HNF of *A* can be computed by an algorithm that
works mod *D* in order to prevent coefficient explosion.

Parameters
==========

A : :py:class:`~.DomainMatrix` over :ref:`ZZ`
    $m \times n$ matrix, having rank $m$.
D : :ref:`ZZ`
    Positive integer, known to be a multiple of the determinant of the
    HNF of *A*.

Returns
=======

:py:class:`~.DomainMatrix`
    The HNF of matrix *A*.

Raises
======

DMDomainError
    If the domain of the matrix is not :ref:`ZZ`, or
    if *D* is given but is not in :ref:`ZZ`.

DMShapeError
    If the matrix has more rows than columns.

References
==========

.. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
   (See Algorithm 2.4.8.)

rw   z0Modulus D must be positive element of domain ZZ.c                 N   \        \        V 4      4       F  pW,          V,          p	\        WI,          WPV,          V,          ,          ,           V,          V4      W,          V&   \        Wi,          WpV,          V,          ,          ,           V,          V4      W,          V&   K  	  R # r    )r   r"   r   )
r   Rr   r   r$   r%   r&   r'   r(   r)   s
   &&&&&&&&  r   add_columns_mod_R8_hermite_normal_form_modulo_D.<locals>.add_columns_mod_R  sq     s1vAQA'qT!W)<(A1EADG'qT!W)<(A1EADG r   z2Matrix must have at least as many columns as rows.rZ   )r   rx   r   r   of_typer   dictr   r   r   r   ru   r*   r   r0   )r}   Dr   Wr   r8   r(   r   r   r   r~   r   r'   r   r3   r%   iir   s   &&                r   _hermite_normal_form_modulo_Dr     s   Z 88>>><==::a==AENOOF 	DA77DAuOPP			A	A	A1q5"b!	Qq1ub"%AtAw!| a!$q'2atAw!|QT!W\1!!aQB:	 &
 DG6OADGa,a(B2qzA~AbE!H 47a<aDGq1uaAQ147"AaQB1- ! 	
% "& q62&//11r   r   N
check_rankFc                  V P                   P                  '       g   \        R4      hVeM   V'       d:   V P                  \        4      P                  4       V P                  ^ ,          8X  d   \        W4      # \        V 4      # )a}  
Compute the Hermite Normal Form of :py:class:`~.DomainMatrix` *A* over
:ref:`ZZ`.

Examples
========

>>> from sympy import ZZ
>>> from sympy.polys.matrices import DomainMatrix
>>> from sympy.polys.matrices.normalforms import hermite_normal_form
>>> m = DomainMatrix([[ZZ(12), ZZ(6), ZZ(4)],
...                   [ZZ(3), ZZ(9), ZZ(6)],
...                   [ZZ(2), ZZ(16), ZZ(14)]], (3, 3), ZZ)
>>> print(hermite_normal_form(m).to_Matrix())
Matrix([[10, 0, 2], [0, 15, 3], [0, 0, 2]])

Parameters
==========

A : $m \times n$ ``DomainMatrix`` over :ref:`ZZ`.

D : :ref:`ZZ`, 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
=======

:py:class:`~.DomainMatrix`
    The HNF of matrix *A*.

Raises
======

DMDomainError
    If the domain of the matrix is not :ref:`ZZ`, or
    if *D* is given but is not in :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.)

rw   )	r   rx   r   
convert_tor   rankr   r   r   )r}   r   r   s   &$$r   hermite_normal_formr     s\    v 88>>><==}jALL,<,A,A,Cqwwqz,Q,Q22#A&&r   )__doc__collectionsr   domainmatrixr   
exceptionsr   r   sympy.ntheory.modularr   sympy.polys.domainsr   r   r   r   r*   r
   r5   r.   ru   r   r   r   rN   r   r   <module>r      sa    2 # & 3 3 &.:"D$8cL*J;ZU2p@' @' @'r   