Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save nilforooshan/9f813f365925a9d9e2425be428ff3d49 to your computer and use it in GitHub Desktop.
f90: Calculate the trace of a matrix *.exe and *.out are the Windows and Linux executables.
PROGRAM trace
IMPLICIT NONE
! Declarations
INTEGER:: i, j, n, error
REAL,DIMENSION(:,:),ALLOCATABLE:: a
REAL:: tr=0.
CHARACTER(20):: input, 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 file, open and check it
PRINT*,
PRINT*,
PRINT*, 'Type the name of the input file.'
READ*, input
OPEN(UNIT=10, FILE=input, STATUS='OLD', IOSTAT=error)
IF (error/=0) THEN
PRINT*,
PRINT*,
PRINT*, 'Error reading file.'
STOP
END IF
! Ask the size of the matrix
PRINT*,
PRINT*,
PRINT*, 'Insert the size of the matrix.'
READ*, n
! Read the matrix
ALLOCATE(a(n,n))
READ(10,*) ((a(i,j),j=1,n),i=1,n)
! Estimate the trace and write it on the screen
DO i=1,n
j=i
tr=tr+a(i,j)
END DO
PRINT*,
PRINT*,
WRITE(*,*) 'Trace=', tr
! Finished
PRINT*,
PRINT*,
PRINT*, 'Press any key to exit.'
READ*, exiit
IF (exiit=='a') THEN
GO TO 69
END IF
69 END PROGRAM trace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment