-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSparse_Math.pas
907 lines (800 loc) · 24.8 KB
/
Sparse_Math.pas
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
{*******************************************************************************
Electric Power Resarch Insitute EPRI 2018
Library created to handle sparse matrix linear algebra ops, the arguments need
to be sparse and provided in compressed coordiante format
Created by Davis Montenegro for EPRI 08/01/2018
based on the code provided by Sudarshan Khasnis in Java at
https://www.geeksforgeeks.org/operations-sparse-matrices/
*******************************************************************************}
{$HINTS OFF}
unit Sparse_Math;
interface
uses
{$IFDEF MSWINDOWS}
{$IFNDEF CONSOLE}
Dialogs,
{$ELSE}
CmdForms,
{$ENDIF}
{$ENDIF}
UComplex,
Ucmatrix;
type
TCmplx_Data = record
Row,
Col: Integer;
Value: Complex;
end;
type
TData = array of Integer;
PData = ^TData;
TComplex = array of TCmplx_Data;
PComplex = ^TComplex;
TComplexArr = array of Complex;
PComplexArr = ^TComplexArr;
type
Tsparse_matrix = class(Tobject)
PRIVATE
row,
col,
len: Integer;
function checkifexists(r, c: Integer): Integer;
procedure getrow(index: Integer; cols, vals: PData);
function R_equal(acols, avals, bcols, bvals: PData): Boolean;
PUBLIC
data: array of array of Integer;
procedure sparse_matrix(r, c: Integer);
function insert(r, c, val: Integer): Integer;
function add(b: Tsparse_matrix): Tsparse_matrix;
function Transpose(): Tsparse_matrix;
function multiply(b: Tsparse_matrix): Tsparse_matrix;
procedure reset();
function NZero(): Integer;
function NCols(): Integer;
function NRows(): Integer;
function Rank(): Integer;
end;
Tsparse_Complex = class(Tobject)
PRIVATE
row,
col,
len: Integer;
function checkifexists(r, c: Integer): Integer;
procedure getrow(index: Integer; cols: PData; vals: PComplexArr);
function getvalue(row, col: Integer): Complex;
function R_equal(acols, bcols: PData; avals, bvals: PComplexArr): Boolean;
PUBLIC
CData: array of TCmplx_Data;
procedure sparse_matrix_Cmplx(r, c: Integer);
function insert(r, c: Integer; val: Complex): Integer;
function add(b: Tsparse_Complex): Tsparse_Complex;
function Transpose(): Tsparse_Complex;
function TransposeConj(): Tsparse_Complex;
function multiply(b: Tsparse_Complex): Tsparse_Complex;
procedure reset();
function NZero(): Integer;
function NCols(): Integer;
function NRows(): Integer;
function Rank(): Integer;
end;
implementation
// Evaluates of both rows are equal
function Tsparse_matrix.R_equal(acols, avals, bcols, bvals: PData): Boolean;
var
idx,
rlen: Integer;
begin
Result := FALSE; // In case they are not equal
if length(acols^) = length(bcols^) then // If they have the same # of Cols
begin
rlen := 0; // First, verify if the cols are the same
for idx := 0 to high(acols^) do
if (acols^[idx] - bcols^[idx]) <> 0 then
inc(rlen);
if rlen = 0 then
Result := TRUE;
end;
end;
// Gets the columns and values at each columns for the row specified
procedure Tsparse_matrix.getrow(index: Integer; cols, vals: PData);
var
rowcols,
rowvals: TData;
j: Integer;
begin
setlength(rowcols, 0);
setlength(rowvals, 0);
for j := 0 to (len - 1) do
begin
if data[j][0] = index then
begin
setlength(rowcols, length(rowcols) + 1);
setlength(rowvals, length(rowvals) + 1);
rowcols[high(rowcols)] := data[j][1];
rowvals[high(rowvals)] := data[j][2];
end;
end;
cols^ := rowcols;
vals^ := rowvals;
end;
function Tsparse_matrix.Rank(): Integer; // Added 08/16/2018 by DM for calculating the
var // Rank of the sparse matrix
i,
j: Integer;
flag: Boolean;
acols, // Row under evaluation
avals,
bcols, // Reference row
bvals: TData;
begin
Result := 0;
for i := 0 to (row - 1) do
begin
getrow(i, @acols, @avals);
if i > 0 then
begin
j := i - 1;
flag := TRUE;
while flag and (j >= 0) do
begin
getrow(j, @bcols, @bvals); // sweeps the matrix bottom up
flag := not R_equal(@acols, @avals, @bcols, @bvals);
dec(j);
end;
if flag then
inc(Result);
end
else
inc(Result);
end;
end;
function Tsparse_matrix.NCols(): Integer;
begin
Result := Col;
end;
function Tsparse_matrix.NRows(): Integer;
begin
Result := Row;
end;
function Tsparse_matrix.checkifexists(r, c: Integer): Integer;
var
i: Integer;
begin
Result := -1; // Default in case the value doesn't exist
if len > 0 then
begin
for i := 0 to (len - 1) do
begin
if (data[i][0] = r) and (data[i][1] = c) then
Result := i; // If the value exists returns the index ( >=0 )
end;
end;
end;
procedure Tsparse_matrix.sparse_matrix(r, c: Integer);
begin
row := r; // Initialize row
col := c; // Initialize Col
len := 0; // Initialize length to 0
setlength(data, 0);
end;
//Inserts elements into the sparse matrix
function Tsparse_matrix.insert(r, c, val: Integer): Integer;
var
lrow,
lcol: Integer; // To store the current lenght of the data matrix
begin
Result := 1;
lrow := checkifexists(r, c);
if lrow >= 0 then
begin
data[lrow][2] := val; // Assigns the new value to the existing cell
end
else
begin
// Reshapes the memory space
lrow := length(data);
setlength(data, lrow + 1);
setlength(data[lrow], 3);
// Adds the data to the new memory space
data[high(data)][0] := r;
data[high(data)][1] := c;
data[high(data)][2] := val;
inc(len);
if col < c then
col := c;
if row < r then
row := r;
end;
end;
// Adds another sparse matrix to this matrix
function Tsparse_matrix.add(b: Tsparse_matrix): Tsparse_matrix;
var
addeval,
apos,
bpos: Integer;
begin
// Creates a memory space to store the result
Result := Tsparse_matrix.create;
// First checks if the matrices have the same dimensions
if (row <> b.row) or (col <> b.col) then
begin
Result.sparse_matrix(1, 1);
Result.insert(0, 0, -1);
end
else
begin
apos := 0;
bpos := 0;
Result.sparse_matrix(row, col);
while (apos < len) and (bpos < b.len) do
begin
if (data[apos][0] > b.data[bpos][0]) or ((data[apos][0] = b.data[bpos][0]) and (data[apos][1] > b.data[bpos][1])) then
begin
Result.insert(b.data[bpos][0], b.data[bpos][1], b.data[bpos][2]);
inc(bpos)
end
else
begin
if (data[apos][0] < b.data[bpos][0]) or ((data[apos][0] = b.data[bpos][0]) and (data[apos][1] < b.data[bpos][1])) then
begin
Result.insert(data[apos][0], data[apos][1], data[apos][2]);
inc(apos)
end
else
begin
addeval := data[apos][2] + b.data[bpos][2];
if addeval <> 0 then
Result.insert(data[apos][0], data[apos][1], addeval);
inc(apos);
inc(bpos);
end;
end;
end;
// Inserts the remaining elements
while (apos < (len - 1)) do
begin
Result.insert(data[apos][0], data[apos][1], data[apos + 1][2]);
inc(apos)
end;
while (bpos < (b.len - 1)) do
begin
Result.insert(b.data[bpos][0], b.data[bpos][1], b.data[bpos + 1][2]);
inc(bpos)
end;
end;
end;
// Transposes the sparse matrix
function Tsparse_matrix.Transpose(): Tsparse_matrix;
var
Count,
Index: array of Integer;
i,
rpos: Integer;
begin
// Creates a memory space to store the result
Result := Tsparse_matrix.create;
// new matrix with inversed row X col
Result.sparse_matrix(col, row);
// same number of elements
for i := 1 to len do
Result.insert(i, 0, 0);
setlength(Count, col + 1);
setlength(Index, col + 1);
// Initialize all to 0
for i := 0 to col do
Count[i] := 0;
for i := 0 to (len - 1) do
inc(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 i := 1 to col do
Index[i] := Index[i - 1] + Count[i - 1];
for i := 0 to (len - 1) do
begin
// insert a data at rpos and increment its value
rpos := index[data[i][1]];
inc(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];
end;
// the above method ensures
// sorting of transpose matrix
// according to row-col value
end;
// Multiplies another sparse matrix by this matrix
function Tsparse_matrix.multiply(b: Tsparse_matrix): Tsparse_matrix;
var
sum,
c,
tempa,
tempb,
r,
apos,
bpos: Integer;
begin
// Creates a memory space to store the result
Result := Tsparse_matrix.create;
// First checks if the matrices have the right dimensions
if col <> b.row then
begin
Result.sparse_matrix(1, 1);
Result.insert(0, 0, -1); //Invalid multiplication
end
else
begin
// 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 do
begin
r := data[apos][0];
// iterate over all elements of B
bpos := 0;
while bpos < b.len do
begin
// 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) and (data[tempa][0] = r) and (tempb < b.len) and (b.data[tempb][0] = c) do
begin
if (data[tempa][1] < b.data[tempb][1]) then
inc(tempa) //skip a
else
begin
if (data[tempa][1] > b.data[tempb][1]) then
inc(tempb) //skip b
else
begin
// same col, so multiply and increment
sum := sum + data[tempa][2] * b.data[tempb][2];
inc(tempa);
inc(tempb);
end;
end;
end;
// insert sum obtained in result[r]
// if its not equal to 0
if sum <> 0 then
result.insert(r, c, sum);
while (bpos < b.len) and (b.data[bpos][0] = c) do
inc(bpos); // Jump to next column
end;
while (apos < len) and (data[apos][0] = r) do
inc(apos); // Jump to next row
end;
end;
end;
// Resets the sparse matrix (makes it empty)
procedure Tsparse_matrix.reset();
begin
setlength(data, 0);
len := 0;
end;
// Returns the lenght of the sparse matrix (number of non-zero elements)
function Tsparse_matrix.NZero(): Integer;
begin
Result := len;
end;
//******************************************************************************
//* Complex sparse matrices
//******************************************************************************
// Evaluates of both rows are equal
function Tsparse_Complex.R_equal(acols, bcols: PData; avals, bvals: PComplexArr): Boolean;
var
idx,
rlen: Integer;
begin
Result := FALSE; // In case they are not equal
if length(acols^) = length(bcols^) then // If they have the same # of Cols
begin
rlen := 0; // First, verify if the cols are the same
for idx := 0 to high(acols^) do
if (acols^[idx] - bcols^[idx]) <> 0 then
inc(rlen);
if rlen = 0 then
Result := TRUE;
end;
end;
// Returns the value contained at the specific position
function Tsparse_Complex.getvalue(row, col: Integer): Complex;
var
go_flag: Boolean;
i: Integer;
begin
Result := cmplx(0, 0);
go_flag := TRUE;
i := 0;
while go_flag do
begin
if (CData[i].Row = row) and (CData[i].Col = col) then
begin
Result := CData[i].Value;
go_flag := FALSE;
end
else
begin
inc(i);
if i > High(CData) then
go_flag := FALSE;
end;
end;
end;
// Gets the columns and values at each columns for the row specified
procedure Tsparse_Complex.getrow(index: Integer; cols: PData; vals: PComplexArr);
var
rowcols: TData;
rowvals: TComplexArr;
j: Integer;
begin
setlength(rowcols, 0);
setlength(rowvals, 0);
for j := 0 to (len - 1) do
begin
if CData[j].Row = index then
begin
setlength(rowcols, length(rowcols) + 1);
setlength(rowvals, length(rowvals) + 1);
rowcols[high(rowcols)] := CData[j].Col;
rowvals[high(rowvals)] := CData[j].Value;
end;
end;
cols^ := rowcols;
vals^ := rowvals;
end;
function Tsparse_Complex.Rank(): Integer; // Added 08/16/2018 by DM for calculating the
var // Rank of the sparse matrix
i,
j: Integer;
flag: Boolean;
acols, // Row under evaluation
bcols: TData; // Reference row
avals,
bvals: TComplexArr;
begin
Result := 0;
for i := 0 to (row - 1) do
begin
getrow(i, @acols, @avals);
if i > 0 then
begin
j := i - 1;
flag := TRUE;
while flag and (j >= 0) do
begin
getrow(j, @bcols, @bvals); // sweeps the matrix bottom up
flag := not R_equal(@acols, @avals, @bcols, @bvals);
dec(j);
end;
if flag then
inc(Result);
end
else
inc(Result);
end;
end;
function Tsparse_Complex.NCols(): Integer;
begin
Result := Col;
end;
function Tsparse_Complex.NRows(): Integer;
begin
Result := Row;
end;
function Tsparse_Complex.checkifexists(r, c: Integer): Integer;
var
i: Integer;
begin
Result := -1; // Default in case the value doesn't exist
if len > 0 then
begin
for i := 0 to (len - 1) do
begin
if (CData[i].Row = r) and (CData[i].Col = c) then
Result := i; // If the value exists returns the index ( >=0 )
end;
end;
end;
procedure Tsparse_Complex.sparse_matrix_Cmplx(r, c: Integer);
begin
row := r; // Initialize row
col := c; // Initialize Col
len := 0; // Initialize length to 0
setlength(CData, 0);
end;
//Inserts elements into the sparse matrix
function Tsparse_Complex.insert(r, c: Integer; val: Complex): Integer;
var
lrow,
lcol: Integer; // To store the current lenght of the data matrix
begin
Result := 1;
lrow := checkifexists(r, c);
if lrow >= 0 then
begin
CData[lrow].Value := val; // Assigns the new value to the existing cell
end
else
begin
// Reshapes the memory space
lrow := length(CData);
setlength(CData, lrow + 1);
// Adds the data to the new memory space
CData[high(CData)].Row := r;
CData[high(CData)].Col := c;
CData[high(CData)].Value := val;
inc(len);
if col < c then
col := c;
if row < r then
row := r;
end;
end;
// Adds another sparse matrix to this matrix
function Tsparse_Complex.add(b: Tsparse_Complex): Tsparse_Complex;
var
addeval: Complex;
apos,
bpos: Integer;
begin
// Creates a memory space to store the result
Result := Tsparse_Complex.create;
// First checks if the matrices have the same dimensions
if (row <> b.row) or (col <> b.col) then
begin
Result.sparse_matrix_Cmplx(1, 1);
Result.insert(0, 0, cmplx(-1, 0));
end
else
begin
apos := 0;
bpos := 0;
Result.sparse_matrix_Cmplx(row, col);
while (apos < len) and (bpos < b.len) do
begin
if (CData[apos].Row > b.CData[bpos].Row) or ((CData[apos].Row = b.CData[bpos].Row) and (CData[apos].Col > b.CData[bpos].Col)) then
begin
Result.insert(b.CData[bpos].Row, b.CData[bpos].Col, b.CData[bpos].Value);
inc(bpos)
end
else
begin
if (CData[apos].Row < b.CData[bpos].Row) or ((CData[apos].Row = b.CData[bpos].Row) and (CData[apos].Col < b.CData[bpos].Col)) then
begin
Result.insert(CData[apos].Row, CData[apos].Col, CData[apos].Value);
inc(apos)
end
else
begin
addeval := cadd(CData[apos].Value, b.CData[bpos].Value);
if (addeval.re <> 0) and (addeval.im <> 0) then
Result.insert(CData[apos].Row, CData[apos].Col, addeval);
inc(apos);
inc(bpos);
end;
end;
end;
// Inserts the remaining elements
while (apos < (len - 1)) do
begin
Result.insert(CData[apos].Row, CData[apos].Col, CData[apos + 1].Value);
inc(apos)
end;
while (bpos < (b.len - 1)) do
begin
Result.insert(b.CData[bpos].Row, b.CData[bpos].Col, b.CData[bpos + 1].Value);
inc(bpos)
end;
end;
end;
// Transposes the sparse matrix
function Tsparse_Complex.Transpose(): Tsparse_Complex;
var
Count,
Index: array of Integer;
i,
j,
k,
rpos: Integer;
begin
// Creates a memory space to store the result
Result := Tsparse_Complex.create;
// new matrix with inversed row X col
Result.sparse_matrix_Cmplx(col, row);
// same number of elements
j := 0;
k := 0;
for i := 1 to len do
begin
Result.insert(j, k, cZERO);
inc(k);
if k = row then
begin
inc(j);
k := 0;
end;
end;
setlength(Count, col + 1);
setlength(Index, col + 1);
// Initialize all to 0
for i := 0 to col do
Count[i] := 0;
for i := 0 to (len - 1) do
inc(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 i := 1 to col do
Index[i] := Index[i - 1] + Count[i - 1];
for i := 0 to (len - 1) do
begin
// insert a data at rpos and increment its value
rpos := index[CData[i].Col];
inc(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;
end;
// the above method ensures
// sorting of transpose matrix
// according to row-col value
end;
// Transposes and conjugates the sparse matrix
function Tsparse_Complex.TransposeConj(): Tsparse_Complex;
var
Count,
Index: array of Integer;
i,
rpos: Integer;
begin
// Creates a memory space to store the result
Result := Tsparse_Complex.create;
// new matrix with inversed row X col
Result.sparse_matrix_Cmplx(col, row);
// same number of elements
for i := 1 to len do
Result.insert(i, 0, cmplx(0, 0));
setlength(Count, col + 1);
setlength(Index, col + 1);
// Initialize all to 0
for i := 0 to col do
Count[i] := 0;
for i := 0 to (len - 1) do
inc(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 i := 1 to col do
Index[i] := Index[i - 1] + Count[i - 1];
for i := 0 to (len - 1) do
begin
// insert a data at rpos and increment its value
rpos := index[CData[i].Col];
inc(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);
end;
// the above method ensures
// sorting of transpose matrix
// according to row-col value
end;
// Multiplies another sparse matrix by this matrix
function Tsparse_Complex.multiply(b: Tsparse_Complex): Tsparse_Complex;
var
sum: Complex;
c,
tempa,
tempb,
r,
apos,
bpos: Integer;
begin
// Creates a memory space to store the result
Result := Tsparse_Complex.create;
// First checks if the matrices have the right dimensions
if col <> b.row then
begin
Result.sparse_matrix_Cmplx(1, 1);
Result.insert(0, 0, cmplx(-1, 0)); //Invalid multiplication
end
else
begin
// 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 do
begin
r := CData[apos].Row;
// iterate over all elements of B
bpos := 0;
while bpos < b.len do
begin
// 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) and (CData[tempa].Row = r) and (tempb < b.len) and (b.CData[tempb].Row = c) do
begin
if (CData[tempa].Col < b.CData[tempb].Col) then
inc(tempa) //skip a
else
begin
if (CData[tempa].Col > b.CData[tempb].Col) then
inc(tempb) //skip b
else
begin
// same col, so multiply and increment
sum := cadd(sum, cmul(CData[tempa].Value, b.CData[tempb].Value));
inc(tempa);
inc(tempb);
end;
end;
end;
// insert sum obtained in result[r]
// if its not equal to 0
if (sum.re <> 0) and (sum.im <> 0) then
result.insert(r, c, sum);
while (bpos < b.len) and (b.CData[bpos].Row = c) do
inc(bpos); // Jump to next column
end;
while (apos < len) and (CData[apos].Row = r) do
inc(apos); // Jump to next row
end;
end;
end;
// Resets the sparse matrix (makes it empty)
procedure Tsparse_Complex.reset();
begin
setlength(CData, 0);
len := 0;
end;
// Returns the lenght of the sparse matrix (number of non-zero elements)
function Tsparse_Complex.NZero(): Integer;
begin
Result := len;
end;
end.