glpspm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /* glpspm.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 "glphbm.h"
  24. #include "glprgr.h"
  25. #include "glpspm.h"
  26. /***********************************************************************
  27. * NAME
  28. *
  29. * spm_create_mat - create general sparse matrix
  30. *
  31. * SYNOPSIS
  32. *
  33. * #include "glpspm.h"
  34. * SPM *spm_create_mat(int m, int n);
  35. *
  36. * DESCRIPTION
  37. *
  38. * The routine spm_create_mat creates a general sparse matrix having
  39. * m rows and n columns. Being created the matrix is zero (empty), i.e.
  40. * has no elements.
  41. *
  42. * RETURNS
  43. *
  44. * The routine returns a pointer to the matrix created. */
  45. SPM *spm_create_mat(int m, int n)
  46. { SPM *A;
  47. xassert(0 <= m && m < INT_MAX);
  48. xassert(0 <= n && n < INT_MAX);
  49. A = xmalloc(sizeof(SPM));
  50. A->m = m;
  51. A->n = n;
  52. if (m == 0 || n == 0)
  53. { A->pool = NULL;
  54. A->row = NULL;
  55. A->col = NULL;
  56. }
  57. else
  58. { int i, j;
  59. A->pool = dmp_create_pool();
  60. A->row = xcalloc(1+m, sizeof(SPME *));
  61. for (i = 1; i <= m; i++) A->row[i] = NULL;
  62. A->col = xcalloc(1+n, sizeof(SPME *));
  63. for (j = 1; j <= n; j++) A->col[j] = NULL;
  64. }
  65. return A;
  66. }
  67. /***********************************************************************
  68. * NAME
  69. *
  70. * spm_new_elem - add new element to sparse matrix
  71. *
  72. * SYNOPSIS
  73. *
  74. * #include "glpspm.h"
  75. * SPME *spm_new_elem(SPM *A, int i, int j, double val);
  76. *
  77. * DESCRIPTION
  78. *
  79. * The routine spm_new_elem adds a new element to the specified sparse
  80. * matrix. Parameters i, j, and val specify the row number, the column
  81. * number, and a numerical value of the element, respectively.
  82. *
  83. * RETURNS
  84. *
  85. * The routine returns a pointer to the new element added. */
  86. SPME *spm_new_elem(SPM *A, int i, int j, double val)
  87. { SPME *e;
  88. xassert(1 <= i && i <= A->m);
  89. xassert(1 <= j && j <= A->n);
  90. e = dmp_get_atom(A->pool, sizeof(SPME));
  91. e->i = i;
  92. e->j = j;
  93. e->val = val;
  94. e->r_prev = NULL;
  95. e->r_next = A->row[i];
  96. if (e->r_next != NULL) e->r_next->r_prev = e;
  97. e->c_prev = NULL;
  98. e->c_next = A->col[j];
  99. if (e->c_next != NULL) e->c_next->c_prev = e;
  100. A->row[i] = A->col[j] = e;
  101. return e;
  102. }
  103. /***********************************************************************
  104. * NAME
  105. *
  106. * spm_delete_mat - delete general sparse matrix
  107. *
  108. * SYNOPSIS
  109. *
  110. * #include "glpspm.h"
  111. * void spm_delete_mat(SPM *A);
  112. *
  113. * DESCRIPTION
  114. *
  115. * The routine deletes the specified general sparse matrix freeing all
  116. * the memory allocated to this object. */
  117. void spm_delete_mat(SPM *A)
  118. { /* delete sparse matrix */
  119. if (A->pool != NULL) dmp_delete_pool(A->pool);
  120. if (A->row != NULL) xfree(A->row);
  121. if (A->col != NULL) xfree(A->col);
  122. xfree(A);
  123. return;
  124. }
  125. /***********************************************************************
  126. * NAME
  127. *
  128. * spm_test_mat_e - create test sparse matrix of E(n,c) class
  129. *
  130. * SYNOPSIS
  131. *
  132. * #include "glpspm.h"
  133. * SPM *spm_test_mat_e(int n, int c);
  134. *
  135. * DESCRIPTION
  136. *
  137. * The routine spm_test_mat_e creates a test sparse matrix of E(n,c)
  138. * class as described in the book: Ole 0sterby, Zahari Zlatev. Direct
  139. * Methods for Sparse Matrices. Springer-Verlag, 1983.
  140. *
  141. * Matrix of E(n,c) class is a symmetric positive definite matrix of
  142. * the order n. It has the number 4 on its main diagonal and the number
  143. * -1 on its four co-diagonals, two of which are neighbour to the main
  144. * diagonal and two others are shifted from the main diagonal on the
  145. * distance c.
  146. *
  147. * It is necessary that n >= 3 and 2 <= c <= n-1.
  148. *
  149. * RETURNS
  150. *
  151. * The routine returns a pointer to the matrix created. */
  152. SPM *spm_test_mat_e(int n, int c)
  153. { SPM *A;
  154. int i;
  155. xassert(n >= 3 && 2 <= c && c <= n-1);
  156. A = spm_create_mat(n, n);
  157. for (i = 1; i <= n; i++)
  158. spm_new_elem(A, i, i, 4.0);
  159. for (i = 1; i <= n-1; i++)
  160. { spm_new_elem(A, i, i+1, -1.0);
  161. spm_new_elem(A, i+1, i, -1.0);
  162. }
  163. for (i = 1; i <= n-c; i++)
  164. { spm_new_elem(A, i, i+c, -1.0);
  165. spm_new_elem(A, i+c, i, -1.0);
  166. }
  167. return A;
  168. }
  169. /***********************************************************************
  170. * NAME
  171. *
  172. * spm_test_mat_d - create test sparse matrix of D(n,c) class
  173. *
  174. * SYNOPSIS
  175. *
  176. * #include "glpspm.h"
  177. * SPM *spm_test_mat_d(int n, int c);
  178. *
  179. * DESCRIPTION
  180. *
  181. * The routine spm_test_mat_d creates a test sparse matrix of D(n,c)
  182. * class as described in the book: Ole 0sterby, Zahari Zlatev. Direct
  183. * Methods for Sparse Matrices. Springer-Verlag, 1983.
  184. *
  185. * Matrix of D(n,c) class is a non-singular matrix of the order n. It
  186. * has unity main diagonal, three co-diagonals above the main diagonal
  187. * on the distance c, which are cyclically continued below the main
  188. * diagonal, and a triangle block of the size 10x10 in the upper right
  189. * corner.
  190. *
  191. * It is necessary that n >= 14 and 1 <= c <= n-13.
  192. *
  193. * RETURNS
  194. *
  195. * The routine returns a pointer to the matrix created. */
  196. SPM *spm_test_mat_d(int n, int c)
  197. { SPM *A;
  198. int i, j;
  199. xassert(n >= 14 && 1 <= c && c <= n-13);
  200. A = spm_create_mat(n, n);
  201. for (i = 1; i <= n; i++)
  202. spm_new_elem(A, i, i, 1.0);
  203. for (i = 1; i <= n-c; i++)
  204. spm_new_elem(A, i, i+c, (double)(i+1));
  205. for (i = n-c+1; i <= n; i++)
  206. spm_new_elem(A, i, i-n+c, (double)(i+1));
  207. for (i = 1; i <= n-c-1; i++)
  208. spm_new_elem(A, i, i+c+1, (double)(-i));
  209. for (i = n-c; i <= n; i++)
  210. spm_new_elem(A, i, i-n+c+1, (double)(-i));
  211. for (i = 1; i <= n-c-2; i++)
  212. spm_new_elem(A, i, i+c+2, 16.0);
  213. for (i = n-c-1; i <= n; i++)
  214. spm_new_elem(A, i, i-n+c+2, 16.0);
  215. for (j = 1; j <= 10; j++)
  216. for (i = 1; i <= 11-j; i++)
  217. spm_new_elem(A, i, n-11+i+j, 100.0 * (double)j);
  218. return A;
  219. }
  220. /***********************************************************************
  221. * NAME
  222. *
  223. * spm_show_mat - write sparse matrix pattern in BMP file format
  224. *
  225. * SYNOPSIS
  226. *
  227. * #include "glpspm.h"
  228. * int spm_show_mat(const SPM *A, const char *fname);
  229. *
  230. * DESCRIPTION
  231. *
  232. * The routine spm_show_mat writes pattern of the specified sparse
  233. * matrix in uncompressed BMP file format (Windows bitmap) to a binary
  234. * file whose name is specified by the character string fname.
  235. *
  236. * Each pixel corresponds to one matrix element. The pixel colors have
  237. * the following meaning:
  238. *
  239. * Black structurally zero element
  240. * White positive element
  241. * Cyan negative element
  242. * Green zero element
  243. * Red duplicate element
  244. *
  245. * RETURNS
  246. *
  247. * If no error occured, the routine returns zero. Otherwise, it prints
  248. * an appropriate error message and returns non-zero. */
  249. int spm_show_mat(const SPM *A, const char *fname)
  250. { int m = A->m;
  251. int n = A->n;
  252. int i, j, k, ret;
  253. char *map;
  254. xprintf("spm_show_mat: writing matrix pattern to `%s'...\n",
  255. fname);
  256. xassert(1 <= m && m <= 32767);
  257. xassert(1 <= n && n <= 32767);
  258. map = xmalloc(m * n);
  259. memset(map, 0x08, m * n);
  260. for (i = 1; i <= m; i++)
  261. { SPME *e;
  262. for (e = A->row[i]; e != NULL; e = e->r_next)
  263. { j = e->j;
  264. xassert(1 <= j && j <= n);
  265. k = n * (i - 1) + (j - 1);
  266. if (map[k] != 0x08)
  267. map[k] = 0x0C;
  268. else if (e->val > 0.0)
  269. map[k] = 0x0F;
  270. else if (e->val < 0.0)
  271. map[k] = 0x0B;
  272. else
  273. map[k] = 0x0A;
  274. }
  275. }
  276. ret = rgr_write_bmp16(fname, m, n, map);
  277. xfree(map);
  278. return ret;
  279. }
  280. /***********************************************************************
  281. * NAME
  282. *
  283. * spm_read_hbm - read sparse matrix in Harwell-Boeing format
  284. *
  285. * SYNOPSIS
  286. *
  287. * #include "glpspm.h"
  288. * SPM *spm_read_hbm(const char *fname);
  289. *
  290. * DESCRIPTION
  291. *
  292. * The routine spm_read_hbm reads a sparse matrix in the Harwell-Boeing
  293. * format from a text file whose name is the character string fname.
  294. *
  295. * Detailed description of the Harwell-Boeing format recognised by this
  296. * routine can be found in the following report:
  297. *
  298. * I.S.Duff, R.G.Grimes, J.G.Lewis. User's Guide for the Harwell-Boeing
  299. * Sparse Matrix Collection (Release I), TR/PA/92/86, October 1992.
  300. *
  301. * NOTE
  302. *
  303. * The routine spm_read_hbm reads the matrix "as is", due to which zero
  304. * and/or duplicate elements can appear in the matrix.
  305. *
  306. * RETURNS
  307. *
  308. * If no error occured, the routine returns a pointer to the matrix
  309. * created. Otherwise, the routine prints an appropriate error message
  310. * and returns NULL. */
  311. SPM *spm_read_hbm(const char *fname)
  312. { SPM *A = NULL;
  313. HBM *hbm;
  314. int nrow, ncol, nnzero, i, j, beg, end, ptr, *colptr, *rowind;
  315. double val, *values;
  316. char *mxtype;
  317. hbm = hbm_read_mat(fname);
  318. if (hbm == NULL)
  319. { xprintf("spm_read_hbm: unable to read matrix\n");
  320. goto fini;
  321. }
  322. mxtype = hbm->mxtype;
  323. nrow = hbm->nrow;
  324. ncol = hbm->ncol;
  325. nnzero = hbm->nnzero;
  326. colptr = hbm->colptr;
  327. rowind = hbm->rowind;
  328. values = hbm->values;
  329. if (!(strcmp(mxtype, "RSA") == 0 || strcmp(mxtype, "PSA") == 0 ||
  330. strcmp(mxtype, "RUA") == 0 || strcmp(mxtype, "PUA") == 0 ||
  331. strcmp(mxtype, "RRA") == 0 || strcmp(mxtype, "PRA") == 0))
  332. { xprintf("spm_read_hbm: matrix type `%s' not supported\n",
  333. mxtype);
  334. goto fini;
  335. }
  336. A = spm_create_mat(nrow, ncol);
  337. if (mxtype[1] == 'S' || mxtype[1] == 'U')
  338. xassert(nrow == ncol);
  339. for (j = 1; j <= ncol; j++)
  340. { beg = colptr[j];
  341. end = colptr[j+1];
  342. xassert(1 <= beg && beg <= end && end <= nnzero + 1);
  343. for (ptr = beg; ptr < end; ptr++)
  344. { i = rowind[ptr];
  345. xassert(1 <= i && i <= nrow);
  346. if (mxtype[0] == 'R')
  347. val = values[ptr];
  348. else
  349. val = 1.0;
  350. spm_new_elem(A, i, j, val);
  351. if (mxtype[1] == 'S' && i != j)
  352. spm_new_elem(A, j, i, val);
  353. }
  354. }
  355. fini: if (hbm != NULL) hbm_free_mat(hbm);
  356. return A;
  357. }
  358. /***********************************************************************
  359. * NAME
  360. *
  361. * spm_count_nnz - determine number of non-zeros in sparse matrix
  362. *
  363. * SYNOPSIS
  364. *
  365. * #include "glpspm.h"
  366. * int spm_count_nnz(const SPM *A);
  367. *
  368. * RETURNS
  369. *
  370. * The routine spm_count_nnz returns the number of structural non-zero
  371. * elements in the specified sparse matrix. */
  372. int spm_count_nnz(const SPM *A)
  373. { SPME *e;
  374. int i, nnz = 0;
  375. for (i = 1; i <= A->m; i++)
  376. for (e = A->row[i]; e != NULL; e = e->r_next) nnz++;
  377. return nnz;
  378. }
  379. /***********************************************************************
  380. * NAME
  381. *
  382. * spm_drop_zeros - remove zero elements from sparse matrix
  383. *
  384. * SYNOPSIS
  385. *
  386. * #include "glpspm.h"
  387. * int spm_drop_zeros(SPM *A, double eps);
  388. *
  389. * DESCRIPTION
  390. *
  391. * The routine spm_drop_zeros removes all elements from the specified
  392. * sparse matrix, whose absolute value is less than eps.
  393. *
  394. * If the parameter eps is 0, only zero elements are removed from the
  395. * matrix.
  396. *
  397. * RETURNS
  398. *
  399. * The routine returns the number of elements removed. */
  400. int spm_drop_zeros(SPM *A, double eps)
  401. { SPME *e, *next;
  402. int i, count = 0;
  403. for (i = 1; i <= A->m; i++)
  404. { for (e = A->row[i]; e != NULL; e = next)
  405. { next = e->r_next;
  406. if (e->val == 0.0 || fabs(e->val) < eps)
  407. { /* remove element from the row list */
  408. if (e->r_prev == NULL)
  409. A->row[e->i] = e->r_next;
  410. else
  411. e->r_prev->r_next = e->r_next;
  412. if (e->r_next == NULL)
  413. ;
  414. else
  415. e->r_next->r_prev = e->r_prev;
  416. /* remove element from the column list */
  417. if (e->c_prev == NULL)
  418. A->col[e->j] = e->c_next;
  419. else
  420. e->c_prev->c_next = e->c_next;
  421. if (e->c_next == NULL)
  422. ;
  423. else
  424. e->c_next->c_prev = e->c_prev;
  425. /* return element to the memory pool */
  426. dmp_free_atom(A->pool, e, sizeof(SPME));
  427. count++;
  428. }
  429. }
  430. }
  431. return count;
  432. }
  433. /***********************************************************************
  434. * NAME
  435. *
  436. * spm_read_mat - read sparse matrix from text file
  437. *
  438. * SYNOPSIS
  439. *
  440. * #include "glpspm.h"
  441. * SPM *spm_read_mat(const char *fname);
  442. *
  443. * DESCRIPTION
  444. *
  445. * The routine reads a sparse matrix from a text file whose name is
  446. * specified by the parameter fname.
  447. *
  448. * For the file format see description of the routine spm_write_mat.
  449. *
  450. * RETURNS
  451. *
  452. * On success the routine returns a pointer to the matrix created,
  453. * otherwise NULL. */
  454. #if 1
  455. SPM *spm_read_mat(const char *fname)
  456. { xassert(fname != fname);
  457. return NULL;
  458. }
  459. #else
  460. SPM *spm_read_mat(const char *fname)
  461. { SPM *A = NULL;
  462. PDS *pds;
  463. jmp_buf jump;
  464. int i, j, k, m, n, nnz, fail = 0;
  465. double val;
  466. xprintf("spm_read_mat: reading matrix from `%s'...\n", fname);
  467. pds = pds_open_file(fname);
  468. if (pds == NULL)
  469. { xprintf("spm_read_mat: unable to open `%s' - %s\n", fname,
  470. strerror(errno));
  471. fail = 1;
  472. goto done;
  473. }
  474. if (setjmp(jump))
  475. { fail = 1;
  476. goto done;
  477. }
  478. pds_set_jump(pds, jump);
  479. /* number of rows, number of columns, number of non-zeros */
  480. m = pds_scan_int(pds);
  481. if (m < 0)
  482. pds_error(pds, "invalid number of rows\n");
  483. n = pds_scan_int(pds);
  484. if (n < 0)
  485. pds_error(pds, "invalid number of columns\n");
  486. nnz = pds_scan_int(pds);
  487. if (nnz < 0)
  488. pds_error(pds, "invalid number of non-zeros\n");
  489. /* create matrix */
  490. xprintf("spm_read_mat: %d rows, %d columns, %d non-zeros\n",
  491. m, n, nnz);
  492. A = spm_create_mat(m, n);
  493. /* read matrix elements */
  494. for (k = 1; k <= nnz; k++)
  495. { /* row index, column index, element value */
  496. i = pds_scan_int(pds);
  497. if (!(1 <= i && i <= m))
  498. pds_error(pds, "row index out of range\n");
  499. j = pds_scan_int(pds);
  500. if (!(1 <= j && j <= n))
  501. pds_error(pds, "column index out of range\n");
  502. val = pds_scan_num(pds);
  503. /* add new element to the matrix */
  504. spm_new_elem(A, i, j, val);
  505. }
  506. xprintf("spm_read_mat: %d lines were read\n", pds->count);
  507. done: if (pds != NULL) pds_close_file(pds);
  508. if (fail && A != NULL) spm_delete_mat(A), A = NULL;
  509. return A;
  510. }
  511. #endif
  512. /***********************************************************************
  513. * NAME
  514. *
  515. * spm_write_mat - write sparse matrix to text file
  516. *
  517. * SYNOPSIS
  518. *
  519. * #include "glpspm.h"
  520. * int spm_write_mat(const SPM *A, const char *fname);
  521. *
  522. * DESCRIPTION
  523. *
  524. * The routine spm_write_mat writes the specified sparse matrix to a
  525. * text file whose name is specified by the parameter fname. This file
  526. * can be read back with the routine spm_read_mat.
  527. *
  528. * RETURNS
  529. *
  530. * On success the routine returns zero, otherwise non-zero.
  531. *
  532. * FILE FORMAT
  533. *
  534. * The file created by the routine spm_write_mat is a plain text file,
  535. * which contains the following information:
  536. *
  537. * m n nnz
  538. * row[1] col[1] val[1]
  539. * row[2] col[2] val[2]
  540. * . . .
  541. * row[nnz] col[nnz] val[nnz]
  542. *
  543. * where:
  544. * m is the number of rows;
  545. * n is the number of columns;
  546. * nnz is the number of non-zeros;
  547. * row[k], k = 1,...,nnz, are row indices;
  548. * col[k], k = 1,...,nnz, are column indices;
  549. * val[k], k = 1,...,nnz, are element values. */
  550. #if 1
  551. int spm_write_mat(const SPM *A, const char *fname)
  552. { xassert(A != A);
  553. xassert(fname != fname);
  554. return 0;
  555. }
  556. #else
  557. int spm_write_mat(const SPM *A, const char *fname)
  558. { FILE *fp;
  559. int i, nnz, ret = 0;
  560. xprintf("spm_write_mat: writing matrix to `%s'...\n", fname);
  561. fp = fopen(fname, "w");
  562. if (fp == NULL)
  563. { xprintf("spm_write_mat: unable to create `%s' - %s\n", fname,
  564. strerror(errno));
  565. ret = 1;
  566. goto done;
  567. }
  568. /* number of rows, number of columns, number of non-zeros */
  569. nnz = spm_count_nnz(A);
  570. fprintf(fp, "%d %d %d\n", A->m, A->n, nnz);
  571. /* walk through rows of the matrix */
  572. for (i = 1; i <= A->m; i++)
  573. { SPME *e;
  574. /* walk through elements of i-th row */
  575. for (e = A->row[i]; e != NULL; e = e->r_next)
  576. { /* row index, column index, element value */
  577. fprintf(fp, "%d %d %.*g\n", e->i, e->j, DBL_DIG, e->val);
  578. }
  579. }
  580. fflush(fp);
  581. if (ferror(fp))
  582. { xprintf("spm_write_mat: writing error on `%s' - %s\n", fname,
  583. strerror(errno));
  584. ret = 1;
  585. goto done;
  586. }
  587. xprintf("spm_write_mat: %d lines were written\n", 1 + nnz);
  588. done: if (fp != NULL) fclose(fp);
  589. return ret;
  590. }
  591. #endif
  592. /***********************************************************************
  593. * NAME
  594. *
  595. * spm_transpose - transpose sparse matrix
  596. *
  597. * SYNOPSIS
  598. *
  599. * #include "glpspm.h"
  600. * SPM *spm_transpose(const SPM *A);
  601. *
  602. * RETURNS
  603. *
  604. * The routine computes and returns sparse matrix B, which is a matrix
  605. * transposed to sparse matrix A. */
  606. SPM *spm_transpose(const SPM *A)
  607. { SPM *B;
  608. int i;
  609. B = spm_create_mat(A->n, A->m);
  610. for (i = 1; i <= A->m; i++)
  611. { SPME *e;
  612. for (e = A->row[i]; e != NULL; e = e->r_next)
  613. spm_new_elem(B, e->j, i, e->val);
  614. }
  615. return B;
  616. }
  617. SPM *spm_add_sym(const SPM *A, const SPM *B)
  618. { /* add two sparse matrices (symbolic phase) */
  619. SPM *C;
  620. int i, j, *flag;
  621. xassert(A->m == B->m);
  622. xassert(A->n == B->n);
  623. /* create resultant matrix */
  624. C = spm_create_mat(A->m, A->n);
  625. /* allocate and clear the flag array */
  626. flag = xcalloc(1+C->n, sizeof(int));
  627. for (j = 1; j <= C->n; j++)
  628. flag[j] = 0;
  629. /* compute pattern of C = A + B */
  630. for (i = 1; i <= C->m; i++)
  631. { SPME *e;
  632. /* at the beginning i-th row of C is empty */
  633. /* (i-th row of C) := (i-th row of C) union (i-th row of A) */
  634. for (e = A->row[i]; e != NULL; e = e->r_next)
  635. { /* (note that i-th row of A may have duplicate elements) */
  636. j = e->j;
  637. if (!flag[j])
  638. { spm_new_elem(C, i, j, 0.0);
  639. flag[j] = 1;
  640. }
  641. }
  642. /* (i-th row of C) := (i-th row of C) union (i-th row of B) */
  643. for (e = B->row[i]; e != NULL; e = e->r_next)
  644. { /* (note that i-th row of B may have duplicate elements) */
  645. j = e->j;
  646. if (!flag[j])
  647. { spm_new_elem(C, i, j, 0.0);
  648. flag[j] = 1;
  649. }
  650. }
  651. /* reset the flag array */
  652. for (e = C->row[i]; e != NULL; e = e->r_next)
  653. flag[e->j] = 0;
  654. }
  655. /* check and deallocate the flag array */
  656. for (j = 1; j <= C->n; j++)
  657. xassert(!flag[j]);
  658. xfree(flag);
  659. return C;
  660. }
  661. void spm_add_num(SPM *C, double alfa, const SPM *A, double beta,
  662. const SPM *B)
  663. { /* add two sparse matrices (numeric phase) */
  664. int i, j;
  665. double *work;
  666. /* allocate and clear the working array */
  667. work = xcalloc(1+C->n, sizeof(double));
  668. for (j = 1; j <= C->n; j++)
  669. work[j] = 0.0;
  670. /* compute matrix C = alfa * A + beta * B */
  671. for (i = 1; i <= C->n; i++)
  672. { SPME *e;
  673. /* work := alfa * (i-th row of A) + beta * (i-th row of B) */
  674. /* (note that A and/or B may have duplicate elements) */
  675. for (e = A->row[i]; e != NULL; e = e->r_next)
  676. work[e->j] += alfa * e->val;
  677. for (e = B->row[i]; e != NULL; e = e->r_next)
  678. work[e->j] += beta * e->val;
  679. /* (i-th row of C) := work, work := 0 */
  680. for (e = C->row[i]; e != NULL; e = e->r_next)
  681. { j = e->j;
  682. e->val = work[j];
  683. work[j] = 0.0;
  684. }
  685. }
  686. /* check and deallocate the working array */
  687. for (j = 1; j <= C->n; j++)
  688. xassert(work[j] == 0.0);
  689. xfree(work);
  690. return;
  691. }
  692. SPM *spm_add_mat(double alfa, const SPM *A, double beta, const SPM *B)
  693. { /* add two sparse matrices (driver routine) */
  694. SPM *C;
  695. C = spm_add_sym(A, B);
  696. spm_add_num(C, alfa, A, beta, B);
  697. return C;
  698. }
  699. SPM *spm_mul_sym(const SPM *A, const SPM *B)
  700. { /* multiply two sparse matrices (symbolic phase) */
  701. int i, j, k, *flag;
  702. SPM *C;
  703. xassert(A->n == B->m);
  704. /* create resultant matrix */
  705. C = spm_create_mat(A->m, B->n);
  706. /* allocate and clear the flag array */
  707. flag = xcalloc(1+C->n, sizeof(int));
  708. for (j = 1; j <= C->n; j++)
  709. flag[j] = 0;
  710. /* compute pattern of C = A * B */
  711. for (i = 1; i <= C->m; i++)
  712. { SPME *e, *ee;
  713. /* compute pattern of i-th row of C */
  714. for (e = A->row[i]; e != NULL; e = e->r_next)
  715. { k = e->j;
  716. for (ee = B->row[k]; ee != NULL; ee = ee->r_next)
  717. { j = ee->j;
  718. /* if a[i,k] != 0 and b[k,j] != 0 then c[i,j] != 0 */
  719. if (!flag[j])
  720. { /* c[i,j] does not exist, so create it */
  721. spm_new_elem(C, i, j, 0.0);
  722. flag[j] = 1;
  723. }
  724. }
  725. }
  726. /* reset the flag array */
  727. for (e = C->row[i]; e != NULL; e = e->r_next)
  728. flag[e->j] = 0;
  729. }
  730. /* check and deallocate the flag array */
  731. for (j = 1; j <= C->n; j++)
  732. xassert(!flag[j]);
  733. xfree(flag);
  734. return C;
  735. }
  736. void spm_mul_num(SPM *C, const SPM *A, const SPM *B)
  737. { /* multiply two sparse matrices (numeric phase) */
  738. int i, j;
  739. double *work;
  740. /* allocate and clear the working array */
  741. work = xcalloc(1+A->n, sizeof(double));
  742. for (j = 1; j <= A->n; j++)
  743. work[j] = 0.0;
  744. /* compute matrix C = A * B */
  745. for (i = 1; i <= C->m; i++)
  746. { SPME *e, *ee;
  747. double temp;
  748. /* work := (i-th row of A) */
  749. /* (note that A may have duplicate elements) */
  750. for (e = A->row[i]; e != NULL; e = e->r_next)
  751. work[e->j] += e->val;
  752. /* compute i-th row of C */
  753. for (e = C->row[i]; e != NULL; e = e->r_next)
  754. { j = e->j;
  755. /* c[i,j] := work * (j-th column of B) */
  756. temp = 0.0;
  757. for (ee = B->col[j]; ee != NULL; ee = ee->c_next)
  758. temp += work[ee->i] * ee->val;
  759. e->val = temp;
  760. }
  761. /* reset the working array */
  762. for (e = A->row[i]; e != NULL; e = e->r_next)
  763. work[e->j] = 0.0;
  764. }
  765. /* check and deallocate the working array */
  766. for (j = 1; j <= A->n; j++)
  767. xassert(work[j] == 0.0);
  768. xfree(work);
  769. return;
  770. }
  771. SPM *spm_mul_mat(const SPM *A, const SPM *B)
  772. { /* multiply two sparse matrices (driver routine) */
  773. SPM *C;
  774. C = spm_mul_sym(A, B);
  775. spm_mul_num(C, A, B);
  776. return C;
  777. }
  778. PER *spm_create_per(int n)
  779. { /* create permutation matrix */
  780. PER *P;
  781. int k;
  782. xassert(n >= 0);
  783. P = xmalloc(sizeof(PER));
  784. P->n = n;
  785. P->row = xcalloc(1+n, sizeof(int));
  786. P->col = xcalloc(1+n, sizeof(int));
  787. /* initially it is identity matrix */
  788. for (k = 1; k <= n; k++)
  789. P->row[k] = P->col[k] = k;
  790. return P;
  791. }
  792. void spm_check_per(PER *P)
  793. { /* check permutation matrix for correctness */
  794. int i, j;
  795. xassert(P->n >= 0);
  796. for (i = 1; i <= P->n; i++)
  797. { j = P->row[i];
  798. xassert(1 <= j && j <= P->n);
  799. xassert(P->col[j] == i);
  800. }
  801. return;
  802. }
  803. void spm_delete_per(PER *P)
  804. { /* delete permutation matrix */
  805. xfree(P->row);
  806. xfree(P->col);
  807. xfree(P);
  808. return;
  809. }
  810. /* eof */