
    \hr                      S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJr  S SKJrJr  S S	KJrJrJrJrJr  S S
KJr  S SKJr  S SKJrJr  S SKJrJ r   S SK!J"r"  S SK#J$r$J%r%J&r&J'r'  S SK(J)r)  S SK*J+r+  S SK,J-r-  S SK.J/r0  S SK1J2r2J3r3J4r4  S SK5J6r6  S SK7J8r8J9r9  S SK:J;r;  S SK<J=r=J>r>  S SK?J@r@  S SKAJBrBJCrC  S SKDJErE  S SKFJGrG  S SKHJIrI  S SKJJKrKJLrL  S SKMJNrNJOrOJPrPJQrQJRrR  S S!/rS " S" S#\T5      rU " S$ S%5      rV " S& S'\V5      rW " S( S)\V5      rX " S* S+\V5      rY " S, S-\V5      rZ " S. S/\V5      r[ " S0 S1\V5      r\ " S2 S3\V5      r] " S4 S5\V5      r^ " S6 S7\V5      r_ " S8 S9\V5      r` " S: S;\V5      ra " S< S=\V5      rb\X\W\Y\Z\[\\\]\^\_\`\a\b/rc\c V s1 s H  o R                  iM     sn reS> rfS? rgS@ rhSA riSB rj\ " SCSDSE9SFSG4SH jrkSI rlS|SJ jrm\ " SCSDSE94SK jrnS}SL jroSMSNR                  \q" \e5      5      -  \olr        \ " SCSDSE94SO jrsS|SP jrtSQ ruSR rv\ " SCSDSE94SS jrwST rx\ " SCSDSE94SU jrySV rzS~SX jr{SY r|\ " SCSDSE94SZ jr}S[ r~S\ rS] rS^ rS_ rS` rSa rSSb jrSc rSd rSe rSf rSg rSSh jrSi rSSj jrSk rSl rSm rSn rSSo jrSp r\ " SWSDSE94Sq jrSSr jrSSs jrSSt jrSu rSv rSw rSSx jr\rSy rSSz jrS{ rgFs  sn f )    )annotations)Add)check_assumptions)Tuple)factor_terms)_mexpand)Mul)Rational
int_valued)igcdexilcmigcdinteger_nthrootisqrt)Eq)S)default_sort_keyordered)Symbolsymbols)_sympify)jacobiremoveinvertiroot)sign)floor)sqrt)MutableDenseMatrix)divisors	factorintperfect_power)	nextprime)	is_squareisprime)symmetric_residue)sqrt_modsqrt_mod_iter)GeneratorsNeeded)Polyfactor_list)signsimp)solveset_real)numbered_symbols)as_int
filldedent)is_sequencesubsetspermute_signssigned_permutationsordered_partitionsdiophantineclassify_diopc                  P   ^  \ rS rSrSrU 4S jrU 4S jrS rS rS r	S r
S	rU =r$ )
DiophantineSolutionSet(   a@  
Container for a set of solutions to a particular diophantine equation.

The base representation is a set of tuples representing each of the solutions.

Parameters
==========

symbols : list
    List of free symbols in the original equation.
parameters: list
    List of parameters to be used in the solution.

Examples
========

Adding solutions:

    >>> from sympy.solvers.diophantine.diophantine import DiophantineSolutionSet
    >>> from sympy.abc import x, y, t, u
    >>> s1 = DiophantineSolutionSet([x, y], [t, u])
    >>> s1
    set()
    >>> s1.add((2, 3))
    >>> s1.add((-1, u))
    >>> s1
    {(-1, u), (2, 3)}
    >>> s2 = DiophantineSolutionSet([x, y], [t, u])
    >>> s2.add((3, 4))
    >>> s1.update(*s2)
    >>> s1
    {(-1, u), (2, 3), (3, 4)}

Conversion of solutions into dicts:

    >>> list(s1.dict_iterator())
    [{x: -1, y: u}, {x: 2, y: 3}, {x: 3, y: 4}]

Substituting values:

    >>> s3 = DiophantineSolutionSet([x, y], [t, u])
    >>> s3.add((t**2, t + u))
    >>> s3
    {(t**2, t + u)}
    >>> s3.subs({t: 2, u: 3})
    {(4, 5)}
    >>> s3.subs(t, -1)
    {(1, u - 1)}
    >>> s3.subs(t, 3)
    {(9, u + 3)}

Evaluation at specific values. Positional arguments are given in the same order as the parameters:

    >>> s3(-2, 3)
    {(4, 1)}
    >>> s3(5)
    {(25, u + 5)}
    >>> s3(None, 2)
    {(t**2, t + 2)}
c                   > [         TU ]  5         [        U5      (       d  [        S5      e[        U5      (       d  [        S5      e[	        U5      U l        [	        U5      U l        g )Nz$Symbols must be given as a sequence.z'Parameters must be given as a sequence.)super__init__r1   
ValueErrortupler   
parameters)selfsymbols_seqr@   	__class__s      ]/var/www/auris/envauris/lib/python3.13/site-packages/sympy/solvers/diophantine/diophantine.pyr=   DiophantineSolutionSet.__init__f   sT    ;''CDD:&&FGG[)
+    c                  > [        U5      [        U R                  5      :w  a/  [        S[        U R                  5      < S[        U5      < 35      e[        U5      n[	        [        U5      5       HZ  nX   n[        U5      [        Ld  M  U* R                  (       d  M/  U* U;  d  M8  U Vs/ s H  oUR                  U* U5      PM     nnM\     [        TU ])  [        U6 5        g s  snf )Nz!Solution should have a length of z, not )lenr   r>   setrangetypeint	is_Symbolsubsr<   addr   )rA   solutionargsix_rC   s         rD   rO   DiophantineSolutionSet.addr   s    x=C--cRVR^R^N_ademanopp8}s8}%AA7c>rnnn!43;<8aFFA2qM8< & 	E8$% =s   .C&c                8    U H  nU R                  U5        M     g N)rO   )rA   	solutionsrP   s      rD   updateDiophantineSolutionSet.update}   s    !HHHX "rF   c              #  r   #    [        U 5       H$  n[        [        U R                  U5      5      v   M&     g 7frW   )r   dictzipr   )rA   rP   s     rD   dict_iterator$DiophantineSolutionSet.dict_iterator   s)     Hs4<<233 &s   57c                    [        U R                  U R                  5      nU  H$  nUR                  UR                  " U0 UD65        M&     U$ rW   )r9   r   r@   rO   rN   )rA   rQ   kwargsresultrP   s        rD   rN   DiophantineSolutionSet.subs   s?    'dooFHJJx}}d5f56 rF   c                0   [        U5      [        U R                  5      :  a/  [        S[        U R                  5      < S[        U5      < 35      e[        U R                  U5       VVs0 s H  u  p#Uc  M
  X#_M     nnnU R	                  U5      $ s  snnf )NzEvaluation should have at most z values, not )rH   r@   r>   r]   rN   )rA   rQ   pvreps        rD   __call__DiophantineSolutionSet.__call__   sx    t9s4??++SVW[WfWfSgilmqirstt #DOOT :L :atqt :Lyy~ Ms   +	B8B)r@   r   )__name__
__module____qualname____firstlineno____doc__r=   rO   rY   r^   rN   rh   __static_attributes____classcell__)rC   s   @rD   r9   r9   (   s,    ;z
,	&4 rF   r9   c                  l    \ rS rSr% SrS\S'   SS jrS r\S 5       r	\S	 5       r
SSS
 jjrSS jrSrg)DiophantineEquationType   a  
Internal representation of a particular diophantine equation type.

Parameters
==========

equation :
    The diophantine equation that is being solved.
free_symbols : list (optional)
    The symbols being solved for.

Attributes
==========

total_degree :
    The maximum of the degrees of all terms in the equation
homogeneous :
    Does the equation contain a term of degree 0
homogeneous_order :
    Does the equation contain any coefficient that is in the symbols being solved for
dimension :
    The number of symbols being solved for
strnameNc                   [        U5      R                  SS9U l        Ub  X l        OA[	        U R                  R                  5      U l        U R                  R                  [        S9  U R                  (       d  [        S5      eU R                  R                  5       U l	        [        S U R                  R                  5        5       5      (       d  [        S5      e[        U R                  5      R                  5       U l        SU R                  ;  U l        [!        U R                  5      [!        U R                  5      -  (       + U l        [%        U R                  5      U l        S U l        g )NTforcekeyz+equation should have 1 or more free symbolsc              3  8   #    U  H  n[        U5      v   M     g 7frW   r   ).0cs     rD   	<genexpr>3DiophantineEquationType.__init__.<locals>.<genexpr>   s     >*=Q:a==*=   zCoefficients should be Integers   )r   expandequationfree_symbolslistsortr   r>   as_coefficients_dictcoeffallvalues	TypeErrorr*   total_degreehomogeneousrI   homogeneous_orderrH   	dimension_parameters)rA   r   r   s      rD   r=    DiophantineEquationType.__init__   s    *111=# , $T]]%?%? @D""'7"8  JKK]]779
>$***;*;*=>>>=>> /<<>DJJ.&)$**oD<M<M8N&N!OT../rF   c                    g)zV
Determine whether the given equation can be matched to the particular equation type.
F rA   s    rD   matchesDiophantineEquationType.matches   s     rF   c                    U R                   $ rW   r   r   s    rD   n_parameters$DiophantineEquationType.n_parameters   s    ~~rF   c                n    U R                   c  [        SU R                  4-  SS9U l         U R                   $ )Nzt_:%iTinteger)r   r   r   r   s    rD   r@   "DiophantineEquationType.parameters   s7    #&w$2C2C1E'EtTDrF   c                2    [        SU R                  -  5      e)N"No solver has been written for %s.)NotImplementedErrorru   )rA   r@   limits      rD   solveDiophantineEquationType.solve   s    !"F"RSSrF   c                    U R                  5       (       d  [        SU R                  -  5      eUb?  [        U5      U R                  :w  a&  [        SU R                  < S[        U5      < 35      eXl        g )Nz2This equation does not match the %s equation type.z	Expected z parameter(s) but got )r   r>   ru   rH   r   r   )rA   r@   s     rD   	pre_solve!DiophantineEquationType.pre_solve   s`    ||~~QTXT]T]]^^!:$"3"33 $J[J[]`ak]l!mnn%rF   )r   r   r   r   r   r   r   r   rW   NNreturnr9   )rj   rk   rl   rm   rn   __annotations__r=   r   propertyr   r@   r   r   ro   r   rF   rD   rr   rr      sK    . I ,      
T&rF   rr   c                  ,    \ rS rSrSrSrS rSS jrSrg)	
Univariate   a  
Representation of a univariate diophantine equation.

A univariate diophantine equation is an equation of the form
`a_{0} + a_{1}x + a_{2}x^2 + .. + a_{n}x^n = 0` where `a_{1}, a_{2}, ..a_{n}` are
integer constants and `x` is an integer variable.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import Univariate
>>> from sympy.abc import x
>>> Univariate((x - 2)*(x - 3)**2).solve() # solves equation (x - 2)*(x - 3)**2 == 0
{(2,), (3,)}


univariatec                     U R                   S:H  $ Nr   r   r   s    rD   r   Univariate.matches   s    ~~""rF   Nc                   U R                  U5        [        U R                  U R                  S9n[	        U R
                  U R                  S   5      R                  [        R                  5       H  nUR                  U45        M     U$ )Nr@   r   )
r   r9   r   r@   r-   r   	intersectr   IntegersrO   )rA   r@   r   rb   rR   s        rD   r   Univariate.solve   sg    z"'(9(9dooVt}}d.?.?.BCMMajjYAJJt ZrF   r   r   	rj   rk   rl   rm   rn   ru   r   r   ro   r   rF   rD   r   r      s    " D#rF   r   c                  ,    \ rS rSrSrSrS rSS jrSrg)	Linear   a  
Representation of a linear diophantine equation.

A linear diophantine equation is an equation of the form `a_{1}x_{1} +
a_{2}x_{2} + .. + a_{n}x_{n} = 0` where `a_{1}, a_{2}, ..a_{n}` are
integer constants and `x_{1}, x_{2}, ..x_{n}` are integer variables.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import Linear
>>> from sympy.abc import x, y, z
>>> l1 = Linear(2*x - 3*y - 5)
>>> l1.matches() # is this equation linear
True
>>> l1.solve() # solves equation 2*x - 3*y - 5 == 0
{(3*t_0 - 5, 2*t_0 - 5)}

Here x = -3*t_0 - 5 and y = -2*t_0 - 5

>>> Linear(2*x - 3*y - 4*z -3).solve()
{(t_0, 2*t_0 + 4*t_1 + 3, -t_0 - 3*t_1 - 3)}

linearc                     U R                   S:H  $ r   )r   r   s    rD   r   Linear.matches  s      A%%rF   Nc                   U R                  U5        U R                  nU R                  nSU;   a  US   * nOSn[        X@R                  S9nUR                  n[        U5      S:X  a.  [        XSUS      5      u  pU	(       d  UR                  U45        U$  U V
s/ s H  oU
   PM	     nn
/ n[        U5      S:  a  UR                  [        US   US   5      5        US   US   -  US'   US   US   -  US'   [        [        U5      S-
  SS5       H:  n[        US   X   5      nUS   U-  US'   X   U-  X'   UR                  SU5        M<     UR                  US   5         / n[        X5       GH^  u  nn/ / nn[        R                  " U5       GH  nUR                  (       a  U[         R"                  nnUS   nO)UR%                  5       u  nnXwR'                  U5      S-      n[)        UUUU5      =nu  nnU[         R"                  L a  S U;   a  Us  s  $ On[+        U[        5      (       a"  UR,                  S   U-  UR,                  S   -   n[+        U[        5      (       a"  UR,                  S   U-  UR,                  S   -   nUR                  U5        UR                  U5        GM     UR                  [        U6 5        [        U6 nGMa     UR                  U5        UR                  U5        U$ s  sn
f )Nr   r   r         )r   r   r   r9   r@   rH   divmodrO   appendr   rJ   insertr]   r   	make_args
is_Integerr   Oneas_coeff_Mulindexbase_solution_linear
isinstancerQ   )rA   r@   r   r   varr~   rb   paramsqrrf   ABrR   gcdrX   AiBitot_xtot_yargkre   pnewsolsol_xsol_ys                              rD   r   Linear.solve  s   z"

: q	AA'H""s8q=!3q6]+DA

A4 M(	T  ##s!1Xs#s8a<HHT!B%2'(bEQqTMAbEbEQqTMAbE3q6A:q"-1Q4&ts{!ts{C 	 .
 	
23	h 	!iFBr5E}}Q'>>qA!!9D ++-DAq!,,q/A"56D%9!RT%JJleU:s{% # "%-- %

1a%**Q- ?!%-- %

1a%**Q- ?U#U#1 (4 S%[)UA=  @ 	

9I $s   K8r   r   r   r   rF   rD   r   r      s    2 D&drF   r   c                  0    \ rS rSrSrSrS rSS	S jjrSrg)
BinaryQuadratici  a\  
Representation of a binary quadratic diophantine equation.

A binary quadratic diophantine equation is an equation of the
form `Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0`, where `A, B, C, D, E,
F` are integer constants and `x` and `y` are integer variables.

Examples
========

>>> from sympy.abc import x, y
>>> from sympy.solvers.diophantine.diophantine import BinaryQuadratic
>>> b1 = BinaryQuadratic(x**3 + y**2 + 1)
>>> b1.matches()
False
>>> b2 = BinaryQuadratic(x**2 + y**2 + 2*x + 2*y + 2)
>>> b2.matches()
True
>>> b2.solve()
{(-1, -1)}

References
==========

.. [1] Methods to solve Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0, [online],
      Available: https://www.alpertron.com.ar/METHODS.HTM
.. [2] Solving the equation ax^2+ bxy + cy^2 + dx + ey + f= 0, [online],
      Available: https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf

binary_quadraticc                L    U R                   S:H  =(       a    U R                  S:H  $ Nr   r   r   r   s    rD   r   BinaryQuadratic.matches  !      A%=$..A*==rF   Nc                d  ^;^<^=^>^?^@^A^B^C U R                  U5        U R                  nU R                  nUu  pVXES-     nXEU-     nXFS-     n	XE   m;XF   m<U[        R                     m=[        XxU	T;T<T=5       V
s/ s H  n
[        U
5      PM     sn
u  pxn	m;m<m=[        X0R                  5      nUR                  u  mCnUS-  SU-  U	-  -
  nUS:X  Ga  U	S:X  Ga
  US:w  Ga  T;T<-  UT=-  -
  S:X  aT  [        T<U5      u  pU(       d  UR                  U* TC45        [        T;U5      u  pU(       d  UR                  TCU* 45        U$ [        T;T<-  UT=-  -
  5      nUU Vs/ s H  nU* PM     sn-   nU Hl  n[        UT<-
  U5      u  nnU(       a  M  [        T;T<-  UT=-  -
  U5      u  pU(       a  M>  [        UT;-
  U5      u  nnU(       a  MY  UR                  UU45        Mn      U$ US:X  Ga  US:X  aH  [        U R                  Xe/S9R                  TCU/S9nU H  nUR                  US   US   45        M     U$ [        U5      [!        Xy5      -  m@UT@-  nU	T@-  n[        X-  5      m?[#        U5      mA[#        U5      mBT?TB-  T;-  TAT<-  -
  m>T>(       d  [%        SSS	9nTAT@-  US-  -  T;U-  -   TAT=-  -   n['        UU5      R)                  [        R*                  5      nU H6  n[-        TAU-  T?TB-  U-  -   U-
  5      nUR                  US   US   45        M8     U$ [/        U5      (       a  U<U=U>U?U@UBUC4S
 jnU;U=U>U@UAUC4S jn[1        S[3        T>5      5       Hx  n [5        TAT@-  U S-  -  T;U -  -   TAT=-  -   T>5      (       d  M+  [5        T?TB-  T@-  U S-  -  T<U -  -   T?TB-  T=-  -   T>5      (       d  MY  UR                  U" U 5      U" U 5      45        Mz     U$ [7        U5      (       Ga  US:w  Ga  [9        U5      n[;        SSS9u  nn![=        SU-  U-  U-  U!-  SU-  T;-  UU!-  X-  -   UU!-  -   X-  -
  -  -   SU-  S-  U-  T<-  UU!-
  -  -   SU-  U-  S-  U-  T=-  -   5      n[-        UTC5      n"U" GH  u  n#n$UU$-  UU#-  -   UU$-  -   UU#-  -
  n%[        U%5      SU-  U-  -  n&[        U#U$-
  5      SU-  -  n'[?        U#[$        5      (       d  [?        U$[$        5      (       aH  [A        [C        U&U'SU-  U-  U5      5      S:  a%  [C        U&U'SU-  U-  U5      nURD                  " U6   M  M  U&RF                  (       d  M  U'RF                  (       d  M  [I        X4U&U'5      (       d  M  UR                  U&U'45        GM     U$ [        U R                  US S S2   S9R                  TCU/S9nU(       a.  UR                  URK                  5       S S S2   5        U(       a  M.  U$ [M        X45      u  n(n)[O        X45      u  m;n*[Q        T;U*5      n+T;S:  ad  U+ H\  u  nnU* U4 HM  nU* U4 HA  nU([S        XV/5      -  U)-   n UR                  U V,s/ s H  n,[        U,5      PM     sn,5        MC     MO     M^     U$ [W        U+5      n+U+RE                  S [Y        U+5       5       5        [Q        T;S5      nUS   S   n-US   S   n.[[        S U(S S U)S S -    5       5      (       a  U+ H  u  nnUU[9        T;5      -  -   U-U.[9        T;5      -  -   TC-  -  n/UU[9        T;5      -  -
  U-U.[9        T;5      -  -
  TC-  -  n0[=        [        U/U0-   5      S-  5      n1[=        [        U/U0-
  5      S[9        T;5      -  -  5      n2U([S        U1U2/5      -  U)-   nUR                  U5        M     U$ []        U(S S U)S S -    V,s/ s H  n,U,R^                  PM     sn,6 n3Sn4U-n5U.n6U5S-
  U3-  S:w  d	  U6U3-  S:w  a7  U5U--  T;U6-  U.-  -   U5U.-  U6U--  -   n6n5U4S-  n4U5S-
  U3-  S:w  a  M,  U6U3-  S:w  a  M7  U+ H  u  n7n8[1        U45       H  n
[[        S U([S        U7U8/5      -  U)-    5       5      (       a  U7[9        T;5      U8-  -   U5[9        T;5      U6-  -   TC-  -  n/U7[9        T;5      U8-  -
  U5[9        T;5      U6-  -
  TC-  -  n0[        U/U0-   5      S-  n9[        U/U0-
  5      S[9        T;5      -  -  n:U([S        U9U:/5      -  U)-   nUR                  U5        U7U--  T;U.-  U8-  -   U7U.-  U8U--  -   n8n7M     M     U$ s  sn
f s  snf s  sn,f ! [T         a     GM  f = fs  sn,f )Nr      r   r   r   r   zT)realc                   > T* T-  T-  T-  TS-  -  TST-  T-  T-  U -  -   T-  -
  TT-  T-  U S-  -  TU -  -   TT-  T-  -   T-  -
  $ r   r   )uEF_cegsqcts    rD   <lambda>'BinaryQuadratic.solve.<locals>.<lambda>8  sn    3qAqD(8A!C	!OQ;N(N+,S571a4<!A#+=#a+GB*N)OrF   c                z   > TT-  T-  TS-  -  TST-  T-  U -  -   T-  -   TT-  U S-  -  TU -  -   TT-  -   T-  -   $ r   r   )r   Dr   r   r   sqar   s    rD   r   r   ;  s[    AbAQsU1WQY8I(I+.q5A:!+;c!e+C*J)KrF   zu, vr   r   c              3  2   #    U  H  u  pU* U* 4v   M     g 7frW   r   )r}   XYs      rD   r   (BinaryQuadratic.solve.<locals>.<genexpr>y  s     !H7GtqA2r(7Gs   c              3  8   #    U  H  n[        U5      v   M     g 7frW   r|   r}   rT   s     rD   r   r     s     <mz!}}mr   c              3  8   #    U  H  n[        U5      v   M     g 7frW   r|   r  s     rD   r   r     s     "O:NQ:a==:Nr   )0r   r   r   r   r   _remove_gcdr/   r9   r@   r   rO   r    r   r   r   r   r   r   r   r-   r   r   
diop_solver   rJ   abs	divisibler$   r   r   r   r   rH   check_paramrY   r   is_solution_quadpop_transformation_to_DN_find_DNdiop_DNMatrixr>   rI   r   r   r   r   )DrA   r@   r   r   r   rS   yr   r   CrR   rb   r   discrr   r   divtermdx0y0ssolnar~   r   eqrootsrootanssolve_xsolve_yz0rf   rP   s0t0numx_0y_0PQN
solns_pellrT   TU_a_bx_ny_nLr   T_kU_kr   r   XtYtr   r   r   r   r   r   r   r   r   sD                                                              @@@@@@@@@rD   r   BinaryQuadratic.solve  s	   z"

Q$KA#JQ$KHH!%%L/:1Aq!/LM/L!F1I/LMaAq (__=  11qs1u6a1fasQqSyA~a|JJAw'a|JJA2w'h e qsQqSy)s3stdUs33A"1q5!,EB1%acAaCi3 q$*1q5!$4EB#$1 &

B8 4 ` E aZAv#DMMGMMZ[]^Y_M`DJJQa12 | u GDJ&FFKAhAhsU1Ws1u_s.AQq!tac)CE1B)"a0::1::FE %(Q3q4)?@

CFCF#34 !&\ U  ]]O OGK KG $As2w/%c!eBEkAbD&83q5&@"EE%aeAgb!emad&:QsU1W&DbII"JJWR['AB	 0F s uAvKvt41aCE!GAI!AqsQSy1Q3'< ==aCE!GAIq1u%&()!Aa	!45 &b!,&FBB$2+",qt3CC&AaCE*CBG*!,C!"f--B1G1G{3QqSUJGH1L"-c3!Az"JC"MM3/ M CNNN+CSAA"JJSz2 '^ C $DMMDbD	JPP]^`a\bPcJJquuwtt}- a@ s )4DAqC'DAq AJ1u(FB!c2Y#%#rA !&!. 01 4A% &

q+Aq!F1Iq+A B "+ ' )h S !_
!!!HtJ7G!HHAqMaDGaDG<aeaem<<< *1!DG)ma!DG)ma-??!DG)ma!DG)ma-??&qbzA~6&qbzQtAwY'?@fc3Z0014

1 !+B 1 AbqEAbqEM:MqqssM:;AACC7a-1,a1#&q51S57?CECEMSQ 7a-1,a1 !+1!&qA""O!FAq6N:JQ:N"OOO&'$q'!)mcDGCK6G!5K%K&'$q'!)mcDGCK6G!5K%K%&rBwZ!^%&rBwZ1T!W9%=$%fb"X&6$6$: &

1#$Q31Q;!ac	qAq "* !+ O N, 4D ,B#- % $%, ;s6   8dd0d?dd!d-d
d*	)d*	r   r   r   r   r   rF   rD   r   r     s    > D>v vrF   r   c                  "    \ rS rSrSrSrS rSrg)InhomogeneousTernaryQuadratici  zu

Representation of an inhomogeneous ternary quadratic.

No solver is currently implemented for this equation type.

inhomogeneous_ternary_quadraticc                    U R                   S:X  a  U R                  S:X  d  gU R                  (       d  gU R                  (       + $ )Nr   r   F)r   r   r   r   r   s    rD   r   %InhomogeneousTernaryQuadratic.matches  s9    !!Q&4>>Q+>))))rF   r   Nrj   rk   rl   rm   rn   ru   r   ro   r   rF   rD   r7  r7    s     -D*rF   r7  c                  0    \ rS rSrSrSrS rSS	S jjrSrg)
!HomogeneousTernaryQuadraticNormali  a1  
Representation of a homogeneous ternary quadratic normal diophantine equation.

Examples
========

>>> from sympy.abc import x, y, z
>>> from sympy.solvers.diophantine.diophantine import HomogeneousTernaryQuadraticNormal
>>> HomogeneousTernaryQuadraticNormal(4*x**2 - 5*y**2 + z**2).solve()
{(1, 2, 4)}

$homogeneous_ternary_quadratic_normalc                d  ^ U R                   S:X  a  U R                  S:X  d  gU R                  (       d  gU R                  (       d  gU R                   Vs/ s H  oR                  U   (       d  M  UPM     snm[        T5      S:H  =(       a    [        U4S jU R                   5       5      $ s  snf )Nr   r   Fc              3  2   >#    U  H  oS -  T;   v   M     g7fr   Nr   r}   rR   nonzeros     rD   r   <HomogeneousTernaryQuadraticNormal.matches.<locals>.<genexpr>  s     (TBSQABS   r   r   r   r   r   rH   r   r   rA   r   rC  s     @rD   r   )HomogeneousTernaryQuadraticNormal.matches  s    !!Q&4>>Q+>%%"jj:jJJqM1j:7|q TS(T$BSBS(T%TT ;s   B-0B-Nc           
        U R                  U5        U R                  nU R                  nUu  pVnXES-     nXFS-     n	XGS-     n
[        XU
SS9u  u  pnu  pnu  nnnU* U-  nU* U-  n[	        X0R
                  S9nUS:  a  US:  a  U$ [        U* U-  U5      b"  [        U* U-  U5      b  [        U* U-  U5      c  U$ [        UU5      u  nnn[        U[        U5      5      u  nnUU-  nUU-  n[        UUU5      u  nnn[        U5      [        U	5      :X  a0  [        UUU[        U5      [        U5      [        U5      5      u  nnnOw[        U5      [        U
5      :X  a0  [        UUU[        U5      [        U5      [        U5      5      u  nnnO/[        UUU[        U5      [        U5      [        U5      5      u  nnn[        UUU5      n[        UUU5      n[        XU5      n[        XU5      n[        UU-  U-  5      n[        UU-  U-  5      n[        UU-  U-  5      nUR                  [        UUU5      5        U$ )Nr   T)stepsr   r   )r   r   r   
sqf_normalr9   r@   r'   descent_rational_pqr  r  r   holzerreconstructr   rO   )rA   r@   r   r   r   rS   r  r   r  br~   sqf_of_asqf_of_bsqf_of_ca_1b_1c_1a_2b_2c_2r   r   rb   z_0r$  r%  r   sq_lcms                               rD   r   'HomogeneousTernaryQuadraticNormal.solve  sU   z"

aQ$KQ$KQ$K qQd+ 	I&X3#sC DHDH'H q5QUM cT#Xs#+cT#Xs#+cT#Xs#+M1S#c3s8,Qqq#Cc2S# 7d1g"3S#c(CHc#hOMCc!WQ"3S#c(CHc#hOMCc"3S#c(CHc#hOMCc#sC(#sC(#C(h(3#f*()#f*()#f*()

;sC-.rF   r   r   r   r   r   rF   rD   r=  r=    s     2D	U9 9rF   r=  c                  ,    \ rS rSrSrSrS rSS jrSrg)	HomogeneousTernaryQuadratici  a  
Representation of a homogeneous ternary quadratic diophantine equation.

Examples
========

>>> from sympy.abc import x, y, z
>>> from sympy.solvers.diophantine.diophantine import HomogeneousTernaryQuadratic
>>> HomogeneousTernaryQuadratic(x**2 + y**2 - 3*z**2 + x*y).solve()
{(-1, 2, 1)}
>>> HomogeneousTernaryQuadratic(3*x**2 + y**2 - 3*z**2 + 5*x*y + y*z).solve()
{(3, 12, 13)}

homogeneous_ternary_quadraticc                n  ^ U R                   S:X  a  U R                  S:X  d  gU R                  (       d  gU R                  (       d  gU R                   Vs/ s H  oR                  U   (       d  M  UPM     snm[        T5      S:H  =(       a    [        U4S jU R                   5       5      (       + $ s  snf )Nr   r   Fc              3  2   >#    U  H  oS -  T;   v   M     g7frA  r   rB  s     rD   r   6HomogeneousTernaryQuadratic.matches.<locals>.<genexpr>)  s     -YGX!dgoGXrE  rF  rG  s     @rD   r   #HomogeneousTernaryQuadratic.matches   s    !!Q&4>>Q+>%%"jj:jJJqM1j:LA%Y#-YtGXGX-Y*YZZ ;s   B20B2Nc                F  ^ U R                  U5        U R                  nU R                  mUu  pEnXEU/n[        XpR                  S9nS n	[        U4S jU 5       5      (       d  TXF-     (       aX  [        TXE-     U-  TXV-     U-  -   XF-  -
  5      n
[        U
S S9nUR                  [        US   TXF-     * US   5      5        U$ US   US   sUS'   US'   U	" [        UT5      5      u  pnUb  UR                  XU45        U$ TUS-     S:X  a\  TUS-     S:X  a(  US   US   sUS'   US'   U	" [        UT5      5      u  pnGOUS   US   sUS'   US'   U	" [        UT5      5      u  pnGOTXE-     (       d  TXF-     (       a  TUS-     nTXE-     nTXF-     nTUS-     nTXV-     nTUS-     n0 nS	US-  -  UUS-  '   S	U-  U-  US-  -
  UUS-  '   S	U-  U-  US-  -
  UUS-  '   S	U-  U-  SU-  U-  -
  UXV-  '   SUXE-  '   SUXF-  '   U	" [        UU5      5      u  pnUc  U$ [        UU-  UU-  -   SU-  5      u  nnUU-  U-
  UU-  UU-  pnOTXe-     S:w  a  TUS-     S:X  aX  TUS-     S:X  a%  TUS-     nTXV-     n[        U* U5      u  nnUUUpnOcUS   US   sUS'   US'   U	" [        UT5      5      u  pnO<US   US   sUS'   US'   U	" [        UT5      5      u  pnOU	" [        UT5      5      u  pnUc  U$ UR                  [        XU5      5        U$ )
Nr   c                >    [        U 5      S:  a  [        U 5      S   $ g)Nr   NNN)rH   r   )r   s    rD   
unpack_sol5HomogeneousTernaryQuadratic.solve.<locals>.unpack_sol?  s    3x!|Cy|##rF   c              3  4   >#    U  H  nTUS -     v   M     g7frA  r   r}   rR   r   s     rD   r   4HomogeneousTernaryQuadratic.solve.<locals>.<genexpr>D  s     ,15A;   c                <    [        U S   5      [        U S   5      -   $ Nr   r   r  )r   s    rD   r   3HomogeneousTernaryQuadratic.solve.<locals>.<lambda>G  s    C!IAaD	,ArF   ry   r   r   r   r   )r   r   r   r9   r@   anyr6   minrO   r  _diop_ternary_quadraticrM  _diop_ternary_quadratic_normal)rA   r@   r   _varrS   r  r   r   rb   rg  solsr  r%  r$  rZ  r   r   r  r   r   r   _coeffre   r   rP  r  r   s                             @rD   r   !HomogeneousTernaryQuadratic.solve+  s   z"  

aQi (H	$
 ,,,,QSz"5:a<%*Q,#>#DE"AB

;qteACj[!A$?@!!Wd1gNCFCF&'>sE'JKMCc

Cc?+MA;!QT{a!%a$q'AA *+B3+N O# "&a$q'AA *+B3+N O# QSzU13Z!Q$K!#J!#J!Q$K!#J!Q$K Avq!t s1uq!t|q!t s1uq!t|q!tc!eac!emqsqsqs *+B3+O P#;!M#AcEAcEM1Q371 #A	3q5#a%##qsqA;!#QT{a'!!Q$K!!#J+QB21()1a## *.a$q'AA(23J3PU3V(W# &*!Wd1gNCFCF$./FsE/R$SMCc !++I#u+U V#;M

;s-.rF   r   r   r   r   rF   rD   r^  r^    s     +D	[hrF   r^  c                  "    \ rS rSrSrSrS rSrg)InhomogeneousGeneralQuadratici  zu

Representation of an inhomogeneous general quadratic.

No solver is currently implemented for this equation type.

inhomogeneous_general_quadraticc                    U R                   S:X  a  U R                  S:  d  gU R                  (       d  g[        S U R                   5       5      =(       a    U R
                  (       + $ )Nr   r   FTc              3  8   #    U  H  oR                   v   M     g 7frW   is_Mulr}   r   s     rD   r   8InhomogeneousGeneralQuadratic.matches.<locals>.<genexpr>       0Z88Zr   r   r   r   rq  r   r   r   s    rD   r   %InhomogeneousGeneralQuadratic.matches  sM    !!Q&4>>Q+>%%0TZZ00I9I9I5IIrF   r   Nr;  r   rF   rD   rz  rz    s     -DJrF   rz  c                  "    \ rS rSrSrSrS rSrg)HomogeneousGeneralQuadratici  zr

Representation of a homogeneous general quadratic.

No solver is currently implemented for this equation type.

homogeneous_general_quadraticc                    U R                   S:X  a  U R                  S:  d  gU R                  (       d  g[        S U R                   5       5      =(       a    U R
                  $ )Nr   r   Fc              3  8   #    U  H  oR                   v   M     g 7frW   r~  r  s     rD   r   6HomogeneousGeneralQuadratic.matches.<locals>.<genexpr>  r  r   r  r   s    rD   r   #HomogeneousGeneralQuadratic.matches  sJ    !!Q&4>>Q+>%%0TZZ00ET5E5EErF   r   Nr;  r   rF   rD   r  r    s     +DFrF   r  c                  ,    \ rS rSrSrSrS rSS jrSrg)	GeneralSumOfSquaresi  a[  
Representation of the diophantine equation

`x_{1}^2 + x_{2}^2 + . . . + x_{n}^2 - k = 0`.

Details
=======

When `n = 3` if `k = 4^a(8m + 7)` for some `a, m \in Z` then there will be
no solutions. Refer [1]_ for more details.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import GeneralSumOfSquares
>>> from sympy.abc import a, b, c, d, e
>>> GeneralSumOfSquares(a**2 + b**2 + c**2 + d**2 + e**2 - 2345).solve()
{(15, 22, 22, 24, 24)}

By default only 1 solution is returned. Use the `limit` keyword for more:

>>> sorted(GeneralSumOfSquares(a**2 + b**2 + c**2 + d**2 + e**2 - 2345).solve(limit=3))
[(15, 22, 22, 24, 24), (16, 19, 24, 24, 24), (16, 20, 22, 23, 26)]

References
==========

.. [1] Representing an integer as a sum of three squares, [online],
    Available:
    https://proofwiki.org/wiki/Integer_as_Sum_of_Three_Squares
general_sum_of_squaresc                   ^  T R                   S:X  a  T R                  S:  d  gT R                  (       d  g[        S T R                   5       5      (       a  g[        U 4S jT R                   5       5      $ )Nr   r   Fc              3  8   #    U  H  oR                   v   M     g 7frW   r~  r  s     rD   r   .GeneralSumOfSquares.matches.<locals>.<genexpr>       ,Axxr   c              3  V   >#    U  H  oS :w  d  M
  TR                   U   S :H  v   M      g7fr   Nr   r}   r   rA   s     rD   r   r    %     Dz!!V%4::a=A%z   	)))r   r   r   rq  r   r   r   s   `rD   r   GeneralSumOfSquares.matches  sX    !!Q&4>>Q+>%%,,,,DtzzDDDrF   Nc           	     ,   U R                  U5        U R                  n[        U R                  S   5      * nU R                  n[        X0R                  S9nUS:  d  US:  a  U$ U Vs/ s H  owR                  (       a  SOSPM     nnUR                  S5      S:g  n	Sn
[        XESS9 H]  nU	(       a5  UR                  [        U5       VVs/ s H  u  pX   U-  PM     snn5        OUR                  U5        U
S-  n
X:X  d  M\    U$    U$ s  snf s  snnf )Nr   r   r   r   Tzeros)r   r   rL   r   r   r9   r@   is_nonpositivecountsum_of_squaresrO   	enumerate)rA   r@   r   r   r   nrb   rS   signsnegstookr   rR   js                 rD   r   GeneralSumOfSquares.solve  s    z"ANN'Hq5EAIM8;<1''Q.<{{2!#D1A

9Q<@<41EHQJ<@A

1AID} 2  = As   (DDr   r   r   r   rF   rD   r  r    s    @ $DErF   r  c                  <    \ rS rSrSrSrS r\S 5       rS	S jr	Sr
g)
GeneralPythagoreani  ar  
Representation of the general pythagorean equation,
`a_{1}^2x_{1}^2 + a_{2}^2x_{2}^2 + . . . + a_{n}^2x_{n}^2 - a_{n + 1}^2x_{n + 1}^2 = 0`.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import GeneralPythagorean
>>> from sympy.abc import a, b, c, d, e, x, y, z, t
>>> GeneralPythagorean(a**2 + b**2 + c**2 - d**2).solve()
{(t_0**2 + t_1**2 - t_2**2, 2*t_0*t_2, 2*t_1*t_2, t_0**2 + t_1**2 + t_2**2)}
>>> GeneralPythagorean(9*a**2 - 4*b**2 + 16*c**2 + 25*d**2 + e**2).solve(parameters=[x, y, z, t])
{(-10*t**2 + 10*x**2 + 10*y**2 + 10*z**2, 15*t**2 + 15*x**2 + 15*y**2 + 15*z**2, 15*t*x, 12*t*y, 60*t*z)}
general_pythagoreanc                  ^  T R                   S:X  a  T R                  S:  d  gT R                  (       d  g[        S T R                   5       5      (       a  g[        U 4S jT R                   5       5      (       a  g[        U 4S jT R                   5       5      (       d  g[        [        U 4S jT R                   5       5      5      T R                  S-
  :H  $ )Nr   r   Fc              3  8   #    U  H  oR                   v   M     g 7frW   r~  r  s     rD   r   -GeneralPythagorean.matches.<locals>.<genexpr>  r  r   c              3  V   >#    U  H  oS :w  d  M
  TR                   U   S :H  v   M      g7fr  r  r  s     rD   r   r    s%     @:aa!tzz!}!:r  c              3  f   >#    U  H&  n[        [        TR                  U   5      5      v   M(     g 7frW   )r$   r  r   r  s     rD   r   r     s&     E*Q9SA/00*s   .1c              3  T   >#    U  H  n[        TR                  U   5      v   M     g 7frW   )r   r   r  s     rD   r   r  $  s!     ?JqtDJJqM**Js   %()r   r   r   rq  r   r   r  sumr   s   `rD   r   GeneralPythagorean.matches  s    !!Q&4>>Q+>%%,,,,@4::@@@E$**EEE 3?DJJ??@DNNUVDVVVrF   c                     U R                   S-
  $ r   r   r   s    rD   r   GeneralPythagorean.n_parameters&  s    ~~!!rF   Nc                H   U R                  U5        U R                  nU R                  nU R                  n[	        X4S   S-     5      [	        X4S   S-     5      -   [	        X4S   S-     5      -   S:  a  UR                  5        H
  nX6   * X6'   M     [        X@R                  S9nSn[        U5       H  u  p[	        X:S-     5      S:X  d  M  U	nM     UR                  n[        S U 5       5      nUSXS-
     S-  -  -
  /nUR                  [        US-
  5       V	s/ s H  n	SX   -  XS-
     -  PM     sn	5        US U U/-   XS  -   nSn[        U5       Hx  u  pX:X  d  US:  a  U	S:X  d  US:X  a+  U	S:X  a%  [        U[        [        X:S-     5      5      5      nMG  [        X:S-     5      n[        U[        U5      (       a  UOUS-  5      nMz     [        U5       H(  u  pXU	   -  [        [        X:S-     5      5      -  X'   M*     UR!                  U5        U$ s  sn	f )Nr   r   r   r   r   c              3  *   #    U  H	  oS -  v   M     g7frA  r   )r}   m_is     rD   r   +GeneralPythagorean.solve.<locals>.<genexpr>?  s     (as(as   )r   r   r   r   r   keysr9   r@   r  r  extendrJ   r   r   r  _oddrO   )rA   r@   r   r   r   r  rz   rb   r   rR   rf   mithr0  r   lcmr  s                    rD   r   GeneralPythagorean.solve*  s   z"

NN!fk"#d5Q1+=&>>ePQFVWKFXAYY\]]zz|#j[
 $ (HcNDAEq&M"b( # (a((1qQx1}$$%	uQU|<|!!ad(Q1uX%|<=i3%!F)+cNDAzeaiAF
qAv3SAv%7 89Av'3T!WW!q&9 # cNDAFld3u!V}+=&>>CF # 	

3 =s   Hr   r   )rj   rk   rl   rm   rn   ru   r   r   r   r   ro   r   rF   rD   r  r    s-     !DW " "&rF   r  c                  "    \ rS rSrSrSrS rSrg)	CubicThueiS  a  
Representation of a cubic Thue diophantine equation.

A cubic Thue diophantine equation is a polynomial of the form
`f(x, y) = r` of degree 3, where `x` and `y` are integers
and `r` is a rational number.

No solver is currently implemented for this equation type.

Examples
========

>>> from sympy.abc import x, y
>>> from sympy.solvers.diophantine.diophantine import CubicThue
>>> c1 = CubicThue(x**3 + y**2 + 1)
>>> c1.matches()
True


cubic_thuec                L    U R                   S:H  =(       a    U R                  S:H  $ )Nr   r   r   r   s    rD   r   CubicThue.matchesj  r   rF   r   Nr;  r   rF   rD   r  r  S  s    ( D>rF   r  c                  ,    \ rS rSrSrSrS rSS jrSrg)	GeneralSumOfEvenPowersin  aK  
Representation of the diophantine equation

`x_{1}^e + x_{2}^e + . . . + x_{n}^e - k = 0`

where `e` is an even, integer power.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import GeneralSumOfEvenPowers
>>> from sympy.abc import a, b
>>> GeneralSumOfEvenPowers(a**4 + b**4 - (2**4 + 3**4)).solve()
{(2, 3)}

general_sum_of_even_powersc                   ^  T R                   S:  d  gT R                   S-  S:w  a  g[        U 4S jT R                   5       5      (       d  g[        U 4S jT R                   5       5      $ )Nr   Fr   r   c              3     >#    U  H8  oS :w  d  M
  UR                   =(       a    UR                  TR                  :H  v   M:     g7fr  )is_Powexpr   r  s     rD   r   1GeneralSumOfEvenPowers.matches.<locals>.<genexpr>  s5     YJqWXRX:188:):): ::Js
   	A3Ac              3  V   >#    U  H  oS :w  d  M
  TR                   U   S :H  v   M      g7fr  r  r  s     rD   r   r    r  r  )r   r   r   r   s   `rD   r   GeneralSumOfEvenPowers.matches  sW      1$q A%YDJJYYYDtzzDDDrF   Nc           	        U R                  U5        U R                  nU R                  nS nUR                  5        H-  nUR                  (       d  M  XF   (       d  M!  UR
                  nM/     [        U5      nUS   * n[        X0R                  S9n	US:  d  US:  a  U	$ U V
s/ s H  oR                  (       a  SOSPM     nn
UR                  " S5      S:g  nSn[        XU5       H^  nU(       a6  U	R                  [        U5       VVs/ s H  u  nnX   U-  PM     snn5        OU	R                  U5        US-  nX:X  d  M]    U	$    U	$ s  sn
f s  snnf )Nr   r   r   r   )r   r   r   r  r  r  rH   r9   r@   r  r  power_representationrO   r  )rA   r@   r   r   r   re   r   r   r  rb   rS   r   r  r  r   rR   r  s                    rD   r   GeneralSumOfEvenPowers.solve  s+   z"

AxxxEHHEE  H1XI'Hq5EAIM7:;s!&&A-s;zz"~"%aA.A

)A,?,$!QDGAI,?@

1AID} /  < @s   #E
	Er   r   r   r   rF   rD   r  r  n  s    " (DErF   r  c                 `    [        U 6 nUS:X  a  U $ [        U  Vs/ s H  o3U-  PM	     sn5      $ ! [         aZ    [        [        S U 5      5      n[	        U5      S:  a  U s $ [        U Vs/ s H  o3R                  5       S   PM     Os  snf sn6 n N[         a    [        S5      ef = fs  snf )Nr   r   z-_remove_gcd(a,b,c) or _remove_gcd(*container)r   )r   r>   r   filterrH   as_content_primitiver   r?   )rS   r   fxrR   s       rD   r  r    s    I!H 	Av"1Q$"##  =&q/"r7Q;H;1))+A.;< IGHHI #s&   0 B+0B("	B(+B

B(B(c                D    [        [        U5      U -  [        U5      5      $ rW   )r  r   r  r  rP  s     rD   rM  rM    s    tAwqy#a&))rF   c                `    [        X5      u  p#[        U5      [        U5      S-  ::  a  U$ US-   $ )Nr   r   )r   r  )re   r   wr   s       rD   _nint_or_floorr    s/    !<DA
1vQq5LrF   c                    U S-  S:g  $ Nr   r   r   rR   s    rD   r  r        q5A:rF   c                    U S-  S:H  $ r  r   r  s    rD   _evenr    r  rF   r   Tr   NFc                  ^)^*^+ [        U 5      n [        U [        5      (       a  U R                  U R                  -
  n  [        U R                  SS9R                  5      m*T*R                  [        S9  U(       a  [        U5      (       d  [        S5      eU Vs/ s H  oDT*;   d  M
  UPM     nnUT*:w  ab  [        [        U[        [        U5      5      5      5      n[!        XUS9 VVs1 s H#  n[#        T* Vs/ s H	  oFXT      PM     sn5      iM%     snn$ U R%                  5       u  pxUR&                  (       a
  [)        5       $ UR&                  (       dW  [!        U5      n	[!        U5      U	-
  n
U
 Vs1 s H0  n[+        UR-                  [        T*U5      5      5      (       d  M.  UiM2     sn$ [/        U5      n U R&                  (       a   eU R0                  " T*SS06S   n [3        U 5      n[5        S	 UR6                   5       5      (       a   eUR9                  5       n U R;                  5       (       d   e SnSnSn [C        U 5      u  nnnU(       Ga  [        U5      n[D        RF                  [H        RF                  /n[J        RF                  [L        RF                  [N        RF                  /nUU;   a  SnGOtUU;   Gam  US:X  a  [        [Q        US5      5      nSnSnS U 5       nU H&  n UU   n[U        U5      =(       a    [U        U5      nM(     [        [Q        US5      5      nU H)  n UUS      n[U        U5      =(       a    [U        U5      nM+     [5        UU45      (       d  SnOU(       d  SnOUS:X  a  [        [Q        US5      5      nSnSnS U 5       nU H&  n UU   n[U        U5      =(       a    [U        U5      nM(     [        [Q        US5      5      nU H)  n UUS      n[U        U5      =(       a    [U        U5      nM+     [5        UU45      (       d  SnO	U(       d  SnUS:X  a  U S4/nO[        e[)        5       nU GH	  nUu  n n![C        U SS9u  m+n!n"[]        U SS9R_                  5       u  n!n [a        U U5      n#U"[b        RF                  [J        RF                  [L        RF                  [d        RF                  4;   a  URg                  [i        T*T+U#5      5        M  U"[N        RF                  [D        RF                  [H        RF                  [j        RF                  4;   a  URm                  U*U+4S jU# 5       5        M  [W        SU"-  5      e   URo                  S5        [#        S/[        T*5      -  5      n$U(       d\  U R-                  [        T*U$5      5      Rp                  (       a2  [s        S [        U$T*5       5       5      (       a  URg                  U$5        [)        5       n%U H  m)[s        S T) 5       5      (       a  U(       a'  [)        [u        T)5      5      n&U%Rm                  U&5        MH  U(       aK  [        [u        T)5      5      n'[        [w        U)4S jU'5      5      n'[)        U'5      n&U%Rm                  U&5        M  U(       a'  [)        [y        T)5      5      n(U%Rm                  U(5        M  U%Rg                  T)5        M  U%Rg                  T)5        M     U%$ s  snf s  snf s  snnf s  snf ! [<        [>        4 a    [        [A        S
5      5      ef = f! [R         a    Sn GN<f = f! [R         a    Sn GNf = f! [R         a    Sn GNf = f! [R         a    Sn GNzf = f! [        [V        4 aD    [Y        U 5      nUS   RZ                  (       a  US   S:w  a  [!        U US   -  XUS9s $ US   n GNf = f)a:  
Simplify the solution procedure of diophantine equation ``eq`` by
converting it into a product of terms which should equal zero.

Explanation
===========

For example, when solving, `x^2 - y^2 = 0` this is treated as
`(x + y)(x - y) = 0` and `x + y = 0` and `x - y = 0` are solved
independently and combined. Each term is solved by calling
``diop_solve()``. (Although it is possible to call ``diop_solve()``
directly, one must be careful to pass an equation in the correct
form and to interpret the output correctly; ``diophantine()`` is
the public-facing function to use in general.)

Output of ``diophantine()`` is a set of tuples. The elements of the
tuple are the solutions for each variable in the equation and
are arranged according to the alphabetic ordering of the variables.
e.g. For an equation with two variables, `a` and `b`, the first
element of the tuple is the solution for `a` and the second for `b`.

Usage
=====

``diophantine(eq, t, syms)``: Solve the diophantine
equation ``eq``.
``t`` is the optional parameter to be used by ``diop_solve()``.
``syms`` is an optional list of symbols which determines the
order of the elements in the returned tuple.

By default, only the base solution is returned. If ``permute`` is set to
True then permutations of the base solution and/or permutations of the
signs of the values will be returned when applicable.

Details
=======

``eq`` should be an expression which is assumed to be zero.
``t`` is the parameter to be used in the solution.

Examples
========

>>> from sympy import diophantine
>>> from sympy.abc import a, b
>>> eq = a**4 + b**4 - (2**4 + 3**4)
>>> diophantine(eq)
{(2, 3)}
>>> diophantine(eq, permute=True)
{(-3, -2), (-3, 2), (-2, -3), (-2, 3), (2, -3), (2, 3), (3, -2), (3, 2)}

>>> from sympy.abc import x, y, z
>>> diophantine(x**2 - y**2)
{(t_0, -t_0), (t_0, t_0)}

>>> diophantine(x*(2*x + 3*y - z))
{(0, n1, n2), (t_0, t_1, 2*t_0 + 3*t_1)}
>>> diophantine(x**2 + 3*x*y + 4*x)
{(0, n1), (-3*t_0 - 4, t_0)}

See Also
========

diop_solve
sympy.utilities.iterables.permute_signs
sympy.utilities.iterables.signed_permutations
Trw   ry   z/syms should be given as a sequence, e.g. a list)permuteas_AddFr   c              3  8   #    U  H  oR                   v   M     g 7frW   )	is_number)r}   r   s     rD   r   diophantine.<locals>.<genexpr>F  s     3Fq{{Fr   z@
    Equation should be a polynomial with Rational coefficients.r   r   c              3  6   #    U  H  oS    US   -  v   M     g7fr   r   Nr   )r}   r  s     rD   r   r  l       $@1qT!A$Y   r   c              3  6   #    U  H  oS    US   -  v   M     g7fr  r   )r}   rS   s     rD   r   r    r  r  r  )paramsymsr  _dict)evaluatec              3  >   >#    U  H  n[        TTU5      v   M     g 7frW   )merge_solution)r}   r   r   var_ts     rD   r   r    s     L8CsE3778s   zunhandled type: %sr   c              3  V   #    U  H  u  p[        U40 UR                  D6S Lv   M!     g7f)FN)r   assumptions0)r}   valr  s      rD   r   r    s(     bSa 77uDSas   ')c              3  8   #    U  H  n[        U5      v   M     g 7frW   r|   )r}   r  s     rD   r   r    s     *cz!}}cr   c                2   > U S   U S   -  TS   TS   -  :H  $ rn  r   )rS   r   s    rD   r   diophantine.<locals>.<lambda>  s"    AaD1IQA,FrF   )=r   r   r   lhsrhsr   r   r   r   r   r1   r   r\   r]   rJ   rH   r6   r?   as_numer_denomr  rI   r   rN   r   as_independentr*   rq  gensas_expris_polynomialr)   AssertionErrorr0   r7   r  ru   r  r^  r=  r   r2   KeyErrorboolr   r+   is_Rationalr,   r   r  r   r  rO   r  r   rY   discardis_zeror   r3   r  r4   ),r  r  r  r  rR   dict_sym_indexr   r  r  dsolgoodr  re   do_permute_signsdo_permute_signs_varpermute_few_signsrf   r~   len_varpermute_signs_forpermute_signs_checkvar_mulxy_coeffx_coeffvar1_mul_var2	v1_mul_v2r   v1termsflrv  r  baserT   eq_typerP   null
final_solnpermuted_signlstpermuted_sign_varr   r   r  s,                                            @@@rD   r6   r6     s   L 
"B"bVVbff_E29949(556%&t$$EG G#0t!CxAtD0s{!%c$c$i0@&A!B%0G%LN%L SAS!23SAB%LN N  ";;5L{{q>Dq>D(D#Et!xs3{0C'DAtEE!_<<2E215H3AFF33333YY[!!!!  X  #1a !fG#((&++!- ,00166$$#& %%'+$)) a<"71a=1G#H"G$@$@M &3	&$%iLE $(>#Ad5k &3 #71a=1G%&$%beHE #'w-"?DK & '233+/($,0)\"71a=1G#H"G$@$@M%2	&$%iLE $(>#Ad5k &3 #71a=1G%&$%beHE #'w-"?DK & '233 ,0($ -1)((!WIEO 5Da)$e<q'4%0==?4dE*+00166"''	) )
 HH^C9:$$#((&++	! !
 KKL8LL &&:W&DEE- 0 	LL!SXDBGGCTN+33bSVW[]`SabbbHHTNJ*c*** #M#$6 7!!-0"=-.6"FLM #C!!-0%$'(;C(@$A!!!"34s#s#! " U 1 B N F n- E
 $C D E 	EET  ( &$%E&  ( &$%E&"  ( &$%E&  ( &$%E&& *+ _a5A!r"Q%xuQQ1	s  A[5 	[ &[ ,<[5 ([*7[%[*[5 ,[5 .[5 1-[0"[0([5 +B[5 ;B/]) +\0;]) ,\04A-]) "]';]) #]+A
]) 6])  [5 %[**[5 5%\\-)]) ,\--]) 0] <]) ?]  ]) ]]) ]]) ]&"]) %]&&]) )A	^=4^=<^=c                <   / nSU;   a  g[        U5      n[        SSSS9nU  H>  nXQ;   a  UR                  [        U5      5        M$  UR                  [        U5      5        M@     [	        X05       H   u  pg[        U40 UR                  D6SL d  M     g   [        U5      $ )a  
This is used to construct the full solution from the solutions of sub
equations.

Explanation
===========

For example when solving the equation `(x - y)(x^2 + y^2 - z^2) = 0`,
solutions for each of the equations `x - y = 0` and `x^2 + y^2 - z^2` are
found independently. Solutions for `x - y = 0` are `(x, y) = (t, t)`. But
we should introduce a value for z when we output the solution for the
original equation. This function converts `(t, t)` into `(t, t, n_{1})`
where `n_{1}` is an integer parameter.
Nr   r  Tr   )r   startF)iterr.   r   nextr]   r   r  r?   )r   r  rP   r   r   rf   r  symbs           rD   r  r    s     CxH~Hc4q9F:JJtH~&JJtF|$	  ]	S6D$5$56%? # :rF   c                    [          H5  nU" U 5      R                  5       (       d  M   U" U 5      R                  US9s  $    g )Nr   )all_diop_classesr   r   )r  r   	diop_types      rD   _diop_solver&    s7    %	R=  ""R=&&&&99 &rF   c                   [        U SS9u  p#nU[        R                  :X  a  [        X5      $ U[        R                  :X  a  [        X5      $ U[        R                  :X  a
  [        U SS9$ U[        R                  :X  a
  [        U SS9$ U[        R                  :X  a  [        X5      $ U[        R                  :X  a  [        U 5      $ U[        R                  :X  a  [        U [         R"                  S9$ U[$        R                  :X  a  ['        U [         R"                  S9$ Ub  U[(        ;  a  [+        [-        S5      5      e[/        SU-  5      e)a  
Solves the diophantine equation ``eq``.

Explanation
===========

Unlike ``diophantine()``, factoring of ``eq`` is not attempted. Uses
``classify_diop()`` to determine the type of the equation and calls
the appropriate solver function.

Use of ``diophantine()`` is recommended over other helper functions.
``diop_solve()`` can return either a set or a tuple depending on the
nature of the equation. All non-trivial solutions are returned: assumptions
on symbols are ignored.

Usage
=====

``diop_solve(eq, t)``: Solve diophantine equation, ``eq`` using ``t``
as a parameter if needed.

Details
=======

``eq`` should be an expression which is assumed to be zero.
``t`` is a parameter to be used in the solution.

Examples
========

>>> from sympy.solvers.diophantine import diop_solve
>>> from sympy.abc import x, y, z, w
>>> diop_solve(2*x + 3*y - 5)
(3*t_0 - 5, 5 - 2*t_0)
>>> diop_solve(4*x + 3*y - 4*z + 5)
(t_0, 8*t_0 + 4*t_1 + 5, 7*t_0 + 3*t_1 + 5)
>>> diop_solve(x + 3*y - 4*z + w - 6)
(t_0, t_0 + t_1, 6*t_0 + 5*t_1 + 4*t_2 - 6, 5*t_0 + 4*t_1 + 3*t_2 - 6)
>>> diop_solve(x**2 + y**2 - 5)
{(-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1)}


See Also
========

diophantine()
Fr  T)parameterizer   z
    Although this type of equation was identified, it is not yet
    handled. It should, however, be listed in `diop_known` at the
    top of this file. Developers should see comments at the end of
    `classify_diop`.
            r   )r7   r   ru   diop_linearr   diop_quadraticr^  diop_ternary_quadraticr=  diop_ternary_quadratic_normalr  diop_general_pythagoreanr   diop_univariater  diop_general_sum_of_squaresr   Infinityr  diop_general_sum_of_even_powers
diop_knownr>   r0   r   )r  r  r   r   r  s        rD   r  r    s,   ` (%8C&++2%%	O((	(b((	/44	4%bt<<	5::	:,RdCC	&++	+'22	JOO	#r""	',,	,*2QZZ@@	*//	/.rDDwj8Z )    "07:< 	<rF   c                2   SnS n[          H$  nU" U 5      nUR                  5       (       d  M"  Sn  O   U(       aK  UR                  U(       a!  [        UR                  5      UR
                  4$ UR                  UR
                  4$ [        [        S5      5      e)NFTz
        This equation is not yet recognized or else has not been
        simplified sufficiently to put it in a form recognized by
        diop_classify().)r$  r   r   r\   r   ru   r   r0   )r  r  matchedr%  
diop_classs        rD   r7   r7   b  s     GI&
rN	G	 ' %%tIOO'<\e\j\jjj9??\e\j\jjj j *   rF   a  
    Helper routine used by diop_solve() to find information about ``eq``.

    Explanation
    ===========

    Returns a tuple containing the type of the diophantine equation
    along with the variables (free symbols) and their coefficients.
    Variables are returned as a list and coefficients are returned
    as a dict with the key being the respective term and the constant
    term is keyed to 1. The type is one of the following:

    * %s

    Usage
    =====

    ``classify_diop(eq)``: Return variables, coefficients and type of the
    ``eq``.

    Details
    =======

    ``eq`` should be an expression which is assumed to be zero.
    ``_dict`` is for internal use: when True (default) a dict is returned,
    otherwise a defaultdict which supplies 0 for missing keys is returned.

    Examples
    ========

    >>> from sympy.solvers.diophantine import classify_diop
    >>> from sympy.abc import x, y, z, w, t
    >>> classify_diop(4*x + 6*y - 4)
    ([x, y], {1: -4, x: 4, y: 6}, 'linear')
    >>> classify_diop(x + 3*y -4*z + 5)
    ([x, y, z], {1: 5, x: 1, y: 3, z: -4}, 'linear')
    >>> classify_diop(x**2 + y**2 - x*y + x + 5)
    ([x, y], {1: 5, x: 1, x**2: 1, y**2: 1, x*y: -1}, 'binary_quadratic')
    z
    * c                l   [        U SS9u  p#nU[        R                  :X  a  SnUb  [        SU[	        U5      4-  SS9n[        U 5      R                  US9nUc  U" S/[	        UR                  5      -  6 n[	        U5      S:  a  [        U5      S   $ [        S/[	        UR                  5      -  5      $ g)	a  
Solves linear diophantine equations.

A linear diophantine equation is an equation of the form `a_{1}x_{1} +
a_{2}x_{2} + .. + a_{n}x_{n} = 0` where `a_{1}, a_{2}, ..a_{n}` are
integer constants and `x_{1}, x_{2}, ..x_{n}` are integer variables.

Usage
=====

``diop_linear(eq)``: Returns a tuple containing solutions to the
diophantine equation ``eq``. Values in the tuple is arranged in the same
order as the sorted variables.

Details
=======

``eq`` is a linear diophantine equation which is assumed to be zero.
``param`` is the parameter to be used in the solution.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import diop_linear
>>> from sympy.abc import x, y, z
>>> diop_linear(2*x - 3*y - 5) # solves equation 2*x - 3*y - 5 == 0
(3*t_0 - 5, 2*t_0 - 5)

Here x = -3*t_0 - 5 and y = -2*t_0 - 5

>>> diop_linear(2*x - 3*y - 4*z -3)
(t_0, 2*t_0 + 4*t_1 + 3, -t_0 - 3*t_1 - 3)

See Also
========

diop_quadratic(), diop_ternary_quadratic(), diop_general_pythagorean(),
diop_general_sum_of_squares()
Fr  Nz%s_0:%iTr   r   r   )	r7   r   ru   r   rH   r   r@   r   r?   )r  r  r   r   r%  r@   rb   s          rD   r*  r*    s    P *"E:C	FKK
 eSX->!>MJ!!Z!8=aSV%6%6!778Fv;?<?"$F$5$5 6677  rF   c                4   [        XU 5      u  pn U S:X  a  Uc  gUS:  a  U* nX#-  U* U-  4$ [        [        U5      [        U5      5      u  pEnU[        U5      -  nU[        U5      -  nX-  (       a  gUc  X-  X-  4$ US:  a  U* nX-  X#-  -   X-  X-  -
  4$ )a,  
Return the base solution for the linear equation, `ax + by = c`.

Explanation
===========

Used by ``diop_linear()`` to find the base solution of a linear
Diophantine equation. If ``t`` is given then the parametrized solution is
returned.

Usage
=====

``base_solution_linear(c, a, b, t)``: ``a``, ``b``, ``c`` are coefficients
in `ax + by = c` and ``t`` is the parameter to be used in the solution.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import base_solution_linear
>>> from sympy.abc import t
>>> base_solution_linear(5, 2, 3) # equation 2*x + 3*y = 5
(-5, 5)
>>> base_solution_linear(0, 5, 7) # equation 5*x + 7*y = 0
(0, 0)
>>> base_solution_linear(5, 2, 3, t) # equation 2*x + 3*y = 5
(3*t - 5, 5 - 2*t)
>>> base_solution_linear(0, 5, 7, t) # equation 5*x + 7*y = 0
(7*t, -5*t)
r   r   r   r   )r  r   r  r   )r~   r  rP  r   r  r  r  s          rD   r   r     s    > !"GA!Av9q5AaRT{s1vs1v&IBA$q'MB$q'MBuyad|1uBD13Jqs
##rF   c                    [        U SS9u  pnU[        R                  :X  aF  [        XS   5      R	                  [
        R                  5       Vs1 s H  n[        U5      4iM     sn$ gs  snf )a  
Solves a univariate diophantine equations.

Explanation
===========

A univariate diophantine equation is an equation of the form
`a_{0} + a_{1}x + a_{2}x^2 + .. + a_{n}x^n = 0` where `a_{1}, a_{2}, ..a_{n}` are
integer constants and `x` is an integer variable.

Usage
=====

``diop_univariate(eq)``: Returns a set containing solutions to the
diophantine equation ``eq``.

Details
=======

``eq`` is a univariate diophantine equation which is assumed to be zero.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import diop_univariate
>>> from sympy.abc import x
>>> diop_univariate((x - 2)*(x - 3)**2) # solves equation (x - 2)*(x - 3)**2 == 0
{(2,), (3,)}

Fr  r   N)r7   r   ru   r-   r   r   r   rL   )r  r   r   r%  rR   s        rD   r/  r/    sq    > *"E:C	JOO##0A$!	!**-$. / $.aQ	 $. / 	/ $/s   A)c                    X-  (       + $ )zF
Returns `True` if ``a`` is divisible by ``b`` and `False` otherwise.
r   r  s     rD   r  r  <  s     u9rF   c                    [        U SS9u  p#nU[        R                  :X  a3  Ub  U[        SSS9/nOSn[	        [        U 5      R                  US95      $ g)a  
Solves quadratic diophantine equations.

i.e. equations of the form `Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0`. Returns a
set containing the tuples `(x, y)` which contains the solutions. If there
are no solutions then `(None, None)` is returned.

Usage
=====

``diop_quadratic(eq, param)``: ``eq`` is a quadratic binary diophantine
equation. ``param`` is used to indicate the parameter to be used in the
solution.

Details
=======

``eq`` should be an expression which is assumed to be zero.
``param`` is a parameter to be used in the solution.

Examples
========

>>> from sympy.abc import x, y, t
>>> from sympy.solvers.diophantine.diophantine import diop_quadratic
>>> diop_quadratic(x**2 + y**2 + 2*x + 2*y + 2, t)
{(-1, -1)}

References
==========

.. [1] Methods to solve Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0, [online],
      Available: https://www.alpertron.com.ar/METHODS.HTM
.. [2] Solving the equation ax^2+ bxy + cy^2 + dx + ey + f= 0, [online],
      Available: https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf

See Also
========

diop_linear(), diop_ternary_quadratic(), diop_general_sum_of_squares(),
diop_general_pythagorean()
Fr  Nr   Tr   r   )r7   r   ru   r   rI   r   )r  r  r   r   r%  r@   s         rD   r+  r+  C  sc    V *"E:C	O(((T!:;JJ?2&,,
,CDD )rF   c           
         [        [        XU45      5      n[        UR                  5        VVs/ s H  u  pVXeR	                  U5      -  PM     snn6 n[        U5      S:H  $ s  snnf )z
Check whether `(u, v)` is solution to the quadratic binary diophantine
equation with the variable list ``var`` and coefficient dictionary
``coeff``.

Not intended for use by normal users.
r   )r\   r]   r   itemsxreplacer   )r   r   r   rf   repsrR   r  r  s           rD   r	  r	  x  sX     CQ !D	ekkm<mdaqD!!m<	=BB<1 =s   A!
c                   U S:  a  US:X  a  S/$ US:  a  / $ / n[        [        U5      SS9 Hg  n[        S[        U * 5      [        XS-  -  5      5       H;  u  pVUR	                  XE-  XF-  45        U S:X  d  M$  UR	                  XF-  XE-  45        M=     Mi     U$ U S:X  a.  US:  a  / $ US:X  a  SU4/$ [        US5      u  pxU(       a  Xr4/$ / $ [        U S5      u  pU(       aw  US:X  a  X-  U4/$ / n[        [        [        U5      US-
  -  SU	-  -  5      S-   5       H6  n [        XS-  -  U-   S5      u  pU(       d  M#  UR	                  W
U45        M8     U$ SUS-  s=:  a  U :  a  O  O[        X5      $ US:X  a  S/$ / n[        U5      S:X  a  [        SSU 5      n[        U5      Gt pn[        U5       H  u  nGt nnnnnUSU	-  :X  a    OUUpM     WS-  (       a  US:X  a  UR	                  X45        U$ US:X  a  X4/$ [        U5       H  n[        U5      Gt nnnM     WW4/$ [        [        U5      SS9 GH  nUUS-  -  n[        U5      n[        U USS	9 H  n[!        UU5      n[        UUU 5      n[        U5      Gt pn[        [#        UUU 5      S-
  5       H  n[        U5      Gtnnnnn[        U5      S:X  a  US-  XS-  -  -
  U:X  a  UR	                  UU-  UU-  45        OX[%        U S5      =n(       aE  UR	                  UUUS   S   -  X-  US   S   -  -   -  UUUS   S   -  UUS   S   -  -   -  45          M  UUpM     M     GM     U$ ! [         a    Sn GN4f = f)
a  
Solves the equation `x^2 - Dy^2 = N`.

Explanation
===========

Mainly concerned with the case `D > 0, D` is not a perfect square,
which is the same as the generalized Pell equation. The LMM
algorithm [1]_ is used to solve this equation.

Returns one solution tuple, (`x, y)` for each class of the solutions.
Other solutions of the class can be constructed according to the
values of ``D`` and ``N``.

Usage
=====

``diop_DN(D, N, t)``: D and N are integers as in `x^2 - Dy^2 = N` and
``t`` is the parameter to be used in the solutions.

Details
=======

``D`` and ``N`` correspond to D and N in the equation.
``t`` is the parameter to be used in the solutions.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import diop_DN
>>> diop_DN(13, -4) # Solves equation x**2 - 13*y**2 = -4
[(3, 1), (393, 109), (36, 10)]

The output can be interpreted as follows: There are three fundamental
solutions to the equation `x^2 - 13y^2 = -4` given by (3, 1), (393, 109)
and (36, 10). Each tuple is in the form (x, y), i.e. solution (3, 1) means
that `x = 3` and `y = 1`.

>>> diop_DN(986, 1) # Solves equation x**2 - 986*y**2 = 1
[(49299, 1570)]

See Also
========

find_DN(), diop_bf_DN()

References
==========

.. [1] Solving the generalized Pell equation x**2 - D*y**2 = N, John P.
    Robertson, July 31, 2004, Pages 16 - 17. [online], Available:
    https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf
r   r9  T	generatorr   r   r   F)	all_roots)r    square_factor
cornacchiarL   r   r   rJ   r   r   r>   _special_diop_DNr  PQar!  r  r'   r&   lengthr  )r   r(  r   r   r  rS   r  sN_exactsDsqpqarT   prev_Bprev_Gr  r  _B_Gfr  amsqmr   r   s                            rD   r  r    s   l 	1u68Oq5I-*d;A"1c1"gs119~>

AC:&7JJQSz* ? <
 
Avq5I6F8O$Q*
G9	 !A&JB6T1I;uT!Wa!e_ad34q89A,Q!tVaZ;
 v

B7# : 
1a4|!|%%Avx
C
1v{!Ql!#YF%.s^!A!Aq"bAbDyF &4 q5Av

F+,J7$%%qAcJQB RzmA&$7AIVArT2C!#r*AaQ-C!%cQ6!R+a/0#'9 1q"bq6Q;qy1QY;.!3

AfHah#78%an,,

Avad1g~1a8H'H$I$%vad1g~qtAw'F$G$I J!#R 1	 3 8" Ja  s   "MM-,M-c                   [        U 5      n[        [        [        U5      5      SS9 Vs0 s H  o1US-  -  U_M     nnSnSnSu  pxSu  p/ n [	        S5       HZ  nXR-   U-  nX-  U-
  nXS-  -
  U-  nXU-  U-   pXU
-  U	-   pUS-  X
S-  -  -
  =o;   d  M?  XN   nUR                  X8-  X:-  45        M\     US:X  a   U$ Mt  s  snf )a  
Solves the equation `x^2 - Dy^2 = N` for the special case where
`1 < N**2 < D` and `D` is not a perfect square.
It is better to call `diop_DN` rather than this function, as
the former checks the condition `1 < N**2 < D`, and calls the latter only
if appropriate.

Usage
=====

WARNING: Internal method. Do not call directly!

``_special_diop_DN(D, N)``: D and N are integers as in `x^2 - Dy^2 = N`.

Details
=======

``D`` and ``N`` correspond to D and N in the equation.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import _special_diop_DN
>>> _special_diop_DN(13, -3) # Solves equation x**2 - 13*y**2 = -3
[(7, 2), (137, 38)]

The output can be interpreted as follows: There are two fundamental
solutions to the equation `x^2 - 13y^2 = -3` given by (7, 2) and
(137, 38). Each tuple is in the form (x, y), i.e. solution (7, 2) means
that `x = 7` and `y = 2`.

>>> _special_diop_DN(2445, -20) # Solves equation x**2 - 2445*y**2 = -20
[(445, 9), (17625560, 356454), (698095554475, 14118073569)]

See Also
========

diop_DN()

References
==========

.. [1] Section 4.4.4 of the following book:
    Quadratic Diophantine Equations, T. Andreescu and D. Andrica,
    Springer, 2015.
TrB  r   r   r   )r   r   )r   r   )r   r    rE  r  rJ   r   )r   r(  sqrt_DrS  r   r&  r'  G0G1B0B1rX   rT   r  r  s                  rD   rG  rG    s    l 1XF'c!f(=NON!adANAO	A	AFBFBI
qA!AaATaA22UQ1uW_$*D  !$.  6  	Ps   Cr  c                   [        5       nX-   U:  a}  U S:X  a:  [        X -  S5      u  pEU(       a  UR                  [        U5      S45        X:X  a  U$ X!-  S:X  a3  [        X!-  S5      u  pEU(       a  UR                  S[        U5      45        U$ [	        U* [        X5      -  U5       H  nXbS-  :  a  M  X&pX US-  -  -
  =n	S::  a  XU-  pX US-  -  -
  =n	S::  a  M  [        X5      u  pU
(       a  ML  [        U	S5      u  pEU(       d  Mc  X:X  a  X:  a  XHpHUR                  [        U5      [        U5      45        M     U$ )a  
Solves `ax^2 + by^2 = m` where `\gcd(a, b) = 1 = gcd(a, m)` and `a, b > 0`.

Explanation
===========

Uses the algorithm due to Cornacchia. The method only finds primitive
solutions, i.e. ones with `\gcd(x, y) = 1`. So this method cannot be used to
find the solutions of `x^2 + y^2 = 20` since the only solution to former is
`(x, y) = (4, 2)` and it is not primitive. When `a = b`, only the
solutions with `x \leq y` are found. For more details, see the References.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import cornacchia
>>> cornacchia(2, 3, 35) # equation 2x**2 + 3y**2 = 35
{(2, 3), (4, 1)}
>>> cornacchia(1, 1, 25) # equation x**2 + y**2 = 25
{(4, 3)}

References
===========

.. [1] A. Nitaj, "L'algorithme de Cornacchia"
.. [2] Solving the diophantine equation ax**2 + by**2 = m by Cornacchia's
    method, [online], Available:
    http://www.numbertheory.org/php/cornacchia.html

See Also
========

sympy.utilities.iterables.signed_permutations
r   r   r   )rI   r   rO   rL   r(   r   r   )r  rP  r  rv  r  rK  r   r   r   m1_rs              rD   rF  rF  ]  s:   H 5Duqy6afa(IA#a&!%v5A:afa(IA!SV% A2fQl?A.Av:11a4ZrA%!eq 1a4ZrA%"aL	6v!%1HHc!fc!f%& / KrF   c              #     #    [        U5      nS=pES=pgUnU * n	U n
Un X-   U-  nX-  U-   UpFX-  U-   UpuX-  U	-   UpXXXX4v   X-  U
-
  n
X*S-  -
  U-  nM9  7f)ai  
Returns useful information needed to solve the Pell equation.

Explanation
===========

There are six sequences of integers defined related to the continued
fraction representation of `\\frac{P + \sqrt{D}}{Q}`, namely {`P_{i}`},
{`Q_{i}`}, {`a_{i}`},{`A_{i}`}, {`B_{i}`}, {`G_{i}`}. ``PQa()`` Returns
these values as a 6-tuple in the same order as mentioned above. Refer [1]_
for more detailed information.

Usage
=====

``PQa(P_0, Q_0, D)``: ``P_0``, ``Q_0`` and ``D`` are integers corresponding
to `P_{0}`, `Q_{0}` and `D` in the continued fraction
`\\frac{P_{0} + \sqrt{D}}{Q_{0}}`.
Also it's assumed that `P_{0}^2 == D mod(|Q_{0}|)` and `D` is square free.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import PQa
>>> pqa = PQa(13, 4, 5) # (13 + sqrt(5))/4
>>> next(pqa) # (P_0, Q_0, a_0, A_0, B_0, G_0)
(13, 4, 3, 3, 1, -1)
>>> next(pqa) # (P_1, Q_1, a_1, A_1, B_1, G_1)
(-1, 1, 1, 4, 1, 3)

References
==========

.. [1] Solving the generalized Pell equation x^2 - Dy^2 = N, John P.
    Robertson, July 31, 2004, Pages 4 - 8. https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf
r   r   r   )r   )P_0Q_0r   sqDA2r[  A1B2rY  G2P_iQ_ia_is                rD   rH  rH    s     J (CKBKB	B
B
C
C
yS "bB"bB"bB''gm6zc! s   AAc           	        [        U 5      n [        U5      n/ n[        U S5      nUS   S   nUS:X  a:  U S:  a  S/$ U S:X  a  SU4/$ [        U S5      u  pgU(       a  Xb-  U4U* U-  U4/$ S/$ [        U5      S:X  a  [        X5      $ US:  a)  Sn[        [	        XS-
  -  SU -  -  5      S5      S   S-   n	OM[        [	        X-  5      * S5      u  pU(       d  US-  n[        [	        XS-   -  SU -  -  5      * S5      S   S-   n	[        X5       H`  n
 [        XU
S-  -  -   S5      u  pU(       d  M#  UR                  WU
45        [        XU* XU5      (       a  ML  UR                  U* U
45        Mb     U$ ! [         a    Sn NZf = f)aC  
Uses brute force to solve the equation, `x^2 - Dy^2 = N`.

Explanation
===========

Mainly concerned with the generalized Pell equation which is the case when
`D > 0, D` is not a perfect square. For more information on the case refer
[1]_. Let `(t, u)` be the minimal positive solution of the equation
`x^2 - Dy^2 = 1`. Then this method requires
`\sqrt{\\frac{\mid N \mid (t \pm 1)}{2D}}` to be small.

Usage
=====

``diop_bf_DN(D, N, t)``: ``D`` and ``N`` are coefficients in
`x^2 - Dy^2 = N` and ``t`` is the parameter to be used in the solutions.

Details
=======

``D`` and ``N`` correspond to D and N in the equation.
``t`` is the parameter to be used in the solutions.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import diop_bf_DN
>>> diop_bf_DN(13, -4)
[(3, 1), (-3, 1), (36, 10)]
>>> diop_bf_DN(986, 1)
[(49299, 1570)]

See Also
========

diop_DN()

References
==========

.. [1] Solving the generalized Pell equation x**2 - D*y**2 = N, John P.
    Robertson, July 31, 2004, Page 15. https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf
r   r   r9  r   F)	r/   r  r   r  rL   rJ   r>   r   
equivalent)r   r(  r   r   r  r   rL  rK  L1L2r  rS   s               rD   
diop_bf_DNrn    s   Z 	q	Aq	A
C1A	!QAAvq58O6F8O$Q*
T1IAqz**x
1v{q}1uSEAaC115a81<$c!#hY2
!GBc!U)QqS/22A6q9A=2]	'adF
A6IA 6JJ1vaQBa00

QB7#  J  	F	s   E44FFc                b    [        X-  XA-  U-  -
  U5      =(       a    [        X-  X-  -
  U5      $ )a  
Returns True if two solutions `(u, v)` and `(r, s)` of `x^2 - Dy^2 = N`
belongs to the same equivalence class and False otherwise.

Explanation
===========

Two solutions `(u, v)` and `(r, s)` to the above equation fall to the same
equivalence class iff both `(ur - Dvs)` and `(us - vr)` are divisible by
`N`. See reference [1]_. No test is performed to test whether `(u, v)` and
`(r, s)` are actually solutions to the equation. User should take care of
this.

Usage
=====

``equivalent(u, v, r, s, D, N)``: `(u, v)` and `(r, s)` are two solutions
of the equation `x^2 - Dy^2 = N` and all parameters involved are integers.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import equivalent
>>> equivalent(18, 5, -18, -5, 13, -1)
True
>>> equivalent(3, 1, -18, 393, 109, -4)
False

References
==========

.. [1] Solving the generalized Pell equation x**2 - D*y**2 = N, John P.
    Robertson, July 31, 2004, Page 12. https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf

)r  )r   rf   r   r  r   r(  s         rD   rk  rk  5	  s2    H QS13q5[!$@139a)@@rF   c                    SSK Jn  U" XU5      n[        US   [        5      (       a   [	        US   5      n[	        U5      S-
  nXV-   $ Sn[	        U5      nXV-   $ )a  
Returns the (length of aperiodic part + length of periodic part) of
continued fraction representation of `\\frac{P + \sqrt{D}}{Q}`.

It is important to remember that this does NOT return the length of the
periodic part but the sum of the lengths of the two parts as mentioned
above.

Usage
=====

``length(P, Q, D)``: ``P``, ``Q`` and ``D`` are integers corresponding to
the continued fraction `\\frac{P + \sqrt{D}}{Q}`.

Details
=======

``P``, ``D`` and ``Q`` corresponds to P, D and Q in the continued fraction,
`\\frac{P + \sqrt{D}}{Q}`.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import length
>>> length(-2, 4, 5) # (-2 + sqrt(5))/4
3
>>> length(-5, 4, 17) # (-5 + sqrt(17))/4
4

See Also
========
sympy.ntheory.continued_fraction.continued_fraction_periodic
r   )continued_fraction_periodicr   r   ) sympy.ntheory.continued_fractionrq  r   r   rH   )r&  r'  r   rq  rf   rptnonrpts          rD   rI  rI  \	  sb    D M#A!,A!B%!B%jQ! < Q<rF   c                \    [        U SS9u  pnU[        R                  :X  a  [        X5      $ g)ac  
This function transforms general quadratic,
`ax^2 + bxy + cy^2 + dx + ey + f = 0`
to more easy to deal with `X^2 - DY^2 = N` form.

Explanation
===========

This is used to solve the general quadratic equation by transforming it to
the latter form. Refer to [1]_ for more detailed information on the
transformation. This function returns a tuple (A, B) where A is a 2 X 2
matrix and B is a 2 X 1 matrix such that,

Transpose([x y]) =  A * Transpose([X Y]) + B

Usage
=====

``transformation_to_DN(eq)``: where ``eq`` is the quadratic to be
transformed.

Examples
========

>>> from sympy.abc import x, y
>>> from sympy.solvers.diophantine.diophantine import transformation_to_DN
>>> A, B = transformation_to_DN(x**2 - 3*x*y - y**2 - 2*y + 1)
>>> A
Matrix([
[1/26, 3/26],
[   0, 1/13]])
>>> B
Matrix([
[-6/13],
[-4/13]])

A, B  returned are such that Transpose((x y)) =  A * Transpose((X Y)) + B.
Substituting these values for `x` and `y` and a bit of simplifying work
will give an equation of the form `x^2 - Dy^2 = N`.

>>> from sympy.abc import X, Y
>>> from sympy import Matrix, simplify
>>> u = (A*Matrix([X, Y]) + B)[0] # Transformation for x
>>> u
X/26 + 3*Y/26 - 6/13
>>> v = (A*Matrix([X, Y]) + B)[1] # Transformation for y
>>> v
Y/13 - 4/13

Next we will substitute these formulas for `x` and `y` and do
``simplify()``.

>>> eq = simplify((x**2 - 3*x*y - y**2 - 2*y + 1).subs(zip((x, y), (u, v))))
>>> eq
X**2/676 - Y**2/52 + 17/13

By multiplying the denominator appropriately, we can get a Pell equation
in the standard form.

>>> eq * 676
X**2 - 13*Y**2 + 884

If only the final equation is needed, ``find_DN()`` can be used.

See Also
========

find_DN()

References
==========

.. [1] Solving the equation ax^2 + bxy + cy^2 + dx + ey + f = 0,
       John P.Robertson, May 8, 2003, Page 7 - 11.
       https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf
Fr  N)r7   r   ru   r  r  r   r   r%  s       rD   transformation_to_DNrw  	  s4    \ *"E:C	O((($S00 )rF   c                   U u  p#XS-     nXU-     nXS-     nX   nX   nUS   n	[        XEXgX5       V
s/ s H  n
[        U
5      PM     sn
u  pEpgp[        SSS9u  pU(       a  [        SU-  U5      u  p[        XMS-  5      u  nnUS-  X-  X-  SUS-  XU-  XS-  -  -
  -  XU-  XU-  U-  UU-  U-  -
  SU	U-  U-  0n[	        X/U5      u  nn[        SS[        R                  U-  [        U5      * U-  SS/5      U-  [        SS[        R                  U-  [        U5      * U-  SS/5      U-  4$ U(       a  [        SU-  U5      u  p[        XMS-  5      u  nnUS-  XU-  SUS-  UU-  USXU-  SU	U-  XS-  -  -
  0n[	        X/U5      u  nn[        SS[        R                  U-  SSS/5      U-  [        SS[        R                  U-  SSS/5      U-  [        [        U5      * U-  S/5      -   4$ U(       a  [        SU-  U5      u  p[        XmS-  5      u  nnUS-  UU-  X-  SUS-  XSUSSU	U-  XS-  -  -
  0n[	        X/U5      u  nn[        SSSSS[        R                  U-  /5      U-  [        SSSSS[        R                  U-  /5      U-  [        S[        U5      * U-  /5      -   4$ [        SS[        R                  U-  SSS/5      [        SS/5      4$ s  sn
f )Nr   r   X, YTr   r   )r  r/   r   rM  r  r  r   r   )r   r   rS   r  r  rP  r~   r  r   rS  rR   r   r   r   r  r   r*  A_0B_0s                      rD   r  r  	  sq   DAdAc
AdAAAaA+6qQ1+HI+Haq	+HIA!64(DAAaC#A!t$1 AqsACAqD!qS1T6\*:AsAs1uqQRsSTu}VWYZ[\Y\]^Y^_(!7SaQUU1WqteAgq!45c96!QqSTUVSWRWXYRY[\^_H`;abe;eeeAaC#A!t$1 AqA#q!Q$!Q1c1acAdFlK(!7SaQUU1WaA./3VAq1557AqRSBT5UVY5Y\befgheidijkdkmnco\p5pppAaC#A!t$1 AqsACAqD!1aAaC!qD&LI(!7SaQ1aeeAg./3VAq1aAEERSGBT5UVY5Y\bdehijkhlglmngnco\p5ppp !Qq!Q*+VQF^;;C Js   K*c                \    [        U SS9u  pnU[        R                  :X  a  [        X5      $ g)a  
This function returns a tuple, `(D, N)` of the simplified form,
`x^2 - Dy^2 = N`, corresponding to the general quadratic,
`ax^2 + bxy + cy^2 + dx + ey + f = 0`.

Solving the general quadratic is then equivalent to solving the equation
`X^2 - DY^2 = N` and transforming the solutions by using the transformation
matrices returned by ``transformation_to_DN()``.

Usage
=====

``find_DN(eq)``: where ``eq`` is the quadratic to be transformed.

Examples
========

>>> from sympy.abc import x, y
>>> from sympy.solvers.diophantine.diophantine import find_DN
>>> find_DN(x**2 - 3*x*y - y**2 - 2*y + 1)
(13, -884)

Interpretation of the output is that we get `X^2 -13Y^2 = -884` after
transforming `x^2 - 3xy - y^2 - 2y + 1` using the transformation returned
by ``transformation_to_DN()``.

See Also
========

transformation_to_DN()

References
==========

.. [1] Solving the equation ax^2 + bxy + cy^2 + dx + ey + f = 0,
       John P.Robertson, May 8, 2003, Page 7 - 11.
       https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf
Fr  N)r7   r   ru   r  rv  s       rD   find_DNr}  
  s4    N *"E:C	O(((## )rF   c           	        U u  p#[        SSS9u  pE[        X5      u  pgU[        XE/5      -  U-   S   nU[        XE/5      -  U-   S   n	US-  XS-     -  X#-  XU-     -  -   US-  XS-     -  -   X!U   -  -   X1U   -  -   US   -   n
[        U
R	                  [        X#4X45      5      5      nUR                  5       nXS-     * XS-     -  US   * XS-     -  4$ )Nry  Tr   r   r   r   )r   r  r  r   rN   r]   r   )r   r   rS   r  r   r   r   r   r   rf   r  
simplifieds               rD   r  r  7
  s   DA64(DA ,DA	
61&>	A	q!A	
61&>	A	q!A	
AeqDk	ACc
N	*QT%1+-=	=(
	JQUVxZ	WZ_`aZb	bB"''#qfqf"567J++-EQ$K<d#eAhYuT{%:::rF   c                   SSK Jn  U R                  (       a  U R                  (       d  [	        X/US9$ UR                  (       a  UR                  (       d  [	        X/US9$ [        SSS9u  pVXP-  Xa-  -   R                  5       u  pxX'R                  -  (       a  [	        X/US9$ U" X5      S   U" X5      S   -
  n	U	R                  5       u  p[        XS9$ )	z
If there is a number modulo ``a`` such that ``x`` and ``y`` are both
integers, then return a parametric representation for ``x`` and ``y``
else return (None, None).

Here ``x`` and ``y`` are functions of ``t``.
r   )clear_coefficientsr   zm, nTr   r   )r   )	sympy.simplify.simplifyr  r  r   r9   r   r  r   r&  )rS   r  r  r   r  r  r  r~   re   r  junks              rD   r  r  H
  s     ;{{1<<%qf@@{{1<<%qf@@64(DAC!#I++-DA33w%qf@@ 
A	!!	$'9!'?'B	BB&&(HDr))rF   c                    [        U SS9u  p#nU[        R                  [        R                  4;   aJ  [	        X#5      n[        U5      S:  a  [        U5      S   u  pgnOSu  pgnU(       a  [        XgU4X#5      $ XgU4$ g)aT  
Solves the general quadratic ternary form,
`ax^2 + by^2 + cz^2 + fxy + gyz + hxz = 0`.

Returns a tuple `(x, y, z)` which is a base solution for the above
equation. If there are no solutions, `(None, None, None)` is returned.

Usage
=====

``diop_ternary_quadratic(eq)``: Return a tuple containing a basic solution
to ``eq``.

Details
=======

``eq`` should be an homogeneous expression of degree two in three variables
and it is assumed to be zero.

Examples
========

>>> from sympy.abc import x, y, z
>>> from sympy.solvers.diophantine.diophantine import diop_ternary_quadratic
>>> diop_ternary_quadratic(x**2 + 3*y**2 - z**2)
(1, 0, 1)
>>> diop_ternary_quadratic(4*x**2 + 5*y**2 - z**2)
(1, 0, 2)
>>> diop_ternary_quadratic(45*x**2 - 7*y**2 - 8*x*y - z**2)
(28, 45, 105)
>>> diop_ternary_quadratic(x**2 - 49*y**2 - z**2 + 13*z*y -8*x*y)
(9, 1, 5)
Fr  r   rf  N)r7   r^  ru   r=  rs  rH   r   _parametrize_ternary_quadratic	r  r(  r   r   r%  r   r$  r%  rZ  s	            rD   r,  r,  d
  s    D *"E:C	',,-224 4 &c1s8a< IaLMCc,MCc13- -}4rF   c                  ^ [        U4S jT 5       5      n[        U5      R                  5       (       a  [        X S9R                  5       $ [	        U5      R                  5       (       a  [	        X S9R                  5       $ g )Nc              3  2   >#    U  H  oTU   -  v   M     g 7frW   r   rj  s     rD   r   *_diop_ternary_quadratic.<locals>.<genexpr>
  s     'AuQxZrE  r   )r  r^  r   r   r=  )ru  r   r  s    ` rD   rs  rs  
  sh    	''	'B"2&..00*2AGGII	*2	.	6	6	8	80GMMOO 
9rF   c                @    [        U SS9u  pnUS;   a  [        X5      $ g)a$  
Returns the transformation Matrix that converts a general ternary
quadratic equation ``eq`` (`ax^2 + by^2 + cz^2 + dxy + eyz + fxz`)
to a form without cross terms: `ax^2 + by^2 + cz^2 = 0`. This is
not used in solving ternary quadratics; it is only implemented for
the sake of completeness.
Fr  r_  r>  N)r7   _transformation_to_normalrv  s       rD   transformation_to_normalr  
  s4     *"E:C	 4 4 )444rF   c                  ^ [        U 5      nU u  p4n[        U4S jU  5       5      (       dk  TX4-     nTXE-     nTX5-     nSn	U(       d  Sn	Xvpv[        SSU* U-  4SSU* U-  4S45      n
U	(       a$  U
R                  SS5        U
R	                  SS5        U
$ TUS-     S:X  a  TUS-     S:X  aC  U S   U S   sUS'   US'   [        UT5      n
U
R                  SS5        U
R	                  SS5        U
$ U S   U S   sUS'   US'   [        UT5      n
U
R                  SS5        U
R	                  SS5        U
$ TX4-     S:w  d  TX5-     S:w  a  TUS-     nTX4-     nTX5-     nTUS-     nTXE-     nTUS-     n0 nS	US-  -  UUS-  '   S	U-  U-  US-  -
  UUS-  '   S	U-  U-  US-  -
  UUS-  '   S	U-  U-  SU-  U-  -
  UXE-  '   SUX4-  '   SUX5-  '   [        UU5      n[        S
S
S[        U* 5      SU-  -  [        U* 5      SU-  -  SSSSSS/	5      U-  $ TXE-     S:w  a  TUS-     S:X  a^  TUS-     S:X  a  [        S
S
/ SQ5      $ U S   U S   sUS'   US'   [        UT5      n
U
R                  SS5        U
R	                  SS5        U
$ U S   U S   sUS'   US'   [        UT5      n
U
R                  SS5        U
R	                  SS5        U
$ [        R                  " S
5      $ )Nc              3  4   >#    U  H  nTUS -     v   M     g7frA  r   rj  s     rD   r   ,_transformation_to_normal.<locals>.<genexpr>
  s     (CquQT{Crl  FTr   r   r   r   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   )r   rq  r  row_swapcol_swapr  r   eye)r   r   ru  rS   r  r   r  rP  r~   swapr*  r   r   r  r   r   r   rw  T_0s    `                 rD   r  r  
  sv   9DGA!(C(((!#J!#J!#JDqQA2a4L1b1"Q$-;<JJq!JJq!QT{aA;!"1vs1vDGT!W)$6AJJq!JJq!Hq63q6Qa%dE2	

1a	

1a QSzQ%*/!Q$K!#J!#J!Q$K!#J!Q$KAvq!ts1uq!t|q!ts1uq!t|q!tc!eac!emqsqsqs'f5aQ1"qsQrUAaC[!Q1aKLSPP	qsqA;!QT{a a$@AA  #1vs1vDGT!W)$6AJJq!JJq!H q63q6Qa%dE2	

1a	

1a::a=rF   c                z    [        U SS9u  pnUS;   a(  [        [        X5      5      S   u  pEn[        XEU4X5      $ g)a4  
Returns the parametrized general solution for the ternary quadratic
equation ``eq`` which has the form
`ax^2 + by^2 + cz^2 + fxy + gyz + hxz = 0`.

Examples
========

>>> from sympy import Tuple, ordered
>>> from sympy.abc import x, y, z
>>> from sympy.solvers.diophantine.diophantine import parametrize_ternary_quadratic

The parametrized solution may be returned with three parameters:

>>> parametrize_ternary_quadratic(2*x**2 + y**2 - 2*z**2)
(p**2 - 2*q**2, -2*p**2 + 4*p*q - 4*p*r - 4*q**2, p**2 - 4*p*q + 2*q**2 - 4*q*r)

There might also be only two parameters:

>>> parametrize_ternary_quadratic(4*x**2 + 2*y**2 - 3*z**2)
(2*p**2 - 3*q**2, -4*p**2 + 12*p*q - 6*q**2, 4*p**2 - 8*p*q + 6*q**2)

Notes
=====

Consider ``p`` and ``q`` in the previous 2-parameter
solution and observe that more than one solution can be represented
by a given pair of parameters. If `p` and ``q`` are not coprime, this is
trivially true since the common factor will also be a common factor of the
solution values. But it may also be true even when ``p`` and
``q`` are coprime:

>>> sol = Tuple(*_)
>>> p, q = ordered(sol.free_symbols)
>>> sol.subs([(p, 3), (q, 2)])
(6, 12, 12)
>>> sol.subs([(q, 1), (p, 1)])
(-1, 2, 2)
>>> sol.subs([(q, 0), (p, 1)])
(2, -4, 4)
>>> sol.subs([(q, 1), (p, 0)])
(-3, -6, 6)

Except for sign and a common factor, these are equivalent to
the solution of (1, 2, 2).

References
==========

.. [1] The algorithmic resolution of Diophantine equations, Nigel P. Smart,
       London Mathematical Society Student Texts 41, Cambridge University
       Press, Cambridge, 1998.

Fr  r  r   N)r7   r   rs  r  )r  r   r   r%  r$  r%  rZ  s          rD   parametrize_ternary_quadraticr  
  sY    n *"E:C	 4 4 4S@A!D#-sOS) 	)	4rF   c                0   SU;  d   eU u  p4n[        U5      nUc  gU R                  S5      S:  a  gUS:X  a&  US   US   sUS'   US'   [        XCU4Xb5      u  pxn	XU	4$ Uu  pn[        SSS9u  pn[	        S UR                  5        5       5      n[        UR                  [        XU4X-  X-  U-   X-  U-   45      5      5      nUR                  USS	9u  nnUU-  n
UU-  [        UU-  U-  5      -
  nUU-  [        UU-  U-  5      -
  n[        XU5      $ )
Nr   rf  r   r   zr, p, qTr   c              3  .   #    U  H  u  pX-  v   M     g 7frW   r   )r}   r   rf   s      rD   r   1_parametrize_ternary_quadratic.<locals>.<genexpr>[  s     +]TQQS]s   )r  )r   r  r  r   r  r>  r   rN   r]   r  r  )rP   ru  r   r$  r%  rZ  rf   y_px_pz_prS   r  r   r   re   r   r  eq_1r   r   s                       rD   r  r  @  sO   E>>MCcT
A
{!~~aA
 "
axqT1Q4
!ad6sOQ'#}GA!i.GA!	+U[[]+	+BBGGC	
q	AE1519aeai02 3 4Dq.DAq 	
#A	
3!A#a%	 A	
3!A#a%	 AqQrF   c                    [        U SS9u  p#nU[        R                  :X  aJ  [        X#5      n[	        U5      S:  a  [        U5      S   u  pgnOSu  pgnU(       a  [        XgU4X#5      $ XgU4$ g)a]  
Solves the quadratic ternary diophantine equation,
`ax^2 + by^2 + cz^2 = 0`.

Explanation
===========

Here the coefficients `a`, `b`, and `c` should be non zero. Otherwise the
equation will be a quadratic binary or univariate equation. If solvable,
returns a tuple `(x, y, z)` that satisfies the given equation. If the
equation does not have integer solutions, `(None, None, None)` is returned.

Usage
=====

``diop_ternary_quadratic_normal(eq)``: where ``eq`` is an equation of the form
`ax^2 + by^2 + cz^2 = 0`.

Examples
========

>>> from sympy.abc import x, y, z
>>> from sympy.solvers.diophantine.diophantine import diop_ternary_quadratic_normal
>>> diop_ternary_quadratic_normal(x**2 + 3*y**2 - z**2)
(1, 0, 1)
>>> diop_ternary_quadratic_normal(4*x**2 + 5*y**2 - z**2)
(1, 0, 2)
>>> diop_ternary_quadratic_normal(34*x**2 - 3*y**2 - 301*z**2)
(4, 9, 1)
Fr  r   rf  N)r7   r=  ru   rt  rH   r   r  r  s	            rD   r-  r-  h  s{    > *"E:C	5:::,S8s8a< IaLMCc,MCc13- -} ;rF   c                \   ^ [        U4S jT 5       5      n[        X S9R                  5       $ )Nc              3  2   >#    U  H  oTU   -  v   M     g 7frW   r   rj  s     rD   r   1_diop_ternary_quadratic_normal.<locals>.<genexpr>  s     )5aq\5rE  r   )r  r=  r   )r   r   r  s    ` rD   rt  rt    s'    	)5)	)B,RBHHJJrF   c           	     \   [        XU5      n[        S U 5       5      n[        [        XE5       VVs/ s H  u  pgXgS-  -  PM     snn5      =nu  pn[        X5      nX-  n	X-  n
[        X5      nX-  n
X-  n[        X5      nX-  n	X-  n
X-  n	X-  n
X-  nU(       a  XXXU44$ XU4$ s  snnf )a  
Return `a', b', c'`, the coefficients of the square-free normal
form of `ax^2 + by^2 + cz^2 = 0`, where `a', b', c'` are pairwise
prime.  If `steps` is True then also return three tuples:
`sq`, `sqf`, and `(a', b', c')` where `sq` contains the square
factors of `a`, `b` and `c` after removing the `gcd(a, b, c)`;
`sqf` contains the values of `a`, `b` and `c` after removing
both the `gcd(a, b, c)` and the square factors.

The solutions for `ax^2 + by^2 + cz^2 = 0` can be
recovered from the solutions of `a'x^2 + b'y^2 + c'z^2 = 0`.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import sqf_normal
>>> sqf_normal(2 * 3**2 * 5, 2 * 5 * 11, 2 * 7**2 * 11)
(11, 1, 5)
>>> sqf_normal(2 * 3**2 * 5, 2 * 5 * 11, 2 * 7**2 * 11, True)
((3, 1, 7), (5, 55, 11), (11, 1, 5))

References
==========

.. [1] Legendre's Theorem, Legrange's Descent,
       https://public.csusm.edu/aitken_html/notes/legendre.pdf


See Also
========

reconstruct()
c              3  8   #    U  H  n[        U5      v   M     g 7frW   )rE  r}   rR   s     rD   r   sqf_normal.<locals>.<genexpr>  s     -A}Qr   r   )r  r?   r]   r   )r  rP  r~   rJ  ABCrM  rR   r  sqfr   r   r  pcpapbs                  rD   rK  rK    s    D aA
C	--	-Bc#l;lsq1d7l;<<C'!	aBGAGA	aBGAGA	aBGAGAGAGAGA!##Qw% <s   B(
c           	         [        U [        5      (       a  U O
[        U 5      n[        UR	                  5        VVs/ s H  u  p#X#S-  -  PM     snn6 $ s  snnf )a  
Returns an integer `c` s.t. `a = c^2k, \ c,k \in Z`. Here `k` is square
free. `a` can be given as an integer or a dictionary of factors.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import square_factor
>>> square_factor(24)
2
>>> square_factor(-36*3)
6
>>> square_factor(1)
1
>>> square_factor({3: 2, 2: 1, -1: 1})  # -18
3

See Also
========
sympy.ntheory.factor_.core
r   )r   r\   r!   r	   r>  )r  rS  re   r   s       rD   rE  rE    sF    , 4  ilAqwwy1ytqTy1221s   A
c                    [        [        X5      5      nUR                  5        H  u  pEUS:w  a  [        S5      eX$-  nM     U$ )a   
Reconstruct the `z` value of an equivalent solution of `ax^2 + by^2 + cz^2`
from the `z` value of a solution of the square-free normal form of the
equation, `a'*x^2 + b'*y^2 + c'*z^2`, where `a'`, `b'` and `c'` are square
free and `gcd(a', b', c') == 1`.
r   za and b should be square-free)r!   r   r>  r>   )r   r   r   rS  re   r   s         rD   rO  rO    sE     	$q*A	6<==	  HrF   c                   U S:X  d  US:X  a  [        S5      e[        U 5      [        U5      :  a  [        X5      u  p#nX$U4$ U S:X  a  gUS:X  a  gUS:X  a  g[        X5      nUc  gUS-  U -
  U-  nUS:X  a  USS4$ [	        U5       Hb  n[        [        U5      U-  S5      u  pU	(       d  M&  [        U5      U-  n
[        X
5      u  pn[        U * U-  X[-  -   X\-  U-
  X-  U-  5      s  $    g)	a  
Return a non-trivial solution to `w^2 = Ax^2 + By^2` using
Lagrange's method; return None if there is no such solution.

Parameters
==========

A : Integer
B : Integer
    non-zero integer

Returns
=======

(int, int, int) | None : a tuple `(w_0, x_0, y_0)` which is a solution to the above equation.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import ldescent
>>> ldescent(1, 1) # w^2 = x^2 + y^2
(1, 1, 0)
>>> ldescent(4, -7) # w^2 = 4x^2 - 7y^2
(2, -1, 0)

This means that `x = -1, y = 0` and `w = 2` is a solution to the equation
`w^2 = 4x^2 - 7y^2`

>>> ldescent(5, -1) # w^2 = 5x^2 - y^2
(2, 1, -1)

References
==========

.. [1] The algorithmic resolution of Diophantine equations, Nigel P. Smart,
       London Mathematical Society Student Texts 41, Cambridge University
       Press, Cambridge, 1998.
.. [2] Cremona, J. E., Rusin, D. (2003). Efficient Solution of Rational Conics.
       Mathematics of Computation, 72(243), 1417-1441.
       https://doi.org/10.1090/S0025-5718-02-01480-1
r   z!A and B must be non-zero integersr   r   r   r   r   r   r   r   Nr   )r>   r  ldescentr'   r    r   r   r  )r   r   r  r  rS   r   r'  rR   r  rK  r{  Wr   r   s                 rD   r  r    s    T 	Ava<==
1vA1.aQwAvAvBwAy	
AaAAv"axa[#CFaK3	6q'!)Cq&GA!r!tacz137AE!G<< rF   c                   [        U 5      [        U5      :  a  [        X5      u  p#nX$U4$ US:X  a  gU S:X  a  gX* :X  a  gX:X  a  [        SU 5      u  p$nX-  XB4$ [        X5      n[        XPU5      u  pgUS-  XS-  -  -
  U-  n[	        U5      n	XS-  -  n
[        X
5      u  pn[        Xk-  X-  U-  -   X{-  Xl-  -   X-  U-  5      $ )a  
Returns a non-trivial solution, (x, y, z), to `x^2 = Ay^2 + Bz^2`
using Lagrange's descent method with lattice-reduction. `A` and `B`
are assumed to be valid for such a solution to exist.

This is faster than the normal Lagrange's descent algorithm because
the Gaussian reduction is used.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import descent
>>> descent(3, 1) # x**2 = 3*y**2 + z**2
(1, 0, 1)

`(x, y, z) = (1, 0, 1)` is a solution to the above equation.

>>> descent(41, -113)
(-16, -3, 1)

References
==========

.. [1] Cremona, J. E., Rusin, D. (2003). Efficient Solution of Rational Conics.
       Mathematics of Computation, 72(243), 1417-1441.
       https://doi.org/10.1090/S0025-5718-02-01480-1
r   r  r  r   r   r   r   r   )r  rL  r'   gaussian_reducerE  r  )r   r   rS   r  r   r  r$  rZ  r   t_2t_1x_1z_1y_1s                 rD   rL  rL  ?  s    8 1vA!-aQwAvAvBwv"a.aQ{AqQ'HC	a!F(	q A

C
Av+CAOMCcsws*CGcg,=sws{KKrF   c                  ^ [        T5      mU4S jnUS4nX -  S:  a  U S4OU * S4nUS-  U S-  T-   :  a  XTpTU" XD5      U" XU5      =n:  a;  U" XE5      U-  nXTS   XuS   -  -
  US   XuS   -  -
  4pTU" XD5      U" XU5      =n:  a  M;  US   US   -
  US   US   -
  4nU" X5      U" XD5      s=::  a  SU" XE5      -  ::  a   U$   U$ U$ )a  
Returns a reduced solution `(x, z)` to the congruence
`X^2 - aZ^2 \equiv 0 \pmod{b}` so that `x^2 + |a|z^2` is as small as possible.
Here ``w`` is a solution of the congruence `x^2 \equiv a \pmod{b}`.

This function is intended to be used only for ``descent()``.

Explanation
===========

The Gaussian reduction can find the shortest vector for any norm.
So we define the special norm for the vectors `u = (u_1, u_2)` and `v = (v_1, v_2)` as follows.

.. math ::
    u \cdot v := (wu_1 + bu_2)(wv_1 + bv_2) + |a|u_1v_1

Note that, given the mapping `f: (u_1, u_2) \to (wu_1 + bu_2, u_1)`,
`f((u_1,u_2))` is the solution to `X^2 - aZ^2 \equiv 0 \pmod{b}`.
In other words, finding the shortest vector in this norm will yield a solution with smaller `X^2 + |a|Z^2`.
The algorithm starts from basis vectors `(0, 1)` and `(1, 0)`
(corresponding to solutions `(b, 0)` and `(w, 1)`, respectively) and finds the shortest vector.
The shortest vector does not necessarily correspond to the smallest solution,
but since ``descent()`` only wants the smallest possible solution, it is sufficient.

Parameters
==========

w : int
    ``w`` s.t. `w^2 \equiv a \pmod{b}`
a : int
    square-free nonzero integer
b : int
    square-free nonzero integer

Examples
========

>>> from sympy.solvers.diophantine.diophantine import gaussian_reduce
>>> from sympy.ntheory.residue_ntheory import sqrt_mod
>>> a, b = 19, 101
>>> gaussian_reduce(sqrt_mod(a, b), a, b) # 1**2 - 19*(-4)**2 = -303
(1, -4)
>>> a, b = 11, 14
>>> x, z = gaussian_reduce(sqrt_mod(a, b), a, b)
>>> (x**2 - a*z**2) % b == 0
True

It does not always return the smallest solution.

>>> a, b = 6, 95
>>> min_x, min_z = 1, 4
>>> x, z = gaussian_reduce(sqrt_mod(a, b), a, b)
>>> (x**2 - a*z**2) % b == 0 and (min_x**2 - a*min_z**2) % b == 0
True
>>> min_x**2 + abs(a)*min_z**2 < x**2 + abs(a)*z**2
True

References
==========

.. [1] Gaussian lattice Reduction [online]. Available:
       https://web.archive.org/web/20201021115213/http://home.ie.cuhk.edu.hk/~wkshum/wordpress/?p=404
.. [2] Cremona, J. E., Rusin, D. (2003). Efficient Solution of Rational Conics.
       Mathematics of Computation, 72(243), 1417-1441.
       https://doi.org/10.1090/S0025-5718-02-01480-1
c                8   > U S   US   -  TU S   -  US   -  -   $ rn  r   )r   rf   r  s     rD   _dotgaussian_reduce.<locals>._dot  s*    tAaDy1QqT6!A$;&&rF   r   r   r   r   ro  )	r  r  rP  r  r   rf   dvr   r~   s	    `       rD   r  r  u  s   F 	AA' 
AA#(A!RA 	!tadQh1 q*d1j(
)J"Q4!aD&=!A$Q4-01 q*d1j(
) 
1!adQqTk"AAzT!Z/1T!Z</ 0H1HrF   c                   [        U5      (       a  SU-  nOUS-  nX4-  U-  nSn X0S-  -  XAS-  -  XRS-  -  pn	X-   U:w  a  US:X  a  [        S5      eGO{XUpn[        XU5      U::  a  GOe[        XmU* 5      =nu  nnSU;   a  GOKUU-  U-  UU-  U-  -   * X^-  nn[	        UU5      n[        U5      (       a/  [        UU5      n[        UU-
  5      [        R                  ::  d   eOKUU-  n[        UU-  UU-  -   UU-  -   5      (       a  US-  n[        UU-
  5      [        R                  ::  d   eUUS-  -  UUS-  -  -   UUS-  -  -   nUU-  U-  UU-  U-  -   UU-  U-  -   n[	        UU-  SU-  U-  -
  U5      n [	        UU-  SU-  U-  -
  U5      n[	        UU-  SU-  U-  -
  U5      n[        S XU4 5       5      (       d   eUS-  nGM  [        WWW4 Vs/ s H  n[        U5      PM     sn5      $ s  snf )an  
Simplify the solution `(x, y, z)` of the equation
`ax^2 + by^2 = cz^2` with `a, b, c > 0` and `z^2 \geq \mid ab \mid` to
a new reduced solution `(x', y', z')` such that `z'^2 \leq \mid ab \mid`.

The algorithm is an interpretation of Mordell's reduction as described
on page 8 of Cremona and Rusin's paper [1]_ and the work of Mordell in
reference [2]_.

References
==========

.. [1] Cremona, J. E., Rusin, D. (2003). Efficient Solution of Rational Conics.
       Mathematics of Computation, 72(243), 1417-1441.
       https://doi.org/10.1090/S0025-5718-02-01480-1
.. [2] Diophantine Equations, L. J. Mordell, page 48.

r   r   zbad starting solutionNr   c              3  8   #    U  H  oR                   v   M     g 7frW   )r   r  s     rD   r   holzer.<locals>.<genexpr>  s     3A<<r   )r  r>   maxr   r
   r  r  r  r   Halfr   r   r?   rL   )rS   r  r   r  rP  r~   r   smallstept1t2t3r$  r%  rZ  uvr   rf   re   r   r   r  r   r   rR   s                            rD   rN  rN    sF   ( AwwaCqDCEED
!tVQ!tVQ!tV7b=qy !899a#rr?e#(#66TQ2:1S1Q3s7"#QU1QN88q!$Aq1u:'''1AAaC!A#I!O$$Qq1u:&&&q!tVa1f_qAv%qSWqs3w1S(SUQqSU]A&SUQqSU]A&SUQqSU]A&3!33333	C F 3S/2/Q#a&/2332s   G8c                    [        U SS9u  p#nU[        R                  :X  aB  Uc  SnO[        SU[	        U5      4-  SS9n[        [        U 5      R                  US95      S   $ g)	a  
Solves the general pythagorean equation,
`a_{1}^2x_{1}^2 + a_{2}^2x_{2}^2 + . . . + a_{n}^2x_{n}^2 - a_{n + 1}^2x_{n + 1}^2 = 0`.

Returns a tuple which contains a parametrized solution to the equation,
sorted in the same order as the input variables.

Usage
=====

``diop_general_pythagorean(eq, param)``: where ``eq`` is a general
pythagorean equation which is assumed to be zero and ``param`` is the base
parameter used to construct other parameters by subscripting.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import diop_general_pythagorean
>>> from sympy.abc import a, b, c, d, e
>>> diop_general_pythagorean(a**2 + b**2 + c**2 - d**2)
(m1**2 + m2**2 - m3**2, 2*m1*m3, 2*m2*m3, m1**2 + m2**2 + m3**2)
>>> diop_general_pythagorean(9*a**2 - 4*b**2 + 16*c**2 + 25*d**2 + e**2)
(10*m1**2  + 10*m2**2  + 10*m3**2 - 10*m4**2, 15*m1**2  + 15*m2**2  + 15*m3**2  + 15*m4**2, 15*m1*m4, 12*m2*m4, 60*m3*m4)
Fr  Nz%s1:%iTr   r   r   )r7   r  ru   r   rH   r   r   )r  r  r   r   r%  r   s         rD   r.  r.    sq    2 +2U;C	&+++=FXC(994HF&r*00F0CDQGG ,rF   c                    [        U SS9u  p#nU[        R                  :X  a!  [        [        U 5      R	                  US95      $ g)aX  
Solves the equation `x_{1}^2 + x_{2}^2 + . . . + x_{n}^2 - k = 0`.

Returns at most ``limit`` number of solutions.

Usage
=====

``general_sum_of_squares(eq, limit)`` : Here ``eq`` is an expression which
is assumed to be zero. Also, ``eq`` should be in the form,
`x_{1}^2 + x_{2}^2 + . . . + x_{n}^2 - k = 0`.

Details
=======

When `n = 3` if `k = 4^a(8m + 7)` for some `a, m \in Z` then there will be
no solutions. Refer to [1]_ for more details.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import diop_general_sum_of_squares
>>> from sympy.abc import a, b, c, d, e
>>> diop_general_sum_of_squares(a**2 + b**2 + c**2 + d**2 + e**2 - 2345)
{(15, 22, 22, 24, 24)}

Reference
=========

.. [1] Representing an integer as a sum of three squares, [online],
    Available:
    https://proofwiki.org/wiki/Integer_as_Sum_of_Three_Squares
Fr  r)  N)r7   r  ru   rI   r   r  r   r   r   r%  s        rD   r0  r0  1  sI    D *"E:C	',,,&r*00u0=>> -rF   c                    [        U SS9u  p#nU[        R                  :X  a!  [        [        U 5      R	                  US95      $ g)a[  
Solves the equation `x_{1}^e + x_{2}^e + . . . + x_{n}^e - k = 0`
where `e` is an even, integer power.

Returns at most ``limit`` number of solutions.

Usage
=====

``general_sum_of_even_powers(eq, limit)`` : Here ``eq`` is an expression which
is assumed to be zero. Also, ``eq`` should be in the form,
`x_{1}^e + x_{2}^e + . . . + x_{n}^e - k = 0`.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import diop_general_sum_of_even_powers
>>> from sympy.abc import a, b
>>> diop_general_sum_of_even_powers(a**4 + b**4 - (2**4 + 3**4))
{(2, 3)}

See Also
========

power_representation
Fr  r)  N)r7   r  ru   rI   r   r  s        rD   r2  r2  Y  sH    6 *"E:C	*///)"-33%3@AA 0rF   c              #     #    U(       a  Uc   [        X5       H  n[        U5      v   M     g[        SUS-   5       H6  n[        X5       H$  n[        U5      nSU[        U5      -
  -  U-   v   M&     M8     g7f)a  
Returns a generator that can be used to generate partitions of an integer
`n`.

Explanation
===========

A partition of `n` is a set of positive integers which add up to `n`. For
example, partitions of 3 are 3, 1 + 2, 1 + 1 + 1. A partition is returned
as a tuple. If ``k`` equals None, then all possible partitions are returned
irrespective of their size, otherwise only the partitions of size ``k`` are
returned. If the ``zero`` parameter is set to True then a suitable
number of zeros are added at the end of every partition of size less than
``k``.

``zero`` parameter is considered only if ``k`` is not None. When the
partitions are over, the last `next()` call throws the ``StopIteration``
exception, so this function should always be used inside a try - except
block.

Details
=======

``partition(n, k)``: Here ``n`` is a positive integer and ``k`` is the size
of the partition which is also positive integer.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import partition
>>> f = partition(5)
>>> next(f)
(1, 1, 1, 1, 1)
>>> next(f)
(1, 1, 1, 2)
>>> g = partition(5, 3)
>>> next(g)
(1, 1, 3)
>>> next(g)
(1, 2, 2)
>>> g = partition(5, 3, zeros=True)
>>> next(g)
(0, 0, 5)

Nr   r   )r5   r?   rJ   rH   )r  r   r  rR   r  s        rD   	partitionr    sm     \ AI#A)A(N * q!a%A'-!HAAJ'!++ . !s   A5A7c                   [        U 5      n U S-  S:w  a  g[        U 5      (       d  [        S5      eU S-  S:X  a  SnOEU S-  S:X  a  S	nO9U S-  S
;   a  SnO-Sn[        X5      S:X  a  [	        U5      n[        X5      S:X  a  M  [        XS-	  U 5      nU nUS-  U :  a  XU-  pUS-  U :  a  M  [        X!-  5      [        U5      4$ )a  
Represent a prime `p` as a unique sum of two squares; this can
only be done if the prime is congruent to 1 mod 4.

Parameters
==========

p : Integer
    A prime that is congruent to 1 mod 4

Returns
=======

(int, int) | None : Pair of positive integers ``(x, y)`` satisfying ``x**2 + y**2 = p``.
                    None if ``p`` is not congruent to 1 mod 4.

Raises
======

ValueError
    If ``p`` is not prime number

Examples
========

>>> from sympy.solvers.diophantine.diophantine import prime_as_sum_of_two_squares
>>> prime_as_sum_of_two_squares(7)  # can't be done
>>> prime_as_sum_of_two_squares(5)
(1, 2)

Reference
=========

.. [1] Representing a number as a sum of four squares, [online],
       Available: https://schorn.ch/lagrange.html

See Also
========

sum_of_squares

r   r   Nzp should be a prime number      r      r   )r   r      )r/   r%   r>   r   r#   powrL   )re   rP  r  s      rD   prime_as_sum_of_two_squaresr    s    V 	q	A1uz1::5661uz	
R1	
Q&Qla!A Qla 	AAvqA	A
Q$(a%1 Q$(JArF   c                b   0 SS_SS_SS_SS_S	S
_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS _S!S"_S#S$0En[        U 5      n U S%:  a  [        S&5      eU S%:X  a  g'[        U S(5      u  pSU-  nU S)-  S*:X  a  g+X;   a   [        X    Vs/ s H  o2U-  PM	     sn5      $ [	        U S5      u  pEU(       a  S%S%X$-  4$ U S)-  S:X  az  US-  (       d  US-  n[        US,S-5       HX  nXS-  -
  S-  n[        U5      (       d  M  [        U5      u  p[        [        X&-  X(U	-   -  U[        X-
  5      -  /5      5      s  $     eU S-  US-  -  (       d  US-  n[        US,S-5       HF  nXS-  -
  n[        U5      (       d  M  [        U5      u  p[        [        X&-  X(-  X)-  /5      5      s  $     es  snf ).a  
Returns a 3-tuple $(a, b, c)$ such that $a^2 + b^2 + c^2 = n$ and
$a, b, c \geq 0$.

Returns None if $n = 4^a(8m + 7)$ for some `a, m \in \mathbb{Z}`. See
[1]_ for more details.

Parameters
==========

n : Integer
    non-negative integer

Returns
=======

(int, int, int) | None : 3-tuple non-negative integers ``(a, b, c)`` satisfying ``a**2 + b**2 + c**2 = n``.
                         a,b,c are sorted in ascending order. ``None`` if no such ``(a,b,c)``.

Raises
======

ValueError
    If ``n`` is a negative integer

Examples
========

>>> from sympy.solvers.diophantine.diophantine import sum_of_three_squares
>>> sum_of_three_squares(44542)
(18, 37, 207)

References
==========

.. [1] Representing a number as a sum of three squares, [online],
    Available: https://schorn.ch/lagrange.html

See Also
========

power_representation :
    ``sum_of_three_squares(n)`` is one of the solutions output by ``power_representation(n, 2, 3, zeros=True)``

r   r  r   r  r   )r   r   r   
   )r   r   r   "   )r   r   r   :   )r   r   r  U   )r      r     )r   r         )r   r        )r  	   r  ir  )r  r     i  )r  r     i  )r  r     i  )r   r      i  )r     !   ir  )r  r  $   i  )r      '   i%  )8   9   r  r   "n should be a non-negative integer)r   r   r   r   r  r  Nr   r   )
r/   r>   r   r?   r   rJ   r%   r  sortedr  )
r  specialrf   rR   r  rK  rS   r(  r  r   s
             rD   sum_of_three_squaresr    s^   b^q) ^Q	 ^1i ^Y ^I ^9^ )^-0*^>A:^ORT]^J^ #Z^14l^DG^ [^ #'^ 7;L^ KOP\^G 	q	A1u=>>Av!Q<DA	QA1uz|7:.:ac:.//1%IA1ac{1uz1uFAq"b!ATaAqzz215VQS!U)Qs15z\$BCDD " 	u Ea!e	Q1b"1H1::.q1DAac13011  57 /s   
F,c           	        [        U 5      n U S:  a  [        S5      eU S:X  a  g[        U S5      u  pSU-  nU S-  S:X  a  SnU S-
  n OU S-  S	;   a  SnU S-
  n OSn[        U 5      u  p4n[	        [        X-  X-  X-  X-  /5      5      $ )
a  
Returns a 4-tuple `(a, b, c, d)` such that `a^2 + b^2 + c^2 + d^2 = n`.
Here `a, b, c, d \geq 0`.

Parameters
==========

n : Integer
    non-negative integer

Returns
=======

(int, int, int, int) : 4-tuple non-negative integers ``(a, b, c, d)`` satisfying ``a**2 + b**2 + c**2 + d**2 = n``.
                       a,b,c,d are sorted in ascending order.

Raises
======

ValueError
    If ``n`` is a negative integer

Examples
========

>>> from sympy.solvers.diophantine.diophantine import sum_of_four_squares
>>> sum_of_four_squares(3456)
(8, 8, 32, 48)
>>> sum_of_four_squares(1294585930293)
(0, 1234, 2161, 1137796)

References
==========

.. [1] Representing a number as a sum of four squares, [online],
    Available: https://schorn.ch/lagrange.html

See Also
========

power_representation :
    ``sum_of_four_squares(n)`` is one of the solutions output by ``power_representation(n, 2, 4, zeros=True)``

r   r  )r   r   r   r   r   r   r  r  r   )r   r  )r/   r>   r   r  r?   r  )r  rf   r  rS   r  r   s         rD   sum_of_four_squaresr  Z  s    Z 	q	A1u=>>Av !Q<DA	QA1uzE	
Q&E"1%GA!ac13,-..rF   c           
   #  D  ^#    XU4 Vs/ s H  n[        U5      PM     snu  pnU S:  a4  US-  (       a)  [        U * XU5       H  n[        S U 5       5      v   M     gUS:  d  US:  a  [        [	        SU< SU< S35      5      eU S:X  a  U(       a  S	U-  v   gUS:X  aI  US:X  a  U 4v   gU S:X  a  S
v   g[        U 5      nU(       a  Uu  px[        X5      u  pU
(       d  Xy-  4v   gUS:X  a  [        XUS9 Sh  vN   gUS:X  a  US:X  aF  [        U S5      u  n mT(       a0  ST-  m[        XX#5       H  n[        U4S jU 5       5      v   M     g[        X5      nU(       d  gU(       dH  U S:  a  US:  a  X ::  a
  X-
  S;   a   gUS:X  a  U S;   d  [        U S5      S   S;   a  gUS:X  a  U S;   a  gUSLa  [        U 5      v   gUS:X  a%  US:  a  [        U 5      nU(       a  US   U-  S:X  a  gX:  a?  [        XS-
  -
  U5      S   n[        XU / U5       H  n[        [        U5      5      v   M     U(       aU  [        X5      S   n[        SU5       H6  n[        XU / U5       H!  n[        [        US	X$-
  -  -   5      5      v   M#     M8     ggs  snf  GN7f)a  
Returns a generator for finding k-tuples of integers,
`(n_{1}, n_{2}, . . . n_{k})`, such that
`n = n_{1}^p + n_{2}^p + . . . n_{k}^p`.

Usage
=====

``power_representation(n, p, k, zeros)``: Represent non-negative number
``n`` as a sum of ``k`` ``p``\ th powers. If ``zeros`` is true, then the
solutions is allowed to contain zeros.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import power_representation

Represent 1729 as a sum of two cubes:

>>> f = power_representation(1729, 3, 2)
>>> next(f)
(9, 10)
>>> next(f)
(1, 12)

If the flag `zeros` is True, the solution may contain tuples with
zeros; any such solutions will be generated after the solutions
without zeros:

>>> list(power_representation(125, 2, 3, zeros=True))
[(5, 6, 8), (3, 4, 10), (0, 5, 10), (0, 2, 11)]

For even `p` the `permute_sign` function can be used to get all
signed values:

>>> from sympy.utilities.iterables import permute_signs
>>> list(permute_signs((1, 12)))
[(1, 12), (-1, 12), (1, -12), (-1, -12)]

All possible signed permutations can also be obtained:

>>> from sympy.utilities.iterables import signed_permutations
>>> list(signed_permutations((1, 12)))
[(1, 12), (-1, 12), (1, -12), (-1, -12), (12, 1), (-12, 1), (12, -1), (-12, -1)]
r   r   c              3  &   #    U  H  o* v   M	     g 7frW   r   r  s     rD   r   'power_representation.<locals>.<genexpr>  s     ?1Bs   Nr   z9
    Expecting positive integers for `(p, k)`, but got `(z, z)`r  r   r  r   r   c              3  ,   >#    U  H	  oT-  v   M     g 7frW   r   )r}   rR   rf   s     rD   r   r    s     /Q!Qs   r  r  )r  r  r  r  r   r   r   )r   r   r  r  r  r     )   )r   r     )
r   r   r  r  r     %   r  r  r  T)r/   r  r?   r>   r0   r"   r   r  r   _can_do_sum_of_squaresr  r   pow_rep_recursivereversedrJ   )r  re   r   r  rR   r   berP  r   r  r   feasibler  rf   s                @rD   r  r    s    \ $%),)Qvay),GA!1uq5)1"aE:??** ;1uA!%   	 	Avq&LAv6$J 	 !VJ 	 q!Ba|4'MAvQ///Av6!Q<DAqF-aA=A/Q/// >)!/2v!q&QV :' 1']Av1 <<q!QS]@]Av!EE4-a00Av!a%1"Q%!)q.vAQK+A."1B2A$$ 3 A!!$q!A&qQA6HQqu%5677 7  U -B 	0s   J JC J ?J FJ c              #    #    U S::  d  US::  a  g X!:  a  g U[        X5      -  U:  a  g US:X  a  US:X  a  [        U5      v   g US:X  a*  [        X$5      u  pVU(       a  XP::  a  [        X5/-   5      v   g U S:  aP  US:  aI  [        SU S-   5       H5  nU[        XT5      -
  nUS:  a    g [	        XQS-
  XsU/-   U5       S h  vN   M7     g g g  N7frn  )r  r?   r   rJ   r  )n_ir   n_remainingr  re   	next_termexactresiduals           rD   r  r    s     
ax16 3s;$Av+"El	
a*;:	Y%+,, !8A"1cAg.	&Y)::a<,YAxR[Q\I\^_```	 / 8
 as   B<C>C
?Cc              #  :   #    [        U SX5       Sh  vN   g N7f)a  Return a generator that yields the k-tuples of nonnegative
values, the squares of which sum to n. If zeros is False (default)
then the solution will not contain zeros. The nonnegative
elements of a tuple are sorted.

* If k == 1 and n is square, (n,) is returned.

* If k == 2 then n can only be written as a sum of squares if
  every prime in the factorization of n that has the form
  4*k + 3 has an even multiplicity. If n is prime then
  it can only be written as a sum of two squares if it is
  in the form 4*k + 1.

* if k == 3 then n can be written as a sum of squares if it does
  not have the form 4**m*(8*k + 7).

* all integers can be written as the sum of 4 squares.

* if k > 4 then n can be partitioned and each partition can
  be written as a sum of 4 squares; if n is not evenly divisible
  by 4 then n can be written as a sum of squares only if the
  an additional partition can be written as sum of squares.
  For example, if k = 6 then n is partitioned into two parts,
  the first being written as a sum of 4 squares and the second
  being written as a sum of 2 squares -- which can only be
  done if the condition above for k = 2 can be met, so this will
  automatically reject certain partitions of n.

Examples
========

>>> from sympy.solvers.diophantine.diophantine import sum_of_squares
>>> list(sum_of_squares(25, 2))
[(3, 4)]
>>> list(sum_of_squares(25, 2, True))
[(3, 4), (0, 5)]
>>> list(sum_of_squares(25, 4))
[(1, 2, 2, 4)]

See Also
========

sympy.utilities.iterables.signed_permutations
r   N)r  )r  r   r  s      rD   r  r  >  s     Z $Aq!333s   c                (   US:  a  gU S:  a  gU S:X  a  gUS:X  a  [        U 5      $ US:X  aK  U S;   a  g[        U 5      (       a  U S-  S:X  a  gg[        S [        U 5      R	                  5        5       5      $ US	:X  a  [        U S5      S   S
-  S:g  $ g)a  Return True if n can be written as the sum of k squares,
False if it cannot, or 1 if ``k == 2`` and ``n`` is prime (in which
case it *can* be written as a sum of two squares). A False
is returned only if it cannot be written as ``k``-squares, even
if 0s are allowed.
r   Fr   Tr   )r   r   r   c              3  T   #    U  H  u  pUS -  S:g  =(       d    US-  S:H  v   M      g7f)r   r   r   r   Nr   )r}   re   r  s      rD   r   )_can_do_sum_of_squares.<locals>.<genexpr>  s,     L7Ktq1q519*A
*7Ks   &(r   r  r  )r$   r%   r   r!   r>  r   )r  r   s     rD   r  r  n  s     	1u1uAvAv|Av;1::1uz Ly|7I7I7KLLLAva|A"a'' rF   rW   )T)r  rL   rP  rL   r  rL   r   zset[tuple[int, int]])F)r  rL   r  rL   rP  rL   r   ztuple[int, int]r  )NF)
__future__r   sympy.core.addr   sympy.core.assumptionsr   sympy.core.containersr   sympy.core.exprtoolsr   sympy.core.functionr   sympy.core.mulr	   sympy.core.numbersr
   r   sympy.core.intfuncr   r   r   r   r   sympy.core.relationalr   sympy.core.singletonr   sympy.core.sortingr   r   sympy.core.symbolr   r   sympy.core.sympifyr   sympy.external.gmpyr   r   r   r   $sympy.functions.elementary.complexesr   #sympy.functions.elementary.integersr   (sympy.functions.elementary.miscellaneousr   sympy.matrices.denser   r  sympy.ntheory.factor_r    r!   r"   sympy.ntheory.generater#   sympy.ntheory.primetestr$   r%   sympy.ntheory.modularr&   sympy.ntheory.residue_ntheoryr'   r(   sympy.polys.polyerrorsr)   sympy.polys.polytoolsr*   r+   r  r,   sympy.solvers.solvesetr-   sympy.utilitiesr.   sympy.utilities.miscr/   r0   sympy.utilities.iterablesr1   r2   r3   r4   r5   __all__rI   r9   rr   r   r   r   r7  r=  r^  rz  r  r  r  r  r  r$  ru   r3  r  rM  r  r  r  r6   r  r&  r  r7   joinr  func_docr*  r   r/  r  r+  r	  r  rG  rF  rH  rn  rk  rI  rw  r  r}  r  r  r,  rs  r  r  r  r  r-  rt  rK  rE  rO  r  rL  r  rN  r.  r0  r2  r  r  r  r  r  sum_of_powersr  r  r  )r6  s   0rD   <module>r2     sq   "  4 ' - (  3 I I $ " 8 - ' = = 5 5 9 = D D , 6 3 A 3 3 , 0 , 3P P
 /
*gS gTK& K&\( @C$ CL[- [|*$; *&T(? TnE"9 EPJ$; J(F"9 F(D1 DNK0 K\>' >6<4 <B !%!  1AA0@*oo0@A
$*
 "#t44|~ F: !d3 S<l:&L ??6*-.M&0  T "#t4 78t1$h#/L %S$7 2Ej
 C. HVJZFR5"p sD1 Tn$AN*ZP1f,<^)$X;"*80fP5 M`>)B% P)XK
6r34A=H3LlUp>4B (/sD'A  HF%?PBL5,pC LZz@/F|8~ %a>-4`cU Bs   K