Scipy 简明教程
SciPy - Special Package
特殊包中提供的函数是通用函数,遵循广播和自动数组循环。
The functions available in the special package are universal functions, which follow broadcasting and automatic array looping.
让我们看看一些最常用的特殊函数 −
Let us look at some of the most frequently used special functions −
-
Cubic Root Function
-
Exponential Function
-
Relative Error Exponential Function
-
Log Sum Exponential Function
-
Lambert Function
-
Permutations and Combinations Function
-
Gamma Function
我们现在简要了解一下这些函数中的每一个函数。
Let us now understand each of these functions in brief.
Cubic Root Function
此三次方根函数的语法为 – scipy.special.cbrt(x)。这将获取 x 的按元素立方根。
The syntax of this cubic root function is – scipy.special.cbrt(x). This will fetch the element-wise cube root of x.
让我们考虑以下示例。
Let us consider the following example.
from scipy.special import cbrt
res = cbrt([10, 9, 0.1254, 234])
print res
上述程序将生成以下输出。
The above program will generate the following output.
[ 2.15443469 2.08008382 0.50053277 6.16224015]
Exponential Function
指数函数的语法为 – scipy.special.exp10(x)。这将按元素计算 10**x。
The syntax of the exponential function is – scipy.special.exp10(x). This will compute 10**x element wise.
让我们考虑以下示例。
Let us consider the following example.
from scipy.special import exp10
res = exp10([2, 9])
print res
上述程序将生成以下输出。
The above program will generate the following output.
[1.00000000e+02 1.00000000e+09]
Relative Error Exponential Function
此函数的语法为 – scipy.special.exprel(x)。它生成相对误差指数,(exp(x) - 1)/x。
The syntax for this function is – scipy.special.exprel(x). It generates the relative error exponential, (exp(x) - 1)/x.
当 x 接近零时,exp(x) 接近 1,因此 exp(x) - 1 的数值计算会遭受精度灾难性损失。然后实现 exprel(x) 以避免当 x 接近零时发生的精度损失。
When x is near zero, exp(x) is near 1, so the numerical calculation of exp(x) - 1 can suffer from catastrophic loss of precision. Then exprel(x) is implemented to avoid the loss of precision, which occurs when x is near zero.
让我们考虑以下示例。
Let us 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 generate the following output.
[0.88479687 0.95162582 1. 1.05170918 1.13610167]
Log Sum Exponential Function
此函数的语法为 – scipy.special.logsumexp(x)。它有助于计算输入元素指数之和的对数。
The syntax for this function is – scipy.special.logsumexp(x). It helps to compute the log of the sum of exponentials of input elements.
让我们考虑以下示例。
Let us 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 generate the following output.
9.45862974443
Lambert Function
此函数的语法为 – scipy.special.lambertw(x)。它也被称为 Lambert W 函数。Lambert W 函数 W(z) 定义为 w * exp(w) 的反函数。换句话说,对于任何复数 z,W(z) 的值满足 z = W(z) * exp(W(z))。
The syntax for this function is – scipy.special.lambertw(x). It is also called as the Lambert W function. The Lambert W function W(z) is defined as the inverse function of w * exp(w). In other words, the value of W(z) is such that z = W(z) * exp(W(z)) for any complex number z.
Lambert W 函数是一个多分支函数,具有无限多个分支。每个分支给出方程 z = w exp(w) 的一个解。此处,分支由整数 k 索引。
The Lambert W function is a multivalued function with infinitely many branches. Each branch gives a separate solution of the equation z = w exp(w). Here, the branches are indexed by the integer k.
我们考虑以下示例。这里,Lambert W 函数是 w exp(w) 的反函数。
Let us 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 generate the following output.
(0.56714329041+0j)
(1+0j)
Permutations & Combinations
让我们先讨论排列和组合,以便能清楚地理解它们。
Let us discuss permutations and combinations separately for understanding them clearly.
Combinations − 组合函数的语法是 - scipy.special.comb(N,k)。让我们考虑以下示例 -
Combinations − The syntax for combinations function is – scipy.special.comb(N,k). Let us consider the following example −
from scipy.special import comb
res = comb(10, 3, exact = False,repetition=True)
print res
上述程序将生成以下输出。
The above program will generate the following output.
220.0
Note − 仅在 exact = False 的情况下接受数组参数。如果 k > N、N < 0 或 k < 0,则返回 0。
Note − Array arguments are accepted only for exact = False case. If k > N, N < 0, or k < 0, then a 0 is returned.
Permutations − 排列函数的语法是 - scipy.special.perm(N,k)。一次对 N 个对象进行 k 个排列,即 N 的 k 个排列。这也称为“部分排列”。
Permutations − The syntax for combinations function is – scipy.special.perm(N,k). Permutations of N things taken k at a time, i.e., k-permutations of N. This is also known as “partial permutations”.
让我们考虑以下示例。
Let us consider the following example.
from scipy.special import perm
res = perm(10, 3, exact = True)
print res
上述程序将生成以下输出。
The above program will generate the following output.
720
Gamma Function
伽马函数通常被称为广义阶乘,因为 z*gamma(z) = gamma(z+1) 并且 gamma(n+1) = n!,其中“n”是自然数。
The gamma function is often referred to as the generalized factorial since z*gamma(z) = gamma(z+1) and gamma(n+1) = n!, for a natural number ‘n’.
组合函数的语法是 - scipy.special.gamma(x)。一次对 N 个对象进行 k 个排列,即 N 的 k 个排列。这也称为“部分排列”。
The syntax for combinations function is – scipy.special.gamma(x). Permutations of N things taken k at a time, i.e., k-permutations of N. This is also known as “partial permutations”.
组合函数的语法是 - scipy.special.gamma(x)。一次对 N 个对象进行 k 个排列,即 N 的 k 个排列。这也称为“部分排列”。
The syntax for combinations function is – scipy.special.gamma(x). Permutations of N things taken k at a time, i.e., k-permutations of N. This is also known as “partial permutations”.
from scipy.special import gamma
res = gamma([0, 0.5, 1, 5])
print res
上述程序将生成以下输出。
The above program will generate the following output.
[inf 1.77245385 1. 24.]