U
    ;qLe13                     @   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   diagdomainshape)minvsZsmf r   w/home/p21-0144/sympy/latex2sympy2solve-back-end/sympyEq/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                    st  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 < qtfddtdD sHtfddtdD r\||qd|krld}n2t	dd dd D d d f}t
|}d d rZd d g}	|	| tt|	d D ]}
|	|
 rN|	|
d  |	|
 d dkrN|	|
d  |	|
 }|	|
 |d |	|
d   |	|
d < ||	|
< n qlqn|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 r0   r   r,   r.   r   r   r1      s     c                 S   s   g | ]}|d d qS )r   Nr   )r-   r&   r   r   r   r/      s     N)r   is_PID
ValueErrorr   listto_denserepr   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
    rB   c              
   C   sr  | j jstd| j\}}|  j } |}t|d ddD ]}|dkrP qX|d8 }t|d ddD ]l}| | | dkrht| | | | | | \}}}| | | | | | | |  }	}
t	| |||||
 |	 qh| | | }|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   r5   r6   copyr   rB   r   r   from_rep)Ar   nr   r   r   uvr   r&   sr   qr   r   r   _hermite_normal_form   s0    
 "


rM   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rt| | | | | |	 \}
}}| | | | | | |	 |  }}|| |||	|
|| | q| | | }|dkr.| | | |< }t||\}
}}t|D ]"}|
| | |  | || |< qF|| | dkr||| |< t|d |D ]4}	|| |	 || |  }t||	|d| dd q|| }qzt|||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.)

    rC   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   rD   r   r	   of_typer   dictr   r   r5   r6   rE   r   rB   r   r   )rG   DrO   Wr   rH   r   rN   r   r   rI   rJ   r   r&   rK   r   iirL   r   r   r   _hermite_normal_form_modulo_D   s@    -
 "
 
rU   NF)rR   
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.)

    rC   Nr   )	r   rD   r   
convert_tor   rankr   rU   rM   )rG   rR   rV   r   r   r   hermite_normal_formV  s
    ;$
rY   )__doc__collectionsr   domainmatrixr   
exceptionsr   r   sympy.ntheory.modularr   sympy.polys.domainsr   r	   r   r   r
   rB   rM   rU   rY   r   r   r   r   <module>   s   		kMX