o
    +ôjÎE  ã                   @   s  d dl 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 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 dl"m#Z# dd„ Z$d dd„Z%dd„ Z&dd„ Z'dd„ Z(ddœdd„Z)dS )!é    )ÚAdd)Úfactor_terms)Ú
expand_logÚ_mexpand)ÚPow)ÚS)Úordered)ÚDummy)ÚLambertWÚexpÚlog)Úroot)Úroots)ÚPolyÚfactor)Úseparatevars)Úcollect)Úpowsimp)ÚsolveÚ_invert)Úuniqc                    s^   ‡ fdd„| j D ƒ}t|ƒD ]}d| }||v r,||v r,| ¡ d tjur'|}| |¡ q|S )aª  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 v r|’qS © ©Úfree_symbols)Ú.0Úg©Úsymbolr   úh/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/sympy/solvers/bivariate.pyÚ	<setcomp>%   s    z!_filtered_gens.<locals>.<setcomp>é   )ÚgensÚlistÚas_numer_denomr   ÚOneÚremove)Úpolyr   r!   r   Úagr   r   r   Ú_filtered_gens   s   
€r(   Nc                    sP   ‡ fdd„|   ˆ¡D ƒ}t|ƒdkr|d S |r&ttt|ƒƒ‡fdd„d�S dS )	a+  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 import exp
    >>> 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)
    c                    s4   g | ]}ˆ rˆ j rˆ |jv sˆ j s| ˆ ¡r|‘qS r   )Ú	is_Symbolr   Úhas©r   Útmp)ÚXr   r   Ú
<listcomp>J   s    ÿ
ÿÿÿz_mostfunc.<locals>.<listcomp>r    r   c                    s
   |   ˆ ¡S ©N)Úcount)Úx)Úfuncr   r   Ú<lambda>P   s   
 z_mostfunc.<locals>.<lambda>)ÚkeyN)ÚatomsÚlenÚmaxr"   r   )Úlhsr2   r-   Zftermsr   )r-   r2   r   Ú	_mostfunc/   s   r9   c                 C   s–   t |  ¡ ƒ} |  |¡\}}| jr$|jr$t||ƒ\}}}|| || |fS | js/d}||}}n|}t|ƒj|dd�\}}| ¡ rF| }| }|||fS )aî  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.solvers.bivariate import _linab
    >>> from sympy.abc import x, y
    >>> from sympy import exp, 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))
    r   F©Úas_Add)r   ÚexpandÚas_independentÚis_MulÚis_AddÚ_linabr   Úcould_extract_minus_sign)Úargr   ÚindÚdepÚaÚbr1   r   r   r   r@   T   s   
r@   c                    sÖ  t t| ƒƒ} t| t|ƒ}|sg S |  |d¡}t| tƒr>| |  ||jd ¡} |jd }t|tƒs3g S | jd  }| |7 } ||jvrEg S t||ƒ\‰}}t	| | |ƒ}| 
|¡‰ ˆ du sb|ˆ jv rdg S |jd }t||ƒ\‰}}	|	|krwg S tdƒ‰t|	ˆ |ƒ}
ddg}g }|ˆ ˆ|  ˆ  ˆ  ¡ \}}| ¡ \}}t|| ƒ}tdƒ}‡ ‡‡fdd„t|| | |ƒ ¡ D ƒ}|D ]*}|D ]%}t||ƒ}|rÏ|jsÏqÂ| ˆ ˆ ˆ |  ‰| ‡‡fdd	„|
D ƒ¡ qÂq¾|S )
zô
    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,
        ``x = g^-1(-c/b + (a/d)*W(d/(a*b)*exp(c*d/a/b)*exp(-f/a)))``.
    r   NÚrhséÿÿÿÿÚtc                    s   g | ]
}ˆˆ ˆ  | ‘qS r   r   )r   rI   )rE   rF   Údr   r   r.   ³   s    z_lambert.<locals>.<listcomp>c                 3   s   � | ]	}|  ˆˆ ¡V  qd S r/   )Úsubs)r   Zxu)rG   Úur   r   Ú	<genexpr>½   s   € z_lambert.<locals>.<genexpr>)r   r   r9   r   rK   Ú
isinstanceÚargsr   r@   r   Úas_coefficientr	   r   r#   Úas_coeff_Mulr   r   Úkeysr
   Úis_realÚextend)Úeqr1   ÚmainlogÚotherÚfÚX2ÚlogtermÚlogargÚcÚX1ZxusolnsZlambert_real_branchesÚsolÚnumÚdenÚpÚerI   rO   rB   ÚkÚwr   )rE   rF   rJ   rG   rL   r   Ú_lamberty   sR   





 
(

úre   c                    s®  ‡ fdd„}| j ˆdd�\}}| }‡fdd„ˆ D ƒ}|s tƒ ‚|js&|jrštdi ˆj¤Ž‰| ‡fdd	„‡fd
d	„¡}|jrp| ˆ¡rp| ˆd¡}|| }	|| }
|	jso|
ro|	 t	j
t	j¡sott|	ƒt|
ƒ ƒ}||ˆˆƒS n#|jr“|r“tt|ƒdd�}t|ƒ}| ˆ¡r“|jr“|| }||ˆˆƒS | ˆˆi¡}tt|dd�ƒ}tƒ }t|| ˆƒ\}}| ||i¡}g }|�st|tˆƒ}|�r|jrÖ|dkrÖtt|ƒt|ƒ ˆƒ}nG|j�r| |d¡}|�r|j�s‡fdd„| t¡D ƒ�r|�st|ƒt|| ƒ }nt|| ƒt|| ƒ }tt|ƒˆƒ}nt|| ˆƒ}|�sxt|tˆƒ}|�rxt||ƒ}|j�rE|dk�rEttt|ƒt|ƒ ƒˆƒ}n3|j�rx| |d¡}|| }|| }| ¡ �ri| ¡ �ri|d9 }|d9 }t|ƒt|ƒ }tt|ƒˆƒ}|�sÈt|tˆƒ}|�rÈˆ|jjv �rÈt||ƒ}|j�r§|dk�r§ttt|ƒt|ƒ ƒˆƒ}n!|j�rÈ| |d¡}|| }|| }t|ƒt|ƒ }tt|ƒˆƒ}|�sÑtd|  ƒ‚tt|ƒƒS )aä  Return solution to ``f`` if it is a Lambert-type expression
    else raise NotImplementedError.

    For ``f(X, a..f) = a*log(b*X + c) + d*X - f = 0`` the solution
    for ``X`` is ``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 in [0, 1] (since those cases would already
      be solved before getting here) then log of both sides gives
      log(B) + log(log(B)) = log(log(R)) and
      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 of both sides gives
      log(B) + a*log(b*log(B) + c) = log(R) and
      X = log(B), d=1, f=log(R)
    1b)
      if a*log(b*B + c) + d*B = R and
      X = B, f = R
    2a)
      if (b*B + c)*exp(d*B + g) = R then log of both sides gives
      log(b*B + c) + d*B + g = log(R) and
      X = B, a = 1, f = log(R) - g
    2b)
      if g*exp(d*B + h) - b*B = c then the log form is
      log(g) + d*B + h - log(b*B + c) = 0 and
      X = B, a = -1, f = -h - log(g)
    3)
      if d*p**(a*B + g) - b*B = c then the log form is
      log(d) + (a*B + g)*log(p) - log(b*B + c) = 0 and
      X = B, a = -1, d = a*log(p), f = -log(d) - g*log(p)
    c                    sL   ‡ ‡‡fdd„dD ƒ\}}t |ˆˆƒ}||kr | t |ˆˆƒ¡ tt|ƒƒS )a„  Return the unique solutions of equations derived from
        ``expr`` by replacing ``t`` with ``+/- symbol``.

        Parameters
        ==========

        expr : Expr
            The expression which includes a dummy variable t to be
            replaced with +symbol and -symbol.

        symbol : Symbol
            The symbol for which a solution is being sought.

        Returns
        =======

        List of unique solution of the two equations generated by
        replacing ``t`` with positive and negative ``symbol``.

        Notes
        =====

        If ``expr = 2*log(t) + x/2` then solutions for
        ``2*log(x) + x/2 = 0`` and ``2*log(-x) + x/2 = 0`` are
        returned by this function. Though this may seem
        counter-intuitive, one must note that the ``expr`` being
        solved here has been derived from a different expression. For
        an expression like ``eq = x**2*g(x) = 1``, if we take the
        log of both sides we obtain ``log(x**2) + log(g(x)) = 0``. If
        x is positive then this simplifies to
        ``2*log(x) + log(g(x)) = 0``; the Lambert-solving routines will
        return solutions for this, but we must also consider the
        solutions for  ``2*log(-x) + log(g(x))`` since those must also
        be a solution of ``eq`` which has the same value when the ``x``
        in ``x**2`` is negated. If `g(x)` does not have even powers of
        symbol then we do not want to replace the ``x`` there with
        ``-x``. So the role of the ``t`` in the expression received by
        this function is to mark where ``+/-x`` should be inserted
        before obtaining the Lambert solutions.

        c                    s   g | ]}ˆ   ˆ|ˆ i¡‘qS r   )Úxreplace)r   Úsgn©Úexprr   rI   r   r   r.     s    ÿzC_solve_lambert.<locals>._solve_even_degree_expr.<locals>.<listcomp>)rH   r    )Ú_solve_lambertrT   r"   r   )ri   rI   r   ZnlhsZplhsÚsols)r!   rh   r   Ú_solve_even_degree_exprã   s   *
ÿz/_solve_lambert.<locals>._solve_even_degree_exprTr:   c                    s0   g | ]}|j ttfv s|jrˆ |jjv r|‘qS r   )r2   r   r   Úis_Powr   r+   r   r   r   r.     s    ÿþz"_solve_lambert.<locals>.<listcomp>rI   c                    s   | j o| jˆ ko| jjS r/   )rm   Úbaser   Úis_even©Úir   r   r   r3   (  s   z _solve_lambert.<locals>.<lambda>c                    s
   ˆ | j  S r/   )r   rp   ©rI   r   r   r3   *  s   
r   )Úforce)Údeepc                    s   g | ]	}ˆ |j v r|‘qS r   r   r+   r   r   r   r.   \  s
    
ÿrH   z:%s does not appear to have a solution in terms of LambertWNrr   )r=   ÚNotImplementedErrorr?   r>   r	   Úassumptions0Úreplacer*   rK   r   ÚComplexInfinityÚNaNr   r   rf   r   r   r   r9   re   r5   r   r   r   rA   r   r"   r   )rX   r   r!   rl   Znrhsr8   rG   ZlamcheckZt_indepZt_termÚ_rhsrU   Úrrq   ÚsolnrV   rW   ÚdiffZmainexpZmaintermZmainpowr   )r!   r   rI   r   rj   Á   s¦   "6

ý
ÿ€
ÿ

ÿ
ÿrj   T©Úfirstc                   s8  t ddd�}|rFt| ˆ ˆƒ}| ¡ } t ƒ }t ƒ }tt|  ˆ |ˆ|i¡||ƒ||dd�}|rD|ˆ |ˆi}	|d  |	¡|d  |	¡|d fS d	S | }| ¡ } t | ¡ ¡}
g }|
D ]}t| ˆ |ˆ ¡ƒ}|j	}ˆ |v snˆ|v rp n| 
|¡ qWˆ ˆ t|Ž |fS ‡ ‡fd
d„}g }| ˆ ¡}| ˆ¡|krÃt| ˆ | ¡|ƒ}t| ˆ| ¡|ƒ}|| ˆ ||ˆ  | ƒ}|d	urÃ|ˆ  |ˆ  ||fS g }| ˆ ¡}| ˆ¡|k�rtdƒD ]C}t| ˆ | ˆ|  ¡|ƒ}t| ˆ| ¡|ƒ}|| ˆ ||ˆ  | ˆ ƒ}|d	u�r|ˆ  ˆ |ˆ  ||f  S ˆˆ ‰ ‰qÖd	S d	S )aç  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 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

    rL   T)ÚpositiveFr~   r   r    é   Nc                    s.   t |  ||¡ƒ}|j}ˆ |v sˆ|v rd S |S r/   )r   rK   r   )rX   Úvr\   ÚnewÚfree©r1   Úyr   r   Úokä  s   zbivariate_type.<locals>.ok)r	   r   Úas_exprÚbivariate_typerK   rf   r   Ú	make_argsr   r   ÚappendÚdegreer   Úcoeff_monomialÚrange)rX   r1   r†   r   rL   ra   Ú_xÚ_yÚrvÚrepsrO   rƒ   rE   r„   r‡   rJ   rF   Zitryr   r…   r   r‰   ¡  sV   '&"


ùr‰   r/   )*Zsympy.core.addr   Úsympy.core.exprtoolsr   Úsympy.core.functionr   r   Úsympy.core.powerr   Zsympy.core.singletonr   Úsympy.core.sortingr   Úsympy.core.symbolr	   Ú&sympy.functions.elementary.exponentialr
   r   r   Ú(sympy.functions.elementary.miscellaneousr   Úsympy.polys.polyrootsr   Úsympy.polys.polytoolsr   r   Úsympy.simplify.simplifyr   Úsympy.simplify.radsimpr   r   Úsympy.solvers.solversr   r   Úsympy.utilities.iterablesr   r(   r9   r@   re   rj   r‰   r   r   r   r   Ú<module>   s.    
%%H a