f5d9770bf7161387905eaa76d0f9128570fd1644
[opencv] / 3rdparty / lapack / sgetri.c
1 #include "clapack.h"
2
3 /* Table of constant values */
4
5 static integer c__1 = 1;
6 static integer c_n1 = -1;
7 static integer c__2 = 2;
8 static real c_b20 = -1.f;
9 static real c_b22 = 1.f;
10
11 /* Subroutine */ int sgetri_(integer *n, real *a, integer *lda, integer *ipiv, 
12          real *work, integer *lwork, integer *info)
13 {
14     /* System generated locals */
15     integer a_dim1, a_offset, i__1, i__2, i__3;
16
17     /* Local variables */
18     integer i__, j, jb, nb, jj, jp, nn, iws, nbmin;
19     extern /* Subroutine */ int sgemm_(char *, char *, integer *, integer *, 
20             integer *, real *, real *, integer *, real *, integer *, real *, 
21             real *, integer *), sgemv_(char *, integer *, 
22             integer *, real *, real *, integer *, real *, integer *, real *, 
23             real *, integer *), sswap_(integer *, real *, integer *, 
24             real *, integer *), strsm_(char *, char *, char *, char *, 
25             integer *, integer *, real *, real *, integer *, real *, integer *
26 ), xerbla_(char *, integer *);
27     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
28             integer *, integer *);
29     integer ldwork, lwkopt;
30     logical lquery;
31     extern /* Subroutine */ int strtri_(char *, char *, integer *, real *, 
32             integer *, integer *);
33
34
35 /*  -- LAPACK routine (version 3.1) -- */
36 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
37 /*     November 2006 */
38
39 /*     .. Scalar Arguments .. */
40 /*     .. */
41 /*     .. Array Arguments .. */
42 /*     .. */
43
44 /*  Purpose */
45 /*  ======= */
46
47 /*  SGETRI computes the inverse of a matrix using the LU factorization */
48 /*  computed by SGETRF. */
49
50 /*  This method inverts U and then computes inv(A) by solving the system */
51 /*  inv(A)*L = inv(U) for inv(A). */
52
53 /*  Arguments */
54 /*  ========= */
55
56 /*  N       (input) INTEGER */
57 /*          The order of the matrix A.  N >= 0. */
58
59 /*  A       (input/output) REAL array, dimension (LDA,N) */
60 /*          On entry, the factors L and U from the factorization */
61 /*          A = P*L*U as computed by SGETRF. */
62 /*          On exit, if INFO = 0, the inverse of the original matrix A. */
63
64 /*  LDA     (input) INTEGER */
65 /*          The leading dimension of the array A.  LDA >= max(1,N). */
66
67 /*  IPIV    (input) INTEGER array, dimension (N) */
68 /*          The pivot indices from SGETRF; for 1<=i<=N, row i of the */
69 /*          matrix was interchanged with row IPIV(i). */
70
71 /*  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK)) */
72 /*          On exit, if INFO=0, then WORK(1) returns the optimal LWORK. */
73
74 /*  LWORK   (input) INTEGER */
75 /*          The dimension of the array WORK.  LWORK >= max(1,N). */
76 /*          For optimal performance LWORK >= N*NB, where NB is */
77 /*          the optimal blocksize returned by ILAENV. */
78
79 /*          If LWORK = -1, then a workspace query is assumed; the routine */
80 /*          only calculates the optimal size of the WORK array, returns */
81 /*          this value as the first entry of the WORK array, and no error */
82 /*          message related to LWORK is issued by XERBLA. */
83
84 /*  INFO    (output) INTEGER */
85 /*          = 0:  successful exit */
86 /*          < 0:  if INFO = -i, the i-th argument had an illegal value */
87 /*          > 0:  if INFO = i, U(i,i) is exactly zero; the matrix is */
88 /*                singular and its inverse could not be computed. */
89
90 /*  ===================================================================== */
91
92 /*     .. Parameters .. */
93 /*     .. */
94 /*     .. Local Scalars .. */
95 /*     .. */
96 /*     .. External Functions .. */
97 /*     .. */
98 /*     .. External Subroutines .. */
99 /*     .. */
100 /*     .. Intrinsic Functions .. */
101 /*     .. */
102 /*     .. Executable Statements .. */
103
104 /*     Test the input parameters. */
105
106     /* Parameter adjustments */
107     a_dim1 = *lda;
108     a_offset = 1 + a_dim1;
109     a -= a_offset;
110     --ipiv;
111     --work;
112
113     /* Function Body */
114     *info = 0;
115     nb = ilaenv_(&c__1, "SGETRI", " ", n, &c_n1, &c_n1, &c_n1);
116     lwkopt = *n * nb;
117     work[1] = (real) lwkopt;
118     lquery = *lwork == -1;
119     if (*n < 0) {
120         *info = -1;
121     } else if (*lda < max(1,*n)) {
122         *info = -3;
123     } else if (*lwork < max(1,*n) && ! lquery) {
124         *info = -6;
125     }
126     if (*info != 0) {
127         i__1 = -(*info);
128         xerbla_("SGETRI", &i__1);
129         return 0;
130     } else if (lquery) {
131         return 0;
132     }
133
134 /*     Quick return if possible */
135
136     if (*n == 0) {
137         return 0;
138     }
139
140 /*     Form inv(U).  If INFO > 0 from STRTRI, then U is singular, */
141 /*     and the inverse is not computed. */
142
143     strtri_("Upper", "Non-unit", n, &a[a_offset], lda, info);
144     if (*info > 0) {
145         return 0;
146     }
147
148     nbmin = 2;
149     ldwork = *n;
150     if (nb > 1 && nb < *n) {
151 /* Computing MAX */
152         i__1 = ldwork * nb;
153         iws = max(i__1,1);
154         if (*lwork < iws) {
155             nb = *lwork / ldwork;
156 /* Computing MAX */
157             i__1 = 2, i__2 = ilaenv_(&c__2, "SGETRI", " ", n, &c_n1, &c_n1, &
158                     c_n1);
159             nbmin = max(i__1,i__2);
160         }
161     } else {
162         iws = *n;
163     }
164
165 /*     Solve the equation inv(A)*L = inv(U) for inv(A). */
166
167     if (nb < nbmin || nb >= *n) {
168
169 /*        Use unblocked code. */
170
171         for (j = *n; j >= 1; --j) {
172
173 /*           Copy current column of L to WORK and replace with zeros. */
174
175             i__1 = *n;
176             for (i__ = j + 1; i__ <= i__1; ++i__) {
177                 work[i__] = a[i__ + j * a_dim1];
178                 a[i__ + j * a_dim1] = 0.f;
179 /* L10: */
180             }
181
182 /*           Compute current column of inv(A). */
183
184             if (j < *n) {
185                 i__1 = *n - j;
186                 sgemv_("No transpose", n, &i__1, &c_b20, &a[(j + 1) * a_dim1 
187                         + 1], lda, &work[j + 1], &c__1, &c_b22, &a[j * a_dim1 
188                         + 1], &c__1);
189             }
190 /* L20: */
191         }
192     } else {
193
194 /*        Use blocked code. */
195
196         nn = (*n - 1) / nb * nb + 1;
197         i__1 = -nb;
198         for (j = nn; i__1 < 0 ? j >= 1 : j <= 1; j += i__1) {
199 /* Computing MIN */
200             i__2 = nb, i__3 = *n - j + 1;
201             jb = min(i__2,i__3);
202
203 /*           Copy current block column of L to WORK and replace with */
204 /*           zeros. */
205
206             i__2 = j + jb - 1;
207             for (jj = j; jj <= i__2; ++jj) {
208                 i__3 = *n;
209                 for (i__ = jj + 1; i__ <= i__3; ++i__) {
210                     work[i__ + (jj - j) * ldwork] = a[i__ + jj * a_dim1];
211                     a[i__ + jj * a_dim1] = 0.f;
212 /* L30: */
213                 }
214 /* L40: */
215             }
216
217 /*           Compute current block column of inv(A). */
218
219             if (j + jb <= *n) {
220                 i__2 = *n - j - jb + 1;
221                 sgemm_("No transpose", "No transpose", n, &jb, &i__2, &c_b20, 
222                         &a[(j + jb) * a_dim1 + 1], lda, &work[j + jb], &
223                         ldwork, &c_b22, &a[j * a_dim1 + 1], lda);
224             }
225             strsm_("Right", "Lower", "No transpose", "Unit", n, &jb, &c_b22, &
226                     work[j], &ldwork, &a[j * a_dim1 + 1], lda);
227 /* L50: */
228         }
229     }
230
231 /*     Apply column interchanges. */
232
233     for (j = *n - 1; j >= 1; --j) {
234         jp = ipiv[j];
235         if (jp != j) {
236             sswap_(n, &a[j * a_dim1 + 1], &c__1, &a[jp * a_dim1 + 1], &c__1);
237         }
238 /* L60: */
239     }
240
241     work[1] = (real) iws;
242     return 0;
243
244 /*     End of SGETRI */
245
246 } /* sgetri_ */