o
    hB                     @   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   V/var/www/html/ai/venv/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 < qt
fddtdD st
fddtdD r|  |  t
fddtdD st
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 }rt|	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	pivotr   r%   r   r"   r#   gd_0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   rD   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 rF   r   )r3   r   rG   r   r   r5      rH   c                 3   s     | ]} d  | kV  qdS r   Nr   r2   rG   r   r   	<genexpr>       z'_smith_normal_decomp.<locals>.<genexpr>r   c                 3   s     | ]} | d  kV  qdS rI   r   r2   rG   r   r   rJ      rK   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 rF   r   r3   rowr   r   r   r5          c                 S   rN   rF   r   rO   r   r   r   r5      rQ   c                 S   s"   g | ]}|d d |d g qS )r   Nr   r   rO   r   r   r   r5     s   " )is_PID
ValueErrorr   r4   r   anycanonical_unitis_Fieldr+   listmapr   extendr    r   r<   gcdr(   tuple)r   r   r   r*   msgr9   rC   rE   indrP   rL   r   lower_rightrets_small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   rR   r   )r	   r<   )r"   r#   rf   rg   r@   r   r   r   _gcdex"  s
   
rj   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   rR   r   N)r   is_ZZr   r   to_ddmcopyr   rj   r(   r   from_repto_dfm_or_ddm)Ar   r7   r&   r   r   uvr%   r   r/   r#   qr   r   r   _hermite_normal_form7  s4   
 "
ru   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.)

    rk   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.rR   r   )r   rl   r   r	   of_typer   dictr   r   r   r   rj   r(   r   r,   )rq   Drw   Wr   r7   r&   rv   r   r   rr   rs   r%   r   r/   r#   iirt   r   r   r   _hermite_normal_form_modulo_D  sB   -
 "
r}   NF)rz   
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.)

    rk   Nr   )	r   rl   r   
convert_tor   rankr   r}   ru   )rq   rz   r~   r   r   r   hermite_normal_form  s
   ;$
r   )__doc__collectionsr   domainmatrixr   
exceptionsr   r   sympy.ntheory.modularr   sympy.polys.domainsr   r	   r   r   r(   r
   r1   r+   rj   ru   r}   r   r   r   r   r   <module>   s"    		 'MX