python function gamma_Python math gamma()用法及代码示例

Python用其语言允许进行各种数学运算,这在科学领域具有多种应用。这样的Python产品之一就是内置的gamma()函数,该函数以数字方式计算函数中传递的数字的伽玛值。

用法:math.gamma(x)

参数:

x:The number whose gamma value needs to be computed.

返回值:伽玛值,在数值上等于“factorial(x-1)”。

代码1:演示gamma()的工作

# Python code to demonstrate

# working of gamma()

import math

# initializing argument

gamma_var = 6

# Printing the gamma value.

print ("The gamma value of the given argument is:"

+ str(math.gamma(gamma_var)))

输出:

The gamma value of the given argument is:120.0

factorial()和gamma()

伽玛值也可以使用factorial(x-1),但用例gamma()因为如果我们比较这两个功能以完成相似的任务,gamma()提供更好的性能。

代码2:比较中factorial()和gamma()

# Python code to demonstrate

# factorial() vs gamma()

import math

import time

# initializing argument

gamma_var = 6

# checking performance

# gamma() vs factorial()

start_fact = time.time()

res_fact = math.factorial(gamma_var-1)

print ("The gamma value using factorial is:"

+ str(res_fact))

print ("The time taken to compute is:"

+ str(time.time() - start_fact))

print ('\n')

start_gamma = time.time()

res_gamma = math.gamma(gamma_var)

print ("The gamma value using gamma() is:"

+ str(res_gamma))

print ("The time taken to compute is:"

+ str(time.time() - start_gamma))

输出:

The gamma value using factorial is:120

The time taken to compute is:9.059906005859375e-06

The gamma value using gamma() is:120.0

The time taken to compute is:5.245208740234375e-06

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值