How to define a mathematical function in SymPy?

Mathematics acts as a base aid for analyzing and modeling patterns, creating and applying algorithms, statistical analysis etc. Thus, mathematical operations play a major role in most python projects.

A mathematical function is a relation between the elements of a set to the elements of another set.

Python provides various mathematical functions to facilitate programmers and developers.
SymPy is a Python library for symbolic mathematics which also aims to become a full-featured computer algebra system (CAS). Its library has been split into a core with many modules that provide capabilities for arithmetic and polynomial functions, trigonometric computations, solving equations, calculus, geometry, matrices etc.

In this tutorial, you will learn about the provisions given by the SymPy library for the usage of mathematical functions.

Mathematical functions in SymPy

Python’s SymPy package comes with a Function class.
The sympy.core.function module defines the Function class. This acts as a base class for various mathematical functions like trigonometric functions, functions on complex numbers, integers etc.

An example is the “sign” function that is defined in the sympy.functions.elementary.complexes module. It returns the sign of an expression as follows:

from sympy import *
sign(-1)
1

The Function class also acts as a constructor class for undefined functions. You can observe the same below.

Defining a mathematical function in SymPy

The sympy’s Function is for undefined functions. This means that functions that are defined using the same, are not going to be evaluated.

Let’s say you want to define a function; f(x)= 2*x

In the below example, we have defined a function f(x)=2x. However, as mentioned earlier, f(x) remains unevaluated in expressions. You can observe the same below:

from sympy import * 
x=symbols('x') 
f(x)=2*x
f(1)
 File "<ipython-input-9-25b627c373f1>", line 3
    f(x)=2*x
            ^
SyntaxError: can't assign to function call

So, let us consider you want to define a mathematical function f(x) that evaluates for different values of x.
You can do so by simply assigning the expression to a variable f which will be your function.

The sympy.symbols() method is used to declare variables for the mathematical function.
The f(symbol(x)) will give a symbolic 2x.

Further, the sympy.subs() method is used to substitute all instances of a variable in a mathematical expression (with some other variable/expression/value).

Thus, a function f(x)=2x which can be evaluated at different values of x is defined as follows:

from sympy import *
x=symbols('x')
f=2*x
f.subs(x,1)
2

Here, we have evaluated f(x) at x=1 by using sympy’s subs(x,1).

You can further integrate or differentiate your function too!
Sympy provides in-built mathematical methods, integrate() and diff() for the same.

from sympy import * 
x=symbols('x') 
f=2*x
f.subs(x,1)
integrate(f)
2
from sympy import * 
x=symbols('x') 
f=2*x
f.diff(x)
#differentiating f with respect to x
2

Here’s another example.
Here, we are defining a function f(x)=x**2+4*x, finding its value at x=1 and then obtaining the derivative as well as integration of the function.

from sympy import *
x=symbols('x')
f=x**2+4*x
result_at_xequals1=f.subs(x,1)
derivative=f.diff(x)
integral=integrate(f)
print(result_at_xequals1)
print(derivative)
print(integral)
5
2*x + 4
x**3/3 + 2*x**2

Note:

You can also do the same by using normal Python functions as shown:

def f(x): 
    return(x**2+4*x)
result=f(1)
derivative=diff(f(x))
integral=integrate(f(x))
print(result)
print(derivative)
print(integral)
5
2*x + 4
x**3/3 + 2*x**2

 

Read more!! Mathematical Functions in Python
The SymPy official documentation – https://docs.sympy.org/latest/index.html

Leave a Reply

Your email address will not be published.