📜  SciPy 中的特殊函数

📅  最后修改于: 2022-05-13 01:54:48.898000             🧑  作者: Mango

SciPy 中的特殊函数

在本文中,我们将了解 Scipy 中的特殊函数。 scipy 中的特殊函数用于对给定数据执行数学运算。 scipy 中的特殊函数是 scipy 包中的一个模块。在这个特殊的函数,可用的方法有:

  • cbrt – 给出给定数字的立方根
  • 梳子——给出元素的组合
  • exp10 – 给出给定数字的 10 次幂的数字
  • exprel – 给出相对误差指数,(exp(x) – 1)/x。
  • gamma – 通过计算自然数“n”的 z*gamma(z) = gamma(z+1) 和 gamma(n+1) = n! 来返回值。
  • Lambertw – 计算任何复数 z 的 W(z) * exp(W(z)),其中 W 是 Lambertw函数
  • logsumexp – 给出给定数字的指数总和的对数
  • perm – 给出元素的排列

让我们详细了解这些功能。

1. cbrt()

这用于返回给定数字的立方根。

示例:求立方根的程序



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


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)


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


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


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


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


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


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


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


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


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


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


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


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


输出:

4.0
4.272658681697917
5.039684199579493

示例:在给定数组元素中查找立方根的程序。

蟒蛇3

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)

输出:

2. 梳()

它被称为组合并返回给定值的组合。

示例 1:

蟒蛇3

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

输出:

4.0

示例 2:

蟒蛇3

# 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)])

输出:

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

3. exp10()

此方法给出了给定数字的 10 次幂的数字。

示例:计算 10 的幂的程序

蟒蛇3

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

输出:

100.0

示例:计算范围的 10 次幂的程序

蟒蛇3

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

输出:

10.0
100.0
1000.0
10000.0
100000.0
1000000.0
10000000.0
100000000.0
1000000000.0

4. exprel()

它被称为相对误差指数函数。它返回给定变量的错误值。如果 x 接近零,则 exp(x) 接近 1。

示例 1:

蟒蛇3



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

输出:

1.0

示例 2:

蟒蛇3

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

输出:

5.伽玛()

它被称为 Gamma函数。它是广义阶乘,因为 z*gamma(z) = gamma(z+1) 和 gamma(n+1) = n!,对于自然数 'n'。

示例 1:

蟒蛇3



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

输出:

1.2696403353658278e+73

示例 2:

蟒蛇3

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

输出:

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

6.lambertw()

它也被称为兰伯特函数。对于任何复数 z,它计算 W(z) 的值使得 z = W(z) * exp(W(z)),其中 W 称为兰伯特函数

例子:

蟒蛇3

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

输出:

7.logsumexp()

它被称为对数总和指数函数。它将返回输入元素指数总和的对数。

示例 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))

输出:

10.45862974442671

示例 2:

蟒蛇3

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

输出:

[10.45862974442671, 15.456193316018123]

8.烫发()

perm 代表排列。它将返回给定数字的排列。



例子:

蟒蛇3

# 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)])

输出:

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