
    oVhB                         d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZ d Zd	 Zd
 Zd Zd Zd Zd Zd Zd ZddddZy)z,Functions returning normal forms of matrices    )defaultdict   )DomainMatrix)DMDomainErrorDMShapeError)symmetric_residue)QQZZc                 r    t        |       }t        j                  || j                  | j                        }|S )aI  
    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      P/home/dcms/DCMS/lib/python3.12/site-packages/sympy/polys/matrices/normalforms.pysmith_normal_formr      s/    $ QD


D!((AGG
4CJ    c                    | j                   }| j                  }|j                  }| j                         } t	        |d         D ])  }t	        |d         D ]  }||k(  r	| |   |   |k(  r  y + t        |d   |d         }t	        d|      D ]O  }| |dz
     |dz
     |k(  r| |   |   |k7  s  y|j                  | |   |   | |dz
     |dz
           d   }||k7  sO y y)z8
    Checks that the matrix is in Smith Normal Form
    r   r   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AvQ47d?		 a%(#E1e_ QqS6!A#;$tAw$

1Q47AacF1Q3K03ADy r   c                     t        t        |             D ]8  }| |   |   }||z  || |   |   z  z   | |   |<   ||z  || |   |   z  z   | |   |<   : y Nr   len	r   r   r   abcdkes	            r   add_columnsr,   E   sj     3q6] "aDGA#!A$q'	/!QA#!A$q'	/!Q"r   c                 p    | j                   }| j                  }| j                         } t        | ||d      S )a3  
    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   s2     XXFGGE			A6UCCr   c                    | j                   }| j                  x\  }}}| j                         } t        | ||d      \  }}}t	        j
                  |||      j                         }t	        ||||f      }t	        ||||f      }|||fS )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   r0   r   r   to_dense)	r   r   rowscolsr   r   str   s	            r   smith_normal_decompr7   `   s      XXF JD$			A%au4HJD!Q


D&%
0
9
9
;CQvdD\:AQvdD\:A19r   c           
      	    !"#$% j                   sd }t        |      |\  " j                  %j                  !!%fd}d|v rrd |"       |       fS yr |"      # |       $d  "#%fd}  $%fd}t	        "      D cg c]  } |   d   %k7  s| }	}|	r9|	d   %k7  r1 |	d       d   c d<    |	d   <   r#|	d      #d   c#d<   #|	d   <   nlt	               D 
cg c]  }
 d   |
   %k7  s|
 }	}
|	rF|	d   %k7  r> D ]  }||	d      |d   c|d<   ||	d   <    r$D ]  }||	d      |d   c|d<   ||	d   <    t         %fdt	        d	       D              st         %fd
t	        d	"      D              rN |         |        t         %fdt	        d	       D              r.t         %fd
t	        d	"      D              rNfd} d   d   dk7  rhj                   d   d         }j                  rd	 d   d   z  }|j                  k7  r+ d   dxx   |z  cc<   r#d   D cg c]  }||z  	 c}#d<   d	|v rd}nŉ d	d D cg c]  }|d	d 	 }}t        |"d	z
   d	z
  f      }r|\  }}}d	gdg"d	z
  z  z   g|D cg c]  }dg|z   
 c}z   }d	gdg d	z
  z  z   g|D cg c]  }dg|z   
 c}z   }t        t        |#|$|g            \  #}$}|#z  #$|z  $#j                         #$j                         $n|} d   d   r$ d   d   g}|j                  |       t	        t        |      d	z
        D ]  }||   ||d	z      }}|r݉j                  ||      d	   %k7  rŉrj                  ||      \  }}}nj!                  ||      }j                  ||      d   }rrj                  ||      d   } #||d	z   d	dd	       t#        $||d	z   d	dd	        #||d	z   d	| dd	       t#        $||d	z   d	d| d	        #||d	z   dd	dd       ||z  ||d	z   <   |||<    n@ n>r0"d	kD  r#d	d #d   gz   # d	kD  r$D cg c]  }|d	d |d   gz    c}$| d   d   fz   }rt%        |      #$fS t%        |      S c c}w c c}
w c c}w c c}w c c}w c c}w c c}w )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           	          t        |       D cg c]"  }t        |       D cg c]  }||k(  rn c}$ c}}S c c}w c c}}w r"   )r   )nr   r   oner   s      r   eyez!_smith_normal_decomp.<locals>.eye   s8    EJ1XN%(;QQD(;NN;Ns   A ;A A r    c                     t        t        | d               D ]8  }| |   |   }||z  || |   |   z  z   | |   |<   ||z  || |   |   z  z   | |   |<   : y )Nr   r#   r%   s	            r   add_rowsz&_smith_normal_decomp.<locals>.add_rows   so     s1Q4y! 	&A!QAcAad1gIoAaDGcAad1gIoAaDG	&r   c            
         d   d   } t        d      D ]  }|   d   k(  r
j                  |   d   |       \  }}|k(  r" 	d|dd| d       sA 	d|dd| d       Q
j                  | |   d         \  }}}
j                  |   d   |      }
j                  | |      } 	d|||||        r 	d|||||        |}  y Nr   r   )r   r   gcdexexquo)pivotr   r)   r   r&   r'   gd_0d_jr?   r   r/   r   r3   r5   r   s            r   clear_columnz*_smith_normal_decomp.<locals>.clear_column   s   !Qq$ 	AtAw$::ad1gu-DAqDyAq!QA.Q1aQB2 ,,uad1g61all1Q47A.ll5!,Aq!QcT2Q1aC#6	r   c            
         d   d   } t        d	      D ]  }d   |   k(  r
j                  d   |   |       \  }}|k(  r(t        d|dd| d       sDt        d|dd| d       W
j                  | d   |         \  }}}
j	                  d   |   |      }
j	                  | |      }t        d|||||        rt        d|||||        |}  y rA   )r   r   r,   rB   rC   )rD   r   r)   r   r&   r'   rE   rF   rG   r4   r   r/   r   r6   r   s            r   	clear_rowz'_smith_normal_decomp.<locals>.clear_row   s   !Qq$ 	AtAw$::ad1gu-DAqDyAq!QA2q11aAr15 ,,uad1g61all1Q47A.ll5!,Aq!Q351aAsSD9	r   c              3   4   K   | ]  }d    |   k7    ywr   Nr=   .0r   r   r   s     r   	<genexpr>z'_smith_normal_decomp.<locals>.<genexpr>        61qtAw$6   r   c              3   4   K   | ]  }|   d    k7    ywrL   r=   rM   s     r   rO   z'_smith_normal_decomp.<locals>.<genexpr>   rP   rQ   c                 N    t        | t        |       t        | d         f      S )Nr   )r   r   )r   r$   )r   r   s    r   to_domain_matrixz._smith_normal_decomp.<locals>.to_domain_matrix   s#    Ac!fc!A$i%8HHr   Nr.   )is_PID
ValueErrorr   r;   r   anycanonical_unitis_Fieldr0   listmapr   extendr$   r   rB   gcdr,   tuple)&r   r   r   r/   msgr<   rH   rJ   r   indr   rowrT   r(   elemr   r   lower_rightrets_smallt_smalls2t2resultr&   r'   xyr)   alphabetar?   r4   r;   r3   r5   r6   r   s&   `` `                           @@@@@@@r   r0   r0   |   s    ==RSYRZ[oJD$;;D
**CO 	Ezs4y#d)++II& ( * Dk
5QqT!W_1
5C
5
s1v~CF)QqT!aAiAi1OAaD!CF)+9Q1aDq993q6T> :&)#a&k3q6#ACF: >C*-c!f+s1v'CFCAK> 6a666a66 6a666a66I 	tAw!|!!!A$q'*??AaDGA

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

