SciPy Special Packages

SciPy Special Packages

Functions in special packages are general-purpose functions that follow the principles of broadcasting and automatic array looping.

Let’s take a look at some of the most commonly used special functions –

  • Cube Root Function
  • Exponential Function
  • Relative Error Exponential Function
  • Logarithmic and Exponential Functions
  • Lambert Function
  • Superposition and Combination Functions
  • Gamma Function

Now let’s take a brief look at each of these functions.

Cube Root Function

The syntax for this cube root function is – scipy.special.cbrt(x). This will get the element-wise cube root of x.

Let’s consider the following example.

from scipy.special import cbrt
res = cbrt([10, 9, 0.1254, 234])
print res

The above program will produce the following output.

[ 2.15443469 2.08008382 0.50053277 6.16224015]

Exponential Function

The syntax for the exponential function is – scipy.special.exp10(x). This will calculate the element-wise value of 10**x.

Let’s consider the following example.

from scipy.special import exp10
res = exp10([2, 9])
print res

The above program will produce the following output.

[1.00000000e+02 1.00000000e+09]

Relative Error Exponential Function

The syntax of this function is – scipy.special.exprel(x). It generates the relative error exponential, (exp(x) – 1)/x.

As x approaches zero, exp(x) approaches 1, so numerical evaluation of exp(x)-1 suffers from catastrophic loss of precision. The implementation of exprel(x) is designed to avoid this loss of precision, which occurs when x approaches zero.

Let’s consider the following example.

from scipy.special import exprel
res = exprel([-0.25, -0.1, 0, 0.1, 0.25])
print res

The above program will produce the following output.

[0.88479687 0.95162582 1. 1.05170918 1.13610167]

Logarithm and Exponential Functions

The syntax of this function is – scipy.special.logsumexp(x). It helps calculate the logarithm of the sum of the exponentials of the input elements.

Let’s consider the following example.

from scipy.special import logsumexp
import numpy as np
a = np.arange(10)
res = logsumexp(a)
print res

The above program will produce the following output.

9.45862974443

Lambert Function

The syntax for this function is – scipy.special.lambertw(x). It is also known as the Lambert W function. The Lambert W function, W(z), is defined as the inverse of w*exp(w). In other words, for any complex number z, the value of W(z) is such that z = W(z) * exp(W(z)).

The Lambert-W function is a multivalued function with infinitely many branches. Each branch gives a separate solution to the equation z = w exp(w). Here, the branches are indexed by the integer k.

Let’s consider the following example. Here, the Lambert-W function is the inverse of w exp(w).

from scipy.special import lambertw
w = lambertw(1)
print w
print w * np.exp(w)

The above program will produce the following output.

(0.56714329041+0j)
(1+0j)

Permutations and Combinations

Let us discuss permutations and combinations separately to understand them clearly.

Combinations – The syntax of the combinatorial function is – scipy.special.comb(N,k). Let’s consider the following example: –

from scipy.special import comb
res = comb(10, 3, exact = False, repetition=True)
print res

The above program will produce the following output.

220.0

Note – The array argument only accepts exact = False. If k > N, N < 0, or k < 0, then 0 will be returned.

Permutations and Combinations – The syntax for the permutation function is – scipy.special.perm(N,k). Permutations and combinations of N items are k permutations of N at a time, also known as “partial permutations and combinations.”

Let’s consider the following example.

from scipy.special import perm
res = perm(10, 3, exact = True)
print res

The above program will produce the following output.

720

Gamma Function

The gamma function is often called the generalized factorial because z*gamma(z) = gamma(z+1), and gamma(n+1) = n!, for a natural number ‘n’.

The syntax for the combination function is – scipy.special.gamma(x). Taking k permutations of N at a time is called k-permutations of N, which is also called a “partial permutation”.

The syntax for the combination function is – scipy.special.gamma(x). Each time, we take N permutations of N, which is called k permutations of N. This is also called a “partial permutation.”

from scipy.special import gamma
res = gamma([0, 0.5, 1, 5])
print res

The above program will produce the following output.

[inf 1.77245385 1. 24.]

Leave a Reply

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