📄 inverse.f
字号:
END IF END IF 20 CONTINUE END IF RETURN** End of DGETRF* END SUBROUTINE DGETRI( N, A, LDA, IPIV, WORK, LWORK, INFO )** -- LAPACK routine (version 3.0) --* Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,* Courant Institute, Argonne National Lab, and Rice University* June 30, 1999** .. Scalar Arguments .. INTEGER INFO, LDA, LWORK, N* ..* .. Array Arguments .. INTEGER IPIV( * ) DOUBLE PRECISION A( LDA, * ), WORK( * )* ..** Purpose* =======** DGETRI computes the inverse of a matrix using the LU factorization* computed by DGETRF.** This method inverts U and then computes inv(A) by solving the system* inv(A)*L = inv(U) for inv(A).** Arguments* =========** N (input) INTEGER* The order of the matrix A. N >= 0.** A (input/output) DOUBLE PRECISION array, dimension (LDA,N)* On entry, the factors L and U from the factorization* A = P*L*U as computed by DGETRF.* On exit, if INFO = 0, the inverse of the original matrix A.** LDA (input) INTEGER* The leading dimension of the array A. LDA >= max(1,N).** IPIV (input) INTEGER array, dimension (N)* The pivot indices from DGETRF; for 1<=i<=N, row i of the* matrix was interchanged with row IPIV(i).** WORK (workspace/output) DOUBLE PRECISION array, dimension (LWORK)* On exit, if INFO=0, then WORK(1) returns the optimal LWORK.** LWORK (input) INTEGER* The dimension of the array WORK. LWORK >= max(1,N).* For optimal performance LWORK >= N*NB, where NB is* the optimal blocksize returned by ILAENV.** If LWORK = -1, then a workspace query is assumed; the routine* only calculates the optimal size of the WORK array, returns* this value as the first entry of the WORK array, and no error* message related to LWORK is issued by XERBLA.** INFO (output) INTEGER* = 0: successful exit* < 0: if INFO = -i, the i-th argument had an illegal value* > 0: if INFO = i, U(i,i) is exactly zero; the matrix is* singular and its inverse could not be computed.** =====================================================================** .. Parameters .. DOUBLE PRECISION ZERO, ONE PARAMETER ( ZERO = 0D+0, ONE = 1D+0 )* ..* .. Local Scalars .. LOGICAL LQUERY INTEGER I, IWS, J, JB, JJ, JP, LDWORK, LWKOPT, NB, $ NBMIN, NN* ..* .. External Functions .. INTEGER ILAENV EXTERNAL ILAENV* ..* .. External Subroutines .. EXTERNAL DGEMM, DGEMV, DSWAP, DTRSM, DTRTRI, XERBLA* ..* .. Intrinsic Functions .. INTRINSIC MAX, MIN* ..* .. Executable Statements ..** Test the input parameters.* INFO = 0 NB = ILAENV( 1, 'DGETRI', ' ', N, -1, -1, -1 ) LWKOPT = N*NB WORK( 1 ) = LWKOPT LQUERY = ( LWORK.EQ.-1 ) IF( N.LT.0 ) THEN INFO = -1 ELSE IF( LDA.LT.MAX( 1, N ) ) THEN INFO = -3 ELSE IF( LWORK.LT.MAX( 1, N ) .AND. .NOT.LQUERY ) THEN INFO = -6 END IF IF( INFO.NE.0 ) THEN CALL XERBLA( 'DGETRI', -INFO ) RETURN ELSE IF( LQUERY ) THEN RETURN END IF** Quick return if possible* IF( N.EQ.0 ) $ RETURN** Form inv(U). If INFO > 0 from DTRTRI, then U is singular,* and the inverse is not computed.* CALL DTRTRI( 'Upper', 'Non-unit', N, A, LDA, INFO ) IF( INFO.GT.0 ) $ RETURN* NBMIN = 2 LDWORK = N IF( NB.GT.1 .AND. NB.LT.N ) THEN IWS = MAX( LDWORK*NB, 1 ) IF( LWORK.LT.IWS ) THEN NB = LWORK / LDWORK NBMIN = MAX( 2, ILAENV( 2, 'DGETRI', ' ', N, -1, -1, -1 ) ) END IF ELSE IWS = N END IF** Solve the equation inv(A)*L = inv(U) for inv(A).* IF( NB.LT.NBMIN .OR. NB.GE.N ) THEN** Use unblocked code.* DO 20 J = N, 1, -1** Copy current column of L to WORK and replace with zeros.* DO 10 I = J + 1, N WORK( I ) = A( I, J ) A( I, J ) = ZERO 10 CONTINUE** Compute current column of inv(A).* IF( J.LT.N ) $ CALL DGEMV( 'No transpose', N, N-J, -ONE, A( 1, J+1 ), $ LDA, WORK( J+1 ), 1, ONE, A( 1, J ), 1 ) 20 CONTINUE ELSE** Use blocked code.* NN = ( ( N-1 ) / NB )*NB + 1 DO 50 J = NN, 1, -NB JB = MIN( NB, N-J+1 )** Copy current block column of L to WORK and replace with* zeros.* DO 40 JJ = J, J + JB - 1 DO 30 I = JJ + 1, N WORK( I+( JJ-J )*LDWORK ) = A( I, JJ ) A( I, JJ ) = ZERO 30 CONTINUE 40 CONTINUE** Compute current block column of inv(A).* IF( J+JB.LE.N ) $ CALL DGEMM( 'No transpose', 'No transpose', N, JB, $ N-J-JB+1, -ONE, A( 1, J+JB ), LDA, $ WORK( J+JB ), LDWORK, ONE, A( 1, J ), LDA ) CALL DTRSM( 'Right', 'Lower', 'No transpose', 'Unit', N, JB, $ ONE, WORK( J ), LDWORK, A( 1, J ), LDA ) 50 CONTINUE END IF** Apply column interchanges.* DO 60 J = N - 1, 1, -1 JP = IPIV( J ) IF( JP.NE.J ) $ CALL DSWAP( N, A( 1, J ), 1, A( 1, JP ), 1 ) 60 CONTINUE* WORK( 1 ) = IWS RETURN** End of DGETRI* END SUBROUTINE DGETRS( TRANS, N, NRHS, A, LDA, IPIV, B, LDB, INFO )** -- LAPACK routine (version 1.0) --* Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,* Courant Institute, Argonne National Lab, and Rice University* February 29, 1992** .. Scalar Arguments .. CHARACTER TRANS INTEGER INFO, LDA, LDB, N, NRHS* ..* .. Array Arguments .. INTEGER IPIV( * ) DOUBLE PRECISION A( LDA, * ), B( LDB, * )* ..** Purpose* =======** DGETRS solves a system of linear equations* A * X = B or A' * X = B* with a general n by n matrix A using the LU factorization computed* by DGETRF.** Arguments* =========** TRANS (input) CHARACTER*1* Specifies the form of the system of equations.* = 'N': A * X = B (No transpose)* = 'T': A'* X = B (Transpose)* = 'C': A'* X = B (Conjugate transpose = Transpose)** N (input) INTEGER* The order of the matrix A. N >= 0.** NRHS (input) INTEGER* The number of right hand sides, i.e., the number of columns* of the matrix B. NRHS >= 0.** A (input) DOUBLE PRECISION array, dimension (LDA,N)* The factors L and U from the factorization A = P*L*U* as computed by DGETRF.** LDA (input) INTEGER* The leading dimension of the array A. LDA >= max(1,N).** IPIV (input) INTEGER array, dimension (N)* The pivot indices from DGETRF; for 1<=i<=N, row i of the* matrix was interchanged with row IPIV(i).** B (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS)* On entry, the right hand side vectors B for the system of* linear equations.* On exit, the solution vectors, X.** LDB (input) INTEGER* The leading dimension of the array B. LDB >= max(1,N).** INFO (output) INTEGER* = 0: successful exit* < 0: if INFO = -k, the k-th argument had an illegal value** =====================================================================** .. Parameters .. DOUBLE PRECISION ONE PARAMETER ( ONE = 1D+0 )* ..* .. Local Scalars .. LOGICAL NOTRAN* ..* .. External Functions .. LOGICAL LSAME EXTERNAL LSAME* ..* .. External Subroutines .. EXTERNAL DLASWP, DTRSM, XERBLA* ..* .. Intrinsic Functions .. INTRINSIC MAX* ..* .. Executable Statements ..** Test the input parameters.* INFO = 0 NOTRAN = LSAME( TRANS, 'N' ) IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT. $ LSAME( TRANS, 'C' ) ) THEN INFO = -1 ELSE IF( N.LT.0 ) THEN INFO = -2 ELSE IF( NRHS.LT.0 ) THEN INFO = -3 ELSE IF( LDA.LT.MAX( 1, N ) ) THEN INFO = -5 ELSE IF( LDB.LT.MAX( 1, N ) ) THEN INFO = -8 END IF IF( INFO.NE.0 ) THEN CALL XERBLA( 'DGETRS', -INFO ) RETURN END IF** Quick return if possible* IF( N.EQ.0 .OR. NRHS.EQ.0 ) $ RETURN* IF( NOTRAN ) THEN** Solve A * X = B.** Apply row interchanges to the right hand sides.* CALL DLASWP( NRHS, B, LDB, 1, N, IPIV, 1 )** Solve L*X = B, overwriting B with X.* CALL DTRSM( 'Left', 'Lower', 'No transpose', 'Unit', N, NRHS, $ ONE, A, LDA, B, LDB )** Solve U*X = B, overwriting B with X.* CALL DTRSM( 'Left', 'Upper', 'No transpose', 'Non-unit', N, $ NRHS, ONE, A, LDA, B, LDB ) ELSE** Solve A' * X = B.** Solve U'*X = B, overwriting B with X.* CALL DTRSM( 'Left', 'Upper', 'Transpose', 'Non-unit', N, NRHS, $ ONE, A, LDA, B, LDB )** Solve L'*X = B, overwriting B with X.* CALL DTRSM( 'Left', 'Lower', 'Transpose', 'Unit', N, NRHS, ONE, $ A, LDA, B, LDB )** Apply row interchanges to the solution vectors.* CALL DLASWP( NRHS, B, LDB, 1, N, IPIV, -1 ) END IF* RETURN** End of DGETRS* END SUBROUTINE DLASWP( N, A, LDA, K1, K2, IPIV, INCX )** -- LAPACK auxiliary routine (version 1.0) --* Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,* Courant Institute, Argonne National Lab, and Rice University* February 29, 1992** .. Scalar Arguments .. INTEGER INCX, K1, K2, LDA, N* ..* .. Array Arguments .. INTEGER IPIV( * ) DOUBLE PRECISION A( LDA, * )* ..** Purpose* =======** DLASWP performs a series of row interchanges on the matrix A.* One row interchange is initiated for each of rows K1 through K2 of A.** Arguments* =========** N (input) INTEGER* The number of columns of the matrix A.** A (input/output) DOUBLE PRECISION array, dimension (LDA,N)* On entry, the matrix of column dimension N to which the row* interchanges will be applied.* On exit, the permuted matrix.** LDA (input) INTEGER* The leading dimension of the array A.** K1 (input) INTEGER* The first element of IPIV for which a row interchange will* be done.** K2 (input) INTEGER* The last element of IPIV for which a row interchange will* be done.** IPIV (input) INTEGER array, dimension (M*abs(INCX))* The vector of pivot indices. Only the elements in positions* K1 through K2 of IPIV are accessed.* IPIV(K) = L implies rows K and L are to be interchanged.** INCX (input) INTEGER* The increment between successive values of IPIV. If IPIV* is negative, the pivots are applied in reverse order.*** .. Local Scalars .. INTEGER I, IP, IX* ..* .. External Subroutines .. EXTERNAL DSWAP* ..* .. Executable Statements ..** Interchange row I with row IPIV(I) for each of rows K1 through K2.* IF( INCX.EQ.0 ) $ RETURN IF( INCX.GT.0 ) THEN IX = K1 ELSE IX = 1 + ( 1-K2 )*INCX END IF IF( INCX.EQ.1 ) THEN DO 10 I = K1, K2 IP = IPIV( I ) IF( IP.NE.I ) $ CALL DSWAP( N, A( I, 1 ), LDA, A( IP, 1 ), LDA ) 10 CONTINUE ELSE IF( INCX.GT.1 ) THEN DO 20 I = K1, K2 IP = IPIV( IX ) IF( IP.NE.I )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -