ó
¡¼™\c           @  sÈ   d  d l  m Z m Z d  d l m Z m Z d  d l m Z d  d l m	 Z	 m
 Z
 d  d l m Z d  d l m Z d  d l m Z m Z m Z d „  Z e e d	 „ Z d
 „  Z e d „ Z d „  Z d S(   iÿÿÿÿ(   t   print_functiont   division(   t   as_intt   reduce(   t   prod(   t   igcdext   igcd(   t   isprime(   t   ZZ(   t   gf_crtt   gf_crt1t   gf_crt2c         C  s   |  | d k r |  S|  | S(   sÔ   Return the residual mod m such that it is within half of the modulus.

    >>> from sympy.ntheory.modular import symmetric_residue
    >>> symmetric_residue(1, 6)
    1
    >>> symmetric_residue(4, 6)
    -2
    i   (    (   t   at   m(    (    s4   lib/python2.7/site-packages/sympy/ntheory/modular.pyt   symmetric_residue   s    	c      	     sè   | r3 t  t t |  ƒ ƒ }  t  t t | ƒ ƒ } n  t | |  t ƒ ‰  t |  ƒ } | rÅ t ‡  f d †  t | |  ƒ Dƒ ƒ sÅ t d t	 d | t  t | |  ƒ ƒ Œ ‰  ˆ  d k r³ ˆ  Sˆ  \ ‰  } qÅ n  | rÞ t ˆ  | ƒ | f Sˆ  | f S(   s}  Chinese Remainder Theorem.

    The moduli in m are assumed to be pairwise coprime.  The output
    is then an integer f, such that f = v_i mod m_i for each pair out
    of v and m. If ``symmetric`` is False a positive integer will be
    returned, else \|f\| will be less than or equal to the LCM of the
    moduli, and thus f may be negative.

    If the moduli are not co-prime the correct result will be returned
    if/when the test of the result is found to be incorrect. This result
    will be None if there is no solution.

    The keyword ``check`` can be set to False if it is known that the moduli
    are coprime.

    Examples
    ========

    As an example consider a set of residues ``U = [49, 76, 65]``
    and a set of moduli ``M = [99, 97, 95]``. Then we have::

       >>> from sympy.ntheory.modular import crt, solve_congruence

       >>> crt([99, 97, 95], [49, 76, 65])
       (639985, 912285)

    This is the correct result because::

       >>> [639985 % m for m in [99, 97, 95]]
       [49, 76, 65]

    If the moduli are not co-prime, you may receive an incorrect result
    if you use ``check=False``:

       >>> crt([12, 6, 17], [3, 4, 2], check=False)
       (954, 1224)
       >>> [954 % m for m in [12, 6, 17]]
       [6, 0, 2]
       >>> crt([12, 6, 17], [3, 4, 2]) is None
       True
       >>> crt([3, 6], [2, 5])
       (5, 6)

    Note: the order of gf_crt's arguments is reversed relative to crt,
    and that solve_congruence takes residue, modulus pairs.

    Programmer's note: rather than checking that all pairs of moduli share
    no GCD (an O(n**2) test) and rather than factoring all moduli and seeing
    that there is no factor in common, a check that the result gives the
    indicated residuals is performed -- an O(n) operation.

    See Also
    ========

    solve_congruence
    sympy.polys.galoistools.gf_crt : low level crt routine used by this routine
    c         3  s)   |  ] \ } } | | ˆ  | k Vq d  S(   N(    (   t   .0t   vR   (   t   result(    s4   lib/python2.7/site-packages/sympy/ntheory/modular.pys	   <genexpr>[   s    t   checkt	   symmetricN(   t   listt   mapR   R	   R   R   t   allt   zipt   solve_congruencet   Falset   NoneR   (   R   R   R   R   t   mm(    (   R   s4   lib/python2.7/site-packages/sympy/ntheory/modular.pyt   crt   s    :%!c         C  s   t  |  t ƒ S(   sÓ   First part of Chinese Remainder Theorem, for multiple application.

    Examples
    ========

    >>> from sympy.ntheory.modular import crt1
    >>> crt1([18, 42, 6])
    (4536, [252, 108, 756], [0, 2, 0])
    (   R
   R   (   R   (    (    s4   lib/python2.7/site-packages/sympy/ntheory/modular.pyt   crt1g   s    c         C  s>   t  | |  | | | t ƒ } | r4 t | | ƒ | f S| | f S(   sû   Second part of Chinese Remainder Theorem, for multiple application.

    Examples
    ========

    >>> from sympy.ntheory.modular import crt1, crt2
    >>> mm, e, s = crt1([18, 42, 6])
    >>> crt2([18, 42, 6], [0, 0, 0], mm, e, s)
    (0, 4536)
    (   R   R   R   (   R   R   R   t   et   sR   R   (    (    s4   lib/python2.7/site-packages/sympy/ntheory/modular.pyt   crt2u   s    c          O  s¦  d „  } |  } | j  d t ƒ } | j  d t ƒ r3g  | D]$ \ } } t | ƒ t | ƒ f ^ q: } i  } xN | D]F \ } } | | ;} | | k r­ | | | k rq d Sqq n  | | | <qq Wg  | j ƒ  D] \ } } | | f ^ qÈ } ~ t d „  | Dƒ ƒ r3t t | Œ  ƒ \ } } t	 | | d | d t ƒSn  d } xf | D]; }	 | | |	 ƒ } | d k rePn  | \ }
 } |
 | }
 q@W| r˜t
 |
 | ƒ | f S|
 | f Sd S(	   s  Compute the integer ``n`` that has the residual ``ai`` when it is
    divided by ``mi`` where the ``ai`` and ``mi`` are given as pairs to
    this function: ((a1, m1), (a2, m2), ...). If there is no solution,
    return None. Otherwise return ``n`` and its modulus.

    The ``mi`` values need not be co-prime. If it is known that the moduli are
    not co-prime then the hint ``check`` can be set to False (default=True) and
    the check for a quicker solution via crt() (valid when the moduli are
    co-prime) will be skipped.

    If the hint ``symmetric`` is True (default is False), the value of ``n``
    will be within 1/2 of the modulus, possibly negative.

    Examples
    ========

    >>> from sympy.ntheory.modular import solve_congruence

    What number is 2 mod 3, 3 mod 5 and 2 mod 7?

    >>> solve_congruence((2, 3), (3, 5), (2, 7))
    (23, 105)
    >>> [23 % m for m in [3, 5, 7]]
    [2, 3, 2]

    If you prefer to work with all remainder in one list and
    all moduli in another, send the arguments like this:

    >>> solve_congruence(*zip((2, 3, 2), (3, 5, 7)))
    (23, 105)

    The moduli need not be co-prime; in this case there may or
    may not be a solution:

    >>> solve_congruence((2, 3), (4, 6)) is None
    True

    >>> solve_congruence((2, 3), (5, 6))
    (5, 6)

    The symmetric flag will make the result be within 1/2 of the modulus:

    >>> solve_congruence((2, 3), (5, 6), symmetric=True)
    (-1, 6)

    See Also
    ========

    crt : high level routine implementing the Chinese Remainder Theorem

    c         S  sÛ   |  \ } } | \ } } | | | | } } } t  t | | | g ƒ }	 g  | | | g D] }
 |
 |	 ^ qX \ } } } | d k r¸ t | | ƒ \ } } }	 |	 d k r« d S| | 9} n  | | | | | } } | | f S(   sô   Return the tuple (a, m) which satisfies the requirement
        that n = a + i*m satisfy n = a1 + j*m1 and n = a2 = k*m2.

        References
        ==========

        - https://en.wikipedia.org/wiki/Method_of_successive_substitution
        i   N(   R   R   R   R   (   t   c1t   c2t   a1t   m1t   a2t   m2R   t   bt   ct   gt   it   inv_at   _R   (    (    s4   lib/python2.7/site-packages/sympy/ntheory/modular.pyt   combine¼   s    	/R   R   c         s  s!   |  ] \ } } t  | ƒ Vq d  S(   N(   R   (   R   t   rR   (    (    s4   lib/python2.7/site-packages/sympy/ntheory/modular.pys	   <genexpr>ñ   s    i    i   N(   i    i   (   t   getR   t   TrueR   R   t   itemsR   R   R   R   R   (   t   remainder_modulus_pairst   hintR-   t   rmR   R.   R   t   uniqt   rvt   rmit   n(    (    s4   lib/python2.7/site-packages/sympy/ntheory/modular.pyR   ˆ   s8    4	1
+N(   t
   __future__R    R   t   sympy.core.compatibilityR   R   t   sympy.core.mulR   t   sympy.core.numbersR   R   t   sympy.ntheory.primetestR   t   sympy.polys.domainsR   t   sympy.polys.galoistoolsR	   R
   R   R   R   R0   R   R   R    R   (    (    (    s4   lib/python2.7/site-packages/sympy/ntheory/modular.pyt   <module>   s   	N	