Change language

# Python | sympy.lambdify () method

|

With the sympy.lambdify () method, we can convert the SymPy expression into an expression that can be evaluated numerically.  lambdify acts like a lambda function, except that it converts SymPy names to the names of a given numeric library, usually NumPy or math .

Syntax: lambdify (variable, expression, library)

Parameters:
variable - It is the variable in the mathematical expression.
expression - It is the mathematical expression which is converted into its respective name in the given library .
library - It is the Python library to which expression is to be converted into.

Returns: Returns a lambda function which can evaluate a mathematical expression.

Example # 1:
In this example, we see that with the sympy.lambdify () method we can get a lambda function from a mathematical expression.

` `

`  # import sympy from sympy import *     x = symbols ( ’x’ ) expr = sin (x)   # Use the sympy.lambdify () method f = lambdify (x, expr, "math" )    print ( "Using lambda function in SymPy to evaluate sin (90): {}" . format (f ( 90 )))  `

` `

Exit :

` Using lambda function in SymPy to evaluate sin (90): 0.893996663601 `

Example # 2:
We can pass a dictionary of sympy_name: numeric_function pairs to use lambdify with numeric libraries he doesn’t know about.

 ` # import sympy ` ` from ` ` sympy ` ` import ` ` * `    ` def ` ` squared (n): ` < / p> ` return ` ` n ` ` * ` ` * ` ` 2 ` ` `  ` x ` ` = ` ` symbols (` ` ’x’ ` `) ` ` expr ` ` = ` ` x ` ` * ` ` * ` ` 2 `   ` # Use the sympy method .lambdify () ` ` f ` ` = ` ` lambdify (x, expr, {` ` "**" ` `: squared}) `   ` print ` ` (` ` "Using lambda function in SymPy to evaluate squared function: {}" ` `. ` ` format ` ` (f (` ` 10 ` `))) `

Output:

` Using lambda function in SymPy to evaluate squared function: 100 `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically