當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.ldexp()用法及代碼示例

在Python中,numpy.ldexp(arr1, arr2[, out]) 函數按元素返回arr1 *(2 ** arr2)。這也稱為numpy.frexp()函數的逆函數。

Synatx: numpy.ldexp()

參數:
arr1: [array_like] Array of multipliers.
arr2: [array_like, int] Array of twos exponents.
out: [ndarray, optional] Output array for the result.



返回: [ndarray, scalar] Return the result of arr1 * (2**arr2). This is a scalar if both arr1 and arr2 are scalars.

代碼1:

# 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)))
輸出:
[  6.  12.  24.  48.]
[ -8. -16. -32. -64.]
[  5.2  10.4  20.8]
[ -3.2  -6.4 -12.8]


代碼2:不支持複雜的數據類型,它們會引發TypeError。

# 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


相關用法


注:本文由純淨天空篩選整理自sanjoy_62大神的英文原創作品 numpy.ldexp() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。