Matrixmultiplikation
Programm zur Multiplikation einer (m x n) Matrix mit einer (n x l) Matrix.
module mysubs
contains
subroutine init(a, b, c)
use kinds
implicit none
real(kind=REAL8), dimension(:,:), pointer :: a, b, c
integer, parameter :: fileid = 1
character(len=256) :: fileName
integer :: status
integer :: n, m, l, i
write (*,*) 'Multiplikation zweier Matrizen'
write (*,*)
write (*,*) 'In welcher Datei liegen die Matrizen&
& (Beispiel in matmult.dat)?'
read (*,*) fileName
open(unit=fileid, file=fileName, action='read', iostat=status)
if (status /= 0) then
write (*,*) 'Konnte Datei ', fileName, 'nicht öffnen.'
stop
end if
read (fileid, *, iostat=status) m, n, l
if (status /= 0) then
write (*,*) 'Konnte Dimension nicht lesen.'
stop
end if
allocate(a(m, n))
allocate(b(n, l))
allocate(c(m, l))
read(fileid, *, iostat=status) (a(i,1:n), i=1,m)
if (status /= 0) then
write (*,*) 'Konnte Array a nicht lesen.'
stop
end if
read(fileid, *, iostat=status) (b(i,1:l), i=1,n)
if (status /= 0) then
write (*,*) 'Konnte Array b nicht lesen.'
stop
end if
close(unit=fileid)
return
end subroutine init
subroutine result(c)
use kinds
implicit none
real(kind=REAL8), dimension(:,:), pointer :: c
integer :: i, n, m
character(len=80) :: format
m = size(c, 1)
n = size(c, 2)
write (format, '(A,I4,A)') '(', n, '(F12.3," "))'
write (*,*) 'Multiplikation liefert folgende Matrix'
write (*, format) (c(i,1:n), i=1,m)
return
end subroutine result
end module mysubs
program MatrixMult
! Multiplikation zweier Matrizen
use kinds
use mysubs
implicit none
real(kind=REAL8), dimension(:,:), pointer :: a, b, c
call init(a, b, c)
c = matmul(a, b)
call result(c)
stop
end program MatrixMult