100% found this document useful (2 votes)
7K views2 pages

FORTRAN77 Subroutine For Matrix Inversion

This document contains a Fortran subroutine that calculates the inverse of a square matrix using an augmenting-pivoting technique. It takes in the matrix dimension N, the original square matrix A, and outputs the inverted matrix in AINV. It works by augmenting the original matrix with an identity matrix to create an augmented matrix, then uses row operations of pivoting, scaling, and subtraction to reduce the augmented matrix to an identity matrix, from which the inverse of the original matrix can be extracted.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
7K views2 pages

FORTRAN77 Subroutine For Matrix Inversion

This document contains a Fortran subroutine that calculates the inverse of a square matrix using an augmenting-pivoting technique. It takes in the matrix dimension N, the original square matrix A, and outputs the inverted matrix in AINV. It works by augmenting the original matrix with an identity matrix to create an augmented matrix, then uses row operations of pivoting, scaling, and subtraction to reduce the augmented matrix to an identity matrix, from which the inverse of the original matrix can be extracted.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

E:\matinv.

f Wednesday, January 05, 2011 5:34 PM

C **********************************************************************
SUBROUTINE MATINV(N,A,AINV)
C Download URL: http://wp.me/p61TQ-zb
C Last modified: 2010/12/30

C A general purpose matrix inverter by augmenting-pivoting technique:

C A B C | 1 0 0 1 0 0 | J K L
C D E F | 0 1 0 => 0 1 0 | M N O
C G H I | 0 0 1 0 0 1 | P Q R

C Based on a lecture by Prof. McFarland


C http://math.uww.edu/~mcfarlat/inverse.htm

C Explanation of passed parameters:


C N: dimension of square matrix
C A: square matrix of dimension NxN to be inverted
C AINV: the inverted matrix

IMPLICIT REAL*8 (A-H,O-Z)


DIMENSION A(N,N),AINV(N,N),B(N,2*N)

C MAKE AUGMENTED MATRIX


DO I=1,N
DO J=1,N
B(I,J)=0.0D0
B(I,J+N)=0.0D0

B(I,J)=A(I,J)
IF(I.EQ.J) THEN
B(I,J+N)=1.0D0
END IF
END DO
END DO

DO I=1,N

C CHOOSE THE LEFTMOST NON-ZERO ELEMENT AS PIVOT


DO J=1,N
IF(DABS(B(I,J)).GT.0)THEN
PIVOT=B(I,J)
EXIT
END IF
END DO

C STEP 1: Change the chosen pivot into "1" by dividing


C the pivot's row by the pivot number
DO J=1,2*N
B(I,J)=B(I,J)/PIVOT
END DO
PIVOT=B(I,I) !UPDATE PIVOT VALUE

C STEP 2: Change the remainder of the pivot's COLUMN into 0's


C by adding to each row a suitable multiple of the PIVOT ROW
DO K=1,N !ROW
IF(K.NE.I) THEN
XNUM=B(K,I)/PIVOT !SAME COLUMN WITH THE CURRENT PIVOT

-1-
E:\matinv.f Wednesday, January 05, 2011 5:34 PM

DO J=1,2*N !COL
B(K,J)=B(K,J)-XNUM*B(I,J)
END DO
END IF
END DO

END DO

C PREPARE THE FINAL INVERTED MATRIX


DO I=1,N
DO J=1,N
AINV(I,J)=B(I,J+N)
END DO
END DO

RETURN
END
C **********************************************************************

-2-

You might also like