a
    kº”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   úN/var/www/auris/lib/python3.9/site-packages/sympy/polys/matrices/normalforms.pyÚsmith_normal_form   s    r   c                 C   sì   | j }| j}|j}|  ¡ } t|d ƒD ]8}t|d ƒD ]&}||krDq6| | | |ks6  dS q6q&t|d |d ƒ}td|ƒD ]j}| |d  |d  |kr°| | | |kræ dS q|| | | | | |d  |d  ¡d }||kr| 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 ]P}| | | }|| || | |   | | |< || || | |   | | |< 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              	      s.  ˆj sdˆ› }t|ƒ‚|\‰‰ˆj‰
ˆj‰‡‡
fdd„}d|v r\ˆrXd|ˆƒ|ˆƒfS dS ˆrp|ˆƒ‰|ˆƒ‰	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 < nŽ‡‡
fdd„tˆƒD ƒ}|r¬|d ˆ
kr¬ˆD ](}	|	|d  |	d  |	d< |	|d < qNˆr¬ˆ	D ](}	|	|d  |	d  |	d< |	|d < q‚t‡‡
fdd„tdˆƒD ƒƒsìt‡‡
fdd„tdˆƒD ƒƒrü|ƒ  |ƒ  q¬‡fdd„}
ˆd d dkr„ˆ ˆd d ¡‰ˆjrDdˆd d  ‰ˆˆjkr„ˆd d  ˆ9  < ˆr„‡fdd„ˆd D ƒˆd< d|v r”d}nÎd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 r¾ˆd d g}| |¡ tt|ƒd ƒD ] }|| ||d   }}|r²ˆ ||¡d ˆ
kr²ˆ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 < |||< n qq˜nPˆrüˆdkräˆdd… ˆd g ‰ˆdkrüdd„ ˆ	D ƒ‰	|ˆd d f }ˆr"t|ƒˆˆ	fS t|ƒS d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>Œ   ó    z@_smith_normal_decomp.<locals>.eye.<locals>.<listcomp>.<listcomp>©r   )r3   )Únr4   r   )r   r   r5   Œ   r6   z5_smith_normal_decomp.<locals>.eye.<locals>.<listcomp>r7   ©r8   )r4   r   r9   r   Úeye‹   s    z!_smith_normal_decomp.<locals>.eyer   r   c           	      S   sf   t t| d ƒƒD ]P}| | | }|| || | |   | | |< || || | |   | | |< 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 ]Ö}ˆ| d ˆkr,qˆ ˆ| d | ¡\}}|ˆkr~ˆ ˆd|dd| dƒ ˆrìˆ ˆd|dd| dƒ qˆ | ˆ| d ¡\}}}ˆ ˆ| d |¡}ˆ | |¡}ˆ ˆd||||| ƒ ˆrèˆ ˆ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    s     z*_smith_normal_decomp.<locals>.clear_columnc            	   	      sò   ˆd d } t dˆ ƒD ]Ö}ˆd | ˆkr,qˆ ˆd | | ¡\}}|ˆkr~tˆd|dd| dƒ ˆrìtˆd|dd| dƒ qˆ | ˆd | ¡\}}}ˆ ˆd | |¡}ˆ | |¡}tˆd||||| ƒ ˆrètˆd||||| ƒ |} qd S r<   )r   r   r(   r=   r>   r?   )r.   r   r*   r   r0   r   r   r   Ú	clear_row´   s     z'_smith_normal_decomp.<locals>.clear_rowc                    s    g | ]}ˆ | d  ˆkr|‘qS ©r   r   r2   ©r   r   r   r   r5   É   r6   z(_smith_normal_decomp.<locals>.<listcomp>c                    s    g | ]}ˆ d  | ˆkr|‘qS rC   r   )r3   r   rD   r   r   r5   Ï   r6   c                 3   s   | ]}ˆ d  | ˆkV  qdS ©r   Nr   r2   rD   r   r   Ú	<genexpr>Ø   r6   z'_smith_normal_decomp.<locals>.<genexpr>r   c                 3   s   | ]}ˆ | d  ˆkV  qdS rE   r   r2   rD   r   r   rF   Ù   r6   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   ç   r6   c                 S   s   g | ]}|d d… ‘qS )r   Nr   )r3   r   r   r   r   r5   ì   r6   Nr)   c                 S   s   g | ]}d g| ‘qS rC   r   ©r3   Úrowr   r   r   r5   ñ   r6   c                 S   s   g | ]}d g| ‘qS rC   r   rI   r   r   r   r5   ò   r6   éÿÿÿÿc                 S   s"   g | ]}|d d… |d g ‘qS )r   Nr   r   rI   r   r   r   r5     r6   )Zis_PIDÚ
ValueErrorr   r4   r   ÚanyZcanonical_unitZis_Fieldr+   ÚlistÚmapr   Úextendr    r   r=   Úgcdr(   Útuple)r   r   r   r*   Úmsgr:   rA   rB   ÚindrJ   rG   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 r6d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   rK   r   )r	   r=   )r"   r#   rY   rZ   r@   r   r   r   Ú_gcdex"  s
    r]   c              
   C   st  | j jstdƒ‚| j\}}|  ¡  ¡ } |}t|d ddƒD ]}|dkrN qV|d8 }t|d ddƒD ]l}| | | dkrft| | | | | | ƒ\}}}| | | | | | | |  }	}
t| |||||
 |	ƒ qf| | | }|dk rt| ||ddddƒ | }|dkr|d7 }q:t|d |ƒD ],}| | | | }t| ||d| ddƒ q&q: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   rK   r   N)r   Úis_ZZr   r   Zto_ddmÚcopyr   r]   r(   r   Zfrom_repZto_dfm_or_ddm)ÚAr   r8   r&   r   r   ÚuÚvr%   r   r/   r#   Úqr   r   r   Ú_hermite_normal_form7  s0    
 "


re   c                 C   sä  | j jstdƒ‚t |¡r"|dk r*tdƒ‚dd„ }ttƒ}| j\}}||k rTtdƒ‚|  	¡ } |}|}t
|d ddƒD ]X}|d8 }t
|d ddƒD ]n}	| | |	 dkr’t| | | | | |	 ƒ\}
}}| | | | | | |	 |  }}|| |||	|
|| |ƒ q’| | | }|dkr(| | | |< }t||ƒ\}
}}t
|ƒD ]"}|
| | |  | || |< q@|| | dkr‚||| |< t
|d |ƒD ]4}	|| |	 || |  }t||	|d| ddƒ q|| }qtt|||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.)

    r^   r   z0Modulus D must be positive element of domain ZZ.c           
      S   sv   t t| ƒƒD ]d}| | | }	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.rK   r   )r   r_   r   r	   Zof_typer   Údictr   r   r   r   r]   r(   r   r,   )ra   ÚDrg   ÚWr   r8   r&   rf   r   r   rb   rc   r%   r   r/   r#   Úiird   r   r   r   Ú_hermite_normal_form_modulo_D„  s@    -
 "
 
rl   NF)ri   Ú
check_rankc                C   sJ   | j jstdƒ‚|dur>|r4|  t¡ ¡ | jd kr>t| |ƒS t| ƒS d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.)

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