Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
Last active March 30, 2019 10:10
Show Gist options
  • Select an option

  • Save nilforooshan/f29640fa4b94f03e275a58b60c3a5491 to your computer and use it in GitHub Desktop.

Select an option

Save nilforooshan/f29640fa4b94f03e275a58b60c3a5491 to your computer and use it in GitHub Desktop.
f90: Multiply two matrices to each other. You can choose to feed the matrices to the program through a file or console keyboard. *.exe and *.out are the Windows and Linux executables.
PROGRAM mmult
IMPLICIT NONE
! Declarations
INTEGER:: i, j, k, row, cola, colb, error
REAL,DIMENSION(:,:),ALLOCATABLE:: A, B, C
CHARACTER(1):: h
CHARACTER(20):: infile, outfile, exiit
! Opening prints
PRINT*,
PRINT*,
PRINT*, 'This program is written by Mohammad A. Nilforooshan.'
PRINT*, 'All rights reserved.'
PRINT*, 'http://sites.google.com/site/mannprofile/'
! Ask for the input way
PRINT*,
PRINT*,
PRINT*, 'How do you want to input the matrices?'
PRINT*,
PRINT*, 'From a file: (press "y")'
PRINT*, '(Max. number of rows and columns = 50)'
PRINT*,
PRINT*, 'From console keyboard: (press any other key)'
PRINT*, '(Max. number of rows and columns = 10)'
READ*, h
IF (h/='y') THEN
GO TO 113
END IF
! Ask for the name of the input file, open and check it
PRINT*,
PRINT*,
PRINT*, 'Insert the name of the input file.'
PRINT*, 'In the file, start matrix B from the first line after matrix A.'
READ*, infile
OPEN(UNIT=10, FILE=infile, STATUS='OLD', IOSTAT=error)
IF (error/=0) THEN
PRINT*,
PRINT*,
PRINT*, 'Error reading file.'
STOP
END IF
! Ask the sizes of the matrices in the file
PRINT*,
PRINT*,
PRINT*, 'Insert the number of rows for matrix A.'
READ*, row
IF (row>50) THEN
STOP
END IF
PRINT*,
PRINT*,
PRINT*, 'Insert the number of columns for matrix A.'
READ*, cola
IF (cola>50) THEN
STOP
END IF
PRINT*,
PRINT*,
PRINT*, 'Insert the number of columns for matrix B.'
READ*, colb
IF (colb>50) THEN
STOP
END IF
ALLOCATE(A(row,cola))
ALLOCATE(B(cola,colb))
! Read the matrices from the file
READ(10,*) ((A(i,j),j=1,cola),i=1,row)
READ(10,*) ((B(i,j),j=1,colb),i=1,cola)
! Create the output
ALLOCATE(C(row,colb))
C=MATMUL(A,B)
! Ask for the name of the output file
PRINT*,
PRINT*,
PRINT*, 'Insert the name of the output file.'
READ*, outfile
! Write in the output file
OPEN(UNIT=11, FILE=outfile, STATUS='UNKNOWN')
DO i=1,row
WRITE(11,'(50F10.3)') (C(i,j), j=1,colb)
END DO
GO TO 187
! Ask the sizes of the matrices
113 PRINT*,
PRINT*,
PRINT*, 'Insert the number of rows for matrix A.'
READ*, row
IF (row>10) THEN
STOP
END IF
PRINT*,
PRINT*,
PRINT*, 'Insert the number of columns for matrix A.'
READ*, cola
IF (cola>10) THEN
STOP
END IF
PRINT*,
PRINT*,
PRINT*, 'Insert the number of columns for matrix B.'
READ*, colb
IF (colb>10) THEN
STOP
END IF
ALLOCATE(A(row,cola))
ALLOCATE(B(cola,colb))
! Read the matrices from console keyboard
PRINT*,
PRINT*,
PRINT*, 'Insert matrix A (column-wise).'
PRINT*,
PRINT*,
DO j=1,cola
PRINT*, 'Insert column',j
DO i=1,row
READ*, A(i,j)
END DO
END DO
PRINT*,
PRINT*,
PRINT*, 'Insert matrix B (column-wise).'
PRINT*,
PRINT*,
DO j=1,colb
PRINT*, 'Insert column',j
DO i=1,cola
READ*, B(i,j)
END DO
END DO
! Create the output
ALLOCATE(C(row,colb))
C=MATMUL(A,B)
! Print the results on the screen
PRINT*,
PRINT*,
PRINT*, 'A x B ='
DO i=1,row
WRITE(*,'(10F11.3)') (C(i,j), j=1,colb)
END DO
! Finished
187 PRINT*,
PRINT*,
PRINT*, 'Press any key to exit.'
READ*, exiit
IF (exiit=='a') THEN
GO TO 196
END IF
196 END PROGRAM mmult
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment