aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/draft/glpspm.c
blob: c6cfd25ddba4b56eb0b064f9262a7a90581555b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/* glpspm.c */

/***********************************************************************
*  This code is part of GLPK (GNU Linear Programming Kit).
*
*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
*  2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
*  Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
*  reserved. E-mail: <mao@gnu.org>.
*
*  GLPK is free software: you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
*  License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#include "glphbm.h"
#include "glprgr.h"
#include "glpspm.h"
#include "env.h"

/***********************************************************************
*  NAME
*
*  spm_create_mat - create general sparse matrix
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  SPM *spm_create_mat(int m, int n);
*
*  DESCRIPTION
*
*  The routine spm_create_mat creates a general sparse matrix having
*  m rows and n columns. Being created the matrix is zero (empty), i.e.
*  has no elements.
*
*  RETURNS
*
*  The routine returns a pointer to the matrix created. */

SPM *spm_create_mat(int m, int n)
{     SPM *A;
      xassert(0 <= m && m < INT_MAX);
      xassert(0 <= n && n < INT_MAX);
      A = xmalloc(sizeof(SPM));
      A->m = m;
      A->n = n;
      if (m == 0 || n == 0)
      {  A->pool = NULL;
         A->row = NULL;
         A->col = NULL;
      }
      else
      {  int i, j;
         A->pool = dmp_create_pool();
         A->row = xcalloc(1+m, sizeof(SPME *));
         for (i = 1; i <= m; i++) A->row[i] = NULL;
         A->col = xcalloc(1+n, sizeof(SPME *));
         for (j = 1; j <= n; j++) A->col[j] = NULL;
      }
      return A;
}

/***********************************************************************
*  NAME
*
*  spm_new_elem - add new element to sparse matrix
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  SPME *spm_new_elem(SPM *A, int i, int j, double val);
*
*  DESCRIPTION
*
*  The routine spm_new_elem adds a new element to the specified sparse
*  matrix. Parameters i, j, and val specify the row number, the column
*  number, and a numerical value of the element, respectively.
*
*  RETURNS
*
*  The routine returns a pointer to the new element added. */

SPME *spm_new_elem(SPM *A, int i, int j, double val)
{     SPME *e;
      xassert(1 <= i && i <= A->m);
      xassert(1 <= j && j <= A->n);
      e = dmp_get_atom(A->pool, sizeof(SPME));
      e->i = i;
      e->j = j;
      e->val = val;
      e->r_prev = NULL;
      e->r_next = A->row[i];
      if (e->r_next != NULL) e->r_next->r_prev = e;
      e->c_prev = NULL;
      e->c_next = A->col[j];
      if (e->c_next != NULL) e->c_next->c_prev = e;
      A->row[i] = A->col[j] = e;
      return e;
}

/***********************************************************************
*  NAME
*
*  spm_delete_mat - delete general sparse matrix
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  void spm_delete_mat(SPM *A);
*
*  DESCRIPTION
*
*  The routine deletes the specified general sparse matrix freeing all
*  the memory allocated to this object. */

void spm_delete_mat(SPM *A)
{     /* delete sparse matrix */
      if (A->pool != NULL) dmp_delete_pool(A->pool);
      if (A->row != NULL) xfree(A->row);
      if (A->col != NULL) xfree(A->col);
      xfree(A);
      return;
}

/***********************************************************************
*  NAME
*
*  spm_test_mat_e - create test sparse matrix of E(n,c) class
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  SPM *spm_test_mat_e(int n, int c);
*
*  DESCRIPTION
*
*  The routine spm_test_mat_e creates a test sparse matrix of E(n,c)
*  class as described in the book: Ole 0sterby, Zahari Zlatev. Direct
*  Methods for Sparse Matrices. Springer-Verlag, 1983.
*
*  Matrix of E(n,c) class is a symmetric positive definite matrix of
*  the order n. It has the number 4 on its main diagonal and the number
*  -1 on its four co-diagonals, two of which are neighbour to the main
*  diagonal and two others are shifted from the main diagonal on the
*  distance c.
*
*  It is necessary that n >= 3 and 2 <= c <= n-1.
*
*  RETURNS
*
*  The routine returns a pointer to the matrix created. */

