
    \hP                    .   S r SSKJr  SSKJrJrJrJrJrJ	r	J
r
JrJr  SSKJr  SSKJrJrJrJrJr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!J"r"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,  SSK-J.r/J0r1  SSK2J3r3  SSK4J5r5  SSK6J7r7J8r8J9r9J:r:  SSK;J<r<J=r=  SSK>J?r?J@r@JArAJBrBJCrCJDrDJErEJFrFJGrGJHrHJIrIJJrJJKrKJLrLJMrMJNrN  SSKOJPrP  SSKQJRrRJSrSJTrTJUrU  SSKVJWrWJXrXJYrYJZrZJ[r[J\r\J]r]J^r^J_r_J`r`JaraJbrbJcrc  SSKdJere  SSKfJgrg  SSKhJiriJjrj  SSKkJlrlJmrmJnrnJoroJprpJqrqJrrrJsrsJtrt  SSKuJvrvJwrw  SSKxJyryJzrzJ{r{  SSK|J}r}  SSK~JrJr  SSKJrJrJrJrJrJrJrJ0r0J.r.  SS KJr  SS!KJrJrJr  SS"KJrJrJr  SS#K2Jr  SS$KJrJrJrJrJr  SS%KJr  SS&KJr  SS'KJrJrJrJr  SS(KJrJrJr  SS)KJr   " S* S+\5      rS, r\GRX                  4S- jr\rS. rS/ rS0qS0qS1 rS2 rS3 rS4 rS5 rS6 r\GRX                  4S7 jrS8 rS9 r " S: S;\5      rS< rS= rS> r\GRX                  4S? jrS@ rSA rSB rS`SD jrSE rSF rSG rSH rSI rSJ rSK rSL rSM rSN rS0\GRX                  4SO jrSP rSQ rSR rSS rSCST.SU jrSV rSW rSX r0 // / S04SY jrSZ rS[ rS\ rS] rS^ rS_ rg0)aa  
This module contains functions to:

    - solve a single equation for a single variable, in any domain either real or complex.

    - solve a single transcendental equation for a single variable in any domain either real or complex.
      (currently supports solving in real domain only)

    - solve a system of linear equations with N variables and M equations.

    - solve a system of Non Linear Equations with N variables and M equations
    )sympify)	SPowDummypiExprWildMulAddBasic)Tuple)Lambdaexpand_complexAppliedUndef
expand_log_mexpandexpand_trignfloat)Mod)INumberRationaloo)integer_log)EqNe
Relational)default_sort_keyordered)Symbol_uniquely_named_symbol)_sympify)preorder_traversal)gcdlcm)_linear_eq_to_dict)UnsolvableFactorError)simplifyfractiontrigsimp	nsimplify)	powdenest
logcombine)logtancotsincosseccscexpacosasinatanacotacscasecpiecewise_fold	Piecewise)totient)Absargreim)HyperbolicFunctionsinhcoshtanhcothsechcschasinhacoshatanhacothasechacsch)	real_root)TrigonometricFunction)AndBooleanTrue)		FiniteSetimagesetIntervalIntersectionUnionConditionSetImageSet
ComplementContains)Set
ProductSet)zerosMatrix
MatrixBase)divisors)discrete_lognthroot_mod)	rootsPolydegreetogetherPolynomialErrorRootOffactorr%   r$   )CoercionFailed)invertgroebnerpoly)sympy_eqs_to_ringsolve_lin_sysPolyNonlinearError)	_linsolve)checksoldenomsunrad_simple_densrecast_to_symbols)solve_poly_system)
filldedent)numbered_symbolshas_dupsis_sequenceiterable)periodicitycontinuous_domainfunction_range)GeneratorTypec                       \ rS rSrSrSrg)NonlinearErrorD   z9Raised when unexpectedly encountering nonlinear equations N__name__
__module____qualname____firstlineno____doc____static_attributes__r       N/var/www/auris/envauris/lib/python3.13/site-packages/sympy/solvers/solveset.pyr   r   D   s    Cr   r   c                 R   [        S[        SS9n/ n[        U R                  " U6 5       H7  nU H  nUR                  " U6 nM     UR                  U[        U5      45        M9     [        U5       H  u  nu  pgU R	                  Xg5      n Xv4X5'   M      [        [        U5      5      nX4$ )a  Return ``f``, with all objects given by ``atoms`` replaced with
Dummy symbols, ``d``, and the list of replacements, ``(d, e)``,
where ``e`` is an object of type given by ``atoms`` in which
any other instances of atoms have been recursively replaced with
Dummy symbols, too. The tuples are ordered so that if they are
applied in sequence, the origin ``f`` will be restored.

Examples
========

>>> from sympy import cos
>>> from sympy.abc import x
>>> from sympy.solvers.solveset import _masked

>>> f = cos(cos(x) + 1)
>>> f, reps = _masked(cos(1 + cos(x)), cos)
>>> f
_a1
>>> reps
[(_a1, cos(_a0 + 1)), (_a0, cos(x))]
>>> for d, e in reps:
...     f = f.xreplace({d: e})
>>> f
cos(cos(x) + 1)
aT)clsreal)
r{   r   r   atomsreplaceappendnext	enumeratelistreversed)fr   symmaskr   ions           r   _maskedr   I   s    4 3E
5CDQWWe_%A		1A QS	N# & t_	6AIIaO& % D7Nr   c                 h   [        U5      nUR                  (       d  [        S5      e[        U 5      n X R                  ;  a  [        S5      e[        U5      nX!R                  ;   a  [        S5      eUR	                  [
        R                  5      (       a  [        U [        U5      U5      u  pEO[        U [        U5      U5      u  pEXB:w  a  XE4$ U[
        R                  L a  XE4$ [        U[        5      (       a  XER                  U5      4$ U[
        R                  L a  XE4$ XER                  U5      4$ )ak  
Reduce the complex valued equation $f(x) = y$ to a set of equations

$$\left\{g(x) = h_1(y),\  g(x) = h_2(y),\ \dots,\  g(x) = h_n(y) \right\}$$

where $g(x)$ is a simpler function than $f(x)$.  The return value is a tuple
$(g(x), \mathrm{set}_h)$, where $g(x)$ is a function of $x$ and $\mathrm{set}_h$ is
the set of function $\left\{h_1(y), h_2(y), \dots, h_n(y)\right\}$.
Here, $y$ is not necessarily a symbol.

$\mathrm{set}_h$ contains the functions, along with the information
about the domain in which they are valid, through set
operations. For instance, if :math:`y = |x| - n` is inverted
in the real domain, then $\mathrm{set}_h$ is not simply
$\{-n, n\}$ as the nature of `n` is unknown; rather, it is:

$$ \left(\left[0, \infty\right) \cap \left\{n\right\}\right) \cup
                   \left(\left(-\infty, 0\right] \cap \left\{- n\right\}\right)$$

By default, the complex domain is used which means that inverting even
seemingly simple functions like $\exp(x)$ will give very different
results from those obtained in the real domain.
(In the case of $\exp(x)$, the inversion via $\log$ is multi-valued
in the complex domain, having infinitely many branches.)

If you are working with real values only (or you are not sure which
function to use) you should probably set the domain to
``S.Reals`` (or use ``invert_real`` which does that automatically).


Examples
========

>>> from sympy.solvers.solveset import invert_complex, invert_real
>>> from sympy.abc import x, y
>>> from sympy import exp

When does exp(x) == y?

>>> invert_complex(exp(x), y, x)
(x, ImageSet(Lambda(_n, I*(2*_n*pi + arg(y)) + log(Abs(y))), Integers))
>>> invert_real(exp(x), y, x)
(x, Intersection({log(y)}, Reals))

When does exp(x) == 1?

>>> invert_complex(exp(x), 1, x)
(x, ImageSet(Lambda(_n, 2*_n*I*pi), Integers))
>>> invert_real(exp(x), 1, x)
(x, {0})

See Also
========
invert_real, invert_complex
zx must be a symbolz*Inverse of constant function doesn't existzy should be independent of x )r   	is_Symbol
ValueErrorfree_symbols	is_subsetr   Reals_invert_realrT   _invert_complex	Complexes
isinstance	intersect)f_xyxdomainx1ss         r   _invertr   p   s	   p 	
A;;-..
#,C   EFF
ANN899  S)A,2AYq\15 	u u!Y;;v&&& u;;v&&&r   c                 8    [        XU[        R                  5      $ )zv
Inverts a real-valued function. Same as :func:`invert_complex`, but sets
the domain to ``S.Reals`` before inverting.
)r   r   r   )r   r   r   s      r   invert_realr      s    
 31agg&&r   c                 	   X:X  d  U[         R                  L a  X!4$ [        SSS9n[        U [        5      (       d/  U R
                  (       aR  U R                  [         R                  :X  a4  [        U R                  [        [        U[        U5      5      U5      U5      $ [        U S5      (       a  U R                  5       b  [        U [        [        45      (       de  [!        U R"                  5      S:  a  [%        S5      e[        U R"                  S   [        [        X0R                  5       " U5      5      U5      U5      $ [        U [&        5      (       a  [)        U R"                  S   X5      $ U R*                  (       aI  U R-                  U5      u  pEU[         R.                  La#  [        U[        [        X3U-
  5      U5      U5      $ U R0                  (       aI  U R-                  U5      u  pEU[         R2                  La#  [        U[        [        X3U-  5      U5      U5      $ U R
                  (       Ga  U R"                  u  pgUR5                  U5      nUR5                  U5      n	U	(       GdH  UR6                  (       a  UR9                  5       u  pUS-  S:X  a^  U
S-  S:X  aU  UR:                  S	L aF  [        U[=        X75      5      nU[?        S[@        5      -  n[        X5      n[        XnU5      u  nnUU4$ US-  S:X  ac  [        U[=        X75      5      n[        X5      nU
S-  S:X  a%  [        [        X3* 5      U5      n[        XnU-   U5      $ U
S-  S:X  a  [        XnU5      $ OQURB                  (       a?  [        U[=        X75      5      nU[?        S[@        5      -  n[        X5      n[        XnU5      $  U(       Gd  UR"                  S   nURD                  (       a)  [        U[        [        U[        X6S	S
95      U5      U5      $ URF                  (       a>  [I        UU5      u  nnU(       a  [        U[K        U5      U5      $ U[         R                  4$ UR:                  (       a\  [M        US5      nU[         RN                  :X  a  [        U[K        S5      U5      $ U[         RP                  :X  a  U[         R                  4$ [        U [        [        45      (       a  [S        XU5      $ X4$ )Helper function for _invert.r   Tr   inverse   /Only functions with one argument are supported.r      Fevaluate)*r   EmptySetr   r   r5   is_PowbaseExp1r   rU   r   r.   hasattrr   rQ   rC   lenargsr   r?   _invert_absis_Addas_independentZerois_MulOnehasis_rationalas_numer_denomis_zerorP   rV   r   is_irrationalis_positiveis_negativer   rT   r   truefalse_invert_trig_hyp_real)r   g_yssymbolr   ghr   expobase_has_symexpo_has_symnumdenrootg_ys_posres_inv_setneg_resrhsr   bones                         r   r   r      s    	{dajj(~cA!Sahh166QVV+;AEE$VAs1v%6="$ 	$ q)!8A!P B B qvv;?NOOAFF1I$VAyy{1~%>E"$ 	$ !S166!9d33xx'AFF?8F1!e,<d#CVLLxx'AEE>8F1cND#A6JJxxxVV
xx'xx'..07a<C!GqLS[[E5I "!Yq%78D#hq"o5H"42C!-d!@JD$ $<'7a<!!Yq%78D"4.CQw!|"*6!R=#">+D-HHQw!|+Dv>>##a1!34(1b/1t.#Dv66
 ))A,C#DVAs1U'CDdKVU U!!"3-1'ilFCC !**--aj!&&='ilFCCAGG^ !**--!+-?@AA$Qf559r   Nc                   ^ ^^^	^
^ [        T [        5      (       GaX  [        SSS9m	[        T [        5      (       a.  [	        T R
                  S   [        T	[        T	5      U5      T5      $ [        T [        5      (       a  UR                  [        S[        5      5      n[        U[        5      (       a  [        U[        5      (       a  UnOT U4$ [	        T R
                  S   [        [        T	[        T	5      U5      [        T	[        T	5      * U5      5      T5      $ [        T [         5      (       a  UR                  [        R"                  " SS5      5      n[        U[        5      (       a  [        U[        5      (       a  UnOT U4$ [	        T R
                  S   [        [        T	[%        T	5      U5      [        T	[%        T	5      * U5      5      T5      $ [        T [&        5      (       a  UR                  [        R(                  " SS5      5      n[        U[        5      (       a  [        U[        5      (       a  UnOT U4$ [	        T R
                  S   [        T	[+        T	5      U5      T5      $ [        T [,        5      (       an  U[        SS5      -
  n[        U[.        5      (       a  [        U[        5      (       a  UnOT U4$ [	        T R
                  S   [        T	[1        T	5      U5      T5      $ [        T [2        5      (       am  U[        S5      -
  n[        U[.        5      (       a  [        U[        5      (       a  UnOT U4$ [	        T R
                  S   [        T	[5        T	5      U5      T5      $ g
[        T [6        5      (       ae  [        U[        5      (       aP  S nU" T R8                  5      u  mm
m[        SSS9m	U UU	U
UU4S	 jn[        U Vs/ s H
  oe" U5      PM     sn6 nTU4$ T U4$ s  snf )aU  Helper function for inverting trigonometric and hyperbolic functions.

This helper only handles inversion over the reals.

For trigonometric functions only finite `g_ys` sets are implemented.

For hyperbolic functions the set `g_ys` is checked against the domain of the
respective inverse functions. Infinite `g_ys` sets are also supported.
r   Tr   r   r   c                 $   [         Gc   [        [        S 4S[        -  [	        SS5      4[
        [        S 4S[        -  [	        SS5      4[        [        4[        [        R                  4[        [        4[        [        R                  4[        [        S 4S[        -  [        [	        [         * S5      [	        S[         5      5      4["        [$        S 4S[        -  [        [	        [         * S5      [	        S[         5      5      40q [         U    $ )Nc                 &    [         [        U 5      -
  $ N)r   r7   r   s    r   <lambda>C_invert_trig_hyp_real.<locals>._get_trig_inverses.<locals>.<lambda>      BtAwJr   r   r   r   c                     [        U 5      * $ r   )r6   r   s    r   r   r     
    T!WHr   c                     [        U 5      * $ r   )r;   r   s    r   r   r     r   r   c                 &    [         [        U 5      -
  $ r   )r   r:   r   s    r   r   r     r   r   )_trig_inversesr1   r7   r   rV   r2   r6   r/   r8   r   r   r0   r9   r3   r;   rX   r   r4   r:   funcs    r   _get_trig_inverses1_invert_trig_hyp_real.<locals>._get_trig_inverses  s    %D"672xAOD"45qtXb!_MD7B0D7B0D"45qthsB/!RACD"672hsB/!RAC"D "$''r   integerc                 R  > [        T Vs/ s H*  n[        TT	T-  U" U 5      -   [        R                  5      PM,     sn6 n[	        TR
                  S   UT5      u  p4UT:X  a  T
R                  U 5      n[        TXT5      $ [        T[        TU 5      [        R                  5      $ s  snf Nr   )
rX   rU   r   Integersr   r   containsrY   r   r   )r   invinvsimginv_finv_g_yscondsr   invsr   periodrngr   s         r   create_return_set0_invert_trig_hyp_real.<locals>.create_return_set  s    FJLFJsF1Hs1v-qzz:dL MG*166!9gvFOEQ#FE<<#FBq!Hagg>>Ls   1B$N)r   rC   r   rD   r   r   rU   rJ   rE   r   rV   r   rW   rT   rX   rK   rH   LopenrN   rF   openrL   rG   r[   rM   rI   rO   rQ   r   )r   r   r   g_ys_domr   r  r   retsetr  r   r  r  s   ` `     @@@@r   r   r   M  s]    !'((#D!aq	8AuQx+FOOa~~hq"o6H(L11dI..  $Ht9$q	5E!Hh/U1XIx0,239; ; a~~hnnQ&:;H(L11dI..#Ht9$q	5E!Hh/U1XIx0,239; ; a~~hmmB&:;H(L11dI..#Ht9$q	E!Hh/9 9 ahr1o-H(J//dI..#Ht9$q	E!Hh/9 9 ail*H(J//dI..#Ht9$q	E!Hh/9 9  
A,	-	-*T92M2M	( /qvv6fc#t$		? 		? t<t!*1-t<= 4y	 =s   O3c                   ^ ^^	^
^^ [        T [        5      (       a  [        U[        5      (       a  U4S jn[        SSS9m[        R
                  m
U" T 5       HG  nT
[        U Vs/ s H-  n[        [        TU" U5      5      [        R                  5      PM/     sn6 -  m
MI     [        T R                  S   T
T5      $ [        T [        5      (       ae  [        U[        5      (       aP  S nU" T R                  5      u  m
mm	[        SSS9mU	U U
UUU4S jn[        U Vs/ s H
  oW" U5      PM     sn6 nTU4$ T U4$ s  snf s  snf )a>  Helper function for inverting trigonometric and hyperbolic functions.

This helper only handles inversion over the complex numbers.
Only finite `g_ys` sets are implemented.

Handling of singularities is only implemented for hyperbolic equations.
In case of a symbolic element g in g_ys a ConditionSet may be returned.
c                 x  >^ ^ [        T [        [        45      (       a/  [        T [        5      (       a  [        O[        mUU4S jUU4S j4$ [        T [
        [        45      (       a/  [        T [
        5      (       a  [        O[        mUU4S jUU4S j4$ [        T [        [        45      (       a  UU 4S j4$ g )Nc                 .   > ST-  [         -  T" U 5      -   $ Nr   r   r   Fr   s    r   r   7_invert_trig_hyp_complex.<locals>.inv.<locals>.<lambda>      ac"fqtmr   c                 <   > ST-  [         -  [         -   T" U 5      -
  $ r  r  r  s    r   r   r    s    ac"frkAaD0r   c                 .   > ST-  [         -  T" U 5      -   $ r  r  r  s    r   r   r    r  r   c                 .   > ST-  [         -  T" U 5      -
  $ r  r  r  s    r   r   r    r  r   c                 D   > T[         -  TR                  5       " U 5      -   $ r   )r   r   )r   r   trigs    r   r   r    s    !B$):":r   )r   r1   r4   r7   r:   r2   r3   r6   r;   r/   r0   )r  r  r   s   `@r   r   %_invert_trig_hyp_complex.<locals>.inv  s    $c
++&tS11Dt+02 2 $c
++&tS11Dt++- - $c
++:<< ,r   r   Tr   r   c                 z   [         c  [        [        S 4S[        -  [        -  S4[
        [        S 4S[        -  [        -  S4[        [        4[        [        -  S4[        [        4[        [        -  S4[        [        S 4S[        -  [        -  S4[        [        S 4S[        -  [        -  S40q [         U    $ )	Nc                 4    [         [        -  [        U 5      -
  $ r   )r   r   rJ   r   s    r   r   E_invert_trig_hyp_complex.<locals>._get_hyp_inverses.<locals>.<lambda>      ad58mr   r   r   c                     [        U 5      * $ r   )rK   r   s    r   r   r    
    uQxir   )r   r   c                     [        U 5      * $ r   )rN   r   s    r   r   r    r"  r   )r   c                 4    [         [        -  [        U 5      -
  $ r   )r   r   rO   r   s    r   r   r    r   r   )_hyp_inversesrD   rJ   r   r   rE   rK   rF   rL   rG   rM   rH   rN   rI   rO   r   s    r   _get_hyp_inverses3_invert_trig_hyp_complex.<locals>._get_hyp_inverses  s    $U$;<ac"fbIU$78!A#b&"EUHadG4UHadG4U$78!A#b&%HU$;<ac"feL!N !&&r   c                 ~  > [        T	 Vs/ s H*  n[        T
TT
-  U" U 5      -   [        R                  5      PM,     sn6 n[	        TR
                  S   UT5      u  p4UT:X  a.  [        T Vs/ s H  n[        X5      PM     sn6 n[        TXd5      $ [        T[        TU 5      [        R                  5      $ s  snf s  snf r   )rX   rU   r   r   r   r   rR   r   rY   r   r   )r   r   r  r  r  er  exclr   r  r   r  r   s          r   r  3_invert_trig_hyp_complex.<locals>.create_return_set  s    FJLFJsF1Hs1v-qzz:dL MG-affQi&IOE51bh56#FE<<#FBq!HakkBBL 6s   1B5+B:)r   rQ   rT   r   r   r   rX   rU   r   r   r   r   rC   r   )r   r   r   r   Lr   r&  r  r  r*  r  r   r  s   ` `      @@@@r   _invert_trig_hyp_complexr-    s&    !*++
40K0K	= #t$zzQAE4P4aHVAqt_ajjA4PQQD qvvay$77	A)	*	*z$	/J/J
	' /qvv6fd#t$		C 		C t<t!*1-t<= 4yW QN =s   &4D<Ec                    X:X  d  U[         R                  L a  X!4$ [        S5      nU R                  (       aI  U R	                  U5      u  pEU[         R
                  La#  [        U[        [        X3U-
  5      U5      U5      $ U R                  (       a  U R	                  U5      u  pEU[         R                  Lah  U[         R                  [         R                  [         R                  1;   a  U[         R                  4$ [        U[        [        X3U-  5      U5      U5      $ U R                  (       aK  U R                  u  pgUR                   (       a,  U[#        S5      :X  a  UR$                  (       a  [        XaU5      $ ['        U S5      (       a  U R)                  5       b  [+        U [,        5      (       d  [+        U [.        5      (       dz  [+        U [0        5      (       de  [3        U R                  5      S:  a  [5        S5      e[        U R                  S   [        [        X0R)                  5       " U5      5      U5      U5      $ [+        U [0        5      (       d1  U R                  (       Ga  U R6                  [         R8                  :X  Ga  [+        U[:        5      (       a  UR<                  R>                  nUR<                  R@                  n	[        SRC                  [3        U	5      5      5      n
U
4U	-   n[E        [        [        U4[F        SU
-  [H        -  [K        U5      -   -  [M        [O        U5      5      -   5      [         RP                  [3        U5      -  5      /6 n[        U R0                  X5      $ [+        U["        5      (       a  [E        U Vs/ s H`  oS:w  d  M
  [        [        U[F        SU-  [H        -  [K        U5      -   -  [M        [O        U5      5      -   5      [         RP                  5      PMb     sn6 n[        U R0                  X5      $ [+        U [,        [.        45      (       a  [S        XU5      $ X4$ s  snf )r   r   r   r   r   r   zk{}r   )*r   r   r   r   r   r   r   rU   r   r   r   NegativeInfinityComplexInfinityInfinityr   r   is_RationalrT   r   r   r   r   rQ   rC   r5   r   r   r   r   rZ   lamdaexpr	variablesformatrX   r   r   r@   r.   r?   r   r-  )r   r   r   r   r   r   r   r   	g_ys_expr	g_ys_varskg_ys_vars_1exp_invsg_ys                 r   r   r     sD    	{dajj(~c
Axx'AFF?"1hvaQ/?&FOOxx'AEE>Q''):):AJJGG1::&"1hva1~t&DfMMxxVV
 	! 4&t6::q)!8a.//a+,,aqvv;?NOOqvvay'q))+a.(A4H&R 	R !Sahhh166QVV+;dH%% 

I

,,Iell3y>23A$*Kx~1Q3r6CPYNCZ@[+.s9~+>A? )ABC**sS^O_B` b c dH"155(;;i((*.<*.3( Kxq!QqSVc#h5F2G/23s8}3= )>?@zz K*.< =H #155(;;!+-?@AA'889<s   	P;AP;c                    UR                   (       df  [        U[        S[        R                  5      5      n[        XU5      n[        U * X25      nUS   US   :w  a  [        eUS   [        US   US   5      4$ / nUR                   H]  nUR                  (       a  UR                  OUR                  nUc  UR                  U5        MB  U(       a  MK  U[        R                  4s  $    U(       a2  [        U V	s/ s H  n	[        U	[        S[         5      5      PM     sn	6 n
OSn
[#        SSS9n[        U [        [%        ['        X5      U5      [%        ['        X* 5      U5      5      U5      u  pU[)        XU5      4$ s  sn	f )a  Helper function for inverting absolute value functions.

Returns the complete result of inverting an absolute value
function along with the conditions which must also be satisfied.

If it is certain that all these conditions are met, a :class:`~.FiniteSet`
of all possible solutions is returned. If any condition cannot be
satisfied, an :class:`~.EmptySet` is returned. Otherwise, a
:class:`~.ConditionSet` of the solutions, with all the required conditions
specified, is returned.

r   r   Tr   r   )is_FiniteSetrW   rV   r   r1  r   NotImplementedErrorrX   r   	is_Numberis_nonnegativer   r   r   rR   r\   r   r   rU   r   rY   )r   r   r   pospargnargunknownr   okr   
conditionsr   g_xvaluess                 r   r   r   G  s`     4!QZZ!89AF+QB,7d1g%%Awd1gtAw/// GYY!"Q!--:NN11::%%   $Ax27 
 
cA q%t$2%#'(.0KC Sf555s   7$E:c                 h    [        U 5      [        U5      p UR                  (       a  g[        XU5      $ )a   Returns False if point p is infinite or any subexpression of f
is infinite or becomes so after replacing symbol with p. If none of
these conditions is met then True will be returned.

Examples
========

>>> from sympy import Mul, oo
>>> from sympy.abc import x
>>> from sympy.solvers.solveset import domain_check
>>> g = 1/(1 + (1/(x + 1))**2)
>>> domain_check(g, x, -1)
False
>>> domain_check(x**2, x, 0)
True
>>> domain_check(1/x, x, oo)
False

* The function relies on the assumption that the original form
  of the equation has not been changed by automatic simplification.

>>> domain_check(x/x, x, 0) # x/x is automatically simplified to 1
True

* To deal with automatic evaluations use evaluate=False:

>>> domain_check(Mul(x, 1/x, evaluate=False), x, 0)
False
F)r   is_infinite_domain_check)r   r   ps      r   domain_checkrN  x  s*    < 1:wqzq}}A&&r   c                   ^^ U R                   (       a  U R                  (       a  gU R                  TT5      R                  (       a  g[	        U [
        5      (       a_  U R                   HN  u  p4UR                  TT5      nU[        R                  L a  M,  U[        R                  L a  [        UTT5      s  $   g   g [        UU4S jU R                   5       5      $ )NTFc              3   >   >#    U  H  n[        UTT5      v   M     g 7fr   )rL  ).0r   rM  r   s     r   	<genexpr> _domain_check.<locals>.<genexpr>  s$      $"q !FA.."   )is_Atom	is_finitesubsrK  r   r=   r   r   r   r   rL  all)r   r   rM  r4  condcondsubss    ``   r   rL  rL    s    yyQ[[	
		&	&	Ay	!	! &&JDyy+H177"QVV#$T6155  !"  $FF$ $ 	$r   c                    ^ U4S jnU R                    Vs0 s H  o3[        S0 U" U5      D6_M     nnU R                  U5      R                  $ s  snf )a  
Return True if the given expression is finite. For symbols that
do not assign a value for `complex` and/or `real`, the domain will
be used to assign a value; symbols that do not assign a value
for `finite` will be made finite. All other assumptions are
left unmodified.
c                    > U R                   nUR                  SUR                  SS5      5        TR                  [        R
                  5      (       a  UR                  SS5        U$ UR                  SS5        U$ )NfiniteTr   complex)assumptions0
setdefaultgetr   r   r   )r   Ar   s     r   assumptions0_is_finite_with_finite_vars.<locals>.assumptions  sd    NN	XquuXt45AGG$$LL&
  LLD)r   r   )r   r   xreplacerV  )r   r   rc  r   repss    `   r   _is_finite_with_finite_varsrg    sL    
 12?1u&{1~&&D?::d%%% @s   Ac                   ^ ^ UR                   (       d  UR                  (       a   [        U U4S jUR                   5       5      $ UR                  (       a8  UR
                  R                  T5      (       d  [        T UR                  T5      $ gUR                  T5      (       d  g[        UT 5      (       a-   [        UR                  S   T5      nUR                  5       S:*  $ g! [         a     gf = f)a  Tests whether the equation is an equation of the given function class.

The given equation belongs to the given function class if it is
comprised of functions of the function class which are multiplied by
or added to expressions independent of the symbol. In addition, the
arguments of all such functions must be linear in the symbol as well.

Examples
========

>>> from sympy.solvers.solveset import _is_function_class_equation
>>> from sympy import tan, sin, tanh, sinh, exp
>>> from sympy.abc import x
>>> from sympy.functions.elementary.trigonometric import TrigonometricFunction
>>> from sympy.functions.elementary.hyperbolic import HyperbolicFunction
>>> _is_function_class_equation(TrigonometricFunction, exp(x) + tan(x), x)
False
>>> _is_function_class_equation(TrigonometricFunction, tan(x) + sin(x), x)
True
>>> _is_function_class_equation(TrigonometricFunction, tan(x**2), x)
False
>>> _is_function_class_equation(TrigonometricFunction, tan(x + 2), x)
True
>>> _is_function_class_equation(HyperbolicFunction, tanh(x) + sinh(x), x)
True
c              3   >   >#    U  H  n[        TUT5      v   M     g 7fr   )_is_function_class_equation)rQ  r@   
func_classr   s     r   rR  ._is_function_class_equation.<locals>.<genexpr>  s$      &$s /z3GG$rT  FTr   r   )r   r   rX  r   r   r5   r   rj  r   r   rf   rg   ri   )rk  r   r   r   s   ` ` r   rj  rj    s    6 	xx188 &ff& & 	& 	xxuuyy  .z1666JJ55==!Z  	QVVAY'A88:?"   		s   7+C$ $
C10C1c                 B   [        [        U SS9SS9n [        U 5      u  p4UR                  U5      (       d   [	        X1U5      $ [        X1U5      n[        XAU5      nXV-
  $ ! [
         a    [        U[        U S5      U5      s $ [         a    [        R                  s $ f = f)zsolve rational functionsT)	recursive)deepr   )rh   r   r)   r   _solve_as_polyr?  rY   r   rl   r   r   	_solveset)r   r   r   r   r   valid_solnsinvalid_solnss          r   _solve_as_rationalrt    s    !t,48AA;DA55==		!!V44  62!!V4** # 	:  1a&99 	::	s   A !BBBc                       \ rS rSrSrSrg)_SolveTrig1Errori  z0Raised when _solve_trig1 heuristics do not applyr   Nr   r   r   r   rv  rv    s    :r   rv  c           
         Su  p4[        U 5       HT  n[        U[        [        45      (       d  M   UR	                  U5      (       d  M8  U(       d  USpCMD  XS:X  a  US-  nMP  Su  p4  O   US:X  a  [        U SX5      u  pgXa:X  a  SnUR                  U1-
  (       a\  UR                  S   R                  USS9u  pU
R                  USS9u  pUR                  5       u  p[        US5      [        US5      -  n[        XU5      $ [        U[        U S5      U5      $ U(       ac  [        S5      nU R                  X>5      n[        XU5      n[        U[         5      (       a&  [#        U Vs/ s H  n[%        UU-
  X5      PM     sn6 $ S	n ['        XU5      nU$ s  snf ! [(         a3     [+        XU5      n U$ ! [,         a    [/        [1        S
5      5      ef = ff = f)z@Function to call other helpers to solve trigonometric equations r   r   )Fr   r   Tas_AddFr   Nzj
                Solution to this kind of trigonometric equations
                is yet to be implemented)r#   r   rQ   rC   r   r   r   r   r   r   r   rY   r   r   rW  solvesetrT   rX   _solve_trig_solve_trig1rv  _solve_trig2r   r?  rz   )r   r   r   	trig_exprcountr4  r   solrY  r   r   mr   r   r   f_covsol_covr   s                     r   r{  r{    s    I"1%d2.0 1 159XXf5E5E#'5"
#+ 	 & zAv.;D%%0 ~~a(77t7L''u'=++-#qzBsAJ.c221a&99	#Jy$5V,gy))DKLGq+ik6:GLN N C
.1f- J M  .	.q&1C
 J	  	.%j 2, '- . .	.	.s$   %FF 
G#F22GGc           
      4  ^ [        S5      n[        [        U T5      (       a<  [        U5      nUR	                  [
        R                  5      (       a  [        O[        nO[        [        U-  5      n[        n[        U 5      n U nU R                  [        [        5      nU Vs/ s H  oR                  S   PM     n	n[        U4S jU	 5       5      (       d  [        UTU5      $ / n
/ nU	 H  n [!        UT5      nUR'                  5       S:  a  [%        S5      eUR'                  5       S:X  a  ME  UR)                  5       S   nUR+                  [-        U5      S   5        U
R+                  [-        U5      S   5        M     [/        U
5      [1        U5      -  nU R3                  TX-  5      n U R5                  [        5      n [7        U 5      n [-        U 5      u  nn[        S5      nUR9                  5       UR9                  5       nnUR3                  UU5      UR3                  UU5      nnUR;                  U5      (       d  UR;                  U5      (       a  [%        S5      e[=        UU5      [=        UU5      -
  n[?        U[@        5      (       a  [%        S	5      e[?        U[B        5      (       Ga  [        S
 U 5       5      (       a  [%        S5      eUR3                  UTU-  5      n[E        U Vs/ s H  nU" UUT5      S   PM     sn6 nUR;                  [F        5      (       an  UR                  [F        5      n[-        U5      u  nnURH                  " USS06S   nURH                  " USS06S   n[K        [M        US5      [M        US5      5      nOSnU[
        RN                  L a  [A        TUU5      $ [A        TU[Q        UU5      5      $ U[
        RR                  L a  [
        RR                  $ [%        S5      es  snf ! ["         a    [%        S5      ef = fs  snf )a^  Primary solver for trigonometric and hyperbolic equations

Returns either the solution set as a ConditionSet (auto-evaluated to a
union of ImageSets if no variables besides 'symbol' are involved) or
raises _SolveTrig1Error if f == 0 cannot be solved.

Notes
=====
Algorithm:
1. Do a change of variable x -> mu*x in arguments to trigonometric and
hyperbolic functions, in order to reduce them to small integers. (This
step is crucial to keep the degrees of the polynomials of step 4 low.)
2. Rewrite trigonometric/hyperbolic functions as exponentials.
3. Proceed to a 2nd change of variable, replacing exp(I*x) or exp(x) by y.
4. Solve the resulting rational equation.
5. Use invert_complex or invert_real to return to the original variable.
6. If the coefficients of 'symbol' were symbolic in nature, add the
necessary consistency conditions in a ConditionSet.

r   r   c              3   D   >#    U  H  oR                  T5      v   M     g 7fr   )r   )rQ  r   r   s     r   rR  _solve_trig1.<locals>.<genexpr>|  s     5nuuV}}n    z!trig argument is not a polynomialr   z&degree of variable must not exceed oner   zchange of variable not possiblez$polynomial has ConditionSet solutionc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r   rj   rQ  r   s     r   rR  r    s     4ez!V$$e   z#polynomial results in RootOf objectry  FTz(polynomial solutions must form FiniteSet)*r   rj  rC   r5   r   r   r   r   invert_complexr   r*   r   rQ   r   anyrz  rf   ri   rv  rg   
all_coeffsr   r)   r%   r$   rW  rewriterh   expandr   solveset_complexr   rY   rT   rX   r    r   rR   r   r   rW   r   )r   r   r   r   covinverter
f_originaltrig_functionsr)  trig_argumentsdenominators
numeratorsarpoly_arcmur   r   r   solnsr   resultsymsmunummudencondnumconddenrY  s    `                          r   r|  r|  X  s   , 	c
A"#5q&AA!f"("2"2177";";;!A#h!AJWW24FGN)78AffQiN8 5n555
FF33LJ	H2v&G >>a"#KLL>>q  #(1+a.)HQKN+  
\	3z?	*B	vrtA			#AAA;DAqc
A88:qxxzqA66#q>166#q>qAuuQxx15588@AAQ"%5a%;;E%&&EFF%##4e444"#HIIhhq&)$eDe#q&1!4eDE 66&>>::f%D#B<LE5**D??BG**D??BGr'1~r'1~6DD Q[[   f55l66.JKK	!**	zzIJJA 9  	H"#FGG	H< Es   "O7-O<-P<Pc                 2   [        U 5      n U nU R                  [        [        [        [
        [        [        5      nU Vs/ s H  oUR                  S   PM     nn/ n/ nU(       d  [        U[        US5      U5      $ U H  n	 [        X5      n
U
R                  5       S:  a  [        S5      eU
R                  5       S:X  a  MD  U
R                  5       S   n UR!                  [#        U5      R$                  5        UR!                  [#        U5      R&                  5        M     [+        S5      n[#        S5      [-        U6 -  [/        U6 -  nU R1                  XU-  5      n U R3                  [        5      n [5        U 5      n [7        U 5      n [9        U 5      u  p[+        S5      nUR;                  5       UR;                  5       pUR1                  [	        U5      U5      UR1                  [	        U5      U5      pUR=                  U5      (       d  UR=                  U5      (       a  [        U[        US5      U5      $ [?        UU[@        RB                  5      [?        UU[@        RB                  5      -
  n[E        U[F        5      (       a  [I        U Vs/ s H  n[K        [	        X-  5      UU5      S   PM      sn6 n[K        [	        X-  5      [L        U5      S   n[        U5      [        U5      :  a  [I        UU5      n[O        UU5      $ U[@        RP                  L a  [@        RP                  $ [        U[        US5      [@        RB                  5      $ s  snf ! [         a    [        S5      ef = f! [(         a    [        U[        US5      U5      s s  $ f = fs  snf )zRSecondary helper to solve trigonometric equations,
called when first helper fails r   z9give up, we cannot solve if this is not a polynomial in xr   z:degree of variable inside polynomial should not exceed oner   r   r   ))r*   r   r1   r2   r/   r3   r0   r4   r   rY   r   rf   ri   r   rg   r  r   r   rM  q	TypeErrorr   
number_lcm
number_gcdrW  r  r   rh   r)   r  r   rz  r   r   r   rT   rX   r   r   rW   r   )r   r   r   r  r  r)  r  r  r  r  r  r  r   r  r   r   r   r  r   r  dsols                        r   r}  r}    s    	AJWWS#sCc:N)78AffQiN8LJ
 FBz1$5v>>
 	Z2&G >>aYZZ>>q  #	Chqkmm,.   	c
A	!Z.	.z:/F	FB	v!tA			#AAAAA;DAc
A88:qxxzq66#a&!affSVQ/quuQxx15588FBz1$5v>>Q177#hq!QWW&==E%## %' %1 %S^Q?B %' (3vy>2v6q9!9vay 64(FFF++	!**	zzFBz1$5qww??q 9   	ZXYY	Z  	C:q(96BB	C*'s+    MMAM+%NM(+!NNc           	         SnU R                  U5      (       a  [        XSSSSS9n[        UR                  5       5      n[	        X5      U::  a  [        UR                  5       6 nGOj[        X5      nUR                  " 5       nUR                  " 5       [        U5      ::  a
  [        U6 nGO&[        U[        U S5      U5      nGO[        U 5      nUc  [        U[        U S5      U5      nUR                   Vs/ s H  owR                  U5      (       d  M  UPM     nn[        U5      S:X  Ga  [        XhS   5      nUR                  n	UR                  " 5       n
[        UR                  " 5       UR                  SS9n[        [        USSSS9R                  5       6 n[        U5      U
:  a  [        U[        U S5      U5      nX:w  a  [!        S	5      nUR#                  [$        R&                  5      (       a  [(        O[*        nU" XU5      u  pX:X  a  [-        U Vs/ s H  nUR/                  UU5      PM     sn6 n[1        U[
        5      (       aR  [1        U	[2        5      (       a=  U	R4                  R6                  (       a"  [        U Vs/ s H  n[9        U5      PM     sn6 nO/[        U[        U S5      U5      nO[        U[        U S5      U5      nUb  [1        U[
        5      (       aA  [;        S
 U 5       5      (       a*  [!        S5      n[=        [?        U[A        U5      5      U5      n[1        U[
        5      (       a%  U[$        RB                  :w  a  URE                  U5      nU$ [        U[        U S5      U5      $ s  snf s  snf s  snf )z
Solve the equation using polynomial techniques if it already is a
polynomial equation or, with a change of variables, can be made so.
NTEX)cubicsquarticsquinticsr   r   r   )	composite)r  r  r  r   c              3      #    U  HF  nUR                  [        [        5      [        5       :H  =(       a    [	        U[
        5      (       + v   MH     g 7fr   )r   r    r   setr   rj   r  s     r   rR  !_solve_as_poly.<locals>.<genexpr>5  s<      $"q 776<0CE9W*QPVBW>WW"s   AAr   )#is_polynomialre   sumrI  rg   rT   keysrf   	all_rootsr   rY   r   gensr   genas_exprr   r   r   r   r   r  rX   rW  r   r   r   r2  r   rX  rU   r   r   r   intersection)r   r   r   r  r  	num_rootsro   r   r  r  deg
poly_solnsr   r  lhsrhs_sr   r   s                     r   rp  rp    s   
 Fvat#D2'	!	)

-F?DNN$E{{}E
*"E*%fbAh?Aw<!&"Q(F;F9969af96t9>1g&D((C++-CDAD"E$td37%99=AJ :$%fbAh?}#J*0*:*:177*C*C;%cf5
="z$Jz!UZZ1%5z$JKF!&)44C : :"%(("6"6!*F,KFqZ]F,K!L)&"Q(FCF!&"Q(F;Ffi((
  $"$ $ $#J!&N1,=">Gfi((Vq{{-B ((0FFBq!Hf55W 7$ %K -Ls   6M<M<NNc                   ^ ^^^ UnU(       a  UOT / 4u  pVU(       d4  U" UT5      [        [        T T5       Vs/ s H  os" UT5      PM     sn6 -
  nOUu  pU" U	[        -
  U	5      (       d0  [        SSS9nU
R	                  X05      n
UR	                  X05      nUn	U" U
T5      nU" XY5      n[        U Vs/ s H  n[        [        X5      U5      PM     sn6 nU U4S jmUU4S jmT" U5      nU$ s  snf s  snf )z1Helper function to solve equations with radicals yrealTr   c                    > / n/ nU  H9  n[        TTU5      (       a  UR                  U5        M(  UR                  U5        M;     [        U6 [        T[	        TS5      [        U6 5      -   $ r   )rt   r   rT   rY   r   )	solutionsf_setc_setr   r   r   s       r   check_finiteset'_solve_radical.<locals>.check_finitesetU  sb    A61%%QQ	 
 % <1a)UBS#TTTr   c                   > U [         R                  L a  U $ [        U [        5      (       a  U $ [        U [        5      (       a  T" U 5      $ [        U [
        5      (       a   U R                  u  p[        T" U5      U5      $ [        U [        5      (       a)  [        U R                   Vs/ s H  nT" U5      PM     sn6 $ U $ s  snf r   )r   r   r   rY   rT   r[   r   rX   )r  rb  Br   r  	check_sets       r   r  !_solve_radical.<locals>.check_set_  s    

"	<00	9--"9--	:..>>DAilA..	5))@A9Q<@AA 	 As   (C )rX   ru   r   r   re  rU   r   )r   unradfr   solveset_solverr   eqr  r   r  r   yeqr  g_y_sf_y_solsr<  solution_setr  r  s   ` `             @@r   _solve_radicalr  B  s   
Ccq"gGB V,q&8IJ8I1OAv.8IJKL q1ua(('-E,,z*CaZ(BAV,"2)$)+$)S "&.(;$)+ ,U$ V$LW K+s   C/
4C4c                    UR                  [        R                  5      (       d  [        [	        S5      5      e[        S5      [        S5      [        S5      pTnU R                  U[        U5      -  U-   5      =(       d    0 nX4U4 Vs/ s H"  ovR                  U[        R                  5      PM$     snu  pn
UR                  (       d  U	R                  (       d|  [        XU5      nSSKJn  U" U	S:  USUS	S
9nUR                  U5      n[        X-  U
-   U5      R!                  U5      n[        X* -  U
-   U5      R!                  U5      n[#        X5      $ [%        U['        U S5      U5      $ s  snf )zDHelper function to solve equation involving absolute value function zR
            Absolute values cannot be inverted in the
            complex domain.rM  r  rr   solve_univariate_inequalityr   FT)
relationalr   
continuous)r   r   r   r   rz   r	   matchr?   ra  r   r   r   inequalitiesr  
complementsolveset_realr   rX   rY   r   )r   r   r   rM  r  r  pattern_matchr   f_pf_qf_rr  
q_pos_cond
q_neg_cond
sols_q_pos
sols_q_negs                   r   
_solve_absr  v  sG   AGG$$ %   ! 	! 3icDI!AGGAc!fHqL)/RM<=!9E9a&&q!&&19EMCcKK3;;"37=06<A&]ac
**62
"37S=+133<9Z3H 	"3:#3+133<9Z3H 	Z,,FBq!Hf55 Fs   )E,c           
      2   SSK Jn  U" X5      n[        S5      nU GH  n[        XaU5      n[	        Xu5      n[
        R                  n[        U[        5      (       a;  U H3  n	[        [        Xi5      X5      n
[        U
[        5      (       a  M/  X-  nM5     GOM[        U[        5      (       a  U4nOG[        U[        5      (       a  UR                  nO%U[
        R                  L a  [
        R                  s  $ W H  n[        [        UR                  R                  U5      X5      n[!        UR                  R                  R"                  5      S   nUR$                  u  n[        U[        5      (       a  UnOF[        U[        5      (       a1  [        UR                  S   [        5      (       a  UR                  S   nW H  nU[        ['        UU5      U5      -  nM     M     U[
        R                  L a  [        U[        U S5      U5      s  $ UnGM     U$ )aG  
Function to solve equations via the principle of "Decomposition
and Rewriting".

Examples
========
>>> from sympy import exp, sin, Symbol, pprint, S
>>> from sympy.solvers.solveset import solve_decomposition as sd
>>> x = Symbol('x')
>>> f1 = exp(2*x) - 3*exp(x) + 2
>>> sd(f1, x, S.Reals)
{0, log(2)}
>>> f2 = sin(x)**2 + 2*sin(x) + 1
>>> pprint(sd(f2, x, S.Reals), use_unicode=False)
          3*pi
{2*n*pi + ---- | n in Integers}
           2
>>> f3 = sin(x + 2)
>>> pprint(sd(f3, x, S.Reals), use_unicode=False)
{2*n*pi - 2 | n in Integers} U {2*n*pi - 2 + pi | n in Integers}

r   )
decompogenr   )sympy.solvers.decompogenr  rT   r   rW   r   r   r   rz  r   rY   rZ   rX   r   r3  r4  tupler   	base_setsr   )r   r   r   r  g_sy_sr   franger  r   r  	iter_isetisetnew_solutions	dummy_varbase_set	new_exprsnew_exprs                     r   solve_decompositionr    s   . 4
Q
C A,C626'c9%%$RXv>	!)\::'F  #x(( F	C''HH	

" zz!! (DJJOOQ)? P!$**//">">?B	"nnmY77 -I|<<!-"4"4Q"7CC$1$6$6q$9	 )Hhvi'BHMMF !* " QZZ1a&99O R Jr   Fc           
      t  ^ SSK Jn  [        U [        5      (       a  T$ U nU R                  (       aQ  U R                  USS9u  p`U[        R                  [        R                  [        R                  1;   a  [        U5      n OU R                  (       an  U R                  U5      u  pxUR                  USS9u  pU	[        R                  [        R                  [        R                  [        R                  1;  a  Xy-  U-   n T4S jn
U4S jn[        R                  nU R                  5       R                  (       a  T$ U R!                  U5      (       d  [        R                  $ U R                  (       aN  [#        U4S jU R$                   5       5      (       a*  ['        U R$                   V	s/ s H
  o" X5      PM     sn	6 nGO[)        [*        X5      (       d  [)        [,        X5      (       a  [/        XT5      nGOQ[        U [0        5      (       aH  U R$                  S   n[3        [5        [7        U5      S:  UT5      [5        [9        U5      UT5      5      nGOU R:                  (       aM  U R=                  T5      nU H4  u  pUR>                  (       a  URA                  5       nU
" XU5      nUU-  nM6     GO[        U [B        5      (       a*  U
" [E        U RF                  U RH                  * SS9UT5      nGOWU R>                  (       a  S	S
K%J&n   U" XTSS9nU$ [S        X5      (       a  [U        XT5      nGOU" U SU5      u  nnUU:X  aL  [        U[V        5      (       a3  [W        U Vs/ s H  n[Y        U" U5      R[                  5       6 PM!     sn6 nUnGO[        U[V        5      (       Gax  U Vs/ s H  nUU-
  PM
     sn GH\  nUU :X  GaF  []        X5      nU(       a  U[_        UUUU
5      -  nM0  UR!                  [`        5      (       a  U[c        XT5      -  nM[  [e        UUT5      n[        U[P        5      (       d  UU-  nM  [g        UUT5      n[        U[P        5      (       a  URi                  5       nUR                  (       a  UU:w  a|  UR                  U5      u  nnUR                  (       dW  / n[X        Rj                  " U5       H3  nUR!                  U5      (       d  M  URm                  U
" UU5      5        M5     ['        U6 nUU-  nGMQ  X" UU5      -  nGM_     O*U[        R                  La  [Q        U[C        U S5      T5      n[        U[P        5      (       ar  [        U [n        5      (       a]  U Rq                  5       u  nn U R!                  U5      (       a4  [5        UUT5      n![        U![P        5      (       d  [5        U UT5      n"U!U"-
  nU(       a  [        U[P        5      (       a  U$ [        U[n        5      (       a'  UR                  USS9S	   n#U#R                  USS9S	   n#OUn#[        U[V        5      (       aB  [W        U V$s/ s H.  n$[        U$[r        5      (       d  [u        U#UU$5      (       d  M,  U$PM0     sn$6 nU$ s  sn	f ! [N         a    [Q        XT5      n U$ f = fs  snf s  snf s  sn$f )zHelper for solveset to return a result from an expression
that has already been sympify'ed and is known to contain the
given symbol.r   )signsimpFrx  c                     [        XU5      $ r   )rq  )r   r   r   s      r   r   _solveset.<locals>.<lambda>  s    1)@r   c                    > [        XUT5      $ r   )r   )r   r   r   r   s      r   r   r    s    gaff&Er   c              3   <   >#    U  H  n[        UT5      v   M     g 7fr   )rg  )rQ  r  r   s     r   rR  _solveset.<locals>.<genexpr>  s"       6a@@   r   r   r  )r   r  T);sympy.simplify.simplifyr  r   rS   r   r   r   r0  r/  r1  rh   r   r   r   r  r   r   rX  r   rX   rj  rQ   rC   r{  r@   rW   rq  rA   rB   is_Piecewiseas_expr_set_pairsis_Relationalas_setr   r   r  r   r  r  r?  rY   _is_modular_solve_modularrT   r
   as_content_primitiverv   r  r?   r  rt  
_transolverk   	make_argsr   r   r   rj   rN  )%r   r   r   _checkr  orig_fcoeffr   r   r  solverr  r  expr_set_pairsr4  in_setr  r  r  r  r   r   equationuresult_rationalt_resultfactored_dep	t_resultsfacr   r   _resultsingularitiesfxr   s%     `                                  r   rq  rq    s   
 1![!!Fxx##F5#9Q&&(:(:AJJGG A	
'u5Q&&

002 2aA "(@FEHZZFxxzUU6]]zz	
c VV   AFF;Fq*F;<
%&;Q
G
G'(:AFFQ/	As		FF1Ii1	66B'1vv>@	
,,V4,NT##40EeOF	 -
 
Ar		AEEAEE6E:FFK	
=	50f8F 	Q		62aF+
U&= %++!"$$" %(QK446%8"$$ % Fy))278%3S3Y%8q=a(A.11717#9 9 "c***Q"??*<Xvv*V)/<HH"o5F (2(FF'KH)(LAA ,4??+<#+??x87K-5-D-DV-LFAs+.:: 57	36==3JC/2wwv090@0@VAT0U 4K 49)3D"h.FfXv66FK 9N !**$!&"Q(F;F&,''a'')HCwwv#C8!'<88$-c66$BM$}4Ffl++ M fd##&&vd&;A>B""6%"8;BBfi((F !6Fq#Av..%b&!4 "#F !6 7F Mi <. # 	5!&V4F	5 $$ 9F!6s0   -Z	Z '&Z+/Z0+Z5=Z5Z('Z(c                 |   U R                  [        5      (       d  g[        U R                  [        5      5      n[	        U5      S:H  =(       ao    US   R
                  S   R                  U5      =(       aG    US   R
                  S   R                  =(       a$    [        S [        [        U 5      5       5       5      $ )a  
Helper function to check below mentioned types of modular equations.
``A - Mod(B, C) = 0``

A -> This can or cannot be a function of symbol.
B -> This is surely a function of symbol.
C -> It is an integer.

Parameters
==========

f : Expr
    The equation to be checked.

symbol : Symbol
    The concerned variable for which the equation is to be checked.

Examples
========

>>> from sympy import symbols, exp, Mod
>>> from sympy.solvers.solveset import _is_modular as check
>>> x, y = symbols('x y')
>>> check(Mod(x, 3) - 1, x)
True
>>> check(Mod(x, 3) - 1, y)
False
>>> check(Mod(x, 3)**2 - 5, x)
False
>>> check(Mod(x, 3)**2 - y, x)
False
>>> check(exp(Mod(x, 3)) - 1, x)
False
>>> check(Mod(3, y) - 1, y)
False
Fr   r   c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r   r   rQ  terms     r   rR  _is_modular.<locals>.<genexpr>  s!      0. 4%%.r  )	r   r   r   r   r   r   
is_integerr  _term_factors)r   r   modtermss      r   r  r  {  s    L 55:: AGGCL!HMQ 0QKQ##F+0QKQ**0  0]1-.0 0r   c           
         U R                   u  pEUR                  SL a  U[        R                  4$ UR                  SL d)  [        S [        [        U5      5       5       5      (       a  X4$ [        U5      [        U5      :  a  U[        R                  4$ XC:X  a+  U[        [        X%U-  U-   5      [        R                  5      4$ UR                  (       aS  UR                  U5      u  pgU[        R                  La-  U[        Xe5      -
  n[!        [        Xu5      [        X5      X#5      $ UR"                  (       aS  UR                  U5      u  pgU[        R$                  La-  U['        Xe5      -  n[!        [        Xu5      [        X5      X#5      $ UR(                  (       Ga  UR                   u  pU
R+                  U5      (       Ga@  U	R+                  U5      (       Gd)  UR,                  (       d/  UR,                  (       a  UR.                  R,                  (       a  X4$ UR0                  [3        UR0                  UR0                  5      -  n [5        XR0                  UR.                  R0                  5      n[9        U5      n[;        U5       HL  n[=        UR.                  X[3        UR0                  UR.                  R0                  5      -  5      S:X  d  MJ  Un  O   U
[        [        X-U-  U-   5      [        R>                  5      4$ U	R+                  U5      (       a  U
R+                  U5      (       dn   [A        XUSS9nU/ :X  a  U[        R                  4$  [        R                  nU H/  nU[        [        X%U-  U-   5      [        R                  5      -  nM1     U	U4$ X4$ ! [6         a    X4s $ f = f! [6        [B        4 a    X4s $ f = f)a
  
Helper function to invert modular equation.
``Mod(a, m) - rhs = 0``

Generally it is inverted as (a, ImageSet(Lambda(n, m*n + rhs), S.Integers)).
More simplified form will be returned if possible.

If it is not invertible then (modterm, rhs) is returned.

The following cases arise while inverting equation ``Mod(a, m) - rhs = 0``:

1. If a is symbol then  m*n + rhs is the required solution.

2. If a is an instance of ``Add`` then we try to find two symbol independent
   parts of a and the symbol independent part gets transferred to the other
   side and again the ``_invert_modular`` is called on the symbol
   dependent part.

3. If a is an instance of ``Mul`` then same as we done in ``Add`` we separate
   out the symbol dependent and symbol independent parts and transfer the
   symbol independent part to the rhs with the help of invert and again the
   ``_invert_modular`` is called on the symbol dependent part.

4. If a is an instance of ``Pow`` then two cases arise as following:

    - If a is of type (symbol_indep)**(symbol_dep) then the remainder is
      evaluated with the help of discrete_log function and then the least
      period is being found out with the help of totient function.
      period*n + remainder is the required solution in this case.
      For reference: (https://en.wikipedia.org/wiki/Euler's_theorem)

    - If a is of type (symbol_dep)**(symbol_indep) then we try to find all
      primitive solutions list with the help of nthroot_mod function.
      m*n + rem is the general solution where rem belongs to solutions list
      from nthroot_mod function.

Parameters
==========

modterm, rhs : Expr
    The modular equation to be inverted, ``modterm - rhs = 0``

symbol : Symbol
    The variable in the equation to be inverted.

n : Dummy
    Dummy variable for output g_n.

Returns
=======

A tuple (f_x, g_n) is being returned where f_x is modular independent function
of symbol and g_n being set of values f_x can have.

Examples
========

>>> from sympy import symbols, exp, Mod, Dummy, S
>>> from sympy.solvers.solveset import _invert_modular as invert_modular
>>> x, y = symbols('x y')
>>> n = Dummy('n')
>>> invert_modular(Mod(exp(x), 7), S(5), n, x)
(Mod(exp(x), 7), 5)
>>> invert_modular(Mod(x, 7), S(5), n, x)
(x, ImageSet(Lambda(_n, 7*_n + 5), Integers))
>>> invert_modular(Mod(3*x + 8, 7), S(5), n, x)
(x, ImageSet(Lambda(_n, 7*_n + 6), Integers))
>>> invert_modular(Mod(x**4, 7), S(5), n, x)
(x, EmptySet)
>>> invert_modular(Mod(2**(x**2 + x + 1), 7), S(2), n, x)
(x**2 + x + 1, ImageSet(Lambda(_n, 3*_n + 1), Naturals0))

Fc              3   >   #    U  H  nUR                   S L v   M     g7f)FN)is_realr  s     r   rR  "_invert_modular.<locals>.<genexpr>  s       #0. $(<<5#8.s   r   T)r  )"r   r  r   r   r$  r  r   r   absrZ   r   r   r   r   r   r   _invert_modularr   r   rm   r   r   
is_Integerr   rM  r  rc   r   r>   rb   pow	Naturals0rd   r?  )modtermr   r   r   r   r  r   r   x_indep_termr   r   mdiv	remainderr  rM  remainder_listg_nrems                     r   r'  r'    s   T <<DA
~~qzz!!
{{es #0]1-.#0  0  0 |
3x3q6qzz!!{xqA#) 4ajjAAAxx'AFF?Q?L"3q9c,.BANNxx'AEE>va|+L"3q9c,.BANNxxxVV
88FDHHV$4$4 <<CNNqvv7H7H|#33*QSS#%%00D$(uuaffhh?	 QZFf%qvvqjaffhh&?"?@AEF	 & &1Hy,@"A1;;OOOXXfdhhv&6&6$!,ST!J!R'!1::-- ( **C%xqA#) 4ajjAA &9<9  $|#$*  34 $|#$s$   	*N! 1"N4 !N10N14O
	O
c                    [        U[        U S5      U5      n[        U R                  [        5      5      S   n[
        R                  * U R                  U[
        R                  5      -  nU R                  5       U   R                  (       a  U[
        R                  * -  nUR                  [
        R                  5      (       d  U$ UR                  U5      (       a  U$ [        SSS9n[        XEXa5      u  pxXt:X  a  X:X  a  U$ Xq:X  a&  U[
        R                  La  UR!                  U5      $ U$ [#        U[$        5      (       a  UR&                  R(                  n	UR&                  R*                  n
UR,                  n[/        Xy-
  U[
        R                  5      n[#        U[0        5      (       a4  [
        R2                  nU H  nU[%        [5        X5      /UQ76 -  nM     UnO[%        [5        X5      /UQ76 nUR!                  U5      $ U$ )a  
Helper function for solving modular equations of type ``A - Mod(B, C) = 0``,
where A can or cannot be a function of symbol, B is surely a function of
symbol and C is an integer.

Currently ``_solve_modular`` is only able to solve cases
where A is not a function of symbol.

Parameters
==========

f : Expr
    The modular equation to be solved, ``f = 0``

symbol : Symbol
    The variable in the equation to be solved.

domain : Set
    A set over which the equation is solved. It has to be a subset of
    Integers.

Returns
=======

A set of integer solutions satisfying the given modular equation.
A ``ConditionSet`` if the equation is unsolvable.

Examples
========

>>> from sympy.solvers.solveset import _solve_modular as solve_modulo
>>> from sympy import S, Symbol, sin, Intersection, Interval, Mod
>>> x = Symbol('x')
>>> solve_modulo(Mod(5*x - 8, 7) - 3, x, S.Integers)
ImageSet(Lambda(_n, 7*_n + 5), Integers)
>>> solve_modulo(Mod(5*x - 8, 7) - 3, x, S.Reals)  # domain should be subset of integers.
ConditionSet(x, Eq(Mod(5*x + 6, 7) - 3, 0), Reals)
>>> solve_modulo(-7 + Mod(x, 5), x, S.Integers)
EmptySet
>>> solve_modulo(Mod(12**x, 21) - 18, x, S.Integers)
ImageSet(Lambda(_n, 6*_n + 2), Naturals0)
>>> solve_modulo(Mod(sin(x), 7) - 3, x, S.Integers) # not solvable
ConditionSet(x, Eq(Mod(sin(x), 7) - 3, 0), Integers)
>>> solve_modulo(3 - Mod(x, 5), x, Intersection(S.Integers, Interval(0, 100)))
Intersection(ImageSet(Lambda(_n, 5*_n + 3), Integers), Range(0, 101, 1))
r   r   Tr   )rY   r   r   r   r   r   r   rW  r   as_coefficients_dictr   r   r   r   r   r'  r   r   rZ   r3  r4  r5  r  rq  rT   r   r   )r   r   r   unsolved_resultr+  r   r   r   r0  
lamda_expr
lamda_varsr  sol_settmp_solr  s                  r   r  r  C  s   ` #62a8V<O1773< #G55&!&&!&&)
*C(44vAJJ''
wwv c4 AwQ7HC
~#*
}###C((
#x  YY^^
YY((
MM	C,fajjAgy))jjG8F:$;HiHH Gz ;HiHG((r   c              #      #    [         R                  " U 5       H!  n[        R                  " U5       Sh  vN   M#     g N	7f)a  
Iterator to get the factors of all terms present
in the given equation.

Parameters
==========
f : Expr
    Equation that needs to be addressed

Returns
=======
Factors of all terms present in the equation.

Examples
========

>>> from sympy import symbols
>>> from sympy.solvers.solveset import _term_factors
>>> x = symbols('x')
>>> list(_term_factors(-2 - x**2 + x*(x + 1)))
[-2, -1, x**2, x, x + 1]
N)r   r  r
   )r   add_args     r   r   r     s/     . ==#==))) $)s   3A?
Ac           
      .   [        U[        X-
  S5      U5      n[        U 5      nX:w  a!  [        XQ-
  5      nX`U-
  :w  a  [	        XbU5      $ [        U [        5      (       a  [        U R                  5      S:X  d  U$ US:w  a  U$ [        [        U R                  5      5      u  pxUR                  U5      S   n	UR                  U5      S   n
U	R                  5       u  pU
R                  5       u  pUR                  [        R                  5      (       a;  [!        US:  US:  [        [#        U5      S5      [        [#        U5      S5      5      nO [!        [%        US5      [%        US5      5      nS Xx* 4 5       u  nn[	        UU-
  X#5      n[        X/U5      $ )a  
Helper function for solving (supported) exponential equations.

Exponential equations are the sum of (currently) at most
two terms with one or both of them having a power with a
symbol-dependent exponent.

For example

.. math:: 5^{2x + 3} - 5^{3x - 1}

.. math:: 4^{5 - 9x} - e^{2 - x}

Parameters
==========

lhs, rhs : Expr
    The exponential equation to be solved, `lhs = rhs`

symbol : Symbol
    The variable in which the equation is solved

domain : Set
    A set over which the equation is solved.

Returns
=======

A set of solutions satisfying the given equation.
A ``ConditionSet`` if the equation is unsolvable or
if the assumptions are not properly defined, in that case
a different style of ``ConditionSet`` is returned having the
solution(s) of the equation with the desired assumptions.

Examples
========

>>> from sympy.solvers.solveset import _solve_exponential as solve_expo
>>> from sympy import symbols, S
>>> x = symbols('x', real=True)
>>> a, b = symbols('a b')
>>> solve_expo(2**x + 3**x - 5**x, 0, x, S.Reals)  # not solvable
ConditionSet(x, Eq(2**x + 3**x - 5**x, 0), Reals)
>>> solve_expo(a**x - b**x, 0, x, S.Reals)  # solvable but incorrect assumptions
ConditionSet(x, (a > 0) & (b > 0), {0})
>>> solve_expo(3**(2*x) - 2**(x + 3), 0, x, S.Reals)
{-3*log(2)/(-2*log(3) + log(2))}
>>> solve_expo(2**x - 4**x, 0, x, S.Reals)
{0}

* Proof of correctness of the method

The logarithm function is the inverse of the exponential function.
The defining relation between exponentiation and logarithm is:

.. math:: {\log_b x} = y \enspace if \enspace b^y = x

Therefore if we are given an equation with exponent terms, we can
convert every term to its corresponding logarithmic form. This is
achieved by taking logarithms and expanding the equation using
logarithmic identities so that it can easily be handled by ``solveset``.

For example:

.. math:: 3^{2x} = 2^{x + 3}

Taking log both sides will reduce the equation to

.. math:: (2x)\log(3) = (x + 3)\log(2)

This form can be easily handed by ``solveset``.
r   r   r   c              3   H   #    U  H  n[        [        U5      S S9v   M     g7f)TforceN)r   r.   rQ  r   s     r   rR  %_solve_exponential.<locals>.<genexpr>$  s     <GqJs1vT*Gs    ")rY   r   r,   rk   rq  r   r   r   r   r   r   r   as_base_expr   r   r   rR   rB   r   )r  r   r   r   r4  newlhsneweqr   r   a_termb_terma_basea_expb_baseb_exprG  r,  Rr  s                      r   _solve_exponentialrK    su   R #62ci+;VDOs^F
}v|$3YUF33sC  S]a%7 
ax!"DAf%a(Ff%a(F&&(MF&&(MF  QJQJr%y!r%y!	
 vqMvqM
 =QG<DAq!a%0II66r   c                     Sn[        U 5       H[  nXR                  ;  a  M  [        U[        5      (       a  XR                  R                  ;  d  [        U[
        5      (       a  SnM[    g   U$ )a  
Return ``True`` if one or more terms contain ``symbol`` only in
exponents, else ``False``.

Parameters
==========

f : Expr
    The equation to be checked

symbol : Symbol
    The variable in which the equation is checked

Examples
========

>>> from sympy import symbols, cos, exp
>>> from sympy.solvers.solveset import _is_exponential as check
>>> x, y = symbols('x y')
>>> check(y, y)
False
>>> check(x**y - 1, y)
True
>>> check(x**y*2**y - 1, y)
True
>>> check(exp(x + 3) + 3**x, x)
True
>>> check(cos(2**x), x)
False

* Philosophy behind the helper

The function extracts each term of the equation and checks if it is
of exponential form w.r.t ``symbol``.
FT)r   r   r   r   r   r5   )r   r   rvexpr_args       r   _is_exponentialrO  *  sa    H 
B!!$...x%%333h$$B % Ir   c                 6    [        U SS9nXA-
  n[        XRU5      $ )aa  
Helper to solve logarithmic equations which are reducible
to a single instance of `\log`.

Logarithmic equations are (currently) the equations that contains
`\log` terms which can be reduced to a single `\log` term or
a constant using various logarithmic identities.

For example:

.. math:: \log(x) + \log(x - 4)

can be reduced to:

.. math:: \log(x(x - 4))

Parameters
==========

lhs, rhs : Expr
    The logarithmic equation to be solved, `lhs = rhs`

symbol : Symbol
    The variable in which the equation is solved

domain : Set
    A set over which the equation is solved.

Returns
=======

A set of solutions satisfying the given equation.
A ``ConditionSet`` if the equation is unsolvable.

Examples
========

>>> from sympy import symbols, log, S
>>> from sympy.solvers.solveset import _solve_logarithm as solve_log
>>> x = symbols('x')
>>> f = log(x - 3) + log(x + 3)
>>> solve_log(f, 0, x, S.Reals)
{-sqrt(10), sqrt(10)}

* Proof of correctness

A logarithm is another way to write exponent and is defined by

.. math:: {\log_b x} = y \enspace if \enspace b^y = x

When one side of the equation contains a single logarithm, the
equation can be solved by rewriting the equation as an equivalent
exponential equation as defined above. But if one side contains
more than one logarithm, we need to use the properties of logarithm
to condense it into a single logarithm.

Take for example

.. math:: \log(2x) - 15 = 0

contains single logarithm, therefore we can directly rewrite it to
exponential form as

.. math:: x = \frac{e^{15}}{2}

But if the equation has more than one logarithm as

.. math:: \log(x - 3) + \log(x + 3) = 0

we use logarithmic identities to convert it into a reduced form

Using,

.. math:: \log(a) + \log(b) = \log(ab)

the equation becomes,

.. math:: \log((x - 3)(x + 3))

This equation contains one logarithm and can be solved by rewriting
to exponents.
Tr=  )r-   rq  )r  r   r   r   new_lhsnew_fs         r   _solve_logarithmrS  [  s%    f D)GMEUF++r   c                    Sn[         R                  " U 5       Hb  nSn[        R                  " U5       H8  nXR                  ;  a  M  [	        U[
        5      (       a  U(       a      gSnM7      g   U(       d  M`  SnMd     U$ )a  
Return ``True`` if the equation is in the form
`a\log(f(x)) + b\log(g(x)) + ... + c` else ``False``.

Parameters
==========

f : Expr
    The equation to be checked

symbol : Symbol
    The variable in which the equation is checked

Returns
=======

``True`` if the equation is logarithmic otherwise ``False``.

Examples
========

>>> from sympy import symbols, tan, log
>>> from sympy.solvers.solveset import _is_logarithmic as check
>>> x, y = symbols('x y')
>>> check(log(x + 2) - log(x + 3), x)
True
>>> check(tan(log(2*x)), x)
False
>>> check(x*log(x), x)
False
>>> check(x + log(x), x)
False
>>> check(y + log(x), x)
True

* Philosophy behind the helper

The function extracts each term and checks whether it is
logarithmic w.r.t ``symbol``.
FT)r   r  r
   r   r   r.   )r   r   rM  r  saw_logterm_args         r   _is_logarithmicrW    sr    R 
Ba d+H222(C((  , 7B ! Ir   c           	        ^^ [        [        U R                  5       5      5      n[        U Vs/ s H  o3R                  " T5      (       d  M  UPM     sn5      n[        U Vs/ s H'  nUR                  " [
        [        5      (       d  M%  UPM)     sn5      nU R                  (       aU  US:  aO  [        [
        [        4m[        UU4S jU 5       5      (       a  XT:  a  g g[        U4S jU 5       5      (       a  ggs  snf s  snf )a  
If this returns ``False`` then the Lambert solver (``_solve_lambert``) will not be called.

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

Quick check for cases that the Lambert solver might be able to handle.

1. Equations containing more than two operands and `symbol`s involving any of
   `Pow`, `exp`, `HyperbolicFunction`,`TrigonometricFunction`, `log` terms.

2. In `Pow`, `exp` the exponent should have `symbol` whereas for
   `HyperbolicFunction`,`TrigonometricFunction`, `log` should contain `symbol`.

3. For `HyperbolicFunction`,`TrigonometricFunction` the number of trigonometric functions in
   equation should be less than number of symbols. (since `A*cos(x) + B*sin(x) - c`
   is not the Lambert type).

Some forms of lambert equations are:
    1. X**X = C
    2. X*(B*log(X) + D)**A = C
    3. A*log(B*X + A) + d*X = C
    4. (B*X + A)*exp(d*X + g) = C
    5. g*exp(B*X + h) - B*X = C
    6. A*D**(E*X + g) - B*X = C
    7. A*cos(X) + B*sin(X) - D*X = C
    8. A*cosh(X) + B*sinh(X) - D*X = C

Where X is any variable,
      A, B, C, D, E are any constants,
      g, h are linear functions or log terms.

Parameters
==========

f : Expr
    The equation to be checked

symbol : Symbol
    The variable in which the equation is checked

Returns
=======

If this returns ``False`` then the Lambert solver (``_solve_lambert``) will not be called.

Examples
========

>>> from sympy.solvers.solveset import _is_lambert
>>> from sympy import symbols, cosh, sinh, log
>>> x = symbols('x')

>>> _is_lambert(3*log(x) - x*log(3), x)
True
>>> _is_lambert(log(log(x - 3)) + log(x-3), x)
True
>>> _is_lambert(cosh(x) - sinh(x), x)
False
>>> _is_lambert((x**2 - 2*x + 1).subs(x, (log(x) + 3*x)**2 - 1), x)
True

See Also
========

_solve_lambert

r   c              3   l   >#    U  H)  oR                   " T5      (       d  M  [        UT5      v   M+     g 7fr   )r   r   )rQ  r@   lambert_funcsr   s     r   rR  _is_lambert.<locals>.<genexpr>?  s,      8#wwv .z#}--#s   44Tc              3      >#    U  HD  oR                   " 5       S    R                  T5      (       d  M,  [        U[        [        45      v   MF     g7f)r   N)rA  r   r   r   r5   )rQ  r@   r   s     r   rR  r[  D  s@      K#(9!(<'A'A&'I -C#s,,#s
   +AAF)
r   r   r  r   r   rC   rQ   r   r.   r  )r   r   term_factorsr@   no_of_symbols
no_of_trigrZ  s    `    @r   _is_lambertr`    s    J ahhj12L HHIM\ ?\c77%'<= \ ? @J 	xxMQ& 02GH 8#8 8 8- .   K#K K K# I?s   C>C>"$D
Dc                    S n[        U[        U S5      U5      n[        U SX5      u  pV[        U[        5      (       aF  [        UR                  5      S:X  d   eUR                  S   nUR                  (       a	  U" XWX5      nU$ UnU$ )aU  
Function to solve transcendental equations. It is a helper to
``solveset`` and should be used internally. ``_transolve``
currently supports the following class of equations:

    - Exponential equations
    - Logarithmic equations

Parameters
==========

f : Any transcendental equation that needs to be solved.
    This needs to be an expression, which is assumed
    to be equal to ``0``.

symbol : The variable for which the equation is solved.
    This needs to be of class ``Symbol``.

domain : A set over which the equation is solved.
    This needs to be of class ``Set``.

Returns
=======

Set
    A set of values for ``symbol`` for which ``f`` is equal to
    zero. An ``EmptySet`` is returned if ``f`` does not have solutions
    in respective domain. A ``ConditionSet`` is returned as unsolved
    object if algorithms to evaluate complete solution are not
    yet implemented.

How to use ``_transolve``
=========================

``_transolve`` should not be used as an independent function, because
it assumes that the equation (``f``) and the ``symbol`` comes from
``solveset`` and might have undergone a few modification(s).
To use ``_transolve`` as an independent function the equation (``f``)
and the ``symbol`` should be passed as they would have been by
``solveset``.

Examples
========

>>> from sympy.solvers.solveset import _transolve as transolve
>>> from sympy.solvers.solvers import _tsolve as tsolve
>>> from sympy import symbols, S, pprint
>>> x = symbols('x', real=True) # assumption added
>>> transolve(5**(x - 3) - 3**(2*x + 1), x, S.Reals)
{-(log(3) + 3*log(5))/(-log(5) + 2*log(3))}

How ``_transolve`` works
========================

``_transolve`` uses two types of helper functions to solve equations
of a particular class:

Identifying helpers: To determine whether a given equation
belongs to a certain class of equation or not. Returns either
``True`` or ``False``.

Solving helpers: Once an equation is identified, a corresponding
helper either solves the equation or returns a form of the equation
that ``solveset`` might better be able to handle.

* Philosophy behind the module

The purpose of ``_transolve`` is to take equations which are not
already polynomial in their generator(s) and to either recast them
as such through a valid transformation or to solve them outright.
A pair of helper functions for each class of supported
transcendental functions are employed for this purpose. One
identifies the transcendental form of an equation and the other
either solves it or recasts it into a tractable form that can be
solved by  ``solveset``.
For example, an equation in the form `ab^{f(x)} - cd^{g(x)} = 0`
can be transformed to
`\log(a) + f(x)\log(b) - \log(c) - g(x)\log(d) = 0`
(under certain assumptions) and this can be solved with ``solveset``
if `f(x)` and `g(x)` are in polynomial form.

How ``_transolve`` is better than ``_tsolve``
=============================================

1) Better output

``_transolve`` provides expressions in a more simplified form.

Consider a simple exponential equation

>>> f = 3**(2*x) - 2**(x + 3)
>>> pprint(transolve(f, x, S.Reals), use_unicode=False)
    -3*log(2)
{------------------}
 -2*log(3) + log(2)
>>> pprint(tsolve(f, x), use_unicode=False)
     /   3     \
     | --------|
     | log(2/9)|
[-log\2         /]

2) Extensible

The API of ``_transolve`` is designed such that it is easily
extensible, i.e. the code that solves a given class of
equations is encapsulated in a helper and not mixed in with
the code of ``_transolve`` itself.

3) Modular

``_transolve`` is designed to be modular i.e, for every class of
equation a separate helper for identification and solving is
implemented. This makes it easy to change or modify any of the
method implemented directly in the helpers without interfering
with the actual structure of the API.

4) Faster Computation

Solving equation via ``_transolve`` is much faster as compared to
``_tsolve``. In ``solve``, attempts are made computing every possibility
to get the solutions. This series of attempts makes solving a bit
slow. In ``_transolve``, computation begins only after a particular
type of equation is identified.

How to add new class of equations
=================================

Adding a new class of equation solver is a three-step procedure:

- Identify the type of the equations

  Determine the type of the class of equations to which they belong:
  it could be of ``Add``, ``Pow``, etc. types. Separate internal functions
  are used for each type. Write identification and solving helpers
  and use them from within the routine for the given type of equation
  (after adding it, if necessary). Something like:

  .. code-block:: python

    def add_type(lhs, rhs, x):
        ....
        if _is_exponential(lhs, x):
            new_eq = _solve_exponential(lhs, rhs, x)
    ....
    rhs, lhs = eq.as_independent(x)
    if lhs.is_Add:
        result = add_type(lhs, rhs, x)

- Define the identification helper.

- Define the solving helper.

Apart from this, a few other things needs to be taken care while
adding an equation solver:

- Naming conventions:
  Name of the identification helper should be as
  ``_is_class`` where class will be the name or abbreviation
  of the class of equation. The solving helper will be named as
  ``_solve_class``.
  For example: for exponential equations it becomes
  ``_is_exponential`` and ``_solve_expo``.
- The identifying helpers should take two input parameters,
  the equation to be checked and the variable for which a solution
  is being sought, while solving helpers would require an additional
  domain parameter.
- Be sure to consider corner cases.
- Add tests for each helper.
- Add a docstring to your helper that describes the method
  implemented.
  The documentation of the helpers should identify:

  - the purpose of the helper,
  - the method used to identify and solve the equation,
  - a proof of correctness
  - the return values of the helpers
c                     [        U[        X-
  S5      U5      n[        X5      (       a  [        XX#5      nU$ [	        X5      (       a  [        XX#5      nU$ )z
Helper for ``_transolve`` to handle equations of
``Add`` type, i.e. equations taking the form as
``a*f(x) + b*g(x) + .... = c``.
For example: 4**x + 8**x = 0
r   )rY   r   rO  rK  rW  rS  )r  r   r   r   r  s        r   add_type_transolve.<locals>.add_type  sY     fbA&6? 3'''&AF
  S))%c?Fr   r   r   )rY   r   r  r   rT   r   r   r   )r   r   r   rc  r  r  r  r   s           r   r  r  J  s    f$ &"Q(F3F  1f5JC%##EJJA%%%jjm::c7F M Mr   c                    [        U 5      n [        U5      nU [        R                  L a  U$ U [        R                  L a  [        R                  $ [        U [        [        [        45      (       d  [        SU -  5      e[        U[        [        45      (       d  Ub  [        U< S35      e[        U[        5      (       d  [        SU-  5      eU R                  nU R                  [        5      (       a  [        U 5      n Ucb  U(       d[  [        U S5      nU[        R                  L a  U$ U[        R                  L a  [        R                  $ [!        [#        SU-  5      5      eUc<  [%        U5      S:X  a  UR'                  5       nOdU(       a  [        [#        S5      5      eOH[        U[(        5      (       d3  [+        U /U/5      u  pn[-        U S   US   U5      R/                  U5      $ SnUR1                  [        R2                  5      (       a  UR4                  S	S
0:w  a
  [7        SS
S9nO@UR1                  [        R8                  5      (       a  UR4                  SS
0:w  a
  [7        SS
S9nUbI  [-        U R/                  X05      Xr5      n UR/                  Xq05      n	UR=                  U	5      (       a  U	nU$ [?        U [@        5      u  p
U RC                  [        5      n U
 HH  u  pURE                  URF                  S   RC                  [        5      5      nU R/                  X05      n MJ     [        U 5      n [I        XUS
S9$ ! [:         a    Un	 Nf = f)ar
  Solves a given inequality or equation with set as output

Parameters
==========

f : Expr or a relational.
    The target equation or inequality
symbol : Symbol
    The variable for which the equation is solved
domain : Set
    The domain over which the equation is solved

Returns
=======

Set
    A set of values for `symbol` for which `f` is True or is equal to
    zero. An :class:`~.EmptySet` is returned if `f` is False or nonzero.
    A :class:`~.ConditionSet` is returned as unsolved object if algorithms
    to evaluate complete solution are not yet implemented.

``solveset`` claims to be complete in the solution set that it returns.

Raises
======

NotImplementedError
    The algorithms to solve inequalities in complex domain  are
    not yet implemented.
ValueError
    The input is not valid.
RuntimeError
    It is a bug, please report to the github issue tracker.


Notes
=====

Python interprets 0 and 1 as False and True, respectively, but
in this function they refer to solutions of an expression. So 0 and 1
return the domain and EmptySet, respectively, while True and False
return the opposite (as they are assumed to be solutions of relational
expressions).


See Also
========

solveset_real: solver for real domain
solveset_complex: solver for complex domain

Examples
========

>>> from sympy import exp, sin, Symbol, pprint, S, Eq
>>> from sympy.solvers.solveset import solveset, solveset_real

* The default domain is complex. Not specifying a domain will lead
  to the solving of the equation in the complex domain (and this
  is not affected by the assumptions on the symbol):

>>> x = Symbol('x')
>>> pprint(solveset(exp(x) - 1, x), use_unicode=False)
{2*n*I*pi | n in Integers}

>>> x = Symbol('x', real=True)
>>> pprint(solveset(exp(x) - 1, x), use_unicode=False)
{2*n*I*pi | n in Integers}

* If you want to use ``solveset`` to solve the equation in the
  real domain, provide a real domain. (Using ``solveset_real``
  does this automatically.)

>>> R = S.Reals
>>> x = Symbol('x')
>>> solveset(exp(x) - 1, x, R)
{0}
>>> solveset_real(exp(x) - 1, x)
{0}

The solution is unaffected by assumptions on the symbol:

>>> p = Symbol('p', positive=True)
>>> pprint(solveset(p**2 - 4))
{-2, 2}

When a :class:`~.ConditionSet` is returned, symbols with assumptions that
would alter the set are replaced with more generic symbols:

>>> i = Symbol('i', imaginary=True)
>>> solveset(Eq(i**2 + i*sin(i), 1), i, domain=S.Reals)
ConditionSet(_R, Eq(_R**2 + _R*sin(_R) - 1, 0), Reals)

* Inequalities can be solved over the real domain only. Use of a complex
  domain leads to a NotImplementedError.

>>> solveset(exp(x) > 1, x, R)
Interval.open(0, oo)

z"%s is not a valid SymPy expressionNz is not a valid SymPy symbolz%s is not a valid domainr   z@
                relationship between value and 0 is unknown: %sr   zh
                The independent variable must be specified for a
                multivariate equation.r   TrJ  r   r^  C)r^  )r  )%r   r   r   r   r   r   r   r   r   r   r]   r   r   r=   r<   r   r?  rz   r   popr    rx   rz  re  r   r   _assumptions_origr   r   r  dummy_eqr   r?   r  r   r   rq  )r   r   r   r   r   r   swapnewsymrM  _rvr   dr)  s                r   rz  rz  !	  s   J 	
AV_FAFF{AGG|zza$
F344=ABBftZ011v7IVEFFfc""3f=>>>>LuuY1~lq!H;M!''\::%j 2CEF2G 'H I I ~|!!%%'FZ )* + , ,  ''&sVH5
d!adF+44T:: F  ##~53T*F			!++	&	&##	4'883-Fajj&!12FC	++v./C ;;sB	 aoGA			)AFF166!9$$Y/0JJv  	qAQt44)  	C	s   M MMc                 6    [        X[        R                  5      $ r   )rz  r   r   r   r   s     r   r  r  	  s    Aqww''r   c                 6    [        X[        R                  5      $ r   )rz  r   r   ro  s     r   r  r  	  s    Aq{{++r   c           
        ^ 0 n[        TU5       H0  u  pEU[        R                  L d  M  [        UR                  SS9X4'   M2     U  Vs/ s H  ofR                  U5      PM     n nT Vs/ s H  oDR                  U5      PM     snm[        T5      m[        U 5      S:X  a  [        U6 $ [        T5      S:X  aT  TS   nUS   nU  Vs/ s H  n[        XdU5      PM     nn[        U6 n	[        [        U4U45      U	5      R                  5       $ [        U U4S jS9n [        U 5       GH   u  p/ nSnT H  nUWR                   ;  a  M  [        XdUTR#                  U5         5      n[%        U[&        5      (       a  TR#                  U5      nTSU TUS-   S -   nUSU X.S-   S -   nU SU
 X
S-   S -   nU Hs  nU Vs/ s H  ofR                  UU5      PM     nn[)        UUU5      n[        U4USU U4-   XS -   5      nUR+                  [        UU5      R                  5       5        Mu     M  SnGM     U(       d  GM  [-        U6 s  $    gs  snf s  snf s  snf s  snf )	zeBasic implementation of a multivariate solveset.

For internal use (not ready for public consumption)Tr   r   r   c                 F   > [        U R                  [        T5      -  5      $ r   )r   r   r  )r  r  s    r   r   !_solveset_multi.<locals>.<lambda>	  s    S3t9)D%Er   keyNF)zipr   r   r    namerW  r  r   r^   rz  rW   rZ   r   doitsortedr   r   indexr   rT   _solveset_multir   rX   )eqsr  domainsrepr   domr  r   solsetssolsetr   solsall_handledr  r   symspdomainspeqspr   eqsp_sub
sol_othersfuns    `                    r   r{  r{  	  sN   
 Cg&!''>chhT2CH ' #&
&#B773<#C
&%)*TcHHSMT*D;D
3x1}7##
4yA~1g7:;s8BV,s;w'vv.7<<>>
E
FC3C"//)2GDJJsO$<=C#y))JJsORa4!:-"2A;162AwqST*A:>?$BQ$H?!05(!KJ %5!9t+;eBi+GHCKKj 9 > > @A	  $! " ;$<+  # '* <(  @s   I	(II9I
c           
         [        XU5      nSnU[        R                  L a  / nU$ [        U[        5      (       a  [        S5      e[        U[        5      (       a  [        U5      nU$ [        X5      nUb  [        R                  nSn[        U[        5      (       a  U4nOB[        U[        5      (       a-  [        S UR                   5       5      (       a  UR                  nU H"  nXhR                  [        SUSS5      5      -  nM$     [        U[        5      (       a  [        U5      nU$ UR                  U5      n[        U[        5      (       am  [        S UR                   5       5      (       aK  UR                   V	V
s/ s H0  n	U	R                    H  n
[        U	[        5      (       d  M  U
PM     M2     nn	n
U$ g[        U[        5      (       a  XH-  nU$ s  sn
n	f )	a;  Solves an equation using solveset and returns the solution in accordance
with the `solve` output API.

Returns
=======

We classify the output based on the type of solution returned by `solveset`.

Solution    |    Output
----------------------------------------
FiniteSet   | list

ImageSet,   | list (if `f` is periodic)
Union       |

Union       | list (with FiniteSet)

EmptySet    | empty list

Others      | None


Raises
======

NotImplementedError
    A ConditionSet is the input.

Examples
========

>>> from sympy.solvers.solveset import solvify
>>> from sympy.abc import x
>>> from sympy import S, tan, sin, exp
>>> solvify(x**2 - 9, x, S.Reals)
[-3, 3]
>>> solvify(sin(x) - 1, x, S.Reals)
[pi/2]
>>> solvify(tan(x), x, S.Reals)
[0]
>>> solvify(exp(x) - 1, x, S.Complexes)

>>> solvify(exp(x) - 1, x, S.Reals)
[0]

Nz*solveset is unable to solve this equation.r   c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   r   rZ   r?  s     r   rR  solvify.<locals>.<genexpr>T
  s     J8I1z!X..8Ir  r   FTc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r   rT   r?  s     r   rR  r  b
  s     GAz!Y//r  )rz  r   r   r   rY   r?  rT   r   r   rZ   rX   rX  r   r   rV   r  )r   r   r   r  r  r  r  iter_solutionssolutionsolnr  s              r   solvifyr  
  s   ^ Av.LFqzz!N MK 
L,	/	/!"NOO	L)	,	,l#B M= Q'

IN,11".L%00J8I8IJJJ%1%6%6N*//FE40PQQ	 + )Y//i  M $--f5H(E** GGGG.6mm Imd $		ZY-G " ) "mF I M  Hi00"MIs   )G0
G)dictc                P   [        U 5      n [        U5      S:X  a6  [        US   5      (       a#  [        US   [        5      (       d  [        S5      e[        U5      n[        U5      [        U5      :w  a  [        S5      e [        U /U5      u  pEUS   nUS   nU(       a  U(       a  XT[        R                  '   U$ [        R                  /[        U5      S-   -  nXWS'   [        U5       H  u  pX;  a  M  XI   Xx'   M     U$ ! [         a  n[        [        U5      5      eSnAff = f)a6  Return a list whose elements are the coefficients of the
corresponding symbols in the sum of terms in  ``eq``.
The additive constant is returned as the last element of the
list.

Raises
======

NonlinearError
    The equation contains a nonlinear term
ValueError
    duplicate or unordered symbols are passed

Parameters
==========

dict - (default False) when True, return coefficients as a
    dictionary with coefficients keyed to syms that were present;
    key 1 gives the constant term

Examples
========

>>> from sympy.solvers.solveset import linear_coeffs
>>> from sympy.abc import x, y, z
>>> linear_coeffs(3*x + 2*y - 1, x, y)
[3, 2, -1]

It is not necessary to expand the expression:

    >>> linear_coeffs(x + y*(z*(x*3 + 2) + 3), x)
    [3*y*z + 1, y*(2*z + 3)]

When nonlinear is detected, an error will be raised:

    * even if they would cancel after expansion (so the
    situation does not pass silently past the caller's
    attention)

    >>> eq = 1/x*(x - 1) + 1/x
    >>> linear_coeffs(eq.expand(), x)
    [0, 1]
    >>> linear_coeffs(eq, x)
    Traceback (most recent call last):
    ...
    NonlinearError:
    nonlinear in given generators

    * when there are cross terms

    >>> linear_coeffs(x*(y + 1), x, y)
    Traceback (most recent call last):
    ...
    NonlinearError:
    symbol-dependent cross-terms encountered

    * when there are terms that contain an expression
    dependent on the symbols that is not linear

    >>> linear_coeffs(x**2, x)
    Traceback (most recent call last):
    ...
    NonlinearError:
    nonlinear in given generators
r   r   z!expecting unpacked symbols, *symsduplicate symbols givenNr   )r"   r   r~   r   r   r   r  r&   rr   r   strr   r   r   r   )
r  r  r  symsetrm  r  errrM  r   r9  s
             r   linear_coeffsr  s
  s   D 
"B
4yA~(47++JtAw4N4N<==YF
6{c$i233'!2$/aDaD aeeH
&&3t9q=	!BrF$:   I  'SX&&'s    D 
D%D  D%c                 J   U(       d  [        [        S5      5      e[        US   [        5      (       a  [	        S5      e[        US   S5      (       a  US   n[        U5      (       a  [        S5      e[        U 5      n [        U [        5      (       a  [        U 5      n OC[        U [        [        45      (       a  U /n O$[        U 5      (       d  [        [        S5      5      e [        X5      u  p#[#        U5      [#        U5      4=u  pVn[%        ['        U[)        U5      5      5      n[+        U6 n	[-        U5       H  u  pU H  nX   nX   XU4'   M     M     [/        USU Vs/ s H  o* PM     sn5      nX4$ ! [         a  n[        [!        U5      5      eSnAff = fs  snf )	a@  
Converts a given System of Equations into Matrix form. Here ``equations``
must be a linear system of equations in ``symbols``. Element ``M[i, j]``
corresponds to the coefficient of the jth symbol in the ith equation.

The Matrix form corresponds to the augmented matrix form. For example:

.. math::

   4x + 2y + 3z & = 1 \\
   3x +  y +  z & = -6 \\
   2x + 4y + 9z & = 2

This system will return :math:`A` and :math:`b` as:

.. math::

   A = \left[\begin{array}{ccc}
   4 & 2 & 3 \\
   3 & 1 & 1 \\
   2 & 4 & 9
   \end{array}\right] \\

.. math::

   b = \left[\begin{array}{c}
   1 \\ -6 \\ 2
   \end{array}\right]

The only simplification performed is to convert
``Eq(a, b)`` :math:`\Rightarrow a - b`.

Raises
======

NonlinearError
    The equations contain a nonlinear term.
ValueError
    The symbols are not given or are not unique.

Examples
========

>>> from sympy import linear_eq_to_matrix, symbols
>>> c, x, y, z = symbols('c, x, y, z')

The coefficients (numerical or symbolic) of the symbols will
be returned as matrices:

>>> eqns = [c*x + z - 1 - c, y + z, x - y]
>>> A, b = linear_eq_to_matrix(eqns, [x, y, z])
>>> A
Matrix([
[c,  0, 1],
[0,  1, 1],
[1, -1, 0]])
>>> b
Matrix([
[c + 1],
[    0],
[    0]])

This routine does not simplify expressions and will raise an error
if nonlinearity is encountered:

>>> eqns = [
...     (x**2 - 3*x)/(x - 3) - 3,
...     y**2 - 3*y - y*(y - 4) + x - 4]
>>> linear_eq_to_matrix(eqns, [x, y])
Traceback (most recent call last):
...
NonlinearError:
symbol-dependent term can be ignored using `strict=False`

Simplifying these equations will discard the removable singularity in the
first and reveal the linear structure of the second:

>>> [e.simplify() for e in eqns]
[x - 3, x + y - 4]

Any such simplification needed to eliminate nonlinear terms must be done
*before* calling this routine.

zd
            Symbols must be given, for which coefficients
            are to be found.
            r   z;Unordered 'set' type is not supported as input for symbols.__iter__zSymbols must be uniquezb
            Equation(s) must be given as a sequence, Expr,
            Eq or Matrix.
            Nr   )r   rz   r   r  r  r   r|   r   ra   r   r   r   r}   r&   rr   r   r  r   r  rv  ranger_   r   r`   )	equationssymbolsr  r  r  r   r  shapeixrb  rowrm  r9  colr   r   s                   r   linear_eq_to_matrixr  
  s   j  %   	 '!*c""IK 	K wqz:&&!*122	"I)Z((O		Ibz	*	*K	## %   	'"96 r7CL((DA5	c'58$	%BuAB-A%C$A3hK    	q!!_!Qb!_%A4K  'SX&&' %s   #E: $F :
FFFc                 ,  ^ U (       d  [         R                  $ U(       a  [        US   S5      (       a  US   n[        U[        5      nSnSn[        U S5      (       a  [        U 5      S:X  a  [        U S   [        5      (       a  U u  pT[        U S   [        5      (       d  U(       d  U(       d  [        [        S5      5      e[        [        U5      5      [        U5      :w  a  [        U5      eU nU Vs/ s H  n[        U5      PM     nn [        Xa5      mTc  [         R                  $ [        [!        U4S jU 5       6 5      mT$ O^[        U [        5      (       aI  U(       a-  [        U[        5      (       d  [        US   [        5      (       d  U SS2SS24   U SS2SS24   pEUc  [        S	5      eU(       a  [#        WR$                  5       V	s/ s H  n	['        U5      PM     nn	[        U5      n
[)        XR*                  UR*                  -  -  5      (       a3  [        [        S
US   R,                  R/                  S5      -  5      5      e[        U
5      [        U5      :w  a  [        U5      eU(       dP  [#        WR$                  5       Vs/ s H  n[1        5       PM     nn[3        SXT4S S9R,                  n[5        U5      nOSn/ nWR7                  5       n[9        X5       H[  u  nn[9        X5       VVs/ s H  u  nnU(       d  M  UU-  PM     nnnUR;                  U* 5        UR;                  [=        U6 5        M]     [?        Xa5      u  nn[A        UUSS9mTc  [         R                  $ [        [!        U4S jU 5       6 5      mUbA  TR*                  nU Vs0 s H  nUU;   d  M  U['        U5      _M     nnTRC                  U5      mT$ s  snf ! [         a  n[        [        U5      5      eSnAff = fs  sn	f s  snf s  snnf s  snf )az  
Solve system of $N$ linear equations with $M$ variables; both
underdetermined and overdetermined systems are supported.
The possible number of solutions is zero, one or infinite.
Zero solutions throws a ValueError, whereas infinite
solutions are represented parametrically in terms of the given
symbols. For unique solution a :class:`~.FiniteSet` of ordered tuples
is returned.

All standard input formats are supported:
For the given set of equations, the respective input types
are given below:

.. math:: 3x + 2y -   z = 1
.. math:: 2x - 2y + 4z = -2
.. math:: 2x -   y + 2z = 0

* Augmented matrix form, ``system`` given below:

$$ \text{system} = \left[{array}{cccc}
    3 &  2 & -1 &  1\\
    2 & -2 &  4 & -2\\
    2 & -1 &  2 &  0
    \end{array}\right] $$

::

    system = Matrix([[3, 2, -1, 1], [2, -2, 4, -2], [2, -1, 2, 0]])

* List of equations form

::

    system  =  [3x + 2y - z - 1, 2x - 2y + 4z + 2, 2x - y + 2z]

* Input $A$ and $b$ in matrix form (from $Ax = b$) are given as:

$$ A = \left[\begin{array}{ccc}
    3 &  2 & -1 \\
    2 & -2 &  4 \\
    2 & -1 &  2
    \end{array}\right] \ \  b = \left[\begin{array}{c}
    1 \\ -2 \\ 0
    \end{array}\right] $$

::

    A = Matrix([[3, 2, -1], [2, -2, 4], [2, -1, 2]])
    b = Matrix([[1], [-2], [0]])
    system = (A, b)

Symbols can always be passed but are actually only needed
when 1) a system of equations is being passed and 2) the
system is passed as an underdetermined matrix and one wants
to control the name of the free variables in the result.
An error is raised if no symbols are used for case 1, but if
no symbols are provided for case 2, internally generated symbols
will be provided. When providing symbols for case 2, there should
be at least as many symbols are there are columns in matrix A.

The algorithm used here is Gauss-Jordan elimination, which
results, after elimination, in a row echelon form matrix.

Returns
=======

A FiniteSet containing an ordered tuple of values for the
unknowns for which the `system` has a solution. (Wrapping
the tuple in FiniteSet is used to maintain a consistent
output format throughout solveset.)

Returns EmptySet, if the linear system is inconsistent.

Raises
======

ValueError
    The input is not valid.
    The symbols are not given.

Examples
========

>>> from sympy import Matrix, linsolve, symbols
>>> x, y, z = symbols("x, y, z")
>>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
>>> b = Matrix([3, 6, 9])
>>> A
Matrix([
[1, 2,  3],
[4, 5,  6],
[7, 8, 10]])
>>> b
Matrix([
[3],
[6],
[9]])
>>> linsolve((A, b), [x, y, z])
{(-1, 2, 0)}

* Parametric Solution: In case the system is underdetermined, the
  function will return a parametric solution in terms of the given
  symbols. Those that are free will be returned unchanged. e.g. in
  the system below, `z` is returned as the solution for variable z;
  it can take on any value.

>>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> b = Matrix([3, 6, 9])
>>> linsolve((A, b), x, y, z)
{(z - 1, 2 - 2*z, z)}

If no symbols are given, internally generated symbols will be used.
The ``tau0`` in the third position indicates (as before) that the third
variable -- whatever it is named -- can take on any value:

>>> linsolve((A, b))
{(tau0 - 1, 2 - 2*tau0, tau0)}

* List of equations as input

>>> Eqns = [3*x + 2*y - z - 1, 2*x - 2*y + 4*z + 2, - x + y/2 - z]
>>> linsolve(Eqns, x, y, z)
{(1, -2, -2)}

* Augmented matrix as input

>>> aug = Matrix([[2, 1, 3, 1], [2, 6, 8, 3], [6, 8, 18, 5]])
>>> aug
Matrix([
[2, 1,  3, 1],
[2, 6,  8, 3],
[6, 8, 18, 5]])
>>> linsolve(aug, x, y, z)
{(3/10, 2/5, 0)}

* Solve for symbolic coefficients

>>> a, b, c, d, e, f = symbols('a, b, c, d, e, f')
>>> eqns = [a*x + b*y - c, d*x + e*y - f]
>>> linsolve(eqns, x, y)
{((-b*f + c*e)/(a*e - b*d), (a*f - c*d)/(a*e - b*d))}

* A degenerate system returns solution as set of given
  symbols.

>>> system = Matrix(([0, 0, 0], [0, 0, 0], [0, 0, 0]))
>>> linsolve(system, x, y)
{(x, y)}

* For an empty system linsolve returns empty set

>>> linsolve([], x)
EmptySet

* An error is raised if any nonlinearity is detected, even
  if it could be removed with expansion

>>> linsolve([x*(1/x - 1)], x)
Traceback (most recent call last):
...
NonlinearError: nonlinear term: 1/x

>>> linsolve([x*(y + 1)], x, y)
Traceback (most recent call last):
...
NonlinearError: nonlinear cross-term: x*(y + 1)

>>> linsolve([x**2 - 1], x)
Traceback (most recent call last):
...
NonlinearError: nonlinear term: x**2
r   r  r  Nr   z
                    When passing a system of equations, the explicit
                    symbols for which a solution is being sought must
                    be given as a sequence, too.
                c              3   F   >#    U  H  nTR                  X5      v   M     g 7fr   ra  rQ  r   r  s     r   rR  linsolve.<locals>.<genexpr>,  s     #I#CGGC$5$5   !r   zInvalid argumentsa  
                At least one of the symbols provided
                already appears in the system to be solved.
                One way to avoid this is to use Dummy symbols in
                the generator, e.g. numbered_symbols('%s', cls=Dummy)
            
1234567890tauc                 6    [        U 5      R                  S5      $ )Nr  )r  rstrip)r   s    r   r   linsolve.<locals>.<lambda>G  s    c!fmmL9r   )compareF)_rawc              3   F   >#    U  H  nTR                  X5      v   M     g 7fr   r  r  s     r   rR  r  Y  s     A#CGGC--r  )"r   r   r   r   r   r   ra   r   rz   r  r   rs   rr   r   r  rT   r   r  colsr   r  r   rw  r  r   r!   r{   tolistrv  r   r   rp   rq   rW  )systemr  sym_gendup_msgr   rb  r|  r  excr   r  r  rw  r  rowsrowibielemr   termsringsolsymr~  r  s                          @r   linsolver  P  s   Z zz 771:z22!*-0G'G 	A vz"" v;!
6!9j A ADA &)Z00g  - "  
 3w< CL0 )) C),-272;C-/-
 {zz!E#I#IJKCJ9 1< 
FJ	'	'Jw>>wqz:..a"f~vaf~1y,--*/-8-Q4=-8Wv!..89::Z )
 !*//((6)7 8 9 9 [CL(W%%$)!&&M2Mq57M2%eaV9;;?4 	% C88:DLb-0-?H-?	c4-?HbS

3; !
 "#/IC
T
.C
{zz
EAAB
CC
!!)0B#C6M~sDI~BhhsmJ} . & /$SX../& 9 3 I CsB   5OO PP>P	P#
P1P
O>%O99O>c                 ~    S U  5       n [        [        U6 [        U 6 [        R                  [        U5      -  5      nU$ )Nc              3   :   #    U  H  n[        US 5      v   M     g7f)r   N)r   )rQ  r  s     r   rR  '_return_conditionset.<locals>.<genexpr>j  s     
%#2c1::s   )rY   r   rR   r   r   r   )r|  r  condition_sets      r   _return_conditionsetr  h  s8    
%
%C wcAKKW$=?Mr   c           
        ^^^^^ ^!^"^#^$^%^&^'^( U (       d  [         R                  $ [        U 5       H7  u  pg[        U[        5      (       d  M  UR
                  UR                  -
  X'   M9     U(       d  Sn[        [        U5      5      e[        U5      (       d$  Sn[        [        U[        U5      U4-  5      5      e[        US   SS5      (       d*  Sn[        [        U[        US   5      US   4-  5      5      eTc  UmUn	0 m%0 m'Sn
SnSUU4S jjm$[        [        U U$4S	 j5      5      m&S
 nU%U'4S jm"U4S jm!S m#SS jm SUU!U#4S jjm S nU U"U$U%U&U'4S jnU" U	[        5      u  nnnU" U	[         5      u  nnnU
UU-   -  n
UUU-   -  nX:X  a  US:w  a  [#        T&T5      $ / n[        U5       H  n[        U5       Hg  nUR%                  5       UR%                  5       :w  a  M'  ['        S [)        UR+                  5       UR+                  5       5       5       5      (       d  Mf    Mw     UR-                  U5        M     UU-   n/ n/ nU H  nU(       d  M  [/        U5      [/        T5      :  aH  UR%                  5       m([        [1        U(4S jT5      5      nU H  nUUU'   M
     UR-                  U5        UU;  d  Mt  UR-                  U5        M     U(       a  UnT'(       d  T%(       a
  U" UT'T%5      n[         R                  nU H/  nT Vs/ s H  nUU   PM
     nnU[3        [5        U5      5      -  nM1     U$ s  snf )a  
Solves the `system` using substitution method. It is used in
:func:`~.nonlinsolve`. This will be called from :func:`~.nonlinsolve` when any
equation(s) is non polynomial equation.

Parameters
==========

system : list of equations
    The target system of equations
symbols : list of symbols to be solved.
    The variable(s) for which the system is solved
known_symbols : list of solved symbols
    Values are known for these variable(s)
result : An empty list or list of dict
    If No symbol values is known then empty list otherwise
    symbol as keys and corresponding value in dict.
exclude : Set of expression.
    Mostly denominator expression(s) of the equations of the system.
    Final solution should not satisfy these expressions.
all_symbols : known_symbols + symbols(unsolved).

Returns
=======

A FiniteSet of ordered tuple of values of `all_symbols` for which the
`system` has solution. Order of values in the tuple is same as symbols
present in the parameter `all_symbols`. If parameter `all_symbols` is None
then same as symbols present in the parameter `symbols`.

Please note that general FiniteSet is unordered, the solution returned
here is not simply a FiniteSet of solutions, rather it is a FiniteSet of
ordered tuple, i.e. the first & only argument to FiniteSet is a tuple of
solutions, which is ordered, & hence the returned solution is ordered.

Also note that solution could also have been returned as an ordered tuple,
FiniteSet is just a wrapper `{}` around the tuple. It has no other
significance except for the fact it is just used to maintain a consistent
output format throughout the solveset.

Raises
======

ValueError
    The input is not valid.
    The symbols are not given.
AttributeError
    The input symbols are not :class:`~.Symbol` type.

Examples
========

>>> from sympy import symbols, substitution
>>> x, y = symbols('x, y', real=True)
>>> substitution([x + y], [x], [{y: 1}], [y], set([]), [x, y])
{(-1, 1)}

* When you want a soln not satisfying $x + 1 = 0$

>>> substitution([x + y], [x], [{y: 1}], [y], set([x + 1]), [y, x])
EmptySet
>>> substitution([x + y], [x], [{y: 1}], [y], set([x - 1]), [y, x])
{(1, -1)}
>>> substitution([x + y - 1, y - x**2 + 5], [x, y])
{(-3, 4), (2, -1)}

* Returns both real and complex solution

>>> x, y, z = symbols('x, y, z')
>>> from sympy import exp, sin
>>> substitution([exp(x) - sin(y), y**2 - 4], [x, y])
{(ImageSet(Lambda(_n, I*(2*_n*pi + pi) + log(sin(2))), Integers), -2),
 (ImageSet(Lambda(_n, 2*_n*I*pi + log(sin(2))), Integers), 2)}

>>> eqs = [z**2 + exp(2*x) - sin(y), -3 + exp(-y)]
>>> substitution(eqs, [y, z])
{(-log(3), -sqrt(-exp(2*x) - sin(log(3)))),
 (-log(3), sqrt(-exp(2*x) - sin(log(3)))),
 (ImageSet(Lambda(_n, 2*_n*I*pi - log(3)), Integers),
  ImageSet(Lambda(_n, -sqrt(-exp(2*x) + sin(2*_n*I*pi - log(3)))), Integers)),
 (ImageSet(Lambda(_n, 2*_n*I*pi - log(3)), Integers),
  ImageSet(Lambda(_n, sqrt(-exp(2*x) + sin(2*_n*I*pi - log(3)))), Integers))}

GSymbols must be given, for which solution of the system is to be found.zBsymbols should be given as a sequence, e.g. a list.Not type %s: %sr   r   FzEIterable of symbols must be given as second argument, not type %s: %sr   c                    > U R                   nU[        T5      -
  [        T5      -  nU(       a  [        U5      nUR                  [        S9  U$ )z:Returns the unsolved symbol present
in the equation `eq`.
rt  )r   r  r   sortr   )r  r  freeunsolvedall_symbolsknown_symbolss       r   _unsolved_syms$substitution.<locals>._unsolved_syms  sG     3}--[1AAH~HMM.M/r   c                 &   > [        T" U 5      5      $ r   )r   )r  r  s    r   r   substitution.<locals>.<lambda>  s    #nQ&7"8r   c                 j   / nU  GH)  nUnUR                  5        H  u  pgSu  pUR                  5        H  u  pX:X  d  M  UnM     UR                  5        H  u  pX:X  d  M  Un	M     U(       d	  U	(       d  M]  [        U5      nU(       a  U[        R                  :w  a  [	        X5      nU	(       a  [        X5      nU[        R                  L a  S n  OEUR                  (       a,  [        U5      S:X  a  [        U5      R                  5       XV'   M  XU'   M     Uc  GM  UR                  U5        GM,     U$ )N)NNr   )itemsrT   r   r   rW   r[   r   r>  r   r  rg  r   )r  intersection_dictcomplement_dictfinal_resultr   res_copykey_res	value_resintersect_setcomplement_setkey_sym	value_sym	new_values                r   add_intersection_complement1substitution.<locals>.add_intersection_complement  s    CH&)iik"0:-*;*A*A*C&G)(1 +D +:*?*?*A&G))2 +B !NN )) 4I$!++)E$0$J	%$.y$I	 AJJ.#'"//C	Na4G,/	N,>,>,@),5)) '2, ###H-3 4 r   c                   > [        U[        5      (       a  UR                  n[        U[        5      (       a!  UR                  S   TU '   UR                  S   n[        U[
        5      (       aN  UR                  n[        R                  nU H,  n[        U[        5      (       a  X-  nM  U[        U5      -  nM.     [        U[        5      (       aR  UR                  S   [        R                  [        R                  4;  a  UR                  S   TU '   UR                  S   n[        U[        5      (       a'  UnUR                  R                  n[        U5      nXRU'   [        U[        5      (       d  [        U5      nX4$ )zSeparate the Complements, Intersections, ImageSet lambda expr and
its base_set. This function returns the unmasked sol from different classes
of sets and also returns the appended ImageSet elements in a
soln_imageset dict: `{unmasked element: ImageSet}`.
r   r   )r   rY   r  r[   r   rX   r   r   rT   rW   r   r   rZ   r3  r4  )	r   r  soln_imagesetsol_argssol_arg2soln_imagestexpr2complementsintersectionss	          r   _extract_main_soln(substitution.<locals>._extract_main_soln  s8    c<((,,Cc:&&"xx{K((1+C c5!!xxH**C %h	22OC 9X..C % c<((xx{177AKK"88 &)XXa[c"((1+C c8$$LIINNEE"C#/% #y))C.C!!r   c                    >^ U mU(       aD  U R                  5       nUS   nUR                  5        H  u  pEUR                  US5      X$'   M     Um [        U4S jT 5       5      nU$ ! [         a    S n U$ f = f)Nr   c              3   <   >#    U  H  n[        UT5      v   M     g 7fr   rt   )rQ  rm  rnew_s     r   rR  7substitution.<locals>._check_exclude.<locals>.<genexpr>c  s      "5,3qE""Gr  )copyr  rW  r  r  )	rnew
imgset_yes	rnew_copydummy_nr  r  satisfy_excluder  excludes	          @r   _check_exclude$substitution.<locals>._check_excludeR  s     		I mG&/oo&7"%.^^GQ%?	" '8E	#
 " "5,3"5 5O   	#"O	#s   A( (A87A8c                     [        U R                  5       5      [        UR                  5       5      -  nU H  nX   nXPU'   M     X;  a  UR                  U 5        g g r   )r  r  r   )r  original_imageset	newresultrestore_symr  imgs         r   _restore_imgset%substitution.<locals>._restore_imgseti  s]    $))+&!&&()*"G#,CM #  T" !r   c                     [        S5      nU(       a  U R                  US5      n U S;   a  U O
[        XUU SS9nUSL a  Sn0 nOUR                  U5        XU4$ )Nr  r   )TFT)minimalF)r   rW  rt   r   )r  r  r   delete_solnr   r  satisfys          r   
_append_eq substitution.<locals>._append_eqr  s]    #JABm+"!D1QeKCMM#K''r   c                 6  > T" X5      nSn	U(       d  Sn
U(       a  US   n
US   nU(       a|  U(       au  [        UR                  [        5      5      n[        S[	        U5      5       Vs/ s H  oPM     nn[        X5      n[        XR                  U5      5      n[        UU5      X'   Ub  T" XvX	U
5      u  p`n	OPUb  T" XvX	5      u  p`n	O@X$R                  5       ;   a  XB   X'   T" XU5        OUR                  U 5        OU(       a  Sn	0 n T" XU5        Xi4$ s  snf )aS  If `rnew` (A dict <symbol: soln>) contains valid soln
append it to `newresult` list.
`imgset_yes` is (base, dummy_var) if there was imageset in previously
 calculated result(otherwise empty tuple). `original_imageset` is dict
 of imageset expr and imageset from this result.
`soln_imageset` dict of imageset expr and imageset of new soln.
FNr   r   T)r   r   r   r  r   rv  r   rW  rZ   r  r   )r  r   r  r  r  r  r  r  r  r  local_nr   
dummy_listr   local_n_list	dummy_ziplamr  r  r  s                    r   _append_new_soln&substitution.<locals>._append_new_soln~  s5    ):G$Q-!!}3
 "&cii&6!7J */s:*0$1*0A *0 ! $1 !$J =I ((9*=>C (d 3DI>3=t'4C0I[/9406,	**,,).	C  &KD;%%/$1s   )Dc                    / n/ nU  Hc  n[        S UR                  5        5       5      (       a  M*  UR                  U5      S:X  a  UR                  U5        MR  UR                  U5        Me     U(       d  U(       a  X#-   $ U $ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   r  )rQ  vals     r   rR  :substitution.<locals>._new_order_result.<locals>.<genexpr>  s     ILSz#x00Lr  r   )r  rI  rW  r   )r  r  first_prioritysecond_priorityr   s        r   _new_order_result'substitution.<locals>._new_order_result  sp     CICJJLIII773<1$"))#.#**3/  _!33r   c                 	  >^ 0 nSnSn[        T"5       GH  u  pV/ nSnU  GH  n	0 n
[        5       nU	R                  5        H  u  p[        U[        5      (       ad  UR
                  R                  X'   XU'   UR
                  R                  R                  [        5      R                  5       nUR                  u  nX4n[        U[        5      (       d  M   e   UR                  U	5      R                  5       nU(       a  UR                  US   5      (       d  SnT " USS9nU(       d2  U	(       a(  T" U	SSXXU5      u  nnU(       a  U R                  U	5        GM:  UR                   " U6 u  nnUR                  ["        5      (       d  UR                  ["        5      (       a  U[$        :X  a  GM  0 nU GH   nSn U" UU5      nUS-  n[&        R(                  n[        U[*        5      (       a!  UR,                  S   T!U'   UR,                  S   n[        U[.        5      (       aJ  UR,                  S   [1        [2        * [2        5      :w  a  UR,                  S   T#U'   UUR,                  S   -  nU(       a  UOUnUS:  a4  U[4        :X  a*  [        U[        [6        45      (       d  U[%        UU5      -  n[        U[6        5      (       aR  UR<                  [&        R>                  [&        R@                  4;   a  [&        R(                  nSnUS-  nOUR<                  nU[&        R(                  La  T" UUU5      u  nnU GHX  nT" UUU5      u  nn[        U5      R                  5       nURB                  mU(       a  [E        U4S jU 5       5      (       a  MY  U	RG                  5       nU	R                  5        HH  u  p[        U[H        5      (       d  M  [        U[H        5      (       d  M3  UR                  UU5      UU'   MJ     UURK                  5       ;   aP  UU   nUR
                  " [M        S[O        UR
                  RP                  5      5       Vs/ s H  nSPM     sn6 UU'   OUUU'   T" UUUXX5      u  nnU(       d  GMG  U R                  U	5        GM[     U(       a  GM  URS                  U5        GM     GM     U(       d  GM  Un GM     XU4$ ! [8        [:        4 a     GM3  f = fs  snf )zSolves the system using already known solution
(result contains the dict <symbol: value>).
solver is :func:`~.solveset_complex` or :func:`~.solveset_real`.
r   FT)r  Nr   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r   )rQ  ssr  s     r   rR  Bsubstitution.<locals>._solve_using_known_values.<locals>.<genexpr>-  s      .1;2$Js   )*r   r  r  r   rZ   r3  r4  r   r   rg  r  rT   rW  r  r   remover   r?   r  r   r   r[   r   rW   rV   r   r  rY   r?  r   r  r   r   r   r  r  r   r  r  r   r5  add)$r  r  r  total_solvest_calltotal_conditionstrz  r  r  r  r   r  
got_symbolr9  vr  r   eq2unsolved_syms
delete_resdepen1depen2r   not_solvabler  soln_newr  r  imgstr   r  r  r  r  r  eqs_in_better_orderr  s$                                @r   _solve_using_known_values/substitution.<locals>._solve_using_known_values  sz     ##67IEIJ$&! U
IIKDA!!X..!"/0!,"#'',,"4"4U";"?"?"A"#++&-_
)!Y7777 ( ggcl))+cggjm&<&< "'J .s >$0@tZ-#1?-	: &
 #MM#.!$!3!3]!CJJsOOvzz#FFV<V  "(C#(L!%c3/*a/*#$::%dJ77/3yy|K,#'99Q<D%dL99#yy|xR/@@ 6:YYq\c 2$		!4H+3x 19=)@ $.dX|4L#M#M $(8c(B B
 "$55==QWWakk,BB#$::D ,0L-2-#'==D1::-.@}/6+m  $ .@m.5*]!#hlln"//%# .1;. + +
 %"xxz %(IIKDA)!T22z#t7L7L +,&&c*:Q	 %0 -"4"4"66$1#$6E(--21c$)KK$9$97; .< "= .<! .< "=)>DI ),DI0@ #sJ-1:-	: &: #MM#.K  $N (<"s+g )U ~ y"I 8J +<<<} 0< ! !!Z"=s   8C8SS(S%	$S%	c              3      #    U  HD  u  p[        U[        5      (       a  [        U[        5      (       a  M1  UR                  U5      v   MF     g 7fr   )r   intri  )rQ  r   r   s      r   rR  substitution.<locals>.<genexpr>h  s<      D-HTQ"1c**z!S/A !1::a==-Hs
   0AAc                    > U T;  $ r   r   )r   solved_symbolss    r   r   r  |  s
    !>1r   Fr   )r   r   r   r   r   r  r   r   rz   r}   r  typegetattrr   r   r  r  r  r  rX  rv  rI  r   r   filterrT   r  ))r  r  r  r  r  r  r   r)  msg
old_resulttotal_conditionsettotal_solveset_callr  r  r(  new_result_realsolve_call1	cnd_call1new_result_complexsolve_call2	cnd_call2filtered_complexjresult_all_variablesresult_infiniter   r  unsolved_symr  symbtempr  r  r  r  r  r  r  r'  r  r.  s)      ```                         @@@@@@@@@@r   substitutionrD  p  sQ   n zz&!aFI " (C))w!
3$w-)A#ABCC71:{E222C4
+;WQZ*H$HIJJ JKM 	 	 89;@5"n.#
( ;?1& 1&f Q= Q=f /HM/#+O[)1J$2&.Y 9y01K+5605HB5N#$7EE $%o&Avvx1668# DSQXXZ-H D D D ' ##A& & //FO s8c+&& XXZNF1;@ AH ($0L! !)""3'** '', "   /: - > ZZF!$/0KD$K0)E$K(( " M 1s   L=c                     [        U S   US   5      n[        U[        5      (       a  [        U Vs/ s H  o34PM     sn6 nU$ [        [        [        U5      5      5      $ s  snf r   )rz  r   rT   r  )r  r  r  r   _solns        r   _solveset_workrG    sZ    F1Iwqz*D$	""$/$QD$/0y/00 0s   A c                     SSK Jn  [        U5      nUR                  [        S9  U" XSS9n/ nU H"  nUR                  UR                  5       5        M$     0 /n[        XaU/ U5      nU$ )Nr   )rn   rt  T)polys)sympy.polys.polytoolsrn   r   r  r   r   r  rD  )	rI  r  r  rn   _symbolsbasis
new_systempoly_eqr  s	            r   _handle_positive_dimensionalrO    so    .G}HMM&M'UD1EJ'//+, TFVRF Mr   c           	         ^ [        U /UQ76 n[        R                  nU HJ  n[        [	        [        X5      5      5      m[        U4S jU 5       5      (       d  M<  U[        U5      -  nML     U$ )Nc              3   <   >#    U  H  n[        UT5      v   M     g 7fr   r  )rQ  r  dict_sym_values     r   rR  +_handle_zero_dimensional.<locals>.<genexpr>  s     =fxN++fr  )ry   r   r   r  r   rv  rX  rT   )rI  r  r  r  result_updater   rR  s         @r   _handle_zero_dimensionalrU    sb    u/w/F JJMd3w#456=f===Ys^+M  r   c                    / n/ n/ n/ n[        5       nS nU  GH%  nUR                  [        X5      5        [        U[        5      (       a  UR
                  UR                  -
  n[        [        U5      /UQ76 n	U	(       a  UR                  U5        U	u  pU(       d  U
n[        U[        5      (       a&  UR                  5       S   nUR                  " USS06nO[        U5      R                  (       a  M  Ub4  UR                  U5        UR                  UR                  " 5       5        GM  UR                  U5        GM(     X#XFU4$ )Nr   	extensionT)r  updaterw   r   r   r  r   rv   r(   r   r   r   as_poly	is_numberr  )r  r  rI  
polys_exprnonpolysunrad_changedr  ro   r  without_radicalseq_unradr  s               r   _separate_poly_nonpolyr`    s   EJH M5LDL56b""&&B "88  $,MHb$""$Q'B::w7$7Db\##LLdlln-OOB- . hmCCr   c           
         0 /n/ n/ n[        S U  5       5      nU(       a#  U  Vs/ s H  n[        [        USS95      PM     n n[        XSSS9nSU;   a  UnUn	X4$ UR                  (       al  UR                  S5      nU(       a  U Vs/ s H  n[        U5      PM     nn [        U/UQ7S	S06n
U
 Vs/ s H  n[        [        X5      5      PM     nnUn	X4$ Un[        [        XSSS95      n	U(       a  U	 Vs/ s H  n[        U5      PM     n	nX4$ s  snf s  snf s  snf ! [         a    Un[        U5      n	 X4$ f = fs  snf )
Nc              3   V   #    U  H  oR                   R                  (       + v   M!     g 7fr   )r   is_Exact)rQ  rM  s     r   rR  _handle_poly.<locals>.<genexpr>  s     7Ahh''''s   ')T)rationalgrevlexF)orderrI  r   lexstrict)r  ro   r+   rn   is_zero_dimensionalfglmr   ry   r  rv  r'   r   )rI  r  no_informationno_solutionsno_equationsinexactrM  rL  poly_solpoly_eqsr  r   s               r   _handle_polyrr    s    TNL L777G =BBEqiD12EB U9EBE
 	Ez  d [ 
	"	" 

5! (-.1VAYE.	$&uDwDtDF <BB6CS./6HB#H$  "uEJK+348aq	8H4C C8 /$ C % 	# &HE{H0 9	#4 5s)   D	D!D# 4D:E#D?>D?c           	      &  ^^ U (       d  [         R                  $ T(       d  Sn[        [        U5      5      e[	        TS   S5      (       a  TS   m[        T5      (       a  T(       d  Sn[        [        U5      5      e[        [        [        T5      5      m[        U T5      u  n mmT(       a8  [        U T5      n[        U Vs/ s H  n[        U4S jU 5       5      PM     sn6 $ [        U 5      S:X  a  [        T5      S:X  a  [        U T5      $ [!        U T5      u  pVpxn	/ n
0 /nU(       a  [#        UT5      u  pU(       av  US   (       al  [%        5       R&                  " S U 5       6 n[%        5       R&                  " S U	 5       6 nX:X  a+  U	(       a$  U Vs/ s H  n[)        X5      (       d  M  UPM     nnX-   nU4S jnU(       d  [        [        UU5      6 $ [+        UTXS	9n[-        U[        5      (       d  U$ U	(       a{  UR.                   Vs/ s H  n[1        [3        TU5      5      PM     nnU Vs/ s H-  n[5        S
 U 5       5      (       d  [)        X5      S:w  d  M+  UPM/     nn[        [        UU5      6 $ U$ s  snf s  snf s  snf s  snf )aC  
Solve system of $N$ nonlinear equations with $M$ variables, which means both
under and overdetermined systems are supported. Positive dimensional
system is also supported (A system with infinitely many solutions is said
to be positive-dimensional). In a positive dimensional system the solution will
be dependent on at least one symbol. Returns both real solution
and complex solution (if they exist).

Parameters
==========

system : list of equations
    The target system of equations
symbols : list of Symbols
    symbols should be given as a sequence eg. list

Returns
=======

A :class:`~.FiniteSet` of ordered tuple of values of `symbols` for which the `system`
has solution. Order of values in the tuple is same as symbols present in
the parameter `symbols`.

Please note that general :class:`~.FiniteSet` is unordered, the solution
returned here is not simply a :class:`~.FiniteSet` of solutions, rather it
is a :class:`~.FiniteSet` of ordered tuple, i.e. the first and only
argument to :class:`~.FiniteSet` is a tuple of solutions, which is
ordered, and, hence ,the returned solution is ordered.

Also note that solution could also have been returned as an ordered tuple,
FiniteSet is just a wrapper ``{}`` around the tuple. It has no other
significance except for the fact it is just used to maintain a consistent
output format throughout the solveset.

For the given set of equations, the respective input types
are given below:

.. math:: xy - 1 = 0
.. math:: 4x^2 + y^2 - 5 = 0

::

   system  = [x*y - 1, 4*x**2 + y**2 - 5]
   symbols = [x, y]

Raises
======

ValueError
    The input is not valid.
    The symbols are not given.
AttributeError
    The input symbols are not `Symbol` type.

Examples
========

>>> from sympy import symbols, nonlinsolve
>>> x, y, z = symbols('x, y, z', real=True)
>>> nonlinsolve([x*y - 1, 4*x**2 + y**2 - 5], [x, y])
{(-1, -1), (-1/2, -2), (1/2, 2), (1, 1)}

1. Positive dimensional system and complements:

>>> from sympy import pprint
>>> from sympy.polys.polytools import is_zero_dimensional
>>> a, b, c, d = symbols('a, b, c, d', extended_real=True)
>>> eq1 =  a + b + c + d
>>> eq2 = a*b + b*c + c*d + d*a
>>> eq3 = a*b*c + b*c*d + c*d*a + d*a*b
>>> eq4 = a*b*c*d - 1
>>> system = [eq1, eq2, eq3, eq4]
>>> is_zero_dimensional(system)
False
>>> pprint(nonlinsolve(system, [a, b, c, d]), use_unicode=False)
  -1       1               1      -1
{(---, -d, -, {d} \ {0}), (-, -d, ---, {d} \ {0})}
   d       d               d       d
>>> nonlinsolve([(x+y)**2 - 4, x + y - 2], [x, y])
{(2 - y, y)}

2. If some of the equations are non-polynomial then `nonlinsolve`
will call the ``substitution`` function and return real and complex solutions,
if present.

>>> from sympy import exp, sin
>>> nonlinsolve([exp(x) - sin(y), y**2 - 4], [x, y])
{(ImageSet(Lambda(_n, I*(2*_n*pi + pi) + log(sin(2))), Integers), -2),
 (ImageSet(Lambda(_n, 2*_n*I*pi + log(sin(2))), Integers), 2)}

3. If system is non-linear polynomial and zero-dimensional then it
returns both solution (real and complex solutions, if present) using
:func:`~.solve_poly_system`:

>>> from sympy import sqrt
>>> nonlinsolve([x**2 - 2*y**2 -2, x*y - 2], [x, y])
{(-2, -1), (2, 1), (-sqrt(2)*I, sqrt(2)*I), (sqrt(2)*I, -sqrt(2)*I)}

4. ``nonlinsolve`` can solve some linear (zero or positive dimensional)
system (because it uses the :func:`sympy.polys.polytools.groebner` function to get the
groebner basis and then uses the ``substitution`` function basis as the
new `system`). But it is not recommended to solve linear system using
``nonlinsolve``, because :func:`~.linsolve` is better for general linear systems.

>>> nonlinsolve([x + 2*y -z - 3, x - y - 4*z + 9, y + z - 4], [x, y, z])
{(3*z - 5, 4 - z, z)}

5. System having polynomial equations and only real solution is
solved using :func:`~.solve_poly_system`:

>>> e1 = sqrt(x**2 + y**2) - 10
>>> e2 = sqrt(y**2 + (-x + 10)**2) - 3
>>> nonlinsolve((e1, e2), (x, y))
{(191/20, -3*sqrt(391)/20), (191/20, 3*sqrt(391)/20)}
>>> nonlinsolve([x**2 + 2/y - 2, x + y - 3], [x, y])
{(1, 2), (1 - sqrt(5), 2 + sqrt(5)), (1 + sqrt(5), 2 - sqrt(5))}
>>> nonlinsolve([x**2 + 2/y - 2, x + y - 3], [y, x])
{(2, 1), (2 - sqrt(5), 1 + sqrt(5)), (2 + sqrt(5), 1 - sqrt(5))}

6. It is better to use symbols instead of trigonometric functions or
:class:`~.Function`. For example, replace $\sin(x)$ with a symbol, replace
$f(x)$ with a symbol and so on. Get a solution from ``nonlinsolve`` and then
use :func:`~.solveset` to get the value of $x$.

How nonlinsolve is better than old solver ``_solve_system`` :
=============================================================

1. A positive dimensional system solver: nonlinsolve can return
solution for positive dimensional system. It finds the
Groebner Basis of the positive dimensional system(calling it as
basis) then we can start solving equation(having least number of
variable first in the basis) using solveset and substituting that
solved solutions into other equation(of basis) to get solution in
terms of minimum variables. Here the important thing is how we
are substituting the known values and in which equations.

2. Real and complex solutions: nonlinsolve returns both real
and complex solution. If all the equations in the system are polynomial
then using :func:`~.solve_poly_system` both real and complex solution is returned.
If all the equations in the system are not polynomial equation then goes to
``substitution`` method with this polynomial and non polynomial equation(s),
to solve for unsolved variables. Here to solve for particular variable
solveset_real and solveset_complex is used. For both real and complex
solution ``_solve_using_known_values`` is used inside ``substitution``
(``substitution`` will be called when any non-polynomial equation is present).
If a solution is valid its general solution is added to the final result.

3. :class:`~.Complement` and :class:`~.Intersection` will be added:
nonlinsolve maintains dict for complements and intersections. If solveset
find complements or/and intersections with any interval or set during the
execution of ``substitution`` function, then complement or/and
intersection for that variable is added before returning final solution.

r  r   r  c              3   D   >#    U  H  oR                  T5      v   M     g 7fr   )re  )rQ  r   rj  s     r   rR  nonlinsolve.<locals>.<genexpr>  s      =1aD!1!11r  r   c              3   8   #    U  H  oR                   v   M     g 7fr   r   rQ  r  s     r   rR  ru    s     %F"oo   c              3   8   #    U  H  oR                   v   M     g 7fr   rw  rx  s     r   rR  ru    s     &O2ry  c                 0   >^  [        U 4S jT 5       5      $ )Nc              3   .   >#    U  H
  nTU   v   M     g 7fr   r   )rQ  r   r  s     r   rR  0nonlinsolve.<locals>.<lambda>.<locals>.<genexpr>  s      9AQs   )r  )r  r  s   `r   r   nonlinsolve.<locals>.<lambda>  s    5 9 99r   )r  r  c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r   r]   )rQ  r  s     r   rR  ru    s     8YUXPQAs9K9KUXr  F)r   r   r   rz   r   r}   
IndexErrorr   mapr"   rx   nonlinsolverT   r  r   rG  r`  rr  r  unionrt   rD  r   r   r  rv  r  )r  r  r3  r  r   rI  r[  r\  r  r]  rq  rp  	poly_syms
unrad_symsr  	remainingto_tuplesubs_resr  correct_solsrj  s    `                  @r   r  r  ;  sT   v zz(C))wqz:&&!*ww(C))3x)*G-fg>FGT67+tLt!5 =1 ==tLMM
6{aCLA-fg..
 	vw/ =Ex} HtH)%9%F%FGI&O&OPJ&= ,4T8Cx7SC8T #I :H #h122  	78Z(I..O 9AG#d3w,-FG+1 F6CS8YUX8Y5Y5Y';uD  6L Fc(L9::Os M* U> HFs$   I?J.JJ	5*J#Jr/  )r   sympy.core.sympifyr   
sympy.corer   r   r   r   r   r	   r
   r   r   sympy.core.containersr   sympy.core.functionr   r   r   r   r   r   r   sympy.core.modr   sympy.core.numbersr   r   r   r   sympy.core.intfuncr   sympy.core.relationalr   r   r   sympy.core.sortingr   r   sympy.core.symbolr    r!   r"   sympy.core.traversalr#   sympy.external.gmpyr$   r  r%   r  sympy.polys.matrices.linsolver&   sympy.polys.polyrootsr'   r  r(   r)   r*   r+   sympy.simplifyr,   r-   sympy.functionsr.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   %sympy.functions.combinatorial.numbersr>   $sympy.functions.elementary.complexesr?   r@   rA   rB   %sympy.functions.elementary.hyperbolicrC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   (sympy.functions.elementary.miscellaneousrP   (sympy.functions.elementary.trigonometricrQ   sympy.logic.boolalgrR   rS   
sympy.setsrT   rU   rV   rW   rX   rY   rZ   r[   r\   sympy.sets.setsr]   r^   sympy.matricesr_   r`   ra   sympy.ntheory.factor_rb   sympy.ntheory.residue_ntheoryrc   rd   sympy.polysre   rf   rg   rh   ri   rj   rk   sympy.polys.polyerrorsrl   rJ  rm   rn   ro   sympy.polys.solversrp   rq   rr   rs   sympy.solvers.solversrt   ru   rv   rw   rx   sympy.solvers.polysysry   sympy.utilitiesrz   sympy.utilities.iterablesr{   r|   r}   r~   sympy.calculus.utilr   r   r   typesr   r   r   r   r   r   r  r   r   r   r%  r   r-  r   r   rN  rL  rg  rj  rt  	Exceptionrv  r{  r|  r}  rp  r  r  r  rq  r  r'  r  r   rK  rO  rS  rW  r`  r  rz  r  r  r{  r  r  r  r  r  rD  rG  rO  rU  r`  rr  r  r   r   r   <module>r     s   '$ $ $ 'K K K  6 6 * 4 4 8 < ' 3 D < 7 K K 08 8 8 8 8 : A AF F F F ? J 0M M M + 4 4 * C3 3 3 1 8 8  3% % 3 &> > N N  	Z 	
$N  kk ['| 'mb m`GT=@.6b!'H$> +,++ &0/d+*;y ;8vaKH>@B &'[[ B6J1h64IX\~1hQhXv*6n7b.bV,r7tYxTn AKK t5n(,/ dYB #( XvDPp +-RaH1 !DHYxhr   