-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmatmul.f90
47 lines (40 loc) · 1.03 KB
/
matmul.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
PROGRAM matmul
REAL(kind=8),ALLOCATABLE :: amatrix(:,:), bmatrix(:,:), cmatrix(:,:)
REAL(kind=8) :: tmp
INTEGER :: arows,acols,brows,bcols,crows,ccols
INTEGER :: i,j,k
! read matrix A
READ*,arows,acols
ALLOCATE(amatrix(arows,acols))
DO i=1,arows
READ*,(amatrix(j,i),j=1,acols)
END DO
! read matrix B
READ*,brows,bcols
ALLOCATE(bmatrix(brows,bcols))
DO i=1,brows
READ*,(bmatrix(j,i),j=1,bcols)
END DO
! allocate matrix C
IF ((arows /= brows) .AND. (acols /= bcols)) THEN
PRINT*, 'Matrices are not compatible. ',arows,' vs ',brows,', ', acols, ' vs ',bcols
DEALLOCATE(amatrix,bmatrix)
STOP 'input data error'
END IF
crows = arows
ccols = acols
ALLOCATE(cmatrix(arows,acols))
DO i=1,crows
DO j=1,ccols
tmp = 0.0_8
DO k=1,brows
tmp = tmp + amatrix(k,i)*bmatrix(k,j)
END DO
cmatrix(j,i) = tmp
END DO
END DO
! output result
DO i=1,crows
WRITE(*,*) (cmatrix(j,i),j=1,ccols)
END DO
END PROGRAM matmul