Numpy 的 cross(~)
方法計算兩個輸入數組的叉積。
參數
1. a
| array-like
第一個輸入數組。
2. b
| array-like
第二個輸入數組。
3. axisa
| int
| optional
用於計算數組 a
叉積的軸。僅當您處理多維數組(例如二維數組)時才需要考慮這一點。默認情況下,將使用最後一個軸。
4. axisb
| int
| optional
用於計算數組 b
叉積的軸。僅當您處理多維數組(例如二維數組)時才需要考慮這一點。默認情況下,將使用最後一個軸。
返回值
表示兩個輸入數組的叉積的 Numpy 數組。
例子
基本用法
x = [1, 2, 3]
y = [4, 5, 6]
np.cross(x, y)
array([-3, 6, -3])
size-two 數組的叉積
我們還可以計算大小為 2 的數組的叉積:
x = [1, 2]
y = [3, 4]
np.cross(x,y)
array(-2)
在這裏,我們計算了點積 (1*4)-(2*3)=-2
。
size-two 數組和 size-three 數組的叉積
x = [1, 2, 3]
y = [4, 5]
np.cross(x,y)
array([-15, 12, -3])
這裏假設y
為[4, 5, 0]
,即z-component補0。
二維數組的叉積
x = [[1,2,3], [4,5,6]]
y = [[5,6], [7,8], [9,10]]
np.cross(x, y, axisa=1, axisb=0) # axis=1 <- row wise, axis=0 <- column-wise
array([[-3, 6, -3],
[ 2, -4, 2]])
為了方便您的觀看,這裏對 x
和 y
進行了美化:
x = [[1, 2, 3], y = [[5, 6]
[4, 5, 6]] [7, 8]
[9, 10]]
我們在這裏所做的計算如下:
np.cross([1,2,3], [5,7,9]) # [-3, 6, -3]
np.cross([4,5,6], [6,8,10]) # [ 2, -4, 2]
相關用法
- Python Pandas crosstab方法用法及代碼示例
- Python cudf.core.column.string.StringMethods.is_vowel用法及代碼示例
- Python NumPy char find方法用法及代碼示例
- Python cudf.Series.ceil用法及代碼示例
- Python cudf.core.column.string.StringMethods.endswith用法及代碼示例
- Python cuxfilter.charts.datashader.heatmap用法及代碼示例
- Python cusignal.windows.windows.hann用法及代碼示例
- Python cudf.DataFrame.isin用法及代碼示例
- Python cudf.core.column.string.StringMethods.title用法及代碼示例
- Python codecs.decode()用法及代碼示例
- Python cuml.metrics.pairwise_distances.pairwise_distances用法及代碼示例
- Python collections.somenamedtuple._replace用法及代碼示例
- Python cuxfilter.charts.panel_widgets.int_slider用法及代碼示例
- Python cudf.DataFrame.rmul用法及代碼示例
- Python cudf.Series.max用法及代碼示例
- Python cmp()用法及代碼示例
- Python cudf.DatetimeIndex.dayofweek用法及代碼示例
- Python cudf.DataFrame.apply用法及代碼示例
- Python cucim.skimage.feature.shape_index用法及代碼示例
- Python cuml.neighbors.KNeighborsClassifier用法及代碼示例
- Python cudf.core.column.string.StringMethods.contains用法及代碼示例
- Python cuxfilter.charts.datashader.line用法及代碼示例
- Python cudf.core.column.string.StringMethods.rsplit用法及代碼示例
- Python cudf.DataFrame.exp用法及代碼示例
- Python cudf.Series.head用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | cross method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。