Python Math.expm1()方法

在Python中,我们利用数学函数,使用math 模块,其中包含各种对数函数。其中一个方法是math.expm1() ,它与math.exp() 非常相似。

math.exp(x) 方法使用ex,而math.expm1() 使用ex-1。这个方法在Python 3.2和更高版本中可用。

math.expm1() 的操作是全精度的。参数x 可能是一个比exp(x)-1 小的数字,提供了一个明显的精度损失。

方法expm1() 处理这种情况,并以全精度返回结果。e 的值大约是2.7182,这是自然对数的基数。

计算器如何解决?假设x 的值接近于0,那么输出结果大约等于1+x ,因为表达式ex-1/x 等于0。

Pythonmath.expm1() 方法的语法

math.expm1(number)

参数

number 必需的参数,有一个浮动的值。我们计算这个数字的指数减去1。

返回

该方法返回所提供的参数的指数值,结果为-1

示例代码:使用math.expm1() 方法

要使用expm1() 方法,我们必须导入math 模块。我们将四种不同的数据类型作为参数传递给math.expm1() 方法。

该方法显示,负数(整数或浮点数)的结果是负数。该方法需要在Python 3.2或更高版本中运行。

# use math import for the use of the expm1() method
import math
# method return results differently for the data
def check_input(number):
    return math.expm1(number)
# test method for four types of data
positive_integer = 6
negative_integer = -8
positive_decimal_number = 14.5
negative_decimal_number = -9.3
print ("The expm1 method returns the value for the positive integer "
                    + str(check_input(positive_integer)))
print ("The expm1 method returns the value for the negative integer "
                    + str(check_input(negative_integer)))
print ("The expm1 method returns the value for the positive decimal number "
                    + str(check_input(positive_decimal_number)))
print ("The expm1 method returns the value for the negative decimal number "
                    + str(check_input(negative_decimal_number)))

输出:

# tests four types of data here
The expm1 method returns the value for the positive integer 402.4287934927351
The expm1 method returns the value for the negative integer -0.9996645373720975
The expm1 method returns the value for the positive decimal number 1982758.2635375687
The expm1 method returns the value for the negative decimal number -0.9999085757685219

示例代码:在math.expm1()math.exp()-1

正如我们讨论的那样,math.expm1() 方法的结果是exp()-1 。所以,这里要证明的是,如果math.expm1() 方法做了exp()-1 ,那么这个方法的基本功能是什么?

原因之一是该方法math.expm1() ,在许多数学和科学应用中经常被使用。第二个原因是当x (给定参数)的值很小时,那么与exp()-1 相比,方法expm1() 提供了更好的结果。

import math
# test the value to compare with both methods
test_value = 2e-14
print ("The result with exp()-1 method is " + str(math.exp(test_value)-1))
print ("The result with expm1() method is " + str(math.expm1(test_value)))

输出:

# method math.expm1() handles the precision more accurately
The result with exp()-1 method is 1.9984014443252818e-14
The result with expm1() method is 2.00000000000002e-14

示例代码:同样的方法有numpy.expm1()

这个方法提供了比exp(x)-1 方法更好的精度,因为x 的值很小。x 的值不是一个单一的值,而是一个类似数组的参数。

numpy.expm1() 方法有不止一个参数,如out (用于存储结果),where (可选的bool值),等等。numpy 模块同时包含exp()expm1() 方法,分别用于单值和数组值。

import numpy as np
# array method for expm1() method.
def numpy_array(x):
    return np.expm1(x)
test_value = 1e-11
print ("The result with expm1() method is " , numpy_array(test_value))

输出:

The result with expm1() method is  1.000000000005e-11

示例代码:计算log(1-exp(x)) 的表达式

方法logarithm() 提供了一种方法来找到log(1-exp(x)) 表达式的数值稳定的计算方法。该方法通过使用math.log(-math.expm1(x)) 方程来计算log(1-exp(x))

import math
# finds the numerically stable computation of log(1-exp(x)) expression
def logarithm(x):
# below comparison returns True. The rest of them are not compared.
  if x < -1:
    return math.log1p(-math.exp(x))
  elif x < 0:
    return math.log(-math.expm1(x))
  elif x == 0:
    return -np.inf
# input number must be non-positive; otherwise, else part will be returned.
  else:
    raise ValueError("Non-positive argument is required")
print("The mumerically stable computation of log(1-exp(x)) is",logarithm(-1e2))

输出:

The mumerically stable computation of log(1-exp(x)) is  -3.720075976020836e-44

示例代码:计算log(1-expm1(x)) 表达式

方法stable_implementation() 提供了一种方法来找到log(1-expm1(x)) 表达式的数值稳定计算。该方法使用math.log(-math.expm1(x)) 方程来计算log(1-expm1(x))

该方法也接受负数作为参数;否则,它返回的结果不是一个数字。

import math
# this is a more stable implementation than the previous example
def stable_implementation(num):
    # returns Nan, if the comparison shows True value
    if num >= 0.:
        return "Not a number"
    elif num > 0:
        return math.log(-math.expm1(num))
    else:
        return math.log1p(-math.exp(num))
print("The numerically stable implementation of `log(1 - exp(val)) is",stable_implementation(-4e-2))

输出:

The numerically stable implementation of `log(1 - exp(val)) is -3.2388091590903993