Question

I am looking for the Python numpy equivalent of the IDL # operator. Here is what the # operator does:

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.

Here are the numpy arrays I am dealing with:

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

and

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

Also, numpy.dot(A,B) results in ValueError: matrices are not aligned.

Was it helpful?

Solution

Reading the notes on IDL's definition of matrix multiplication, it seems they use the opposite notation to everyone else:

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

So # can be achieved by the rather strange looking:

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

from their example values:

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)

gives

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

OTHER TIPS

If I'm correct you want matrix multiplication.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top