!! File  : slineq.f90
!!
!! Creation Date: 06.11.2015; Time: 12:12
!! Time-stamp: <2015-11-08 18:32:55 (andrzejbaran)> 
!!
!! Andrzej Baran
!! IF UMCS, ul. Radziszewskiego 10, 20-031 Lublin, PL
!! baran@kft.umcs.lublin.pl
!!
!! BASIS: http://www-01.ibm.com/support/knowledgecenter/
!!
!! Przykład korzystania z bibliotek B L A S  i   L A P A C K
!! ( L A P A C K  =  Linear ALgebra PACKage )
!! ( B L A S  =  Basic Linear Algebra Subprograms )

program slineq
  IMPLICIT NONE
  integer, parameter :: n = 9, m = 9, nrhs = 5
  double precision, dimension(m,n) :: a
  double precision, dimension(n,5)   :: bx
  double precision :: det
  integer ipvt(n), INFO, i, j


  data a / &
       1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, &
       1.2, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, &
       1.4, 1.2, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, &
       1.6, 1.4, 1.2, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, &
       1.8, 1.6, 1.4, 1.2, 1.0, 1.2, 1.4, 1.6, 1.8, &
       2.0, 1.8, 1.6, 1.4, 1.2, 1.0, 1.2, 1.4, 1.6, &
       2.2, 2.0, 1.8, 1.6, 1.4, 1.2, 1.0, 1.2, 1.4, &
       2.4, 2.2, 2.0, 1.8, 1.6, 1.4, 1.2, 1.0, 1.2, &
       2.6, 2.4, 2.2, 2.0, 1.8, 1.6, 1.4, 1.2, 1.0 /

! OUTPUT of DGETRF
!      ┌                                              ┐
!      | 2.6   2.4  2.2  2.0  1.8  1.6  1.4  1.2  1.0 |
!      | 0.4   0.3  0.6  0.8  1.1  1.4  1.7  1.9  2.2 |
!      | 0.5  -0.4  0.4  0.8  1.2  1.6  2.0  2.4  2.8 |
!      | 0.5  -0.3  0.0  0.4  0.8  1.2  1.6  2.0  2.4 |
! A =  | 0.6  -0.3  0.0  0.0  0.4  0.8  1.2  1.6  2.0 |
!      | 0.7  -0.2  0.0  0.0  0.0  0.4  0.8  1.2  1.6 |
!      | 0.8  -0.2  0.0  0.0  0.0  0.0  0.4  0.8  1.2 |
!      | 0.8  -0.1  0.0  0.0  0.0  0.0  0.0  0.4  0.8 |
!      | 0.9  -0.1  0.0  0.0  0.0  0.0  0.0  0.0  0.4 |
!      └                                              ┘

  data ((bx(i,j),j=1,nrhs),i=1,n) / &
       93.0, 186.0, 279.0, 372.0, 465.0, &
       84.4, 168.8, 253.2, 337.6, 422.0, &
       76.6, 153.2, 229.8, 306.4, 383.0, &
       70.0, 140.0, 210.0, 280.0, 350.0, &
       65.0, 130.0, 195.0, 260.0, 325.0, &
       62.0, 124.0, 186.0, 248.0, 310.0, &
       61.4, 122.8, 184.2, 245.6, 307.0, &
       63.6, 127.2, 190.8, 254.4, 318.0, &
       69.0, 138.0, 207.0, 276.0, 345.0/


! OUTPUT of DGETRS
!         ┌                             ┐
!         | 1.0   2.0   3.0   4.0   5.0 |
!         | 2.0   4.0   6.0   8.0  10.0 |
!         | 3.0   6.0   9.0  12.0  15.0 |
!         | 4.0   8.0  12.0  16.0  20.0 |
! BX   =  | 5.0  10.0  15.0  20.0  25.0 |
!         | 6.0  12.0  18.0  24.0  30.0 |
!         | 7.0  14.0  21.0  28.0  35.0 |
!         | 8.0  16.0  24.0  32.0  40.0 |
!         | 9.0  18.0  27.0  36.0  45.0 |
!         └                             ┘


  ! faktoryzacja A
  print *, "A"
  print "(9f7.2)", ((a(i,j),i=1,n),j=1,n)
  CALL DGETRF( 9 , 9 , A,  9 , IPVT, INFO )
  print *, "info(dgetrf)=", info
  print *, "LU"
  print "(9f7.2)", ((a(i,j),i=1,n),j=1,n)

  ! wyznacznik A
  !print *, "det = ", det(A,ipvt,n)

  ! 5 rozwiązań układu ró∑nań A.x = bx;
  print *, "b"
  print "(5f7.1)", ((bx(i,j),j=1,nrhs),i=1,n)
  CALL DGETRS('N',  9,  nrhs, A, 9, IPVT, BX , 9 , INFO)
  print *,"info(dgetrs) = ", info
  print *, "x"
  print "(5f7.1)", ((bx(i,j),j=1,nrhs),i=1,n)

END program slineq