SPM *spm_test_mat_e(int n, int c)
{     SPM *A;
      int i;
      xassert(n >= 3 && 2 <= c && c <= n-1);
      A = spm_create_mat(n, n);
      for (i = 1; i <= n; i++)
         spm_new_elem(A, i, i, 4.0);
      for (i = 1; i <= n-1; i++)
      {  spm_new_elem(A, i, i+1, -1.0);
         spm_new_elem(A, i+1, i, -1.0);
      }
      for (i = 1; i <= n-c; i++)
      {  spm_new_elem(A, i, i+c, -1.0);
         spm_new_elem(A, i+c, i, -1.0);
      }
      return A;
}

/***********************************************************************
*  NAME
*
*  spm_test_mat_d - create test sparse matrix of D(n,c) class
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  SPM *spm_test_mat_d(int n, int c);
*
*  DESCRIPTION
*
*  The routine spm_test_mat_d creates a test sparse matrix of D(n,c)
*  class as described in the book: Ole 0sterby, Zahari Zlatev. Direct
*  Methods for Sparse Matrices. Springer-Verlag, 1983.
*
*  Matrix of D(n,c) class is a non-singular matrix of the order n. It
*  has unity main diagonal, three co-diagonals above the main diagonal
*  on the distance c, which are cyclically continued below the main
*  diagonal, and a triangle block of the size 10x10 in the upper right
*  corner.
*
*  It is necessary that n >= 14 and 1 <= c <= n-13.
*
*  RETURNS
*
*  The routine returns a pointer to the matrix created. */

SPM *spm_test_mat_d(int n, int c)
{     SPM *A;
      int i, j;
      xassert(n >= 14 && 1 <= c && c <= n-13);
      A = spm_create_mat(n, n);
      for (i = 1; i <= n; i++)
         spm_new_elem(A, i, i, 1.0);
      for (i = 1; i <= n-c; i++)
         spm_new_elem(A, i, i+c, (double)(i+1));
      for (i = n-c+1; i <= n; i++)
         spm_new_elem(A, i, i-n+c, (double)(i+1));
      for (i = 1; i <= n-c-1; i++)
         spm_new_elem(A, i, i+c+1, (double)(-i));
      for (i = n-c; i <= n; i++)
         spm_new_elem(A, i, i-n+c+1, (double)(-i));
      for (i = 1; i <= n-c-2; i++)
         spm_new_elem(A, i, i+c+2, 16.0);
      for (i = n-c-1; i <= n; i++)
         spm_new_elem(A, i, i-n+c+2, 16.0);
      for (j = 1; j <= 10; j++)
         for (i = 1; i <= 11-j; i++)
            spm_new_elem(A, i, n-11+i+j, 100.0 * (double)j);
      return A;
}

/***********************************************************************
*  NAME
*
*  spm_show_mat - write sparse matrix pattern in BMP file format
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  int spm_show_mat(const SPM *A, const char *fname);
*
*  DESCRIPTION
*
*  The routine spm_show_mat writes pattern of the specified sparse
*  matrix in uncompressed BMP file format (Windows bitmap) to a binary
*  file whose name is specified by the character string fname.
*
*  Each pixel corresponds to one matrix element. The pixel colors have
*  the following meaning:
*
*  Black    structurally zero element
*  White    positive element
*  Cyan     negative element
*  Green    zero element
*  Red      duplicate element
*
*  RETURNS
*
*  If no error occured, the routine returns zero. Otherwise, it prints
*  an appropriate error message and returns non-zero. */

int spm_show_mat(const SPM *A, const char *fname)
{     int m = A->m;
      int n = A->n;
      int i, j, k, ret;
      char *map;
      xprintf("spm_show_mat: writing matrix pattern to '%s'...\n",
         fname);
      xassert(1 <= m && m <= 32767);
      xassert(1 <= n && n <= 32767);
      map = xmalloc(m * n);
      memset(map, 0x08, m * n);
      for (i = 1; i <= m; i++)
      {  SPME *e;
         for (e = A->row[i]; e != NULL; e = e->r_next)
         {  j = e->j;
            xassert(1 <= j && j <= n);
            k = n * (i - 1) + (j - 1);
            if (map[k] != 0x08)
               map[k] = 0x0C;
            else if (e->val > 0.0)
               map[k] = 0x0F;
            else if (e->val < 0.0)
               map[k] = 0x0B;
            else
               map[k] = 0x0A;
         }
      }
      ret = rgr_write_bmp16(fname, m, n, map);
      xfree(map);
      return ret;
}

