School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

Other Languages Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 340,125 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,055 people online right now. Registration is fast and FREE... Join Now!




FORTRAN 90: Determinant of a Matrix

Thus function finds the determinant of a square matrix of any order

Submitted By: Louisda16th
Actions:
Rating:
Views: 7,087

Language: Other Languages

Last Modified: September 18, 2007
Instructions: !Example program:
PROGRAM MAT_DET
IMPLICIT NONE

REAL, ALLOCATABLE, DIMENSION(:,:) :: Matrix
REAL :: Determinant, FindDet
INTEGER :: n, i, j

PRINT*,"Enter order of matrix"
READ*, n

ALLOCATE(Matrix(n,n))

PRINT*,"Enter elements row-wise"

DO i = 1,n
READ*,(MATRIX(i,j),j=1,n)
END DO

Determinant = FindDet(Matrix,n)

PRINT*,"Determinant =",Determinant
END PROGRAM MAT_DET
!Function Source Here

Snippet


  1. !Function to find the determinant of a square matrix
  2. !Author : Louisda16th a.k.a Ashwith J. Rego
  3. !Description: The subroutine is based on two key points:
  4. !1] A determinant is unaltered when row operations are performed: Hence, using this principle,
  5. !row operations (column operations would work as well) are used
  6. !to convert the matrix into upper traingular form
  7. !2]The determinant of a triangular matrix is obtained by finding the product of the diagonal elements
  8. !
  9. REAL FUNCTION FindDet(matrix, n)
  10.         IMPLICIT NONE
  11.         REAL, DIMENSION(n,n) :: matrix
  12.         INTEGER, INTENT(IN) :: n
  13.         REAL :: m, temp
  14.         INTEGER :: i, j, k, l
  15.         LOGICAL :: DetExists = .TRUE.
  16.         l = 1
  17.         !Convert to upper triangular form
  18.         DO k = 1, n-1
  19.                 IF (matrix(k,k) == 0) THEN
  20.                         DetExists = .FALSE.
  21.                         DO i = k+1, n
  22.                                 IF (matrix(i,k) /= 0) THEN
  23.                                         DO j = 1, n
  24.                                                 temp = matrix(i,j)
  25.                                                 matrix(i,j)= matrix(k,j)
  26.                                                 matrix(k,j) = temp
  27.                                         END DO
  28.                                         DetExists = .TRUE.
  29.                                         l=-l
  30.                                         EXIT
  31.                                 ENDIF
  32.                         END DO
  33.                         IF (DetExists .EQV. .FALSE.) THEN
  34.                                 FindDet = 0
  35.                                 return
  36.                         END IF
  37.                 ENDIF
  38.                 DO j = k+1, n
  39.                         m = matrix(j,k)/matrix(k,k)
  40.                         DO i = k+1, n
  41.                                 matrix(j,i) = matrix(j,i) - m*matrix(k,i)
  42.                         END DO
  43.                 END DO
  44.         END DO
  45.        
  46.         !Calculate determinant by finding product of diagonal elements
  47.         FindDet = l
  48.         DO i = 1, n
  49.                 FindDet = FindDet * matrix(i,i)
  50.         END DO
  51.        
  52. END FUNCTION FindDet

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month