Guest User

LU

a guest
Sep 26th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. subroutine LU(n, m, A, P, Y)
  2.     integer, intent(in) :: n, m
  3.     integer :: i, j, k, pivot, temp
  4.     double precision :: pivotValue
  5.     integer, dimension(:), intent(inout) :: P
  6.     double precision, dimension(:), allocatable :: x
  7.     double precision, dimension(:, :), intent(inout) :: A, Y
  8.     allocate(x(n))
  9.     do i = 1, n
  10.         !searching for max
  11.         pivotValue = 0.0
  12.         pivot = -1
  13.         do k = i, n
  14.             if (abs(A(k, i)) > pivotValue) then
  15.                 pivotValue = abs(A(k, i))
  16.                 pivot = k
  17.             end if
  18.         end do
  19.         !is matrix invertible?
  20.         if (abs(pivotValue) < 1.0/10000) then
  21.             print *, "matrix is invertiblle"
  22.         end if
  23.         !row swap
  24.         !call dswap(1, P(i), 0, P(pivot), 0)
  25.         temp = P(i)
  26.         P(i) = P(pivot)
  27.         P(pivot) = temp
  28.         call dswap(n, A(i,1:n), 1, A(pivot, 1:n), 1)
  29.         do j = i + 1, n
  30.             A(j, i) = A(j, i) / A(i, i)
  31.             do k = i + 1, n
  32.                 A(j, k) = A(j, k) - A(j, i) * A(i, k)    
  33.             end do
  34.         end do
  35.     end do
  36.     !A = L + U - I
  37.     !Gauss
  38.     do k = 1, m
  39.         !forward
  40.         do i = 1, n
  41.             x(i) = Y(P(i), k)
  42.             x(i) = x(i) - ddot(i-1, A(i,1:i-1), 1, x(1:i-1), 1)
  43.             !do j = 1, i - 1
  44.             !    x(i) = x(i) - A(i, j) * x(j)
  45.             !end do
  46.         end do
  47.         !backward
  48.         do i = n, 1, -1
  49.             if (i < n) then
  50.                 x(i) = x(i) - ddot(n-i, x(i+1:n), 1, A(i,i+1:n), 1)
  51.             end if
  52.             !do j = i + 1, n
  53.             !    x(i) = x(i) - A(i, j) * x(j)
  54.             !end do
  55.             x(i) = x(i) / A(i, i)
  56.         end do
  57.         !answer
  58.         do i = 1, n
  59.             Y(i, k) = x(i)
  60.         end do
  61.     end do
  62.     deallocate(x)
  63. end subroutine
  64.  
  65. program lu_decomposition
  66.     implicit none
  67.     integer :: n, m, i, inp, outp
  68.     integer, dimension(:), allocatable :: P, sP
  69.     double precision, dimension(:,:), allocatable :: A, sA, tempY, Y, sY
  70.     interface
  71.         subroutine LU(n, m, A, P, Y)
  72.             integer, intent(in) :: n, m
  73.             integer :: i, j, k, pivot, temp
  74.             double precision :: pivotValue
  75.             integer, dimension(:), intent(inout) :: P
  76.             double precision, dimension(:), allocatable :: x
  77.             double precision, dimension(:, :), intent(inout) :: A, Y
  78.         end subroutine LU
  79.     end interface
  80.     !input
  81.     inp = 42
  82.     outp = 43
  83.     open(unit = inp, file = "input.txt")
  84.     read(inp, *) n, m
  85.     allocate(P(n))
  86.     do i = 1, n
  87.         P(i) = i
  88.     end do
  89.     allocate(sP(n))
  90.     sP = P
  91.     allocate(A(n, n))
  92.     allocate(sA(n, n))
  93.     allocate(Y(n, m))
  94.     allocate(sY(n, m))
  95.     allocate(tempY(m, n))
  96.     read (inp, *) A
  97.     A = transpose(A)
  98.     sA = A
  99.     read (inp, *) tempY
  100.     Y = transpose(tempY)
  101.     sY = Y
  102.     close(inp)
  103.     open(unit = outp, file = "output.txt")
  104.     !LU decomposition
  105.     call LU(n, m, A, P, Y)
  106.     !LU output
  107.     write(outp, *) "A"
  108.     do i = 1, n
  109.         write(outp, *) A(i, :)
  110.     end do
  111.     write(outp, *) "P"
  112.     write(outp, *) P
  113.     write(outp, *) "Y"
  114.     do i = 1, n
  115.         write(outp, "(1000F10.3)") Y(i, :)
  116.     end do
  117.     !sgesv
  118.     call dgesv(n, m, sA, n, sP, sY, n, i)
  119.     !sgesv output
  120.     if (i .eq. 0) then
  121.         print *, "successful sgesv execution"
  122.     end if
  123.     write(outp, *) "A by sgesv"
  124.     do i = 1, n
  125.         write(outp, *) sA(i, :)
  126.     end do
  127.     write(outp, *) "P by sgesv"
  128.     write(outp, *) sP
  129.     write(outp, *) "Y by sgesv"
  130.     do i = 1, n
  131.         write(outp, "(1000F10.3)") sY(i, :)
  132.     end do
  133.     close(outp)
  134.     !memory
  135.     deallocate(P)
  136.     deallocate(sP)
  137.     deallocate(A)
  138.     deallocate(Y)
  139.     deallocate(sY)
  140. end program
Advertisement
Add Comment
Please, Sign In to add comment