/***********************************************************************
*  NAME
*
*  spm_read_hbm - read sparse matrix in Harwell-Boeing format
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  SPM *spm_read_hbm(const char *fname);
*
*  DESCRIPTION
*
*  The routine spm_read_hbm reads a sparse matrix in the Harwell-Boeing
*  format from a text file whose name is the character string fname.
*
*  Detailed description of the Harwell-Boeing format recognised by this
*  routine can be found in the following report:
*
*  I.S.Duff, R.G.Grimes, J.G.Lewis. User's Guide for the Harwell-Boeing
*  Sparse Matrix Collection (Release I), TR/PA/92/86, October 1992.
*
*  NOTE
*
*  The routine spm_read_hbm reads the matrix "as is", due to which zero
*  and/or duplicate elements can appear in the matrix.
*
*  RETURNS
*
*  If no error occured, the routine returns a pointer to the matrix
*  created. Otherwise, the routine prints an appropriate error message
*  and returns NULL. */

SPM *spm_read_hbm(const char *fname)
{     SPM *A = NULL;
      HBM *hbm;
      int nrow, ncol, nnzero, i, j, beg, end, ptr, *colptr, *rowind;
      double val, *values;
      char *mxtype;
      hbm = hbm_read_mat(fname);
      if (hbm == NULL)
      {  xprintf("spm_read_hbm: unable to read matrix\n");
         goto fini;
      }
      mxtype = hbm->mxtype;
      nrow = hbm->nrow;
      ncol = hbm->ncol;
      nnzero = hbm->nnzero;
      colptr = hbm->colptr;
      rowind = hbm->rowind;
      values = hbm->values;
      if (!(strcmp(mxtype, "RSA") == 0 || strcmp(mxtype, "PSA") == 0 ||
            strcmp(mxtype, "RUA") == 0 || strcmp(mxtype, "PUA") == 0 ||
            strcmp(mxtype, "RRA") == 0 || strcmp(mxtype, "PRA") == 0))
      {  xprintf("spm_read_hbm: matrix type '%s' not supported\n",
            mxtype);
         goto fini;
      }
      A = spm_create_mat(nrow, ncol);
      if (mxtype[1] == 'S' || mxtype[1] == 'U')
         xassert(nrow == ncol);
      for (j = 1; j <= ncol; j++)
      {  beg = colptr[j];
         end = colptr[j+1];
         xassert(1 <= beg && beg <= end && end <= nnzero + 1);
         for (ptr = beg; ptr < end; ptr++)
         {  i = rowind[ptr];
            xassert(1 <= i && i <= nrow);
            if (mxtype[0] == 'R')
               val = values[ptr];
            else
               val = 1.0;
            spm_new_elem(A, i, j, val);
            if (mxtype[1] == 'S' && i != j)
               spm_new_elem(A, j, i, val);
         }
      }
fini: if (hbm != NULL) hbm_free_mat(hbm);
      return A;
}

/***********************************************************************
*  NAME
*
*  spm_count_nnz - determine number of non-zeros in sparse matrix
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  int spm_count_nnz(const SPM *A);
*
*  RETURNS
*
*  The routine spm_count_nnz returns the number of structural non-zero
*  elements in the specified sparse matrix. */

int spm_count_nnz(const SPM *A)
{     SPME *e;
      int i, nnz = 0;
      for (i = 1; i <= A->m; i++)
         for (e = A->row[i]; e != NULL; e = e->r_next) nnz++;
      return nnz;
}

/***********************************************************************
*  NAME
*
*  spm_drop_zeros - remove zero elements from sparse matrix
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  int spm_drop_zeros(SPM *A, double eps);
*
*  DESCRIPTION
*
*  The routine spm_drop_zeros removes all elements from the specified
*  sparse matrix, whose absolute value is less than eps.
*
*  If the parameter eps is 0, only zero elements are removed from the
*  matrix.
*
*  RETURNS
*
*  The routine returns the number of elements removed. */

int spm_drop_zeros(SPM *A, double eps)
{     SPME *e, *next;
      int i, count = 0;
      for (i = 1; i <= A->m; i++)
      {  for (e = A->row[i]; e != NULL; e = next)
         {  next = e->r_next;
            if (e->val == 0.0 || fabs(e->val) < eps)
            {  /* remove element from the row list */
               if (e->r_prev == NULL)
                  A->row[e->i] = e->r_next;
               else
                  e->r_prev->r_next = e->r_next;
               if (e->r_next == NULL)
                  ;
               else
                  e->r_next->r_prev = e->r_prev;
               /* remove element from the column list */
               if (e->c_prev == NULL)
                  A->col[e->j] = e->c_next;
               else
                  e->c_prev->c_next = e->c_next;
               if (e->c_next == NULL)
                  ;
               else
                  e->c_next->c_prev = e->c_prev;
               /* return element to the memory pool */
               dmp_free_atom(A->pool, e, sizeof(SPME));
               count++;
            }
         }
      }
      return count;
}

