简体   繁体   English

在Fortran中相当于numpy.dot(python)

[英]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....) 我在python中有需要“翻译”为Fortran的代码(我不太了解...。)

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 我想在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 . 但是在这种情况下, numpy.dot并不是真正的点产品...我不知道该使用DOT_PRODUCT还是MATMUL Things are really unclear for me. 我真的不清楚。

For this, MATMUL is the way you want to go. 为此,MATMUL是您想要的方式。 See here . 这里 DOT_PRODUCT is only for vectors. DOT_PRODUCT仅适用于矢量。 MATMUL can handle any matrices whose dimensions allow matrix multiplication. MATMUL可以处理尺寸允许矩阵相乘的任何矩阵。

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. 数学(不仅是FORTRAN)期望将m*k矩阵乘以k*n矩阵。 You need to swap the axes of Mat1. 您需要交换Mat1的轴。

EDIT : Or, as francescalus notes, you can make it a rank-1 vector by declaring DIMENSION(4) . 编辑 :或者,正如francescalus指出的那样,您可以通过声明DIMENSION(4)使其成为1级向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM