The following are a Fortran IV Gauss-Jordan elimination routine that computes the inverse of a matrix along with the magnitude of its determinate and the det mag. scaled by the Euclidean norm of the matrix which I guess is a measure of how well the matrix is conditioned and my modern Fortran refactoring. The source is a verbatim copy (in all its statement label and GO TO glory) of a routine listed in Hornbeck’s “Numerical Methods” book from 1975.
I tested with ifx 2025.3 and gfortran-13 and both give the same results for the following matrix
1.0 3.0 3.0 2.0 1.0
1.0 4.0 3.0 3.0 -1.0
1.0 3.0 4.0 1.0 1.0
1.0 1.0 1.0 1.0 -1.0
1.0 -2.0 -1.0 2.0 2.0
which has as its inverse (to 1 decimal place output)
2.0 -1.3 -1.0 1.7 -0.3
2.0 -0.7 -1.0 0.5 -0.3
-2.0 0.8 1.4 -0.6 0.4
-1.0 0.8 0.4 -0.6 0.4
1.0 -0.5 -0.4 -0.1 -0.1
det mag = 15.0
This is the Fortran IV code
SUBROUTINE INVDET(C,N,DTNRM,DETM)
DIMENSION C(70,70),J(120)
PD=1.
DO 124 L=1,N
DD=0.
DO 123 K=1,N
123 DD=DD+C(L,K)*C(L,K)
DD=SQRT(DD)
124 PD=PD*DD
DETM=1.
DO 125 L=1,N
125 J(L+20)=L
DO 144 L=1,N
CC=0.
M=L
DO 135 K=L,N
IF ((ABS(CC)-ABS(C(L,K))).GE.0.) GO TO 135
126 M=K
CC=C(L,K)
135 CONTINUE
127 IF(L.EQ.M) GO TO 138
128 K=J(M+20)
J(M+20)=J(L+20)
J(L+20)=K
DO 137 K=1,N
S=C(K,L)
C(K,L)=C(K,M)
137 C(K,M)=S
138 C(L,L)=1.
DETM=DETM*CC
DO 139 M=1,N
139 C(L,M)=C(L,M)/CC
DO 142 M=1,N
IF(L.EQ.M) GO TO 142
129 CC=C(M,L)
IF (CC.EQ.0.) GO TO 142
130 C(M,L)=0.
DO 141 K=1,N
141 C(M,K)=C(M,K)-CC*C(L,K)
142 CONTINUE
144 CONTINUE
DO 143 L=1,N
IF (J(L+20).EQ.L) GO TO 143
131 M=L
132 M=M+1
IF (J(M+20).EQ.L) GO TO 133
136 IF (N.GT.M) GO TO 132
133 J(M+20)=J(L+20)
DO 163 K=1,N
CC=C(L,K)
C(L,K)=C(M,K)
163 C(M,K)=CC
J(L+20)=L
143 CONTINUE
DETM=ABS(DETM)
DTNRM=DETM/PD
RETURN
END
Here is my refactored code (much longer because I added whitespace to improve readability)
invdetmf.f90 (1.9 KB)
and a test program. Fortran IV must be compiled separately due to fixed field format.
testinvdet.f90 (3.6 KB)
Also a question for the mods. Why can’t I upload a file with a .f file extension. With my browser (firefox) it only appears to support .f90
Edit. For some reason the original code required the J array dimension which is used for column pivoting to be at least N+21. I have no idea where this comes from.