o
    GZŽhâB  ã                   @   s¤   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d	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdddœdd„ZdS )z,Functions returning normal forms of matricesé    )Údefaultdicté   )ÚDomainMatrix)ÚDMDomainErrorÚDMShapeError)Úsymmetric_residue)ÚQQÚZZc                 C   s   t | ƒ}t || 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Úsmf© r   úO/var/www/auris/lib/python3.10/site-packages/sympy/polys/matrices/normalforms.pyÚsmith_normal_form   s   r   c                 C   sì   | j }| j}|j}|  ¡ } t|d ƒD ]}t|d ƒD ]}||kr"q| | | |ks.  dS qqt|d |d ƒ}td|ƒD ]5}| |d  |d  |krX| | | |krW dS q>| | | | | |d  |d  ¡d }||krs dS q>dS )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Úrr   r   r   Úis_smith_normal_form(   s.   ÿýÿ(ÿr   c           	      C   sb   t t| ƒƒD ](}| | | }|| || | |   | | |< || || | |   | | |< qd S ©N©r   Úlen©	r   r   r   ÚaÚbÚcÚdÚkÚer   r   r   Úadd_columnsE   s
    "ýr(   c                 C   s$   | j }| j}|  ¡ } t| ||d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   r   r   r   r
   N   s   r
   c           	      C   sr   | j }| j \}}}|  ¡ } t| ||dd\}}}t |||¡ ¡ }t||||fd}t||||fd}|||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   r+   r   r   Úto_dense)	r   r   ÚrowsÚcolsr   r   ÚsÚtr   r   r   r   Úsmith_normal_decomp`   s   
r1   c              	      sH  ˆj sdˆ› }t|ƒ‚|\‰‰ˆj‰
ˆj‰‡‡
fdd„}d|v r.ˆr,d|ˆƒ|ˆƒfS dS ˆr8|ˆƒ‰|ˆƒ‰	dd„ ‰ ‡ ‡‡‡‡‡‡
fdd	„}‡‡‡‡‡	‡
fd
d„}‡‡
fdd„tˆƒD ƒ}|rŒ|d ˆ
krŒˆ|d  ˆd ˆd< ˆ|d < ˆr‹ˆ|d  ˆd ˆd< ˆ|d < nB‡‡
fdd„tˆƒD ƒ}|rÎ|d ˆ
krÎˆD ]}	|	|d  |	d |	d< |	|d < q¢ˆrÎˆ	D ]}	|	|d  |	d |	d< |	|d < qºt‡‡
fdd„tdˆƒD ƒƒsít‡‡
fdd„tdˆƒD ƒƒr|ƒ  |ƒ  t‡‡
fdd„tdˆƒD ƒƒsít‡‡
fdd„tdˆƒD ƒƒsí‡fdd„}
ˆd d dkrUˆ ˆd d ¡‰ˆjr5dˆd d  ‰ˆˆjkrUˆd d  ˆ9  < ˆrU‡fdd„ˆd D ƒˆd< d|v r]d}ngdd„ ˆdd… D ƒ}t|ˆˆd ˆd fˆd}ˆrÂ|\}}}dgdgˆd   gdd„ |D ƒ }dgdgˆd   gdd„ |D ƒ }t	t
|
ˆ|ˆ	|gƒƒ\‰}‰	}|ˆ ‰ˆ	| ‰	ˆ ¡ ‰ˆ	 ¡ ‰	n|}ˆd d rnˆd d g}| |¡ tt|ƒd ƒD ]}|| ||d  }}|rlˆ ||¡d ˆ
krlˆrˆ ||¡\}}}nˆ ||¡}ˆ ||¡d }ˆr^ˆ ||¡d }ˆ ˆ||d dd|dƒ tˆ	||d d|ddƒ ˆ ˆ||d d| ddƒ tˆ	||d dd| dƒ ˆ ˆ||d ddddƒ || ||d < |||< qß n(ˆrˆdkrˆdd… ˆd g ‰ˆdkrdd„ ˆ	D ƒ‰	|ˆd d f }ˆr t|ƒˆˆ	fS t|ƒS )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                    s   ‡ ‡‡fdd„t ˆ ƒD ƒS )Nc                    s&   g | ]‰ ‡ ‡‡fd d„t ˆƒD ƒ‘qS )c                    s   g | ]
}|ˆ kr
ˆnˆ‘qS r   r   ©Ú.0r   )r   Úoner   r   r   Ú
<listcomp>Œ   s    z@_smith_normal_decomp.<locals>.eye.<locals>.<listcomp>.<listcomp>©r   )r3   )Únr4   r   )r   r   r5   Œ   s   & z5_smith_normal_decomp.<locals>.eye.<locals>.<listcomp>r6   ©r7   )r4   r   r8   r   Úeye‹   s   z!_smith_normal_decomp.<locals>.eyer   r   c           	      S   sf   t t| d ƒƒD ](}| | | }|| || | |   | | |< || || | |   | | |< qd S )Nr   r   r!   r   r   r   Úadd_rows˜   s
    "ýz&_smith_normal_decomp.<locals>.add_rowsc            	   	      sò   ˆd d } t dˆƒD ]k}ˆ| d ˆkrqˆ ˆ| d | ¡\}}|ˆkr?ˆ ˆd|dd| dƒ ˆr>ˆ ˆd|dd| dƒ qˆ | ˆ| d ¡\}}}ˆ ˆ| d |¡}ˆ | |¡}ˆ ˆd||||| ƒ ˆrtˆ ˆd||||| ƒ |} qd S ©Nr   r   )r   r   ÚgcdexÚexquo©	Zpivotr   r%   r   r"   r#   ÚgZd_0Zd_j)r:   r   r*   r   r-   r/   r   r   r   Úclear_column    ó$   €ñz*_smith_normal_decomp.<locals>.clear_columnc            	   	      sò   ˆd d } t dˆ ƒD ]k}ˆd | ˆkrqˆ ˆd | | ¡\}}|ˆkr?tˆd|dd| dƒ ˆr>tˆd|dd| dƒ qˆ | ˆd | ¡\}}}ˆ ˆd | |¡}ˆ | |¡}tˆd||||| ƒ ˆrttˆd||||| ƒ |} qd S r;   )r   r   r(   r<   r=   r>   )r.   r   r*   r   r0   r   r   r   Ú	clear_row´   rA   z'_smith_normal_decomp.<locals>.clear_rowc                    s    g | ]}ˆ | d  ˆkr|‘qS ©r   r   r2   ©r   r   r   r   r5   É   ó     z(_smith_normal_decomp.<locals>.<listcomp>c                    s    g | ]}ˆ d  | ˆkr|‘qS rC   r   )r3   r   rD   r   r   r5   Ï   rE   c                 3   s     | ]}ˆ d  | ˆkV  qdS ©r   Nr   r2   rD   r   r   Ú	<genexpr>Ø   ó   € z'_smith_normal_decomp.<locals>.<genexpr>r   c                 3   s     | ]}ˆ | d  ˆkV  qdS rF   r   r2   rD   r   r   rG   Ù   rH   c                    s   t | t| ƒt| d ƒfˆ dS )Nr   )r   r   )r   r    )r   )r   r   r   Úto_domain_matrixÝ   s   z._smith_normal_decomp.<locals>.to_domain_matrixc                    s   g | ]}|ˆ  ‘qS r   r   )r3   Úelem)r$   r   r   r5   ç   s    c                 S   s   g | ]}|d d… ‘qS )r   Nr   )r3   r   r   r   r   r5   ì   s    Nr)   c                 S   ó   g | ]}d g| ‘qS rC   r   ©r3   Úrowr   r   r   r5   ñ   ó    c                 S   rK   rC   r   rL   r   r   r   r5   ò   rN   éÿÿÿÿc                 S   s"   g | ]}|d d… |d g ‘qS )r   Nr   r   rL   r   r   r   r5     s   " )Zis_PIDÚ
ValueErrorr   r4   r   ÚanyZcanonical_unitZis_Fieldr+   ÚlistÚmapr   Úextendr    r   r<   Úgcdr(   Útuple)r   r   r   r*   Úmsgr9   r@   rB   ÚindrM   rI   r   Zlower_rightÚretZs_smallZt_smallÚs2Út2Úresultr   r"   r#   ÚxÚyr%   ÚalphaÚbetar   )r:   r$   r.   r   r*   r   r4   r-   r/   r0   r   r   r+   |   sº   
""€$$ÿýÿ
ÿ
$$

€

r+   c                 C   sD   t  | |¡\}}}| dkr||  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   rO   r   )r	   r<   )r"   r#   r]   r^   r?   r   r   r   Ú_gcdex"  s
   
ra   c              
   C   sj  | j jstdƒ‚| j\}}|  ¡  ¡ } |}t|d ddƒD ]ˆ}|dkr% n|d8 }t|d ddƒD ]6}| | | dkrgt| | | | | | ƒ\}}}| | | | | | | | }	}
t| |||||
 |	ƒ q1| | | }|dk rt| ||ddddƒ | }|dkrˆ|d7 }qt|d |ƒD ]}| | | | }t| ||d| ddƒ qqt	 
|  ¡ ¡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   rO   r   N)r   Úis_ZZr   r   Zto_ddmÚcopyr   ra   r(   r   Zfrom_repZto_dfm_or_ddm)ÚAr   r7   r&   r   r   ÚuÚvr%   r   r/   r#   Úqr   r   r   Ú_hermite_normal_form7  s4   
 "€
þri   c                 C   sÚ  | j jstdƒ‚t |¡r|dk rtdƒ‚dd„ }ttƒ}| j\}}||k r*tdƒ‚|  	¡ } |}|}t
|d ddƒD ]¨}|d8 }t
|d ddƒD ]7}	| | |	 dkrt| | | | | |	 ƒ\}
}}| | | | | | |	 | }}|| |||	|
|| |ƒ qH| | | }|dkr’| | | |< }t||ƒ\}
}}t
|ƒD ]}|
| | |  | || |< qž|| | dkr½||| |< t
|d |ƒD ]}	|| |	 || |  }t||	|d| ddƒ qÄ|| }q:t|||ftƒ ¡ 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.)

    rb   r   z0Modulus D must be positive element of domain ZZ.c           
      S   sv   t t| ƒƒD ]2}| | | }	t||	 || | |   | |ƒ| | |< t||	 || | |   | |ƒ| | |< qd S r   )r   r    r   )
r   ÚRr   r   r"   r#   r$   r%   r&   r'   r   r   r   Úadd_columns_mod_R¶  s
   *,ýz8_hermite_normal_form_modulo_D.<locals>.add_columns_mod_Rz2Matrix must have at least as many columns as rows.rO   r   )r   rc   r   r	   Zof_typer   Údictr   r   r   r   ra   r(   r   r,   )re   ÚDrk   ÚWr   r7   r&   rj   r   r   rf   rg   r%   r   r/   r#   Úiirh   r   r   r   Ú_hermite_normal_form_modulo_D„  sB   -
 "€
rp   NF)rm   Ú
check_rankc                C   sF   | j jstdƒ‚|dur|r|  t¡ ¡ | 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.)

    rb   Nr   )	r   rc   r   Z
convert_tor   Zrankr   rp   ri   )re   rm   rq   r   r   r   Úhermite_normal_formÜ  s
   ;$
rr   )Ú__doc__Úcollectionsr   Zdomainmatrixr   Ú
exceptionsr   r   Zsympy.ntheory.modularr   Zsympy.polys.domainsr   r	   r   r   r(   r
   r1   r+   ra   ri   rp   rr   r   r   r   r   Ú<module>   s"    		 'MX