Dadep
Dadep

Reputation: 2788

Equivalent of numpy.dot (python) in Fortran

I have code in python that I need to "translate" to Fortran (that I don't know that much....)

I have :

>>>Mat1
array([[ 0.2],
       [ 0.4],
       [-0.2],
       [-0.8]])
>>> X
array([[0, 0, 1, 1],
       [0, 1, 1, 0],
       [1, 0, 1, 0],
       [1, 1, 1, 1]])

Then I do :

Result=np.dot(X,Mat1)

I would like to do the equivalent in Fortran

REAL(8), DIMENSION(4,1)::Mat1
REAL(8), DIMENSION(4,4)::X

X(:, 1)=(/0, 0, 1, 1/)
X(:, 2)=(/0, 1, 1, 0/)
X(:, 3)=(/1, 0, 1, 0/)
X(:, 4)=(/1, 1, 1, 1/)

Mat1(:,1)=(/0.2,0.4,-0.2,-0.8/) 

But in this case numpy.dot is not really doing a dot product... I don't know if I should use DOT_PRODUCT or MATMUL. Things are really unclear for me.

Upvotes: 0

Views: 441

Answers (1)

Arya McCarthy
Arya McCarthy

Reputation: 8829

For this, MATMUL is the way you want to go. See here. DOT_PRODUCT is only for vectors. MATMUL can handle any matrices whose dimensions allow matrix multiplication.

In your example, your matrices don't have matching dimensions. Math (not just FORTRAN) expects an m*k matrix to be multiplied by a k*n matrix. You need to swap the axes of Mat1.

EDIT: Or, as francescalus notes, you can make it a rank-1 vector by declaring DIMENSION(4).

Upvotes: 1

Related Questions