Special functions in SciPy (original) (raw)

SciPy provides special mathematical functions through **scipy.special module. These functions include advanced computations like gamma functions, Bessel functions, error functions, beta functions etc. that are commonly used in scientific, statistical and engineering applications.

Commonly used functions in **scipy.special:

Let's understand about these functions in detail.

**1. cbrt

**cbrt() function computes cube root of a number or an array of numbers.

**Syntax:

scipy.special.cbrt(x)

**Parameter: x is a single number or a list/array.

**Example:

Python `

from scipy.special import cbrt

print(cbrt(64))
print(cbrt(78))

`

**Output

4.0
4.272658681697917

**2. comb

**comb() function calculates number of combinations, i.e., how many ways you can choose k items from N without regard to order.

**Syntax:

scipy.special.comb(N, k)

**Parameter:

**Example 1: Simple Combination

Python `

from scipy.special import comb print(comb(4, 1))

`

**Output

4.0

**Example 2: Multiple Combinations

Python `

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()

**exp10() function computes 10 raised to the power of the given input. It is equivalent to writing 10 ** x.

**Syntax:

scipy.special.exp10(x)

**Parameter: x is a exponent value (a number or array)

**Example 1: Power of 10 for a Single Number

Python `

from scipy.special import exp10 print(exp10(2))

`

**Output

100.0

**Example 2: Powers of 10 for a Range of Values

Python `

from scipy.special import exp10 for i in range(1, 6): print(exp10(i))

`

**Output

10.0
100.0
1000.0
10000.0
100000.0

**4. exprel()

exprel() function calculates exponential result used when input is close to zero. It helps avoid small calculation errors that can occur when using the standard exponential function (exp) near zero.

**Syntax:

scipy.special.exprel(x)

**Parameter: x is a input number (a single value or a list/array)

**Example:

Python `

from scipy.special import exprel print(exprel(0))

`

**Output

1.0

**5. gamma()

**gamma() function is a generalization of the factorial function. For natural numbers, it behaves like a factorial:
gamma(n+1) = n!

**Syntax:

scipy.special.gamma(x)

**Parameter: x is a input value (a number or list of numbers)

**Example:

Python `

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

`

**Output

1.2696403353658055e+73

**6. lambertw()

lambertw() function solve equations where variable appears both in the base and in the exponent. It is used when dealing with exponential and logarithmic expressions.

**Syntax:

scipy.special.lambertw(x)

**Parameter: x is a input value (real or complex)

**Example:

Python `

from scipy.special import lambertw print(lambertw(5))

`

**Output

(1.3267246652422002+0j)

**7. logsumexp()

**logsumexp() function compute logarithm of the sum of exponentials of input values. Used in numerical computations to maintain stability when working with very large or very small numbers.

**Syntax:

scipy.special.logsumexp(x)

**Parameter: x is a input list, array, or iterable of numbers

**Example 1: Basic Usage

Python `

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

`

**Output

10.45862974442671

**Example 2: With Two Lists

Python `

from scipy.special import logsumexp

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] b = [10, 11, 12, 13, 14, 15] print(logsumexp(a), logsumexp(b))

`

**Output

10.45862974442671 15.456193316018123

**8. perm()

**perm() function calculates number of permutations of k items chosen from N items. It consider order of selection.

**Syntax:

scipy.special.perm(N, k)

**Parameter:

**Example:

Python `

from scipy.special import perm print([perm(4, 1), perm(4, 2), perm(4, 3), perm(4, 4), perm(4, 5)])

`