glpmat.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /* glpmat.c */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #include "glpenv.h"
  24. #include "glpmat.h"
  25. #include "glpqmd.h"
  26. #include "amd.h"
  27. #include "colamd.h"
  28. /*----------------------------------------------------------------------
  29. -- check_fvs - check sparse vector in full-vector storage format.
  30. --
  31. -- SYNOPSIS
  32. --
  33. -- #include "glpmat.h"
  34. -- int check_fvs(int n, int nnz, int ind[], double vec[]);
  35. --
  36. -- DESCRIPTION
  37. --
  38. -- The routine check_fvs checks if a given vector of dimension n in
  39. -- full-vector storage format has correct representation.
  40. --
  41. -- RETURNS
  42. --
  43. -- The routine returns one of the following codes:
  44. --
  45. -- 0 - the vector is correct;
  46. -- 1 - the number of elements (n) is negative;
  47. -- 2 - the number of non-zero elements (nnz) is negative;
  48. -- 3 - some element index is out of range;
  49. -- 4 - some element index is duplicate;
  50. -- 5 - some non-zero element is out of pattern. */
  51. int check_fvs(int n, int nnz, int ind[], double vec[])
  52. { int i, t, ret, *flag = NULL;
  53. /* check the number of elements */
  54. if (n < 0)
  55. { ret = 1;
  56. goto done;
  57. }
  58. /* check the number of non-zero elements */
  59. if (nnz < 0)
  60. { ret = 2;
  61. goto done;
  62. }
  63. /* check vector indices */
  64. flag = xcalloc(1+n, sizeof(int));
  65. for (i = 1; i <= n; i++) flag[i] = 0;
  66. for (t = 1; t <= nnz; t++)
  67. { i = ind[t];
  68. if (!(1 <= i && i <= n))
  69. { ret = 3;
  70. goto done;
  71. }
  72. if (flag[i])
  73. { ret = 4;
  74. goto done;
  75. }
  76. flag[i] = 1;
  77. }
  78. /* check vector elements */
  79. for (i = 1; i <= n; i++)
  80. { if (!flag[i] && vec[i] != 0.0)
  81. { ret = 5;
  82. goto done;
  83. }
  84. }
  85. /* the vector is ok */
  86. ret = 0;
  87. done: if (flag != NULL) xfree(flag);
  88. return ret;
  89. }
  90. /*----------------------------------------------------------------------
  91. -- check_pattern - check pattern of sparse matrix.
  92. --
  93. -- SYNOPSIS
  94. --
  95. -- #include "glpmat.h"
  96. -- int check_pattern(int m, int n, int A_ptr[], int A_ind[]);
  97. --
  98. -- DESCRIPTION
  99. --
  100. -- The routine check_pattern checks the pattern of a given mxn matrix
  101. -- in storage-by-rows format.
  102. --
  103. -- RETURNS
  104. --
  105. -- The routine returns one of the following codes:
  106. --
  107. -- 0 - the pattern is correct;
  108. -- 1 - the number of rows (m) is negative;
  109. -- 2 - the number of columns (n) is negative;
  110. -- 3 - A_ptr[1] is not 1;
  111. -- 4 - some column index is out of range;
  112. -- 5 - some column indices are duplicate. */
  113. int check_pattern(int m, int n, int A_ptr[], int A_ind[])
  114. { int i, j, ptr, ret, *flag = NULL;
  115. /* check the number of rows */
  116. if (m < 0)
  117. { ret = 1;
  118. goto done;
  119. }
  120. /* check the number of columns */
  121. if (n < 0)
  122. { ret = 2;
  123. goto done;
  124. }
  125. /* check location A_ptr[1] */
  126. if (A_ptr[1] != 1)
  127. { ret = 3;
  128. goto done;
  129. }
  130. /* check row patterns */
  131. flag = xcalloc(1+n, sizeof(int));
  132. for (j = 1; j <= n; j++) flag[j] = 0;
  133. for (i = 1; i <= m; i++)
  134. { /* check pattern of row i */
  135. for (ptr = A_ptr[i]; ptr < A_ptr[i+1]; ptr++)
  136. { j = A_ind[ptr];
  137. /* check column index */
  138. if (!(1 <= j && j <= n))
  139. { ret = 4;
  140. goto done;
  141. }
  142. /* check for duplication */
  143. if (flag[j])
  144. { ret = 5;
  145. goto done;
  146. }
  147. flag[j] = 1;
  148. }
  149. /* clear flags */
  150. for (ptr = A_ptr[i]; ptr < A_ptr[i+1]; ptr++)
  151. { j = A_ind[ptr];
  152. flag[j] = 0;
  153. }
  154. }
  155. /* the pattern is ok */
  156. ret = 0;
  157. done: if (flag != NULL) xfree(flag);
  158. return ret;
  159. }
  160. /*----------------------------------------------------------------------
  161. -- transpose - transpose sparse matrix.
  162. --
  163. -- *Synopsis*
  164. --
  165. -- #include "glpmat.h"
  166. -- void transpose(int m, int n, int A_ptr[], int A_ind[],
  167. -- double A_val[], int AT_ptr[], int AT_ind[], double AT_val[]);
  168. --
  169. -- *Description*
  170. --
  171. -- For a given mxn sparse matrix A the routine transpose builds a nxm
  172. -- sparse matrix A' which is a matrix transposed to A.
  173. --
  174. -- The arrays A_ptr, A_ind, and A_val specify a given mxn matrix A to
  175. -- be transposed in storage-by-rows format. The parameter A_val can be
  176. -- NULL, in which case numeric values are not copied. The arrays A_ptr,
  177. -- A_ind, and A_val are not changed on exit.
  178. --
  179. -- On entry the arrays AT_ptr, AT_ind, and AT_val must be allocated,
  180. -- but their content is ignored. On exit the routine stores a resultant
  181. -- nxm matrix A' in these arrays in storage-by-rows format. Note that
  182. -- if the parameter A_val is NULL, the array AT_val is not used.
  183. --
  184. -- The routine transpose has a side effect that elements in rows of the
  185. -- resultant matrix A' follow in ascending their column indices. */
  186. void transpose(int m, int n, int A_ptr[], int A_ind[], double A_val[],
  187. int AT_ptr[], int AT_ind[], double AT_val[])
  188. { int i, j, t, beg, end, pos, len;
  189. /* determine row lengths of resultant matrix */
  190. for (j = 1; j <= n; j++) AT_ptr[j] = 0;
  191. for (i = 1; i <= m; i++)
  192. { beg = A_ptr[i], end = A_ptr[i+1];
  193. for (t = beg; t < end; t++) AT_ptr[A_ind[t]]++;
  194. }
  195. /* set up row pointers of resultant matrix */
  196. pos = 1;
  197. for (j = 1; j <= n; j++)
  198. len = AT_ptr[j], pos += len, AT_ptr[j] = pos;
  199. AT_ptr[n+1] = pos;
  200. /* build resultant matrix */
  201. for (i = m; i >= 1; i--)
  202. { beg = A_ptr[i], end = A_ptr[i+1];
  203. for (t = beg; t < end; t++)
  204. { pos = --AT_ptr[A_ind[t]];
  205. AT_ind[pos] = i;
  206. if (A_val != NULL) AT_val[pos] = A_val[t];
  207. }
  208. }
  209. return;
  210. }
  211. /*----------------------------------------------------------------------
  212. -- adat_symbolic - compute S = P*A*D*A'*P' (symbolic phase).
  213. --
  214. -- *Synopsis*
  215. --
  216. -- #include "glpmat.h"
  217. -- int *adat_symbolic(int m, int n, int P_per[], int A_ptr[],
  218. -- int A_ind[], int S_ptr[]);
  219. --
  220. -- *Description*
  221. --
  222. -- The routine adat_symbolic implements the symbolic phase to compute
  223. -- symmetric matrix S = P*A*D*A'*P', where P is a permutation matrix,
  224. -- A is a given sparse matrix, D is a diagonal matrix, A' is a matrix
  225. -- transposed to A, P' is an inverse of P.
  226. --
  227. -- The parameter m is the number of rows in A and the order of P.
  228. --
  229. -- The parameter n is the number of columns in A and the order of D.
  230. --
  231. -- The array P_per specifies permutation matrix P. It is not changed on
  232. -- exit.
  233. --
  234. -- The arrays A_ptr and A_ind specify the pattern of matrix A. They are
  235. -- not changed on exit.
  236. --
  237. -- On exit the routine stores the pattern of upper triangular part of
  238. -- matrix S without diagonal elements in the arrays S_ptr and S_ind in
  239. -- storage-by-rows format. The array S_ptr should be allocated on entry,
  240. -- however, its content is ignored. The array S_ind is allocated by the
  241. -- routine itself which returns a pointer to it.
  242. --
  243. -- *Returns*
  244. --
  245. -- The routine returns a pointer to the array S_ind. */
  246. int *adat_symbolic(int m, int n, int P_per[], int A_ptr[], int A_ind[],
  247. int S_ptr[])
  248. { int i, j, t, ii, jj, tt, k, size, len;
  249. int *S_ind, *AT_ptr, *AT_ind, *ind, *map, *temp;
  250. /* build the pattern of A', which is a matrix transposed to A, to
  251. efficiently access A in column-wise manner */
  252. AT_ptr = xcalloc(1+n+1, sizeof(int));
  253. AT_ind = xcalloc(A_ptr[m+1], sizeof(int));
  254. transpose(m, n, A_ptr, A_ind, NULL, AT_ptr, AT_ind, NULL);
  255. /* allocate the array S_ind */
  256. size = A_ptr[m+1] - 1;
  257. if (size < m) size = m;
  258. S_ind = xcalloc(1+size, sizeof(int));
  259. /* allocate and initialize working arrays */
  260. ind = xcalloc(1+m, sizeof(int));
  261. map = xcalloc(1+m, sizeof(int));
  262. for (jj = 1; jj <= m; jj++) map[jj] = 0;
  263. /* compute pattern of S; note that symbolically S = B*B', where
  264. B = P*A, B' is matrix transposed to B */
  265. S_ptr[1] = 1;
  266. for (ii = 1; ii <= m; ii++)
  267. { /* compute pattern of ii-th row of S */
  268. len = 0;
  269. i = P_per[ii]; /* i-th row of A = ii-th row of B */
  270. for (t = A_ptr[i]; t < A_ptr[i+1]; t++)
  271. { k = A_ind[t];
  272. /* walk through k-th column of A */
  273. for (tt = AT_ptr[k]; tt < AT_ptr[k+1]; tt++)
  274. { j = AT_ind[tt];
  275. jj = P_per[m+j]; /* j-th row of A = jj-th row of B */
  276. /* a[i,k] != 0 and a[j,k] != 0 ergo s[ii,jj] != 0 */
  277. if (ii < jj && !map[jj]) ind[++len] = jj, map[jj] = 1;
  278. }
  279. }
  280. /* now (ind) is pattern of ii-th row of S */
  281. S_ptr[ii+1] = S_ptr[ii] + len;
  282. /* at least (S_ptr[ii+1] - 1) locations should be available in
  283. the array S_ind */
  284. if (S_ptr[ii+1] - 1 > size)
  285. { temp = S_ind;
  286. size += size;
  287. S_ind = xcalloc(1+size, sizeof(int));
  288. memcpy(&S_ind[1], &temp[1], (S_ptr[ii] - 1) * sizeof(int));
  289. xfree(temp);
  290. }
  291. xassert(S_ptr[ii+1] - 1 <= size);
  292. /* (ii-th row of S) := (ind) */
  293. memcpy(&S_ind[S_ptr[ii]], &ind[1], len * sizeof(int));
  294. /* clear the row pattern map */
  295. for (t = 1; t <= len; t++) map[ind[t]] = 0;
  296. }
  297. /* free working arrays */
  298. xfree(AT_ptr);
  299. xfree(AT_ind);
  300. xfree(ind);
  301. xfree(map);
  302. /* reallocate the array S_ind to free unused locations */
  303. temp = S_ind;
  304. size = S_ptr[m+1] - 1;
  305. S_ind = xcalloc(1+size, sizeof(int));
  306. memcpy(&S_ind[1], &temp[1], size * sizeof(int));
  307. xfree(temp);
  308. return S_ind;
  309. }
  310. /*----------------------------------------------------------------------
  311. -- adat_numeric - compute S = P*A*D*A'*P' (numeric phase).
  312. --
  313. -- *Synopsis*
  314. --
  315. -- #include "glpmat.h"
  316. -- void adat_numeric(int m, int n, int P_per[],
  317. -- int A_ptr[], int A_ind[], double A_val[], double D_diag[],
  318. -- int S_ptr[], int S_ind[], double S_val[], double S_diag[]);
  319. --
  320. -- *Description*
  321. --
  322. -- The routine adat_numeric implements the numeric phase to compute
  323. -- symmetric matrix S = P*A*D*A'*P', where P is a permutation matrix,
  324. -- A is a given sparse matrix, D is a diagonal matrix, A' is a matrix
  325. -- transposed to A, P' is an inverse of P.
  326. --
  327. -- The parameter m is the number of rows in A and the order of P.
  328. --
  329. -- The parameter n is the number of columns in A and the order of D.
  330. --
  331. -- The matrix P is specified in the array P_per, which is not changed
  332. -- on exit.
  333. --
  334. -- The matrix A is specified in the arrays A_ptr, A_ind, and A_val in
  335. -- storage-by-rows format. These arrays are not changed on exit.
  336. --
  337. -- Diagonal elements of the matrix D are specified in the array D_diag,
  338. -- where D_diag[0] is not used, D_diag[i] = d[i,i] for i = 1, ..., n.
  339. -- The array D_diag is not changed on exit.
  340. --
  341. -- The pattern of the upper triangular part of the matrix S without
  342. -- diagonal elements (previously computed by the routine adat_symbolic)
  343. -- is specified in the arrays S_ptr and S_ind, which are not changed on
  344. -- exit. Numeric values of non-diagonal elements of S are stored in
  345. -- corresponding locations of the array S_val, and values of diagonal
  346. -- elements of S are stored in locations S_diag[1], ..., S_diag[n]. */
  347. void adat_numeric(int m, int n, int P_per[],
  348. int A_ptr[], int A_ind[], double A_val[], double D_diag[],
  349. int S_ptr[], int S_ind[], double S_val[], double S_diag[])
  350. { int i, j, t, ii, jj, tt, beg, end, beg1, end1, k;
  351. double sum, *work;
  352. work = xcalloc(1+n, sizeof(double));
  353. for (j = 1; j <= n; j++) work[j] = 0.0;
  354. /* compute S = B*D*B', where B = P*A, B' is a matrix transposed
  355. to B */
  356. for (ii = 1; ii <= m; ii++)
  357. { i = P_per[ii]; /* i-th row of A = ii-th row of B */
  358. /* (work) := (i-th row of A) */
  359. beg = A_ptr[i], end = A_ptr[i+1];
  360. for (t = beg; t < end; t++)
  361. work[A_ind[t]] = A_val[t];
  362. /* compute ii-th row of S */
  363. beg = S_ptr[ii], end = S_ptr[ii+1];
  364. for (t = beg; t < end; t++)
  365. { jj = S_ind[t];
  366. j = P_per[jj]; /* j-th row of A = jj-th row of B */
  367. /* s[ii,jj] := sum a[i,k] * d[k,k] * a[j,k] */
  368. sum = 0.0;
  369. beg1 = A_ptr[j], end1 = A_ptr[j+1];
  370. for (tt = beg1; tt < end1; tt++)
  371. { k = A_ind[tt];
  372. sum += work[k] * D_diag[k] * A_val[tt];
  373. }
  374. S_val[t] = sum;
  375. }
  376. /* s[ii,ii] := sum a[i,k] * d[k,k] * a[i,k] */
  377. sum = 0.0;
  378. beg = A_ptr[i], end = A_ptr[i+1];
  379. for (t = beg; t < end; t++)
  380. { k = A_ind[t];
  381. sum += A_val[t] * D_diag[k] * A_val[t];
  382. work[k] = 0.0;
  383. }
  384. S_diag[ii] = sum;
  385. }
  386. xfree(work);
  387. return;
  388. }
  389. /*----------------------------------------------------------------------
  390. -- min_degree - minimum degree ordering.
  391. --
  392. -- *Synopsis*
  393. --
  394. -- #include "glpmat.h"
  395. -- void min_degree(int n, int A_ptr[], int A_ind[], int P_per[]);
  396. --
  397. -- *Description*
  398. --
  399. -- The routine min_degree uses the minimum degree ordering algorithm
  400. -- to find a permutation matrix P for a given sparse symmetric positive
  401. -- matrix A which minimizes the number of non-zeros in upper triangular
  402. -- factor U for Cholesky factorization P*A*P' = U'*U.
  403. --
  404. -- The parameter n is the order of matrices A and P.
  405. --
  406. -- The pattern of the given matrix A is specified on entry in the arrays
  407. -- A_ptr and A_ind in storage-by-rows format. Only the upper triangular
  408. -- part without diagonal elements (which all are assumed to be non-zero)
  409. -- should be specified as if A were upper triangular. The arrays A_ptr
  410. -- and A_ind are not changed on exit.
  411. --
  412. -- The permutation matrix P is stored by the routine in the array P_per
  413. -- on exit.
  414. --
  415. -- *Algorithm*
  416. --
  417. -- The routine min_degree is based on some subroutines from the package
  418. -- SPARSPAK (see comments in the module glpqmd). */
  419. void min_degree(int n, int A_ptr[], int A_ind[], int P_per[])
  420. { int i, j, ne, t, pos, len;
  421. int *xadj, *adjncy, *deg, *marker, *rchset, *nbrhd, *qsize,
  422. *qlink, nofsub;
  423. /* determine number of non-zeros in complete pattern */
  424. ne = A_ptr[n+1] - 1;
  425. ne += ne;
  426. /* allocate working arrays */
  427. xadj = xcalloc(1+n+1, sizeof(int));
  428. adjncy = xcalloc(1+ne, sizeof(int));
  429. deg = xcalloc(1+n, sizeof(int));
  430. marker = xcalloc(1+n, sizeof(int));
  431. rchset = xcalloc(1+n, sizeof(int));
  432. nbrhd = xcalloc(1+n, sizeof(int));
  433. qsize = xcalloc(1+n, sizeof(int));
  434. qlink = xcalloc(1+n, sizeof(int));
  435. /* determine row lengths in complete pattern */
  436. for (i = 1; i <= n; i++) xadj[i] = 0;
  437. for (i = 1; i <= n; i++)
  438. { for (t = A_ptr[i]; t < A_ptr[i+1]; t++)
  439. { j = A_ind[t];
  440. xassert(i < j && j <= n);
  441. xadj[i]++, xadj[j]++;
  442. }
  443. }
  444. /* set up row pointers for complete pattern */
  445. pos = 1;
  446. for (i = 1; i <= n; i++)
  447. len = xadj[i], pos += len, xadj[i] = pos;
  448. xadj[n+1] = pos;
  449. xassert(pos - 1 == ne);
  450. /* construct complete pattern */
  451. for (i = 1; i <= n; i++)
  452. { for (t = A_ptr[i]; t < A_ptr[i+1]; t++)
  453. { j = A_ind[t];
  454. adjncy[--xadj[i]] = j, adjncy[--xadj[j]] = i;
  455. }
  456. }
  457. /* call the main minimimum degree ordering routine */
  458. genqmd(&n, xadj, adjncy, P_per, P_per + n, deg, marker, rchset,
  459. nbrhd, qsize, qlink, &nofsub);
  460. /* make sure that permutation matrix P is correct */
  461. for (i = 1; i <= n; i++)
  462. { j = P_per[i];
  463. xassert(1 <= j && j <= n);
  464. xassert(P_per[n+j] == i);
  465. }
  466. /* free working arrays */
  467. xfree(xadj);
  468. xfree(adjncy);
  469. xfree(deg);
  470. xfree(marker);
  471. xfree(rchset);
  472. xfree(nbrhd);
  473. xfree(qsize);
  474. xfree(qlink);
  475. return;
  476. }
  477. /**********************************************************************/
  478. void amd_order1(int n, int A_ptr[], int A_ind[], int P_per[])
  479. { /* approximate minimum degree ordering (AMD) */
  480. int k, ret;
  481. double Control[AMD_CONTROL], Info[AMD_INFO];
  482. /* get the default parameters */
  483. amd_defaults(Control);
  484. #if 0
  485. /* and print them */
  486. amd_control(Control);
  487. #endif
  488. /* make all indices 0-based */
  489. for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]--;
  490. for (k = 1; k <= n+1; k++) A_ptr[k]--;
  491. /* call the ordering routine */
  492. ret = amd_order(n, &A_ptr[1], &A_ind[1], &P_per[1], Control, Info)
  493. ;
  494. #if 0
  495. amd_info(Info);
  496. #endif
  497. xassert(ret == AMD_OK || ret == AMD_OK_BUT_JUMBLED);
  498. /* retsore 1-based indices */
  499. for (k = 1; k <= n+1; k++) A_ptr[k]++;
  500. for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]++;
  501. /* patch up permutation matrix */
  502. memset(&P_per[n+1], 0, n * sizeof(int));
  503. for (k = 1; k <= n; k++)
  504. { P_per[k]++;
  505. xassert(1 <= P_per[k] && P_per[k] <= n);
  506. xassert(P_per[n+P_per[k]] == 0);
  507. P_per[n+P_per[k]] = k;
  508. }
  509. return;
  510. }
  511. /**********************************************************************/
  512. static void *allocate(size_t n, size_t size)
  513. { void *ptr;
  514. ptr = xcalloc(n, size);
  515. memset(ptr, 0, n * size);
  516. return ptr;
  517. }
  518. static void release(void *ptr)
  519. { xfree(ptr);
  520. return;
  521. }
  522. void symamd_ord(int n, int A_ptr[], int A_ind[], int P_per[])
  523. { /* approximate minimum degree ordering (SYMAMD) */
  524. int k, ok;
  525. int stats[COLAMD_STATS];
  526. /* make all indices 0-based */
  527. for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]--;
  528. for (k = 1; k <= n+1; k++) A_ptr[k]--;
  529. /* call the ordering routine */
  530. ok = symamd(n, &A_ind[1], &A_ptr[1], &P_per[1], NULL, stats,
  531. allocate, release);
  532. #if 0
  533. symamd_report(stats);
  534. #endif
  535. xassert(ok);
  536. /* restore 1-based indices */
  537. for (k = 1; k <= n+1; k++) A_ptr[k]++;
  538. for (k = 1; k < A_ptr[n+1]; k++) A_ind[k]++;
  539. /* patch up permutation matrix */
  540. memset(&P_per[n+1], 0, n * sizeof(int));
  541. for (k = 1; k <= n; k++)
  542. { P_per[k]++;
  543. xassert(1 <= P_per[k] && P_per[k] <= n);
  544. xassert(P_per[n+P_per[k]] == 0);
  545. P_per[n+P_per[k]] = k;
  546. }
  547. return;
  548. }
  549. /*----------------------------------------------------------------------
  550. -- chol_symbolic - compute Cholesky factorization (symbolic phase).
  551. --
  552. -- *Synopsis*
  553. --
  554. -- #include "glpmat.h"
  555. -- int *chol_symbolic(int n, int A_ptr[], int A_ind[], int U_ptr[]);
  556. --
  557. -- *Description*
  558. --
  559. -- The routine chol_symbolic implements the symbolic phase of Cholesky
  560. -- factorization A = U'*U, where A is a given sparse symmetric positive
  561. -- definite matrix, U is a resultant upper triangular factor, U' is a
  562. -- matrix transposed to U.
  563. --
  564. -- The parameter n is the order of matrices A and U.
  565. --
  566. -- The pattern of the given matrix A is specified on entry in the arrays
  567. -- A_ptr and A_ind in storage-by-rows format. Only the upper triangular
  568. -- part without diagonal elements (which all are assumed to be non-zero)
  569. -- should be specified as if A were upper triangular. The arrays A_ptr
  570. -- and A_ind are not changed on exit.
  571. --
  572. -- The pattern of the matrix U without diagonal elements (which all are
  573. -- assumed to be non-zero) is stored on exit from the routine in the
  574. -- arrays U_ptr and U_ind in storage-by-rows format. The array U_ptr
  575. -- should be allocated on entry, however, its content is ignored. The
  576. -- array U_ind is allocated by the routine which returns a pointer to it
  577. -- on exit.
  578. --
  579. -- *Returns*
  580. --
  581. -- The routine returns a pointer to the array U_ind.
  582. --
  583. -- *Method*
  584. --
  585. -- The routine chol_symbolic computes the pattern of the matrix U in a
  586. -- row-wise manner. No pivoting is used.
  587. --
  588. -- It is known that to compute the pattern of row k of the matrix U we
  589. -- need to merge the pattern of row k of the matrix A and the patterns
  590. -- of each row i of U, where u[i,k] is non-zero (these rows are already
  591. -- computed and placed above row k).
  592. --
  593. -- However, to reduce the number of rows to be merged the routine uses
  594. -- an advanced algorithm proposed in:
  595. --
  596. -- D.J.Rose, R.E.Tarjan, and G.S.Lueker. Algorithmic aspects of vertex
  597. -- elimination on graphs. SIAM J. Comput. 5, 1976, 266-83.
  598. --
  599. -- The authors of the cited paper show that we have the same result if
  600. -- we merge row k of the matrix A and such rows of the matrix U (among
  601. -- rows 1, ..., k-1) whose leftmost non-diagonal non-zero element is
  602. -- placed in k-th column. This feature signficantly reduces the number
  603. -- of rows to be merged, especially on the final steps, where rows of
  604. -- the matrix U become quite dense.
  605. --
  606. -- To determine rows, which should be merged on k-th step, for a fixed
  607. -- time the routine uses linked lists of row numbers of the matrix U.
  608. -- Location head[k] contains the number of a first row, whose leftmost
  609. -- non-diagonal non-zero element is placed in column k, and location
  610. -- next[i] contains the number of a next row with the same property as
  611. -- row i. */
  612. int *chol_symbolic(int n, int A_ptr[], int A_ind[], int U_ptr[])
  613. { int i, j, k, t, len, size, beg, end, min_j, *U_ind, *head, *next,
  614. *ind, *map, *temp;
  615. /* initially we assume that on computing the pattern of U fill-in
  616. will double the number of non-zeros in A */
  617. size = A_ptr[n+1] - 1;
  618. if (size < n) size = n;
  619. size += size;
  620. U_ind = xcalloc(1+size, sizeof(int));
  621. /* allocate and initialize working arrays */
  622. head = xcalloc(1+n, sizeof(int));
  623. for (i = 1; i <= n; i++) head[i] = 0;
  624. next = xcalloc(1+n, sizeof(int));
  625. ind = xcalloc(1+n, sizeof(int));
  626. map = xcalloc(1+n, sizeof(int));
  627. for (j = 1; j <= n; j++) map[j] = 0;
  628. /* compute the pattern of matrix U */
  629. U_ptr[1] = 1;
  630. for (k = 1; k <= n; k++)
  631. { /* compute the pattern of k-th row of U, which is the union of
  632. k-th row of A and those rows of U (among 1, ..., k-1) whose
  633. leftmost non-diagonal non-zero is placed in k-th column */
  634. /* (ind) := (k-th row of A) */
  635. len = A_ptr[k+1] - A_ptr[k];
  636. memcpy(&ind[1], &A_ind[A_ptr[k]], len * sizeof(int));
  637. for (t = 1; t <= len; t++)
  638. { j = ind[t];
  639. xassert(k < j && j <= n);
  640. map[j] = 1;
  641. }
  642. /* walk through rows of U whose leftmost non-diagonal non-zero
  643. is placed in k-th column */
  644. for (i = head[k]; i != 0; i = next[i])
  645. { /* (ind) := (ind) union (i-th row of U) */
  646. beg = U_ptr[i], end = U_ptr[i+1];
  647. for (t = beg; t < end; t++)
  648. { j = U_ind[t];
  649. if (j > k && !map[j]) ind[++len] = j, map[j] = 1;
  650. }
  651. }
  652. /* now (ind) is the pattern of k-th row of U */
  653. U_ptr[k+1] = U_ptr[k] + len;
  654. /* at least (U_ptr[k+1] - 1) locations should be available in
  655. the array U_ind */
  656. if (U_ptr[k+1] - 1 > size)
  657. { temp = U_ind;
  658. size += size;
  659. U_ind = xcalloc(1+size, sizeof(int));
  660. memcpy(&U_ind[1], &temp[1], (U_ptr[k] - 1) * sizeof(int));
  661. xfree(temp);
  662. }
  663. xassert(U_ptr[k+1] - 1 <= size);
  664. /* (k-th row of U) := (ind) */
  665. memcpy(&U_ind[U_ptr[k]], &ind[1], len * sizeof(int));
  666. /* determine column index of leftmost non-diagonal non-zero in
  667. k-th row of U and clear the row pattern map */
  668. min_j = n + 1;
  669. for (t = 1; t <= len; t++)
  670. { j = ind[t], map[j] = 0;
  671. if (min_j > j) min_j = j;
  672. }
  673. /* include k-th row into corresponding linked list */
  674. if (min_j <= n) next[k] = head[min_j], head[min_j] = k;
  675. }
  676. /* free working arrays */
  677. xfree(head);
  678. xfree(next);
  679. xfree(ind);
  680. xfree(map);
  681. /* reallocate the array U_ind to free unused locations */
  682. temp = U_ind;
  683. size = U_ptr[n+1] - 1;
  684. U_ind = xcalloc(1+size, sizeof(int));
  685. memcpy(&U_ind[1], &temp[1], size * sizeof(int));
  686. xfree(temp);
  687. return U_ind;
  688. }
  689. /*----------------------------------------------------------------------
  690. -- chol_numeric - compute Cholesky factorization (numeric phase).
  691. --
  692. -- *Synopsis*
  693. --
  694. -- #include "glpmat.h"
  695. -- int chol_numeric(int n,
  696. -- int A_ptr[], int A_ind[], double A_val[], double A_diag[],
  697. -- int U_ptr[], int U_ind[], double U_val[], double U_diag[]);
  698. --
  699. -- *Description*
  700. --
  701. -- The routine chol_symbolic implements the numeric phase of Cholesky
  702. -- factorization A = U'*U, where A is a given sparse symmetric positive
  703. -- definite matrix, U is a resultant upper triangular factor, U' is a
  704. -- matrix transposed to U.
  705. --
  706. -- The parameter n is the order of matrices A and U.
  707. --
  708. -- Upper triangular part of the matrix A without diagonal elements is
  709. -- specified in the arrays A_ptr, A_ind, and A_val in storage-by-rows
  710. -- format. Diagonal elements of A are specified in the array A_diag,
  711. -- where A_diag[0] is not used, A_diag[i] = a[i,i] for i = 1, ..., n.
  712. -- The arrays A_ptr, A_ind, A_val, and A_diag are not changed on exit.
  713. --
  714. -- The pattern of the matrix U without diagonal elements (previously
  715. -- computed with the routine chol_symbolic) is specified in the arrays
  716. -- U_ptr and U_ind, which are not changed on exit. Numeric values of
  717. -- non-diagonal elements of U are stored in corresponding locations of
  718. -- the array U_val, and values of diagonal elements of U are stored in
  719. -- locations U_diag[1], ..., U_diag[n].
  720. --
  721. -- *Returns*
  722. --
  723. -- The routine returns the number of non-positive diagonal elements of
  724. -- the matrix U which have been replaced by a huge positive number (see
  725. -- the method description below). Zero return code means the matrix A
  726. -- has been successfully factorized.
  727. --
  728. -- *Method*
  729. --
  730. -- The routine chol_numeric computes the matrix U in a row-wise manner
  731. -- using standard gaussian elimination technique. No pivoting is used.
  732. --
  733. -- Initially the routine sets U = A, and before k-th elimination step
  734. -- the matrix U is the following:
  735. --
  736. -- 1 k n
  737. -- 1 x x x x x x x x x x
  738. -- . x x x x x x x x x
  739. -- . . x x x x x x x x
  740. -- . . . x x x x x x x
  741. -- k . . . . * * * * * *
  742. -- . . . . * * * * * *
  743. -- . . . . * * * * * *
  744. -- . . . . * * * * * *
  745. -- . . . . * * * * * *
  746. -- n . . . . * * * * * *
  747. --
  748. -- where 'x' are elements of already computed rows, '*' are elements of
  749. -- the active submatrix. (Note that the lower triangular part of the
  750. -- active submatrix being symmetric is not stored and diagonal elements
  751. -- are stored separately in the array U_diag.)
  752. --
  753. -- The matrix A is assumed to be positive definite. However, if it is
  754. -- close to semi-definite, on some elimination step a pivot u[k,k] may
  755. -- happen to be non-positive due to round-off errors. In this case the
  756. -- routine uses a technique proposed in:
  757. --
  758. -- S.J.Wright. The Cholesky factorization in interior-point and barrier
  759. -- methods. Preprint MCS-P600-0596, Mathematics and Computer Science
  760. -- Division, Argonne National Laboratory, Argonne, Ill., May 1996.
  761. --
  762. -- The routine just replaces non-positive u[k,k] by a huge positive
  763. -- number. This involves non-diagonal elements in k-th row of U to be
  764. -- close to zero that, in turn, involves k-th component of a solution
  765. -- vector to be close to zero. Note, however, that this technique works
  766. -- only if the system A*x = b is consistent. */
  767. int chol_numeric(int n,
  768. int A_ptr[], int A_ind[], double A_val[], double A_diag[],
  769. int U_ptr[], int U_ind[], double U_val[], double U_diag[])
  770. { int i, j, k, t, t1, beg, end, beg1, end1, count = 0;
  771. double ukk, uki, *work;
  772. work = xcalloc(1+n, sizeof(double));
  773. for (j = 1; j <= n; j++) work[j] = 0.0;
  774. /* U := (upper triangle of A) */
  775. /* note that the upper traingle of A is a subset of U */
  776. for (i = 1; i <= n; i++)
  777. { beg = A_ptr[i], end = A_ptr[i+1];
  778. for (t = beg; t < end; t++)
  779. j = A_ind[t], work[j] = A_val[t];
  780. beg = U_ptr[i], end = U_ptr[i+1];
  781. for (t = beg; t < end; t++)
  782. j = U_ind[t], U_val[t] = work[j], work[j] = 0.0;
  783. U_diag[i] = A_diag[i];
  784. }
  785. /* main elimination loop */
  786. for (k = 1; k <= n; k++)
  787. { /* transform k-th row of U */
  788. ukk = U_diag[k];
  789. if (ukk > 0.0)
  790. U_diag[k] = ukk = sqrt(ukk);
  791. else
  792. U_diag[k] = ukk = DBL_MAX, count++;
  793. /* (work) := (transformed k-th row) */
  794. beg = U_ptr[k], end = U_ptr[k+1];
  795. for (t = beg; t < end; t++)
  796. work[U_ind[t]] = (U_val[t] /= ukk);
  797. /* transform other rows of U */
  798. for (t = beg; t < end; t++)
  799. { i = U_ind[t];
  800. xassert(i > k);
  801. /* (i-th row) := (i-th row) - u[k,i] * (k-th row) */
  802. uki = work[i];
  803. beg1 = U_ptr[i], end1 = U_ptr[i+1];
  804. for (t1 = beg1; t1 < end1; t1++)
  805. U_val[t1] -= uki * work[U_ind[t1]];
  806. U_diag[i] -= uki * uki;
  807. }
  808. /* (work) := 0 */
  809. for (t = beg; t < end; t++)
  810. work[U_ind[t]] = 0.0;
  811. }
  812. xfree(work);
  813. return count;
  814. }
  815. /*----------------------------------------------------------------------
  816. -- u_solve - solve upper triangular system U*x = b.
  817. --
  818. -- *Synopsis*
  819. --
  820. -- #include "glpmat.h"
  821. -- void u_solve(int n, int U_ptr[], int U_ind[], double U_val[],
  822. -- double U_diag[], double x[]);
  823. --
  824. -- *Description*
  825. --
  826. -- The routine u_solve solves an linear system U*x = b, where U is an
  827. -- upper triangular matrix.
  828. --
  829. -- The parameter n is the order of matrix U.
  830. --
  831. -- The matrix U without diagonal elements is specified in the arrays
  832. -- U_ptr, U_ind, and U_val in storage-by-rows format. Diagonal elements
  833. -- of U are specified in the array U_diag, where U_diag[0] is not used,
  834. -- U_diag[i] = u[i,i] for i = 1, ..., n. All these four arrays are not
  835. -- changed on exit.
  836. --
  837. -- The right-hand side vector b is specified on entry in the array x,
  838. -- where x[0] is not used, and x[i] = b[i] for i = 1, ..., n. On exit
  839. -- the routine stores computed components of the vector of unknowns x
  840. -- in the array x in the same manner. */
  841. void u_solve(int n, int U_ptr[], int U_ind[], double U_val[],
  842. double U_diag[], double x[])
  843. { int i, t, beg, end;
  844. double temp;
  845. for (i = n; i >= 1; i--)
  846. { temp = x[i];
  847. beg = U_ptr[i], end = U_ptr[i+1];
  848. for (t = beg; t < end; t++)
  849. temp -= U_val[t] * x[U_ind[t]];
  850. xassert(U_diag[i] != 0.0);
  851. x[i] = temp / U_diag[i];
  852. }
  853. return;
  854. }
  855. /*----------------------------------------------------------------------
  856. -- ut_solve - solve lower triangular system U'*x = b.
  857. --
  858. -- *Synopsis*
  859. --
  860. -- #include "glpmat.h"
  861. -- void ut_solve(int n, int U_ptr[], int U_ind[], double U_val[],
  862. -- double U_diag[], double x[]);
  863. --
  864. -- *Description*
  865. --
  866. -- The routine ut_solve solves an linear system U'*x = b, where U is a
  867. -- matrix transposed to an upper triangular matrix.
  868. --
  869. -- The parameter n is the order of matrix U.
  870. --
  871. -- The matrix U without diagonal elements is specified in the arrays
  872. -- U_ptr, U_ind, and U_val in storage-by-rows format. Diagonal elements
  873. -- of U are specified in the array U_diag, where U_diag[0] is not used,
  874. -- U_diag[i] = u[i,i] for i = 1, ..., n. All these four arrays are not
  875. -- changed on exit.
  876. --
  877. -- The right-hand side vector b is specified on entry in the array x,
  878. -- where x[0] is not used, and x[i] = b[i] for i = 1, ..., n. On exit
  879. -- the routine stores computed components of the vector of unknowns x
  880. -- in the array x in the same manner. */
  881. void ut_solve(int n, int U_ptr[], int U_ind[], double U_val[],
  882. double U_diag[], double x[])
  883. { int i, t, beg, end;
  884. double temp;
  885. for (i = 1; i <= n; i++)
  886. { xassert(U_diag[i] != 0.0);
  887. temp = (x[i] /= U_diag[i]);
  888. if (temp == 0.0) continue;
  889. beg = U_ptr[i], end = U_ptr[i+1];
  890. for (t = beg; t < end; t++)
  891. x[U_ind[t]] -= U_val[t] * temp;
  892. }
  893. return;
  894. }
  895. /* eof */