
    wgh                      d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZmZmZ dd	l m!Z!m"Z" dd
l#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZB ddlCmDZD ddlEmFZF ddlGmHZHmIZI ddlJmKZK ddlLmMZM ddlNmOZO ddlPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[ ddl\m]Z] ddl^m_Z_m`Z`maZambZb ddlcmdZd ddlemfZf ddlgmhZhmiZi ddljmkZkmlZlmmZmmnZn ddlompZpmqZq ddlrmsZsmtZt dd lumvZv dd!lwmxZx dd"lymzZzm{Z{ dd#l|m}Z}m~Z~mZmZmZmZmZmZ dd$lmZ dd%lmZ dd&lmZ dd'lmZ dd(lmZ dd)lmZmZ dd*lZd+ Zd, Zd- Zd. ZdHd/Zd0 Zd1 Zd2 Z	 	 dId3Zd4 Zdg g fd5Zd6 Zd7 Zd8 Zd9 Zd: Zd; ZdHd<Zd= Ze8d> e7d? iZd@ ZdA ZedBdCdD       ZdE ZdF ZddGlmZmZmZ y*)Ja  
This module contain solvers for all kinds of equations:

    - algebraic or transcendental, use solve()

    - recurrence, use rsolve()

    - differential, use dsolve()

    - nonlinear (numerically), use nsolve()
      (you will need a good starting point)

    )annotations)SAddSymbolDummyExprMul)check_assumptions)factor_terms)
expand_mul
expand_log
DerivativeAppliedUndefUndefinedFunctionnfloatFunctionexpand_power_exp_mexpandexpandexpand_func)	fuzzy_not)FloatRational_illegal)integer_logilcm)Pow)EqNe)ordereddefault_sort_key)sympify_sympify)preorder_traversal)AndBooleanAtom)logexpLambertWcossintanacosasinatanAbsreimargsqrtatan2)binomial)HyperbolicFunction)piecewise_fold	Piecewise)TrigonometricFunction)Integral)divisors)simplifycollectpowsimpposify	powdenest	nsimplifydenom
logcombine
sqrtdenestfractionseparatevars)
sqrt_depth)TR1TR2iTR10TR11)rebuild)NonInvertibleMatrixError)Matrixzeros)rootscancelfactorPoly)sympy_eqs_to_ringsolve_lin_sys)GeneratorsNeededPolynomialError)gcd)lambdify)
filldedentdebugf)connected_componentsgenerate_belluniqiterableis_sequencesubsetsflattensift)conserve_mpmath_dps)findroot)solve_poly_system)GeneratorType)defaultdict)combinationsproductNc                   t        |       st        |      rt        d      t        |      }t        t        |            }i }d}|D ].  }t	        |t
              r||vst        d|z        ||<   |dz  }0 g }| D ]:  }t        |dd      }||j                   ||             *|j                  |       < |j                         D 	ci c]  \  }}	|	|
 }
}}	||D cg c]  }|j                  ||       c}|
fS c c}	}w c c}w )a  
    Return (e, s, d) where e and s are versions of *eqs* and
    *symbols* in which any non-Symbol objects in *symbols* have
    been replaced with generic Dummy symbols and d is a dictionary
    that can be used to restore the original expressions.

    Examples
    ========

    >>> from sympy.solvers.solvers import recast_to_symbols
    >>> from sympy import symbols, Function
    >>> x, y = symbols('x y')
    >>> fx = Function('f')(x)
    >>> eqs, syms = [fx + 1, x, y], [fx, y]
    >>> e, s, d = recast_to_symbols(eqs, syms); (e, s, d)
    ([_X0 + 1, x, y], [_X0, y], {_X0: f(x)})

    The original equations and symbols can be restored using d:

    >>> assert [i.xreplace(d) for i in eqs] == eqs
    >>> assert [d.get(i, i) for i in s] == syms

    z%Both eqs and symbols must be iterabler   zX%d   subsN)r`   
ValueErrorlistr    
isinstancer   r   getattrappenditemsget)eqssymbolsorigswap_symisnew_fisubskvrestores              Z/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/solvers/solvers.pyrecast_to_symbolsr   F   s   0 C=Xg.@AA=D77#$GH	A !V$():	*HQKFA E 64(LLx)LLO !) 011q!t1G1t4!8<<1%4g== 24s   C?!Dc                b    t        | t              xr | j                  xs t        | t              S )z$Return True if e is a Pow or is exp.)rq   r   is_Powr(   es    r   _ispowr   s   s$    aCAHH$B
1c0BC    c                    t               }t        | |      D ]X  }|j                  r9|j                  j                  r#|j                  j
                  r<|j                  }|j                  |       Z |S N)setdenomsr   r(   	is_Numberis_zerobaseadd)frw   densds       r   _simple_densr   x   s[    
 5DAw 88uu}}A Kr   c                   t        |       }t               }|D ]U  }t        |t              st	        |      }|t
        j                  u r2|j                  t        j                  |             W |s|S t        |      dk(  rt        |d         r|d   }|D ch c]  t        fd|D              s c}S c c}w )aV  
    Return (recursively) set of all denominators that appear in *eq*
    that contain any symbol in *symbols*; if *symbols* are not
    provided then all denominators will be returned.

    Examples
    ========

    >>> from sympy.solvers.solvers import denoms
    >>> from sympy.abc import x, y, z

    >>> denoms(x/y)
    {y}

    >>> denoms(x/(y*z))
    {y, z}

    >>> denoms(3/x + y/z)
    {x, z}

    >>> denoms(x/2 + y/z)
    {2, z}

    If *symbols* are provided then only denominators containing
    those symbols will be returned:

    >>> denoms(1/x + 1/y + 1/z, y, z)
    {y, z}

    rm   r   c              3  :   K   | ]  }|j                   v   y wr   free_symbols).0r{   r   s     r   	<genexpr>zdenoms.<locals>.<genexpr>   s     "H11#6"H   )r$   r   rq   r   rC   r   Oneupdater	   	make_argslenr`   any)eqrw   potr   pdenr   s         `r   r   r      s    @ R
 C5D ( !T"Ah!%%<CMM#&'( 	W	GAJajGI!s"H"HHAIIIs   B<5B<c                p	   ddl m} |j                  dd      }|||i}n%t        |t              r|}nd}t        |||fz        t        |       r0| st        d      d}| D ]  }t        ||fi |}	|	r|	du r yd} |S t        |       } | j                  r| j                  S t        | t              r| j                         } nt        | t        t        f      r| j                  t         j"                  t         j$                  fv r| j&                  } | j(                  \  }
}t        |
t*              r| j-                  |      } | j.                  s4yt        | t              r#t1        | j2                  | j                   d	      } t        | t*              rt5        |       S | j6                  s| syt9        t:              t=        fd
|j?                         D              ryd}|j                  dd      }	 |dz  }|dk(  rJ| j-                  |      }t        |t@              r|jC                  |      d   }|jE                         z  ry|dk(  rkj                  s |jF                  tI        |jK                               d| isy|jM                         \  }}tO        |jQ                         d   d      }nu|dk(  r|ry|j                  dd      r|D ]  }tS        ||         ||<    tS        | j-                  |            }|j                  dd      rtU        |      \  }}tO        |d      }|j                  r|}ntW        tY                    }t9               }d}|D ]b  }||v r|j[                  |       |j\                  r|j^                  j`                  sd}n!|jb                  rd}nt        |td              rd}|sb n |du ry|j                  dd      r|j-                        }tg        |j                        }|%|j                  r|ji                  tj              ry| S n_|jl                  r|dk(  S |rJ|j                  r>to        |jq                  d      jq                  dd            dk  t         j"                  u S |j                  dd      rts        jt                  d|z         yy)a  
    Checks whether sol is a solution of equation f == 0.

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

    Input can be either a single symbol and corresponding value
    or a dictionary of symbols and values. When given as a dictionary
    and flag ``simplify=True``, the values in the dictionary will be
    simplified. *f* can be a single equation or an iterable of equations.
    A solution must satisfy all equations in *f* to be considered valid;
    if a solution does not satisfy any equation, False is returned; if one or
    more checks are inconclusive (and none are False) then None is returned.

    Examples
    ========

    >>> from sympy import checksol, symbols
    >>> x, y = symbols('x,y')
    >>> checksol(x**4 - 1, x, 1)
    True
    >>> checksol(x**4 - 1, x, 0)
    False
    >>> checksol(x**2 + y**2 - 5**2, {x: 3, y: 4})
    True

    To check if an expression is zero using ``checksol()``, pass it
    as *f* and send an empty dictionary for *symbol*:

    >>> checksol(x**2 + x - x*(x + 1), {})
    True

    None is returned if ``checksol()`` could not conclude.

    flags:
        'numerical=True (default)'
           do a fast numerical check if ``f`` has only one symbol.
        'minimal=True (default is False)'
           a very fast, minimal testing.
        'warn=True (default is False)'
           show a warning if checksol() could not conclude.
        'simplify=True (default)'
           simplify solution before substituting into function and
           simplify the function before trying specific simplifications
        'force=True (default is False)'
           make positive all symbols without assumptions regarding sign.

    r   )UnitminimalFNz;Expecting (sym, val) or ({sym: val}, None) but got (%s, %s)zno functions to checkTevaluatec              3  \   K   | ]#  \  }}t        |      j                         z   % y wr   )r"   atoms)r   r~   r   illegals      r   r   zchecksol.<locals>.<genexpr>  s'     
@DAq71:'
@s   ),	numericalrm   r=   	recursive   force      )chopg&.>warnz(
	Warning: could not verify solution %s.);sympy.physics.unitsr   ru   rq   dictro   r`   checksolr#   	is_numberr   rT   as_exprr   r   rhsr   truefalsereversedargsr&   rn   
is_Booleanr   lhsboolis_Relationalr   r   r   rt   r	   as_independentr   is_constantrp   keysas_content_primitiver   as_numer_denomr=   r@   r$   r   r   r   r(   
is_Integeris_Functionr   r   hasr)   is_Rationalabsnwarningsr   )r   symbolsolflagsr   r   msgrvficheckBEattemptr   val_r~   repsexvalr   seensaw_pow_funcr   nzr   s                           @r   r   r      s(   b )ii	5)G
sm	FD	!K},--{455 	BR..E~B	 	A{{yy!TIIK	ABx	 55QVVQWW%%

Avv1a%sA<<2AEEAEE6E2A![!Aw__Q(mG

@CIIK
@@G		+t,I
1a<&&+C#s#((.q1yy{W$\==&sSXXZ(8Ow;O 1133s113A6$G\yyT* .A%c!f-CF. 166#;'Cyy$'"3K	T 5??C %Z_5C5D L 988AEE$4$4#'L]]#'L#45#'L u$yy$'hhtn3;;'B~ ==SWWX%6   v??!8Ob	BT23d:qvvEES V yyBSHI  r   c                  >?@ ddl m} d}d}|D ]-  }|j                  |t        |j	                  ||                  }/ |j	                  dd      }|j	                  dd      }|j	                  dd      }	t        |        }
|j	                  d	d
      |j	                  dd
      st        d      |j	                  dd      r|
rt        t        d            d ??fd| fD        \  } d
}s t               j                  | D cg c]  }|j                   c} t              t        |       k  rR| D ]M  }t        |      }|D ];  }t        |t              s|	sd|d<   j                  |       |j!                          = O d}t#        t%                    nzt              dk(  rt        d         rd   xr t'        t(              }t#        t+                    }t        |      t              k7  rd}t#        t%                    n|t              t        t                    k7  rt        d      |j-                  dt                     }|r`t        |t.              r|g} t               j                  t1        |      D cg c]  }|j                   c} }D cg c]	  }||vs| c}t        | t"              r"| D cg c]  }|t2        j4                  us| } }t7        |       D ]  \  }}t        |t8        t:        f      r-d|j<                  D cg c]  }t?        |      j@                   c}v r|jB                  |jD                  z
  }n|j<                  \  }}t        |tF              r||}}t        |tF              ryt        |t:              r| }|jH                  r|t2        jJ                  u r| n|}nu|jL                  r|c S |jN                  r| jL                  r| c S tQ        t        d            t        |t8              r#tS        |jB                  |jD                   d      }|| |<   |jH                  rZt              dk7  rt        d      |r3d   jT                  r$tW        jX                  t        dd   z                ||       c S t        |tZ              r|j]                         | |<   | |   j_                  fdd       | |<   | |   j`                  r2d}
| jc                  t#        | |                t2        jd                  | |<   | |   j                  }|sDtg        d |D              sX| |   ji                         \  }}| |   jk                  tl        tn        tp        tr              @|s|s||k7  stu        @fd||fD              r|
rd}
||g| ||dz     tu        d | D              r|	rg t               fS g S t7        |       D ]{  \  }}	 |}|j_                  tv        fd       }||k(  rn"|jy                  tv              D ]  } |jz                   stQ        d!|z         |j_                  tp        d"       }|| |<   } | D cg c]  }|j{                  tl        tn              s|! }}|rg }D ]i  j|                  sj~                  rtu        fd#|D              s1|j                  tm              t2        j                  to              z  z   f       k |r`|D ]R  \  }| D cg c]  }|j                  |i       c}|z
  gz   } jc                  tm              to              g       T |
rd}
d|d<   t        |       \  } }t              }g }| D ]h  }d} |j                  |z  rd} n>|j                  r |j                  r|j                  r<g c S d} n|j                         rd} | sX|j                  |       j |s|	rt               fS g S |} ~t               }!t               }"| D ]  }t        |      }|D ]  }t        |t.              rt        |t              rnt        |t              sD|j<                  r8||v s4|j                  s(|j                  s|j                  r|r|j                  r|s|j                  tl        tn        fvr||!vr3|!j                  |       |j                  |z  r|"j                  |       n|j!                            ~!t        t#        t        |"|"D #cg c]  }#t                c}#                  }"| D cg c]  }|j                  |"       } }|"j                         D $cg c]'  \  }}$|$|j                  |      j                  |      f) }"}}$d}%|j	                  d$d      dur;t7        |       D ]-  \  }}|j{                  t              sd}%t        |d%      | |<   / t        | d         |d&<   >fd'>t7        |       D ]  \  }} >|      st        |      | |<    | D &ch c]  }|jk                  t              D ]  }&|&   }'}}&t        |'      dkD  rKt        |'d( d)      \  }(})|(|)fD *cg c]"  }*|*D cg c]  } |j                   s| c}$ c}}*\  }(})i }+|(D ]v  }&|&j<                  d   } |j                   \  },}-|-v s|- v s.|,j                  s
t               n|,}.t        |&j                  |-|.z               j                  |.|,i      |+|&<   x |)rgt        |)      d*k  rYt        |)      dkD  r#t        |)D cg c]  }|j<                  d    c} n|)d   j<                  d   }/|)D ]  }t        ||/      |+|<    | D cg c]  }|j                  |+       } }|
r5d
}0t              dk7  rt        | d   |      }0|0s#t        | d   gi |}0nt        | fi |\  }1}0t?        |0      t"        u sJ |0rt?        |0d         t        u sJ |0       |j	                  dd      }2fd+}3|2s|	rd
}4nl|
rIt              dk(  rfd,}4nVt        |0      dk(  rt        |0d         t              k(  rd- }4n*|r|3}4n%d. }4n!|0r1rt        |0      dk(  rd/ }4n|r|3}4nd0 }4nd
}4|"rUt?        |0      t"        u rD|0D $cg c]5  }|j                         D $ci c]  \  }}$||$j                  |"       c}$}7 }0}}}$|ryD cg c]  }|j	                  ||       c}t7        |0      D ]K  \  }}5|5j                         D $ci c]'  \  }}$|j	                  ||      |$j                  |      ) c}$}|0|<   M |j	                  d1d      }6|%r,|0r*|j	                  d$d
      t        |0d2      }0t        |0      }0|6r|0r|j	                  dd      }g }7g }8|0D ]b  }5d}9|5j                         D ]&  \  }:};t        |;fi |:jT                  }<|<r|<du r :d}9( |8j                  |5       |9sR|7j                  |5       d |8}0|r9|7r7tW        jX                  t        d3d4j                  d5 |7D              z                |0s|	rt               fS g S |	sB|0D cg c]  }t%        |      D ci c]  }|||   
 c}! }0}}|0j                  t        6       |	s
|2s |4|0      S |2r|0S |r}n-t#        t%        t        t        d7 |0D                                }||0D =ch c]+  }t        |D =cg c]  }=|j	                  |=|=       c}=      - c}=}fS c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}#w c c}w c c}$}w c c}&}w c c}w c c}}*w c c}w c c}w c c}$}w c c}$}}w c c}w c c}$}w c c}w c c}}w c c}=w c c}=}w )8aF  
    Algebraically solves equations and systems of equations.

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

    Currently supported:
        - polynomial
        - transcendental
        - piecewise combinations of the above
        - systems of linear and polynomial equations
        - systems containing relational expressions
        - systems implied by undetermined coefficients

    Examples
    ========

    The default output varies according to the input and might
    be a list (possibly empty), a dictionary, a list of
    dictionaries or tuples, or an expression involving relationals.
    For specifics regarding different forms of output that may appear, see :ref:`solve_output`.
    Let it suffice here to say that to obtain a uniform output from
    `solve` use ``dict=True`` or ``set=True`` (see below).

        >>> from sympy import solve, Poly, Eq, Matrix, Symbol
        >>> from sympy.abc import x, y, z, a, b

    The expressions that are passed can be Expr, Equality, or Poly
    classes (or lists of the same); a Matrix is considered to be a
    list of all the elements of the matrix:

        >>> solve(x - 3, x)
        [3]
        >>> solve(Eq(x, 3), x)
        [3]
        >>> solve(Poly(x - 3), x)
        [3]
        >>> solve(Matrix([[x, x + y]]), x, y) == solve([x, x + y], x, y)
        True

    If no symbols are indicated to be of interest and the equation is
    univariate, a list of values is returned; otherwise, the keys in
    a dictionary will indicate which (of all the variables used in
    the expression(s)) variables and solutions were found:

        >>> solve(x**2 - 4)
        [-2, 2]
        >>> solve((x - a)*(y - b))
        [{a: x}, {b: y}]
        >>> solve([x - 3, y - 1])
        {x: 3, y: 1}
        >>> solve([x - 3, y**2 - 1])
        [{x: 3, y: -1}, {x: 3, y: 1}]

    If you pass symbols for which solutions are sought, the output will vary
    depending on the number of symbols you passed, whether you are passing
    a list of expressions or not, and whether a linear system was solved.
    Uniform output is attained by using ``dict=True`` or ``set=True``.

        >>> #### *** feel free to skip to the stars below *** ####
        >>> from sympy import TableForm
        >>> h = [None, ';|;'.join(['e', 's', 'solve(e, s)', 'solve(e, s, dict=True)',
        ... 'solve(e, s, set=True)']).split(';')]
        >>> t = []
        >>> for e, s in [
        ...         (x - y, y),
        ...         (x - y, [x, y]),
        ...         (x**2 - y, [x, y]),
        ...         ([x - 3, y -1], [x, y]),
        ...         ]:
        ...     how = [{}, dict(dict=True), dict(set=True)]
        ...     res = [solve(e, s, **f) for f in how]
        ...     t.append([e, '|', s, '|'] + [res[0], '|', res[1], '|', res[2]])
        ...
        >>> # ******************************************************* #
        >>> TableForm(t, headings=h, alignments="<")
        e              | s      | solve(e, s)  | solve(e, s, dict=True) | solve(e, s, set=True)
        ---------------------------------------------------------------------------------------
        x - y          | y      | [x]          | [{y: x}]               | ([y], {(x,)})
        x - y          | [x, y] | [(y, y)]     | [{x: y}]               | ([x, y], {(y, y)})
        x**2 - y       | [x, y] | [(x, x**2)]  | [{y: x**2}]            | ([x, y], {(x, x**2)})
        [x - 3, y - 1] | [x, y] | {x: 3, y: 1} | [{x: 3, y: 1}]         | ([x, y], {(3, 1)})

        * If any equation does not depend on the symbol(s) given, it will be
          eliminated from the equation set and an answer may be given
          implicitly in terms of variables that were not of interest:

            >>> solve([x - y, y - 3], x)
            {x: y}

    When you pass all but one of the free symbols, an attempt
    is made to find a single solution based on the method of
    undetermined coefficients. If it succeeds, a dictionary of values
    is returned. If you want an algebraic solutions for one
    or more of the symbols, pass the expression to be solved in a list:

        >>> e = a*x + b - 2*x - 3
        >>> solve(e, [a, b])
        {a: 2, b: 3}
        >>> solve([e], [a, b])
        {a: -b/x + (2*x + 3)/x}

    When there is no solution for any given symbol which will make all
    expressions zero, the empty list is returned (or an empty set in
    the tuple when ``set=True``):

        >>> from sympy import sqrt
        >>> solve(3, x)
        []
        >>> solve(x - 3, y)
        []
        >>> solve(sqrt(x) + 1, x, set=True)
        ([x], set())

    When an object other than a Symbol is given as a symbol, it is
    isolated algebraically and an implicit solution may be obtained.
    This is mostly provided as a convenience to save you from replacing
    the object with a Symbol and solving for that Symbol. It will only
    work if the specified object can be replaced with a Symbol using the
    subs method:

        >>> from sympy import exp, Function
        >>> f = Function('f')

        >>> solve(f(x) - x, f(x))
        [x]
        >>> solve(f(x).diff(x) - f(x) - x, f(x).diff(x))
        [x + f(x)]
        >>> solve(f(x).diff(x) - f(x) - x, f(x))
        [-x + Derivative(f(x), x)]
        >>> solve(x + exp(x)**2, exp(x), set=True)
        ([exp(x)], {(-sqrt(-x),), (sqrt(-x),)})

        >>> from sympy import Indexed, IndexedBase, Tuple
        >>> A = IndexedBase('A')
        >>> eqs = Tuple(A[1] + A[2] - 3, A[1] - A[2] + 1)
        >>> solve(eqs, eqs.atoms(Indexed))
        {A[1]: 1, A[2]: 2}

        * To solve for a function within a derivative, use :func:`~.dsolve`.

    To solve for a symbol implicitly, use implicit=True:

        >>> solve(x + exp(x), x)
        [-LambertW(1)]
        >>> solve(x + exp(x), x, implicit=True)
        [-exp(x)]

    It is possible to solve for anything in an expression that can be
    replaced with a symbol using :obj:`~sympy.core.basic.Basic.subs`:

        >>> solve(x + 2 + sqrt(3), x + 2)
        [-sqrt(3)]
        >>> solve((x + 2 + sqrt(3), x + 4 + y), y, x + 2)
        {y: -2 + sqrt(3), x + 2: -sqrt(3)}

        * Nothing heroic is done in this implicit solving so you may end up
          with a symbol still in the solution:

            >>> eqs = (x*y + 3*y + sqrt(3), x + 4 + y)
            >>> solve(eqs, y, x + 2)
            {y: -sqrt(3)/(x + 3), x + 2: -2*x/(x + 3) - 6/(x + 3) + sqrt(3)/(x + 3)}
            >>> solve(eqs, y*x, x)
            {x: -y - 4, x*y: -3*y - sqrt(3)}

        * If you attempt to solve for a number, remember that the number
          you have obtained does not necessarily mean that the value is
          equivalent to the expression obtained:

            >>> solve(sqrt(2) - 1, 1)
            [sqrt(2)]
            >>> solve(x - y + 1, 1)  # /!\ -1 is targeted, too
            [x/(y - 1)]
            >>> [_.subs(z, -1) for _ in solve((x - y + 1).subs(-1, z), 1)]
            [-x + y]

    **Additional Examples**

    ``solve()`` with check=True (default) will run through the symbol tags to
    eliminate unwanted solutions. If no assumptions are included, all possible
    solutions will be returned:

        >>> x = Symbol("x")
        >>> solve(x**2 - 1)
        [-1, 1]

    By setting the ``positive`` flag, only one solution will be returned:

        >>> pos = Symbol("pos", positive=True)
        >>> solve(pos**2 - 1)
        [1]

    When the solutions are checked, those that make any denominator zero
    are automatically excluded. If you do not want to exclude such solutions,
    then use the check=False option:

        >>> from sympy import sin, limit
        >>> solve(sin(x)/x)  # 0 is excluded
        [pi]

    If ``check=False``, then a solution to the numerator being zero is found
    but the value of $x = 0$ is a spurious solution since $\sin(x)/x$ has the well
    known limit (without discontinuity) of 1 at $x = 0$:

        >>> solve(sin(x)/x, check=False)
        [0, pi]

    In the following case, however, the limit exists and is equal to the
    value of $x = 0$ that is excluded when check=True:

        >>> eq = x**2*(1/x - z**2/x)
        >>> solve(eq, x)
        []
        >>> solve(eq, x, check=False)
        [0]
        >>> limit(eq, x, 0, '-')
        0
        >>> limit(eq, x, 0, '+')
        0

    **Solving Relationships**

    When one or more expressions passed to ``solve`` is a relational,
    a relational result is returned (and the ``dict`` and ``set`` flags
    are ignored):

        >>> solve(x < 3)
        (-oo < x) & (x < 3)
        >>> solve([x < 3, x**2 > 4], x)
        ((-oo < x) & (x < -2)) | ((2 < x) & (x < 3))
        >>> solve([x + y - 3, x > 3], x)
        (3 < x) & (x < oo) & Eq(x, 3 - y)

    Although checking of assumptions on symbols in relationals
    is not done, setting assumptions will affect how certain
    relationals might automatically simplify:

        >>> solve(x**2 > 4)
        ((-oo < x) & (x < -2)) | ((2 < x) & (x < oo))

        >>> r = Symbol('r', real=True)
        >>> solve(r**2 > 4)
        (2 < r) | (r < -2)

    There is currently no algorithm in SymPy that allows you to use
    relationships to resolve more than one variable. So the following
    does not determine that ``q < 0`` (and trying to solve for ``r``
    and ``q`` will raise an error):

        >>> from sympy import symbols
        >>> r, q = symbols('r, q', real=True)
        >>> solve([r + q - 3, r > 3], r)
        (3 < r) & Eq(r, 3 - q)

    You can directly call the routine that ``solve`` calls
    when it encounters a relational: :func:`~.reduce_inequalities`.
    It treats Expr like Equality.

        >>> from sympy import reduce_inequalities
        >>> reduce_inequalities([x**2 - 4])
        Eq(x, -2) | Eq(x, 2)

    If each relationship contains only one symbol of interest,
    the expressions can be processed for multiple symbols:

        >>> reduce_inequalities([0 <= x  - 1, y < 3], [x, y])
        (-oo < y) & (1 <= x) & (x < oo) & (y < 3)

    But an error is raised if any relationship has more than one
    symbol of interest:

        >>> reduce_inequalities([0 <= x*y  - 1, y < 3], [x, y])
        Traceback (most recent call last):
        ...
        NotImplementedError:
        inequality has more than one symbol of interest.

    **Disabling High-Order Explicit Solutions**

    When solving polynomial expressions, you might not want explicit solutions
    (which can be quite long). If the expression is univariate, ``CRootOf``
    instances will be returned instead:

        >>> solve(x**3 - x + 1)
        [-1/((-1/2 - sqrt(3)*I/2)*(3*sqrt(69)/2 + 27/2)**(1/3)) -
        (-1/2 - sqrt(3)*I/2)*(3*sqrt(69)/2 + 27/2)**(1/3)/3,
        -(-1/2 + sqrt(3)*I/2)*(3*sqrt(69)/2 + 27/2)**(1/3)/3 -
        1/((-1/2 + sqrt(3)*I/2)*(3*sqrt(69)/2 + 27/2)**(1/3)),
        -(3*sqrt(69)/2 + 27/2)**(1/3)/3 -
        1/(3*sqrt(69)/2 + 27/2)**(1/3)]
        >>> solve(x**3 - x + 1, cubics=False)
        [CRootOf(x**3 - x + 1, 0),
         CRootOf(x**3 - x + 1, 1),
         CRootOf(x**3 - x + 1, 2)]

    If the expression is multivariate, no solution might be returned:

        >>> solve(x**3 - x + a, x, cubics=False)
        []

    Sometimes solutions will be obtained even when a flag is False because the
    expression could be factored. In the following example, the equation can
    be factored as the product of a linear and a quadratic factor so explicit
    solutions (which did not require solving a cubic expression) are obtained:

        >>> eq = x**3 + 3*x**2 + x - 1
        >>> solve(eq, cubics=False)
        [-1, -1 + sqrt(2), -sqrt(2) - 1]

    **Solving Equations Involving Radicals**

    Because of SymPy's use of the principle root, some solutions
    to radical equations will be missed unless check=False:

        >>> from sympy import root
        >>> eq = root(x**3 - 3*x**2, 3) + 1 - x
        >>> solve(eq)
        []
        >>> solve(eq, check=False)
        [1/3]

    In the above example, there is only a single solution to the
    equation. Other expressions will yield spurious roots which
    must be checked manually; roots which give a negative argument
    to odd-powered radicals will also need special checking:

        >>> from sympy import real_root, S
        >>> eq = root(x, 3) - root(x, 5) + S(1)/7
        >>> solve(eq)  # this gives 2 solutions but misses a 3rd
        [CRootOf(7*x**5 - 7*x**3 + 1, 1)**15,
        CRootOf(7*x**5 - 7*x**3 + 1, 2)**15]
        >>> sol = solve(eq, check=False)
        >>> [abs(eq.subs(x,i).n(2)) for i in sol]
        [0.48, 0.e-110, 0.e-110, 0.052, 0.052]

    The first solution is negative so ``real_root`` must be used to see that it
    satisfies the expression:

        >>> abs(real_root(eq.subs(x, sol[0])).n(2))
        0.e-110

    If the roots of the equation are not real then more care will be
    necessary to find the roots, especially for higher order equations.
    Consider the following expression:

        >>> expr = root(x, 3) - root(x, 5)

    We will construct a known value for this expression at x = 3 by selecting
    the 1-th root for each radical:

        >>> expr1 = root(x, 3, 1) - root(x, 5, 1)
        >>> v = expr1.subs(x, -3)

    The ``solve`` function is unable to find any exact roots to this equation:

        >>> eq = Eq(expr, v); eq1 = Eq(expr1, v)
        >>> solve(eq, check=False), solve(eq1, check=False)
        ([], [])

    The function ``unrad``, however, can be used to get a form of the equation
    for which numerical roots can be found:

        >>> from sympy.solvers.solvers import unrad
        >>> from sympy import nroots
        >>> e, (p, cov) = unrad(eq)
        >>> pvals = nroots(e)
        >>> inversion = solve(cov, x)[0]
        >>> xvals = [inversion.subs(p, i) for i in pvals]

    Although ``eq`` or ``eq1`` could have been used to find ``xvals``, the
    solution can only be verified with ``expr1``:

        >>> z = expr - v
        >>> [xi.n(chop=1e-9) for xi in xvals if abs(z.subs(x, xi).n()) < 1e-9]
        []
        >>> z1 = expr1 - v
        >>> [xi.n(chop=1e-9) for xi in xvals if abs(z1.subs(x, xi).n()) < 1e-9]
        [-3.0]

    Parameters
    ==========

    f :
        - a single Expr or Poly that must be zero
        - an Equality
        - a Relational expression
        - a Boolean
        - iterable of one or more of the above

    symbols : (object(s) to solve for) specified as
        - none given (other non-numeric objects will be used)
        - single symbol
        - denested list of symbols
          (e.g., ``solve(f, x, y)``)
        - ordered iterable of symbols
          (e.g., ``solve(f, [x, y])``)

    flags :
        dict=True (default is False)
            Return list (perhaps empty) of solution mappings.
        set=True (default is False)
            Return list of symbols and set of tuple(s) of solution(s).
        exclude=[] (default)
            Do not try to solve for any of the free symbols in exclude;
            if expressions are given, the free symbols in them will
            be extracted automatically.
        check=True (default)
            If False, do not do any testing of solutions. This can be
            useful if you want to include solutions that make any
            denominator zero.
        numerical=True (default)
            Do a fast numerical check if *f* has only one symbol.
        minimal=True (default is False)
            A very fast, minimal testing.
        warn=True (default is False)
            Show a warning if ``checksol()`` could not conclude.
        simplify=True (default)
            Simplify all but polynomials of order 3 or greater before
            returning them and (if check is not False) use the
            general simplify function on the solutions and the
            expression obtained when they are substituted into the
            function which should be zero.
        force=True (default is False)
            Make positive all symbols without assumptions regarding sign.
        rational=True (default)
            Recast Floats as Rational; if this option is not used, the
            system containing Floats may fail to solve because of issues
            with polys. If rational=None, Floats will be recast as
            rationals but the answer will be recast as Floats. If the
            flag is False then nothing will be done to the Floats.
        manual=True (default is False)
            Do not use the polys/matrix method to solve a system of
            equations, solve them one at a time as you might "manually."
        implicit=True (default is False)
            Allows ``solve`` to return a solution for a pattern in terms of
            other functions that contain that pattern; this is only
            needed if the pattern is inside of some invertible function
            like cos, exp, ect.
        particular=True (default is False)
            Instructs ``solve`` to try to find a particular solution to
            a linear system with as many zeros as possible; this is very
            expensive.
        quick=True (default is False; ``particular`` must be True)
            Selects a fast heuristic to find a solution with many zeros
            whereas a value of False uses the very slow method guaranteed
            to find the largest number of zeros possible.
        cubics=True (default)
            Return explicit solutions when cubic expressions are encountered.
            When False, quartics and quintics are disabled, too.
        quartics=True (default)
            Return explicit solutions when quartic expressions are encountered.
            When False, quintics are disabled, too.
        quintics=True (default)
            Return explicit solutions (if possible) when quintic expressions
            are encountered.

    See Also
    ========

    rsolve: For solving recurrence relationships
    dsolve: For solving differential equations

    rm   )reduce_inequalitiescubicsquarticsquinticsTimplicitFr   r   quickN
particularz/when using `quick`, `particular` should be Truez
            The 'particular/quick' flag is usually used with systems of
            equations. Either pass your equation in a list or
            consider using a solver like `diophantine` if you are
            looking for a solution in integers.c                `    t        t        t        t        |       r
|             S | g            S r   )rp   mapr"   r`   ws    r   _sympified_listzsolve.<locals>._sympified_listj  s'    Chqk;<<s;<<r   c              3  .   K   | ]  } |        y wr    )r   r   r   s     r   r   zsolve.<locals>.<genexpr>l  s     ;/!$;   r   r   )includezduplicate symbols givenexcludeImmutableDenseMatrixz
                            Unanticipated argument of Eq when other arg
                            is True or False.
                        r   z'can only solve for one symbol at a timezm
                    	Warning: assumptions about variable '%s' are
                    not handled currently.rw   c                F    t        | t              xr  | j                   S r   )rq   r7   has_free)r   rw   s    r   <lambda>zsolve.<locals>.<lambda>  s%    j4F&G '!AJJ  r   c                ,    | j                  t              S r   )rewriter(   r   s    r   r   zsolve.<locals>.<lambda>  s    AIIcN r   c              3  P   K   | ]  }|j                   xs |j                     y wr   )is_extended_realis_imaginaryr   r{   s     r   r   zsolve.<locals>.<genexpr>  s"     M!++=q~~=Ms   $&c              3  l   K   | ]+  }|j                  t        t        t        t              z
   - y wr   )r   r1   r2   r3   r5   )r   rz   hads     r   r   zsolve.<locals>.<genexpr>  s,      2I:;AGGBC/#52Is   14c              3  H   K   | ]  }t        |t        t        f        y wr   )rq   r   r&   )r   r   s     r   r   zsolve.<locals>.<genexpr>  s     
;2:b4-.
;s    "c                     | j                    r't        t        |             j                  t              S t        |       S r   )r   rG   r0   r   r9   )r3   rw   s    r   r   zsolve.<locals>.<lambda>  s8    =DSWWg=NSX&..y9 X r   z6solving %s when the argument is not real or imaginary.c                d    t        |       j                  t              j                  t              S r   )r3   r   r5   r/   )as    r   r   zsolve.<locals>.<lambda>  s    s1v~~e'<'D'DT'J r   c              3  f   K   | ](  }|j                  t              t                     * y wr   )r   r1   r2   )r   r   r{   s     r   r   zsolve.<locals>.<genexpr>  s$     8B266"Q%A'8s   .1rational)r  _denominatorsc                v    | j                   r | j                   S t        fd| j                  D              S )Nc              3  .   K   | ]  } |        y wr   r   )r   r  _has_piecewises     r   r   z0solve.<locals>._has_piecewise.<locals>.<genexpr>n  s     5>!$5r   )is_Piecewiser   r   r   )r   r  rw   s    r   r  zsolve.<locals>._has_piecewisek  s/    >>155'?"5aff555r   c                4    | j                   d   j                  S Nr   )r   is_Addxs    r   r   zsolve.<locals>.<lambda>z  s    166!9+;+; r   )binaryr   c                    | D cg c]+  }t        D cg c]  }|j                  ||       c}      - c}}S c c}w c c}}w r   )tupleru   )r{   rz   r  rw   s      r   r   zsolve.<locals>.<lambda>  s2    QOe'$BQQUU1a[$BCO $BOs   A ;	A A c                :    | D cg c]
  }|d       c}S c c}w r  r   )r{   rz   rw   s     r   r   zsolve.<locals>.<lambda>  s    q9!'!*9 9s   c                    | d   S r  r   r{   s    r   r   zsolve.<locals>.<lambda>  s
    qt r   c                    | S r   r   r  s    r   r   zsolve.<locals>.<lambda>  s    q r   c                    | d   S r  r   r  s    r   r   zsolve.<locals>.<lambda>  s
    1Q4 r   c                    | S r   r   r  s    r   r   zsolve.<locals>.<lambda>  s    1 r   r   )exponentzl
                	Warning: assumptions concerning following solution(s)
                cannot be checked:
	, c              3  2   K   | ]  }t        |        y wr   )strr   s     r   r   zsolve.<locals>.<genexpr>  s     3Q#a&3   keyc              3  N   K   | ]  }t        |j                                 y wr   )r  r   r   rz   s     r   r   zsolve.<locals>.<genexpr>  s     $GU1668_$Gs   #%)iinequalitiesr   
setdefaultr   ru   r`   ro   r[   r   unionr   r   r$   rq   r   r   skiprp   r    ra   rh   r_   popr   r"   r   r   	enumerater   r   r   type__name__r   r   r&   r   r   	is_Symbolr   NotImplementedErrorr   assumptions0r   r   rT   r   replace	is_MatrixextendZeroallas_real_imagr   r1   r2   r3   r5   r   r0   findr   is_realr   rs   ImaginaryUnitxreplacer   r   r   r   r   r9   r  is_Mulr   r   funcr   zipr   rn   rt   r   rB   r   r8   r:   rd   r   r   rK   rY   rL   _solve_undetermined_solve_solve_systemr   _remove_duplicate_solutionsr
   joinsortr!   rc   r  )Ar   rw   r   r   hintsdefaultr~   r   r   as_setbare_fordered_symbolsr   r   r   _symbolsr   r   r{   rz   r  LRfreeifrwasfreimirfr   ry   symsetnewfokr   non_invertsr   r   floatsttargsr   otherltrepinddepr   r   solutionlinearas_dicttuple_formatunpackr   r   got_Noneno_Falsea_Nonesymbr   testkir  r   r  sA    `                `                                           @@@r   solverf  r  s(   ` 2 /EG C""1d599Q+@&ABC yyU+H 99VU#D YYue$F !_F yy$+yyt,NOOyyu%& %3 4 5 	5=;q'l;JAw
 O#%++!<B<=w<#a&  #(, #A!!\2%,0E&MA
##  ww'(w<1'!*!5ajG! /k' -'/W&x=CL(#O77+,GG 7|s3w<((233ii	35)Ggt$iG#%++8HI1IJ%:')91: !T-1Qaff_Q-- 1 >'2b2r(#%BGG)Lq$q'*:*:)LLVVbff_ww1a-aqAa-!"b)B#$<aRQ A2.. !r	1* > 3    B'RVVbffWu=BAaD 7|q  !JKK
//j *.07
*; < = 'q':: b$::<AaD t|| !":<! Q4>>FHHT!A$Z 66AaD !!!SMuMMqT&&(FBA$**RS%0CbR2Xc 2I@BBx2I /I"F!2h!QU}>'B 
;
;;su9	1 2CC " B by   	5Aquug) +023+4 5 5	5 ZZJK !', .Brvvb"~R.E. 	?AyyANN8%88

Ar!uqr!u'<<=>	?  /3567rR[[!S*71s7)C1r!u~./  E&M -Q8Aw\F D  ??V#B||<<zz I>>#BKKO9: CE>!	A
 5D%K  $ 	Aa&*Q	*BQ%KHHHHXMM(r2h8N$>>F*OOA&HHJ!	& 	tC{-K!eg-KLMNK()*"	*A* ITHYHYH[\1Aqzz(+00:;\K\ FyyT"%/q\ 	4EArvve} d3!	4 *!A$8E/6 1 &2"!"%AaD& D2BHH-B$CDqQDQDED
5zA~%!;DI
UGJElS!<Qzqzz7';q<S
U 	CAq	A'q''1HCg~##&==EGcqvvcAg/88!SBQ	C SZ1_7:5zA~3E2q235QR8==YZK[D (q$-Q()*+2R[[++
 w<1*1Q4%@Had6W66H(G=u=>T!!!4,4>h>4
 ii&G PL&	w<19F]aC$4G$D#F!F F#h-1, ( %$F tH~-  9:	B1Q{++B   /67!8<<1%7) 	/FAs"%))+/!Q $<<1-qvvh/?? /HQK	/ IIgt$E (uyyT:B(U3.x8yy' 	)CF YY[ 
)	c(B0A0AB5=
) $OOC(	) HMM* &/ 		3(33&4 5 6 CE>!	 ;CDagaj1Q!W1DD*+gh  W$Gh$GGHIJ8Daua0aeeBm01DDD{  =@  J: .
 *Mt / 8\ .L* ]> E =S 3 ,d C  8/d 2D, 1Ds   
}'},1	}1;}1}6.}6,};;~ ~ !~+~
~<,~#~	~%~ /~ 3~%#~+)~0~;5~5~;!!,-?2 	 ~%5~;c           	         | j                   t        |      z
  r*t        | |fi t        |dd      }t	        |      dk(  r|S yy)a  solve helper to return a list with one dict (solution) else None

    A direct call to solve_undetermined_coeffs is more flexible and
    can return both multiple solutions and handle more than one independent
    variable. Here, we have to be more cautious to keep from solving
    something that does not look like an undetermined coeffs system --
    to minimize the surprise factor since singularities that cancel are not
    prohibited in solve_undetermined_coeffs.
    TN)r   r   rm   )r   r   solve_undetermined_coeffsr   r   )grw   r   r   s       r   r<  r<    sI     	~~G$'7Wd5tQU6VWs8q=J  %r   c           
     Z  C d}t        |      dk7  r	g }t               }t               }g }|D ]  t        | g      \  }}	|k(  rYj                  dd      rt	        |	      }	|	j
                  }
|
|z  rH||
z  }|j                  |       |j                  ||	i       r|su|j                          |s|S |D ]L  	 t        | fi }|D ]7  }|   j
                  |z  r|j                         |j                  |       9 N |r|S t        || z        |d   C| j                  Cfdd       } j                  d	d      xd	<   x}}| j                  rt               }| j                  D ]f  }|t        j                  t        j                   t        j"                  hv rt               } n)t%        |Cfi }|j'                  t        |             h |D 	cg c]  }	C|	i }}	|rAj                  d
t)        | |            }|D cg c]  t+        fd|D              s }}d}dd<   n_| j,                  r?t               }t+        d | j                  D              r| j	                         } dx}}| j                  D ]  \  }}t/        ||      }t/        ||       }|j0                  r*|j	                         dk7  rt        t3        d|z              t%        |Cfi }|D ]~  }||v r	 |j5                  C|      }	t7        |	dd      }| |dd       }	|	dk(  r;|	dk(  r|j                  |       R|j                  t;        ||	ft        j<                  df               |D cg c]  }C|i c}S t        | |      \  }}|j0                  s|t        j<                  u rg S |j>                  r"j                  dd      rt	        |      }||igS d}|j@                  sz|jC                  tD              D cg c]  }C|j
                  v r| }}t        |      dk(  r<|d   }|jG                  C      d   jI                         d   }|Ck(  r	 tK        ||      }d}d} 	 |tK        |      }|tO        d|z        	 |jR                  D "cg c]  }"|"jU                  C      s|" }#}"d }$t        |#      dkD  r}tW        tY        |#D "cg c]
  }" |$|"       c}"       \  }%}&t        |%      }%t        |%      dkD  st[        d |&D              sw|%D 'ch c]  }'|'j\                  s|' }(}'|(D )ch c]  })t_        |)t`              r|) }*})|(|*z
  }+|+st        |(jc                  |*            dkD  rkd},|j@                  r t        |j                        dk(  r|j                  }-|-D cg c]&  }|jC                  td              jc                  |*      ( c}x}.\  }/}'t[        d |.D              r|.D cg c]  }|j                          c}\  }/}'t_        |/tf              r|'|/}'}/|-ddd   }-t_        |/th              ret_        |'tf              rU|/j                  d   |'j                  d   k(  r6tk        |-d   |-d   z        dz   jm                         \  },}0|0jn                  sd},|,tq        |      js                  tt              },|,|k7  rdd	<   t        |,Cfi }|d	<   |du r|(rtW        tw        |(            }(|(d   }1ty        d      }.|j5                  |1|.      }2|2jU                  C      sRt        |2|.fi }3tW        tw        t%        |.|1z
  Cfi             d   }4|3D cg c]  }C|4j5                  |      i }}|du r	d|#z  } n|%j                         }5ty        d      }.t%        |5|.z
  Cfi }6t_        |5tz        t|        f      rd }7|j                  d |7      j5                  |5|.      }2|2jU                  C      st        |2|.fi }|6D cg c]  }|D ]  }C|j5                  |      i  }}}nPt        |#      dk(  rAtK        ||#d   d      }j                  d d      sd}|j                         }8dd <   d!}9|9D :ci c]  }:|:j                  |:       };}:t        |fi |;}t        |j                               |8k  r	 |j                         }ntW        |j                               }|}|j                  }5|5Ck7  rE	 ty        d      }.t%        |5|.z
  Cfi }6|6D ch c]  }|D ]  }|j5                  |.|        }}}nd}|#t        |      dkD  rj                  dd      d<   ||D 	cg c]  }	C|	i }}	|du rj                  d$d      r	 t        C      }5|5r|5\  }<}=|=r?|=\  }>}?t%        |?Cfi d   }6t        |<|>fi D ch c]  }|6j5                  |       }@}n	 t        t%        |<Cfi       }@@(@D 	cg c]  }	C|	i }}	j                  dd      d<   n	 |du r3j                  d d       	 t        Cfi }||D 	cg c]  }	C|	i }}	|du rt        d%j                   || z  g            t        |      }j                  dd      rA|D ABcg c]$  }A|AD Bci c]  }B|BA|B   j	                          c}B& }}A}Bt        |      }dd<   |r1t)        | |      }|D cg c]  t+        fd&|D              s }}|r|D cg c]  }t        |fi dur| }}|S # t        $ r Y 
w xY wc c}	w c c}w # t8        $ r Y w xY wc c}w c c}w # tL        $ r Y &w xY w# tP        $ r+ t	        |      }!|!|k7  rt        |!Cfi cY S tO        d      w xY wc c}"w c c}"w c c}'w c c})w c c}w c c}w c c}w c c}}w c c}:w # t        $ r* j                  d"d      st        t3        d#            Y &w xY wc c}}w # t        $ r d}Y w xY wc c}	w # tN        t        f$ r d}5Y w xY wc c}w # t        $ r d}@Y ^w xY wc c}	w c c}	w # tL        $ r Y w xY wc c}Bw c c}B}Aw c c}w c c}w )'a=  Return a checked solution for *f* in terms of one or more of the
    symbols in the form of a list of dictionaries.

    If no method is implemented to solve the equation, a NotImplementedError
    will be raised. In the case that conversion of an expression to a Poly
    gives None a ValueError will be raised.
    z2No algorithms are implemented to solve equation %srm   r   r=   Tr   c                J    t        | t              xr | j                        S r   )rq   r6   r   )r   r   s    r   r   z_solve.<locals>.<lambda>h  s    Jq(3Ef r   c                    t        |       S r   )r   r   s    r   r   z_solve.<locals>.<lambda>i  s
    +a. r   r   r  c              3  8   K   | ]  }t        |fi   y wr   r   )r   r   r   r{   s     r   r   z_solve.<locals>.<genexpr>  s!      ca151    Fc              3  :   K   | ]  \  }}|j                     y wr   )r   )r   r   cs      r   r   z_solve.<locals>.<genexpr>  s     ,TQqyy,s   az  
                    An expression is already zero when %s.
                    This means that in this *region* the solution
                    is zero but solve can only represent discrete,
                    not interval, solutions. If this is a spurious
                    interval it might be resolved with simplification
                    of the Piecewise conditions._eval_simplifyNr   c                     yNrm   r   r  s    r   r   z_solve.<locals>.<lambda>  s    r   )ratiomeasure zcould not convert %s to Polyz#expression appears to be a constantc                   | j                         \  }}|j                  r||j                  fS |j                  s| dfS |j	                         \  }}|j                  r#|t
        j                  ur||z  |j                  fS | dfS )zReturn (b**e, q) for x = b**(p*e/q) where p/q is the leading
            Rational of the exponent of x, e.g. exp(-2*x/3) -> (exp(x), 3)
            rm   )as_base_expr   qr9  as_coeff_Mulr   r   )r  br   rq  ees        r   
_as_base_qz_solve.<locals>._as_base_q  ss     ==?DAq}}!##v88!tNN$EAr}}!%%"uaccz!a4Kr   c              3  &   K   | ]	  }|d k(    ywrm   Nr   )r   rz  s     r   r   z_solve.<locals>.<genexpr>  s     (<Aa(<   c              3  8   K   | ]  }t        |      d k(    ywr  r   r#  s     r   r   z_solve.<locals>.<genexpr>  s     6qs1v{6   r   rT  zmultiple generators %sc                Z    | j                         \  }}t        |      }t        ||z        S r   )ry  r   r   )r   r|  r   s      r   _expandz_solve.<locals>._expandK  s)     }}1&qM/155r   c                >    | j                   xs t        | t              S r   )r   rq   r(   r   s    r   r   z_solve.<locals>.<lambda>P  s    !(("@jC.@ r   )	compositetsolver   
incompletea  
    Neither high-order multivariate polynomials
    nor sorting of EX-domain polynomials is supported.
    If you want to see any results, pass keyword incomplete=True to
    solve; to see numerical values of roots
    for univariate expressions, use nroots.
    _unrad
c              3  8   K   | ]  }t        |fi   y wr   rn  r   r   r   rs     r   r   z_solve.<locals>.<genexpr>  s%      ) #1a151 )ro  )Kr   r   solve_linearru   r=   r   r   rs   r=  r-  r/  r(  r9  r   r   NegativeInfinityComplexInfinityInfinity_vsolver   r   r   r  r%   r   r[   rn   rr   	TypeErrorr9   NaNr,  r  r   r   r   ry  rT   rX   ro   rW   gensr   rp   r;  r3  r   rq   r:   intersectionr   r*   r+   rJ   r   r   rI   r   r,   r    r   r   r(   degreerQ   sumvalues	all_rootsr   genunrad_tsolver@  r?  r   )Dr   rw   r   not_impl_msgnonlin_sgot_srhs_sresultxir   vfreesolnr   	checkdensr   mr   r{   condnegexprcnd
candidates	candidaterr  r  f_numpolyrz   add_argsr  spartr   simplified_fri  r  r~  basesqsr|  funcsr   trigrV  rP  _argsrT  r  _df1ftrycv_solscv_invuinvr  degrB  hsolversr   covisymieqr   r   r~   r   sD     `              `       `                                         @r   r=  r=  $  sZ    HL
7|q  	#A QC0EBQw99Z. A5= 		"r1g&"!	#" M 
	A	a,e, 'C1v**U2 IIaLMM#&'
	 M%lQ&677 QZF 	
		E 	"A */7D)AAE'NAY 	xx 	%AQ''):):AJJGG1f..DMM#d)$	% (..!61+.. 99_l1g.FGD!' A    F 
 !j	
,QVV,,

As $	(ID# sC=DcD5/C||5 8)* 64 7;6; +< = = !v77J' (	&		&)4A%,Q0@$%GN%1*KH :9JJy)JJy"A ' (%(!$	(N &,,,, "!W5
s==C155LI__yyT*smCL>!||#(;;s#3 -aQ^^+  -H -8}!qk**6215AACAFF?#E51 		D|E{| !?%!GHH   996af66	 t9q= S$"?Q:a="?@AIE2JE5zA~S(<(<%<$);qQ]];;#( :aq"78  : :U%7%7%=!>!BD||EJJ1(< %

+0$2&' %&GGH$5$B$B %" $2 2DAq6A6656#7AEEG#7DAq)!S1'(!1(-dd)!S1jC 7&*+&&)qvvay*@,0q%(1B,Ca,G&4n&6 !)b')||+/D|"5z11#6u} */g!'f!>!>).g U?u 0EqBc
A ::b!,D  88F+"(q":E":!%gga"ff.N.N&O!PQR!SHO!P66;;s+;"<!P!PU?2T9C IIK#Ja!eV5u5a#s,6 !==@!!%a   88F+%dA77?B!Q!D!Qq6166!9"5!Q"5!Q!QY!^ tAw$7D 99Xu-kkm"&h:49:q1eiil?::T-W-t{{}%+!#~~/  		,D#AF{( %c
A")!a%"A5"AC:=#LQt#L!AFF1aL#LL#LD#L
 !&'t9q= 16		*e0LE*-#378avqk8F8 99Xt$%( C #ID#!#v77:C17D1JE1JK2#((2,KBK" V!=u!=> >356avqk6F6 ).		*e(DE*% 		(D!	5&2E2D/34!61+44
 !$))S,2B,C"DEE(0FyyT";ABaq1!1admmo%1BB,V4 "j Aw'# * )#') )  * * # <5!-u-U:  < <My ' : /P !  - - + (   	D#E?Lu$lF<e<<BCC		D 76 #@ <:$2 $8H "Q< "R& ; / !$yyt<&9 * , !	'
 !
 !!* $M2 (#'D(  9  34  L / "!" 7 5 		 2B*
<sW  A	m"m#m(;,m-9m=
n!n 2n o5oo%o7oo:+o"o'0o,$!o1o7o<  p8 < p2p8 q
1q *q'q, "q>r 0r;r 	rr&rr#7r(	m m -	m:9m:	nn&o?o</p/.p/2p8 8qqq$#q$,q;:q;r 	rrrc                    t               }g }| D ]B  }t        |j                               }||vs!|j                  |       |j	                  |       D |S )z&Remove duplicates from a list of dicts)r   	frozensetrt   rs   r   )	solutionssolutions_setsolutions_newr   solsets        r   r?  r?    s[     EMM &399;'&  %f%	& r   c                  -=>?@A | sdg fS j                  dd      rM| }t        |      }| D ci c]  }||j                  |z   }}g }t        |      D 	ci c]  \  }}	|	|
 c}	}At        |       D ].  \  }
}| d|
 D ]!  }||   ||   z  s|j	                  ||f       # 0 ||f}t        |      }t        |      dkD  rg }d}|D ]l  }t               }|D ]
  }|||   z  } t        |Afd      }dd<   t        ||fi \  }}|r|xr |}t        |t              s|g}|j	                  |       n g }t        | D ]#  }|j	                  t        d |D                     % ||fS g }t               }g }g }g }d}j                  d	d      }j                  d
d      x}}t        |       D ]  \  }} |j                  t        | |             t!        | g| \  }}!|!|v r|rt#        | d|!g      d   |!k(  }|!|z
  } | j%                         d   } |r|j	                  |        v | j&                  |ddi}"|"|j	                  |"       |j	                  |         |rMt)        d |D              rt        |      t        |      }#}
t+        |
|#dz         }$t        |      D ]6  \  }}"|"j-                         D ]  \  }%}&	 |%j/                  d      }|&|$||f<     8 j                  dd      rt3        |$g|i }nt5        |$g|i }|r|gng }|r|rt        |d   j7                               }nwg }nsd}t        |      t        |      kD  r t               j8                  |D 'cg c]  }'|'j                   c}' }(t        t;        |(j=                  |                  }(t               })g }t?        |(t        |            D ]  }*	 tA        |g|* }+|+rtt        |+      D ]f  -d},-D ]  ?|)stC        ?fd|)D              sd}, |,r)|)j                  |*       |j	                  t        t        tE        |*-                         h  |)rt        |)      }nPtG        d      	 tA        |g| }|r6|}t        |      D -cg c]   }-t        t        tE        ||-                  " }}-|xs i g}|rd}d>fd	=t        |      >tM               }.t;        |=fd      D ]  }/g }0g }1d}2|D ]y  -t               })|/jO                  -      }3|r9-r7tQ        |.|.|3d      }4|4&|4r|0j	                  -       n|1j	                  -       Z =|3d      }5|5s-r|0j	                  -        |5D ]  }6	 tS        |3|6fi }7|7D ]  @|)rtC        @fd|)D              r-jU                         }8-jW                         D ]  \  }9}:|:jO                  |6@      |8|9<    @|8|6<   t        |8jW                               };|0D ]8  }t        |      t        |;      k  st        |jW                               |;z
  r8  |0j	                  |8        d}2|)jY                  |6        |2rntG        d|3z         |0}|1D ]  }4|4|v s|j[                  |4         |sdg fS t]        |      }<j                  d|<      r$|D ]  --D ]  }9t_        -|9         -|9<     dd<   |r%|D --cg c]  -tC        -fd|D              s- }}-|r'|s%|D --cg c]  -tC        -fd| D              s- }}-|D -cg c]  }-|-s|-	 }}-||fS c c}w c c}	}w # t0        $ r |& |$||#f<   Y w xY wc c}'w # tF        $ r Y w xY wc c}-w # tF        $ r9 |jI                  |D  cg c]  } | jK                          nc c} w c}        g }Y w xY w# tF        $ r Y ]w xY wc c}-w c c}-w c c}-w )zreturn ``(linear, solution)`` where ``linear`` is True
    if the system was linear, else False; ``solution``
    is a list of dictionaries giving solutions for the symbols
    F_splitTNrm   c                    |    S r   r   )r  sym_indicess    r   r   z_solve_system.<locals>.<lambda>  s    +a. r   r   c              3  J   K   | ]  }|j                         D ]  }|   y wr   )rt   )r   sditems      r   r   z _solve_system.<locals>.<genexpr>  s/      !," "
!, "& !, !,s   !#manualr   r   	extensionc              3  4   K   | ]  }|j                     y wr   )	is_linear)r   r   s     r   r   z _solve_system.<locals>.<genexpr>3  s     *qq{{*   r   c              3  :   K   | ]  }|j                   v   y wr   r   )r   ssr1s     r   r   z _solve_system.<locals>.<genexpr>[  s"      5</1 682??5J 5<r   zno valid subset foundc                T      j                   z   fd}|rt        |      S )Nc                ,   j                  |       }|0t        j                  t        j                  t        j                  f}nB|j                         j                  }|j                         t        |z        t        |      f}|t        |       fz   S r   )as_polyr   r  LCr   r  r   r!   )symep
complexity
coeff_symsr   r   s       r   r!  z,_solve_system.<locals>._ok_syms.<locals>.key  sq    YYs^:"#**ajj!**!EJ!#!5!5J"$))+s:?/CS_!UJ!%5c%:$<<<r   r   )r   sorted)r   rA  r!  r   legals   `  @r   _ok_symsz_solve_system.<locals>._ok_syms}  s,    %'B= BC(Ir   c                &    t         |             S r   r  )r   r  s    r   r   z_solve_system.<locals>.<lambda>  s    C,< r   r   )rA  c              3  :   K   | ]  }|j                   v   y wr   r   )r   r  r   s     r   r   z _solve_system.<locals>.<genexpr>  s     (PBs/?/?)?(Pr   zcould not solve %sr=   c              3  8   K   | ]  }t        |fi   y wr   rn  r  s     r   r   z _solve_system.<locals>.<genexpr>  s     =1x1..=ro  c              3  <   K   | ]  }t        |fi d u   yw)FNrn  )r   r   r   r  s     r   r   z _solve_system.<locals>.<genexpr>  s"     GAx1..%7Gs   )F)0r(  r   r   r)  rs   r]   r   r  r>  rq   rp   rk   r   ru   r   r   _invertr  r   r  r3  rP   termsindexro   minsolve_linear_systemsolve_linear_systemr   r&  r    r  rb   rg   r   r;  r-  r1  r   r   rn   r   r  copyrt   r   remover   r=   )Bexprsrw   r   Vsymssetr   exprsymsr   rz   r  r   e1e2Gsubexprssubsolsr\  subexprsubsyms_linearsubsolsolssoldictspolysr   failedr  solved_symsr  r  r   jri  r   r  r  matrixmonomcoeffr   freer  symsresr'  r  r  r   	newresultbad_resultshiteq2r|  ok_symsr{   r  rnewr~   r   isetdefault_simplifyr  r  r  r   r  sB     `                                          `               @@@@@r   r>  r>    s   
 byyy4 g,9>?AAq~~//??,5g,>?&!SsAv?u% 	'EArBQi 'B<(2,.HHb"X&'	'
 qD'*x=1GF# '%  +Ax{*G+ 0HI"'h"/"JE"J#/F!&$/$XFv&' D#W- -D !, !, , -- 4<E5DFFKFYYx'F		'400I%  1LG,-q#7#1<%aQC03q8Eq!MM!qyy'2T2LLMM!#& *E**u:s7|qA1a!e_F$U+ .4$(JJL .LE5.!KKN',q!t.. yyu-/JJEJ,VGgGG!'fXRF"&vay~~'7"8K"$K F7|c%j("su{{U$CQ^^$CDGD$5$5g$>?@#D#e*5 D/==%(X 
L',*+ !4B', 5<5:5< 2< 04!4 (,$)LL$6$*MM$tCaL7I2J$K
L$ "&u+K-.EFF%.u?w?F&-KNv;!Wa$tCQ,?'@"A!W!W ^tF	( GG&"<= ?	)BIKC ;)ggaj Q AsD9A} %,,Q/'..q1  #3T2!((+  !A!&sA77  $ 3 S(P%(P%P$ vvx$%GGI 5DAq&'ffQnDG5 #&Q"4::<0!* 3A"1vD	1#aggi.4:O !&	3 &,,T2%3& CIIaL9!: -.BS.HIIm;)p #$ )AF{a(){?	)B by F|yy-. 	&A &!~!&	& "j# ?===  ? ? V# IGGG  I I  %A1a%F%6>u @?H & .(-vq!t.& %D( /  "X* %MM">1199;">">?"$K%H / ! !j?I &s   ] ]]1]#%](6](](A ](.]= %]80]= 5_!_
_._6_] ] (	]54]58]= =^?^-
,^?>^?	_	_	c           	        t        | t              r1|rt        t        d|z              | j                  }| j
                  } d}| |z
  }|j                         \  }}|s t        j                  t        j                  fS |j                  }|s|}n|D 	cg c]  }	|	j                  r|	 }
}	|
r[t        |
      dk(  r|
d   }
t        |      dk(  rd|d|d   d}nd|dt        |      d}t        t        d	|
d
|d            |j                  |      }|j                  |      }|s t        j                  t        j                  fS t!        t              }|j#                  t$              D ],  }|j                  |z  }|D ]  }||   j'                  |        . d}t)        |t*              D ]R  t        |   t              r%|   D ci c]  }||j-                          c}|<   |j/                  |         }|j1                        j                  }sp|rt3        fd|D              s||k(  sd}t        j4                  u r nj                  vsdz  |j/                  d      z  |t7        ||      }t3        fd|D              rj#                  t8              D cg c]*  }|j:                  j<                  r||j-                         f, }}t?        j/                  |            fc S  |r t        j                  t        j                  fS |j                  r t        j                  t        j                  fS ||fS c c}	w c c}w c c}w )a  
    Return a tuple derived from ``f = lhs - rhs`` that is one of
    the following: ``(0, 1)``, ``(0, 0)``, ``(symbol, solution)``, ``(n, d)``.

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

    ``(0, 1)`` meaning that ``f`` is independent of the symbols in *symbols*
    that are not in *exclude*.

    ``(0, 0)`` meaning that there is no solution to the equation amongst the
    symbols given. If the first element of the tuple is not zero, then the
    function is guaranteed to be dependent on a symbol in *symbols*.

    ``(symbol, solution)`` where symbol appears linearly in the numerator of
    ``f``, is in *symbols* (if given), and is not in *exclude* (if given). No
    simplification is done to ``f`` other than a ``mul=True`` expansion, so the
    solution will correspond strictly to a unique solution.

    ``(n, d)`` where ``n`` and ``d`` are the numerator and denominator of ``f``
    when the numerator was not linear in any symbol of interest; ``n`` will
    never be a symbol unless a solution for that symbol was found (in which case
    the second element is the solution, not the denominator).

    Examples
    ========

    >>> from sympy import cancel, Pow

    ``f`` is independent of the symbols in *symbols* that are not in
    *exclude*:

    >>> from sympy import cos, sin, solve_linear
    >>> from sympy.abc import x, y, z
    >>> eq = y*cos(x)**2 + y*sin(x)**2 - y  # = y*(1 - 1) = 0
    >>> solve_linear(eq)
    (0, 1)
    >>> eq = cos(x)**2 + sin(x)**2  # = 1
    >>> solve_linear(eq)
    (0, 1)
    >>> solve_linear(x, exclude=[x])
    (0, 1)

    The variable ``x`` appears as a linear variable in each of the
    following:

    >>> solve_linear(x + y**2)
    (x, -y**2)
    >>> solve_linear(1/x - y**2)
    (x, y**(-2))

    When not linear in ``x`` or ``y`` then the numerator and denominator are
    returned:

    >>> solve_linear(x**2/y**2 - 3)
    (x**2 - 3*y**2, y**2)

    If the numerator of the expression is a symbol, then ``(0, 0)`` is
    returned if the solution for that symbol would have set any
    denominator to 0:

    >>> eq = 1/(1/x - 2)
    >>> eq.as_numer_denom()
    (x, 1 - 2*x)
    >>> solve_linear(eq)
    (0, 0)

    But automatic rewriting may cause a symbol in the denominator to
    appear in the numerator so a solution will be returned:

    >>> (1/x)**-1
    x
    >>> solve_linear((1/x)**-1)
    (x, 0)

    Use an unevaluated expression to avoid this:

    >>> solve_linear(Pow(1/x, -1, evaluate=False))
    (0, 0)

    If ``x`` is allowed to cancel in the following expression, then it
    appears to be linear in ``x``, but this sort of cancellation is not
    done by ``solve_linear`` so the solution will always satisfy the
    original expression without causing a division by zero error.

    >>> eq = x**2*(1/x - z**2/x)
    >>> solve_linear(cancel(eq))
    (x, 0)
    >>> solve_linear(eq)
    (x**2*(1 - z**2), x)

    A list of symbols for which a solution is desired may be given:

    >>> solve_linear(x + y + z, symbols=[y])
    (y, -x - z)

    A list of symbols to ignore may also be given:

    >>> solve_linear(x + y + z, exclude=[x])
    (y, -x - z)

    (A solution for ``y`` is obtained because it is the first variable
    from the canonically sorted list of symbols that had a linear
    solution.)

    z<
            If lhs is an Equality, rhs must be 0 but was %sNrm   r   zsolve(r  )z, *z8
                solve_linear only handles symbols, not z=. To isolate
                non-symbols use solve, e.g. >>> z# <<<.
                             Tr   c              3  @   K   | ]  }j                  |        y wr   )diff)r   r{   	dnewn_dxis     r   r   zsolve_linear.<locals>.<genexpr>  s     )J)..*;)J   Fr   c              3  B   K   | ]  }t        |id       d u   yw)Tr  Nrn  )r   divir  s     r   r   zsolve_linear.<locals>.<genexpr>  s+      *  $BR$?4G *   ) rq   r   ro   r[   r   r   r   r   r2  r   r   r,  r   rp   r  
differenceri   r   r   rs   r  r!   doitrn   r  r   r  r   r;   functionr   r   )r   r   rw   r   r   r   r   r   r	  r{   badegderivsdercsymrq  all_zeronewnrz   irepr  r  r  s                       @@@r   r  r    s4   V #rZ )?AD)E F G GggggD	sBDAqvvquu}>>D!5Qq553x1}!f7|q (*GAJ7 ! *,T'];Z %()- . / / ##G,  )Gvvquu}
 Fwwz" "') 	"A1IS!	""
 HW"23 "fRj$'5;BZ@c#sxxz/@F2JvvfRj!IIbM	 %%dc)JT)J&JdV]oHAEE!///	\499R#34<'G4D *$(* * 4688H3E 2aJJ00 M 2D 2 $BGGDM2Br6M1"2 vvquu}{{vvqvv~a4Kq 6< A"2s   M(MM"9/M'c           
        |j                  dd      }t        | g|i |}|r t        d |j                         D              r|S |rt        | g| }d }i } |||       |rt	        d |j                         D        d       }|j
                  }	t        t        t        t        |	                        t        |	      dk7  rt        j                  |<   nOt        |d	      d
   s7t        fd|j                         D              st        j                  |<   n|<    |||       |r|S t        |      }
t!        | g|ddi}t        |j                         D cg c]
  }|d
k7  s	| c}      }t#        |dz
  dd      D ]d  }t%        d|       d}t'        t#        |
      |      D ]3  }t)        |D cg c]  }| j+                  |      j,                   c}| j+                  d      j,                  gz         j,                  }t        |g|D cg c]  }||   	 c} }|s}t        d |j                         D              r|D cg c]  }||   t        j                  f }}|j/                         D ]  \  }}|j1                  |      ||<    |D ]A  }||vs|j3                  |      |v rt        j                  ||<   /t        j                  ||<   C |} n | |S |}g |S c c}w c c}w c c}w c c}w )a!  
    Find a particular solution to a linear system.

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

    In particular, try to find a solution with the minimal possible number
    of non-zero variables using a naive algorithm with exponential complexity.
    If ``quick=True``, a heuristic is used.

    r   Fc              3  &   K   | ]	  }|d k(    ywr   Nr   r   r   s     r   r   z)minsolve_linear_system.<locals>.<genexpr>  s     1Q!V1r  c                    g }|j                         D ]B  \  }}|j                  |       ||<   ||   j                  r*|j                  |       ||   | |<   D |D ]  }||=  y r   )rt   rn   r   rs   )
determinedr[  deleter~   r   s        r   r   z&minsolve_linear_system.<locals>.update  sq    F ( 01ffZ0{//MM!$$,QKJqM	0
   QK r   c              3      K   | ]  }|  y wr   r   )r   r~   s     r   r   z)minsolve_linear_system.<locals>.<genexpr>  s     +1Q+s   c                B    t        | j                        t        |       fS r   )r   r   r!   r  s    r   r   z(minsolve_linear_system.<locals>.<lambda>  s    3q~~#68H8K"L r   r   rm   )r   r   c              3  B   K   | ]  }|j                          y wr   )rn   )r   r   r   r  s     r   r   z)minsolve_linear_system.<locals>.<genexpr>  s     &J!qvva~&Jr  Tr   zminsolve: %sNc              3  &   K   | ]	  }|d k(    ywr+  r   r,  s     r   r   z)minsolve_linear_system.<locals>.<genexpr>  s      <Aa <r  )ru   r  r3  r  maxr   nextr   rp   r    r   r   r2  r  r   r   r  ranger\   rj   rO   colTrt   rn   r  )systemrw   r   r   s0r{   r   r.  r~   kfreeNbestsolr  n0r   thissolnonzerosrz   submr   rn   r  r   s               `         @r   r  r    s    IIgu%E	V	7g	7	7B1RYY[11	  11	  
z1+
+LNANNEXd75>234A5zQ !
1a%033&Jqxxz&J#J$%EEJqM$'JqM:q!   L(F'FFW^^-8a!89rAvq"% 	A>1%G(q15 A1vzz!}AVZZPR^EUEUDVVWYY'NH/Mq
/MNS < <<9ABAWQZ/BDB !	 ,1 vvd|!,& 0a<&}}S1X=)*#)*#0  G  G)	* - 9
 B/MBs   +
K;6K;"L  LL
c                   | j                   d   t        |      dz   k(  sJ t        | t        |dz         z        }t	        ||      \  }}t        ||d      }|)|j                         D ci c]  \  }}||k7  s|| }}}|S c c}}w )a9  
    Solve system of $N$ linear equations with $M$ variables, which means
    both under- and overdetermined systems are supported.

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

    The possible number of solutions is zero, one, or infinite. Respectively,
    this procedure will return None or a dictionary with solutions. In the
    case of underdetermined systems, all arbitrary parameters are skipped.
    This may cause a situation in which an empty dictionary is returned.
    In that case, all symbols can be assigned arbitrary values.

    Input to this function is a $N\times M + 1$ matrix, which means it has
    to be in augmented form. If you prefer to enter $N$ equations and $M$
    unknowns then use ``solve(Neqs, *Msymbols)`` instead. Note: a local
    copy of the matrix is made by this routine so the matrix that is
    passed will not be modified.

    The algorithm used here is fraction-free Gaussian elimination,
    which results, after elimination, in an upper-triangular matrix.
    Then solutions are found using back-substitution. This approach
    is more efficient and compact than the Gauss-Jordan method.

    Examples
    ========

    >>> from sympy import Matrix, solve_linear_system
    >>> from sympy.abc import x, y

    Solve the following system::

           x + 4 y ==  2
        -2 x +   y == 14

    >>> system = Matrix(( (1, 4, 2), (-2, 1, 14)))
    >>> solve_linear_system(system, x, y)
    {x: -6, y: 2}

    A degenerate system returns an empty dictionary:

    >>> system = Matrix(( (0,0,0), (0,0,0) ))
    >>> solve_linear_system(system, x, y)
    {}

    rm   )r   F)_raw)shaper   rp   rO   rU   rV   rt   )r9  rw   r   rv   ringr   r  r   s           r   r  r    s    ^ <<?c'lQ.... vw//
0C!#w/IC
T
.C
'*yy{A83cSjs3wAAJ Bs   -B;Bc                8   |rt        d |D              st        d      t        | t              r| j                  | j
                  z
  }n| }t        |      }t        |j                         d   d      }|j                  }|t        |      z  }|s|j                  dd      rg i fS g S |sN |j                  |ddi\  }}	|	j                  }
|
|j                  z  }|s|
t        |      z
  }|sEt               g}n9t        |      d	k(  rt        |d         r|d   }t!        |g|      \  }}}|d   }|}t         |j"                  | j%                               d	hz
  }t        |      t'        fd
|D              ryt!        |gt)        |            \  }}}|d   }t)        t+        ||d      j-                               }t/        ||fi |}|j                  dd      xs |j                  dd      }t1        |      t2        u s|st        |      d	k7  r|S |d   S )az	  
    Solve a system of equations in $k$ parameters that is formed by
    matching coefficients in variables ``coeffs`` that are on
    factors dependent on the remaining variables (or those given
    explicitly by ``syms``.

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

    The result of this function is a dictionary with symbolic values of those
    parameters with respect to coefficients in $q$ -- empty if there
    is no solution or coefficients do not appear in the equation -- else
    None (if the system was not recognized). If there is more than one
    solution, the solutions are passed as a list. The output can be modified using
    the same semantics as for `solve` since the flags that are passed are sent
    directly to `solve` so, for example the flag ``dict=True`` will always return a list
    of solutions as dictionaries.

    This function accepts both Equality and Expr class instances.
    The solving process is most efficient when symbols are specified
    in addition to parameters to be determined,  but an attempt to
    determine them (if absent) will be made. If an expected solution is not
    obtained (and symbols were not specified) try specifying them.

    Examples
    ========

    >>> from sympy import Eq, solve_undetermined_coeffs
    >>> from sympy.abc import a, b, c, h, p, k, x, y

    >>> solve_undetermined_coeffs(Eq(a*x + a + b, x/2), [a, b], x)
    {a: 1/2, b: -1/2}
    >>> solve_undetermined_coeffs(a - 2, [a])
    {a: 2}

    The equation can be nonlinear in the symbols:

    >>> X, Y, Z = y, x**y, y*x**y
    >>> eq = a*X + b*Y + c*Z - X - 2*Y - 3*Z
    >>> coeffs = a, b, c
    >>> syms = x, y
    >>> solve_undetermined_coeffs(eq, coeffs, syms)
    {a: 1, b: 2, c: 3}

    And the system can be nonlinear in coefficients, too, but if
    there is only a single solution, it will be returned as a
    dictionary:

    >>> eq = a*x**2 + b*x + c - ((x - h)**2 + 4*p*k)/4/p
    >>> solve_undetermined_coeffs(eq, (h, p, k), x)
    {h: -b/(2*a), k: (4*a*c - b**2)/(4*a), p: 1/(4*a)}

    Multiple solutions are always returned in a list:

    >>> solve_undetermined_coeffs(a**2*x + b - x, [a, b], x)
    [{a: -1, b: 0}, {a: 1, b: 0}]

    Using flag ``dict=True`` (in keeping with semantics in :func:`~.solve`)
    will force the result to always be a list with any solutions
    as elements in that list.

    >>> solve_undetermined_coeffs(a*x - 2*x, [a], dict=True)
    [{a: 2}]
    c              3  4   K   | ]  }|j                     y wr   )r,  r#  s     r   r   z,solve_undetermined_coeffs.<locals>.<genexpr>w	  s     711;;7r  zmust provide symbols for coeffsr   Tr   r   Nas_Addrm   c              3  @   K   | ]  }|j                          y wr   )	has_xfree)r   ri  csets     r   r   z,solve_undetermined_coeffs.<locals>.<genexpr>	  s     