/***********************************************************************
*  NAME
*
*  spm_read_mat - read sparse matrix from text file
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  SPM *spm_read_mat(const char *fname);
*
*  DESCRIPTION
*
*  The routine reads a sparse matrix from a text file whose name is
*  specified by the parameter fname.
*
*  For the file format see description of the routine spm_write_mat.
*
*  RETURNS
*
*  On success the routine returns a pointer to the matrix created,
*  otherwise NULL. */

#if 1
SPM *spm_read_mat(const char *fname)
{     xassert(fname != fname);
      return NULL;
}
#else
SPM *spm_read_mat(const char *fname)
{     SPM *A = NULL;
      PDS *pds;
      jmp_buf jump;
      int i, j, k, m, n, nnz, fail = 0;
      double val;
      xprintf("spm_read_mat: reading matrix from '%s'...\n", fname);
      pds = pds_open_file(fname);
      if (pds == NULL)
      {  xprintf("spm_read_mat: unable to open '%s' - %s\n", fname,
            strerror(errno));
         fail = 1;
         goto done;
      }
      if (setjmp(jump))
      {  fail = 1;
         goto done;
      }
      pds_set_jump(pds, jump);
      /* number of rows, number of columns, number of non-zeros */
      m = pds_scan_int(pds);
      if (m < 0)
         pds_error(pds, "invalid number of rows\n");
      n = pds_scan_int(pds);
      if (n < 0)
         pds_error(pds, "invalid number of columns\n");
      nnz = pds_scan_int(pds);
      if (nnz < 0)
         pds_error(pds, "invalid number of non-zeros\n");
      /* create matrix */
      xprintf("spm_read_mat: %d rows, %d columns, %d non-zeros\n",
         m, n, nnz);
      A = spm_create_mat(m, n);
      /* read matrix elements */
      for (k = 1; k <= nnz; k++)
      {  /* row index, column index, element value */
         i = pds_scan_int(pds);
         if (!(1 <= i && i <= m))
            pds_error(pds, "row index out of range\n");
         j = pds_scan_int(pds);
         if (!(1 <= j && j <= n))
            pds_error(pds, "column index out of range\n");
         val = pds_scan_num(pds);
         /* add new element to the matrix */
         spm_new_elem(A, i, j, val);
      }
      xprintf("spm_read_mat: %d lines were read\n", pds->count);
done: if (pds != NULL) pds_close_file(pds);
      if (fail && A != NULL) spm_delete_mat(A), A = NULL;
      return A;
}
#endif

/***********************************************************************
*  NAME
*
*  spm_write_mat - write sparse matrix to text file
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  int spm_write_mat(const SPM *A, const char *fname);
*
*  DESCRIPTION
*
*  The routine spm_write_mat writes the specified sparse matrix to a
*  text file whose name is specified by the parameter fname. This file
*  can be read back with the routine spm_read_mat.
*
*  RETURNS
*
*  On success the routine returns zero, otherwise non-zero.
*
*  FILE FORMAT
*
*  The file created by the routine spm_write_mat is a plain text file,
*  which contains the following information:
*
*     m n nnz
*     row[1] col[1] val[1]
*     row[2] col[2] val[2]
*     . . .
*     row[nnz] col[nnz] val[nnz]
*
*  where:
*  m is the number of rows;
*  n is the number of columns;
*  nnz is the number of non-zeros;
*  row[k], k = 1,...,nnz, are row indices;
*  col[k], k = 1,...,nnz, are column indices;
*  val[k], k = 1,...,nnz, are element values. */