1a(A

1a(+!::a+A.DQ1q5!Q151a!eQ1a8Q1q5!eVQ:1a!eQD5!<Q1q5!QA6%iqsq	)	, axabEQqTFNax345CSWAx'51a
"V}a""V}m 6 :0 3
 -
 )G(FN 6s6   R-*R-8R2	R2	R7)R<'S
S0Sc                 p    t        j                  | |      \  }}}| dk7  r|| z  dk(  rd}| dk  rdnd}|||fS )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.

    r   rU   r   )r
   rB   )r&   r'   rk   rl   rE   s        r   _gcdexrp   "  sG     hhq!nGAq!Av!a%1*a%BQa7Nr   c                    | j                   j                  st        d      | j                  \  }}| j	                         j                         } |}t        |dz
  dd      D ]  }|dk(  r n|dz  }t        |dz
  dd      D ]R  }| |   |   dk7  st        | |   |   | |   |         \  }}}| |   |   |z  | |   |   |z  }
}	t        | |||||
 |	       T | |   |   }|dk  rt        | ||dddd       | }|dk(  r|dz  }t        |dz   |      D ]  }| |   |   |z  }t        | ||d| dd       !  t        j                  | j                               dd|df   S )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.r   rU   r   N)r   is_ZZr   r   to_ddmcopyr   rp   r,   r   from_repto_dfm_or_ddm)Ar   r:   r*   r   r   uvr)   r   r5   r'   qs                r   _hermite_normal_formr|   7  s   8 88>><== 77DAq	
