K iUdZddlmZddlmZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZddlmZddlmZmZmZmZdd lmZd d d giZiZd ed<iZd ed<iZd ed<ddiZd ed<ddiZd ed<ddiZd ed<ddiZ d ed<iZ!d ed<ddiZ"d ed<iZ#d ed<iZ$d ed<ejKZ&ejKZ'ejKZ(ejKZ)ejKZ*ejKZ+e jKZ,e!jKZ-e"jKZ.e#jKZ/e$jKZ0ddddZ1iZ2ded <id!d"d#d$d%d&d'd(d)d*d+dd,d-d.d/d0d1d2ddd3d4dd5d6d7d8d9d:d;d:dd?d@dAdBdCdDdEdFdG Z3dHdIiZ4dedJ<dKdLdMZ5dedN<iZ6dedO<iZ7dedP<iZ8dedQ<iZ9dedR<iZ:dedS<e&ee1dTfe'ee2dUfe(ee3dVfe)ee4dWfe*ee5dXfe+ee6dYfe,e e7dZfe-e!e8d[fe.e"e9d\fe/e#id]fe0e$e:d^fd_ Z;dqd`ZdfZ?dgZ@dhZAdsdiZBGdjdkZCGdldmeCZDdtdnZEdoZFdpZGy)uz This module provides convenient functions to transform SymPy expressions to lambda functions which can be used to calculate numerical values very fast. ) annotations)AnyN) import_module)sympy_deprecation_warning)doctest_depends_on) is_sequenceiterable NotIterableflatten) filldedent)lambdifynumpy tensorflowzdict[str, Any] MATH_DEFAULT CMATH_DEFAULTMPMATH_DEFAULTIy? NUMPY_DEFAULT SCIPY_DEFAULT CUPY_DEFAULT JAX_DEFAULTTENSORFLOW_DEFAULT TORCH_DEFAULT SYMPY_DEFAULTNUMEXPR_DEFAULTceilelog)ceilingElnzdict[str, str]CMATH_TRANSLATIONSAbsfabs elliptic_kellipk elliptic_fellipf elliptic_eellipe elliptic_piellippir chebyshevtchebyt chebyshevuchebyuassoc_legendrelegenpr jr!ooinfLambertWlambertwMutableDenseMatrixmatrixImmutableDenseMatrix conjugateconjaltzetaeishichisicirfffbetainc) dirichlet_etaEiShiChiSiCiRisingFactorialFallingFactorialbetainc_regularized Heaviside heavisideNUMPY_TRANSLATIONS spherical_jn spherical_yn)jnynSCIPY_TRANSLATIONSCUPY_TRANSLATIONSJAX_TRANSLATIONSTENSORFLOW_TRANSLATIONSTORCH_TRANSLATIONSNUMEXPR_TRANSLATIONS)zfrom math import *)z!import cmath; from cmath import *)zfrom mpmath import *)z=import numpy; from numpy import *; from numpy.linalg import *)z7import scipy; import numpy; from scipy.special import *)z import cupy)z import jax)zimport tensorflow)z import torch)zfrom sympy.functions import *zfrom sympy.matrices import *z2from sympy import Integral, pi, oo, nan, zoo, E, I)zimport_module('numexpr')) mathcmathmpmathrscipycupyjaxrtorchsympynumexprc t|\}}}}||k7r%|r"|j|j |ny|D]\}|j dr)t |}|+|j |j= t|i|Ltd|d|d|jD] \}}||||<d|vr t|d<yy#t$rtd|zwxYw#t$rYhwxYw)a! Creates a global translation dictionary for module. The argument module has to be one of the following strings: "math","cmath" "mpmath", "numpy", "sympy", "tensorflow", "jax". These dictionaries map names of Python functions to their equivalent in other modules. z-'%s' module cannot be used for lambdificationNrzCannot import 'z' with 'z ' commandr#) MODULESKeyError NameErrorclearupdate startswitheval__dict__exec ImportErroritemsabs) modulereload namespacenamespace_default translationsimport_commandsimport_command sympyname translations ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/utilities/lambdify.py_importr|s@FFM GC $lO%%  OO    . / )O  $ $_ 5.)F!  1 ^R36kDrtj|+d?dd@z}+| },dAj{|*|+|,dBj}|!C|)_C|)S#t $r' t ddg}n#t $rgd}YnwxYwY2wxYwcc}}w#t $r|d1|"d2|}$tg|$i| YwxYw)DakTConvert a SymPy expression into a function that allows for fast numeric evaluation. .. warning:: This function uses ``exec``, and thus should not be used on unsanitized input. .. deprecated:: 1.7 Passing a set for the *args* parameter is deprecated as sets are unordered. Use an ordered iterable such as a list or tuple. Explanation =========== For example, to convert the SymPy expression ``sin(x) + cos(x)`` to an equivalent NumPy function that numerically evaluates it: >>> from sympy import sin, cos, symbols, lambdify >>> import numpy as np >>> x = symbols('x') >>> expr = sin(x) + cos(x) >>> expr sin(x) + cos(x) >>> f = lambdify(x, expr, 'numpy') >>> a = np.array([1, 2]) >>> f(a) [1.38177329 0.49315059] The primary purpose of this function is to provide a bridge from SymPy expressions to numerical libraries such as NumPy, SciPy, NumExpr, mpmath, and tensorflow. In general, SymPy functions do not work with objects from other libraries, such as NumPy arrays, and functions from numeric libraries like NumPy or mpmath do not work on SymPy expressions. ``lambdify`` bridges the two by converting a SymPy expression to an equivalent numeric function. The basic workflow with ``lambdify`` is to first create a SymPy expression representing whatever mathematical function you wish to evaluate. This should be done using only SymPy functions and expressions. Then, use ``lambdify`` to convert this to an equivalent function for numerical evaluation. For instance, above we created ``expr`` using the SymPy symbol ``x`` and SymPy functions ``sin`` and ``cos``, then converted it to an equivalent NumPy function ``f``, and called it on a NumPy array ``a``. Parameters ========== args : List[Symbol] A variable or a list of variables whose nesting represents the nesting of the arguments that will be passed to the function. Variables can be symbols, undefined functions, or matrix symbols. >>> from sympy import Eq >>> from sympy.abc import x, y, z The list of variables should match the structure of how the arguments will be passed to the function. Simply enclose the parameters as they will be passed in a list. To call a function like ``f(x)`` then ``[x]`` should be the first argument to ``lambdify``; for this case a single ``x`` can also be used: >>> f = lambdify(x, x + 1) >>> f(1) 2 >>> f = lambdify([x], x + 1) >>> f(1) 2 To call a function like ``f(x, y)`` then ``[x, y]`` will be the first argument of the ``lambdify``: >>> f = lambdify([x, y], x + y) >>> f(1, 1) 2 To call a function with a single 3-element tuple like ``f((x, y, z))`` then ``[(x, y, z)]`` will be the first argument of the ``lambdify``: >>> f = lambdify([(x, y, z)], Eq(z**2, x**2 + y**2)) >>> f((3, 4, 5)) True If two args will be passed and the first is a scalar but the second is a tuple with two arguments then the items in the list should match that structure: >>> f = lambdify([x, (y, z)], x + y + z) >>> f(1, (2, 3)) 6 expr : Expr An expression, list of expressions, or matrix to be evaluated. Lists may be nested. If the expression is a list, the output will also be a list. >>> f = lambdify(x, [x, [x + 1, x + 2]]) >>> f(1) [1, [2, 3]] If it is a matrix, an array will be returned (for the NumPy module). >>> from sympy import Matrix >>> f = lambdify(x, Matrix([x, x + 1])) >>> f(1) [[1] [2]] Note that the argument order here (variables then expression) is used to emulate the Python ``lambda`` keyword. ``lambdify(x, expr)`` works (roughly) like ``lambda x: expr`` (see :ref:`lambdify-how-it-works` below). modules : str, optional Specifies the numeric library to use. If not specified, *modules* defaults to: - ``["scipy", "numpy"]`` if SciPy is installed - ``["numpy"]`` if only NumPy is installed - ``["math","cmath", "mpmath", "sympy"]`` if neither is installed. That is, SymPy functions are replaced as far as possible by either ``scipy`` or ``numpy`` functions if available, and Python's standard library ``math`` and ``cmath``, or ``mpmath`` functions otherwise. *modules* can be one of the following types: - The strings ``"math"``, ``"cmath"``, ``"mpmath"``, ``"numpy"``, ``"numexpr"``, ``"scipy"``, ``"sympy"``, or ``"tensorflow"`` or ``"jax"``. This uses the corresponding printer and namespace mapping for that module. - A module (e.g., ``math``). This uses the global namespace of the module. If the module is one of the above known modules, it will also use the corresponding printer and namespace mapping (i.e., ``modules=numpy`` is equivalent to ``modules="numpy"``). - A dictionary that maps names of SymPy functions to arbitrary functions (e.g., ``{'sin': custom_sin}``). - A list that contains a mix of the arguments above, with higher priority given to entries appearing first (e.g., to use the NumPy module but override the ``sin`` function with a custom version, you can use ``[{'sin': custom_sin}, 'numpy']``). dummify : bool, optional Whether or not the variables in the provided expression that are not valid Python identifiers are substituted with dummy symbols. This allows for undefined functions like ``Function('f')(t)`` to be supplied as arguments. By default, the variables are only dummified if they are not valid Python identifiers. Set ``dummify=True`` to replace all arguments with dummy symbols (if ``args`` is not a string) - for example, to ensure that the arguments do not redefine any built-in names. cse : bool, or callable, optional Large expressions can be computed more efficiently when common subexpressions are identified and precomputed before being used multiple time. Finding the subexpressions will make creation of the 'lambdify' function slower, however. When ``True``, ``sympy.simplify.cse`` is used, otherwise (the default) the user may pass a function matching the ``cse`` signature. docstring_limit : int or None When lambdifying large expressions, a significant proportion of the time spent inside ``lambdify`` is spent producing a string representation of the expression for use in the automatically generated docstring of the returned function. For expressions containing hundreds or more nodes the resulting docstring often becomes so long and dense that it is difficult to read. To reduce the runtime of lambdify, the rendering of the full expression inside the docstring can be disabled. When ``None``, the full expression is rendered in the docstring. When ``0`` or a negative ``int``, an ellipsis is rendering in the docstring instead of the expression. When a strictly positive ``int``, if the number of nodes in the expression exceeds ``docstring_limit`` an ellipsis is rendered in the docstring, otherwise a string representation of the expression is rendered as normal. The default is ``1000``. Examples ======== >>> from sympy.utilities.lambdify import implemented_function >>> from sympy import sqrt, sin, Matrix >>> from sympy import Function >>> from sympy.abc import w, x, y, z >>> f = lambdify(x, x**2) >>> f(2) 4 >>> f = lambdify((x, y, z), [z, y, x]) >>> f(1,2,3) [3, 2, 1] >>> f = lambdify(x, sqrt(x)) >>> f(4) 2.0 >>> f = lambdify((x, y), sin(x*y)**2) >>> f(0, 5) 0.0 >>> row = lambdify((x, y), Matrix((x, x + y)).T, modules='sympy') >>> row(1, 2) Matrix([[1, 3]]) ``lambdify`` can be used to translate SymPy expressions into mpmath functions. This may be preferable to using ``evalf`` (which uses mpmath on the backend) in some cases. >>> f = lambdify(x, sin(x), 'mpmath') >>> f(1) 0.8414709848078965 Tuple arguments are handled and the lambdified function should be called with the same type of arguments as were used to create the function: >>> f = lambdify((x, (y, z)), x + y) >>> f(1, (2, 4)) 3 The ``flatten`` function can be used to always work with flattened arguments: >>> from sympy.utilities.iterables import flatten >>> args = w, (x, (y, z)) >>> vals = 1, (2, (3, 4)) >>> f = lambdify(flatten(args), w + x + y + z) >>> f(*flatten(vals)) 10 Functions present in ``expr`` can also carry their own numerical implementations, in a callable attached to the ``_imp_`` attribute. This can be used with undefined functions using the ``implemented_function`` factory: >>> f = implemented_function(Function('f'), lambda x: x+1) >>> func = lambdify(x, f(x)) >>> func(4) 5 ``lambdify`` always prefers ``_imp_`` implementations to implementations in other namespaces, unless the ``use_imps`` input parameter is False. Usage with Tensorflow: >>> import tensorflow as tf >>> from sympy import Max, sin, lambdify >>> from sympy.abc import x >>> f = Max(x, sin(x)) >>> func = lambdify(x, f, 'tensorflow') After tensorflow v2, eager execution is enabled by default. If you want to get the compatible result across tensorflow v1 and v2 as same as this tutorial, run this line. >>> tf.compat.v1.enable_eager_execution() If you have eager execution enabled, you can get the result out immediately as you can use numpy. If you pass tensorflow objects, you may get an ``EagerTensor`` object instead of value. >>> result = func(tf.constant(1.0)) >>> print(result) tf.Tensor(1.0, shape=(), dtype=float32) >>> print(result.__class__) You can use ``.numpy()`` to get the numpy value of the tensor. >>> result.numpy() 1.0 >>> var = tf.Variable(2.0) >>> result = func(var) # also works for tf.Variable and tf.Placeholder >>> result.numpy() 2.0 And it works with any shape array. >>> tensor = tf.constant([[1.0, 2.0], [3.0, 4.0]]) >>> result = func(tensor) >>> result.numpy() [[1. 2.] [3. 4.]] Notes ===== - For functions involving large array calculations, numexpr can provide a significant speedup over numpy. Please note that the available functions for numexpr are more limited than numpy but can be expanded with ``implemented_function`` and user defined subclasses of Function. If specified, numexpr may be the only option in modules. The official list of numexpr functions can be found at: https://numexpr.readthedocs.io/en/latest/user_guide.html#supported-functions - In the above examples, the generated functions can accept scalar values or numpy arrays as arguments. However, in some cases the generated function relies on the input being a numpy array: >>> import numpy >>> from sympy import Piecewise >>> from sympy.testing.pytest import ignore_warnings >>> f = lambdify(x, Piecewise((x, x <= 1), (1/x, x > 1)), "numpy") >>> with ignore_warnings(RuntimeWarning): ... f(numpy.array([-1, 0, 1, 2])) [-1. 0. 1. 0.5] >>> f(0) Traceback (most recent call last): ... ZeroDivisionError: division by zero In such cases, the input should be wrapped in a numpy array: >>> with ignore_warnings(RuntimeWarning): ... float(f(numpy.array([0]))) 0.0 Or if numpy functionality is not required another module can be used: >>> f = lambdify(x, Piecewise((x, x <= 1), (1/x, x > 1)), "math") >>> f(0) 0 .. _lambdify-how-it-works: How it works ============ When using this function, it helps a great deal to have an idea of what it is doing. At its core, lambdify is nothing more than a namespace translation, on top of a special printer that makes some corner cases work properly. To understand lambdify, first we must properly understand how Python namespaces work. Say we had two files. One called ``sin_cos_sympy.py``, with .. code:: python # sin_cos_sympy.py from sympy.functions.elementary.trigonometric import (cos, sin) def sin_cos(x): return sin(x) + cos(x) and one called ``sin_cos_numpy.py`` with .. code:: python # sin_cos_numpy.py from numpy import sin, cos def sin_cos(x): return sin(x) + cos(x) The two files define an identical function ``sin_cos``. However, in the first file, ``sin`` and ``cos`` are defined as the SymPy ``sin`` and ``cos``. In the second, they are defined as the NumPy versions. If we were to import the first file and use the ``sin_cos`` function, we would get something like >>> from sin_cos_sympy import sin_cos # doctest: +SKIP >>> sin_cos(1) # doctest: +SKIP cos(1) + sin(1) On the other hand, if we imported ``sin_cos`` from the second file, we would get >>> from sin_cos_numpy import sin_cos # doctest: +SKIP >>> sin_cos(1) # doctest: +SKIP 1.38177329068 In the first case we got a symbolic output, because it used the symbolic ``sin`` and ``cos`` functions from SymPy. In the second, we got a numeric result, because ``sin_cos`` used the numeric ``sin`` and ``cos`` functions from NumPy. But notice that the versions of ``sin`` and ``cos`` that were used was not inherent to the ``sin_cos`` function definition. Both ``sin_cos`` definitions are exactly the same. Rather, it was based on the names defined at the module where the ``sin_cos`` function was defined. The key point here is that when function in Python references a name that is not defined in the function, that name is looked up in the "global" namespace of the module where that function is defined. Now, in Python, we can emulate this behavior without actually writing a file to disk using the ``exec`` function. ``exec`` takes a string containing a block of Python code, and a dictionary that should contain the global variables of the module. It then executes the code "in" that dictionary, as if it were the module globals. The following is equivalent to the ``sin_cos`` defined in ``sin_cos_sympy.py``: >>> import sympy >>> module_dictionary = {'sin': sympy.sin, 'cos': sympy.cos} >>> exec(''' ... def sin_cos(x): ... return sin(x) + cos(x) ... ''', module_dictionary) >>> sin_cos = module_dictionary['sin_cos'] >>> sin_cos(1) cos(1) + sin(1) and similarly with ``sin_cos_numpy``: >>> import numpy >>> module_dictionary = {'sin': numpy.sin, 'cos': numpy.cos} >>> exec(''' ... def sin_cos(x): ... return sin(x) + cos(x) ... ''', module_dictionary) >>> sin_cos = module_dictionary['sin_cos'] >>> sin_cos(1) 1.38177329068 So now we can get an idea of how ``lambdify`` works. The name "lambdify" comes from the fact that we can think of something like ``lambdify(x, sin(x) + cos(x), 'numpy')`` as ``lambda x: sin(x) + cos(x)``, where ``sin`` and ``cos`` come from the ``numpy`` namespace. This is also why the symbols argument is first in ``lambdify``, as opposed to most SymPy functions where it comes after the expression: to better mimic the ``lambda`` keyword. ``lambdify`` takes the input expression (like ``sin(x) + cos(x)``) and 1. Converts it to a string 2. Creates a module globals dictionary based on the modules that are passed in (by default, it uses the NumPy module) 3. Creates the string ``"def func({vars}): return {expr}"``, where ``{vars}`` is the list of variables separated by commas, and ``{expr}`` is the string created in step 1., then ``exec``s that string with the module globals namespace and returns ``func``. In fact, functions returned by ``lambdify`` support inspection. So you can see exactly how they are defined by using ``inspect.getsource``, or ``??`` if you are using IPython or the Jupyter notebook. >>> f = lambdify(x, sin(x) + cos(x)) >>> import inspect >>> print(inspect.getsource(f)) def _lambdifygenerated(x): return sin(x) + cos(x) This shows us the source code of the function, but not the namespace it was defined in. We can inspect that by looking at the ``__globals__`` attribute of ``f``: >>> f.__globals__['sin'] >>> f.__globals__['cos'] >>> f.__globals__['sin'] is numpy.sin True This shows us that ``sin`` and ``cos`` in the namespace of ``f`` will be ``numpy.sin`` and ``numpy.cos``. Note that there are some convenience layers in each of these steps, but at the core, this is how ``lambdify`` works. Step 1 is done using the ``LambdaPrinter`` printers defined in the printing module (see :mod:`sympy.printing.lambdarepr`). This allows different SymPy expressions to define how they should be converted to a string for different modules. You can change which printer ``lambdify`` uses by passing a custom printer in to the ``printer`` argument. Step 2 is augmented by certain translations. There are default translations for each module, but you can provide your own by passing a list to the ``modules`` argument. For instance, >>> def mysin(x): ... print('taking the sin of', x) ... return numpy.sin(x) ... >>> f = lambdify(x, sin(x), [{'sin': mysin}, 'numpy']) >>> f(1) taking the sin of 1 0.8414709848078965 The globals dictionary is generated from the list by merging the dictionary ``{'sin': mysin}`` and the module dictionary for NumPy. The merging is done so that earlier items take precedence, which is why ``mysin`` is used above instead of ``numpy.sin``. If you want to modify the way ``lambdify`` works for a given function, it is usually easiest to do so by modifying the globals dictionary as such. In more complicated cases, it may be necessary to create and pass in a custom printer. Finally, step 3 is augmented with certain convenience operations, such as the addition of a docstring. Understanding how ``lambdify`` works can make it easier to avoid certain gotchas when using it. For instance, a common mistake is to create a lambdified function for one module (say, NumPy), and pass it objects from another (say, a SymPy expression). For instance, say we create >>> from sympy.abc import x >>> f = lambdify(x, x + 1, 'numpy') Now if we pass in a NumPy array, we get that array plus 1 >>> import numpy >>> a = numpy.array([1, 2]) >>> f(a) [2 3] But what happens if you make the mistake of passing in a SymPy expression instead of a NumPy array: >>> f(x + 1) x + 2 This worked, but it was only by accident. Now take a different lambdified function: >>> from sympy import sin >>> g = lambdify(x, x + sin(x), 'numpy') This works as expected on NumPy arrays: >>> g(a) [1.84147098 2.90929743] But if we try to pass in a SymPy expression, it fails >>> g(x + 1) Traceback (most recent call last): ... TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sin method Now, let's look at what happened. The reason this fails is that ``g`` calls ``numpy.sin`` on the input expression, and ``numpy.sin`` does not know how to operate on a SymPy object. **As a general rule, NumPy functions do not know how to operate on SymPy expressions, and SymPy functions do not know how to operate on NumPy arrays. This is why lambdify exists: to provide a bridge between SymPy and NumPy.** However, why is it that ``f`` did work? That's because ``f`` does not call any functions, it only adds 1. So the resulting function that is created, ``def _lambdifygenerated(x): return x + 1`` does not depend on the globals namespace it is defined in. Thus it works, but only by accident. A future version of ``lambdify`` may remove this behavior. Be aware that certain implementation details described here may change in future versions of SymPy. The API of passing in custom modules and printers will not change, but the details of how a lambda function is created may change. However, the basic idea will remain the same, and understanding it will be helpful to understanding the behavior of lambdify. **In general: you should create lambdified functions for one module (say, NumPy), and only pass it input types that are compatible with that module (say, NumPy arrays).** Remember that by default, if the ``module`` argument is not provided, ``lambdify`` creates functions using the NumPy and SciPy namespaces. r)SymbolExprNr_r)r\r^rc__iter__rdr}z*numexpr must be the only item in 'modules'atomsr^) MpmathPrinter) SciPyPrinter) NumPyPrinterr`) CuPyPrinterra) JaxPrinter)NumExprPrinterr)TensorflowPrinterrb) TorchPrinterrc) SymPyPrinterr]) CmathPrinter)PythonCodePrinterFT)fully_qualified_modulesinlineallow_unknown_functionsuser_functionsz Passing the function arguments to lambdify() as a set is deprecated. This leads to unpredictable results since sets are unordered. Instead, use a list or tuple for the function arguments. z1.6.3z!deprecated-lambdify-arguments-set)deprecated_since_versionactive_deprecations_targetnamearg__lambdifygenerated)cse)listcsesmodule_importszfrom z import z = .)builtinsrangezrncfd}|S)NcNtjvrtj=yyN) linecachecache)filenamesr{_cleanupz5lambdify..cleanup_linecache.._cleanups 9??*OOH-+r)rrs` r{cleanup_linecachez#lambdify..cleanup_linecaches .rzfunc({}), c32K|]}t|ywrstr).0is r{ zlambdify..s%<c!f%<z )subsequent_indentzEEXPRESSION REDACTED DUE TO LENGTH, (see lambdify's `docstring_limit`)zFSOURCE CODE REDACTED DUE TO LENGTH, (see lambdify's `docstring_limit`)NKz...zqCreated with lambdify. Signature: {sig} Expression: {expr} Source code: {src} Imported modules: {imp_mods} )sigexprsrcimp_mods)Dsympy.core.symbolrsympy.core.exprrr|roappend_imp_namespace isinstancedictrhasattr_module_presentlen TypeErrorr_get_namespacerjrsympy.printing.pycodersympy.printing.numpyrrrrsympy.printing.lambdareprrsympy.printing.tensorflowrsympy.printing.pytorchrrrrsetrinspect currentframef_backf_localsrp enumerater_TensorflowEvaluatorPrinter_EvaluatorPrintersympy.simplify.cse_mainrcallabledoprintgetattrrnrr_lambdify_generated_countercompile splitlinesrrweakreffinalizeformatjointextwrapfill_too_large_for_docstringwrap__doc__)-argsrrprinteruse_impsdummifyrdocstring_limitrr namespacesrtmbufsymstermPrinterrk iterable_argsnamescallers_local_varsnvarvar_namevar_val name_listfuncname funcprinter_cser_exprfuncstr imp_mod_linesmodkeysr! funclocalsrcrfuncrexpr_strsrc_strs- r{r r s~)$ ) G (GJ../'D#;'ww /K'" 9g .3w$!  &-'J  *$5TG4M E!--/66??EEGM* .3 3  LL "D"KKM ) T )A !,/3,R+$$R( ) )59:J'*EEH1$6*AIz"!$WtW5G5G5Mx XIOOH h D T,X67   DII%U?cX||vry|D] }t|ds|j|k(s yy)NT__name__F)rr)modnamemodlistrs r{rrs9'  1j !ajjG&; rct|trt|t|dSt|tr|St |dr |j Std|z)z; This is used by _lambdify to parse its arguments. rrmz>Argument must be either a string, dict or module but it is: %s)rrr|rfrrrmr)rs r{rrsX!S qz!} At  J zzX[\\]]rcbddlm}ddlm}t |||fr|St |rht |t rd\}}n2t |trd\}}|sytdt|d||djfd |Dz|zSt |tr|S|S) zFunctions in lambdify accept both SymPy types and non-SymPy types such as python lists and tuples. This method ensures that we only call the doprint method of the printer with SymPy types (so that the printer safely can use SymPy-methods).r MatrixBaseBasic)[])(z,)z()zunhandled type: rc36K|]}t|ywr)_recursive_to_string)rrrs r{rz'_recursive_to_string..sMA3GQ?M) sympy.matrices.matrixbasersympy.core.basicrrr rtupleNotImplementedErrortyperr)rargrrleftrights` r{rrs5&#z*+s| # c4 "KD% U ##KD%%$s)S&QR RTYYMMMMPUUU C  s|rc ddlm ddlm ddlmmddlmm ddl m 9tjr}n'tjrfd}n fd }ndd lm} fd  fd  fd fd|!t# fd|r|n|gD}|rt#fd|Drt%t'|Dcgc]}t)t)|}}dj+|Dcgc]0}||ddj+|ddDcgc]}d|z c}z2c}}} t-t/|||} ddj+|d| d| dSi} |r || }n6t1|t(rn%t3| rdj+d|D}|rt1|t(rn || }t5||}d|d|dScc}wcc}wcc}}w)aa Returns a string that can be evaluated to a lambda function. Examples ======== >>> from sympy.abc import x, y, z >>> from sympy.utilities.lambdify import lambdastr >>> lambdastr(x, x**2) 'lambda x: (x**2)' >>> lambdastr((x,y,z), [z,y,x]) 'lambda x,y,z: ([z, y, x])' Although tuples may not appear as arguments to lambda in Python 3, lambdastr will create a lambda function that will unpack the original arguments so that nested arguments can be handled: >>> lambdastr((x, (y, z)), x + y) 'lambda _0,_1: (lambda x,y,z: (x + y))(_0,_1[0],_1[1])' rDeferredVectorr DerivativeFunction)DummyrsympifyNc0j|Srrrrs r{zlambdastr..s')*;*;D*Arc&j|Srr)r*s r{r+zlambdastr..s'//$*?r) lambdareprc dt|tr|St|r t|St|r8t|Dcgc] } || c}}dj d|DSt|fr%}|j ||it|St|Scc}w)N,c32K|]}t|ywrrras r{rz.lambdastr..sub_args..s4qCF4r)rrr r rrj) r dummies_dictr2dummiesr!r#r%r$rsub_argss r{r5zlambdastr..sub_argss dC K n -t9  d^$GQx<8GHG884G44 4$6: >?'##TG$457|#4y HsB-c|}t|r|j|}|St|tr|Dcgc] }|| }}|Scc}wr)rxreplacer)rr3r2rsub_exprr's r{r8zlambdastr..sub_expr's[t} dE "==.D d #7;.isiter2sC#EFFrc3pKd}|D]*}|r|D] }|f|z n|f|dz },ywNrr}r)r relndeep flat_indexesr=s r{rBzlambdastr..flat_indexes5sT  Bbz)"-'E$,&'d FA s36c3^K|]$}t|xr|j&ywr)rr)rr2rr#r$s r{rzlambdastr..Bs8/./!E** GGHj )*/s*-c3.K|] }|ywrr)rrr=s r{rzlambdastr..Fs4!F1I4sr/r}z[%s])rrzlambda z: (z)()r:c32K|]}t|ywrrr1s r{rzlambdastr..Xs1qCF1r)sympy.matricesr!rrsympy.core.functionr#r$rr%rsympy.core.sympifyr'r isfunctionisclassrr-anyrrrr lambdastrr rr r)rrrrr-rdum_argsindr indexed_argslstrr3rr!r#r%r$rrBr=r5r8r's ` @@@@@@@@@@@r{rNrNs,.&:1*   g & Jw'A ?  9!!"G /4LDtf//d|4t4405c$i0@A1Cc!f &AAxx+D1!3 SV rwwCG'Dq 'DE E!34 gwO(+(:D,OOLl+ dC  dN 3881D11D dC D,/D  D 1D $d ++5B(E!3s!!G?H 8 H H H cPeZdZd dZdddZedZd dZdZd Z d Z d Z y)rNc||_ddlm}||}tj|r||_n-tj |r|}|j|_|j|_y)Nr) LambdaPrinter) _dummifyrrUrrK _exprreprrLr_argrepr)selfrrrUs r{__init__z_EvaluatorPrinter.__init__ds^  < ?#oG   g &$DNw'!)$__DN&// rrrc Lddlm}g}t|s|g}|rSt|}t |\}}|gt|z} |j || |\} } | d| dd}}t ||}n|j ||\} }g} g} | D]h} t| rJ| j |j|| j|j| | dX| j | jdj|dj| }|j|j| |j| |D]v\}}|0|j d j|j|8|j d j|j||j|xg}|j|| }|D]D\}}|j d j|j||j|Ft|j|}d |vrd j|}|j dj||g}|j|Dcgc]}d|z c}d j|d zScc}w)zC Returns the function definition code as a string. rr%rr}Nrz def {}({}):rzdel {}{} = {})outrz({})z return {}z )rr%r rzip _preprocessrrXextend_print_unpackingrr_print_funcargwrappingrW _handle_Subsr)rYrrrrr%funcbodysubvarssubexprsexprsargstrsfuncargs unpackingsargstrfuncsigsrsubs_assignmentslhsrhsstr_expr funclineslines r{rz_EvaluatorPrinter.doprint~sz ,~6D :D #T GXFT(^+E!--dE-ENGU"1XuQRy(Dw)D ,,T48MGT  (F eg 67!!$"7"7 "MN'  ( &&x81DE 33H=> # XDAqyq0A BC 0 01BDNNSTDU VW  X  +; <( XHC OOI,,T^^C-@$..QTBUV W X(= 8 }}X.H **845I H=D&4-=>yy#d**>s: J!cvt|txr(|jxrtj| Sr)rr isidentifierkeyword iskeyword)clsidents r{_is_safe_identz _EvaluatorPrinter._is_safe_idents7%%1%*<*<*>1))%00 1rcddlm}ddlm}ddlm}m}ddlmm } ddl m } ddl m } |jxstfdt!|D} d gt#|z} ifd }t%t'|t)|t+t#|D]"\}}t-|r|j/|| \}}nt1|| r t3|}nt1||r|j4rtt3|}| s|j7|s}t1|| r| |j8|d }|j;|}||||j=|}nO| st1|||fr4}|j;|}||||j=|}n t3|}|| |<%| |fS)zPreprocess args, expr to replace arguments that do not map to valid Python identifiers. Returns string form of args, and updated expr. rr)orderedr")r%uniquely_named_symbolr rc36K|]}t|ywr)r)rrr%s r{rz0_EvaluatorPrinter._preprocess..s'='*JsE "'=rNcT||<D]\}}|j||i}||<yr)r7)rdummyreplsub _dummies_dictrs r{update_dummiesz5_EvaluatorPrinter._preprocess..update_dummiess<!&M# ! + cllC;/%* c" +r)rrc d|zS)N_r)rns r{r+z/_EvaluatorPrinter._preprocess..s sQwr)modify)rrsympy.core.sortingr}rIr#r$rr%r~rHr!rrrVrMr rreversedrr_rr r`rr is_symbolr{rrX_subexpr)rYrrrrrr}r#r$r~r!rrrirrrrnrr%s `` @r{r`z_EvaluatorPrinter._preprocesss +.>B1( --=3'=.5dm'=$=&T"  M + tGCeCI6F,G$HIJ FC}**34}*]4C0HC'CMMH$"5"5a"8!GE!$- 5!JJ5F!H e,A"3.==}=DJsXz,BCMM%(sE*}}T=9HGAJ- .}rcvddlm}ddlm|}t |dd}| |}|St ||r |St |t r}|jDcgc]}j|}}|jDcgc]}j|}}t t||}|St |trtfd|D}|St |tr%|Dcgc]}j|}}|Scc}wcc}wcc}w)Nrr r&r7c3NK|]}j|ywr)r)rr2r3rYr's r{rz-_EvaluatorPrinter._subexpr..s ST]]71:|DSs"%) rHr!rJr'rrrrrvaluesr_rr) rYrr3r!r7r2rvr's ` ` @r{rz_EvaluatorPrinter._subexprs$1.t}4T2  L)D $/ D$'FJiikRT]]71:|<RRFJkkmTT]]71:|<TTC1I  D%(SdSS D$'IMNA gaj,?NN ST Os"D,D1 D6cgS)zGenerate argument wrapping code. args is the argument list of the generated function (strings). Return value is a list of lines of code that will be inserted at the beginning of the function definition. r)rYrs r{rcz(_EvaluatorPrinter._print_funcargwrapping s  rc@fddj||gS)zGenerate argument unpacking code. arg is the function argument to be unpacked (a string), and unpackto is a list or nested lists of the variable names (strings) to unpack to. cVdjdjfd|DS)Nz[{}]rc3HK|]}t|r|n|ywrr )rval unpack_lhss r{rzI_EvaluatorPrinter._print_unpacking..unpack_lhs..s'+N>A8C= 3c9+Ns")rr)lvaluesrs r{rz6_EvaluatorPrinter._print_unpacking..unpack_lhss1==+NEL+N"NO Orr])r)rYunpacktorrs @r{rbz"_EvaluatorPrinter._print_unpackings& O  H!5s;<._replace*sSD 51 )S!S  E3<( );;t$ $r) rrrIrrr%rrrreplacer rrd) rYrr^rrrrrr%s ` @r{rdz_EvaluatorPrinter._handle_Subs#sx*,+8 % dUJ/ 0<<h/D d^4:$GQt00C8GHD HsA?)NF)rN) r __module__ __qualname__rZr classmethodr{r`rrcrbrdrrr{rrcs@0457<+|114l* =rrceZdZdZy)rcfddjfd|D}djdjt||gS)zGenerate argument unpacking code. This method is used when the input value is not iterable, but can be indexed (see issue #14655). c3vKd}|D]-}t|r|D] }|f|z n|f|dz }/ywr?r)elemsrr@rArBs r{rBzB_TensorflowEvaluatorPrinter._print_unpacking..flat_indexes@sTA B.Ls5B$'%OOFDIIc#sm4LMBs9<z [{}] = [{}])rrr )rYrrindexedrBs ` @r{rbz,_TensorflowEvaluatorPrinter._print_unpacking9sS ))B+7+@BB$$TYYww/?%@'JKKrN)rrrrbrrr{rr8sLrrcddlm}|i}t|r|D]}t|||St |t r2|j D]\}}t||t|||St|dd}t ||rDt|dd}|5|jj}||vr|||k7rtd|z|||<t|dr|jD]}t|||S)ao Return namespace dict with function implementations We need to search for functions in anything that can be thrown at us - that is - anything that could be passed as ``expr``. Examples include SymPy expressions, as well as tuples, lists and dicts that may contain SymPy expressions. Parameters ---------- expr : object Something passed to lambdify, that will generate valid code from ``str(expr)``. namespace : None or mapping Namespace to fill. None results in new empty dict Returns ------- namespace : dict dict with keys of implemented function names within ``expr`` and corresponding values being the numerical implementation of function Examples ======== >>> from sympy.abc import x >>> from sympy.utilities.lambdify import implemented_function, _imp_namespace >>> from sympy import Function >>> f = implemented_function(Function('f'), lambda x: x+1) >>> g = implemented_function(Function('g'), lambda x: x*10) >>> namespace = _imp_namespace(f(g(x))) >>> sorted(namespace.keys()) ['f', 'g'] r) FunctionClassNr_imp_z4We found more than one implementation with name "%s"r) rIrrrrrrprrr ValueErrorrr) rrtrrkeyrrimprs r{rrQsH2 4 +C 3 * + D$   +HC 3 * 3 * + 4 &D$ &dGT* ?99%%Dy Yt_%; "(*."/00"IdOtV99 +C 3 * + rcddlm}i}t||r|j}|j}t|t r||fdt |i|}|St||sttd|S)a Add numerical ``implementation`` to function ``symfunc``. ``symfunc`` can be an ``UndefinedFunction`` instance, or a name string. In the latter case we create an ``UndefinedFunction`` instance with that name. Be aware that this is a quick workaround, not a general method to create special symbolic functions. If you want to create a symbolic function to be used by all the machinery of SymPy you should subclass the ``Function`` class. Parameters ---------- symfunc : ``str`` or ``UndefinedFunction`` instance If ``str``, then create new ``UndefinedFunction`` with this as name. If ``symfunc`` is an Undefined function, create a new function with the same name and the implemented function attached. implementation : callable numerical implementation to be called by ``evalf()`` or ``lambdify`` Returns ------- afunc : sympy.FunctionClass instance function with attached implementation Examples ======== >>> from sympy.abc import x >>> from sympy.utilities.lambdify import implemented_function >>> from sympy import lambdify >>> f = implemented_function('f', lambda x: x+1) >>> lam_f = lambdify(x, f(x)) >>> lam_f(4) 5 r)UndefinedFunctionrz\ symfunc should be either a string or an UndefinedFunction instance.) rIrr_kwargsrr staticmethodrr )symfuncimplementationrkwargss r{implemented_functionrsL6 F',-""'3$ C'7C;AC N !2 3%./0 0 NrcLddlm}|yd}||D]}|dz }||kDsyy)a Decide whether an ``Expr`` is too large to be fully rendered in a ``lambdify`` docstring. This is a fast alternative to ``count_ops``, which can become prohibitively slow for large expressions, because in this instance we only care whether ``limit`` is exceeded rather than counting the exact number of nodes in the expression. Parameters ========== expr : ``Expr``, (nested) ``list`` of ``Expr``, or ``Matrix`` The same objects that can be passed to the ``expr`` argument of ``lambdify``. limit : ``int`` or ``None`` The threshold above which an expression contains too many nodes to be usefully rendered in the docstring. If ``None`` then there is no limit. Returns ======= bool ``True`` if the number of nodes in the expression exceeds the limit, ``False`` otherwise. Examples ======== >>> from sympy.abc import x, y, z >>> from sympy.utilities.lambdify import _too_large_for_docstring >>> expr = x >>> _too_large_for_docstring(expr, None) False >>> _too_large_for_docstring(expr, 100) False >>> _too_large_for_docstring(expr, 1) False >>> _too_large_for_docstring(expr, 0) True >>> _too_large_for_docstring(expr, -1) True Does this split it? >>> expr = [x, y, z] >>> _too_large_for_docstring(expr, None) False >>> _too_large_for_docstring(expr, 100) False >>> _too_large_for_docstring(expr, 1) True >>> _too_large_for_docstring(expr, 0) True >>> _too_large_for_docstring(expr, -1) True >>> expr = [x, [y], z, [[x+y], [x*y*z, [x+y+z]]]] >>> _too_large_for_docstring(expr, None) False >>> _too_large_for_docstring(expr, 100) False >>> _too_large_for_docstring(expr, 1) True >>> _too_large_for_docstring(expr, 0) True >>> _too_large_for_docstring(expr, -1) True >>> expr = ((x + y + z)**5).expand() >>> _too_large_for_docstring(expr, None) False >>> _too_large_for_docstring(expr, 100) True >>> _too_large_for_docstring(expr, 1) True >>> _too_large_for_docstring(expr, 0) True >>> _too_large_for_docstring(expr, -1) True >>> from sympy import Matrix >>> expr = Matrix([[(x + y + z), ((x + y + z)**2).expand(), ... ((x + y + z)**3).expand(), ((x + y + z)**4).expand()]]) >>> _too_large_for_docstring(expr, None) False >>> _too_large_for_docstring(expr, 1000) False >>> _too_large_for_docstring(expr, 100) True >>> _too_large_for_docstring(expr, 1) True >>> _too_large_for_docstring(expr, 0) True >>> _too_large_for_docstring(expr, -1) True r)postorder_traversalFr}T)sympy.core.traversalr)rlimitrrrs r{rrsAB9 } A  & Q u9 r)F)NNTFFi)NNr)Hr __future__rtypingrrrrwrrrsympy.externalrsympy.utilities.exceptionsrsympy.utilities.decoratorrsympy.utilities.iterablesrr r r sympy.utilities.miscr __doctest_requires__r__annotations__rrrrrrrrrrcopyMATHCMATHMPMATHNUMPYSCIPYCUPYJAX TENSORFLOWTORCHSYMPYNUMEXPRMATH_TRANSLATIONSr"MPMATH_TRANSLATIONSrQrVrWrXrYrZr[rfr|rr rrrrNrrrrrrrr{rs  #)@8+&'>? " n!! }!!##!$b ~)!$b ~) #Ry n("Bi ^'%'N'!$b ~) " ~""$$       $ $ &        &(N' 6(((  9  v ((h % %  !"(#$H%&'(     $9@&N  &N%'>&#%.%*,,%'N'')n)B7;v Tv p ^2s,jSSjL"3L2AH5pkr