exciting-0.9.218
[exciting.git] / src / LAPACK / dgetri.f
blob9f1c1182a3aa7c1ebdba0c4f85939d539f4d876d
1 SUBROUTINE DGETRI( N, A, LDA, IPIV, WORK, LWORK, INFO )
3 * -- LAPACK routine (version 3.1) --
4 * Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
5 * November 2006
7 * .. Scalar Arguments ..
8 INTEGER INFO, LDA, LWORK, N
9 * ..
10 * .. Array Arguments ..
11 INTEGER IPIV( * )
12 DOUBLE PRECISION A( LDA, * ), WORK( * )
13 * ..
15 * Purpose
16 * =======
18 * DGETRI computes the inverse of a matrix using the LU factorization
19 * computed by DGETRF.
21 * This method inverts U and then computes inv(A) by solving the system
22 * inv(A)*L = inv(U) for inv(A).
24 * Arguments
25 * =========
27 * N (input) INTEGER
28 * The order of the matrix A. N >= 0.
30 * A (input/output) DOUBLE PRECISION array, dimension (LDA,N)
31 * On entry, the factors L and U from the factorization
32 * A = P*L*U as computed by DGETRF.
33 * On exit, if INFO = 0, the inverse of the original matrix A.
35 * LDA (input) INTEGER
36 * The leading dimension of the array A. LDA >= max(1,N).
38 * IPIV (input) INTEGER array, dimension (N)
39 * The pivot indices from DGETRF; for 1<=i<=N, row i of the
40 * matrix was interchanged with row IPIV(i).
42 * WORK (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK))
43 * On exit, if INFO=0, then WORK(1) returns the optimal LWORK.
45 * LWORK (input) INTEGER
46 * The dimension of the array WORK. LWORK >= max(1,N).
47 * For optimal performance LWORK >= N*NB, where NB is
48 * the optimal blocksize returned by ILAENV.
50 * If LWORK = -1, then a workspace query is assumed; the routine
51 * only calculates the optimal size of the WORK array, returns
52 * this value as the first entry of the WORK array, and no error
53 * message related to LWORK is issued by XERBLA.
55 * INFO (output) INTEGER
56 * = 0: successful exit
57 * < 0: if INFO = -i, the i-th argument had an illegal value
58 * > 0: if INFO = i, U(i,i) is exactly zero; the matrix is
59 * singular and its inverse could not be computed.
61 * =====================================================================
63 * .. Parameters ..
64 DOUBLE PRECISION ZERO, ONE
65 PARAMETER ( ZERO = 0.0D+0, ONE = 1.0D+0 )
66 * ..
67 * .. Local Scalars ..
68 LOGICAL LQUERY
69 INTEGER I, IWS, J, JB, JJ, JP, LDWORK, LWKOPT, NB,
70 $ NBMIN, NN
71 * ..
72 * .. External Functions ..
73 INTEGER ILAENV
74 EXTERNAL ILAENV
75 * ..
76 * .. External Subroutines ..
77 EXTERNAL DGEMM, DGEMV, DSWAP, DTRSM, DTRTRI, XERBLA
78 * ..
79 * .. Intrinsic Functions ..
80 INTRINSIC MAX, MIN
81 * ..
82 * .. Executable Statements ..
84 * Test the input parameters.
86 INFO = 0
87 NB = ILAENV( 1, 'DGETRI', ' ', N, -1, -1, -1 )
88 LWKOPT = N*NB
89 WORK( 1 ) = LWKOPT
90 LQUERY = ( LWORK.EQ.-1 )
91 IF( N.LT.0 ) THEN
92 INFO = -1
93 ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
94 INFO = -3
95 ELSE IF( LWORK.LT.MAX( 1, N ) .AND. .NOT.LQUERY ) THEN
96 INFO = -6
97 END IF
98 IF( INFO.NE.0 ) THEN
99 CALL XERBLA( 'DGETRI', -INFO )
100 RETURN
101 ELSE IF( LQUERY ) THEN
102 RETURN
103 END IF
105 * Quick return if possible
107 IF( N.EQ.0 )
108 $ RETURN
110 * Form inv(U). If INFO > 0 from DTRTRI, then U is singular,
111 * and the inverse is not computed.
113 CALL DTRTRI( 'Upper', 'Non-unit', N, A, LDA, INFO )
114 IF( INFO.GT.0 )
115 $ RETURN
117 NBMIN = 2
118 LDWORK = N
119 IF( NB.GT.1 .AND. NB.LT.N ) THEN
120 IWS = MAX( LDWORK*NB, 1 )
121 IF( LWORK.LT.IWS ) THEN
122 NB = LWORK / LDWORK
123 NBMIN = MAX( 2, ILAENV( 2, 'DGETRI', ' ', N, -1, -1, -1 ) )
124 END IF
125 ELSE
126 IWS = N
127 END IF
129 * Solve the equation inv(A)*L = inv(U) for inv(A).
131 IF( NB.LT.NBMIN .OR. NB.GE.N ) THEN
133 * Use unblocked code.
135 DO 20 J = N, 1, -1
137 * Copy current column of L to WORK and replace with zeros.
139 DO 10 I = J + 1, N
140 WORK( I ) = A( I, J )
141 A( I, J ) = ZERO
142 10 CONTINUE
144 * Compute current column of inv(A).
146 IF( J.LT.N )
147 $ CALL DGEMV( 'No transpose', N, N-J, -ONE, A( 1, J+1 ),
148 $ LDA, WORK( J+1 ), 1, ONE, A( 1, J ), 1 )
149 20 CONTINUE
150 ELSE
152 * Use blocked code.
154 NN = ( ( N-1 ) / NB )*NB + 1
155 DO 50 J = NN, 1, -NB
156 JB = MIN( NB, N-J+1 )
158 * Copy current block column of L to WORK and replace with
159 * zeros.
161 DO 40 JJ = J, J + JB - 1
162 DO 30 I = JJ + 1, N
163 WORK( I+( JJ-J )*LDWORK ) = A( I, JJ )
164 A( I, JJ ) = ZERO
165 30 CONTINUE
166 40 CONTINUE
168 * Compute current block column of inv(A).
170 IF( J+JB.LE.N )
171 $ CALL DGEMM( 'No transpose', 'No transpose', N, JB,
172 $ N-J-JB+1, -ONE, A( 1, J+JB ), LDA,
173 $ WORK( J+JB ), LDWORK, ONE, A( 1, J ), LDA )
174 CALL DTRSM( 'Right', 'Lower', 'No transpose', 'Unit', N, JB,
175 $ ONE, WORK( J ), LDWORK, A( 1, J ), LDA )
176 50 CONTINUE
177 END IF
179 * Apply column interchanges.
181 DO 60 J = N - 1, 1, -1
182 JP = IPIV( J )
183 IF( JP.NE.J )
184 $ CALL DSWAP( N, A( 1, J ), 1, A( 1, JP ), 1 )
185 60 CONTINUE
187 WORK( 1 ) = IWS
188 RETURN
190 * End of DGETRI