A 	
A1q5"b! !26 	Q q1ub"% 	2AtAw!| !1a!A$q'21atAw!|QqT!W\1Aq!QA2q1	2 aDGq51aQA.A 6FA
 1q5!_ 2aDGqLAq!QAq12?!2H   !23AqrE::r   c                    | j                   j                  st        d      t        j                  |      r|dk  rt        d      d }t        t              }| j                  \  }}||k  rt        d      | j                         } |}|}t        |dz
  dd      D ]  }|dz  }t        |dz
  dd      D ]P  }	| |   |	   dk7  st        | |   |   | |   |	         \  }
}}| |   |   |z  | |   |	   |z  }} || |||	|
|| |       R | |   |   }|dk(  r
|x| |   |<   }t        ||      \  }
}}t        |      D ]  }|
| |   |   z  |z  ||   |<    ||   |   dk(  r|||   |<   t        |dz   |      D ]%  }	||   |	   ||   |   z  }t        ||	|d| dd       ' ||z  } t        |||ft              j                         S )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.)

    rr   r   z0Modulus D must be positive element of domain ZZ.c                     t        t        |             D ]R  }| |   |   }	t        ||	z  || |   |   z  z   |z  |      | |   |<   t        ||	z  || |   |   z  z   |z  |      | |   |<   T y r"   )r   r$   r   )
r   Rr   r   r&   r'   r(   r)   r*   r+   s
             r   add_columns_mod_Rz8_hermite_normal_form_modulo_D.<locals>.add_columns_mod_R  s     s1v 	FA!QA'QQqT!W)<(A1EAaDG'QQqT!W)<(A1EAaDG	Fr   z2Matrix must have at least as many columns as rows.rU   r   )r   rs   r   r
   of_typer   dictr   r   r   r   rp   r,   r   r2   )rx   Dr   Wr   r:   r*   r   r   r   ry   rz   r)   r   r5   r'   iir{   s                     r   _hermite_normal_form_modulo_Dr     s!   Z 88>><==::a=AENOOF 	DA77DAq1uOPP			A	A	A1q5"b! 	Qq1ub"% 	;AtAw!| 1a!A$q'21atAw!|QqT!W\1!!Q1aQB:		;
 aDG6OAaDGaA,1a( 	&B2qzA~AbE!H	&Q47a<AaDGq1ua 	.A!Q1Q47"A1aQB1-	. 	
a%& Aq62&//11r   NF)r   
check_rankc                    | j                   j                  st        d      |A|r3| j                  t              j                         | j                  d   k(  rt        | |      S t        |       S )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.)

    rr   r   )	r   rs   r   
convert_tor	   rankr   r   r|   )rx   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   r7   r0   rp   r|   r   r   r=   r   r   <module>r      sX    2 # & 3 3 &.:"D$8cL*J;ZU2p !% @'r   