Open In App

How to define a mathematical function in SymPy?

Last Updated : 01 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

SymPy is a Python Library that makes ‘Symbolic Computation’ possible in Python.

Mathematical Functions using SymPy

We can define mathematical functions by using SymPy in Python. There are two types of functions that we can define with the help of SymPy: ‘Undefined Functions’ and ‘Custom Functions’.

Undefined Functions:

A programmer can create ‘Undefined Functions’ when he/she doesn’t want to evaluate the result and keep the result unevaluated. These functions don’t have mathematical property defined on them. This means that any argument passed in an undefined function will remain unevaluated and the function will never evaluate to any result.

Custom Functions:

With the help of ‘Custom Functions’, a programmer can give the defined function a mathematical property. In custom functions, the mathematical property is applied on the argument, the result is evaluated and printed on the screen.

The eval()  Class Method:

The eval() class method evaluates the result automatically. It is used on a subclass function to return a value by performing calculations on the arguments or return None. This means that eval() will always return an evaluated result or it will return None.

Define a mathematical function in SymPy

Undefined Functions:

First of all, to define an undefined function in Python, we need to import SymPy. After importing SymPy , we just need to declare a variable and define a Function in it.

Example 1: Defining an Undefined Function with a SymPy Variable

Python3




# importing SymPy Library
import sympy
  
# creating undefined function 'f'
f = sympy.Function('f')
  
# creating SymPy variable 'x'
x = sympy.Symbol('x')
  
# calling function 'f' with variable 'x' as its argument
f(x)


Output:

f(x)

Example 2: Defining an Undefined Function with an Integer

Python3




# importing SymPy Library
import sympy
  
# creating undefined function 'f'
f = sympy.Function('f')
  
# calling the function 'f' with an integer as its argument
f(0)


Output:

f(0)

Example 3: Defining an Undefined Function that is a Non-Callable Object

Python




# importing SymPy Library
import sympy
  
# creating a SymPy variable 'x'
x = sympy.Symbol('x')
  
# creating non-callable function
f = sympy.Function('f')(x)
  
# calling the function 'f'
f


Output:

f(x)

If we call the function ‘f’ here with some argument, it will throw an error.

Python3




# importing SymPy Library
import sympy
  
# creating a SymPy variable 'x'
x = sympy.Symbol('x')
  
#creating another SymPy variable 'y'
y = sympy.Symbol('y')
  
# creating non-callable function
f = sympy.Function('f')(x)
  
# calling the function 'f' with an argument
f(y)


Output:

 

Example 4: Performing Differentiation on an Undefined Function

Python3




# importing SymPy Library
import sympy
  
# creating undefined function 'f'
f = sympy.Function('f')
  
# creating SymPy variable 'x'
x = sympy.Symbol('x')
  
# performing differentiation on the function 'f'
f(x).diff(x)


Output:

d/dx {f(x)}

Custom Functions:

In a custom function, the result is fully evaluated. For that purpose, we will define a class and inside it, for defining mathematical property on that function, we will define subclass function. 

Example 1: Function to get the square of a number

In this example, we are going to define a function that will return the square of the passed argument.

Python3




# importing SymPy Library
import sympy
  
# creating class square
class square(sympy.Function):
  @classmethod
  # defining subclass function with 'eval()' class method
  def eval(cls, x):
    print(x**2)
      
# calling the eval() subclass
# function with integer 4 as its argument
square.eval(4)


Output:

16

If we just call the class, then we will not have the result. Instead, we’ll have the function name and as its argument, we’ll have that number.

Python3




# importing SymPy Library
import sympy
  
# creating class 'square'
class square(sympy.Function):
  @classmethod
  # defining subclass function with 'eval()' class method
  def eval(cls, x):
    print(x**2)
    
# calling class
square(4)


Output:

square(4)

2. Function to get the Cube of a Number

Python3




# importing SymPy Library
import sympy
  
# creating class
class cube(sympy.Function):
  @classmethod
  # defining subclass function with 'eval()' class method
  def eval(cls, x):
    print(x**3)
  
# calling the function
cube(9)


Output:

cube(9)

Calling the eval() Subclass Function.

Python3




# importing SymPy Library
import sympy
  
# creating class
class cube(sympy.Function):
  @classmethod
  # defining subclass function with 'eval()' class method
  def eval(cls, x):
    print(x**3)
  
# calling the subclass function 
cube.eval(9)


Output:

729


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads