📜  Python中的 numpy.ldexp()

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

Python中的 numpy.ldexp()

在Python中,numpy.ldexp(arr1, arr2[, out])函数按元素返回 arr1 * (2**arr2)。这也称为 numpy.freexp() 函数的逆函数。

代码#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