The following code works correctly. I generate a 9x9 matrix, which consists of 9 separate 3x3 sub block matrices. However, lots of code seems duplicated and I'm using an excessive amount of DO loops it seems.
Is there any way I can generate the same 9x9 matrix but without duplicating lots of code and minimizing the amount of do loops?
My actual problem involves a square matrix much larger than a 9x9 matrix, so this TestCode is not so general or useful yet even though it works.
The case of the matrix being 9x9 here is just for a minimal, complete, verifiable example. In general, I need to do this for an n by n matrix where each sub-block is of size sqrt(n) by sqrt(n).
PROGRAM TestCode
IMPLICIT NONE
INTEGER :: i, j, m!matrix indices (i,j)
INTEGER,PARAMETER :: n = 9 ! matrix is 9x9
DOUBLE PRECISION :: KE(n,n)
REAL :: nn
nn = n
m = SQRT(nn)
DO i = 1, m
DO j = 1, m
IF( i .EQ. j) THEN
KE(i,j) = -4
ELSEIF ( ABS(i-j) .EQ. 1) THEN
KE(i,j) = 1
ELSE
KE(i,j) = 0
END IF
END DO
END DO
DO i = 4,6
DO j = 4,6
IF( i .EQ. j) THEN
KE(i,j) = -4
ELSEIF ( ABS(i-j) .EQ. 1) THEN
KE(i,j) = 1
ELSE
KE(i,j) = 0
END IF
END DO
END DO
DO i = 7,9
DO j = 7,9
IF( i .EQ. j) THEN
KE(i,j) = -4
ELSEIF ( ABS(i-j) .EQ. 1) THEN
KE(i,j) = 1
ELSE
KE(i,j) = 0
END IF
END DO
END DO
DO i = 4,6
DO j = 1,m
IF( ABS(i-j) .EQ. m) THEN
KE(i,j) = 1
ELSE
KE(i,j) = 0
END IF
END DO
END DO
DO i = 7,9
DO j = 1,m
KE(i,j) = 0
END DO
END DO
DO i = 1,m
DO j = 4,6
IF ( ABS(i-j) .EQ. m) THEN
KE(i,j) = 1
ELSE
KE(i,j) = 0
END IF
END DO
END DO
DO i = 7,9
DO j = 4,6
IF ( ABS(i-j) .EQ. m) THEN
KE(i,j) = 1
ELSE
KE(i,j) = 0
END IF
END DO
END DO
DO i = 1,m
DO j = 7,9
KE(i,j) = 0
END DO
END DO
DO i = 4,6
DO j = 7,9
IF( ABS(i-j) .EQ. m) THEN
KE(i,j) = 1
ELSE
KE(i,j) = 0
END IF
END DO
END DO
END PROGRAM