本文簡要介紹 python 語言中 numpy.cross
的用法。
用法:
numpy.cross(a, b, axisa=- 1, axisb=- 1, axisc=- 1, axis=None)
返回兩個(數組)向量的叉積。
的叉積a和b在
是垂直於兩者的向量a和b.如果a和b是向量數組,向量由最後一個軸定義a和b默認情況下,這些軸的維度可以是 2 或 3。a或者b為 2,假設輸入向量的第三個分量為零,並據此計算叉積。如果兩個輸入向量的維度都為 2,則返回叉積的 z-component。- a: array_like
第一個向量的分量。
- b: array_like
第二個向量的分量。
- axisa: 整數,可選
定義向量的 a 的軸。默認情況下,最後一個軸。
- axisb: 整數,可選
定義向量的 b 軸。默認情況下,最後一個軸。
- axisc: 整數,可選
包含叉積向量的 c 軸。如果兩個輸入向量的維度都為 2,則忽略,因為返回是標量。默認情況下,最後一個軸。
- axis: 整數,可選
如果已定義,則定義向量和叉積的 a、b 和 c 軸。覆蓋axisa、axisb和axisc。
- c: ndarray
矢量叉積。
- ValueError
當 a 和/或 b 中向量的維度不等於 2 或 3 時。
參數:
返回:
拋出:
注意:
支持輸入的完全廣播。
例子:
矢量cross-product。
>>> x = [1, 2, 3] >>> y = [4, 5, 6] >>> np.cross(x, y) array([-3, 6, -3])
一個維數為 2 的向量。
>>> x = [1, 2] >>> y = [4, 5, 6] >>> np.cross(x, y) array([12, -6, -3])
等效地:
>>> x = [1, 2, 0] >>> y = [4, 5, 6] >>> np.cross(x, y) array([12, -6, -3])
兩個維度為 2 的向量。
>>> x = [1,2] >>> y = [4,5] >>> np.cross(x, y) array(-3)
多個向量cross-products。請注意,叉積向量的方向由右手定則定義。
>>> x = np.array([[1,2,3], [4,5,6]]) >>> y = np.array([[4,5,6], [1,2,3]]) >>> np.cross(x, y) array([[-3, 6, -3], [ 3, -6, 3]])
可以使用 axisc 關鍵字更改 c 的方向。
>>> np.cross(x, y, axisc=0) array([[-3, 3], [ 6, -6], [-3, 3]])
使用axisa 和axisb 更改x 和y 的向量定義。
>>> x = np.array([[1,2,3], [4,5,6], [7, 8, 9]]) >>> y = np.array([[7, 8, 9], [4,5,6], [1,2,3]]) >>> np.cross(x, y) array([[ -6, 12, -6], [ 0, 0, 0], [ 6, -12, 6]]) >>> np.cross(x, y, axisa=0, axisb=0) array([[-24, 48, -24], [-30, 60, -30], [-36, 72, -36]])
相關用法
- Python numpy chararray.ndim用法及代碼示例
- Python numpy chebyshev.chebsub用法及代碼示例
- Python numpy chararray.nbytes用法及代碼示例
- Python numpy chebyshev.chebdiv用法及代碼示例
- Python numpy copy用法及代碼示例
- Python numpy chararray.setflags用法及代碼示例
- Python numpy chararray.flat用法及代碼示例
- Python numpy can_cast用法及代碼示例
- Python numpy chararray.strides用法及代碼示例
- Python numpy chebyshev.cheb2poly用法及代碼示例
- Python numpy chararray.view用法及代碼示例
- Python numpy chebyshev.chebx用法及代碼示例
- Python numpy chebyshev.chebmul用法及代碼示例
- Python numpy char.strip用法及代碼示例
- Python numpy c_用法及代碼示例
- Python numpy chebyshev.chebroots用法及代碼示例
- Python numpy copysign用法及代碼示例
- Python numpy char.upper用法及代碼示例
- Python numpy chararray.imag用法及代碼示例
- Python numpy chararray.base用法及代碼示例
- Python numpy chararray.flatten用法及代碼示例
- Python numpy chararray.copy用法及代碼示例
- Python numpy clip用法及代碼示例
- Python numpy char.count用法及代碼示例
- Python numpy char.chararray用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.cross。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。