Python中的 numpy.ldexp()
在Python中,numpy.ldexp(arr1, arr2[, out])函数按元素返回 arr1 * (2**arr2)。这也称为 numpy.freexp() 函数的逆函数。
Syntax: numpy.ldexp()
Parameters:
arr1: [array_like] Array of multipliers.
arr2: [array_like, int] Array of twos exponents.
out: [ndarray, optional] Output array for the result.
Returns: [ndarray, scalar] Return the result of arr1 * (2**arr2). This is a scalar if both arr1 and arr2 are scalars.
代码#1:
Python3
# Python program explaining
# numpy.ldexp() method
# importing numpy
import numpy as geek
# ldexp() Function on + ve nd -ve Numbers
print(geek.ldexp(6, geek.arange(4)))
print(geek.ldexp(-8, geek.arange(4)))
# ldexp() Function on fractional Number
print(geek.ldexp(5.2, geek.arange(3)))
print(geek.ldexp(-3.2, geek.arange(3)))
Python3
# Python program explaining
# numpy.ldexp() method
# importing numpy
import numpy as geek
# ldexp() Function on complex dtypes
print(geek.ldexp(-5 + 9J, geek.arange(4)))
输出:
[ 6. 12. 24. 48.]
[ -8. -16. -32. -64.]
[ 5.2 10.4 20.8]
[ -3.2 -6.4 -12.8]
代码 #2:不支持复杂的数据类型,它们会引发TypeError。
Python3
# Python program explaining
# numpy.ldexp() method
# importing numpy
import numpy as geek
# ldexp() Function on complex dtypes
print(geek.ldexp(-5 + 9J, geek.arange(4)))
输出:
TypeError: ufunc 'ldexp' not supported for the input types