You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sgetri.c 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <complex.h>
  6. #ifdef complex
  7. #undef complex
  8. #endif
  9. #ifdef I
  10. #undef I
  11. #endif
  12. #if defined(_WIN64)
  13. typedef long long BLASLONG;
  14. typedef unsigned long long BLASULONG;
  15. #else
  16. typedef long BLASLONG;
  17. typedef unsigned long BLASULONG;
  18. #endif
  19. #ifdef LAPACK_ILP64
  20. typedef BLASLONG blasint;
  21. #if defined(_WIN64)
  22. #define blasabs(x) llabs(x)
  23. #else
  24. #define blasabs(x) labs(x)
  25. #endif
  26. #else
  27. typedef int blasint;
  28. #define blasabs(x) abs(x)
  29. #endif
  30. typedef blasint integer;
  31. typedef unsigned int uinteger;
  32. typedef char *address;
  33. typedef short int shortint;
  34. typedef float real;
  35. typedef double doublereal;
  36. typedef struct { real r, i; } complex;
  37. typedef struct { doublereal r, i; } doublecomplex;
  38. #ifdef _MSC_VER
  39. static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
  40. static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
  41. static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
  42. static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
  43. #else
  44. static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
  45. static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
  46. static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
  47. static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
  48. #endif
  49. #define pCf(z) (*_pCf(z))
  50. #define pCd(z) (*_pCd(z))
  51. typedef blasint logical;
  52. typedef char logical1;
  53. typedef char integer1;
  54. #define TRUE_ (1)
  55. #define FALSE_ (0)
  56. /* Extern is for use with -E */
  57. #ifndef Extern
  58. #define Extern extern
  59. #endif
  60. /* I/O stuff */
  61. typedef int flag;
  62. typedef int ftnlen;
  63. typedef int ftnint;
  64. /*external read, write*/
  65. typedef struct
  66. { flag cierr;
  67. ftnint ciunit;
  68. flag ciend;
  69. char *cifmt;
  70. ftnint cirec;
  71. } cilist;
  72. /*internal read, write*/
  73. typedef struct
  74. { flag icierr;
  75. char *iciunit;
  76. flag iciend;
  77. char *icifmt;
  78. ftnint icirlen;
  79. ftnint icirnum;
  80. } icilist;
  81. /*open*/
  82. typedef struct
  83. { flag oerr;
  84. ftnint ounit;
  85. char *ofnm;
  86. ftnlen ofnmlen;
  87. char *osta;
  88. char *oacc;
  89. char *ofm;
  90. ftnint orl;
  91. char *oblnk;
  92. } olist;
  93. /*close*/
  94. typedef struct
  95. { flag cerr;
  96. ftnint cunit;
  97. char *csta;
  98. } cllist;
  99. /*rewind, backspace, endfile*/
  100. typedef struct
  101. { flag aerr;
  102. ftnint aunit;
  103. } alist;
  104. /* inquire */
  105. typedef struct
  106. { flag inerr;
  107. ftnint inunit;
  108. char *infile;
  109. ftnlen infilen;
  110. ftnint *inex; /*parameters in standard's order*/
  111. ftnint *inopen;
  112. ftnint *innum;
  113. ftnint *innamed;
  114. char *inname;
  115. ftnlen innamlen;
  116. char *inacc;
  117. ftnlen inacclen;
  118. char *inseq;
  119. ftnlen inseqlen;
  120. char *indir;
  121. ftnlen indirlen;
  122. char *infmt;
  123. ftnlen infmtlen;
  124. char *inform;
  125. ftnint informlen;
  126. char *inunf;
  127. ftnlen inunflen;
  128. ftnint *inrecl;
  129. ftnint *innrec;
  130. char *inblank;
  131. ftnlen inblanklen;
  132. } inlist;
  133. #define VOID void
  134. union Multitype { /* for multiple entry points */
  135. integer1 g;
  136. shortint h;
  137. integer i;
  138. /* longint j; */
  139. real r;
  140. doublereal d;
  141. complex c;
  142. doublecomplex z;
  143. };
  144. typedef union Multitype Multitype;
  145. struct Vardesc { /* for Namelist */
  146. char *name;
  147. char *addr;
  148. ftnlen *dims;
  149. int type;
  150. };
  151. typedef struct Vardesc Vardesc;
  152. struct Namelist {
  153. char *name;
  154. Vardesc **vars;
  155. int nvars;
  156. };
  157. typedef struct Namelist Namelist;
  158. #define abs(x) ((x) >= 0 ? (x) : -(x))
  159. #define dabs(x) (fabs(x))
  160. #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
  161. #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
  162. #define dmin(a,b) (f2cmin(a,b))
  163. #define dmax(a,b) (f2cmax(a,b))
  164. #define bit_test(a,b) ((a) >> (b) & 1)
  165. #define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
  166. #define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
  167. #define abort_() { sig_die("Fortran abort routine called", 1); }
  168. #define c_abs(z) (cabsf(Cf(z)))
  169. #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
  170. #ifdef _MSC_VER
  171. #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
  172. #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
  173. #else
  174. #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
  175. #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
  176. #endif
  177. #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
  178. #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
  179. #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
  180. //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
  181. #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
  182. #define d_abs(x) (fabs(*(x)))
  183. #define d_acos(x) (acos(*(x)))
  184. #define d_asin(x) (asin(*(x)))
  185. #define d_atan(x) (atan(*(x)))
  186. #define d_atn2(x, y) (atan2(*(x),*(y)))
  187. #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
  188. #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
  189. #define d_cos(x) (cos(*(x)))
  190. #define d_cosh(x) (cosh(*(x)))
  191. #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
  192. #define d_exp(x) (exp(*(x)))
  193. #define d_imag(z) (cimag(Cd(z)))
  194. #define r_imag(z) (cimagf(Cf(z)))
  195. #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  196. #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
  197. #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  198. #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
  199. #define d_log(x) (log(*(x)))
  200. #define d_mod(x, y) (fmod(*(x), *(y)))
  201. #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
  202. #define d_nint(x) u_nint(*(x))
  203. #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
  204. #define d_sign(a,b) u_sign(*(a),*(b))
  205. #define r_sign(a,b) u_sign(*(a),*(b))
  206. #define d_sin(x) (sin(*(x)))
  207. #define d_sinh(x) (sinh(*(x)))
  208. #define d_sqrt(x) (sqrt(*(x)))
  209. #define d_tan(x) (tan(*(x)))
  210. #define d_tanh(x) (tanh(*(x)))
  211. #define i_abs(x) abs(*(x))
  212. #define i_dnnt(x) ((integer)u_nint(*(x)))
  213. #define i_len(s, n) (n)
  214. #define i_nint(x) ((integer)u_nint(*(x)))
  215. #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
  216. #define s_cat(lpp, rpp, rnp, np, llp) { ftnlen i, nc, ll; char *f__rp, *lp; ll = (llp); lp = (lpp); for(i=0; i < (int)*(np); ++i) { nc = ll; if((rnp)[i] < nc) nc = (rnp)[i]; ll -= nc; f__rp = (rpp)[i]; while(--nc >= 0) *lp++ = *(f__rp)++; } while(--ll >= 0) *lp++ = ' '; }
  217. #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
  218. #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
  219. #define sig_die(s, kill) { exit(1); }
  220. #define s_stop(s, n) {exit(0);}
  221. #define z_abs(z) (cabs(Cd(z)))
  222. #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
  223. #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
  224. #define myexit_() break;
  225. #define mycycle() continue;
  226. #define myceiling(w) {ceil(w)}
  227. #define myhuge(w) {HUGE_VAL}
  228. //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
  229. #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
  230. /* -- translated by f2c (version 20000121).
  231. You must link the resulting object file with the libraries:
  232. -lf2c -lm (in that order)
  233. */
  234. /* Table of constant values */
  235. static integer c__1 = 1;
  236. static integer c_n1 = -1;
  237. static integer c__2 = 2;
  238. static real c_b20 = -1.f;
  239. static real c_b22 = 1.f;
  240. /* > \brief \b SGETRI */
  241. /* =========== DOCUMENTATION =========== */
  242. /* Online html documentation available at */
  243. /* http://www.netlib.org/lapack/explore-html/ */
  244. /* > \htmlonly */
  245. /* > Download SGETRI + dependencies */
  246. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgetri.
  247. f"> */
  248. /* > [TGZ]</a> */
  249. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgetri.
  250. f"> */
  251. /* > [ZIP]</a> */
  252. /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgetri.
  253. f"> */
  254. /* > [TXT]</a> */
  255. /* > \endhtmlonly */
  256. /* Definition: */
  257. /* =========== */
  258. /* SUBROUTINE SGETRI( N, A, LDA, IPIV, WORK, LWORK, INFO ) */
  259. /* INTEGER INFO, LDA, LWORK, N */
  260. /* INTEGER IPIV( * ) */
  261. /* REAL A( LDA, * ), WORK( * ) */
  262. /* > \par Purpose: */
  263. /* ============= */
  264. /* > */
  265. /* > \verbatim */
  266. /* > */
  267. /* > SGETRI computes the inverse of a matrix using the LU factorization */
  268. /* > computed by SGETRF. */
  269. /* > */
  270. /* > This method inverts U and then computes inv(A) by solving the system */
  271. /* > inv(A)*L = inv(U) for inv(A). */
  272. /* > \endverbatim */
  273. /* Arguments: */
  274. /* ========== */
  275. /* > \param[in] N */
  276. /* > \verbatim */
  277. /* > N is INTEGER */
  278. /* > The order of the matrix A. N >= 0. */
  279. /* > \endverbatim */
  280. /* > */
  281. /* > \param[in,out] A */
  282. /* > \verbatim */
  283. /* > A is REAL array, dimension (LDA,N) */
  284. /* > On entry, the factors L and U from the factorization */
  285. /* > A = P*L*U as computed by SGETRF. */
  286. /* > On exit, if INFO = 0, the inverse of the original matrix A. */
  287. /* > \endverbatim */
  288. /* > */
  289. /* > \param[in] LDA */
  290. /* > \verbatim */
  291. /* > LDA is INTEGER */
  292. /* > The leading dimension of the array A. LDA >= f2cmax(1,N). */
  293. /* > \endverbatim */
  294. /* > */
  295. /* > \param[in] IPIV */
  296. /* > \verbatim */
  297. /* > IPIV is INTEGER array, dimension (N) */
  298. /* > The pivot indices from SGETRF; for 1<=i<=N, row i of the */
  299. /* > matrix was interchanged with row IPIV(i). */
  300. /* > \endverbatim */
  301. /* > */
  302. /* > \param[out] WORK */
  303. /* > \verbatim */
  304. /* > WORK is REAL array, dimension (MAX(1,LWORK)) */
  305. /* > On exit, if INFO=0, then WORK(1) returns the optimal LWORK. */
  306. /* > \endverbatim */
  307. /* > */
  308. /* > \param[in] LWORK */
  309. /* > \verbatim */
  310. /* > LWORK is INTEGER */
  311. /* > The dimension of the array WORK. LWORK >= f2cmax(1,N). */
  312. /* > For optimal performance LWORK >= N*NB, where NB is */
  313. /* > the optimal blocksize returned by ILAENV. */
  314. /* > */
  315. /* > If LWORK = -1, then a workspace query is assumed; the routine */
  316. /* > only calculates the optimal size of the WORK array, returns */
  317. /* > this value as the first entry of the WORK array, and no error */
  318. /* > message related to LWORK is issued by XERBLA. */
  319. /* > \endverbatim */
  320. /* > */
  321. /* > \param[out] INFO */
  322. /* > \verbatim */
  323. /* > INFO is INTEGER */
  324. /* > = 0: successful exit */
  325. /* > < 0: if INFO = -i, the i-th argument had an illegal value */
  326. /* > > 0: if INFO = i, U(i,i) is exactly zero; the matrix is */
  327. /* > singular and its inverse could not be computed. */
  328. /* > \endverbatim */
  329. /* Authors: */
  330. /* ======== */
  331. /* > \author Univ. of Tennessee */
  332. /* > \author Univ. of California Berkeley */
  333. /* > \author Univ. of Colorado Denver */
  334. /* > \author NAG Ltd. */
  335. /* > \date December 2016 */
  336. /* > \ingroup realGEcomputational */
  337. /* ===================================================================== */
  338. /* Subroutine */ void sgetri_(integer *n, real *a, integer *lda, integer *ipiv,
  339. real *work, integer *lwork, integer *info)
  340. {
  341. /* System generated locals */
  342. integer a_dim1, a_offset, i__1, i__2, i__3;
  343. /* Local variables */
  344. integer i__, j, nbmin;
  345. extern /* Subroutine */ void sgemm_(char *, char *, integer *, integer *,
  346. integer *, real *, real *, integer *, real *, integer *, real *,
  347. real *, integer *), sgemv_(char *, integer *,
  348. integer *, real *, real *, integer *, real *, integer *, real *,
  349. real *, integer *), sswap_(integer *, real *, integer *,
  350. real *, integer *), strsm_(char *, char *, char *, char *,
  351. integer *, integer *, real *, real *, integer *, real *, integer *
  352. );
  353. integer jb, nb, jj, jp, nn;
  354. extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
  355. extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
  356. integer *, integer *, ftnlen, ftnlen);
  357. integer ldwork, lwkopt;
  358. logical lquery;
  359. extern /* Subroutine */ int strtri_(char *, char *, integer *, real *,
  360. integer *, integer *);
  361. integer iws;
  362. /* -- LAPACK computational routine (version 3.7.0) -- */
  363. /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */
  364. /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
  365. /* December 2016 */
  366. /* ===================================================================== */
  367. /* Test the input parameters. */
  368. /* Parameter adjustments */
  369. a_dim1 = *lda;
  370. a_offset = 1 + a_dim1 * 1;
  371. a -= a_offset;
  372. --ipiv;
  373. --work;
  374. /* Function Body */
  375. *info = 0;
  376. nb = ilaenv_(&c__1, "SGETRI", " ", n, &c_n1, &c_n1, &c_n1, (ftnlen)6, (
  377. ftnlen)1);
  378. lwkopt = *n * nb;
  379. work[1] = (real) lwkopt;
  380. lquery = *lwork == -1;
  381. if (*n < 0) {
  382. *info = -1;
  383. } else if (*lda < f2cmax(1,*n)) {
  384. *info = -3;
  385. } else if (*lwork < f2cmax(1,*n) && ! lquery) {
  386. *info = -6;
  387. }
  388. if (*info != 0) {
  389. i__1 = -(*info);
  390. xerbla_("SGETRI", &i__1, (ftnlen)6);
  391. return;
  392. } else if (lquery) {
  393. return;
  394. }
  395. /* Quick return if possible */
  396. if (*n == 0) {
  397. return;
  398. }
  399. /* Form inv(U). If INFO > 0 from STRTRI, then U is singular, */
  400. /* and the inverse is not computed. */
  401. strtri_("Upper", "Non-unit", n, &a[a_offset], lda, info);
  402. if (*info > 0) {
  403. return;
  404. }
  405. nbmin = 2;
  406. ldwork = *n;
  407. if (nb > 1 && nb < *n) {
  408. /* Computing MAX */
  409. i__1 = ldwork * nb;
  410. iws = f2cmax(i__1,1);
  411. if (*lwork < iws) {
  412. nb = *lwork / ldwork;
  413. /* Computing MAX */
  414. i__1 = 2, i__2 = ilaenv_(&c__2, "SGETRI", " ", n, &c_n1, &c_n1, &
  415. c_n1, (ftnlen)6, (ftnlen)1);
  416. nbmin = f2cmax(i__1,i__2);
  417. }
  418. } else {
  419. iws = *n;
  420. }
  421. /* Solve the equation inv(A)*L = inv(U) for inv(A). */
  422. if (nb < nbmin || nb >= *n) {
  423. /* Use unblocked code. */
  424. for (j = *n; j >= 1; --j) {
  425. /* Copy current column of L to WORK and replace with zeros. */
  426. i__1 = *n;
  427. for (i__ = j + 1; i__ <= i__1; ++i__) {
  428. work[i__] = a[i__ + j * a_dim1];
  429. a[i__ + j * a_dim1] = 0.f;
  430. /* L10: */
  431. }
  432. /* Compute current column of inv(A). */
  433. if (j < *n) {
  434. i__1 = *n - j;
  435. sgemv_("No transpose", n, &i__1, &c_b20, &a[(j + 1) * a_dim1
  436. + 1], lda, &work[j + 1], &c__1, &c_b22, &a[j * a_dim1
  437. + 1], &c__1);
  438. }
  439. /* L20: */
  440. }
  441. } else {
  442. /* Use blocked code. */
  443. nn = (*n - 1) / nb * nb + 1;
  444. i__1 = -nb;
  445. for (j = nn; i__1 < 0 ? j >= 1 : j <= 1; j += i__1) {
  446. /* Computing MIN */
  447. i__2 = nb, i__3 = *n - j + 1;
  448. jb = f2cmin(i__2,i__3);
  449. /* Copy current block column of L to WORK and replace with */
  450. /* zeros. */
  451. i__2 = j + jb - 1;
  452. for (jj = j; jj <= i__2; ++jj) {
  453. i__3 = *n;
  454. for (i__ = jj + 1; i__ <= i__3; ++i__) {
  455. work[i__ + (jj - j) * ldwork] = a[i__ + jj * a_dim1];
  456. a[i__ + jj * a_dim1] = 0.f;
  457. /* L30: */
  458. }
  459. /* L40: */
  460. }
  461. /* Compute current block column of inv(A). */
  462. if (j + jb <= *n) {
  463. i__2 = *n - j - jb + 1;
  464. sgemm_("No transpose", "No transpose", n, &jb, &i__2, &c_b20,
  465. &a[(j + jb) * a_dim1 + 1], lda, &work[j + jb], &
  466. ldwork, &c_b22, &a[j * a_dim1 + 1], lda);
  467. }
  468. strsm_("Right", "Lower", "No transpose", "Unit", n, &jb, &c_b22, &
  469. work[j], &ldwork, &a[j * a_dim1 + 1], lda);
  470. /* L50: */
  471. }
  472. }
  473. /* Apply column interchanges. */
  474. for (j = *n - 1; j >= 1; --j) {
  475. jp = ipiv[j];
  476. if (jp != j) {
  477. sswap_(n, &a[j * a_dim1 + 1], &c__1, &a[jp * a_dim1 + 1], &c__1);
  478. }
  479. /* L60: */
  480. }
  481. work[1] = (real) iws;
  482. return;
  483. /* End of SGETRI */
  484. } /* sgetri_ */