本文簡要介紹 python 語言中 numpy.left_shift
的用法。
用法:
numpy.left_shift(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True [, signature, extobj ]) = <ufunc 'left_shift'>
將整數的位向左移動。
通過附加位向左移動x2右邊的 0x1。由於數字的內部表示是二進製格式,因此該運算相當於乘法x1經過
2**x2
.- x1: 整數類型的數組
輸入值。
- x2: 整數類型的數組
要附加的零數x1。必須是非負數。如果
x1.shape != x2.shape
,它們必須可以廣播到一個共同的形狀(成為輸出的形狀)。- out: ndarray,None,或 ndarray 和 None 的元組,可選
存儲結果的位置。如果提供,它必須具有輸入廣播到的形狀。如果未提供或 None,則返回一個新分配的數組。元組(隻能作為關鍵字參數)的長度必須等於輸出的數量。
- where: 數組,可選
此條件通過輸入廣播。在條件為真的位置,out數組將設置為 ufunc 結果。在其他地方,out數組將保留其原始值。請注意,如果未初始化out數組是通過默認創建的
out=None
,其中條件為 False 的位置將保持未初始化狀態。- **kwargs:
對於其他僅關鍵字參數,請參閱 ufunc 文檔。
- out: 整數類型數組
返回x1位移位x2次向左。如果兩者都是,則這是一個標量x1和x2是標量。
參數:
返回:
例子:
>>> np.binary_repr(5) '101' >>> np.left_shift(5, 2) 20 >>> np.binary_repr(20) '10100'
>>> np.left_shift(5, [1,2,3]) array([10, 20, 40])
請注意,第二個參數的 dtype 可能會更改結果的 dtype,並且在某些情況下可能導致意外結果(請參閱強製轉換規則):
>>> a = np.left_shift(np.uint8(255), 1) # Expect 254 >>> print(a, type(a)) # Unexpected result due to upcasting 510 <class 'numpy.int64'> >>> b = np.left_shift(np.uint8(255), np.uint8(1)) >>> print(b, type(b)) 254 <class 'numpy.uint8'>
<<
運算符可用作 ndarray 上np.left_shift
的簡寫。>>> x1 = 5 >>> x2 = np.array([1, 2, 3]) >>> x1 << x2 array([10, 20, 40])
相關用法
- Python numpy legendre.legint用法及代碼示例
- Python numpy legendre.legmulx用法及代碼示例
- Python numpy less用法及代碼示例
- Python numpy legendre.legroots用法及代碼示例
- Python numpy legendre.legdomain用法及代碼示例
- Python numpy legendre.legfromroots用法及代碼示例
- Python numpy legendre.leg2poly用法及代碼示例
- Python numpy legendre.legone用法及代碼示例
- Python numpy legendre.poly2leg用法及代碼示例
- Python numpy legendre.legdiv用法及代碼示例
- Python numpy legendre.legmul用法及代碼示例
- Python numpy legendre.legadd用法及代碼示例
- Python numpy legendre.legline用法及代碼示例
- Python numpy lexsort用法及代碼示例
- Python numpy legendre.legx用法及代碼示例
- Python numpy legendre.legtrim用法及代碼示例
- Python numpy legendre.legfit用法及代碼示例
- Python numpy legendre.legsub用法及代碼示例
- Python numpy less_equal用法及代碼示例
- Python numpy legendre.legder用法及代碼示例
- Python numpy legendre.legzero用法及代碼示例
- Python numpy linalg.svd用法及代碼示例
- Python numpy laguerre.lagone用法及代碼示例
- Python numpy linalg.pinv用法及代碼示例
- Python numpy logaddexp用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.left_shift。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。