-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSparse_Math.cpp
992 lines (856 loc) · 27.9 KB
/
Sparse_Math.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
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
#pragma hdrstop
#include "Sparse_Math.h"
// Evaluates of both rows are equal
#include "System.h"
namespace Sparse_Math
{
Tsparse_matrix::Tsparse_matrix()
: Row(0),
col(0),
len(0)
{
}
TSparse_Complex::TSparse_Complex()
: Row(0),
col(0),
len(0)
{
}
bool Tsparse_matrix::R_equal(PData acols, PData avals, PData bcols, PData bvals)
{
bool result = false;
int idx = 0, rlen = 0;
result = false; // In case they are not equal
if (acols->size() == bcols->size()) // If they have the same # of Cols
{
rlen = 0; // First, verify if the cols are the same
for (int stop = (acols->size() - 1), idx = 0; idx <= stop; idx++)
if (((*acols)[idx] - (*bcols)[idx] ) != 0)
rlen++;
if (rlen == 0)
result = true;
}
return result;
}
// Gets the columns and values at each columns for the row specified
/*
Tsparse_matrix::Tsparse_matrix()
: Row(0),
col(0),
len(0)
{
}
TSparse_Complex::TSparse_Complex()
: Row(0),
col(0),
len(0)
{
}
*/
void Tsparse_matrix::getrow(int Index, PData cols, PData vals)
{
TData rowcols, rowvals;
int j = 0;
rowcols.clear();
rowvals.clear();
for (int stop = (len - 1), j = 0; j <= stop; j++)
{
if (data[j][0] == Index)
{
rowcols.push_back(data[j][1]);
rowvals.push_back(data[j][2]);
}
}
*cols = rowcols;
*vals = rowvals;
}
int Tsparse_matrix::Rank() // Added 08/16/2018 by DM for calculating the
{
int result = 0;
// Rank of the sparse matrix
int i = 0, j = 0;
bool Flag = false; // Row under evaluation
// Reference row
TData acols, avals, bcols, bvals;
result = 0;
for (int stop = (Row - 1), i = 0; i <= stop; i++)
{
getrow(i, &acols, &avals);
if (i > 0)
{
j = i - 1;
Flag = true;
while (Flag && (j >= 0))
{
getrow(j, &bcols, &bvals); // sweeps the matrix bottom up
Flag = !R_equal(&acols, &avals, &bcols, &bvals);
j--;
}
if (Flag)
result++;
}
else
result++;
}
return result;
}
/*
Tsparse_matrix::Tsparse_matrix()
: Row(0),
col(0),
len(0)
{
}
TSparse_Complex::TSparse_Complex()
: Row(0),
col(0),
len(0)
{
}
*/
int Tsparse_matrix::NCols()
{
int result = 0;
result = col;
return result;
}
int Tsparse_matrix::NRows()
{
int result = 0;
result = Row;
return result;
}
int Tsparse_matrix::checkifexists(int r, int c)
{
int result = 0;
int i = 0;
result = -1; // Default in case the value doesn't exist
if (len > 0)
{
for (int stop = (len - 1), i = 0; i <= stop; i++)
{
if ((data[i][0] == r) && (data[i][1] == c))
result = i; // If the value exists returns the index ( >=0 )
}
}
return result;
}
void Tsparse_matrix::sparse_matrix(int r, int c)
{
Row = r; // Initialize row
col = c; // Initialize Col
len = 0; // Initialize length to 0
data.clear();
}
//Inserts elements into the sparse matrix
int Tsparse_matrix::Insert(int r, int c, int val)
{
int result = 0;
int lrow = 0 // To store the current lenght of the data matrix
, lcol = 0;
result = 1;
lrow = checkifexists(r, c);
if (lrow >= 0)
{
data[lrow][2] = val; // Assigns the new value to the existing cell
}
else
{
// Reshapes the memory space
lrow = data.size();
data.resize( lrow + 1 );
data[lrow].resize(3);
// Adds the data to the new memory space
data[data.size() - 1][0] = r;
data[data.size() - 1][1] = c;
data[data.size() - 1][2] = val;
len++;
if (col < c)
col = c;
if (Row < r)
Row = r;
}
return result;
}
/*
Tsparse_matrix::Tsparse_matrix()
: Row(0),
col(0),
len(0)
{
}
TSparse_Complex::TSparse_Complex()
: Row(0),
col(0),
len(0)
{
}
*/
Tsparse_matrix* Tsparse_matrix::Add(Tsparse_matrix* b)
{
Tsparse_matrix* result = NULL;
int addeval = 0, apos = 0, bpos = 0;
// Creates a memory space to store the result
result = new Tsparse_matrix;
// First checks if the matrices have the same dimensions
if ((Row != b->Row) || (col != b->col))
{
result->sparse_matrix(1, 1);
result->Insert(0, 0, -1);
}
else
{
apos = 0;
bpos = 0;
result->sparse_matrix(Row, col);
while ((apos < len) && (bpos < b->len))
{
if ((data[apos][0] > b->data[bpos][0]) || ((data[apos][0] == b->data[bpos][0]) && (data[apos][1] > b->data[bpos][1])))
{
result->Insert(b->data[bpos][0], b->data[bpos][1], b->data[bpos][2]);
bpos++;
}
else
{
if ((data[apos][0] < b->data[bpos][0]) || ((data[apos][0] == b->data[bpos][0]) && (data[apos][1] < b->data[bpos][1])))
{
result->Insert(data[apos][0], data[apos][1], data[apos][2]);
apos++;
}
else
{
addeval = data[apos][2] + b->data[bpos][2];
if (addeval != 0)
result->Insert(data[apos][0], data[apos][1], addeval);
apos++;
bpos++;
}
}
}
// Inserts the remaining elements
while (apos < (len - 1))
{
result->Insert(data[apos][0], data[apos][1], data[apos + 1][2]);
apos++;
}
while (bpos < (b->len - 1))
{
result->Insert(b->data[bpos][0], b->data[bpos][1], b->data[bpos + 1][2]);
bpos++;
}
}
return result;
}
// Transposes the sparse matrix
Tsparse_matrix* Tsparse_matrix::Transpose()
{
Tsparse_matrix* result = NULL;
std::vector < int > count, Index;
int i = 0, rpos = 0;
// Creates a memory space to store the result
result = new Tsparse_matrix;
// new matrix with inversed row X col
result->sparse_matrix(col, Row);
// same number of elements
for (int stop = len, i = 1; i <= stop; i++)
result->Insert(i, 0, 0);
count.resize(col + 1);
Index.resize(col + 1);
// Initialize all to 0
for (int stop = col, i = 0; i <= stop; i++)
count[i] = 0;
for (int stop = (len - 1), i = 0; i <= stop; i++)
count[data[i][1]]++;
// to count number of elements having col smaller
// than particular i
// as there is no col with value < 1
Index[0] = 0;
// initialize rest of the indices
for (int stop = col, i = 1; i <= stop; i++)
Index[i] = Index[i - 1] + count[i - 1];
for (int stop = (len - 1), i = 0; i <= stop; i++)
{
// insert a data at rpos and increment its value
rpos = Index[data[i][1]];
Index[data[i][1]]++;
// transpose row=col
result->data[rpos][0] = data[i][1];
// transpose col=row
result->data[rpos][1] = data[i][0];
// same value
result->data[rpos][2] = data[i][2];
}
// the above method ensures
// sorting of transpose matrix
// according to row-col value
return result;
}
// Multiplies another sparse matrix by this matrix
/*
Tsparse_matrix::Tsparse_matrix()
: Row(0),
col(0),
len(0)
{
}
TSparse_Complex::TSparse_Complex()
: Row(0),
col(0),
len(0)
{
}
*/
Tsparse_matrix* Tsparse_matrix::multiply(Tsparse_matrix* b)
{
Tsparse_matrix* result = NULL;
int sum = 0, c = 0, tempa = 0, tempb = 0, r = 0, apos = 0, bpos = 0;
// Creates a memory space to store the result
result = new Tsparse_matrix;
// First checks if the matrices have the right dimensions
if (col != b->Row)
{
result->sparse_matrix(1, 1);
result->Insert(0, 0, -1); //Invalid multiplication
}
else
{
// transpose b to compare row
// and col values and to add them at the end
b = b->Transpose();
// result matrix of dimension row X b.col
// however b has been transposed, hence row X b.row
result->sparse_matrix(Row, b->Row);
// iterate over all elements of A (this matrix)
apos = 0;
while (apos < len)
{
r = data[apos][0];
// iterate over all elements of B
bpos = 0;
while (bpos < b->len)
{
// current column of result matrix
// data[][0] used as b is transposed
c = b->data[bpos][0];
// temporary pointers created to add all
// multiplied values to obtain current
// element of result matrix
tempa = apos;
tempb = bpos;
sum = 0;
// iterate over all elements with
// same row and col value
// to calculate result[r]
while ((tempa < len) && (data[tempa][0] == r) && (tempb < b->len) && (b->data[tempb][0] == c))
{
if (data[tempa][1] < b->data[tempb][1])
tempa++; //skip a
else
{
if (data[tempa][1] > b->data[tempb][1])
tempb++; //skip b
else
{
// same col, so multiply and increment
sum = sum + data[tempa][2] * b->data[tempb][2];
tempa++;
tempb++;
}
}
}
// insert sum obtained in result[r]
// if its not equal to 0
if (sum != 0)
result->Insert(r, c, sum);
while ((bpos < b->len) && (b->data[bpos][0] == c))
bpos++; // Jump to next column
}
while ((apos < len) && (data[apos][0] == r))
apos++; // Jump to next row
}
}
return result;
}
// Sorts the content of the matrix by rows, important for multiplications
void Tsparse_matrix::Sort()
{
Tsparse_matrix* myTemp;
myTemp = new Tsparse_matrix;
myTemp->Reset();
for (int idx = 0; idx <= Row; idx++)
{
for (int i = 0; i < data.size(); i++)
{
if (data[i][0] == idx)
myTemp->Insert(data[i][0], data[i][1], data[i][2]);
}
}
// moves the new data into the local object
for (int i = 0; i < myTemp->data.size(); i++)
{
data[i][0] = myTemp->data[i][0];
data[i][1] = myTemp->data[i][1];
data[i][2] = myTemp->data[i][2];
}
}
// Resets the sparse matrix (makes it empty)
void Tsparse_matrix::Reset()
{
data.clear();
len = 0;
}
// Returns the lenght of the sparse matrix (number of non-zero elements)
int Tsparse_matrix::NZero()
{
int result = 0;
result = len;
return result;
}
//******************************************************************************
//* Complex sparse matrices
//******************************************************************************
// Evaluates of both rows are equal
bool TSparse_Complex::R_equal(PData acols, PData bcols, PComplexArr avals, PComplexArr bvals)
{
bool result = false;
int idx = 0, rlen = 0;
result = false; // In case they are not equal
if (acols->size() == bcols->size()) // If they have the same # of Cols
{
rlen = 0; // First, verify if the cols are the same
for (int stop = (acols->size() - 1), idx = 0; idx <= stop; idx++)
if (((*acols)[idx] - (*bcols)[idx]) != 0)
rlen++;
if (rlen == 0)
result = true;
}
return result;
}
// Returns the value contained at the specific position
complex TSparse_Complex::getvalue(int Row, int col)
{
complex result;
bool Go_Flag = false;
int i = 0;
result = cmplx(0, 0);
Go_Flag = true;
i = 0;
while (Go_Flag)
{
if ((CData[i].Row == Row) && (CData[i].col == col))
{
result = CData[i].Value;
Go_Flag = false;
}
else
{
i++;
if (i > ( CData.size() - 1 ) )
Go_Flag = false;
}
}
return result;
}
// Gets the columns and values at each columns for the row specified
void TSparse_Complex::getrow(int Index, PData cols, PComplexArr vals)
{
TData rowcols;
TComplexArr rowvals;
int j = 0;
rowcols.clear();
rowvals.clear();
for (int stop = (len - 1), j = 0; j <= stop; j++)
{
if (CData[j].Row == Index)
{
rowcols.push_back( CData[j].col );
rowvals.push_back( CData[j].Value );
}
}
*cols = rowcols;
*vals = rowvals;
}
int TSparse_Complex::Rank() // Added 08/16/2018 by DM for calculating the
{
int result = 0;
// Rank of the sparse matrix
int i = 0, j = 0;
bool Flag = false; // Row under evaluation
TData acols, bcols; // Reference row
TComplexArr avals, bvals;
result = 0;
for (int stop = (Row - 1), i = 0; i <= stop; i++)
{
getrow(i, &acols, &avals);
if (i > 0)
{
j = i - 1;
Flag = true;
while (Flag && (j >= 0))
{
getrow(j, &bcols, &bvals); // sweeps the matrix bottom up
Flag = !R_equal(&acols, &bcols, &avals, &bvals);
j--;
}
if (Flag)
result++;
}
else
result++;
}
return result;
}
int TSparse_Complex::NCols()
{
int result = 0;
result = col;
return result;
}
int TSparse_Complex::NRows()
{
int result = 0;
result = Row;
return result;
}
int TSparse_Complex::checkifexists(int r, int c)
{
int result = 0;
int i = 0;
result = -1; // Default in case the value doesn't exist
if (len > 0)
{
for (int stop = (len - 1), i = 0; i <= stop; i++)
{
if ((CData[i].Row == r) && (CData[i].col == c))
result = i; // If the value exists returns the index ( >=0 )
}
}
return result;
}
void TSparse_Complex::sparse_matrix_Cmplx(int r, int c)
{
Row = r; // Initialize row
col = c; // Initialize Col
len = 0; // Initialize length to 0
CData.clear();
}
//Inserts elements into the sparse matrix
int TSparse_Complex::Insert(int r, int c, complex val)
{
int result = 0;
int lrow = 0 // To store the current lenght of the data matrix
, lcol = 0;
result = 1;
lrow = checkifexists(r, c);
if (lrow >= 0)
{
CData[lrow].Value = val; // Assigns the new value to the existing cell
}
else
{
// Reshapes the memory space
lrow = CData.size();
CData.resize( lrow + 1 );
// Adds the data to the new memory space
CData[CData.size() - 1].Row = r;
CData[CData.size() - 1].col = c;
CData[CData.size() - 1].Value = val;
len++;
if (col < c)
col = c;
if (Row < r)
Row = r;
}
return result;
}
// Adds another sparse matrix to this matrix
TSparse_Complex* TSparse_Complex::Add(TSparse_Complex* b)
{
TSparse_Complex* result = NULL;
complex addeval;
int apos = 0, bpos = 0;
// Creates a memory space to store the result
result = new TSparse_Complex;
// First checks if the matrices have the same dimensions
if ((Row != b->Row) || (col != b->col))
{
result->sparse_matrix_Cmplx(1, 1);
result->Insert(0, 0, cmplx(-1, 0));
}
else
{
apos = 0;
bpos = 0;
result->sparse_matrix_Cmplx(Row, col);
while ((apos < len) && (bpos < b->len))
{
if ((CData[apos].Row > b->CData[bpos].Row) || ((CData[apos].Row == b->CData[bpos].Row) && (CData[apos].col > b->CData[bpos].col)))
{
result->Insert(b->CData[bpos].Row, b->CData[bpos].col, b->CData[bpos].Value);
bpos++;
}
else
{
if ((CData[apos].Row < b->CData[bpos].Row) || ((CData[apos].Row == b->CData[bpos].Row) && (CData[apos].col < b->CData[bpos].col)))
{
result->Insert(CData[apos].Row, CData[apos].col, CData[apos].Value);
apos++;
}
else
{
addeval = cadd(CData[apos].Value, b->CData[bpos].Value);
if ((addeval.re != 0) && (addeval.im != 0))
result->Insert(CData[apos].Row, CData[apos].col, addeval);
apos++;
bpos++;
}
}
}
// Inserts the remaining elements
while (apos < (len - 1))
{
result->Insert(CData[apos].Row, CData[apos].col, CData[apos + 1].Value);
apos++;
}
while (bpos < (b->len - 1))
{
result->Insert(b->CData[bpos].Row, b->CData[bpos].col, b->CData[bpos + 1].Value);
bpos++;
}
}
return result;
}
// Transposes the sparse matrix
/*
Tsparse_matrix::Tsparse_matrix()
: Row(0),
col(0),
len(0)
{
}
TSparse_Complex::TSparse_Complex()
: Row(0),
col(0),
len(0)
{
}*/
TSparse_Complex* TSparse_Complex::Transpose()
{
TSparse_Complex* result = NULL;
std::vector < int > count, Index;
int i = 0, j = 0, k = 0, rpos = 0;
// Creates a memory space to store the result
result = new TSparse_Complex;
// new matrix with inversed row X col
result->sparse_matrix_Cmplx(col, Row);
// same number of elements
j = 0;
k = 0;
for (int stop = len, i = 1; i <= stop; i++)
{
result->Insert(j, k, CZero);
k++;
if (k == Row)
{
j++;
k = 0;
}
}
count.resize( col + 1 );
Index.resize( col + 1 );
// Initialize all to 0
for (int stop = col, i = 0; i <= stop; i++)
count[i] = 0;
for (int stop = (len - 1), i = 0; i <= stop; i++)
count[CData[i].col]++;
// to count number of elements having col smaller
// than particular i
// as there is no col with value < 1
Index[0] = 0;
// initialize rest of the indices
for (int stop = col, i = 1; i <= stop; i++)
Index[i] = Index[i - 1] + count[i - 1];
for (int stop = (len - 1), i = 0; i <= stop; i++)
{
// insert a data at rpos and increment its value
rpos = Index[CData[i].col];
Index[CData[i].col]++;
// transpose row=col
result->CData[rpos].Row = CData[i].col;
// transpose col=row
result->CData[rpos].col = CData[i].Row;
// same value
result->CData[rpos].Value = CData[i].Value;
}
// the above method ensures
// sorting of transpose matrix
// according to row-col value
return result;
}
// Transposes and conjugates the sparse matrix
TSparse_Complex* TSparse_Complex::TransposeConj()
{
TSparse_Complex* result = NULL;
std::vector < int > count, Index;
int i = 0, rpos = 0;
// Creates a memory space to store the result
result = new TSparse_Complex;
// new matrix with inversed row X col
result->sparse_matrix_Cmplx(col, Row);
// same number of elements
for (int stop = len, i = 1; i <= stop; i++)
result->Insert(i, 0, cmplx(0, 0));
count.resize( col + 1 );
Index.resize( col + 1 );
// Initialize all to 0
for (int stop = col, i = 0; i <= stop; i++)
count[i] = 0;
for (int stop = (len - 1), i = 0; i <= stop; i++)
count[CData[i].col]++;
// to count number of elements having col smaller
// than particular i
// as there is no col with value < 1
Index[0] = 0;
// initialize rest of the indices
for (int stop = col, i = 1; i <= stop; i++)
Index[i] = Index[i - 1] + count[i - 1];
for (int stop = (len - 1), i = 0; i <= stop; i++)
{
// insert a data at rpos and increment its value
rpos = Index[CData[i].col];
Index[CData[i].col]++;
// transpose row=col
result->CData[rpos].Row = CData[i].col;
// transpose col=row
result->CData[rpos].col = CData[i].Row;
// same value
result->CData[rpos].Value = conjg(CData[i].Value);
}
// the above method ensures
// sorting of transpose matrix
// according to row-col value
return result;
}
// Multiplies another sparse matrix by this matrix
/*
Tsparse_matrix::Tsparse_matrix()
: Row(0),
col(0),
len(0)
{
}
TSparse_Complex::TSparse_Complex()
: Row(0),
col(0),
len(0)
{
}
*/
TSparse_Complex* TSparse_Complex::multiply(TSparse_Complex* b)
{
TSparse_Complex* result = NULL;
complex sum;
int c = 0, tempa = 0, tempb = 0, r = 0, apos = 0, bpos = 0;
// Creates a memory space to store the result
result = new TSparse_Complex;
// First checks if the matrices have the right dimensions
if (col != b->Row)
{
result->sparse_matrix_Cmplx(1, 1);
result->Insert(0, 0, cmplx(-1, 0)); //Invalid multiplication
}
else
{
// transpose b to compare row
// and col values and to add them at the end
b = b->Transpose();
// result matrix of dimension row X b.col
// however b has been transposed, hence row X b.row
result->sparse_matrix_Cmplx(Row, b->Row);
// iterate over all elements of A (this matrix)
apos = 0;
while (apos < len)
{
r = CData[apos].Row;
// iterate over all elements of B
bpos = 0;
while (bpos < b->len)
{
// current column of result matrix
// data[][0] used as b is transposed
c = b->CData[bpos].Row;
// temporary pointers created to add all
// multiplied values to obtain current
// element of result matrix
tempa = apos;
tempb = bpos;
sum = cmplx(0, 0);
// iterate over all elements with
// same row and col value
// to calculate result[r]
while ((tempa < len) && (CData[tempa].Row == r) && (tempb < b->len) && (b->CData[tempb].Row == c))
{
if (CData[tempa].col < b->CData[tempb].col)
tempa++; //skip a
else
{
if (CData[tempa].col > b->CData[tempb].col)
tempb++; //skip b
else
{
// same col, so multiply and increment
sum = cadd(sum, cmul(CData[tempa].Value, b->CData[tempb].Value));
tempa++;
tempb++;
}
}
}
// insert sum obtained in result[r]
// if its not equal to 0
if ((sum.re != 0) && (sum.im != 0))
result->Insert(r, c, sum);
while ((bpos < b->len) && (b->CData[bpos].Row == c))
bpos++; // Jump to next column
}
while ((apos < len) && (CData[apos].Row == r))
apos++; // Jump to next row
}
}
return result;
}
// Sorts the content of the matrix by rows, important for multiplications
void TSparse_Complex::Sort()
{
TSparse_Complex* myTemp;
myTemp = new TSparse_Complex;
myTemp->Reset();
for (int idx = 0; idx <= Row; idx++)
{
for (int i = 0; i < CData.size(); i++)
{
if (CData[i].Row == idx)
myTemp->Insert(CData[i].Row, CData[i].col, CData[i].Value);
}
}
// moves the new data into the local object
for (int i = 0; i < myTemp->CData.size(); i++)
{
CData[i].Row = myTemp->CData[i].Row;
CData[i].col = myTemp->CData[i].col;
CData[i].Value = myTemp->CData[i].Value;
}
}
// Resets the sparse matrix (makes it empty)
void TSparse_Complex::Reset()
{
CData.clear();
len = 0;
}
// Returns the lenght of the sparse matrix (number of non-zero elements)
int TSparse_Complex::NZero()
{
int result = 0;
result = len;
return result;
}
} // namespace Sparse_Math