skfdiff.core.backends package¶
Submodules¶
skfdiff.core.backends.base_backend module¶
-
class
skfdiff.core.backends.base_backend.Backend(system: skfdiff.core.system.PDESys, grid_builder: skfdiff.core.grid_builder.GridBuilder)[source]¶ Bases:
abc.ABC
skfdiff.core.backends.numba_backend module¶
skfdiff.core.backends.numpy_backend module¶
-
class
skfdiff.core.backends.numpy_backend.NumpyBackend(system: skfdiff.core.system.PDESys, grid_builder: skfdiff.core.grid_builder.GridBuilder)[source]¶ Bases:
skfdiff.core.backends.base_backend.Backend-
name= 'numpy'¶
-
-
skfdiff.core.backends.numpy_backend.lambdify(args, expr, *, modules=[{'amax': <function np_Max>, 'amin': <function np_Min>, 'Heaviside': <function np_Heaviside>}, 'scipy'], printer=None, use_imps=True, dummify=False)¶ Translates a SymPy expression into an equivalent numeric function
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.
lambdifybridges the two by converting a SymPy expression to an equivalent numeric function.The basic workflow with
lambdifyis 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, uselambdifyto convert this to an equivalent function for numerical evaluation. For instance, above we createdexprusing the SymPy symbolxand SymPy functionssinandcos, then converted it to an equivalent NumPy functionf, and called it on a NumPy arraya.Warning
This function uses
exec, and thus shouldn’t be used on unsanitized input.- Parameters
first argument of lambdify is a variable or list of variables in (The) –
expression. Variable lists may be nested. Variables can be Symbols, (the) –
functions, or matrix symbols. The order and nesting of the (undefined) –
corresponds to the order and nesting of the parameters passed to (variables) –
lambdified function. For instance, (the) –
from sympy.abc import x, y, z (>>>) –
f = lambdify([x, (y, z)], x + y + z) (>>>) –
f(1, (2, 3)) (>>>) –
6 –
second argument of lambdify is the expression, list of (The) –
or matrix to be evaluated. Lists may be nested. If the (expressions,) –
is a list, the output will also be a list. (expression) –
f = lambdify(x, [x, [x + 1, x + 2]]) (>>>) –
f(1) (>>>) –
[2, 3]] ([1,) –
it is a matrix, an array will be returned (for the NumPy module) (If) –
from sympy import Matrix (>>>) –
f = lambdify(x, Matrix([x, x + 1])) (>>>) –
f(1) –
[[1] – [2]]
that the argument order here, variables then expression, is used to (Note) –
the Python lambda keyword. lambdify(x, expr) works (emulate) –
like ``lambda x ((roughly)) –
third argument, modules is optional. If not specified, modules (The) –
to ["scipy", "numpy"] if SciPy is installed, ["numpy"] if (defaults) –
NumPy is installed, and ["math", "mpmath", "sympy"] if neither is (only) –
That is, SymPy functions are replaced as far as possible by (installed.) –
scipy or numpy functions if available, and Python's (either) –
library math, or mpmath functions otherwise. (standard) –
can be one of the following types (modules) –
the strings
"math","mpmath","numpy","numexpr","scipy","sympy", or"tensorflow". 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=numpyis equivalent tomodules="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
sinfunction with a custom version, you can use[{'sin': custom_sin}, 'numpy']).
dummify keyword argument controls whether or not the variables in (The) –
provided expression that are not valid Python identifiers are (the) –
with dummy symbols. This allows for undefined functions like (substituted) –
to be supplied as arguments. By default, the (Function('f')(t)) –
are only dummified if they are not valid Python identifiers. Set (variables) –
to replace all arguments with dummy symbols (if args (dummify=True) –
not a string) - for example, to ensure that the arguments do not (is) –
any built-in names. (redefine) –
_lambdify-how-it-works (.) –
it works (How) –
============ –
using this function, it helps a great deal to have an idea of what it (When) –
doing. At its core, lambdify is nothing more than a namespace (is) –
on top of a special printer that makes some corner cases work (translation,) –
properly. –
understand lambdify, first we must properly understand how Python (To) –
work. Say we had two files. One called sin_cos_sympy.py, (namespaces) –
with –
:param .. code:: python: # sin_cos_sympy.py
from sympy import sin, cos
- def sin_cos(x):
return sin(x) + cos(x)
and one called
sin_cos_numpy.pywith# 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,sinandcosare defined as the SymPysinandcos. In the second, they are defined as the NumPy versions.If we were to import the first file and use the
sin_cosfunction, 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_cosfrom 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
sinandcosfunctions from SymPy. In the second, we got a numeric result, becausesin_cosused the numericsinandcosfunctions from NumPy. But notice that the versions ofsinandcosthat were used was not inherent to thesin_cosfunction definition. Bothsin_cosdefinitions are exactly the same. Rather, it was based on the names defined at the module where thesin_cosfunction 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
execfunction.exectakes 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 thesin_cosdefined insin_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
lambdifyworks. The name “lambdify” comes from the fact that we can think of something likelambdify(x, sin(x) + cos(x), 'numpy')aslambda x: sin(x) + cos(x), wheresinandcoscome from thenumpynamespace. This is also why the symbols argument is first inlambdify, as opposed to most SymPy functions where it comes after the expression: to better mimic thelambdakeyword.lambdifytakes the input expression (likesin(x) + cos(x)) andConverts it to a string
Creates a module globals dictionary based on the modules that are passed in (by default, it uses the NumPy module)
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., thenexec``s that string with the module globals namespace and returns ``func.
In fact, functions returned by
lambdifysupport inspection. So you can see exactly how they are defined by usinginspect.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 off:>>> f.__globals__['sin'] <ufunc 'sin'> >>> f.__globals__['cos'] <ufunc 'cos'> >>> f.__globals__['sin'] is numpy.sin True
This shows us that
sinandcosin the namespace offwill benumpy.sinandnumpy.cos.Note that there are some convenience layers in each of these steps, but at the core, this is how
lambdifyworks. Step 1 is done using theLambdaPrinterprinters defined in the printing module (seesympy.printing.lambdarepr). This allows different SymPy expressions to define how they should be converted to a string for different modules. You can change which printerlambdifyuses by passing a custom printer in to theprinterargument.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
modulesargument. 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 whymysinis used above instead ofnumpy.sin.If you want to modify the way
lambdifyworks 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
lambdifyworks 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): ... AttributeError: 'Add' object has no attribute 'sin'
Now, let’s look at what happened. The reason this fails is that
gcallsnumpy.sinon the input expression, andnumpy.sindoes 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
fdid work? That’s becausefdoesn’t call any functions, it only adds 1. So the resulting function that is created,def _lambdifygenerated(x): return x + 1does not depend on the globals namespace it is defined in. Thus it works, but only by accident. A future version oflambdifymay 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
moduleargument is not provided,lambdifycreates functions using the NumPy and SciPy namespaces.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]])
lambdifycan be used to translate SymPy expressions into mpmath functions. This may be preferable to usingevalf(which uses mpmath on the backend) in some cases.>>> import mpmath >>> 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
flattenfunction 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
exprcan also carry their own numerical implementations, in a callable attached to the_imp_attribute. This can be used with undefined functions using theimplemented_functionfactory:>>> f = implemented_function(Function('f'), lambda x: x+1) >>> func = lambdify(x, f(x)) >>> func(4) 5
lambdifyalways prefers_imp_implementations to implementations in other namespaces, unless theuse_impsinput parameter is False.Usage with Tensorflow:
>>> import tensorflow as tf >>> from sympy import Max, sin >>> f = Max(x, sin(x)) >>> func = lambdify(x, f, 'tensorflow') >>> result = func(tf.constant(1.0)) >>> print(result) # a tf.Tensor representing the result of the calculation Tensor("Maximum:0", shape=(), dtype=float32) >>> sess = tf.Session() >>> sess.run(result) # compute result 1.0 >>> var = tf.Variable(1.0) >>> sess.run(tf.global_variables_initializer()) >>> sess.run(func(var)) # also works for tf.Variable and tf.Placeholder 1.0 >>> tensor = tf.constant([[1.0, 2.0], [3.0, 4.0]]) # works with any shape tensor >>> sess.run(func(tensor)) [[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_functionand 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-functionsIn previous versions of SymPy,
lambdifyreplacedMatrixwithnumpy.matrixby default. As of SymPy 1.0numpy.arrayis the default. To get the old default behavior you must pass in[{'ImmutableDenseMatrix': numpy.matrix}, 'numpy']to themoduleskwarg.>>> from sympy import lambdify, Matrix >>> from sympy.abc import x, y >>> import numpy >>> array2mat = [{'ImmutableDenseMatrix': numpy.matrix}, 'numpy'] >>> f = lambdify((x, y), Matrix([x, y]), modules=array2mat) >>> f(1, 2) [[1] [2]]
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:
>>> from sympy import Piecewise >>> from sympy.utilities.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