Python sympy.Lambda() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.Lambda() Method:

Using the sympy.Lambda() function, we can do any mathematical operation by simply defining the formula and then passing the parameters with reference variables.

Syntax:

sympy.Lambda()

Return Value:

The result of the mathematical formula is returned by the Lambda() function.

sympy.Lambda() Method in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
  • Pass the reference variables, and mathematical formula as arguments to the Lambda() function to compute the mathematical operation and get the result.
  • Store it in a variable.
  • Pass some random reference variable value to the above lambda function and print the result (here random value acts as an argument to the lambda function).
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function 
# and store them in corresponding variables.
y = symbols('y')

# Pass the reference variables and mathematical formula as arguments to the Lambda()
# function to compute the mathematical operation and get the result.
# Store it in a variable.
rslt = Lambda(y, y*3)

# Pass some random reference variable value to the above lambda function and
# print the result(here random value acts as an argument to the lambda function).
print(rslt(5))

Output:

15

Explanation:

Here the variable "y" is replaced with 5 and the result of the 
mathematical formula is computed

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
  • Pass the reference variables, mathematical formula as arguments to the Lambda() function to compute the mathematical operation and get the result.
  • Store it in a variable.
  • Here we are adding the x,y, and z variable values.
  • Scan the arguments to be passed as user input using the map(), input(), and split() functions and store them in separate variables.
  • Pass the above reference variable values to the above lambda function and print the result(here random value acts as an argument to the lambda function).

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function 
# and store them in corresponding variables.
x, y, z= symbols('x y z')

# Pass the reference variables, mathematical formula as arguments to the Lambda()
# function to compute the mathematical operation and get the result.
# Store it in a variable.
# Here we are adding the x,y,z variable values.
rslt = Lambda((x, y, z), x+y+z)

# Scan the arguments to be passed as user input using the map(), input(),
# split functions and store them in separate variables.
a, b, c = map(int, input("Enter 3 random values = ").split())

# Pass the above reference variable values to the above lambda function and 
# print the result(here random value acts as an argument to the lambda function).
print("The result of x+y+z = ", rslt(a, b, c))

Output:

Enter 3 random values = 10 20 15
The result of x+y+z = 45