Saturday, November 16, 2024
Google search engine
HomeLanguagesSpecial functions in SciPy

Special functions in SciPy

In this article, we are going to see about special functions in Scipy. The special functions in scipy are used to perform mathematical operations on the given data. Special function in scipy is a module available in scipy package. Inside this special function, the available methods are:

  • cbrt – which gives the cube root of the given number
  • comb – gives the combinations of the elements
  • exp10 – gives the number with raise to 10 power of the given number
  • exprel – gives the relative error exponential, (exp(x) – 1)/x.
  • gamma – returns the value by calculating the z*gamma(z) = gamma(z+1) and gamma(n+1) = n!, for a natural number ‘n’.
  • lambertw –  computes the W(z) * exp(W(z)) for any complex number z, where W is the lambertw function
  • logsumexp – gives the log of the sum of exponential of given number
  • perm – gives the permutations of the elements

Let’s understand about these functions in detail.

1. cbrt()

This is used to return the cube root of the given number.

Syntax: cbrt(number)

Example: Program to find the cube root

Python3




from scipy.special import cbrt
  
# cube root of 64
print(cbrt(64))
  
# cube root of 78
print(cbrt(78))
  
# cube root of 128
print(cbrt(128))


Output:

4.0
4.272658681697917
5.039684199579493

Example: Program to find cube root in the given array elements.

Python3




from scipy.special import cbrt
  
# cube root of elements in an array
arr = [64, 164, 564, 4, 640]
arr = list(map(cbrt,arr))
print(arr)


Output:

[4.0, 5.473703674798428, 8.26214922566535, 1.5874010519681994, 8.617738760127535]

2. comb()

It is known as combinations and returns the combination of a given value.

Syntax: scipy.special.comb(N, k)

Where, N is the input value and k is the number of repetitions.

Example 1:

Python3




# import combinations
from scipy.special import comb
  
# combinations of input 4
print(comb(4,1))


Output:

4.0

Example 2:

Python3




# import combinations module
from scipy.special import comb
  
# combinations of 4
print([comb(4,1),comb(4,2),comb(4,3),
       comb(4,4),comb(4,5)])
  
# combinations of 6
print([comb(6,1),comb(6,2),comb(6,3),
       comb(6,4),comb(6,5)])


Output:

[4.0, 6.0, 4.0, 1.0, 0.0]
[6.0, 15.0, 20.0, 15.0, 6.0]

3. exp10()

This method gives the number with raise to 10 power of the given number.

Syntax: exp10(value)

Where value is the number which is given as the input.

Example: Program to find the power of 10

Python3




from scipy.special import exp10
  
  
# 10 to the power of 2
print(exp10(2))


Output:

100.0

Example: Program to find the powers of 10 for a range

Python3




from scipy.special import exp10
  
# exponent raise to power 10 
# for a range
for i in range(1,10):
  print(exp10(i)


Output:

10.0
100.0
1000.0
10000.0
100000.0
1000000.0
10000000.0
100000000.0
1000000000.0

4. exprel()

It is known as the Relative Error Exponential Function. It returns the error value for a given variable. If x is near zero, then exp(x) is near 1.

Syntax: scipy.special.exprel(input_data)

Example 1:

Python3




# import exprel
from scipy.special import exprel
  
  
# calculate exprel of 0
print(exprel(0))


Output:

1.0

Example 2:

Python3




# import exprel
from scipy.special import exprel
  
# list of elements 
arr = [0,1,2,3,4,5]
  
print(list(map(exprel,arr)))


Output:

[1.0, 1.718281828459045, 3.194528049465325, 6.361845641062556, 13.399537508286059, 29.48263182051532]

5. gamma()

It is known as Gamma function. It is the generalized factorial since z*gamma(z) = gamma(z+1) and gamma(n+1) = n!, for a natural number ‘n’.

Syntax: scipy.special.gamma(input_data)

Where, input data is the input number.

Example 1:

Python3




# import gamma function
from scipy.special import gamma
  
  
print(gamma(56))


Output:

1.2696403353658278e+73

Example 2:

Python3




# import gamma function
from scipy.special import gamma
  
  
print([gamma(56), gamma(156), gamma(0),
       gamma(1), gamma(5)])


Output:

[1.2696403353658278e+73, 4.789142901463394e+273, inf, 1.0, 24.0]

6. lambertw()

It is also known as Lambert Function. It calculates the value of W(z) is such that z = W(z) * exp(W(z)) for any complex number z, where W is known as the Lambert Function

Syntax: scipy.special.lambertw(input_data)

Example:

Python3




# import lambert function
from scipy.special import lambertw
  
# calculate W value
print([lambertw(1),lambertw(0),lambertw(56),
       lambertw(68),lambertw(10)])


Output:

[(0.5671432904097838+0j), 0j, (2.9451813101206707+0j), (3.0910098540499797+0j), (1.7455280027406994+0j)]

7. logsumexp()

It is known as Log Sum Exponential Function. It will return the log of the sum of the exponential of input elements.

Syntax: scipy.special.logsumexp(input_value)

where, input value is the input data.

Example 1:

Python




from scipy.special import logsumexp
  
# logsum exp of numbers from 
# 1 to 10
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(logsumexp(a))


Output:

10.45862974442671

Example 2:

Python3




from scipy.special import logsumexp
  
# logsum exp of numbers from
# 1 to 10
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  
# logsum exp of numbers from 
# 10 to 15
b = [10, 11, 12, 13, 14, 15]
print([logsumexp(a), logsumexp(b)])


Output:

[10.45862974442671, 15.456193316018123]

8. perm()

The perm stands for the permutation. It will return the permutation of the given numbers.

Syntax: scipy.special.perm(N,k)

where N is the input value and k is the no of repetitions.

Example:

Python3




# import permutations module
from scipy.special import perm
  
# permutations of 4
print([perm(4, 1), perm(4, 2), perm(4, 3), 
       perm(4, 4), perm(4, 5)])
  
# permutations of 6
print([perm(6, 1), perm(6, 2), perm(6, 3), 
       perm(6, 4), perm(6, 5)])


Output:

[4.0, 12.0, 24.0, 24.0, 0.0]
[6.0, 30.0, 120.0, 360.0, 720.0]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments