Advertisement
Guest User

Untitled

a guest
Jan 8th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Program cholesky
  2.   Implicit None
  3.  
  4.   Integer, parameter :: pr=4
  5.   Integer, parameter :: dim=3
  6.   Real(kind=pr), dimension(:,:), allocatable :: A
  7.  
  8.   Allocate(A(dim,dim))
  9.  
  10.   A(1,1)=4;A(1,2)=-2;A(1,3)=4
  11.   A(2,1)=-2;A(2,2)=10;A(2,3)=-5
  12.   A(3,1)=4;A(3,2)=-5;A(3,3)=6
  13.  
  14.   Print *,A
  15.   print *,decomp(A)
  16.  
  17.   Deallocate(A)
  18.  
  19.   Contains
  20.    
  21.   Function decomp(X)
  22.     Real(kind=pr), Dimension(:,:), Intent(in) :: X
  23.     Real(kind=pr), Dimension(dim,dim)         :: decomp
  24.     Integer                                   :: i,j,k,n
  25.     Real(kind=pr)                             :: temp
  26.  
  27.     n=dim
  28.     decomp=0
  29.    
  30.     Do j=1,n
  31.  
  32.        temp=0._pr
  33.        if (j>=2) Then
  34.           Do k=1,j-1
  35.              temp=temp+decomp(j,k)**2
  36.           End Do
  37.        End if
  38.  
  39.        decomp(j,j)=sqrt(X(j,j)-temp)
  40.        
  41.        Do i=j+1,n
  42.  
  43.           temp=0._pr
  44.           Do k=1,j-1
  45.              temp=temp+decomp(i,k)*decomp(j,k)
  46.           End Do
  47.          
  48.           decomp(i,j)=(X(i,j)-temp)/decomp(j,j)
  49.          
  50.        End Do
  51.        
  52.     End Do
  53.   End Function decomp
  54.  
  55.    
  56.  
  57. End Program cholesky
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement