SymPy - Function class

Sympy package has Function class, which is defined in sympy.core.function module. It is a base class for all applied mathematical functions, as also a constructor for undefined function classes.
Following categories of functions are inherited from Function class −
Functions for complex number
Trigonometric functions
Functions for integer number
Combinatorial functions
Other miscellaneous functions
This set of functions is defined in sympy.functions.elementary.complexes module.
re
This function returns real part of an expression −
>>> from sympy import * 
>>> re(5+3*I)

The output for the above code snippet is given below −
5
>>> re(I)

The output for the above code snippet is −
0
Im
This function returns imaginary part of an expression −
>>> im(5+3*I)

The output for the above code snippet is given below −
3
>>> im(I)

The output for the above code snippet is given below −
1
sign
This function returns the complex sign of an expression.
For real expression, the sign will be −
1 if expression is positive
0 if expression is equal to zero
-1 if expression is negative
If expression is imaginary the sign returned is −
I if im(expression) is positive
-I if im(expression) is negative
>>> sign(1.55), sign(-1), sign(S.Zero)

The output for the above code snippet is given below −
(1, -1, 0)
>>> sign (-3*I), sign(I*2)

The output for the above code snippet is given below −
(-I, I)
Abs
This function return absolute value of a complex number. It is defined as the distance between the origin (0,0) and the point (a,b) in the complex plane. This function is an extension of the built-in function abs() to accept symbolic values.
>>> Abs(2+3*I)

The output for the above code snippet is given below −
13
conjugate
This function returns conjugate of a complex number. To find the complex conjugate we change the sign of the imaginary part.
>>> conjugate(4+7*I)

You get the following output after executing the above code snippet −
4 - 7i
SymPy has defintions for all trigonometric ratios - sin cos, tan etc as well as well as its inverse counterparts such as asin, acos, atan etc. These functions compute respective value for given angle expressed in radians.
>>> sin(pi/2), cos(pi/4), tan(pi/6)

The output for the above code snippet is given below −
(1, sqrt(2)/2, sqrt(3)/3)
>>> asin(1), acos(sqrt(2)/2), atan(sqrt(3)/3)

The output for the above code snippet is given below −
(pi/2, pi/4, pi/6)
This is a set of functions to perform various operations on integer number.
ceiling
This is a univariate function which returns the smallest integer value not less than its argument. In case of complex numbers, ceiling of the real and imaginary parts separately.
>>> ceiling(pi), ceiling(Rational(20,3)), ceiling(2.6+3.3*I)

The output for the above code snippet is given below −
(4, 7, 3 + 4*I)
floor
This function returns the largest integer value not greater than its argument. In case of complex numbers, this function too takes the floor of the real and imaginary parts separately.
>>> floor(pi), floor(Rational(100,6)), floor(6.3-5.9*I)

The output for the above code snippet is given below −
(3, 16, 6 - 6*I)
frac
This function represents the fractional part of x.
>>> frac(3.99), frac(Rational(10,3)), frac(10)

The output for the above code snippet is given below −
(0.990000000000000, 1/3, 0)
Combinatorics is a field of mathematics concerned with problems of selection, arrangement, and operation within a finite or discrete system.
factorial
The factorial is very important in combinatorics where it gives the number of ways in which n objects can be permuted. It is symbolically represented as x! This function is implementation of factorial function over nonnegative integers, factorial of a negative integer is complex infinity.
>>> x=Symbol('x') 
>>> factorial(x)

The output for the above code snippet is given below −
x!
>>> factorial(5)

The output for the above code snippet is given below −
120
>>> factorial(-1)

The output for the above code snippet is given below −
∞∽
This function the number of ways we can choose k elements from a set of n elements.
>>> x,y=symbols('x y') 
>>> binomial(x,y)

The output for the above code snippet is given below −
(xy)
>>> binomial(4,2)

The output for the above code snippet is given below −
6
Rows of Pascal's triangle can be generated with the binomial function.
>>> for i in range(5): print ([binomial(i,j) for j in range(i+1)])

You get the following output after executing the above code snippet −
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
fibonacci
The Fibonacci numbers are the integer sequence defined by the initial terms F0=0, F1=1 and the two-term recurrence relation Fn=Fn−1+Fn−2.
>>> [fibonacci(x) for x in range(10)]

The following output is obtained after executing the above code snippet −
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
tribonacci
The Tribonacci numbers are the integer sequence defined by the initial terms F0=0, F1=1, F2=1 and the three-term recurrence relation Fn=Fn-1+Fn-2+Fn-3.
>>> tribonacci(5, Symbol('x'))

The above code snippet gives an output equivalent to the below expression −
x8+3x5+3x2
>>> [tribonacci(x) for x in range(10)]

The following output is obtained after executing the above code snippet −
[0, 1, 1, 2, 4, 7, 13, 24, 44, 81]
Following is a list of some frequently used functions −
Min − Returns minimum value of the list. It is named Min to avoid conflicts with the built-in function min.
Max − Returns maximum value of the list. It is named Max to avoid conflicts with the built-in function max.
root − Returns nth root of x.
sqrt − Returns the principal square root of x.
cbrt − This function computes the principal cube root of x, (shortcut for x++Rational(1,3)).
The following are the examples of the above miscellaneous functions and their respective outputs −
>>> Min(pi,E)

e
>>> Max(5, Rational(11,2))

112
>>> root(7,Rational(1,2))

49
>>> sqrt(2)

2
>>> cbrt(1000)

10
 Print
 Add Notes
 Bookmark this page