This module provides convenient functions to transform sympy expressions to lambda functions which can be used to calculate numerical values very fast.

sympy.utilities.lambdify.implemented_function(symfunc, implementation)[source]

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 a sympy function, attach implementation to it.

implementation : callable

numerical implementation to be called by evalf() or lambdify

Returns :

afunc : sympy.FunctionClass instance

function with attached implementation


>>> from sympy.abc import x
>>> from sympy.utilities.lambdify import lambdify, implemented_function
>>> from sympy import Function
>>> f = implemented_function(Function('f'), lambda x: x+1)
>>> lam_f = lambdify(x, f(x))
>>> lam_f(4)
sympy.utilities.lambdify.lambdastr(args, expr, printer=None, dummify=False)[source]

Returns a string that can be evaluated to a lambda function.

>>> 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])'
sympy.utilities.lambdify.lambdify(args, expr, modules=None, printer=None, use_imps=True)[source]

Returns a lambda function for fast calculation of numerical values.

If not specified differently by the user, SymPy functions are replaced as far as possible by either python-math, numpy (if available) or mpmath functions - exactly in this order. To change this behavior, the “modules” argument can be used. It accepts:

  • the strings “math”, “mpmath”, “numpy”, “sympy”
  • any modules (e.g. math)
  • dictionaries that map names of sympy functions to arbitrary functions
  • lists that contain a mix of the arguments above, with higher priority given to entries appearing first.


>>> from sympy.utilities.lambdify import implemented_function, lambdify
>>> from sympy import sqrt, sin, Matrix
>>> from sympy import Function
>>> from sympy.abc import x, y, z
>>> f = lambdify(x, x**2)
>>> f(2)
>>> f = lambdify((x, y, z), [z, y, x])
>>> f(1,2,3)
[3, 2, 1]
>>> f = lambdify(x, sqrt(x))
>>> f(4)
>>> f = lambdify((x, y), sin(x*y)**2)
>>> f(0, 5)
>>> f = lambdify((x, y), Matrix((x, x + y)).T, modules='sympy')
>>> f(1, 2)
Matrix([[1, 3]])

Functions present in \(expr\) can also carry their own numerical implementations, in a callable attached to the _imp_ attribute. Usually you attach this using the implemented_function factory:

>>> f = implemented_function(Function('f'), lambda x: x+1)
>>> func = lambdify(x, f(x))
>>> func(4)

lambdify always prefers _imp_ implementations to implementations in other namespaces, unless the use_imps input parameter is False.


  1. Use one of the provided modules:

    >> f = lambdify(x, sin(x), “math”)

    Attention: Functions that are not in the math module will throw a name

    error when the lambda function is evaluated! So this would be better:

    >> f = lambdify(x, sin(x)*gamma(x), (“math”, “mpmath”, “sympy”))

  2. Use some other module:

    >> import numpy >> f = lambdify((x,y), tan(x*y), numpy)

    Attention: There are naming differences between numpy and sympy. So if

    you simply take the numpy module, e.g. sympy.atan will not be translated to numpy.arctan. Use the modified module instead by passing the string “numpy”:

    >> f = lambdify((x,y), tan(x*y), “numpy”) >> f(1, 2) -2.18503986326 >> from numpy import array >> f(array([1, 2, 3]), array([2, 3, 5])) [-2.18503986 -0.29100619 -0.8559934 ]

  3. Use own dictionaries:

    >> def my_cool_function(x): ... >> dic = {“sin” : my_cool_function} >> f = lambdify(x, sin(x), dic)

    Now f would look like:

    >> lambda x: my_cool_function(x)

Previous topic


Next topic


This Page