U
    ZÊ¹j\3  ã                   @   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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 )aJ  
    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   ZdiagÚdomainÚshape)ÚmÚinvsZsmf© r   ú_/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/polys/matrices/normalforms.pyÚsmith_normal_form   s    r   c           	      C   sb   t t| ƒƒD ]P}| | | }|| || | |   | | |< || || | |   | | |< qd S ©N)ÚrangeÚlen©	r   ÚiÚjÚaÚbÚcÚdÚkÚer   r   r   Úadd_columns(   s     r   c                    sx  ˆj ‰ˆjsd}t|ƒ‚dˆjkr&dS ˆj \‰‰}tˆ ¡ j ¡ ƒ‰‡fdd„‰ ‡ ‡‡fdd„}‡‡fdd	„}‡fd
d„tˆƒD ƒ}|rº|d dkrºˆ|d  ˆd  ˆd< ˆ|d < nV‡fdd„tˆƒD ƒ}|�r|d dk�rˆD ]&}||d  |d  |d< ||d < qèt	‡fdd„tdˆƒD ƒƒ�sLt	‡fdd„tdˆƒD ƒƒ�r`|ˆƒ‰|ˆƒ‰�qd|k�rpd}n2t
dd„ ˆdd… D ƒˆd ˆd fˆƒ}t|ƒ}ˆd d �r^ˆd d g}	|	 |¡ tt|	ƒd ƒD ]‚}
|	|
 �rRˆ |	|
d  |	|
 ¡d dk�rRˆ |	|
d  |	|
 ¡}ˆ |	|
 |¡d |	|
d   |	|
d < ||	|
< n �qp�qØn|ˆd d f }	t|	ƒ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

    z8The matrix entries must be over a principal ideal domainr   r   c           	         s^   t ˆ ƒD ]P}| | | }|| || | |   | | |< || || | |   | | |< qd S r   )r   r   )Úcolsr   r   Úadd_rowsH   s     z#invariant_factors.<locals>.add_rowsc           
   	      sÚ   | d d dkr| S | d d }t dˆƒD ]ª}| | d dkr@q*ˆ | | d |¡\}}|dkrxˆ | d|dd| dƒ q*ˆ || | d ¡\}}}ˆ | | d |¡d }ˆ ||¡d }	ˆ | d|||||	 ƒ |}q*| S ©Nr   r   )r   ÚdivÚgcdex©
r   Zpivotr   r   Úrr   r   ÚgZd_0Zd_j)r    r   Úrowsr   r   Úclear_columnP   s    z'invariant_factors.<locals>.clear_columnc           
   	      sÚ   | d d dkr| S | d d }t dˆ ƒD ]ª}| d | dkr@q*ˆ | d | |¡\}}|dkrxt| d|dd| dƒ q*ˆ || d | ¡\}}}ˆ | d | |¡d }ˆ ||¡d }	t| d|||||	 ƒ |}q*| S r!   )r   r"   r   r#   r$   )r   r   r   r   Ú	clear_rowc   s    z$invariant_factors.<locals>.clear_rowc                    s    g | ]}ˆ | d  d kr|‘qS ©r   r   ©Ú.0r   ©r   r   r   Ú
<listcomp>w   s      z%invariant_factors.<locals>.<listcomp>c                    s    g | ]}ˆ d  | d kr|‘qS r*   r   )r,   r   r-   r   r   r.   {   s      c                 3   s   | ]}ˆ d  | d kV  qdS ©r   Nr   r+   r-   r   r   Ú	<genexpr>�   s     z$invariant_factors.<locals>.<genexpr>r   c                 3   s   | ]}ˆ | d  d kV  qdS r/   r   r+   r-   r   r   r0   ‚   s     c                 S   s   g | ]}|d d… ‘qS )r   Nr   )r,   r%   r   r   r   r.   ‰   s     N)r   Zis_PIDÚ
ValueErrorr   ÚlistÚto_denseÚrepÚto_ddmr   Úanyr   r
   Úextendr   r"   ÚgcdÚtuple)r   Úmsgr   r(   r)   ÚindÚrowr   Zlower_rightÚresultr   r&   r   )r    r   r   r   r'   r   r
   1   sL    
$$ÿ
*
,(
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   éÿÿÿÿr   )r	   r#   )r   r   ÚxÚyr&   r   r   r   Ú_gcdexœ   s
    rA   c              
   C   sz  | j jstdƒ‚| j\}}|  ¡ j ¡  ¡ } |}t|d ddƒD �]}|dkrT �q\|d8 }t|d ddƒD ]l}| | | dkrlt	| | | | | | ƒ\}}}| | | | | | | |  }	}
t
| |||||
 |	ƒ ql| | | }|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   r>   r   N)r   Úis_ZZr   r   r3   r4   r5   Úcopyr   rA   r   r   Zfrom_repZto_dfm_or_ddm)ÚAr   Únr   r   r   ÚuÚvr   r%   Úsr   Úqr   r   r   Ú_hermite_normal_form±   s0    
 "


rK   c                 C   sî  | j jstdƒ‚t |¡r"|dk r*tdƒ‚dd„ }ttƒ}| j\}}||k rTtdƒ‚|  	¡ j
 ¡  ¡ } |}|}t|d ddƒD �]X}|d8 }t|d ddƒD ]n}	| | |	 dkrœt| | | | | |	 ƒ\}
}}| | | | | | |	 |  }}|| |||	|
|| |ƒ qœ| | | }|dk�r2| | | |< }t||ƒ\}
}}t|ƒD ]"}|
| | |  | || |< �qJ|| | dk�rŒ||| |< t|d |ƒD ]4}	|| |	 || |  }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 ]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_R0  s    *z8_hermite_normal_form_modulo_D.<locals>.add_columns_mod_Rz2Matrix must have at least as many columns as rows.r>   r   )r   rC   r   r	   Zof_typer   Údictr   r   r3   r4   r5   rD   r   rA   r   r   )rE   ÚDrM   ÚWr   rF   r   rL   r   r   rG   rH   r   r%   rI   r   ÚiirJ   r   r   r   Ú_hermite_normal_form_modulo_Dþ   s@    -
 "
 
rR   NF)rO   Ú
check_rankc                C   sJ   | j jstdƒ‚|dk	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.)

    rB   Nr   )	r   rC   r   Z
convert_tor   Zrankr   rR   rK   )rE   rO   rS   r   r   r   Úhermite_normal_formV  s
    ;$
rT   )Ú__doc__Úcollectionsr   Zdomainmatrixr   Ú
exceptionsr   r   Zsympy.ntheory.modularr   Zsympy.polys.domainsr   r	   r   r   r
   rA   rK   rR   rT   r   r   r   r   Ú<module>   s   		kMX