Vector multiplication using MATMUL in Fortran

浪子不回头ぞ 提交于 2019-11-26 14:47:32

问题


I am trying to multiply part of a column vector (n,1) by a part of another row vector (1,n). Both parts have the same length. So I should get a matrix (n,n).

Here is my simple code:

PROGRAM test_pack_1
REAL :: m(1,10), x(10,1), y(10,10)

m = reshape( (/ 1, -1, 3, 2, 1, 2, -2, -2, 1, 0 /), (/ 1, 10 /))
x = reshape( (/ 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 /), (/ 10, 1 /))

y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))


DO j = 1,10
PRINT* ;WRITE(*,*) y(:,j)
ENDDO
print *

END PROGRAM

I'm Using:

ifort -g -debug -traceback -check all -ftrapuv test_cshift.f90

And I'm getting:

test_cshift.f90(7): error #6241: The shapes of the arguments are inconsistent or nonconformable.   [MATMUL]
y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))
-------------^
test_cshift.f90(7): error #6366: The shapes of the array expressions do not conform.   [Y]
y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))

回答1:


The problem is that x(1:9,1) isn't of shape [9 1] but [9]. You need to use x(1:9, 1:1). The same goes for m.



来源:https://stackoverflow.com/questions/22305554/vector-multiplication-using-matmul-in-fortran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!