ó
¡¼™\c           @  sH   d  Z  d d l m Z m Z d d l m Z d d „ Z d d „ Z d S(   s,   Functions returning normal forms of matricesiÿÿÿÿ(   t   divisiont   print_function(   t   diagc         C  s¢   t  |  d | ƒ} t | Œ  } t | ƒ } |  j | k rd | j |  j t |  j | |  j ƒ ƒ } n: |  j | k rž | j |  j t |  j |  j | ƒ ƒ } n  | S(   s  
    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.polys.solvers import RawMatrix as Matrix
    >>> from sympy.polys.domains import ZZ
    >>> from sympy.matrices.normalforms import smith_normal_form
    >>> m = Matrix([[12, 6, 4], [3, 9, 6], [2, 16, 14]])
    >>> setattr(m, "ring", ZZ)
    >>> print(smith_normal_form(m))
    Matrix([[1, 0, 0], [0, 10, 0], [0, 0, -30]])

    t   domain(   t   invariant_factorsR   t   lent   rowst
   row_insertt   zerost   colst
   col_insert(   t   mR   t   invst   smft   n(    (    s9   lib/python2.7/site-packages/sympy/matrices/normalforms.pyt   smith_normal_form   s    ++c   
        s"  ˆ s< t  |  d ƒ o |  j j s0 t d ƒ ‚ q< |  j ‰ n  t |  ƒ d k rR d S|  d d … d d … f }  d „  ‰ d „  ‰  ‡ ‡ f d †  } ‡  ‡ f d †  } g  t |  j ƒ D]" } |  | d f d k r´ | ^ q´ } | r|  j d | d g g ƒ }  n] g  t |  j ƒ D]" } |  d | f d k r| ^ q} | r^|  j	 d | d g g ƒ }  n  x’ t
 g  t d	 |  j ƒ D] } |  d | f d k ^ qwƒ s×t
 g  t d	 |  j ƒ D] } |  | d f d k ^ q²ƒ rò| |  ƒ }  | |  ƒ }  qaWd	 |  j k rd } n( t |  d	 d … d	 d … f d
 ˆ ƒ} |  d r|  d g } | j | ƒ x¾ t t | ƒ d	 ƒ D]’ } | | rÿˆ j | | d	 | | ƒ d	 d k rÿˆ j | | d	 | | ƒ }	 ˆ j | | |	 ƒ d | | d	 | | d	 <|	 | | <qnPqnWn | |  d f } t | ƒ S(   s  
    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] http://sierra.nmsu.edu/morandi/notes/SmithNormalForm.pdf

    t   rings8   The matrix entries must be over a principal ideal domaini    Nc   	      S  sz   xs t  |  j ƒ D]b } |  | | f } | | | |  | | f |  | | f <| | | |  | | f |  | | f <q Wd  S(   N(   t   rangeR	   (	   R   t   it   jt   at   bt   ct   dt   kt   e(    (    s9   lib/python2.7/site-packages/sympy/matrices/normalforms.pyt   add_rows9   s    &c   	      S  sz   xs t  |  j ƒ D]b } |  | | f } | | | |  | | f |  | | f <| | | |  | | f |  | | f <q Wd  S(   N(   R   R   (	   R   R   R   R   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/sympy/matrices/normalforms.pyt   add_columnsA   s    &c   
   	     s'  |  d d k r |  S|  d } xt  d |  j ƒ D]î } |  | d f d k rS q1 n  ˆ j |  | d f | ƒ \ } } | d k r¡ ˆ  |  d | d d | d ƒ q1 ˆ j | |  | d f ƒ \ } } } ˆ j |  | d f | ƒ d } ˆ j | | ƒ d }	 ˆ  |  d | | | | |	 ƒ | } q1 W|  S(   Ni    i   (   i    i    (   i    i    (   R   R   t   divt   gcdex(
   R   t   pivotR   R   t   rR   R   t   gt   d_0t   d_j(   R   R   (    s9   lib/python2.7/site-packages/sympy/matrices/normalforms.pyt   clear_columnI   s    
" % 
c   
   	     s'  |  d d k r |  S|  d } xt  d |  j ƒ D]î } |  d | f d k rS q1 n  ˆ j |  d | f | ƒ \ } } | d k r¡ ˆ  |  d | d d | d ƒ q1 ˆ j | |  d | f ƒ \ } } } ˆ j |  d | f | ƒ d } ˆ j | | ƒ d }	 ˆ  |  d | | | | |	 ƒ | } q1 W|  S(   Ni    i   (   i    i    (   R   R	   R   R   (
   R   R   R   R   R   R   R   R    R!   R"   (   R   R   (    s9   lib/python2.7/site-packages/sympy/matrices/normalforms.pyt	   clear_row\   s    
" % 
i   R   (    (    (   i    i    (   i    i    (   i    i    (   t   hasattrR   t   is_PIDt
   ValueErrorR   R   R   t   permute_rowsR	   t   permute_colst   anyt   shapeR   t   extendR   t   gcdt   tuple(
   R   R   R#   R$   R   t   indR   R   t   resultR    (    (   R   R   R   s9   lib/python2.7/site-packages/sympy/matrices/normalforms.pyR   !   sH    		88>;	(
2.N(	   t   __doc__t
   __future__R    R   t   sympy.matrices.denseR   t   NoneR   R   (    (    (    s9   lib/python2.7/site-packages/sympy/matrices/normalforms.pyt   <module>   s   