[CSC 335] Computing the Inverse Matrix with Fortran and Lapack

Andrew Pounds pounds at sandbox.mercer.edu
Sun Nov 26 20:21:56 EST 2017


While I included a version of how to compute the inverse matrix using 
the GSL in the repo I sent earlier, those of you using Fortran may also 
need an example to follow.  In the following example I create a matrix 
(A) and then fill the (B) matrix with the identity matrix.  The DGESV 
lapack routine overwrites the B matrix with the solution matrix (x)

Ax=B

which is the inverse of A.


Here is the code...   compile on cobra with "gfortran inverse.f -llapack"

       program inverse

       parameter (NMAX=3)

       double precision A(NMAX,NMAX), B(NMAX,NMAX)
       integer IPIV(NMAX)

       A(1,1) = 1.0
       A(2,1) = 2.0
       A(3,1) = -1.0
       A(1,2) = 2.0
       A(2,2) = 1.0
       A(3,2) = 0.0
       A(1,3) = -1.0
       A(2,3) = 1.0
       A(3,3) = 2.0

       B(1,1) = 1.0
       B(2,1) = 0.0
       B(3,1) = 0.0
       B(1,2) = 0.0
       B(2,2) = 1.0
       B(3,2) = 0.0
       B(1,3) = 0.0
       B(2,3) = 0.0
       B(3,3) = 1.0

       call DGESV( NMAX, NMAX, A, NMAX, IPIV, B, NMAX, INFO )

       do 10 j = 1, nmax
          write (6,'(3(f10.5))') (B(i,j),i=1,nmax)
10    continue

       end


-- 
Andrew J. Pounds, Ph.D.
Professor of Chemistry and Computer Science
Mercer University,  Macon, GA 31207   (478) 301-5627
http://faculty.mercer.edu/pounds_aj



More information about the csc335 mailing list