ó
¡¼™\c           @  sA  d  d l  m Z m Z d  d l m Z d  d l m Z m Z d  d l m	 Z	 d  d l
 m Z d  d l m Z d  d l m Z d  d l m Z m Z m Z d  d	 l m Z d  d
 l m Z m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z m  Z  d „  Z! d d „ Z# d „  Z$ d „  Z% d „  Z& d „  Z' d S(   iÿÿÿÿ(   t   print_functiont   division(   t   Add(   t   orderedt   range(   t
   expand_log(   t   Pow(   t   S(   t   Dummy(   t   LambertWt   expt   log(   t   root(   t   Polyt   factor(   t   _mexpand(   t   separatevars(   t   collect(   t   solvet   _invertc           sˆ   ‡  f d †  |  j  Dƒ } xh t | ƒ D]Z } d | } | | k r& | | k r& | j ƒ  d t j k	 rp | } n  | j | ƒ q& q& W| S(   sª  process the generators of ``poly``, returning the set of generators that
    have ``symbol``.  If there are two generators that are inverses of each other,
    prefer the one that has no denominator.

    Examples
    ========

    >>> from sympy.solvers.bivariate import _filtered_gens
    >>> from sympy import Poly, exp
    >>> from sympy.abc import x
    >>> _filtered_gens(Poly(x + 1/x + exp(x)), x)
    {x, exp(x)}

    c           s%   h  |  ] } ˆ  | j  k r | ’ q S(    (   t   free_symbols(   t   .0t   g(   t   symbol(    s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pys	   <setcomp>!   s   	 i   (   t   genst   listt   as_numer_denomR   t   Onet   remove(   t   polyR   R   R   t   ag(    (   R   s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pyt   _filtered_gens   s    
	c           s£   g  |  j  ˆ  ƒ D]D } | sN | j r5 | | j k sN | j r | j | ƒ r | ^ q } t | ƒ d k rt | d S| rŸ t t t | ƒ ƒ d ‡  f d †  ƒSd S(   s~  Returns the term in lhs which contains the most of the
    func-type things e.g. log(log(x)) wins over log(x) if both terms appear.

    ``func`` can be a function (exp, log, etc...) or any other SymPy object,
    like Pow.

    If ``X`` is not ``None``, then the function returns the term composed with the
    most ``func`` having the specified variable.

    Examples
    ========

    >>> from sympy.solvers.bivariate import _mostfunc
    >>> from sympy.functions.elementary.exponential import exp
    >>> from sympy.utilities.pytest import raises
    >>> from sympy.abc import x, y
    >>> _mostfunc(exp(x) + exp(exp(x) + 2), exp)
    exp(exp(x) + 2)
    >>> _mostfunc(exp(x) + exp(exp(y) + 2), exp)
    exp(exp(y) + 2)
    >>> _mostfunc(exp(x) + exp(exp(y) + 2), exp, x)
    exp(x)
    >>> _mostfunc(x, exp, x) is None
    True
    >>> _mostfunc(exp(x) + exp(x*y), exp, x)
    exp(x)
    i   i    t   keyc           s   |  j  ˆ  ƒ S(   N(   t   count(   t   x(   t   func(    s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pyt   <lambda>M   t    N(	   t   atomst	   is_SymbolR   t   hast   lent   maxR   R   t   None(   t   lhsR#   t   Xt   tmpt   fterms(    (   R#   s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pyt	   _mostfunc+   s    %%c         C  s‘   |  j  ƒ  }  |  j | ƒ \ } } |  j s@ d } | | } } n' | } t | ƒ j | d t ƒ\ } } | j ƒ  r„ | } | } n  | | | f S(   s(  Return ``a, b, X`` assuming ``arg`` can be written as ``a*X + b``
    where ``X`` is a symbol-dependent factor and ``a`` and ``b`` are
    independent of ``symbol``.

    Examples
    ========

    >>> from sympy.functions.elementary.exponential import exp
    >>> from sympy.solvers.bivariate import _linab
    >>> from sympy.abc import x, y
    >>> from sympy import S
    >>> _linab(S(2), x)
    (2, 0, 1)
    >>> _linab(2*x, x)
    (2, 0, x)
    >>> _linab(y + y*x + 2*x, x)
    (y + 2, y, x)
    >>> _linab(3 + 2*exp(x), x)
    (2, 3, exp(x))
    i    t   as_Add(   t   expandt   as_independentt   is_AddR   t   Falset   could_extract_minus_sign(   t   argR   t   indt   dept   bt   aR"   (    (    s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pyt   _linabQ   s    	!
c         C  s7  t  t |  ƒ ƒ }  t |  t | ƒ } | s. g  S|  j | d ƒ } t | t ƒ r© |  | j | | j d ƒ }  | j d } t | t ƒ s g  S| j d } |  | 7}  n  | | j k r¼ g  St | | ƒ \ } } } t	 |  | | ƒ } | j
 | ƒ } | d k s| | j k rg  S| j d }	 t |	 | ƒ \ }
 } } | | k rJg  St d ƒ } g  } xÔ d d g D]Æ } t | | |
 t | | | |
 ƒ t | | ƒ | ƒ } | rÁ| j rÁqin  | |
 | | | } t | | | ƒ } xA t | ƒ D]3 \ } } | j | | ƒ | | <| j | | ƒ qøWqiW| S(   s   
    Given an expression assumed to be in the form
        ``F(X, a..f) = a*log(b*X + c) + d*X + f = 0``
    where X = g(x) and x = g^-1(X), return the Lambert solution if possible:
        ``x = g^-1(-c/b + (a/d)*W(d/(a*b)*exp(c*d/a/b)*exp(-f/a)))``.
    i    t   rhsiÿÿÿÿN(   R   R   R0   R   t   subst
   isinstancet   argsR   R<   R   t   as_coefficientR+   R   R	   R
   t   is_realR   t	   enumeratet   append(   t   eqR"   t   mainlogt   othert   dt   ft   X2t   logtermR;   t   logargR:   t   ct   X1t   ut   solt   kt   lR=   t   solnst   iR.   (    (    s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pyt   _lambertu   sF    <c         C  s!  |  j  | d t ƒ\ } } | } g  | D]< } | j t t g k s_ | j r) | | j j k r) | ^ q) } | s} t ƒ  ‚ n  | j r§ t	 t | ƒ ƒ } t | ƒ } n  t
 | d t ƒ} t ƒ  } t | | | ƒ \ }	 } |	 j i | | 6ƒ } g  }
 |
 st | t | ƒ } | r| j rL| d k rLt t | ƒ t | ƒ | ƒ }
 q| j r| j | d ƒ } | r| j rg  | j t ƒ D] } | | j k r‡| ^ q‡r| sËt | ƒ t | | ƒ } n t | | ƒ t | | ƒ } t t	 | ƒ | ƒ }
 qt | | | ƒ }
 qqn  |
 st | t | ƒ } | rt | | ƒ } | j r‡| d k r‡t t	 t | ƒ t | ƒ ƒ | ƒ }
 q| j r| j | d ƒ } | | } | | } | j ƒ  rå| j ƒ  rå| d 9} | d 9} n  t | ƒ t | ƒ } t t	 | ƒ | ƒ }
 qqn  |
 søt | t | ƒ } | rø| | j j k røt | | ƒ } | j r•| d k r•t t	 t | ƒ t | ƒ ƒ | ƒ }
 qõ| j rõ| j | d ƒ } | | } | | } t | ƒ t | ƒ } t t	 | ƒ | ƒ }
 qõqøn  |
 st d |  ƒ ‚ n  t t |
 ƒ ƒ S(   s  Return solution to ``f`` if it is a Lambert-type expression
    else raise NotImplementedError.

    The equality, ``f(x, a..f) = a*log(b*X + c) + d*X - f = 0`` has the
    solution,  `X = -c/b + (a/d)*W(d/(a*b)*exp(c*d/a/b)*exp(f/a))`. There
    are a variety of forms for `f(X, a..f)` as enumerated below:

    1a1)
      if B**B = R for R not [0, 1] then
      log(B) + log(log(B)) = log(log(R))
      X = log(B), a = 1, b = 1, c = 0, d = 1, f = log(log(R))
    1a2)
      if B*(b*log(B) + c)**a = R then
      log(B) + a*log(b*log(B) + c) = log(R)
      X = log(B); d=1, f=log(R)
    1b)
      if a*log(b*B + c) + d*B = R then
      X = B, f = R
    2a)
      if (b*B + c)*exp(d*B + g) = R then
      log(b*B + c) + d*B + g = log(R)
      a = 1, f = log(R) - g, X = B
    2b)
      if -b*B + g*exp(d*B + h) = c then
      log(g) + d*B + h - log(b*B + c) = 0
      a = -1, f = -h - log(g), X = B
    3)
      if d*p**(a*B + g) - b*B = c then
      log(d) + (a*B + g)*log(p) - log(c + b*B) = 0
      a = -1, d = a*log(p), f = -log(d) - g*log(p)
    R1   t   deepi    iÿÿÿÿs:   %s does not appear to have a solution in terms of LambertW(   R3   t   TrueR#   R
   R   t   is_PowR   t   NotImplementedErrort   is_MulR   R   R   R   t   xreplaceR0   RU   R4   R>   R&   R   R   R6   R   R   (   RI   R   R   t   nrhsR,   R=   R.   t   lamcheckt   rRT   t   solnRF   RG   t   difft   mainexpt   maintermt   mainpow(    (    s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pyt   _solve_lambert¦   s|    !'		"	(	


(	

c           s  t  d d t ƒ} | j d t ƒ rØ t |  ˆ  ˆ ƒ } | j ƒ  }  t  ƒ  } t  ƒ  } t t |  j i | ˆ  6| ˆ 6ƒ | | ƒ | | d t ƒ} | rÔ i ˆ  | 6ˆ | 6}	 | d j |	 ƒ | d j |	 ƒ | d f Sd S|  } | j ƒ  }  t	 j
 | j ƒ  ƒ }
 g  } xs |
 D]T } t | j ˆ  | ˆ ƒ ƒ } | j } ˆ  | k sOˆ | k rSPn  | j | ƒ qWˆ  ˆ t	 | Œ  | f S‡  ‡ f d †  } g  } | j ˆ  ƒ } | j ˆ ƒ | k r5t | j ˆ  | ƒ | ƒ } t | j ˆ | ƒ | ƒ } | |  ˆ  | | ˆ | ƒ } | d k	 r5| ˆ  | ˆ | | f Sn  g  } | j ˆ  ƒ } | j ˆ ƒ | k rx¯ t d ƒ D]ž } t | j ˆ  | ˆ | ƒ | ƒ } t | j ˆ | ƒ | ƒ } | |  ˆ  | | ˆ | ˆ ƒ } | d k	 rý| ˆ  ˆ | ˆ | | f Sˆ ˆ  ‰  ‰ qlWn  d S(	   s÷  Given an expression, f, 3 tests will be done to see what type
    of composite bivariate it might be, options for u(x, y) are::

        x*y
        x+y
        x*y+x
        x*y+y

    If it matches one of these types, ``u(x, y)``, ``P(u)`` and dummy
    variable ``u`` will be returned. Solving ``P(u)`` for ``u`` and
    equating the solutions to ``u(x, y)`` and then solving for ``x`` or
    ``y`` is equivalent to solving the original expression for ``x`` or
    ``y``. If ``x`` and ``y`` represent two functions in the same
    variable, e.g. ``x = g(t)`` and ``y = h(t)``, then if ``u(x, y) - p``
    can be solved for ``t`` then these represent the solutions to
    ``P(u) = 0`` when ``p`` are the solutions of ``P(u) = 0``.

    Only positive values of ``u`` are considered.

    Examples
    ========

    >>> from sympy.solvers.solvers import solve
    >>> from sympy.solvers.bivariate import bivariate_type
    >>> from sympy.abc import x, y
    >>> eq = (x**2 - 3).subs(x, x + y)
    >>> bivariate_type(eq, x, y)
    (x + y, _u**2 - 3, _u)
    >>> uxy, pu, u = _
    >>> usol = solve(pu, u); usol
    [sqrt(3)]
    >>> [solve(uxy - s) for s in solve(pu, u)]
    [[{x: -y + sqrt(3)}]]
    >>> all(eq.subs(s).equals(0) for sol in _ for s in sol)
    True

    RO   t   positivet   firsti    i   i   Nc           sA   t  |  j | | ƒ ƒ } | j } ˆ  | k s9 ˆ | k r= d  S| S(   N(   R   R>   R   R+   (   RI   t   vRM   t   newt   free(   R"   t   y(    s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pyt   okw  s    	(   R   RW   t   popR   t   as_exprt   bivariate_typeR>   R5   R[   R   t	   make_argsR   R   RD   t   degreeR   t   coeff_monomialR+   R   (   RI   R"   Rj   t   kwargsRO   t   pt   _xt   _yt   rvt   repsR@   Rh   R;   Ri   Rk   RH   R:   t   itry(    (   R"   Rj   s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pyRn   4  sR    '		;+	$"N((   t
   __future__R    R   t   sympy.core.addR   t   sympy.core.compatibilityR   R   t   sympy.core.functionR   t   sympy.core.powerR   t   sympy.core.singletonR   t   sympy.core.symbolR   t&   sympy.functions.elementary.exponentialR	   R
   R   t(   sympy.functions.elementary.miscellaneousR   t   sympy.polys.polytoolsR   R   R   t   sympy.simplify.simplifyR   t   sympy.simplify.radsimpR   t   sympy.solvers.solversR   R   R   R+   R0   R<   RU   Rd   Rn   (    (    (    s6   lib/python2.7/site-packages/sympy/solvers/bivariate.pyt   <module>   s&   	&	$	1	Ž