#if 1
int spm_write_mat(const SPM *A, const char *fname)
{     xassert(A != A);
      xassert(fname != fname);
      return 0;
}
#else
int spm_write_mat(const SPM *A, const char *fname)
{     FILE *fp;
      int i, nnz, ret = 0;
      xprintf("spm_write_mat: writing matrix to '%s'...\n", fname);
      fp = fopen(fname, "w");
      if (fp == NULL)
      {  xprintf("spm_write_mat: unable to create '%s' - %s\n", fname,
            strerror(errno));
         ret = 1;
         goto done;
      }
      /* number of rows, number of columns, number of non-zeros */
      nnz = spm_count_nnz(A);
      fprintf(fp, "%d %d %d\n", A->m, A->n, nnz);
      /* walk through rows of the matrix */
      for (i = 1; i <= A->m; i++)
      {  SPME *e;
         /* walk through elements of i-th row */
         for (e = A->row[i]; e != NULL; e = e->r_next)
         {  /* row index, column index, element value */
            fprintf(fp, "%d %d %.*g\n", e->i, e->j, DBL_DIG, e->val);
         }
      }
      fflush(fp);
      if (ferror(fp))
      {  xprintf("spm_write_mat: writing error on '%s' - %s\n", fname,
            strerror(errno));
         ret = 1;
         goto done;
      }
      xprintf("spm_write_mat: %d lines were written\n", 1 + nnz);
done: if (fp != NULL) fclose(fp);
      return ret;
}
#endif

/***********************************************************************
*  NAME
*
*  spm_transpose - transpose sparse matrix
*
*  SYNOPSIS
*
*  #include "glpspm.h"
*  SPM *spm_transpose(const SPM *A);
*
*  RETURNS
*
*  The routine computes and returns sparse matrix B, which is a matrix
*  transposed to sparse matrix A. */

SPM *spm_transpose(const SPM *A)
{     SPM *B;
      int i;
      B = spm_create_mat(A->n, A->m);
      for (i = 1; i <= A->m; i++)
      {  SPME *e;
         for (e = A->row[i]; e != NULL; e = e->r_next)
            spm_new_elem(B, e->j, i, e->val);
      }
      return B;
}

SPM *spm_add_sym(const SPM *A, const SPM *B)
{     /* add two sparse matrices (symbolic phase) */
      SPM *C;
      int i, j, *flag;
      xassert(A->m == B->m);
      xassert(A->n == B->n);
      /* create resultant matrix */
      C = spm_create_mat(A->m, A->n);
      /* allocate and clear the flag array */
      flag = xcalloc(1+C->n, sizeof(int));
      for (j = 1; j <= C->n; j++)
         flag[j] = 0;
      /* compute pattern of C = A + B */
      for (i = 1; i <= C->m; i++)
      {  SPME *e;
         /* at the beginning i-th row of C is empty */
         /* (i-th row of C) := (i-th row of C) union (i-th row of A) */
         for (e = A->row[i]; e != NULL; e = e->r_next)
         {  /* (note that i-th row of A may have duplicate elements) */
            j = e->j;
            if (!flag[j])
            {  spm_new_elem(C, i, j, 0.0);
               flag[j] = 1;
            }
         }
         /* (i-th row of C) := (i-th row of C) union (i-th row of B) */
         for (e = B->row[i]; e != NULL; e = e->r_next)
         {  /* (note that i-th row of B may have duplicate elements) */
            j = e->j;
            if (!flag[j])
            {  spm_new_elem(C, i, j, 0.0);
               flag[j] = 1;
            }
         }
         /* reset the flag array */
         for (e = C->row[i]; e != NULL; e = e->r_next)
            flag[e->j] = 0;
      }
      /* check and deallocate the flag array */
      for (j = 1; j <= C->n; j++)
         xassert(!flag[j]);
      xfree(flag);
      return C;
}

void spm_add_num(SPM *C, double alfa, const SPM *A, double beta,
      const SPM *B)
{     /* add two sparse matrices (numeric phase) */
      int i, j;
      double *work;
      /* allocate and clear the working array */
      work = xcalloc(1+C->n, sizeof(double));
      for (j = 1; j <= C->n; j++)
         work[j] = 0.0;
      /* compute matrix C = alfa * A + beta * B */
      for (i = 1; i <= C->n; i++)
      {  SPME *e;
         /* work := alfa * (i-th row of A) + beta * (i-th row of B) */
         /* (note that A and/or B may have duplicate elements) */
         for (e = A->row[i]; e != NULL; e = e->r_next)
            work[e->j] += alfa * e->val;
         for (e = B->row[i]; e != NULL; e = e->r_next)
            work[e->j] += beta * e->val;
         /* (i-th row of C) := work, work := 0 */
         for (e = C->row[i]; e != NULL; e = e->r_next)
         {  j = e->j;
            e->val = work[j];
            work[j] = 0.0;
         }
      }
      /* check and deallocate the working array */
      for (j = 1; j <= C->n; j++)
         xassert(work[j] == 0.0);
      xfree(work);
      return;
}

