� ��g�E���ddlmZddlmZddlmZmZddlmZddl m Z ddl m Z ddl mZddlmZmZmZdd lmZdd lmZdd lmZmZdd lmZdd lmZddlmZddlm Z m!Z!ddl"m#Z#d�Z$dd�Z%d�Z&d�Z'd�Z(dd�d�Z)dS)�)�Add)� factor_terms)� expand_log�_mexpand)�Pow)�S)�ordered)�Dummy)�LambertW�exp�log)�root)�roots)�Poly�factor)� separatevars)�collect)�powsimp)�solve�_invert)�uniqc�����fd�|jD��}t|��D]L}d|z }||vrA||vr=|���dtjur|}|�|���M|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�&��h|] }�|jv� |��S��� free_symbols)�.0�g�symbols ��g/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/solvers/bivariate.py� <setcomp>z!_filtered_gens.<locals>.<setcomp>%s%��� =� =� =�!�F�a�n�$<�$<�A�$<�$<�$<��)�gens�list�as_numer_denomr�One�remove)�polyrr$r�ags ` r �_filtered_gensr+s����$ >� =� =� =�t�y� =� =� =�D� �$�Z�Z���� �q�S�� ��9�9��t���� � �"�"�1�%�Q�U�2�2��� �K�K��N�N�N�� �Kr"Nc������fd�|����D��}t|��dkr|dS|r.ttt |�����fd����SdS)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�p��g|]2}�r,�jr �|jvs�j�|�����0|��3Sr)� is_Symbolr�has)r�tmp�Xs �r � <listcomp>z_mostfunc.<locals>.<listcomp>Js]���)�)�)�c�Q�)� � �)��S�-�-�-� �K�.��G�G�A�J�J�.��-�-�-r"r#rc�.��|����S�N)�count)�x�funcs �r �<lambda>z_mostfunc.<locals>.<lambda>Ps������� � �r")�keyN)�atoms�len�maxr%r )�lhsr7r1�ftermss `` r � _mostfuncr?/s�����6)�)�)�)�S�Y�Y�t�_�_�)�)�)�F� �6�{�{�a����a�y�� �G��4�����(�(�.E�.E�.E�.E�F�F�F�F� �4r"c�z�t|�����}|�|��\}}|jr&|jrt ||��\}}}||z||z|fS|jsd}||}}n)|}t |���|d���\}}|���r| }| }|||fS)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)) rF��as_Add)r�expand�as_independent�is_Mul�is_Add�_linabr�could_extract_minus_sign)�argr�ind�dep�a�br6s r rGrGTs���( �s�z�z�|�|� $� $�C��!�!�&�)�)�H�C�� �z��c�j����f�%�%���1�a��1�u�c�!�e�Q��� �:�F� ���C�1��� ���C� � �/�/��u�/�E�E���1��!�!�#�#�� �B�� �B�� �a��7�Nr"c�������tt|����}t|t|��}|sgS|�|d��}t | t��r\||z �||jd��}|jd}t |t��sgS| jd }||z }||jvrgSt||��\�}}t||z |��}|� |����� |�jvrgS|jd}t||��\�}} | |krgStd���t| �z |��} ddg} g} |�z�|zz �z �z � ��\} }|���\}}t| |z ��}td��}���fd�t!||z|z |�����D��}|D]R}| D]M}t%||��}|r|js�| �z ��z |zz�| ���fd�| D�����N�S| 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)))``. rN�rhs������tc�&��g|] }���zz |z��Srr)rrQrLrM�ds ���r r2z_lambert.<locals>.<listcomp>�s%��� 9� 9� 9�!�A�q��s�G�A�I� 9� 9� 9r"c3�D�K�|]}|�����V��dSr4)�subs)r�xurO�us ��r � <genexpr>z_lambert.<locals>.<genexpr>�s/�����9�9�2�r�w�w�q�#���9�9�9�9�9�9r")rrr?r rU� isinstance�argsrrGr�as_coefficientr rr&� as_coeff_Mulr r�keysr �is_real�extend)�eqr6�mainlog�other�f�X2�logterm�logarg�c�X1�xusolns�lambert_real_branches�sol�num�den�p�erQrZrI�k�wrLrMrSrOrWs @@@@@r �_lambertrrys�������� �*�R�.�.� !� !�B���C��#�#�G� ��� � �G�G�G�Q� � �E��5�&�#�����5�j� � �w�� �Q�� 8� 8���,�q�/���'�3�'�'� ��I��&��q�!�!�� �e� ����"�"�"�� ��e�Q���H�A�q�"��b�5�j�'�*�*�G����w�'�'�A��y�A���'�'�� � �\�!�_�F��f�a� � �H�A�q�"� �R�x�x�� � �e� � �A��B��F�A���G� ��G�� �C��1��Q�q�S��!� �A� �-�-�/�/�H�C�� � � � � �F�A�s� �C��G� � �A� �c� � �A� 9� 9� 9� 9� 9� 9�u�Q��T�A�X�q�1�1�6�6�8�8� 9� 9� 9�D��:�:��&� :� :�A���a� � �A�� ��� ���"�Q�$�!�A�#�q��.�C� �J�J�9�9�9�9�9��9�9�9� 9� 9� 9� 9�  :� �Jr"c�p �����fd�}|��d���\}}| }�fd��D��}|st���|js|j�rQt di�j���|��fd��fd���}|jr�|����r�|��d��}||z } ||z } | jse| rc| �tj tj ��s9tt| ��t| ��z ��} || ����Snd|jr]|r[tt|��d� ��}t|��}|����r|jr||z } || ����S|���i��}tt!|d� ����}t ��} t#|| z ���\} }| �| |i��}g}|�st%|t���}|�r|jr4|dkr.t't|��t|��z ���}n�|jr�|�|d��}|r�|js��fd �|�t*��D��rh|s#t|��t||z ��z }n%t||z ��t||z ��z }t't|�����}nt'||z ���}|�st%|t,���}|r�t/||��}|jrA|dkr;t'tt|��t|��z �����}n�|jr�|�|d��}||z }||z }|���r|���r |d z}|d z}t|��t|��z }t't|�����}|s�t%|t*���}|rɉ|jjvr�t/||��}|jrA|dkr;t'tt|��t|��z �����}nc|jr\|�|d��}||z }||z }t|��t|��z }t't|�����}|std |z���t5t7|����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����������fd�dD��\}}t|����}||kr$|�t|������tt|����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�D��g|]}���|�zi����Sr)�xreplace)r�sgn�exprrrQs ���r r2zC_solve_lambert.<locals>._solve_even_degree_expr.<locals>.<listcomp> s:���?�?�?�/2�D�M�M�1�c�&�j�/� *� *�?�?�?r")rPr#)�_solve_lambertr_r%r)rxrQr�nlhs�plhs�solsr$s``` �r �_solve_even_degree_exprz/_solve_lambert.<locals>._solve_even_degree_expr�s�������T?�?�?�?�?�?�6=�?�?�?� ��d��d�F�D�1�1�� �4�<�<� �K�K��t�V�T�:�:� ;� ;� ;� �D��J�J���r"TrAc�h��g|].}|jttfvs|jr�|jjv�,|��/Sr)r7r r �is_Powr�rr0rs �r r2z"_solve_lambert.<locals>.<listcomp>sP���B�B�B���H��c� �*�*���+� &�#�'�*>� >� >�� >� >� >r"rQc�@��|jo|j�ko |jjSr4)r�baser �is_even)�irs �r r8z _solve_lambert.<locals>.<lambda>(s"�����?�Q�V�v�-�?�!�%�-�r"c����|jzSr4)r )r�rQs �r r8z _solve_lambert.<locals>.<lambda>*s����1�5��r"r)�force)�deepc�&��g|] }�|jv� |��Srrr�s �r r2z"_solve_lambert.<locals>.<listcomp>\s1���37�37�37� #�!�S�%5�5�5��5�5�5r"rPz:%s does not appear to have a solution in terms of LambertW)rQ)rD�NotImplementedErrorrFrEr � assumptions0�replacer/rUr�ComplexInfinity�NaNrr rvrrrr?rrr:rr rrHrr%r )rcrr$r}�nrhsr=rO�lamcheck�t_indep�t_term�_rhsr`�rr��solnrarb�diff�mainexp�mainterm�mainpowrQs `` @r ryry�s������D4 �4 �4 �4 �4 �l� � ��� �5�5�I�D�#� �%�C�B�B�B�B�t�B�B�B�H� �$�!�#�#�#� �z�(�S�Z�(� � -� -��,� -� -���k�k� @� @� @� @� � � � ���� �:� >�#�'�'�!�*�*� >��h�h�q�!�n�n�G��7�]�F���=�D��=� >�T� >��J�J�q�0�!�%�8�8� >���F� � �c�$�i�i� 7�8�8��.�.�r�1�f�=�=�=�� �Z� >�C� >��S��X�X�T�2�2�2�C��c�(�(�C��w�w�q�z�z� >�c�j� >��3�Y��.�.�r�1�f�=�=�=��l�l�A�v�;�'�'�� �&��4�(�(�(� )� )�C� ���A� �S�1�W�f� %� %�F�A�s� �*�*�a��X� � �C� �D� �7��C��f�-�-�� � 7��z� 7�c�Q�h�h���C���3�s�8�8� 3�V�<�<����� 7�����!�,�,��� 7��� 7�37�37�37�37�',�{�{�3�'7�'7�37�37�37� 7��C�"�5�z�z�C��� �,<�,<�<���"�3��;�/�/�#�c�E�k�2B�2B�B��#�J�t�$4�$4�f�=�=�D�D�$�C�#�I�v�6�6�D� �:��C��f�-�-�� � :��#�w�'�'�C��z� :�c�Q�h�h�� �3�s�8�8�c�#�h�h�+>� ?� ?��H�H����� :�����!�,�,����;���E�k���5�5�7�7���0�0�2�2����N�H��2�I�C��8�}�}�s�3�x�x�/��� �4� 0� 0�&�9�9�� � :��C��f�-�-�� � :�v���!9�9�9��#�w�'�'�C��z� :�c�Q�h�h�� �3�s�8�8�c�#�h�h�+>� ?� ?��H�H����� :�����!�,�,����;���E�k���8�}�}�s�3�x�x�/��� �4� 0� 0�&�9�9�� �%�!�# �"#�#$�%�%� %� ��� � � � �r"T��firstc �����tdd���}|r�t|����}|���}t��}t��}tt|��|�|i��||��||d���}|rC|�|�i} |d�| ��|d�| ��|dfSd S|}|���}t j|�����} g} | D]N} t| ��|�z ����} | j } �| vs�| vrn%| � | ���O��zt | �|fS��fd �}g} |� ���}|� ���|krrt|� �|z��|��} t|� �|z��|��}||�||�zz | z ��} | �| �z|�zz| |fSg} |� ���}|� ���|kr�td��D]�}t|� �|z�|zz��|��} t|� �|z��|��}||�||�zz | z �z ��} | �| �z�z|�zz| |fcS��c����d Sd 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 rWT)�positiveFr�rr#�Nc�p��t|�||����}|j}�|vs�|vrdn|Sr4)rrUr)rc�vrg�new�freer6�ys ��r �okzbivariate_type.<locals>.ok�s>����q�v�v�a��|�|�$�$������T� � �Q�$�Y�Y�t�t�S�8r")r r�as_expr�bivariate_typerUrvr� make_argsrr�append�degreer�coeff_monomial�range)rcr6r�r�rWrn�_x�_y�rv�repsrZr�rLr�r�rSrM�itrys `` r r�r��s-����N �c�D�!�!�!�A� � � ��A�q�M�M�� �I�I�K�K�� �W�W�� �W�W�� �D�����B��2��!7�!7��R�@�@�"�b�PU� V� V� V�� � E���2�q�>�D��a�5�>�>�$�'�'��A�����)=�)=�r�!�u�D� D��� �A� � � � � �A� �=������ %� %�D� �C� �!�!�� �Q�V�V�A�q��s�^�^� $� $���~�� ��9�9��T� � � �E� � � �1� � � � ���s�C��I�q� � �9�9�9�9�9�9� �C� ���� � �A��x�x��{�{�a��� ��!�!�!�Q�$�'�'�� +� +�� ��!�!�!�Q�$�'�'�� +� +���b��A��A�a�C���{�#�#�� �?��Q�3��1��9�c�1�$� $� �C� ���� � �A��x�x��{�{�a����!�H�H� � �D��Q�%�%�a��d�1�a�4�i�0�0�!�4�4�A��Q�%�%�a��d�+�+�Q�/�/�A��"�Q��A��!��G�Q�;�q�=�)�)�C�����s�1�u�q��s�{�C��*�*�*�*��a�D�A�q�q��� � r"r4)*�sympy.core.addr�sympy.core.exprtoolsr�sympy.core.functionrr�sympy.core.powerr�sympy.core.singletonr�sympy.core.sortingr �sympy.core.symbolr �&sympy.functions.elementary.exponentialr r r �(sympy.functions.elementary.miscellaneousr�sympy.polys.polyrootsr�sympy.polys.polytoolsrr�sympy.simplify.simplifyr�sympy.simplify.radsimprr�sympy.solvers.solversrr�sympy.utilities.iterablesrr+r?rGrrryr�rr"r �<module>r�s���������-�-�-�-�-�-�4�4�4�4�4�4�4�4� � � � � � �"�"�"�"�"�"�&�&�&�&�&�&�#�#�#�#�#�#�G�G�G�G�G�G�G�G�G�G�9�9�9�9�9�9�'�'�'�'�'�'�.�.�.�.�.�.�.�.�0�0�0�0�0�0�*�*�*�*�*�*�+�+�+�+�+�+�0�0�0�0�0�0�0�0�*�*�*�*�*�*����8"�"�"�"�J"�"�"�JE�E�E�P]�]�]�@&*�\�\�\�\�\�\�\r"
Memory