----------------------------------------------------------------------------------------------------------------------------------------
PROGRAM matrix_inverse
IMPLICIT NONE
INTEGER::n,i,j,k,p,i1,j1,i2,j2,i3
REAL::s,detr,deta
REAL,ALLOCATABLE::a(:,:),b(:,:),c(:,:),f(:),g(:,:)
PRINT*
PRINT*," _ _ "
PRINT*," | a11 a12 ..a1n1 | "
PRINT*," | | -1"
PRINT*,"A = | a21 a22 ..a2n1 | A = ?"
PRINT*," | . |"
PRINT*," | . |"
PRINT*," |_am11 am12..am1n1_|"
PRINT*," nxn "
PRINT*
PRINT*,"------------------------------------------------------------------------"
PRINT*
PRINT*," n ra vared konid : "
READ*,n
ALLOCATE(a(n,n),b(n-1,n-1),c(n,n),f(n-1),g(n,n))
DO i2=1,n
PRINT*," khate",i2,"matris a ra vared konid : "
READ*,a(i2,:)
END DO
PRINT*,"-----------------------------------------------------"
PRINT*
call det(n,a,deta)
IF (deta==0) THEN
PRINT*," matris makoos pazir nist! "
PRINT*
STOP
END IF
DO i1=1,n
DO j1=1,n
p=0
DO i=1,n
IF (i/=i1) THEN
k=0
DO j=1,n
IF (j/=j1) THEN
k=k+1
f(k)=a(i,j)
END IF
END DO
p=p+1
b(p,:)=f
END IF
END DO
call det(n-1,b,detr)
s=((-1)**(i1+j1))*detr
c(i1,j1)=s
END DO
END DO
DO j2=1,n
g(:,j2)=c(j2,:)
END DO
g=g/deta
PRINT*," -1 "
PRINT*," A : "
PRINT*
DO i3=1,n
PRINT*," ",g(i3,:)
END DO
PRINT*
END
subroutine det(n,a,t1)
IMPLICIT NONE
INTEGER::i,j,g,f,r,j1,k,s,n
REAL::landa,t,a(n,n),b(n),c(n,n),t1
c=a
k=0
DO i=1,n
IF (c(i,i)==0) THEN
DO j=i+1,n
IF (c(j,i)/=0) THEN
k=k+1
b=c(j,:)
c(j,:)=c(i,:)
c(i,:)=b
END IF
END DO
END IF
DO j1=i+1,n
landa=-c(j1,i)/c(i,i)
c(j1,:)=(landa*c(i,:))+c(j1,:)
END DO
END DO
t=1
DO s=1,n
t=t*c(s,s)
END DO
IF (MOD(k,2)==0) THEN
t1=t
ELSE
t1=-t
END IF
END