3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
29 /***********************************************************************
32 * spm_create_mat - create general sparse matrix
37 * SPM *spm_create_mat(int m, int n);
41 * The routine spm_create_mat creates a general sparse matrix having
42 * m rows and n columns. Being created the matrix is zero (empty), i.e.
47 * The routine returns a pointer to the matrix created. */
49 SPM *spm_create_mat(int m, int n)
51 xassert(0 <= m && m < INT_MAX);
52 xassert(0 <= n && n < INT_MAX);
53 A = xmalloc(sizeof(SPM));
63 A->pool = dmp_create_pool();
64 A->row = xcalloc(1+m, sizeof(SPME *));
65 for (i = 1; i <= m; i++) A->row[i] = NULL;
66 A->col = xcalloc(1+n, sizeof(SPME *));
67 for (j = 1; j <= n; j++) A->col[j] = NULL;
72 /***********************************************************************
75 * spm_new_elem - add new element to sparse matrix
80 * SPME *spm_new_elem(SPM *A, int i, int j, double val);
84 * The routine spm_new_elem adds a new element to the specified sparse
85 * matrix. Parameters i, j, and val specify the row number, the column
86 * number, and a numerical value of the element, respectively.
90 * The routine returns a pointer to the new element added. */
92 SPME *spm_new_elem(SPM *A, int i, int j, double val)
94 xassert(1 <= i && i <= A->m);
95 xassert(1 <= j && j <= A->n);
96 e = dmp_get_atom(A->pool, sizeof(SPME));
101 e->r_next = A->row[i];
102 if (e->r_next != NULL) e->r_next->r_prev = e;
104 e->c_next = A->col[j];
105 if (e->c_next != NULL) e->c_next->c_prev = e;
106 A->row[i] = A->col[j] = e;
110 /***********************************************************************
113 * spm_delete_mat - delete general sparse matrix
117 * #include "glpspm.h"
118 * void spm_delete_mat(SPM *A);
122 * The routine deletes the specified general sparse matrix freeing all
123 * the memory allocated to this object. */
125 void spm_delete_mat(SPM *A)
126 { /* delete sparse matrix */
127 if (A->pool != NULL) dmp_delete_pool(A->pool);
128 if (A->row != NULL) xfree(A->row);
129 if (A->col != NULL) xfree(A->col);
134 /***********************************************************************
137 * spm_test_mat_e - create test sparse matrix of E(n,c) class
141 * #include "glpspm.h"
142 * SPM *spm_test_mat_e(int n, int c);
146 * The routine spm_test_mat_e creates a test sparse matrix of E(n,c)
147 * class as described in the book: Ole 0sterby, Zahari Zlatev. Direct
148 * Methods for Sparse Matrices. Springer-Verlag, 1983.
150 * Matrix of E(n,c) class is a symmetric positive definite matrix of
151 * the order n. It has the number 4 on its main diagonal and the number
152 * -1 on its four co-diagonals, two of which are neighbour to the main
153 * diagonal and two others are shifted from the main diagonal on the
156 * It is necessary that n >= 3 and 2 <= c <= n-1.
160 * The routine returns a pointer to the matrix created. */
162 SPM *spm_test_mat_e(int n, int c)
165 xassert(n >= 3 && 2 <= c && c <= n-1);
166 A = spm_create_mat(n, n);
167 for (i = 1; i <= n; i++)
168 spm_new_elem(A, i, i, 4.0);
169 for (i = 1; i <= n-1; i++)
170 { spm_new_elem(A, i, i+1, -1.0);
171 spm_new_elem(A, i+1, i, -1.0);
173 for (i = 1; i <= n-c; i++)
174 { spm_new_elem(A, i, i+c, -1.0);
175 spm_new_elem(A, i+c, i, -1.0);
180 /***********************************************************************
183 * spm_test_mat_d - create test sparse matrix of D(n,c) class
187 * #include "glpspm.h"
188 * SPM *spm_test_mat_d(int n, int c);
192 * The routine spm_test_mat_d creates a test sparse matrix of D(n,c)
193 * class as described in the book: Ole 0sterby, Zahari Zlatev. Direct
194 * Methods for Sparse Matrices. Springer-Verlag, 1983.
196 * Matrix of D(n,c) class is a non-singular matrix of the order n. It
197 * has unity main diagonal, three co-diagonals above the main diagonal
198 * on the distance c, which are cyclically continued below the main
199 * diagonal, and a triangle block of the size 10x10 in the upper right
202 * It is necessary that n >= 14 and 1 <= c <= n-13.
206 * The routine returns a pointer to the matrix created. */
208 SPM *spm_test_mat_d(int n, int c)
211 xassert(n >= 14 && 1 <= c && c <= n-13);
212 A = spm_create_mat(n, n);
213 for (i = 1; i <= n; i++)
214 spm_new_elem(A, i, i, 1.0);
215 for (i = 1; i <= n-c; i++)
216 spm_new_elem(A, i, i+c, (double)(i+1));
217 for (i = n-c+1; i <= n; i++)
218 spm_new_elem(A, i, i-n+c, (double)(i+1));
219 for (i = 1; i <= n-c-1; i++)
220 spm_new_elem(A, i, i+c+1, (double)(-i));
221 for (i = n-c; i <= n; i++)
222 spm_new_elem(A, i, i-n+c+1, (double)(-i));
223 for (i = 1; i <= n-c-2; i++)
224 spm_new_elem(A, i, i+c+2, 16.0);
225 for (i = n-c-1; i <= n; i++)
226 spm_new_elem(A, i, i-n+c+2, 16.0);
227 for (j = 1; j <= 10; j++)
228 for (i = 1; i <= 11-j; i++)
229 spm_new_elem(A, i, n-11+i+j, 100.0 * (double)j);
233 /***********************************************************************
236 * spm_show_mat - write sparse matrix pattern in BMP file format
240 * #include "glpspm.h"
241 * int spm_show_mat(const SPM *A, const char *fname);
245 * The routine spm_show_mat writes pattern of the specified sparse
246 * matrix in uncompressed BMP file format (Windows bitmap) to a binary
247 * file whose name is specified by the character string fname.
249 * Each pixel corresponds to one matrix element. The pixel colors have
250 * the following meaning:
252 * Black structurally zero element
253 * White positive element
254 * Cyan negative element
256 * Red duplicate element
260 * If no error occured, the routine returns zero. Otherwise, it prints
261 * an appropriate error message and returns non-zero. */
263 int spm_show_mat(const SPM *A, const char *fname)
268 xprintf("spm_show_mat: writing matrix pattern to `%s'...\n",
270 xassert(1 <= m && m <= 32767);
271 xassert(1 <= n && n <= 32767);
272 map = xmalloc(m * n);
273 memset(map, 0x08, m * n);
274 for (i = 1; i <= m; i++)
276 for (e = A->row[i]; e != NULL; e = e->r_next)
278 xassert(1 <= j && j <= n);
279 k = n * (i - 1) + (j - 1);
282 else if (e->val > 0.0)
284 else if (e->val < 0.0)
290 ret = rgr_write_bmp16(fname, m, n, map);
295 /***********************************************************************
298 * spm_read_hbm - read sparse matrix in Harwell-Boeing format
302 * #include "glpspm.h"
303 * SPM *spm_read_hbm(const char *fname);
307 * The routine spm_read_hbm reads a sparse matrix in the Harwell-Boeing
308 * format from a text file whose name is the character string fname.
310 * Detailed description of the Harwell-Boeing format recognised by this
311 * routine can be found in the following report:
313 * I.S.Duff, R.G.Grimes, J.G.Lewis. User's Guide for the Harwell-Boeing
314 * Sparse Matrix Collection (Release I), TR/PA/92/86, October 1992.
318 * The routine spm_read_hbm reads the matrix "as is", due to which zero
319 * and/or duplicate elements can appear in the matrix.
323 * If no error occured, the routine returns a pointer to the matrix
324 * created. Otherwise, the routine prints an appropriate error message
325 * and returns NULL. */
327 SPM *spm_read_hbm(const char *fname)
330 int nrow, ncol, nnzero, i, j, beg, end, ptr, *colptr, *rowind;
333 hbm = hbm_read_mat(fname);
335 { xprintf("spm_read_hbm: unable to read matrix\n");
338 mxtype = hbm->mxtype;
341 nnzero = hbm->nnzero;
342 colptr = hbm->colptr;
343 rowind = hbm->rowind;
344 values = hbm->values;
345 if (!(strcmp(mxtype, "RSA") == 0 || strcmp(mxtype, "PSA") == 0 ||
346 strcmp(mxtype, "RUA") == 0 || strcmp(mxtype, "PUA") == 0 ||
347 strcmp(mxtype, "RRA") == 0 || strcmp(mxtype, "PRA") == 0))
348 { xprintf("spm_read_hbm: matrix type `%s' not supported\n",
352 A = spm_create_mat(nrow, ncol);
353 if (mxtype[1] == 'S' || mxtype[1] == 'U')
354 xassert(nrow == ncol);
355 for (j = 1; j <= ncol; j++)
358 xassert(1 <= beg && beg <= end && end <= nnzero + 1);
359 for (ptr = beg; ptr < end; ptr++)
361 xassert(1 <= i && i <= nrow);
362 if (mxtype[0] == 'R')
366 spm_new_elem(A, i, j, val);
367 if (mxtype[1] == 'S' && i != j)
368 spm_new_elem(A, j, i, val);
371 fini: if (hbm != NULL) hbm_free_mat(hbm);
375 /***********************************************************************
378 * spm_count_nnz - determine number of non-zeros in sparse matrix
382 * #include "glpspm.h"
383 * int spm_count_nnz(const SPM *A);
387 * The routine spm_count_nnz returns the number of structural non-zero
388 * elements in the specified sparse matrix. */
390 int spm_count_nnz(const SPM *A)
393 for (i = 1; i <= A->m; i++)
394 for (e = A->row[i]; e != NULL; e = e->r_next) nnz++;
398 /***********************************************************************
401 * spm_drop_zeros - remove zero elements from sparse matrix
405 * #include "glpspm.h"
406 * int spm_drop_zeros(SPM *A, double eps);
410 * The routine spm_drop_zeros removes all elements from the specified
411 * sparse matrix, whose absolute value is less than eps.
413 * If the parameter eps is 0, only zero elements are removed from the
418 * The routine returns the number of elements removed. */
420 int spm_drop_zeros(SPM *A, double eps)
423 for (i = 1; i <= A->m; i++)
424 { for (e = A->row[i]; e != NULL; e = next)
426 if (e->val == 0.0 || fabs(e->val) < eps)
427 { /* remove element from the row list */
428 if (e->r_prev == NULL)
429 A->row[e->i] = e->r_next;
431 e->r_prev->r_next = e->r_next;
432 if (e->r_next == NULL)
435 e->r_next->r_prev = e->r_prev;
436 /* remove element from the column list */
437 if (e->c_prev == NULL)
438 A->col[e->j] = e->c_next;
440 e->c_prev->c_next = e->c_next;
441 if (e->c_next == NULL)
444 e->c_next->c_prev = e->c_prev;
445 /* return element to the memory pool */
446 dmp_free_atom(A->pool, e, sizeof(SPME));
454 /***********************************************************************
457 * spm_read_mat - read sparse matrix from text file
461 * #include "glpspm.h"
462 * SPM *spm_read_mat(const char *fname);
466 * The routine reads a sparse matrix from a text file whose name is
467 * specified by the parameter fname.
469 * For the file format see description of the routine spm_write_mat.
473 * On success the routine returns a pointer to the matrix created,
477 SPM *spm_read_mat(const char *fname)
478 { xassert(fname != fname);
482 SPM *spm_read_mat(const char *fname)
486 int i, j, k, m, n, nnz, fail = 0;
488 xprintf("spm_read_mat: reading matrix from `%s'...\n", fname);
489 pds = pds_open_file(fname);
491 { xprintf("spm_read_mat: unable to open `%s' - %s\n", fname,
500 pds_set_jump(pds, jump);
501 /* number of rows, number of columns, number of non-zeros */
502 m = pds_scan_int(pds);
504 pds_error(pds, "invalid number of rows\n");
505 n = pds_scan_int(pds);
507 pds_error(pds, "invalid number of columns\n");
508 nnz = pds_scan_int(pds);
510 pds_error(pds, "invalid number of non-zeros\n");
512 xprintf("spm_read_mat: %d rows, %d columns, %d non-zeros\n",
514 A = spm_create_mat(m, n);
515 /* read matrix elements */
516 for (k = 1; k <= nnz; k++)
517 { /* row index, column index, element value */
518 i = pds_scan_int(pds);
519 if (!(1 <= i && i <= m))
520 pds_error(pds, "row index out of range\n");
521 j = pds_scan_int(pds);
522 if (!(1 <= j && j <= n))
523 pds_error(pds, "column index out of range\n");
524 val = pds_scan_num(pds);
525 /* add new element to the matrix */
526 spm_new_elem(A, i, j, val);
528 xprintf("spm_read_mat: %d lines were read\n", pds->count);
529 done: if (pds != NULL) pds_close_file(pds);
530 if (fail && A != NULL) spm_delete_mat(A), A = NULL;
535 /***********************************************************************
538 * spm_write_mat - write sparse matrix to text file
542 * #include "glpspm.h"
543 * int spm_write_mat(const SPM *A, const char *fname);
547 * The routine spm_write_mat writes the specified sparse matrix to a
548 * text file whose name is specified by the parameter fname. This file
549 * can be read back with the routine spm_read_mat.
553 * On success the routine returns zero, otherwise non-zero.
557 * The file created by the routine spm_write_mat is a plain text file,
558 * which contains the following information:
561 * row[1] col[1] val[1]
562 * row[2] col[2] val[2]
564 * row[nnz] col[nnz] val[nnz]
567 * m is the number of rows;
568 * n is the number of columns;
569 * nnz is the number of non-zeros;
570 * row[k], k = 1,...,nnz, are row indices;
571 * col[k], k = 1,...,nnz, are column indices;
572 * val[k], k = 1,...,nnz, are element values. */
575 int spm_write_mat(const SPM *A, const char *fname)
577 xassert(fname != fname);
581 int spm_write_mat(const SPM *A, const char *fname)
584 xprintf("spm_write_mat: writing matrix to `%s'...\n", fname);
585 fp = fopen(fname, "w");
587 { xprintf("spm_write_mat: unable to create `%s' - %s\n", fname,
592 /* number of rows, number of columns, number of non-zeros */
593 nnz = spm_count_nnz(A);
594 fprintf(fp, "%d %d %d\n", A->m, A->n, nnz);
595 /* walk through rows of the matrix */
596 for (i = 1; i <= A->m; i++)
598 /* walk through elements of i-th row */
599 for (e = A->row[i]; e != NULL; e = e->r_next)
600 { /* row index, column index, element value */
601 fprintf(fp, "%d %d %.*g\n", e->i, e->j, DBL_DIG, e->val);
606 { xprintf("spm_write_mat: writing error on `%s' - %s\n", fname,
611 xprintf("spm_write_mat: %d lines were written\n", 1 + nnz);
612 done: if (fp != NULL) fclose(fp);
617 /***********************************************************************
620 * spm_transpose - transpose sparse matrix
624 * #include "glpspm.h"
625 * SPM *spm_transpose(const SPM *A);
629 * The routine computes and returns sparse matrix B, which is a matrix
630 * transposed to sparse matrix A. */
632 SPM *spm_transpose(const SPM *A)
635 B = spm_create_mat(A->n, A->m);
636 for (i = 1; i <= A->m; i++)
638 for (e = A->row[i]; e != NULL; e = e->r_next)
639 spm_new_elem(B, e->j, i, e->val);
644 SPM *spm_add_sym(const SPM *A, const SPM *B)
645 { /* add two sparse matrices (symbolic phase) */
648 xassert(A->m == B->m);
649 xassert(A->n == B->n);
650 /* create resultant matrix */
651 C = spm_create_mat(A->m, A->n);
652 /* allocate and clear the flag array */
653 flag = xcalloc(1+C->n, sizeof(int));
654 for (j = 1; j <= C->n; j++)
656 /* compute pattern of C = A + B */
657 for (i = 1; i <= C->m; i++)
659 /* at the beginning i-th row of C is empty */
660 /* (i-th row of C) := (i-th row of C) union (i-th row of A) */
661 for (e = A->row[i]; e != NULL; e = e->r_next)
662 { /* (note that i-th row of A may have duplicate elements) */
665 { spm_new_elem(C, i, j, 0.0);
669 /* (i-th row of C) := (i-th row of C) union (i-th row of B) */
670 for (e = B->row[i]; e != NULL; e = e->r_next)
671 { /* (note that i-th row of B may have duplicate elements) */
674 { spm_new_elem(C, i, j, 0.0);
678 /* reset the flag array */
679 for (e = C->row[i]; e != NULL; e = e->r_next)
682 /* check and deallocate the flag array */
683 for (j = 1; j <= C->n; j++)
689 void spm_add_num(SPM *C, double alfa, const SPM *A, double beta,
691 { /* add two sparse matrices (numeric phase) */
694 /* allocate and clear the working array */
695 work = xcalloc(1+C->n, sizeof(double));
696 for (j = 1; j <= C->n; j++)
698 /* compute matrix C = alfa * A + beta * B */
699 for (i = 1; i <= C->n; i++)
701 /* work := alfa * (i-th row of A) + beta * (i-th row of B) */
702 /* (note that A and/or B may have duplicate elements) */
703 for (e = A->row[i]; e != NULL; e = e->r_next)
704 work[e->j] += alfa * e->val;
705 for (e = B->row[i]; e != NULL; e = e->r_next)
706 work[e->j] += beta * e->val;
707 /* (i-th row of C) := work, work := 0 */
708 for (e = C->row[i]; e != NULL; e = e->r_next)
714 /* check and deallocate the working array */
715 for (j = 1; j <= C->n; j++)
716 xassert(work[j] == 0.0);
721 SPM *spm_add_mat(double alfa, const SPM *A, double beta, const SPM *B)
722 { /* add two sparse matrices (driver routine) */
724 C = spm_add_sym(A, B);
725 spm_add_num(C, alfa, A, beta, B);
729 SPM *spm_mul_sym(const SPM *A, const SPM *B)
730 { /* multiply two sparse matrices (symbolic phase) */
733 xassert(A->n == B->m);
734 /* create resultant matrix */
735 C = spm_create_mat(A->m, B->n);
736 /* allocate and clear the flag array */
737 flag = xcalloc(1+C->n, sizeof(int));
738 for (j = 1; j <= C->n; j++)
740 /* compute pattern of C = A * B */
741 for (i = 1; i <= C->m; i++)
743 /* compute pattern of i-th row of C */
744 for (e = A->row[i]; e != NULL; e = e->r_next)
746 for (ee = B->row[k]; ee != NULL; ee = ee->r_next)
748 /* if a[i,k] != 0 and b[k,j] != 0 then c[i,j] != 0 */
750 { /* c[i,j] does not exist, so create it */
751 spm_new_elem(C, i, j, 0.0);
756 /* reset the flag array */
757 for (e = C->row[i]; e != NULL; e = e->r_next)
760 /* check and deallocate the flag array */
761 for (j = 1; j <= C->n; j++)
767 void spm_mul_num(SPM *C, const SPM *A, const SPM *B)
768 { /* multiply two sparse matrices (numeric phase) */
771 /* allocate and clear the working array */
772 work = xcalloc(1+A->n, sizeof(double));
773 for (j = 1; j <= A->n; j++)
775 /* compute matrix C = A * B */
776 for (i = 1; i <= C->m; i++)
779 /* work := (i-th row of A) */
780 /* (note that A may have duplicate elements) */
781 for (e = A->row[i]; e != NULL; e = e->r_next)
782 work[e->j] += e->val;
783 /* compute i-th row of C */
784 for (e = C->row[i]; e != NULL; e = e->r_next)
786 /* c[i,j] := work * (j-th column of B) */
788 for (ee = B->col[j]; ee != NULL; ee = ee->c_next)
789 temp += work[ee->i] * ee->val;
792 /* reset the working array */
793 for (e = A->row[i]; e != NULL; e = e->r_next)
796 /* check and deallocate the working array */
797 for (j = 1; j <= A->n; j++)
798 xassert(work[j] == 0.0);
803 SPM *spm_mul_mat(const SPM *A, const SPM *B)
804 { /* multiply two sparse matrices (driver routine) */
806 C = spm_mul_sym(A, B);
807 spm_mul_num(C, A, B);
811 PER *spm_create_per(int n)
812 { /* create permutation matrix */
816 P = xmalloc(sizeof(PER));
818 P->row = xcalloc(1+n, sizeof(int));
819 P->col = xcalloc(1+n, sizeof(int));
820 /* initially it is identity matrix */
821 for (k = 1; k <= n; k++)
822 P->row[k] = P->col[k] = k;
826 void spm_check_per(PER *P)
827 { /* check permutation matrix for correctness */
830 for (i = 1; i <= P->n; i++)
832 xassert(1 <= j && j <= P->n);
833 xassert(P->col[j] == i);
838 void spm_delete_per(PER *P)
839 { /* delete permutation matrix */