Quantcast
Channel: Intel® oneAPI Math Kernel Library & Intel® Math Kernel Library
Viewing all articles
Browse latest Browse all 2652

Matrix Inversion using Lapack (DGETRF/DGETRI)

$
0
0

I am trying to invert the following matrix using DGETRF and DGETRI

matrix = 

  0.50E+12  0.00E+00 -0.30E+09  0.25E+12  0.00E+00  0.10E-02  0.00E+00  0.00E+00  0.00E+00  0.00E+00  0.00E+00  0.00E+00
  0.00E+00  0.12E+08  0.00E+00  0.00E+00 -0.60E+07 -0.00E+00 -0.00E+00  0.00E+00  0.00E+00  0.00E+00  0.00E+00  0.00E+00
 -0.30E+09  0.00E+00  0.48E+06  0.00E+00 -0.00E+00 -0.00E+00  0.30E+09  0.00E+00  0.00E+00  0.00E+00  0.00E+00  0.00E+00
  0.25E+12  0.00E+00  0.00E+00  0.10E+13  0.00E+00 -0.00E+00  0.25E+12  0.00E+00  0.00E+00  0.00E+00  0.00E+00  0.00E+00
  0.00E+00 -0.60E+07 -0.00E+00  0.00E+00  0.12E+08 -0.00E+00  0.00E+00 -0.60E+07 -0.00E+00 -0.00E+00  0.00E+00  0.00E+00
  0.00E+00 -0.00E+00 -0.24E+06 -0.30E+09  0.00E+00  0.60E+04  0.00E+00 -0.00E+00 -0.24E+06  0.30E+09  0.00E+00  0.00E+00
  0.00E+00 -0.00E+00  0.30E+09  0.25E+12  0.00E+00 -0.00E+00  0.10E+13  0.00E+00 -0.30E+09  0.25E+12  0.00E+00  0.00E+00
  0.00E+00  0.00E+00  0.00E+00  0.00E+00 -0.60E+07 -0.00E+00  0.00E+00  0.12E+08  0.00E+00  0.00E+00 -0.60E+07 -0.00E+00
  0.00E+00  0.00E+00  0.00E+00  0.00E+00 -0.00E+00 -0.00E+00 -0.30E+09  0.00E+00  0.48E+06  0.00E+00 -0.00E+00  0.30E+09
  0.00E+00  0.00E+00  0.00E+00  0.00E+00 -0.00E+00 -0.00E+00  0.25E+12  0.00E+00  0.00E+00  0.10E+13  0.00E+00  0.25E+12
  0.00E+00  0.00E+00  0.00E+00  0.00E+00  0.00E+00 -0.10E-02  0.00E+00 -0.60E+07 -0.00E+00  0.00E+00  0.60E+07  0.00E+00
  0.00E+00  0.00E+00  0.00E+00  0.00E+00  0.00E+00 -0.10E-02  0.00E+00 -0.00E+00  0.30E+09  0.25E+12  0.00E+00  0.50E+12

however it says the matrix is numerically singular. However I can invert the matrix using excel/matlab with no problems

Does anyone have any idea why this is happening?

in the code below, the matrix is of size MJNT3xMJNT3 however I want to invert only the first NxN terms of S or Sinv.

I tried calling 

CALL DGETRF(N, N, Sinv, N, ipiv, info)

CALL DGETRI(N, Sinv, N, ipiv, work, N, info)

it doesn't work. Then I tried a few other possibilities replacing the LDA in both DGETRF and DGETRI from N to MJNT3. and it didn't work either

I am not expert, so this is quite confusing to me. 

the whole subroutine is given below:

      SUBROUTINE INVERT(S)

      INTEGER MJNT,MJNT3
      PARAMETER (MJNT=510,MJNT3=3*MJNT)

!     DECLARING GLOBAL VARIABLES

      COMMON /CLIST21/ N,NJ3,M,NMT,NJ,NRJ,NR
      INTEGER N,NJ3,M,NMT,NJ,NRJ,NR !21    

!     DECLARING LOCAL VARIABLES
      INTEGER I,J !21  

      REAL*8 S(MJNT3,MJNT3)
      REAL*8 Sinv(MJNT3,MJNT3)
      INTEGER, DIMENSION(size(Sinv,1)) :: ipiv   ! pivot indices
      INTEGER, PARAMETER :: dp = selected_real_kind(15, 307)
      real(dp), DIMENSION(size(Sinv,1)) :: work  ! work array for LAPACK
      
      INTEGER :: info
      
      EXTERNAL DGETRF
      EXTERNAL DGETRI
      

      ! Store S in Sinv to prevent it from being overwritten by LAPACK
      DO 10 I=1,N
      DO 20 J=1,N
      Sinv(I,J) = S(I,J)
20    CONTINUE
10    CONTINUE
      
      ! DGETRF COMPUTES AN LU FACTORIZATION OF A GENERAL M-BY-N MATRIX A
      ! USING PARTIAL PIVOTING WITH ROW INTERCHANGES.
      CALL DGETRF(N, N, Sinv, N, ipiv, info)
      IF (info /= 0) THEN
          STOP 'MATRIX IS NUMERICALLY SINGULAR!'
      END IF      
      ! DGETRI COMPUTES THE INVERSE OF A MATRIX USING THE LU FACTORIZATION
      ! COMPUTED BY DGETRF.
      CALL DGETRI(N, Sinv, N, ipiv, work, N, info)
      IF (info /= 0) THEN
          STOP 'MATRIX INVERSION FAILED!'
      END IF
      
      DO 30 I=1,N
      DO 40 J=1,N
      S(I,J) = Sinv(I,J)
40    CONTINUE
30    CONTINUE
      
      PAUSE !to test
      
      RETURN
      END

 

TCE Open Date: 

Thursday, December 12, 2019 - 03:30

Viewing all articles
Browse latest Browse all 2652

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>