Special sub-package of Scipy module in Python

In this tutorial, you are going to learn about the special sub-package of Scipy module in Python. Scipy module is an inbuilt module of python and used in mathematics and scientific computing. It can also operate over the numpy array. This module has various sub-packages like linalg, fftpack, ndimage, stats, special, io and optimize etc. To use this module, firstly you have to import it.

from scipy import special

Special sub-package of Scipy module

Special sub-package have various methods related to the mathematics that helps in solving the problem like gamma function, logsumexp, lambert function, Bessel function, Cubic root, Exponential, Permutation and Combination etc. The special sub-package has inbuilt methods to find out the solutions of the mathematic function.

Cube root function:

The cube root of a number means that the resultant will multiply by itself for the three times to get the cube of a number.
For ex: cube root of 64 is 4 (i.e., 4x4x4 = 64).
Cbrt() method will use to get the cube root of a number. You can pass a single number or a list of numbers to get the cube root of these numbers.

from scipy.special import cbrt  # import the module
l=[64, 343, 27]      # list
re=cbrt(l)        # use of cbrt() method
n=0
# to get the cube root one by one
for i in re:
    print("The cube root of",l[n],":",i)
    n=n+1
print(re)

Output:-

The cube root of 64 : 4.0
The cube root of 343 : 7.0
The cube root of 27 : 3.0
[4.  7.  3.]

Exponential function:

Exponential function (exp10) means that 10^a where a is the argument that will pass into the exp10 method.
For ex: exp10(2) will compute it like 10^2 i.e., multiply 10×10 which gives you 100.

# import the module
from scipy.special import exp10
# use of exponential method
ex=exp10(2)
print("Exponential of 10 is:",ex)

Output:-

Exponential of 10 is: 100.0

Permutation and combination:-

The permutation of (n,k) is calculated as n!/(n-k)! k! whereas the combination is calculated as n!/(n-k)!. The permutation and combination will calculate with the help of the perm(n,k) and comb(n,k) methods.
For ex: perm(5,2)= 5!/(5-2)! 2!
=5!/ 3!* 2!
= 5x4x3x2x1 / (3x2x1) (2×1)

=20.0
comb(5,2)= 5!/ (5-2)!
= 5!/3!  = 10.0

# import the  module
from scipy.special import comb,perm
# use of perm()
pe=perm(5,2) 
print("The permutation is:",pe)
# use of comb()
co=comb(5,2)
print("The comination is:",co)

Output:-

The permutation is: 20.0
The comination is: 10.0

Gamma function:

The gamma function of positive number (n>0) can be find out with the help of (n-1)!.

# import the module
from scipy.special import gamma
# use of gamma()
res1 = gamma(7)    # when n>0
res2 = gamma(-2.2) # when n<0
res3 = gamma(2.5)  # for decimal number
print(res1)
print(res2)
print(res3)

Output:-

720.0
-2.2049805184191333
1.329340388179137

Check out the other tutorials on python:

Linear algebra with Scipy module in Python

Exploring random Module in Python

Leave a Reply

Your email address will not be published. Required fields are marked *