Python sympy.evalf() 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.evalf() Method:

The mathematical expressions can be evaluated using the sympy.evalf() method.

Syntax:

 sympy.evalf()

Return Value:

The evaluated mathematical expression is returned by the evalf() function.

sympy.evalf() Method Python

Example1

Approach:

  • Import all the functions from sympy using the import keyword
  • Give the mathematical expression as static input and store it in a variable.
  • Apply evalf() function on the above given mathematical expression to evaluate the given mathematical expression.
  • Store it in another variable.
  • Print the result of the evaluated mathematical expression.
  • The Exit of the Program.

Below is the implementation:

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

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = sqrt(16)

# Apply evalf() function on the above given mathematical expression to 
# evaluate the given mathematical expression. 
# Store it in another variable.
rslt = gvn_expression.evalf()
# Print the result of the evaluated mathematical expression. 
print("The result of the evaluated mathematical expression:") 
print(rslt)

Output:

The result of the evaluated mathematical expression:
4.00000000000000

Example2

Approach:

  • Import all the functions from sympy using the import keyword
  • Give the mathematical expression as static input and store it in a variable.
  • Apply evalf() function on the above given mathematical expression to evaluate the given mathematical expression.
  •  Here we multiply the values of sin(45) and cos(45).
  • Store it in another variable.
  • Print the result of the evaluated mathematical expression.
  • The Exit of the Program.

Below is the implementation:

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

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = sin(45)*cos(45)

# Apply evalf() function on the above given mathematical expression to 
# evaluate the given mathematical expression. 
# Here we multiply the values of sin(45) and cos(45).
# Store it in another variable.
rslt = gvn_expression.evalf()
# Print the result of the evaluated mathematical expression. 
print("The result of the evaluated mathematical expression:") 
print(rslt)

Output:

The result of the evaluated mathematical expression:
0.446998331800279