matrix multiplication

silveira13
04-18-2008, 10:05 PM
Dear friends,

I´d like to know how to perform a matrix multiplication with Fortran 90.

davekw7x
04-18-2008, 11:17 PM
...how to perform a matrix multiplication with Fortran 90

1. Get a book or other reference to Fortran 90.
2. Look up matmul:

PROGRAM xmatmul

IMPLICIT NONE

INTEGER i,j
INTEGER, PARAMETER :: M=2,N=3

REAL :: a(M,N) = RESHAPE((/1.0,4.0,2.0,5.0,3.0,6.0/),(/M,N/))
REAL :: b(N,M) = RESHAPE((/1.0,2.0,3.0,4.0,5.0,6.0/),(/N,M/))
REAL :: c(M,M)

write(*,*) 'Matrix [a]'
do i=1,M
write(*,1000) (a(i,j),j=1,N)
enddo
write(*,*)

write(*,*) 'Matrix [b]'
do i=1,N
write(*,1000) (b(i,j),j=1,M)
enddo
write(*,*)

c = matmul(a, b)
write(*,*) 'Matrix [c] = [a] x [b]'
do i = 1,M
write(*,1000) (c(i,j),j=1,M)
enddo

1000 FORMAT(1x,1P10E14.6)

END PROGRAM xmatmul

Output:

Matrix [a]
1.000000E+00 2.000000E+00 3.000000E+00
4.000000E+00 5.000000E+00 6.000000E+00

Matrix [b]
1.000000E+00 4.000000E+00
2.000000E+00 5.000000E+00
3.000000E+00 6.000000E+00

Matrix [c] = [a] x [b]
1.400000E+01 3.200000E+01
3.200000E+01 7.700000E+01

Regards,

Dave