+1;;t
+r  Fr   r   )r3  ro   rq   r   r   r   rR   r   r   r   r   ru   r   r   r   r`   r   as_coefficients_dictr   r   rp   r>   r  rf  r*  r   )equcoeffsr
  r   r   ceqxeqr	  rY  rZ  dfreer   r{   r   r  r9  r  settingsrK  s                     @r   rh  rh  6	  s   B s777:;;#rWWsww
*C
3%%'*d
;CDCKF 99UD1Bx9r9 &3%%v;d;S  s''' 3v;&DG9Dt9>htAw/7D#SE401ad 's''.3356!<Dv;D

+d
++ #C5$t*5JAtQ
A$C '#te4;;=>F )5)D yy&@%))E4*@HDzTXTa7Nr   c                6   | j                   | j                  dz
  k7  rt        d      | d| j                   d| j                   f   }| dd| j                  dz
  df   }|j                  |      }i }t	        |j                         D ]  }||df   |||   <    |S )a  
    Solves the augmented matrix system using ``LUsolve`` and returns a
    dictionary in which solutions are keyed to the symbols of *syms* as ordered.

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

    The matrix must be invertible.

    Examples
    ========

    >>> from sympy import Matrix, solve_linear_system_LU
    >>> from sympy.abc import x, y, z

    >>> solve_linear_system_LU(Matrix([
    ... [1, 2, 0, 1],
    ... [3, 2, 2, 1],
    ... [2, 0, 0, 1]]), [x, y, z])
    {x: 1/2, y: 1/4, z: -1/2}

    See Also
    ========

    LUsolve

    rm   z#Rows should be equal to columns - 1Nr   )rowscolsro   LUsolver6  )r  r
  Ar|  r  r  rz   s          r   solve_linear_system_LUrX  	  s    8 {{fkkAo%>??||\fkk\)*Aq&++/""#A99Q<DI499 (!!Q$Z	$q'(r   c                   g }d}| j                   }| j                         }t        |      D ]J  }g }d}|D ]  }|j                  |||z             ||z  }  t	        | }	|j                  |r|	n|	        | }L t        | S )a|  
    Return the determinant of *M* by using permutations to select factors.

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

    For sizes larger than 8 the number of permutations becomes prohibitively
    large, or if there are no symbols in the matrix, it is better to use the
    standard determinant routines (e.g., ``M.det()``.)

    See Also
    ========

    det_minor
    det_quick

    Tr   )rT  flatr^   rs   r	   r   )
Mr   r{   r   list_permfacidxr  terms
             r   det_permra  	  s    $ DA	AFFHEa   	AJJuS1W~&1HC	 CyADD5)E :r   c                      j                   }|dk(  r d    d   z   d    d   z  z
  S t         fdt        |      D              S )z
    Return the ``det(M)`` computed from minors without
    introducing new nesting in products.

    See Also
    ========

    det_perm
    det_quick

    r   )r   r   )rm   rm   )rm   r   )r   rm   c              3     K   | ]n  }d |f   rSd|dz     t        t        j                  t        j                  d |                  D cg c]  }d |f   |z   c} z  nt        j
                   p yc c}w w)r   )rm   r   r   N)r   r   	det_minorminor_submatrixr   r2  )r   rz   r   r[  s      r   r   zdet_minor.<locals>.<genexpr>
  s      6'(qAw 1q5>#MM)A$5$5a$;<=(?a!Q$	 (? #@ @FF# 6 (?s   AA<A7 A<)rT  r  r6  )r[  r   s   ` r   rd  rd  
  sV     	
AAvwqw4400 6,1!H6 6 	6r   c                    t        d | D              r7| j                  dk  rt        d | D              rt        |       S t	        |       S |r| j                  |      S | j                         S )a  
    Return ``det(M)`` assuming that either
    there are lots of zeros or the size of the matrix
    is small. If this assumption is not met, then the normal
    Matrix.det function will be used with method = ``method``.

    See Also
    ========

    det_minor
    det_perm

    c              3  F   K   | ]  }|j                  t                y wr   r   r   r#  s     r   r   zdet_quick.<locals>.<genexpr>&
  s     
$Q155=
$   !   c              3  F   K   | ]  }|j                  t                y wr   rh  r#  s     r   r   zdet_quick.<locals>.<genexpr>'
  s     7aeeFm7ri  )method)r   rT  r3  ra  rd  det)r[  rl  s     r   	det_quickrn  
  s\     
$!
$$66A:#7Q77A;|'-quuFu#:1557:r   c                   t        d | D              st        d | D              sd }nd }n| j                         S | j                  } ||       }|t        j
                  k(  rt        d      t        |      }d}t        |      D ]?  }| x}}t        |      D ]*  } || j                  ||            }	||	z  |z  |||f<   | }, A |S )zzReturn the inverse of ``M``, assuming that either
    there are lots of zeros or the size of the matrix
    is small.
    c              3  4   K   | ]  }|j                     y wr   r   r#  s     r   r   zinv_quick.<locals>.<genexpr>3
  s     &qq{{&r  c              3  4   K   | ]  }|j                     y wr   rq  r#  s     r   r   zinv_quick.<locals>.<genexpr>4
  s     *11;;*r  c                    t        |       S r   )ra  r   s    r   r   zinv_quick.<locals>.<lambda>5
  s
    HQK r   c                    t        |       S r   )rd  rt  s    r   r   zinv_quick.<locals>.<lambda>7
  s
    IaL r   zMatrix det == 0; not invertibler   )
r3  r   r  rT  r   r2  rN   rP   r6  re  )
r[  rm  r   r   rets1rz   r{   r  r  s
             r   	inv_quickrx  .
  s    
 &A&&***'C(Cuuw	AAAAFF{&'HII
(C	B1X Bq 	AQ&&q!,-B"QC1IA	 Jr   c                P    t        |       t        j                  t        |       z
  fS r   )r.   r   Pir  s    r   r   r   K
  s    DGQTTDG^, r   c                V    t        |       dt        j                  z  t        |       z
  fS Nr   )r-   r   rz  r  s    r   r   r   L
  s     DGQqttVd1g-. r   c                H    t        | |fi |D cg c]  }||   	 c}S c c}w )z?return list of scalar values for the solution of e for symbol s)r=  )r   r{   r   rz   s       r   r  r  P
  s&     A//0QAaD000s   c                |   d|vrg |d<   | |d   v ry|d   j                  |        t        | |      \  }}||k(  r|gS 	 |j                  rt        t	        ||z
              }|j
                  rt        ||fi |S |rjt        ||j                  dd            }|j                  t              |j                  t              k7  r!t        |t              r&t        |j                  d   t        |      z
  |fi |S t        ||z
  |fi |S |j                  r|j                  j                   r||z
  | k7  rt        ||z
  |fi |S ||j                  j"                  vr*t        |j$                  |d|j                  z  z  z
  |fi |S t'        d |j"                  D              rt(        |sGt        |j$                  |fi |}|D cg c]$  }|j                  j+                  ||      dk7  s#|& c}S |j$                  j-                  |      s|j$                  dk(  r|dk7  rt        |j                  |fi |S g S |j$                  |j/                         d   k(  r,t        |j                  |j/                         d   z
  |fi |}nMt        t        |j$                        |j                  z        t        t        |            z
  }t        ||fi |}d	 }	t1        t        t3        |            t        t3        |j$                              z        }
t5        t7        |
      d         }
t9        |
      \  }}t;        |j$                  |z  ||z  z
        dk(  r\|D cg c](  } |	|j                  j+                  ||      |
      r'|* }}|j=                  t        |j                  |
z
  |fi |       t?        tA        |            S g }|j                  t        |j$                        z  t        |      z
  }|||z
  k7  r	 |j=                  t        ||fi |       g }|dk(  rz|j=                  t        |j                  |fi |       |j=                  t        |j$                  dz
  |fi |       |j=                  t        |j$                  dz   |fi |       n|jB                  rd
 tE        t3        |jF                              D        D ]  }tI        |jF                  |      \  }}|stE        t3        |jJ                              D ]  }||z  |jJ                  k(  stM        ||      }|j=                  t        |j$                  |z
  |fi |       |j=                  t        |j$                  |z   |fi |       |j=                  t        |j                  |z
  |fi |         n|jN                  r|j$                  j/                         \  }}||j                  z  jQ                         \  }}tS        |      j/                         \  }}t        |j$                  |z
  |fi |D cg c]  }tS        |       }}|j=                  t        |j                  |z
  |fi |D cg c]  }tS        |       c}       ||z  dk7  r(|j=                  t        ||z  |||z  z  z
  |fi |       |D ]F  }tU        | ||      }|!| j+                  ||      jW                  d      }|s6|j                  |       H t?        tA        |            S |jX                  rt[        |j                        dk(  r|j\                  t^        v r^g }t_        ta        |         |      D ].  }|j=                  t        |j                  d   |z
  |fi |       0 t?        tA        |            S |j\                  tb        k(  r)t        |j                  d   |t        |      z  z
  |fi |S |je                  t              }tg        |      }||k7  rt        ||z
  |fi |S 	 |ji                  dd      r| jk                  t              }tm        |D ch c]7  }|jk                  tn              D ]  }|j$                  |k(  r|j                   9 c}}xs dh      }|dk7  rx||z  }tq        d      }| j+                  ||      }|js                  |      sEt        ||fi |}t        ||z
  |      }|D cg c]  }|D ]  }|j+                  ||        c}}S tu        | jw                         |      } tA               }!| D ]  }"t        |"t        t        f      s)|"j                  r/|"j$                  tx        jz                  k(  r|!j}                  |"       T|"j                  sat	        t        |"            }#|#j                  s||#j                  j"                  v s|!j}                  |"        t        t        |             j+                  t        t?        t        |!dgt[        |!      z                          }$t        t        |$d      | |$z
  z         } t        | |      \  }}|j-                  |      r	 |jw                         }%tu        |%|      } ||z
  }&t        |&||       }'t        |'      D ]p  \  }}t1        |      }(|(|k7  s|(j                         |j                         k  s9tU        |&||(      }|!|&j+                  ||(      jW                  d      }|sl|(|'|<   r |'S |ji                  dd      rd|d<   t7        ||z
        \  }+},|tx        j                  k(  rg S |,j                         D ]  \  }}||k(  s n |}|+j-                  |      r-	 t        |+|fi |}|D cg c]  }|j+                  |,       c}S 	 yc c}w c c}w # t(        $ r Y w xY wc c}w c c}w # t(        $ r Y w xY wc c}}w c c}}w # t(        $ r t[        |       dk(  r	 t        ||z
  g|  })|)t(        |)\  } }}d|d<   t        | |z
  |fi |}*|*rHt        ||fi |}t?        |*D ch c]  }|D ]  }|j+                  ||        nc c}}w c}}      cY S n# t(        $ r Y nw xY w	 Y w xY wc c}w # t(        $ r Y yw xY w)a  
    Helper for ``_solve`` that solves a transcendental equation with respect
    to the given symbol. Various equations containing powers and logarithms,
    can be solved.

    There is currently no guarantee that all solutions will be returned or
    that a real solution will be favored over a complex one.

    Either a list of potential solutions will be returned or None will be
    returned (in the case that no method was known to get a solution
    for the equation). All other errors (like the inability to cast an
    expression as a Poly) are unhandled.

    Examples
    ========

    >>> from sympy import log, ordered
    >>> from sympy.solvers.solvers import _tsolve as tsolve
    >>> from sympy.abc import x

    >>> list(ordered(tsolve(3**(2*x + 5) - 4, x)))
    [-5/2 + log(2)/log(3), (-5*log(3)/2 + log(2) + I*pi)/log(3)]

    >>> tsolve(log(x) + 2*x, x)
    [LambertW(2)/2]

    
tsolve_sawNr   T)r   r   rm   c              3  4   K   | ]  }|j                     y wr   )is_Dummy)r   rT  s     r   r   z_tsolve.<locals>.<genexpr>
  s     8!1::8r  c                    t               }t        | |z
  ||      }|)t        |       t        |      k7  ry| j                  |      }|S )NF)r   r   rB   equals)expr1expr2r   r   s       r   equalz_tsolve.<locals>.equal
  sJ    A!%!)Q6Bz$U+y/??#( #\\%0Ir   c              3  ,   K   | ]  }|d k7  s	|  ywr  r   r#  s     r   r   z_tsolve.<locals>.<genexpr>
  s     HAaaHs   
	bivariatezbivariate-cov)deepr   F)Irs   r  r  rS   rA   r9  r  rD   ru   countr'   rq   r   r(   r  r   r   r   r   r   r-  rn   r   ry  rB   r   r=   r@   rF   r   r1  rp   r   r   r<   r   r   rz  r   is_irrationalr   rE   r   r  r   r   r:  multi_inversesr*  r)   r   rM   r(  r   minr   r   r   _filtered_gensr  r   Exp1r   r   r   r   r;  _solve_lambertr)  	count_opsbivariate_typer  rt   )-r   r  r   r   r   r   sol_baser{   r   r  e_ratr   r   logformr   r   rT  r  b_le_lr|  rz   rQ  r  r   logsr  spowr   r  ueqr  ri  	up_or_loggigisimpeq_downr  _eqr   nsgpu	inversionposr   s-                                                r   r  r  U
  s   8 5  l	U<  l""2&r3HC
czuA:: ys+,Axxq#///s%))GT*BC773<399S>1!!S)&qvvay3s8';SJEJJ"1s7C9599ZZww!!9?"39c;U;;#''...sxx##''	*::CI5II 8s'7'788))  #388S:E:#+Iasww||C/Cq/HII 88<<$88q==@AX7377C959M2M 88s033!#''COO,=a,@"@#OOC CM#''12SS]BA!!S2E2C	 "#c#h-CM0B"BC q!121#((A+Q./14&)Tsww||C7KU1S1TCTJJwswwEuEFCH~% ''#chh-/#c(:cCi'

