-
Notifications
You must be signed in to change notification settings - Fork 2
/
SparseReduceMatrix.cpp
782 lines (674 loc) · 24 KB
/
SparseReduceMatrix.cpp
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
/******************************************************************/
/*** FILE : SparseReduceMatrix.c ***/
/*** PROGRAMMER: David Lee ***/
/*** DATE WRITTEN: April-August 1992. ***/
/*** PUBLIC ROUTINES: ***/
/*** SparseReduceMatrix() ***/
/*** Get_Matrix_Element() ***/
/*** Insert_Element() ***/
/*** Delete_Element() ***/
/*** Change_Element() ***/
/*** Locate_Node() ***/
/*** PRIVATE ROUTINES: ***/
/*** SparseMultRow() ***/
/*** SparseAddRow() ***/
/*** SparseKnockOut() ***/
/*** SparseInterchange() ***/
/*** Insert_Node() ***/
/*** Delete_Node() ***/
/*** Change_Node() ***/
/*** Row_Empty() ***/
/*** MODULE DESCRIPTION: ***/
/*** This module reduces the sparse matrix ***/
/*** in row canonical form. This code is ***/
/*** similar to the code in ReduceMatrix.c ***/
/******************************************************************/
#include <list>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "SparseReduceMatrix.h"
#include "Build_defs.h"
#include "Scalar_arithmetic.h"
#include "profile.h"
#include "memory_usage.h"
extern bool __record;
extern int __deg;
extern int __nn1;
extern int __nn2;
namespace SparseReduceMatrix_ns {
using std::max;
using std::min;
using std::list;
using std::vector;
using std::lower_bound;
//using std::random_shuffle;
static void SparseMultRow(SparseMatrix &SM, int Row, Scalar Factor);
static void SparseAddRow(SparseMatrix &SM, Scalar Factor, int Row1, int Row2);
static void SparseKnockOut(SparseMatrix &SM, int row, int col, int last_row);
#if 0
static void Print_Matrix(MAT_PTR Sparse_Matrix, int r, int c);
static void Print_Rows(int Row1, int Row2, int nCols);
static void Print_SLList(Node *SLHead_Ptr);
static void Print_Node(NODE_PTR Prt_Node);
#endif
#define DEBUG_MATRIX 0
static bool do_sort = true;
static int sort_freq = 1;
struct stats {
//size_t n_zero_elements;
size_t n_elements;
size_t capacity;
size_t n_zero_rows;
size_t n_rows;
size_t n_cols;
int last_nextstairrow;
int prev_col;
int cur_col;
time_t first_update;
time_t prev_update;
time_t cur_update;
stats() : // n_zero_elements(0),
n_elements(0),
capacity(0),
n_zero_rows(0),
n_rows(0),
n_cols(0),
last_nextstairrow(0),
prev_col(0),
cur_col(0),
first_update(0),
prev_update(0),
cur_update(0) {}
void clear() {
//n_zero_elements = 0;
n_elements = 0;
capacity = 0;
n_zero_rows = 0;
n_rows = 0;
n_cols = 0;
last_nextstairrow = 0;
// These are not reset between updates because they are used
// to calculate rates.
//prev_col = 0;
//cur_col = 0;
//first_update = 0;
//prev_update = 0;
//cur_update = 0;
}
static void tp(float t) {
if (t > 3600) {
printf("%.02fh", t / 3600.);
} else if (t > 60) {
printf("%.02fm", t / 60.);
} else {
printf("%.02fs", t);
}
}
void print() const {
printf("\r\t\tne:%lu (%.1fMB)", n_elements, n_elements * sizeof(Node) / 1024. / 1024.);
#if 0
if(n_zero_elements > 0) {
printf(" ze:%lu", n_zero_elements);
}
#endif
if (n_elements != capacity) {
printf(" ce:%lu", capacity);
}
printf(" zr:%lu lr:%d/%lu lc:%d/%lu",
n_zero_rows,
last_nextstairrow, n_rows,
cur_col, n_cols);
{
time_t dt = cur_update - first_update;
if (dt > 0) {
printf(" tt:");
tp(dt);
}
}
if (cur_col > 100) {
int dt = cur_update - prev_update;
if (dt != 0) {
float cps = (cur_col - prev_col + 1) / float(dt);
printf(" cps:%.02f", cps);
float eta = (n_cols - cur_col) / cps;
if (eta > 1) {
printf(" etr:");
tp(eta);
}
}
}
printf(" ");
fflush(nullptr);
}
void update(const SparseMatrix &SM, int nextstairrow_, int last_col_, int nCols_, int timeout = -1,
bool do_print = false) {
time_t t = time(nullptr);
if (timeout != -1 && cur_update != 0 && t - cur_update < timeout) {
return;
}
clear();
if (first_update == 0) {
first_update = t;
}
prev_update = cur_update;
cur_update = t;
n_rows = SM.size();
n_cols = nCols_;
last_nextstairrow = nextstairrow_;
prev_col = cur_col;
cur_col = last_col_;
for (int ii = 0; ii < (int) SM.size(); ii++) {
capacity += SM[ii].capacity();
n_elements += SM[ii].size();
if (SM[ii].empty()) {
n_zero_rows++;
}
#if 0
// There should be no zero elements
for(int jj=0; jj<(int)SM[ii].size(); jj++) {
if(SM[ii][jj].getElement() == S_zero()) {
n_zero_elements++;
}
}
#endif
}
if (do_print) {
print();
}
}
};
static void save_mat_image(int a, int b, int c, const SparseMatrix &SM, int nCols) {
int mh = SM.size();
int mw = nCols;
if (mh * mw == 0) return;
const int iih = 2160;
const int iiw = 3840;
int ih = min(mh, iih);
int iw = min(mw, iiw);
// if(mh < ih || mw < iw) return;
auto img = new unsigned char[ih * iw]();
for (int r = 0; r < (int) SM.size(); r++) {
for (int j = 0; j < (int) SM[r].size(); j++) {
if (SM[r][j].getElement() != S_zero()) {
int col = SM[r][j].getColumn();
int y = r;
int x = col;
if (ih < mh) y = int(y / float(mh) * ih);
if (iw < mw) x = int(x / float(mw) * iw);
//if(x >= iw) abort();
//if(y >= ih) abort();
if (img[y * iw + x] < 255) {
img[y * iw + x]++;
}
}
}
}
#if 0
int mv = 0;
for (int i = 0; i < ih * iw; i++) {
mv = max(int(img[i]), mv);
}
for (int i = 0; i < ih * iw; i++) {
if (img[i] != 0) {
img[i] = int(float(img[i]) / float(mv) * (255 - 63) + 63 + .5);
}
}
#else
for (int i = 0; i < ih * iw; i++) {
if (img[i]) {
img[i] = 255;
}
}
#endif
{
int s = 1;
if (ih >= iw) {
s = iih / ih;
} else {
s = iih / iw;
}
if (s > 1) {
int hoff = (iih - s * ih) / 2;
int woff = (iiw - s * iw) / 2;
auto s_img = new unsigned char[iih * iiw]();
for (int i = 0; i < ih; i++) {
for (int j = 0; j < iw; j++) {
for (int ii = 0; ii < s; ii++) {
for (int jj = 0; jj < s; jj++) {
s_img[((i * s + hoff + ii) * iiw) + woff + j * s + jj] = img[i * iw + j];
}
}
}
}
delete[] img;
img = s_img;
ih = iih;
iw = iiw;
}
}
char fn[128];
static int ind = 0;
//sprintf(fn, "%d_%d_%d__%d_%d_%d__%d.pgm", __deg, __nn1, __nn2, a, b, c, ind);
sprintf(fn, "%08d_%03d_%03d_%03d_%06d_%06d.pgm", ind, __deg, __nn1, __nn2, c, nCols);
ind++;
FILE *f = fopen(fn, "wb");
fprintf(f, "P5\n%d %d 255\n", iw, ih);
fwrite(img, 1, ih * iw, f);
fclose(f);
delete[] img;
}
static bool SM_sort(const SparseRow &r1, const SparseRow &r2) {
{
if (r1.empty()) return false;
if (r2.empty()) return true;
//auto a = r1.empty();
//auto b = r2.empty();
//if (!a && b) return true;
//if (a && b) return false;
//if (a && !b) return false;
}
auto r1i = r1.front();
auto r2i = r2.front();
{
auto a = r1i.getColumn();
auto b = r2i.getColumn();
if (a < b) return true;
if (a > b) return false;
}
{
auto a = r1.size();
auto b = r2.size();
#if 1
// Generally results in greater sparsity
if (a < b) return true;
if (a > b) return false;
#else
// Generally results in greater density, i.e. more non-zero intermediate entries
if (a > b) return true;
if (a < b) return false;
#endif
}
{
auto a = r1i.getElement();
auto b = r2i.getElement();
if (a < b) return true;
//if (a > b) return false;
}
return false;
}
int SparseReduceMatrix(SparseMatrix &SM, int nCols, int *Rank) {
memory_usage_init(nCols);
if (SM.empty() || nCols == 0) {
return OK;
}
if (do_sort) sort(SM.begin(), SM.end(), SM_sort);
//random_shuffle(SM.begin(), SM.end());
//printf("s:%d c:%d ", (int)SM.size(), (int)SM.capacity());
/* Search for the rightmost nonzero element */
/* Dependent on the current stairrow */
stats s1;
s1.update(SM, 0, 0, nCols, -1, true);
float nper = .1;
if (__record) {
save_mat_image(0, 0, 0, SM, nCols);
}
int nextstairrow = 0;
int last_row = SM.size();
for (int i = 0; i < nCols; i++) {
memory_usage_update(i);
// putchar('\n');
// Profile p("Iteration");
int j;
{
// Profile p("find next");
for (j = nextstairrow; j < last_row; j++) {
if (Get_Matrix_Element(SM, j, i) != S_zero()) {
break;
}
}
}
/* When found interchange and then try to knockout any nonzero
elements in the same column */
#if DEBUG_MATRIX
printf("\nCol:%d/%d j:%d nextstairrow:%d nRows:%d reducing?:%d\n", i, nCols, j, nextstairrow, SM.size(), j < (int) SM.size());
{
printf("Start\n");
for (int i = 0; i < (int) SM.size(); i++) {
for (int j = 0; j<(int)nCols; j++) {
Scalar s = Get_Matrix_Element(SM, i, j);
printf(" %3d", s);
}
putchar('\n');
}
}
#endif
if (j < last_row) {
SM[nextstairrow].swap(SM[j]);
#if DEBUG_MATRIX
{
printf("After swap\n");
for (int i = 0; i < (int) SM.size(); i++) {
for (int j = 0; j<(int)nCols; j++) {
Scalar s = Get_Matrix_Element(SM, i, j);
printf(" %3d", s);
}
putchar('\n');
}
}
#endif
{
char s[128];
sprintf(s, "Knockout %d/%d %d/%d/%d", i, nCols, nextstairrow, last_row, SM.size());
//Profile p0("c");
// Profile p(s);
SparseKnockOut(SM, nextstairrow, i, last_row);
}
for (int iii = last_row; iii > 0; iii--) {
if (!SM[iii - 1].empty()) {
last_row = iii;
break;
}
}
// printf("%d %d\n", SM.size(), last_row);
if (do_sort) {
if (i % sort_freq == 0) {
// Profile p1("sort");
sort(SM.begin() + nextstairrow + 1, SM.begin() + last_row, SM_sort);
}
}
#if 0
if (__record) {
printf(">> %d", nextstairrow);
long aa = 0;
long bb = 0;
for (int ii = nextstairrow; ii < SM.size(); ii++) {
// printf("(%.2f %.2f) ", SM[i].size() / float(nCols - nextstairrow) * 100, SM[ii].size() * 4 / float(nCols - nextstairrow + 4));
if (!SM[ii].empty()) {
int a = SM[ii].size() * 4;
int b = (nCols - SM[ii].front().getColumn() + 1) * 1 + 4;
printf(" %.2f", a / float(b));
aa += a;
bb += b;
}
}
printf("\n %.2f\n", aa / float(bb));
}
#endif
#if DEBUG_MATRIX
{
printf("After reduce\n");
for (int i = 0; i < (int) SM.size(); i++) {
for (int j = 0; j<(int)nCols; j++) {
Scalar s = Get_Matrix_Element(SM, i, j);
printf(" %3d", s);
}
putchar('\n');
}
}
#endif
nextstairrow++;
}
if (__record && (i / float(nCols) > nper)) {
nper += .1;
save_mat_image(0, 1, i, SM, nCols);
}
{
// Profile p("update");
s1.update(SM, nextstairrow, i, nCols, 60, true);
}
}
*Rank = nextstairrow;
s1.update(SM, nextstairrow, nCols, nCols, -1, true);
putchar('\n');
if (__record) {
save_mat_image(0, 2, nCols, SM, nCols);
}
#if DEBUG_MATRIX
{
printf("Final\n");
for (int i = 0; i < (int) SM.size(); i++) {
for (int j = 0; j<(int)nCols; j++) {
Scalar s = Get_Matrix_Element(SM, i, j);
printf(" %3d", s);
}
putchar('\n');
}
}
#endif
return OK;
}
void SparseMultRow(SparseMatrix &SM, int Row, Scalar Factor) {
/* Step thru row ... multiplying each element by the factor */
for (auto ii = SM[Row].begin(); ii != SM[Row].end(); ii++) {
ii->setElement(S_mul(ii->getElement(), Factor));
}
}
template<typename T, class Allocator>
void shrink_capacity(std::vector<T, Allocator> &v) {
std::vector<T, Allocator>(v.begin(), v.end()).swap(v);
}
/*********************************************************************/
/* Three things can happen with the SparseAddRow routine ....
First there is a target row and a row which is multiplied by a factor
and added to the target row.
1. The result is nonzero and there is no column in the target row so add
a new node.
2. The result is nonzero and there is a column in the target row so change
the value in the node.
3. The result is zero and there is a column in the target row so delete
the node.
*/
/*********************************************************************/
void SparseAddRow(SparseMatrix &SM, Scalar Factor, int Row1, int Row2) {
/* check for zero factor */
if (Factor == S_zero()) {
return;
}
/* get the beginning of the two rows to work with */
const SparseRow &r1 = SM[Row1];
SparseRow &r2 = SM[Row2];
SparseRow tmp;
tmp.reserve(r1.size() + r2.size());
auto r1i = r1.cbegin();
auto r2i = r2.cbegin();
for (; r1i != r1.cend() && r2i != r2.cend();) {
if (r1i->getColumn() == r2i->getColumn()) {
Scalar x = S_add(r2i->getElement(), S_mul(Factor, r1i->getElement()));
if (x != S_zero()) {
Node n = *r1i;
n.setElement(x);
tmp.push_back(n);
}
r1i++;
r2i++;
} else if (r1i->getColumn() < r2i->getColumn()) {
Scalar x = S_mul(Factor, r1i->getElement());
//if(x != S_zero()) {
Node n = *r1i;
n.setElement(x);
tmp.push_back(n);
//}
r1i++;
} else { //if(r1i->column > r2i->column) {
tmp.push_back(*r2i);
r2i++;
}
}
// append r2 with remaining r1 nodes
for (; r1i != r1.cend(); r1i++) {
Scalar x = S_mul(Factor, r1i->getElement());
//if(x != S_zero()) {
Node n = *r1i;
n.setElement(x);
tmp.push_back(n);
//}
}
// append r2 with remaining r1 nodes
for (; r2i != r2.cend(); r2i++) {
tmp.push_back(*r2i);
}
SparseRow(tmp.begin(), tmp.end()).swap(r2); // shrink capacity while assigning
//r2 = SparseRow(tmp.begin(), tmp.end());
//shrink_capacity(tmp);
//printf("<%d %d %d %d>", (int)r2.size(), (int)r2.capacity(), (int)tmp.size(), (int)tmp.capacity());
//r2 = tmp;
//r2.swap(tmp);
}
void SparseKnockOut(SparseMatrix &SM, int row, int col, int last_row) {
Scalar x = Get_Matrix_Element(SM, row, col);
if (x != S_one()) {
/* if the rightmost element in the current row is not one then multiply*/
SparseMultRow(SM, row, S_inv(x));
}
/* try to knockout elements in column in the rows above */
#pragma omp parallel for shared(SM, row, col, last_row) schedule(static, 50) default(none)
// for (int j = 0; j < (int) SM.size(); j++) {
for (int j = 0; j < last_row; j++) {
if (j != row) {
SparseAddRow(SM, S_minus(Get_Matrix_Element(SM, j, col)), row, j);
}
}
}
#if 0
void Print_Matrix(MAT_PTR Sparse_Matrix, int r, int c) {
int i, row, col;
Node *Row_Head_Ptr, *Row_Element_Ptr;
if (Sparse_Matrix == NULL) {
return;
}
for (row = 0; row < r; row++) {
Row_Head_Ptr = Sparse_Matrix[row];
Row_Element_Ptr = Row_Head_Ptr;
if (Row_Element_Ptr == NULL) {
printf("EMPTY ROW %d\n", row);
} else {
while (Row_Element_Ptr != NULL) {
printf("%4d", Row_Element_Ptr->element);
Row_Element_Ptr = Row_Element_Ptr->Next_Node;
}
printf("\n");
}
}
printf("\n");
for (row = 0; row < r; row++) {
Row_Head_Ptr = Sparse_Matrix[row];
Row_Element_Ptr = Row_Head_Ptr;
for (col = 0; (col < c); col++) {
if (Row_Element_Ptr != NULL) {
if (Row_Element_Ptr->column != col) {
printf(" 0");
} else {
printf("%4d", Row_Element_Ptr->element);
if (Row_Element_Ptr->Next_Node != NULL)
Row_Element_Ptr = Row_Element_Ptr->Next_Node;
}
} else {
printf(" 0");
}
}
printf("\n");
}
printf("\n");
}
void Print_Rows(int Row1, int Row2, int nCols) {
int i, row, col;
NODE_PTR Row1_Ptr;
NODE_PTR Row2_Ptr;
Row1_Ptr = Matrix_Base_Ptr[Row1];
Row2_Ptr = Matrix_Base_Ptr[Row2];
for (col = 0; (col < nCols); col++) {
if (Row1_Ptr != NULL) {
if (Row1_Ptr->column != col) {
printf(" 0");
} else {
printf("%4d", Row1_Ptr->element);
if (Row1_Ptr->Next_Node != NULL)
Row1_Ptr = Row1_Ptr->Next_Node;
}
} else {
printf(" 0");
}
}
printf("\n");
for (col = 0; (col < nCols); col++) {
if (Row2_Ptr != NULL) {
if (Row2_Ptr->column != col) {
printf(" 0");
} else {
printf("%4d", Row2_Ptr->element);
if (Row2_Ptr->Next_Node != NULL)
Row2_Ptr = Row2_Ptr->Next_Node;
}
} else {
printf(" 0");
}
}
printf("\n");
}
#endif
#if 0
static bool cmp_column(const Node &n1, const Node &n2) { return n1.column < n2.column; }
#if 0
struct A {
bool operator()(const Node &n1, const Node &n2) const { return n1.column < n2.column; }
};
#endif
Scalar Get_Matrix_Element(const SparseMatrix &SM, int i, int j) {
Node n;
n.column = j;
SparseRow::const_iterator ii = lower_bound(SM[i].begin(), SM[i].end(), n, cmp_column);
//SparseRow::const_iterator ii = lower_bound(SM[i].begin(), SM[i].end(), n, A());
//SparseRow::const_iterator ii = lower_bound(SM[i].begin(), SM[i].end(), n);
if (ii != SM[i].end() && ii->column == j) {
return ii->element;
}
return S_zero();
}
#else
Scalar Get_Matrix_Element(const SparseMatrix &SM, int i, int j) {
/* either return the element at location i,j or return a zero */
for (auto ii = SM[i].cbegin(); ii != SM[i].cend() && ii->getColumn() <= j; ii++) {
if (ii->getColumn() == j) return ii->getElement();
}
return S_zero();
}
#endif
#if 0
void Print_SLList(Node *SLHead_Ptr) {
Node *Prt_Ptr;
Prt_Ptr = SLHead_Ptr;
printf("\nColumn :");
while (Prt_Ptr != nullptr) {
printf(" %3d", Prt_Ptr->column);
Prt_Ptr = Prt_Ptr->Next_Node;
}
Prt_Ptr = SLHead_Ptr;
printf("\n");
printf("Element:");
while (Prt_Ptr != nullptr) {
printf(" %3d", Prt_Ptr->element);
Prt_Ptr = Prt_Ptr->Next_Node;
}
printf("\n");
printf("\n");
}
void Print_Node(NODE_PTR Prt_Node) {
if (Prt_Node == nullptr) {
printf("NULL\n");
return;
}
printf("Node element:%d\tcolumn:%d\n", Prt_Node->element,
Prt_Node->column);
}
#endif
}
int SparseReduceMatrix(SparseMatrix &SM, int nCols, int *Rank) {
return SparseReduceMatrix_ns::SparseReduceMatrix(SM, nCols, Rank);
}
Scalar Get_Matrix_Element(const SparseMatrix &SM, int i, int j) {
return SparseReduceMatrix_ns::Get_Matrix_Element(SM, i, j);
}