
    \h!                     ~    S SK Jr  S SKJrJr  S SKJr  S SKJr  S SK	J
r
JrJr  S SKJr  S rSS jrS	 rSS
 jrS rg)    )prodgcdgcdextisprime)ZZ)gf_crtgf_crt1gf_crt2)as_intc                     XS-  ::  a  U $ X-
  $ )zReturn 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     M/var/www/auris/envauris/lib/python3.13/site-packages/sympy/ntheory/modular.pysymmetric_residuer   
   s     	F{5L    c                   ^ U(       a2  [        [        [        U 5      5      n [        [        [        U5      5      n[        X[        5      m[        U 5      nU(       aK  [        U4S j[        X5       5       5      (       d(  [        [        [        X5      5      SUS.6mTc  T$ Tu  mnU(       a   [        [        TU5      5      [        U5      4$ [        T5      [        U5      4$ )a  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   <   >#    U  H  u  pX-  TU-  :H  v   M     g 7fNr   ).0vr   results      r   	<genexpr>crt.<locals>.<genexpr>Z   s     =94115FQJ&9s   F)check	symmetric)listmapr   r
   r	   r   allzipsolve_congruenceintr   )r   r   r   r   mmr   s        @r   crtr'      s    t VQ VQ A"F	aB=3q9===%tCI96F~JFB$VR013r7::v;Br   c                 "    [        U [        5      $ )a  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 1b>r   c                     [        XX#U[        5      nU(       a  [        [        Xb5      5      [        U5      4$ [        U5      [        U5      4$ )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-      sD    , Q2!R(F$V013r7::v;Br   c                  >   S nU nUR                  SS5      nUR                  SS5      (       a  U VVs/ s H  u  pV[        U5      [        U5      4PM     nnn0 nU H  u  pVXV-  nXg;   a  XWU   :w  a    gM  XWU'   M      UR                  5        VVs/ s H  u  peXV4PM
     nnnA[        S U 5       5      (       a  [	        [        U6 5      u  pV[        XeUSS9$ S	nU H  n	U" X5      nUc    gUu  pX-  n
M     U(       a  [        W
W5      U4$ W
W4$ s  snnf s  snnf )
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                     U u  p#Uu  pEX4U-
  Upn[        XgU5      n	XgU4 V
s/ s H  oU	-  PM	     sn
u  pgnUS:w  a  [        Xh5      u  pnU	S:w  a  gX{-  nX#U-  -   X8-  pXm4$ s  sn
f )zReturn 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
   Nr   )c1c2a1m1a2m2r   bcgiinv_a_r   s                 r   combine!solve_congruence.<locals>.combine   s     2graaL"#+Aa4+a6 ,KAaAvJAqDy"$1t ,s   A"r   Fr   TNc              3   <   #    U  H  u  p[        U5      v   M     g 7fr   r   )r   rr   s      r   r   #solve_congruence.<locals>.<genexpr>  s     )bdawqzzbs   )r   r   )r   r0   )getr   itemsr"   r    r#   r'   r   )remainder_modulus_pairshintr=   rmr   r@   r   uniqrvrmins              r   r$   r$      s6   h, 
!Be,Ixx134vay&)$4 DAFAyQ<G  "&.qf.
 )b)))R>DAqy>>	BR:E  $Q*A--!tS 5* /s   "DD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>rQ      s7     + + " < < 'K \&R :wr   