4

我正在尋找等效於IDL#操作員的Python numpy。 這裏是# operator做什麼:什麼是IDL#運算符的Python numpy等價物?

Computes array elements by multiplying the columns of the first array by the rows of the second array. The second array must have the same number of columns as the first array has rows. The resulting array has the same number of columns as the first array and the same number of rows as the second array.

這裏是numpy數組我處理:

A = [[ 0.9826128 0.   0.18566662] 
    [ 0.   1.   0.  ] 
    [-0.18566662 0.   0.9826128 ]] 

B = [[ 1.   0.   0.  ] 
    [ 0.62692564 0.77418869 0.08715574]] 

此外,numpy.dot(A,B)結果ValueError: matrices are not aligned

+1

您正在考慮與IDL相同的行列排序。 NumPy遵循數學標準,第一個矩陣中的列數必須等於第二個矩陣中的行數。因此,下面的所有轉換都是「奇怪」的表達。 – mgalloy

回答

1

閱讀矩陣乘法的IDL定義中的音符,似乎他們用相反的符號給其他人:

IDL’s convention is to consider the first dimension to be the column and the second dimension to be the row

因此#可以用很奇怪的看着來實現:

numpy.dot(A.T, B.T).T

從它們的示例值:

import numpy as np 
A = np.array([[0, 1, 2], [3, 4, 5]]) 
B = np.array([[0, 1], [2, 3], [4, 5]]) 
C = np.dot(A.T, B.T).T 
print(C) 

給出

[[ 3 4 5] 
[ 9 14 19] 
[15 24 33]] 
+2

'sharp = lambda x,y:np.dot(x.T,y.T).T' then'sharp(A,B)'for ** IDL#** – emeth

0

如果我沒有錯,你想要matrix multiplication

+1

不完全是因爲'numpy.dot(A,B)'導致'ValueError:矩陣不對齊'。 –

相關問題