Vector multiplication using MATMUL in Fortran

2020-01-29 02:30发布

问题:

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.