§
    OŠtj!  ã                   ó|   — 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 dlmZ d„ Zdd
„Zd„ Zdd„Zd„ ZdS )é    )Úprod©ÚgcdÚgcdext©Úisprime)ÚZZ)Úgf_crtÚgf_crt1Úgf_crt2©Úas_intc                 ó"   — | |dz  k    r| S | |z
  S )zÔ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
    é   © )ÚaÚms     úS/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/ntheory/modular.pyÚsymmetric_residuer   
   s   € ð 	ˆA�‰F‚{€{ØˆØˆq‰5€Ló    FTc                 ó*  ‡— |rDt          t          t          | ¦  «        ¦  «        } t          t          t          |¦  «        ¦  «        }t          || t          ¦  «        Št          | ¦  «        }|rZt          ˆfd„t          || ¦  «        D ¦   «         ¦  «        s1t          t          t          || ¦  «        ¦  «        d|dœŽŠ‰€‰S ‰\  Š}|r,t          t          ‰|¦  «        ¦  «        t          |¦  «        fS t          ‰¦  «        t          |¦  «        fS )ak  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

       >>> 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   ó6   •K  — | ]\  }}||z  ‰|z  k    V — Œd S ©Nr   )Ú.0Úvr   Úresults      €r   ú	<genexpr>zcrt.<locals>.<genexpr>Z   s4   øè è € Ð=Ð=©4¨1¨a�1�q‘5˜F Q™JÒ&Ð=Ð=Ð=Ð=Ð=Ð=r   F)ÚcheckÚ	symmetric)ÚlistÚmapr   r
   r	   r   ÚallÚzipÚsolve_congruenceÚintr   )r   r   r   r   Úmmr   s        @r   Úcrtr'      s  ø€ ðt ð !Ý••V˜Q‘”Ñ Ô ˆÝ••V˜Q‘”Ñ Ô ˆå�A�q�"ÑÔ€FÝ	ˆa‰Œ€Bàð  ÝÐ=Ð=Ð=Ð=µ3°q¸!±9´9Ð=Ñ=Ô=Ñ=Ô=ð 	 Ý%¥t­C°°1©I¬I¡¤Ø¨9ð6ð 6ð 6ˆFàˆ~Ø�Ø‰JˆF�Bàð ;ÝÕ$ V¨RÑ0Ô0Ñ1Ô1µ3°r±7´7Ð:Ð:Ýˆv‰;Œ;�˜B™œÐÐr   c                 ó,   — t          | t          ¦  «        S )aC  First part of Chinese Remainder Theorem, for multiple application.

    Examples
    ========

    >>> from sympy.ntheory.modular import crt, crt1, crt2
    >>> m = [99, 97, 95]
    >>> v = [49, 76, 65]

    The following two codes have the same result.

    >>> crt(m, v)
    (639985, 912285)

    >>> mm, e, s = crt1(m)
    >>> crt2(m, v, mm, e, s)
    (639985, 912285)

    However, it is faster when we want to fix ``m`` and
    compute for multiple ``v``, i.e. the following cases:

    >>> mm, e, s = crt1(m)
    >>> vs = [[52, 21, 37], [19, 46, 76]]
    >>> for v in vs:
    ...     print(crt2(m, v, mm, e, s))
    (397042, 912285)
    (803206, 912285)

    See Also
    ========

    sympy.polys.galoistools.gf_crt1 : low level crt routine used by this routine
    sympy.ntheory.modular.crt
    sympy.ntheory.modular.crt2

    )r   r	   )r   s    r   Úcrt1r)   f   s   € õL �1•b‰>Œ>Ðr   c                 óÌ   — t          || |||t          ¦  «        }|r,t          t          ||¦  «        ¦  «        t          |¦  «        fS t          |¦  «        t          |¦  «        fS )aÃ  Second part of Chinese Remainder Theorem, for multiple application.

    See ``crt1`` for usage.

    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)

    See Also
    ========

    sympy.polys.galoistools.gf_crt2 : low level crt routine used by this routine
    sympy.ntheory.modular.crt
    sympy.ntheory.modular.crt1

    )r   r	   r%   r   )r   r   r&   ÚeÚsr   r   s          r   Úcrt2r-   �   s^   € õ, �Q˜˜2˜q !¥RÑ(Ô(€Fàð ;ÝÕ$ V¨RÑ0Ô0Ñ1Ô1µ3°r±7´7Ð:Ð:Ýˆv‰;Œ;�˜B™œÐÐr   c                  ó  — d„ }| }|                      dd¦  «        }|                      dd¦  «        r˜d„ |D ¦   «         }i }|D ]#\  }}||z  }||v r|||         k    r dS Œ|||<   Œ$d„ |                     ¦   «         D ¦   «         }~t          d	„ |D ¦   «         ¦  «        r,t          t	          |Ž ¦  «        \  }}t          |||d¬
¦  «        S d}|D ]}	 |||	¦  «        }|€ dS |\  }
}|
|z  }
Œ|rt          |
|¦  «        |fS |
|fS )a  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                 óæ   ‡— | \  }}|\  }}|||z
  |}}}t          |||¦  «        Šˆfd„|||fD ¦   «         \  }}}|dk    r!t          ||¦  «        \  Š}	}
‰dk    rdS ||	z  }|||z  z   ||z  }}||fS )zù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
        ==========

        .. [1] https://en.wikipedia.org/wiki/Method_of_successive_substitution
        c                 ó   •— g | ]}|‰z  ‘ŒS r   r   )r   ÚiÚgs     €r   ú
<listcomp>z5solve_congruence.<locals>.combine.<locals>.<listcomp>í   s   ø€ Ð+Ð+Ð+˜A�1�a‘4Ð+Ð+Ð+r   é   Nr   )Úc1Úc2Úa1Úm1Úa2Úm2r   ÚbÚcÚinv_aÚ_r   r2   s               @r   Úcombinez!solve_congruence.<locals>.combineà   s­   ø€ ð ‰ˆˆBØ‰ˆˆBØ�b˜2‘g˜rˆaˆ1ˆÝ��1�a‰LŒLˆØ+Ð+Ð+Ð+ ! Q¨ Ð+Ñ+Ô+‰ˆˆ1ˆaØ�Š6ˆ6Ý   A™,œ,‰KˆAˆu�aØ�AŠvˆvØ�tØ�‰JˆAØ�B�q‘D‰y˜"˜Q™$ˆ1ˆØ�!ˆtˆr   r   Fr   Tc                 óP   — g | ]#\  }}t          |¦  «        t          |¦  «        f‘Œ$S r   r   ©r   Úrr   s      r   r3   z$solve_congruence.<locals>.<listcomp>ú   s-   € Ð4Ð4Ð4©¨¨A�v�a‰yŒy�& ™)œ)Ð$Ð4Ð4Ð4r   Nc                 ó   — g | ]	\  }}||f‘Œ
S r   r   )r   r   rB   s      r   r3   z$solve_congruence.<locals>.<listcomp>  s    € Ð.Ð.Ð.™˜˜Aˆq�!ˆfÐ.Ð.Ð.r   c              3   ó:   K  — | ]\  }}t          |¦  «        V — Œd S r   r   rA   s      r   r   z#solve_congruence.<locals>.<genexpr>  s,   è è € Ð)Ð)™d˜a �w�q‰zŒzÐ)Ð)Ð)Ð)Ð)Ð)r   )r   r   )r   r4   )ÚgetÚitemsr"   r    r#   r'   r   )Úremainder_modulus_pairsÚhintr?   Úrmr   ÚuniqrB   r   ÚrvÚrmiÚns              r   r$   r$   ¬   sw  € ðhð ð ð, 
!€BØ—’˜ eÑ,Ô,€Ià‡x‚x�˜ÑÔð ?Ø4Ð4°Ð4Ñ4Ô4ˆð ˆØð 	ð 	‰DˆAˆqØ�‰FˆAØ�DˆyˆyØ˜˜Qœ’<�<Ø˜4˜4ØØˆD�‰GˆGØ.Ð. §¢¡¤Ð.Ñ.Ô.ˆØõ
 Ð)Ð) bÐ)Ñ)Ô)Ñ)Ô)ð 	?Ý�˜R˜‘>”>‰DˆAˆqÝ�q˜! y¸Ð>Ñ>Ô>Ð>à	€BØð 	ð 	ˆØˆW�R˜ÑÔˆØˆ:ØˆEˆEØ‰ˆˆ1Ø�‰Eˆˆàð 	.Ý$ Q¨Ñ*Ô*¨AÐ-Ð-Ø�!ˆtˆr   N)FT)F)Úmathr   Úsympy.external.gmpyr   r   Úsympy.ntheory.primetestr   Úsympy.polys.domainsr	   Úsympy.polys.galoistoolsr
   r   r   Úsympy.utilities.miscr   r   r'   r)   r-   r$   r   r   r   ú<module>rT      sí   ðØ Ð Ð Ð Ð Ð à +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ø +Ð +Ð +Ð +Ð +Ð +Ø "Ð "Ð "Ð "Ð "Ð "Ø <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'ðð ð ðK ð K ð K ð K ð\&ð &ð &ðR ð  ð  ð  ð:wð wð wð wð wr   