77C#A5#AB
 !8LL#!?!?@LLAs!De!DELLAs!De!DE__H#cee*)=H 	Q*355!41 $!)#cee*!5 QA !tcee|$,QN %WSXX\3-P%-P Q %WSXX\3-P%-P Q %WSWWq[#-O-O PQ		Q &&"xx335HCK779DAq%c?668DAq5<SXX\35XRW5XZqZ]ZEZLL'#''A+s:\V[:\!^A*Q-!^_1uzWS!VcCEl-BC%Q5%QR &A!"c1-BzWWS!_33A6

1& CH~%__SXX!!3xx>)'S	237 HAKKa F FGHCI&X%sxx{SS\93H%HH++c"'"c>7S=#777  yyd# xx}  q!''#, Q# UU U  &#$#' 19T	Ao&A''!Q-C<<$c1..a!eS).1?3?aq!???2::<-E	 	&B"sCj)biiBGGqvv<Mb!"#3B#78==SFJJ,C,C%CMM"%	& -b1277c)aSY%789:<fW48BLIJ2s#S773<!{{}"4-Ci%c32 &dO )DAq"1BQw2<<>Q[[]#B%c33:!$#r!2!9!9!!<B&(DG) ( yy$g39%	T!###IJJL 	DAqCx	 A771:sA//.23t33 
y JD U / 2 [!^2   @@ ' q6Q;,S3Y;;;"55"%1a-2k*$+AE3$@%$@	$")!Q"8%"8C#')2)A$%C)A78 *+1 )A )A )A $B B % /  #@ 4&  s_  ;o" :Bo" <o" :o" Ao" 	?o" $n>-n>1o" 4Ao" ;o" =D"o" (oo>o" 8o" o  Do" &Do" 4o)o" /oA.o" 0&o" Bo" +;o" '4o" <o2
1 o82Ao> !o> $2o> o> r/ !r*9r/ >
o" 	oo" oo" "	o/.o/>r'Ar! rrr'r'	rr'rr'&r'*r/ /	r;:r;Fr   c                   d|v rt        t        d            d|v r$ddl}|j                  d      |j                  _        | }ddlm}  t        |      dk(  r^|d   }|d   }|d	   }t        |      rt        |      rt        |      t        |      k7  rt        d
t        |      t        |      fz        t        |      d	k(  r"|d   }d}|d   }t        |      rGt        d      t        |      d	k  rt        dt        |      z        t        dt        |      z        |j                  ddg      }t        |      r`t        |      }t        |      D ]2  \  }	}
t        |
t              s|
j                   |
j"                  z
  ||	<   4 t%        |      j&                  }t        |      rt        |      }t        |t$              st        |t              r|j                   |j"                  z
  }n|j(                  rt        d      |j*                  }||j-                         j                         }t        |      dk(  r||v s|d   |v st        t        d            t/        |||      }t1        t3        ||fi |      }|r||igS |S t        |      |j4                  kD  rt7        t        d            |j                  dd      }|rt9        d       t9        |       |j;                  |      }|rt9        d       t9        |       t/        ||j&                  |      }t/        |||      }t3        ||fd|i|}|r* | t=        ||D cg c]  }t1        |       c}            gS t%        |      S c c}w )a  
    Solve a nonlinear equation system numerically: ``nsolve(f, [args,] x0,
    modules=['mpmath'], **kwargs)``.

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

    ``f`` is a vector function of symbolic expressions representing the system.
    *args* are the variables. If there is only one variable, this argument can
    be omitted. ``x0`` is a starting vector close to a solution.

    Use the modules keyword to specify which modules should be used to
    evaluate the function and the Jacobian matrix. Make sure to use a module
    that supports matrices. For more information on the syntax, please see the
    docstring of ``lambdify``.

    If the keyword arguments contain ``dict=True`` (default is False) ``nsolve``
    will return a list (perhaps empty) of solution mappings. This might be
    especially useful if you want to use ``nsolve`` as a fallback to solve since
    using the dict argument for both methods produces return values of
    consistent type structure. Please note: to keep this consistent with
    ``solve``, the solution will be returned in a list even though ``nsolve``
    (currently at least) only finds one solution at a time.

    Overdetermined systems are supported.

    Examples
    ========

    >>> from sympy import Symbol, nsolve
    >>> import mpmath
    >>> mpmath.mp.dps = 15
    >>> x1 = Symbol('x1')
    >>> x2 = Symbol('x2')
    >>> f1 = 3 * x1**2 - 2 * x2**2 - 1
    >>> f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8
    >>> print(nsolve((f1, f2), (x1, x2), (-1, 1)))
    Matrix([[-1.19287309935246], [1.27844411169911]])

    For one-dimensional functions the syntax is simplified:

    >>> from sympy import sin, nsolve
    >>> from sympy.abc import x
    >>> nsolve(sin(x), x, 2)
    3.14159265358979
    >>> nsolve(sin(x), 2)
    3.14159265358979

    To solve with higher precision than the default, use the prec argument:

    >>> from sympy import cos
    >>> nsolve(cos(x) - x, 1)
    0.739085133215161
    >>> nsolve(cos(x) - x, 1, prec=50)
    0.73908513321516064165531208767387340401341175890076
    >>> cos(_)
    0.73908513321516064165531208767387340401341175890076

    To solve for complex roots of real functions, a nonreal initial point
    must be specified:

    >>> from sympy import I
    >>> nsolve(x**2 + 2, I)
    1.4142135623731*I

    ``mpmath.findroot`` is used and you can find their more extensive
    documentation, especially concerning keyword parameters and
    available solvers. Note, however, that functions which are very
    steep near the root, the verification of the solution may fail. In
    this case you should use the flag ``verify=False`` and
    independently verify the solution.

    >>> from sympy import cos, cosh
    >>> f = cos(x)*cosh(x) - 1
    >>> nsolve(f, 3.14*100)
    Traceback (most recent call last):
    ...
    ValueError: Could not find root within given tolerance. (1.39267e+230 > 2.1684e-19)
    >>> ans = nsolve(f, 3.14*100, verify=False); ans
    312.588469032184
    >>> f.subs(x, ans).n(2)
    2.1e+121
    >>> (f/f.diff(x)).subs(x, ans).n(2)
    7.4e-15

    One might safely skip the verification if bounds of the root are known
    and a bisection method is used:

    >>> bounds = lambda i: (3.14*i, 3.14*(i + 1))
    >>> nsolve(f, bounds(100), solver='bisect', verify=False)
    315.730061685774

    Alternatively, a function may be better behaved when the
    denominator is ignored. Since this is not always the case, however,
    the decision of what function to use is left to the discretion of
    the user.

    >>> eq = x**2/(1 - x)/(1 - 2*x)**2 - 100
    >>> nsolve(eq, 0.46)
    Traceback (most recent call last):
    ...
    ValueError: Could not find root within given tolerance. (10000 > 2.1684e-19)
    Try another starting point or tweak arguments.
    >>> nsolve(eq.as_numer_denom()[0], 0.46)
    0.46792545969349058

    rl  z
            Keyword "method" should not be used in this context.  When using
            some mpmath solvers directly, the keyword "method" is
            used, but when using nsolve (and findroot) the keyword to use is
            "solver".precr   Nr     rm   r   z0nsolve expected exactly %i guess vectors, got %iz"nsolve expected 3 arguments, got 2z,nsolve expected at least 2 arguments, got %iz+nsolve expected at most 3 arguments, got %imodulesmpmathz!nsolve cannot accept inequalitieszB
                expected a one-dimensional and numerical functionz9
            need at least as many equations as variablesverboseFzf(x):zJ(x):J)ro   r[   r  r(  mpdpsbuiltinsr   r   r`   r  ru   rp   r)  rq   r   r   r   rO   r8  r   r   r  rZ   r"   rf   rU  r-  printjacobianr;  )r   r   kwargsr  r]  r   fargsx0r  rz   r   r
  r  r  r  r  s                   r   nsolver  \  sL   ^ 6 %   	 

6*		 G 4yA~GQ!WE?x|2w#e*$ R#&u:s2w"7!8 9 9	TaG!WA;@AA	TQFd)$ % 	% Ed)$ % 	%jjXJ/G{Gq\ 	'EAr"b!vv!	' 1IKK|"Xa aA__?@@~~=IIKOO%ED	QETMU1X5EZ )E F G G UAw'HQ-f-.AJ<
5zAFF!* .< #= > 	>jjE*Gga	

5AgaW%A7#AB&!&v&ASq 9 9:;<<!9 !:s   M4c           	        t        |       } | j                  r | j                  | j                   } | j                  }|s|}|t	        |      z  s| t
        j                  fS t        |j                  dd            }| }t
        j                  }	 |}	  |j                  | \  }}	|j                  r|j                  rn#|	}||z  }n|t
        j                  u rn|	}||z  }N|j                  ri }
|j                  D ]5  } |j                  | \  }}|
j                  |g       j                  |       7 t        d |
j!                         D              rbg }|
j#                         D ]E  \  }}t%        |      dkD  r|j                  t'        | |z         /|j                  |d   |z         G t'        | }|j                  r|st%        |j                        dk(  r |j(                  | st+        |j                        \  }} |j                  | \  }} |j                  | \  }}t        d ||fD              r|j-                         \  }}|j-                         \  }}||k(  r2|j/                  |      !t1        t3        ||z              }|}| |z  }n#||z  }t1        ||z        }||k7  r
|}| |z  }n || k(  rt5        |t6              r|j                  |j                  k(  rt%        |j                        t%        |j                        cxk(  rdk(  r#n n |j                  d   |j                  d   z
  }n}t%        |j                        t%        |j                        k(  rt9        d      t;        d	      |j<                  r0t        d
 |j                  D              rt1        t3        |            }|j>                  rMtA        |d      rN|jC                         >t%        |j                        dk(  r& |jC                         |      }|j                  d   }nt5        |tD              r6|j                  \  }}dtG        |tI        |dz  |dz  z         |z   z        z  }n|j                  |j                  k(  rt%        |j                        t%        |j                        cxk(  rdk(  r"n n|j                  d   }|j                  d   }nAt%        |j                        t%        |j                        k(  rt9        d      t;        d	      |r;|jJ                  r/|jL                  jN                  r|jL                  dk  r
d|z  }d|z  }|jJ                  r|jL                  jN                  r|sR|jL                  jN                  sZt%        |      dkD  rLt%        |jP                  j                  t	        |      z        dkD  r|d|jL                  z  z  }|jP                  }||k(  r	 ||fS e)aY  
    Return tuple (i, d) where ``i`` is independent of *symbols* and ``d``
    contains symbols.

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

    ``i`` and ``d`` are obtained after recursively using algebraic inversion
    until an uninvertible ``d`` remains. If there are no free symbols then
    ``d`` will be zero. Some (but not necessarily all) solutions to the
    expression ``i - d`` will be related to the solutions of the original
    expression.

    Examples
    ========

    >>> from sympy.solvers.solvers import _invert as invert
    >>> from sympy import sqrt, cos
    >>> from sympy.abc import x, y
    >>> invert(x - 3)
    (3, x)
    >>> invert(3)
    (3, 0)
    >>> invert(2*cos(x) - 1)
    (1/2, cos(x))
    >>> invert(sqrt(x) - 3)
    (3, sqrt(x))
    >>> invert(sqrt(x) + y, x)
    (-y, sqrt(x))
    >>> invert(sqrt(x) + y, y)
    (-sqrt(x), y)
    >>> invert(sqrt(x) + y, x, y)
    (0, sqrt(x) + y)

    If there is more than one symbol in a power's base and the exponent
    is not an Integer, then the principal root will be used for the
    inversion:

    >>> invert(sqrt(x + y) - 2)
    (4, x + y)
    >>> invert(sqrt(x + y) + 2)  # note +2 instead of -2
    (4, x + y)

    If the exponent is an Integer, setting ``integer_power`` to True
    will force the principal root to be selected:

    >>> invert(x**2 - 4, integer_power=True)
    (2, x)

    integer_powerFc              3  8   K   | ]  }t        |      d kD    ywr  r  r,  s     r   r   z_invert.<locals>.<genexpr>  s     6!3q6A:6r  rm   r   r   c              3  2   K   | ]  }t        |        y wr   r   r#  s     r   r   z_invert.<locals>.<genexpr>  s     /6!9/r  z(equal function with more than 1 argumentz'function with different numbers of argsc              3  2   K   | ]  }t        |        y wr   r  )r   r  s     r   r   z_invert.<locals>.<genexpr>  s     <aq	<r  inverse))r"   r   r:  r   r   r   r2  r   ru   r   r  r   r   r%  rs   r   r  rt   r   r   is_polynomialr    ry  extract_additivelyr?   rA   rq   r   r-  ro   r9  r   hasattrr  r5   r/   r4   r   r(   r   r   )r   rw   r  r	  dointpowr   r   rL  indeprZ  r  r  rz   r   r   r|  aiadbibda_basea_expb_baseb_exprat_lhsyr  s                               r   r  r  &  s1   f 
B	wwRWWbgg??D#g,166zFJJ67H
C
&&C
+++W5JE3 zz==u
 AEE>u' , ::EXX 2'q''11  B'..q12 6u||~66!KKM ,DAq1vzCGAI.AaDF+	,
 4j ::cc#((mq&8%C%%w/388$DAq%Q%%w/FB%Q%%w/FB/r2h// " 0 " 0V#(@(@(G(O ")BrE"23CC#b&CR%C"2b5>Ds{"!c"fsb(+2770B277|s277|8q8 ggaj2771:5RWWRWW5 2FH H )EG G ZZC<388<<)C.)C??sI&3;;=+DSXXZ[I[ $ckkmC(hhqkC'xx1QQTAqD[ 1A 5677SXX%sxx=CM6Q6((1+C((1+C]c#((m3 .BD D %AC C 3::#''"4"41C%CC%C ::GG83773E3EGq S)>)>W)M%NQR%R#''	"C((C#:8O{ r   c                   FGHIJKL dddLILfd}d }d HHfd}|j                  d|      }t        | t              r| j                  | j                  z
  } nt        | t
              syt        g dd	d
j                               D cg c]  \  }}|j                  ||       c}}\  I}}	t        t        | dd            } | j                         d	   } t        | d      } | j                  ryt              xs | j                  | j                         }
|
j                   D cg c]  } ||      s| }}|sy | j                  | }
|
yt#        fd|D              ryHfd} ||
      \  }}}t%        dd      }t               }|D ]   }|j'                  |j                  z         " |k7  r|t)        t+        |t-        t/        |                        }dg iKt1        j2                  |
j5                               }|D ]  } ||      rXt        |j                         j                         j7                  |      }t9        t        |D cg c]  }||   	 c}            }nd}Kj                  |g       j;                  |        t1        Kj=                  d       }Kj?                         D cg c]  }t1        K|     c}KtA        tC        tA        tE        K                        Kd}tG        |       }t/        K      dk(  r'Kd	   jH                  r|dkD  sKd	   |z  | |z  z
  } d}nt/        K      dk(  r'Kd	   jH                  rtA        Kd	   jJ                        Kt/        |      dk(  r|j=                         }t/              dkD  r|j                  }n}tA        tE        |            d	   }	 tM        ||z  |z
  |fi L}|stN        |
j5                         jQ                  |||z        jQ                  ||d	         }  ||||z  |z
          || I      S t/        K      dk(  rV|sKd	   |z  Kd    |z  z
  } d}n-tS        |d      jT                  sK\  } }!|jW                  dd      r| |!} }! || j                               x}"\  }#}$}% ||!j                               x}&\  }'}(})t-        d      D ]  }*|*r|&|"}&}"|!| }!} |&\  }'}+})tY        |' }'|'|)z  },||)z  |,z
  }-D ]  }	 tM        |-|fi L}.|.stN        | jQ                  ||.d	         ||!z  |'z  z   |z   }/t[        |/|      }0|0r>|0\  } }1|1r-|1\  }2}3 ||2|-jQ                  |tM        |3|fi Ld	                n |||-       n|/}  |||-       d} n |s n nt/        K      dk(  rKD cg c]  } ||j                                c}Jd	GdFd}4Jd	   |4   dk7  rAJj;                  Jj=                  d	             Kj;                  Kj=                  d	             nKJd   |4   dk7  r@Jj;                  Jj=                  d             Kj;                  Kj=                  d             Jd	   |4   Jd   |4   cxk(  rdk(  rn nJd   F   Jd   F   k7  r"Jd   Jd	   cJd	<   Jd<   Kd   Kd	   cKd	<   Kd<   Jd   F   Jd   F   k(  rKd	   dz  Kd   Kd   z   |z   dz  z   } d}nJd   |4   dk  rdD cg c]  }t%        |       c}\  }5}}-}6}7}8|-|6z
  |7dz  |5dz  z  d|7dz  z  |8z  |5dz  z  |dz  z  z   d|7dz  z  |5dz  z  |-dz  z  z
  d|7dz  z  |5dz  z  |-dz  z  |6z  z   d|7dz  z  |5dz  z  |-z  |6dz  z  z
  d|7dz  z  |5dz  z  |6dz  z  z   d|7z  |8dz  z  |5dz  z  |dz  z  z   d|7z  |8z  |5dz  z  |dz  z  |-dz  z  z   d|7z  |8z  |5dz  z  |dz  z  |-dz  z  |6z  z
  d|7z  |8z  |5dz  z  |dz  z  |-z  |6dz  z  z   d|7z  |8z  |5dz  z  |dz  z  |6dz  z  z
  d|7z  |5dz  z  |-dz  z  z   d|7z  |5dz  z  |-dz  z  |6z  z
  d|7z  |5dz  z  |-d z  z  |6dz  z  z   d!|7z  |5dz  z  |-dz  z  |6dz  z  z
  d|7z  |5dz  z  |-dz  z  |6d z  z  z   d|7z  |5dz  z  |-z  |6dz  z  z
  d|7z  |5dz  z  |6dz  z  z   |8dz  |dz  z  z   d|8dz  z  |dz  z  |-dz  z  z
  d|8dz  z  |dz  z  |-dz  z  |6z  z   d|8dz  z  |dz  z  |-z  |6dz  z  z
  d|8dz  z  |dz  z  |6dz  z  z   d|8z  |dz  z  |-dz  z  z   d|8z  |dz  z  |-dz  z  |6z  z
  d|8z  |dz  z  |-d z  z  |6dz  z  z   d!|8z  |dz  z  |-dz  z  |6dz  z  z
  d|8z  |dz  z  |-dz  z  |6d z  z  z   d|8z  |dz  z  |-z  |6dz  z  z
  d|8z  |dz  z  |6dz  z  z   |-dz  z
  d|-d"z  z  |6z  z   d#|-d$z  z  |6dz  z  z
  d%|-dz  z  |6dz  z  z   d&|-dz  z  |6d z  z  z
  d&|-d z  z  |6dz  z  z   d%|-dz  z  |6dz  z  z
  d#|-dz  z  |6d$z  z  z   d|-z  |6d"z  z  z
  |6dz  z   z  }9FGJKfd'}: |:d	      \  };}< |:d      \  }=}>Kd    }?|}@|9j]                  t)        t+        |5|7||8|-|6f|;|<|=|>|?|@f                  } d}|stS        |d      jT                  r|st/        K      d k(  st/        K      d k  r|d( }At/        K      d k(  rK\  } }!}B}C A| |!       |A|B|C      z
  } d}nNt/        K      dk(  rK\  } }!}B A|!|B       |A| |      z
  } d}n$t/        K      dk(  rK\  } }!| dz   A|!|      z
  } d}|rtG        |       n|}D|	dz  }	|r|%t/        K      |k(  rDD|k(  r|	dkD  rtO        d)      |j'                  It/        K      |	d
       t[        | gi |}E|ErE\  } I || I      \  } I| IfS c c}}w c c}w c c}w c c}w # tN        $ r Y w xY w# tN        $ r |*rtO        d      Y w xY wc c}w c c}w )*a	  
    Remove radicals with symbolic arguments and return (eq, cov),
    None, or raise an error.

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

    None is returned if there are no radicals to remove.

    NotImplementedError is raised if there are radicals and they cannot be
    removed or if the relationship between the original symbols and the
    change of variable needed to rewrite the system as a polynomial cannot
    be solved.

    Otherwise the tuple, ``(eq, cov)``, is returned where:

    *eq*, ``cov``
        *eq* is an equation without radicals (in the symbol(s) of
        interest) whose solutions are a superset of the solutions to the
        original expression. *eq* might be rewritten in terms of a new
        variable; the relationship to the original variables is given by
        ``cov`` which is a list containing ``v`` and ``v**p - b`` where
        ``p`` is the power needed to clear the radical and ``b`` is the
        radical now expressed as a polynomial in the symbols of interest.
        For example, for sqrt(2 - x) the tuple would be
        ``(c, c**2 - 2 + x)``. The solutions of *eq* will contain
        solutions to the original equation (if there are any).

    *syms*
        An iterable of symbols which, if provided, will limit the focus of
        radical removal: only radicals with one or more of the symbols of
        interest will be cleared. All free symbols are used if *syms* is not
        set.

    *flags* are used internally for communication during recursive calls.
    Two options are also recognized:

        ``take``, when defined, is interpreted as a single-argument function
        that returns True if a given Pow should be handled.

    Radicals can be removed from an expression if:

        *   All bases of the radicals are the same; a change of variables is
            done in this case.
        *   If all radicals appear in one term of the expression.
        *   There are only four terms with sqrt() factors or there are less than
            four terms having sqrt() factors.
        *   There are only two terms with radicals.

    Examples
    ========

    >>> from sympy.solvers.solvers import unrad
    >>> from sympy.abc import x
    >>> from sympy import sqrt, Rational, root

    >>> unrad(sqrt(x)*x**Rational(1, 3) + 2)
    (x**5 - 64, [])
    >>> unrad(sqrt(x) + root(x + 1, 3))
    (-x**3 + x**2 + 2*x + 1, [])
    >>> eq = sqrt(x) + root(x, 3) - 2
    >>> unrad(eq)
    (_p**3 + _p**2 - 2, [_p, _p**6 - x])

    F)r   r=   c           	         rN\  }}t        ||       j                  |       dv r&| |j                  |t        || fi d         gd d  y t        | |gd d  y )N)rm   r   r   )rT   r  rn   r  r-  )r   r   oldpolder  uflagss       r   _covzunrad.<locals>._cov,  se    JD$Aqz  #v-TYYtWQ-DV-DQ-GHIA))VCFr   c                   |rO|\  }}|t        |j                        i}| j                  |      } |j                  |      |j                  |      g}t        t	        | j                         d   d      d      } | j                  rag }| j                  D ]H  }|j                  r|j                  r|j                  |j                         8|j                  |       J t        | } t        t        j                  |             }d}t        |      D ]  \  }	}
|
j!                         s|
 ||	<   d}  |rt        |ddi} | |fS )Nr   Tr   )clearFr   )r   namer8  r   r   r   r9  r   r   r   rs   r   r	   rp   r   r)  could_extract_minus_sign)r   r  r   r   repr   r   margschangedrz   r  s              r   
_canonicalzunrad.<locals>._canonical7  s-   DAqeAFFm$CS!B::c?AJJsO4C (2#4#4#6q#9TJRVW99DWW #;;88KK'KKN# dB S]]2&'e$ 	DAq))+2a	 e,e,B3wr   c                    | j                         d   j                         d   }|j                  st        j                  S |j
                  S )Nrm   r   )ry  r{  r   r   r   rz  )powrq  s     r   _Qzunrad.<locals>._QY  s:    OOa --/2}}55Lss
r   c                    t        j                  |       D ],  }|j                  s |      dk(  r|j                  z  s, y y)Nrm   TF)r	   r   r   r   )r   r  r  r
  s     r   _takezunrad.<locals>._takea  sI    ==# 	C::#w!|$&	 r   r  Nr   )r  r   rptT)radicalr  r   c              3  P   K   | ]  } |j                   j                     y wr   )r(   r   )r   ri  r
  s     r   r   zunrad.<locals>.<genexpr>  s      
*915599d
*s   #&c                    d}t               }t               }| j                  D ]H  } |      }|dk7  s|j                  |       t        ||      }|j                  |j                         J |||fS rt  )r   r  r   r   r   )r  lcmradsr  ri  rz  r  s         r   _rads_bases_lcmzunrad.<locals>._rads_bases_lcm  sp     u 	"A1AAv3l		!&&!	" UCr   r   )nonnegativer   rm   r   _reversez&no successful change of variable foundr     abcdAB	         ?   r   -      <   rj  $      T   ~   c                ^    t        |        }t        |    |z        t        |        fS r   )r	   rR   )rz   r|  BASESRADinfortermss     r   _tzunrad.<locals>._t*  s:    a.%fQik2Ca4HHHr   c                *    | dz  |dz  z   d| z  |z  z   S r|  r   )r  r|  s     r   _norm2zunrad.<locals>._norm29  s!    a4!Q$;1Q..r   zCannot remove all radicals)/r%  rq   r   r   r   r   r  rt   rA   r   r   r   r   r   r   r  r  r   r   r   r   r;  r6  r   r   r   r   r  r  rs   r(  r   rp   r   r    rH   r  r   r  r-  rn   r'   r   ru   r	   r  r8  )Mr   r
  r   r  r  r  r~   r   nwasr  r  ri  r  r  r  r  r  covsymnewsymsr  dradr   rT  commonrz   r!  othersrQ  depthr|  r  r  r0r  i0_rads0_bases0lcm0i1_rads1_bases1lcm1reverser   t1rq  r   neweqtmpnewcovnewpnewcLCMr  r   rW  r   zzr  aaAAbbBBccddr  r2r3	new_depthneqr  r  r  r  r  r  r  sM    `                                                                    @@@@@@@r   r  r    s   F %0F	 D	 We,E"bVVbff_D! 	rQ/5578:Ae&&q!, :NCs 
<D=	>B				Q	B	"	%B	|| t9'D::<Dyy-!E!HA-D- 2::tD | 
*T
**  't,D%3D)F eG .tann,-.$D%D	*+,D"XF==(D -8))*77=F8AQ89:CC#r"))!,- &**R.!F'-{{}5!c6!9o5F (4012F	BrNE
6{a!1!1cAgAY^#~.v;!q	 0 0&)..)Fu:?		A4y1}NNWQZ #AfckAoq;F;--\\^((FCK8==aQHVVS[1_-!"c** v;!AY^q	zC&77a[++B99Z/B-<RZZ\-JJ*VWd-<RZZ\-JJ*VWd$Qx $G!#RB!#RB&(OFAt &\FBr)A! %%")!Q"9&"9C#&&9 9$&GGAs1v$669I$I &%'E"'v"6C"-0
F#)17JD$$(qvvf(/f(G(G(J0L %M %)O%* $VQ!%B!)%6 I$J [A ;AAQOAIIK0ADCECAws|q DHHQK(fjjm,a"DHHQK(fjjm,Aws|tAws|0q075>T!WU^3'+AwQ$DGT!W+1!9fQi(F1Ivay75>T!WU^3AVAY)>)G!(KKBB!WS\A%:B'CQa'C$Aq!Q1 a%!Q$q!t)a1fQhq!tmAqD.@"@!Q$q!tAqD(#)+,QT6!Q$;q!t+;A+=#>@A!Q$q!tAaQRd@R#S!Q$q!tAqD(#)+,Q3q!t8AqD=A+=#>@B1Qq!tAqD@PQRTUQU@U#V 1Qq!tAqD(A-a/#0 35Q$q&A+ad2B12DQT2I#J 1Qq!tAqD(A-	#. 12!AqDA	#> AC1QT	!Q$q@P	#Q
 1QT	!Q$q!t+#,
 /1d1a4i1nQT.A#B
 EGqDAIaQRdNSTVWSWDW#X 1QT	!AqD(#) ,-Q3q!t8AqD=#9 <=a419#E HIAvaQRd{STVWSWGW#X !Q$q!tAqD(*#+ ./q!tVAqD[]1a4-?#@ CDAqD&A+aQRdBR#S !AqDA#& )+1QT	!Q$q(8#9 <>a419QT>!Q$;N#O 1QT	!Q$q!t+	#, /1d1a4i1nQT.A	#B EGqDAIaKPQSTPTDT	#U !AqDA
#& )*1
#- 01Avax
#8 ;=QT'!Q$,
#G JLAqDQRTUQU
#V AqDA#& ),AqDA#6 9;1a41#E HJ!Q$wqRSt|#T !AqD#! $%a4#( )BI  UFBUFB )BBT#Aq!Q*RRR0+2 &3 4B B3{%%vK1$Fa/ v;!#%+NBBB&R.8BB[A%!'JBBB&V*<<BB[A%#FBQF!33BB"$
2%I1HC S[D%8%)u*<a!">??	LL3v;s;<

#T
#U
#C
CS!GBs7NO: .^  9
 6: ' V  3 %&&9$L'N !N !%% B& (DsP   n4,n::n:n??oA(o	 7Bo%o60o;		ooo32o3)r  r  r  r   )r  list[dict[Expr, Expr]]returnr   )__doc__
__future__r   
sympy.corer   r   r   r   r   r	   sympy.core.assumptionsr
   sympy.core.exprtoolsr   sympy.core.functionr   r   r   r   r   r   r   r   r   r   r   sympy.core.logicr   sympy.core.numbersr   r   r   sympy.core.intfuncr   r   sympy.core.powerr   sympy.core.relationalr   r   sympy.core.sortingr    r!   sympy.core.sympifyr"   r#   sympy.core.traversalr$   sympy.logic.boolalgr%   r&   sympy.functionsr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   (sympy.functions.combinatorial.factorialsr6   %sympy.functions.elementary.hyperbolicr7   $sympy.functions.elementary.piecewiser8   r9   (sympy.functions.elementary.trigonometricr:   sympy.integrals.integralsr;   sympy.ntheory.factor_r<   sympy.simplifyr=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   sympy.simplify.sqrtdenestrH   sympy.simplify.furI   rJ   rK   rL   sympy.strategies.rlrM   sympy.matrices.exceptionsrN   sympy.matricesrO   rP   sympy.polysrQ   rR   rS   rT   sympy.polys.solversrU   rV   sympy.polys.polyerrorsrW   rX   sympy.polys.polytoolsrY   sympy.utilities.lambdifyrZ   sympy.utilities.miscr[   r\   sympy.utilities.iterablesr]   r^   r_   r`   ra   rb   rc   rd   sympy.utilities.decoratorre   r  rf   sympy.solvers.polysysrg   typesrh   collectionsri   	itertoolsrj   rk   r   r   r   r   r   r   rf  r<  r=  r?  r>  r  r  r  rh  rX  ra  rd  rn  rx  r  r  r  r  r  r  sympy.solvers.bivariater  r  r  r   r   r   <module>rK     s   # 9 9 4 -. . . . ' 8 8 0   ( 8 0 3 0< < < < < = D J J . *    1 3 3 ' > ( 3 3 @ D % - 3H H H 9  3  # + *>ZD
0JfsJp_ED tn%;FR R slPf7tAH$ND6*;,: 	,	.1
BN  F FR~Brl4 4r   