How to define a mathematical function in SymPy?
Last Updated :
01 Dec, 2022
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
import sympy
f = sympy.Function( 'f' )
x = sympy.Symbol( 'x' )
f(x)
|
Output:
f(x)
Example 2: Defining an Undefined Function with an Integer
Python3
import sympy
f = sympy.Function( 'f' )
f( 0 )
|
Output:
f(0)
Example 3: Defining an Undefined Function that is a Non-Callable Object
Python
import sympy
x = sympy.Symbol( 'x' )
f = sympy.Function( 'f' )(x)
f
|
Output:
f(x)
If we call the function ‘f’ here with some argument, it will throw an error.
Python3
import sympy
x = sympy.Symbol( 'x' )
y = sympy.Symbol( 'y' )
f = sympy.Function( 'f' )(x)
f(y)
|
Output:
Example 4: Performing Differentiation on an Undefined Function
Python3
import sympy
f = sympy.Function( 'f' )
x = sympy.Symbol( 'x' )
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
import sympy
class square(sympy.Function):
@classmethod
def eval ( cls , x):
print (x * * 2 )
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
import sympy
class square(sympy.Function):
@classmethod
def eval ( cls , x):
print (x * * 2 )
square( 4 )
|
Output:
square(4)
2. Function to get the Cube of a Number
Python3
import sympy
class cube(sympy.Function):
@classmethod
def eval ( cls , x):
print (x * * 3 )
cube( 9 )
|
Output:
cube(9)
Calling the eval() Subclass Function.
Python3
import sympy
class cube(sympy.Function):
@classmethod
def eval ( cls , x):
print (x * * 3 )
cube. eval ( 9 )
|
Output:
729
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...