SPM *spm_add_mat(double alfa, const SPM *A, double beta, const SPM *B)
{     /* add two sparse matrices (driver routine) */
      SPM *C;
      C = spm_add_sym(A, B);
      spm_add_num(C, alfa, A, beta, B);
      return C;
}

SPM *spm_mul_sym(const SPM *A, const SPM *B)
{     /* multiply two sparse matrices (symbolic phase) */
      int i, j, k, *flag;
      SPM *C;
      xassert(A->n == B->m);
      /* create resultant matrix */
      C = spm_create_mat(A->m, B->n);
      /* allocate and clear the flag array */
      flag = xcalloc(1+C->n, sizeof(int));
      for (j = 1; j <= C->n; j++)
         flag[j] = 0;
      /* compute pattern of C = A * B */
      for (i = 1; i <= C->m; i++)
      {  SPME *e, *ee;
         /* compute pattern of i-th row of C */
         for (e = A->row[i]; e != NULL; e = e->r_next)
         {  k = e->j;
            for (ee = B->row[k]; ee != NULL; ee = ee->r_next)
            {  j = ee->j;
               /* if a[i,k] != 0 and b[k,j] != 0 then c[i,j] != 0 */
               if (!flag[j])
               {  /* c[i,j] does not exist, so create it */
                  spm_new_elem(C, i, j, 0.0);
                  flag[j] = 1;
               }
            }
         }
         /* reset the flag array */
         for (e = C->row[i]; e != NULL; e = e->r_next)
            flag[e->j] = 0;
      }
      /* check and deallocate the flag array */
      for (j = 1; j <= C->n; j++)
         xassert(!flag[j]);
      xfree(flag);
      return C;
}

void spm_mul_num(SPM *C, const SPM *A, const SPM *B)
{     /* multiply two sparse matrices (numeric phase) */
      int i, j;
      double *work;
      /* allocate and clear the working array */
      work = xcalloc(1+A->n, sizeof(double));
      for (j = 1; j <= A->n; j++)
         work[j] = 0.0;
      /* compute matrix C = A * B */
      for (i = 1; i <= C->m; i++)
      {  SPME *e, *ee;
         double temp;
         /* work := (i-th row of A) */
         /* (note that A may have duplicate elements) */
         for (e = A->row[i]; e != NULL; e = e->r_next)
            work[e->j] += e->val;
         /* compute i-th row of C */
         for (e = C->row[i]; e != NULL; e = e->r_next)
         {  j = e->j;
            /* c[i,j] := work * (j-th column of B) */
            temp = 0.0;
            for (ee = B->col[j]; ee != NULL; ee = ee->c_next)
               temp += work[ee->i] * ee->val;
            e->val = temp;
         }
         /* reset the working array */
         for (e = A->row[i]; e != NULL; e = e->r_next)
            work[e->j] = 0.0;
      }
      /* check and deallocate the working array */
      for (j = 1; j <= A->n; j++)
         xassert(work[j] == 0.0);
      xfree(work);
      return;
}

SPM *spm_mul_mat(const SPM *A, const SPM *B)
{     /* multiply two sparse matrices (driver routine) */
      SPM *C;
      C = spm_mul_sym(A, B);
      spm_mul_num(C, A, B);
      return C;
}

PER *spm_create_per(int n)
{     /* create permutation matrix */
      PER *P;
      int k;
      xassert(n >= 0);
      P = xmalloc(sizeof(PER));
      P->n = n;
      P->row = xcalloc(1+n, sizeof(int));
      P->col = xcalloc(1+n, sizeof(int));
      /* initially it is identity matrix */
      for (k = 1; k <= n; k++)
         P->row[k] = P->col[k] = k;
      return P;
}

void spm_check_per(PER *P)
{     /* check permutation matrix for correctness */
      int i, j;
      xassert(P->n >= 0);
      for (i = 1; i <= P->n; i++)
      {  j = P->row[i];
         xassert(1 <= j && j <= P->n);
         xassert(P->col[j] == i);
      }
      return;
}

void spm_delete_per(PER *P)
{     /* delete permutation matrix */
      xfree(P->row);
      xfree(P->col);
      xfree(P);
      return;
}

/* eof */