[CSC 335] Determinants with Fortran

Andrew J. Pounds pounds_aj at mercer.edu
Tue Nov 14 10:44:09 EST 2023


Here is the example I showed in class for finding the determinant of a 
Hilbert matrix

       program determinant

       parameter (NMAX=4)

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

       double precision arg, prod, zero, one
       integer N

       zero = 0.0D0
       one  = 1.0D0

       N = NMAX

* Build the Hilbert Matrix
       do k = 1, N
         do j = 1, N
           A(j,k) = 1.0D0 / (dble(j+k-1))
           if ( j .ne. k ) then
              B(j,k) = zero
           else
              B(j,k) = one
           endif
         enddo
       enddo

* Perform an PLU factorization.  Use diagonal elements of U (stored in 
A) and
* the number of row interchanges (stored in IPIV) to calculate determinant.

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

       print *, 'Info = ', INFO

       sumd = one
       prod = one
       do i = 1, n
           if ( IPIV(i) .ne. i ) sumd = sumd * (-1.0D0)
           prod = prod * A(i,i)
       enddo

       print *, sumd, '  ', prod
       print *, 'The determinant is ', sumd*prod
       end

-- 
*Andrew J. Pounds, Ph.D.*
/Professor of Chemistry and Computer Science/
/Director of the Computational Science Program/
/Mercer University, Macon, GA, 31207 (478) 301-5627 /
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc335/attachments/20231114/c4fa4bfd/attachment.html>


More information about the csc335 mailing list