-
Notifications
You must be signed in to change notification settings - Fork 1
/
src.d
1428 lines (1214 loc) · 34.3 KB
/
src.d
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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module gretlmat.base;
import std.conv, std.exception, std.range, std.stdio;
extern(C) {
int gretl_matrix_multiply(const GretlMatrix * a, const GretlMatrix * b, GretlMatrix * c);
void gretl_matrix_multiply_by_scalar(GretlMatrix * m, double x);
//~ int gretl_matrix_multiply_mod(const GretlMatrix * a, matmod amod, const GretlMatrix * b, matmod bmod, GretlMatrix * c, matmod cmod);
int gretl_matrix_cholesky_decomp(GretlMatrix * a);
int gretl_matrix_kronecker_product(const GretlMatrix * A, const GretlMatrix * B, GretlMatrix * K);
int gretl_LU_solve(GretlMatrix * a, GretlMatrix * b);
int gretl_invert_matrix(GretlMatrix * a);
double gretl_matrix_determinant(GretlMatrix * a, int * err);
double gretl_matrix_log_determinant(GretlMatrix * a, int * err);
double gretl_matrix_trace(const GretlMatrix * m);
void gretl_matrix_raise(GretlMatrix * m, double x);
void gretl_matrix_free(GretlMatrix * m);
int gretl_matrix_ols (const GretlMatrix * y, const GretlMatrix * X, GretlMatrix * b, GretlMatrix * vcv, GretlMatrix * uhat, double * s2);
void gretl_matrix_print(const GretlMatrix * m, const char * msg);
int gretl_matrix_transpose(GretlMatrix * targ, const GretlMatrix * src);
GretlMatrix * gretl_matrix_alloc(int rows, int cols);
double gretl_vector_mean (const GretlMatrix * v);
double gretl_vector_variance (const GretlMatrix * v);
}
private struct matrix_info {
int t1;
int t2;
char **colnames;
char **rownames;
}
/* This is used internally
* The end user should never have a reason to know anything about GretlMatrix.
*/
struct GretlMatrix {
int rows;
int cols;
double * ptr;
matrix_info * info;
GretlMatrix * matptr() {
return &this;
}
}
struct DoubleMatrix {
double[] data;
int rows;
int cols;
private GretlMatrix temp;
alias mat this;
invariant {
assert(rows > 0, "Number of rows has to be positive");
assert(cols > 0, "Number of columns has to be positive");
assert(rows*cols == data.length, "Dimensions do not match the underlying data array");
}
GretlMatrix mat() {
temp.rows = rows;
temp.cols = cols;
temp.ptr = data.ptr;
return temp;
}
GretlMatrix * matptr() {
temp.rows = rows;
temp.cols = cols;
temp.ptr = data.ptr;
return &temp;
}
double * ptr() {
return data.ptr;
}
this(int r, int c=1) {
assert(r*c > 0, "Need to allocate a positive number of elements in a DoubleMatrix");
data = new double[r*c];
rows = r;
cols = c;
}
// Use a template to allow conversion of arguments to int
this(T1, T2)(T1 r, T2 c=1) {
assert(r*c > 0, "Need to allocate a positive number of elements in a DoubleMatrix");
data = new double[r*c];
rows = r.to!int;
cols = c.to!int;
}
this(double[][] m, bool rowElements=true) {
data = new double[m.length*m[0].length];
// Treat each element as a row
if (rowElements) {
rows = to!int(m.length);
cols = to!int(m[0].length);
foreach(row, vals; m) {
foreach(col; 0..cols) {
data[elt(row, col)] = vals[col];
}
}
// Treat each element as a column
} else {
rows= to!int(m[0].length);
cols = to!int(m.length);
foreach(col, vals; m) {
foreach(row; 0..rows) {
data[elt(row, col)] = vals[row];
}
}
}
}
this(GretlMatrix * m) {
data = new double[m.cols*m.rows];
rows = m.rows;
cols = m.cols;
foreach(row; 0..rows) {
foreach(col; 0..cols) {
data[elt(row, col)] = m.ptr[elt(row, col)];
}
}
}
this(double[] v) {
data = v;
rows = to!int(v.length);
cols = 1;
}
int[2] dim() {
return [this.rows, this.cols];
}
int length() {
return data.length.to!int;
}
int index(int r, int c) {
return c*this.rows + r;
}
// Find element number associated with index
// Include all asserts in here
// These are stripped out in release mode
int elt(int r, int c) {
assert(r >= 0, "Cannot have a negative row index");
assert(c >= 0, "Cannot have a positive row index");
assert(r < this.rows, "First index (" ~ r.to!string ~ ") exceeds the number of rows");
assert(c < this.cols, "Second index (" ~ c.to!string ~ ") exceeds the number of columns");
return c*this.rows + r;
}
int elt(T1, T2)(T1 r, T2 c) {
return elt(r.to!int, c.to!int);
}
double opIndex(int r, int c) {
return data[elt(r, c)];
}
double opIndex(T1, T2)(T1 r, T2 c) {
return data[elt(r.to!int, c.to!int)];
}
double opIndex(int[2] ind) {
return data[elt(ind[0], ind[1])];
}
// Support for multidimensional indexing
int[2] opSlice(int dim)(int begin, int end) {
return [begin, end];
}
Submatrix opIndex(int[2] rr, int[2] cc) {
return Submatrix(this, rr[0], cc[0], rr[1], cc[1]);
}
//~ Col opIndex(AllElements tmp, int c) {
//~ return Col(this, c);
//~ }
Row opIndex(int r, AllElements tmp) {
return Row(this, r);
}
void opIndexAssign(double v, int r, int c) {
ptr[elt(r, c)] = v;
}
void opIndexAssign(double v, int[2] ind) {
opIndexAssign(v, ind[0], ind[1]);
}
void opIndexAssign(T1, T2)(double v, T1 r, T2 c) {
ptr[elt(r.to!int, c.to!int)] = v;
}
void opAssign(DoubleMatrix m) {
assert(this.data.length == m.data.length, "Dimensions do not match for matrix assignment");
this.data[] = m.data[];
}
void opAssign(double a) {
this.data[] = a;
}
void fill(double[] v) {
assert(this.data.length == v.length, "Argument to fill has length different from the number of elements in the matrix");
this.data[] = v[];
}
void fillByColumn(double[] v) {
fill(v);
}
void fillByRow(double[] v) {
assert(this.data.length == v.length, "Argument to fill has length different from the number of elements in the matrix");
foreach(row; 0..rows) {
foreach(col; 0..cols) {
this[row, col] = v[index(col, row)];
}
}
}
// Safe (non-mutating) approaches to changing dimensions
DoubleMatrix reshape(int newrows, int newcols=1) {
auto result = DoubleMatrix(newrows, newcols);
assert(result.length == this.length, "Wrong number of elements in call to reshape");
result.data[] = this.data[];
return result;
}
DoubleMatrix setColumns(int newcols) {
assert(newcols > 0, "Number of columns has to be greater than zero");
auto result = DoubleMatrix(this.length / newcols, newcols);
assert(result.length == this.length, "Wrong number of elements in call to setColumns");
result.data[] = this.data[];
return result;
}
DoubleMatrix setRows(int newrows) {
assert(newrows > 0, "Number of rows has to be greater than zero");
auto result = DoubleMatrix(newrows, this.rows*this.cols / newrows);
assert(result.length == this.length, "Wrong number of elements in call to setRows");
result.data[] = this.data[];
return result;
}
// Templated versions of the above functions to handle non-int input
DoubleMatrix reshape(T1, T2)(T1 nr, T2 nc=1) {
return reshape(nr.to!int, nc.to!int);
}
DoubleMatrix setColumns(T)(T nc) {
return setColumns(nc.to!int);
}
DoubleMatrix setRows(T)(T nr) {
return setRows(nr.to!int);
}
// No need for assert/enforce statements inside this method
// Invariant conditions and existing asserts for DoubleMatrix should
// catch all possible invalid data
void unsafeReshape(int newrows, int newcols=1) {
rows = newrows;
cols = newcols;
}
void unsafeSetColumns(int newcols) {
rows = this.length / newcols;
cols = newcols;
}
void unsafeSetRows(int newrows) {
cols = this.length / newrows;
rows = newrows;
}
// Use templates for non-int arguments
void unsafeReshape(T1, T2)(T1 nr, T2 nc=1) {
rows = nr.to!int;
cols = nc.to!int;
}
void unsafeSetColumns(T)(T nc) {
int newcols = nc.to!int;
// Do this calculation first!
rows = this.length / newcols;
cols = newcols;
}
void unsafeSetRows(T)(T nr) {
int newrows = nr.to!int;
cols = this.rows*this.cols / newrows;
rows = newrows;
}
DoubleMatrix unsafeClone() {
DoubleMatrix result;
result.rows = this.rows;
result.cols = this.cols;
result.data = this.data;
return result;
}
}
/* This is used to specify all of the elements in a row or column. */
struct AllElements {}
AllElements _all;
DoubleMatrix dup(DoubleMatrix m) {
auto result = DoubleMatrix(m.rows, m.cols);
result.data[] = m.data[];
return result;
}
DoubleMatrix stack(DoubleMatrix m) {
auto result = DoubleMatrix(m.length);
result.data[] = m.data[];
return result;
}
DoubleMatrix t(DoubleMatrix m) {
auto result = DoubleMatrix(m.cols, m.rows);
int err = gretl_matrix_transpose(result.matptr, m.matptr);
enforce(err == 0, "Taking the transpose of a matrix failed with error code " ~ err.to!string);
return result;
}
DoubleMatrix chol(DoubleMatrix m) {
assert(m.rows == m.cols, "You are trying to compute a Cholesky decomposition of a non-square matrix");
auto result = dup(m);
int err = gretl_matrix_cholesky_decomp(result.matptr);
enforce(err == 0, "Cholesky decomposition failed");
return result;
}
void print(DoubleMatrix m, string msg="") {
writeln(msg);
foreach(row; 0..m.rows) {
foreach(col; 0..m.cols) {
write(m[row, col], " ");
}
writeln("");
}
}
// This struct holds a reference to the data in a matrix.
// It's up to the user to make sure the reference doesn't outlive the underlying matrix.
// They are designed to be short-lived, for convenience, not for actual data storage.
// The user should normally not be using Submatrix types directly
struct Submatrix {
// Original matrix
double * ptr;
int rows;
// The submatrix
int rowOffset;
int colOffset;
int subRows;
int subCols;
alias dup this;
DoubleMatrix dup() {
auto result = DoubleMatrix(subRows, subCols);
foreach(col; 0..subCols) {
foreach(row; 0..subRows) {
result[row, col] = this[row, col];
}
}
return result;
}
this(DoubleMatrix m, int r0, int c0, int r1, int c1) {
ptr = m.ptr;
rows = m.rows;
subRows = r1-r0;
subCols = c1-c0;
rowOffset = r0;
colOffset = c0;
}
this(T1, T2, T3, T4)(DoubleMatrix m, T1 r0, T2 c0, T3 r1, T4 c1) {
this(m, r0.to!int, c0.to!int, r1.to!int, c1.to!int);
}
this(DoubleMatrix m) {
ptr = m.ptr;
rows = m.rows;
subRows = m.rows;
subCols = m.cols;
rowOffset = 0;
colOffset = 0;
}
this(GretlMatrix m, int r0, int c0, int r1, int c1) {
ptr = m.ptr;
rows = m.rows;
subRows = r1-r0;
subCols = c1-c0;
rowOffset = r0;
colOffset = c0;
}
this(T1, T2, T3, T4)(GretlMatrix m, T1 r0, T2 c0, T3 r1, T4 c1) {
this(m, r0.to!int, c0.to!int, r1.to!int, c1.to!int);
}
this(GretlMatrix m) {
ptr = m.ptr;
rows = m.rows;
subRows = m.rows;
subCols = m.cols;
rowOffset = 0;
colOffset = 0;
}
double opIndex(int r, int c) {
assert(r >= 0, "Row index cannot be negative");
assert(c >= 0, "Column index cannot be negative");
assert(r < subRows, "First index on Submatrix has to be less than the number of rows");
assert(c < subCols, "Second index on Submatrix has to be less than the number of columns");
int newr = r+rowOffset;
int newc = c+colOffset;
return ptr[newc*rows + newr];
}
double opIndex(T1, T2)(T1 r, T2 c) {
opIndex(r.to!int, c.to!int);
}
void opIndexAssign(double v, int r, int c) {
assert(r >= 0, "Row index cannot be negative");
assert(c >= 0, "Column index cannot be negative");
assert(r < subRows, "First index on Submatrix has to be less than the number of rows");
assert(c < subCols, "Second index on Submatrix has to be less than the number of columns");
int newr = r+rowOffset;
int newc = c+colOffset;
ptr[newc*rows + newr] = v;
}
void opIndexAssign(T1, T2)(double v, T1 r, T2 c) {
opIndexAssign(v, r.to!int, c.to!int);
}
DoubleMatrix opBinary(string op)(Submatrix sm) {
static if(op == "+") {
return SubmatrixAddition(this, sm);
}
static if(op == "-") {
return SubmatrixSubtraction(this, sm);
}
static if(op == "*") {
return SubmatrixMultiplication(this, sm);
}
static if(op == "/") {
return SubmatrixDivision(this, sm);
}
}
/* It's handy to be able to convert a submatrix that has only one row
* or one column into an array.
*/
double[] array() {
enforce( (subCols == 1) | (subRows == 1), "Cannot convert a submatrix with multiple rows and columns into an array");
double[] result;
if (subCols == 1) {
foreach(row; 0..subRows) {
result ~= this[row,0];
}
} else {
foreach(col; 0..subCols) {
result ~= this[0,col];
}
}
return result;
}
void opAssign(double v) {
foreach(col; 0..subCols) {
foreach(row; 0..subRows) {
this[row, col] = v;
}
}
}
void opAssign(Submatrix m) {
enforce(m.subRows == this.subRows, "Number of rows does not match");
enforce(m.subCols == this.subCols, "Number of columns does not match");
foreach(col; 0..subCols) {
foreach(row; 0..subRows) {
this[row, col] = m[row, col];
}
}
}
//~ void opAssign(GretlMatrix m) {
//~ enforce(m.rows == this.subRows, "Number of rows does not match");
//~ enforce(m.cols == this.subCols, "Number of columns does not match");
//~ foreach(col; 0..m.cols) {
//~ foreach(row; 0..m.rows) {
//~ this[row, col] = m[row, col];
//~ }
//~ }
//~ }
// We have this function defined because there is some overhead to using alias this with a DoubleMatrix.
// No such overhead with an RMatrix.
void opAssign(DoubleMatrix m) {
enforce(m.rows == this.subRows, "Number of rows does not match");
enforce(m.cols == this.subCols, "Number of columns does not match");
foreach(col; 0..m.cols) {
foreach(row; 0..m.rows) {
this[row, col] = m[row, col];
}
}
}
double[] opSlice(int i0, int i1) {
enforce( (subCols == 1) | (subRows == 1), "Can only slice a submatrix with one row or one column. Other slicing of a Submatrix is not supported at this time.");
double[] result;
if (subCols == 1) {
foreach(row; i0..i1) {
result ~= this[row,0];
}
} else {
foreach(col; i0..i1) {
result ~= this[0,col];
}
}
return result;
}
}
DoubleMatrix SubmatrixAddition(Submatrix x, Submatrix y) {
assert(x.subRows == y.subRows, "Rows for Submatrix addition do not match");
assert(x.subCols == y.subCols, "Rows for Submatrix addition do not match");
auto result = DoubleMatrix(x.subRows, x.subCols);
foreach(c; 0..result.cols) {
foreach(r; 0..result.rows) {
result[r, c] = x[r, c]+y[r, c];
}
}
return result;
}
struct All {};
All _;
struct Element {
double val;
int row;
int col;
this(double _val, int _row, int _col) {
val = _val;
row = _row;
col = _col;
}
this(double _val, int[2] ind) {
val = _val;
row = ind[0];
col = ind[1];
}
}
alias Elements = Element[];
struct BelowDiagonal {
DoubleMatrix m;
alias elements this;
invariant {
assert(m.rows == m.cols, "BelowDiagonal is only defined for square matrices. "
~ "If you want the main diagonal, use a Submatrix.");
}
Elements elements() {
Elements result;
foreach(cc; 0..m.cols) {
foreach(rr; cc+1..m.rows) {
result ~= Element(m[rr, cc], rr, cc);
}
}
return result;
}
DoubleMatrix mat() {
auto result = DoubleMatrix(m.rows, m.cols);
foreach(col; 0..m.cols) {
foreach(row; 0..m.rows) {
if (row <= col) {
result[row, col] = 0.0;
} else {
result[row, col] = m[row, col];
}
}
}
return result;
}
double[] array() {
double[] result;
int[2] ind = [1, 0];
foreach(ii; 0..this.length) {
result ~= this[ind];
ind = nextIndex(ind);
}
return result;
}
// Don't try to call this. It's confusing to index this struct!
// m[[2,4]]
private double opIndex(int[2] ind) {
return m[ind];
}
private double opIndex(int row, int col) {
return m[row, col];
}
// Don't try to call this either.
private void opIndexAssign(double val, int[2] ind) {
m[ind] = val;
}
private void opIndexAssign(double val, int row, int col) {
m[row, col] = val;
}
void opAssign(Elements es) {
assert(this.length == es.length, "Number of elements doesn't match in assignment involving BelowDiagonal");
foreach(e; es) {
m[e.row, e.col] = e.val;
}
}
void opAssign(BelowDiagonal bd) {
assert(this.length == bd.length, "Cannot do BelowDiagonal assignment unless dimensions match");
int[2] ind = [1,0];
foreach(ii; 0..bd.length) {
m[ind] = bd[ind];
ind = nextIndex(ind);
}
}
void opAssign(AboveDiagonal ad) {
Elements es = ad.elements;
writeln(es);
foreach(e; es) {
m[e.col, e.row] = ad[e.row, e.col];
}
}
void opAssign(double a) {}
// Since we know the previous element's index, use that information
// to calculate the next index
int[2] nextIndex(int[2] ind) {
int rowNumber = ind[0];
int colNumber = ind[1];
if (rowNumber > m.rows-2) {
return [colNumber+2, colNumber+1];
} else {
return [rowNumber+1, colNumber];
}
}
// For filling with random elements
void fill(double[] v) {
assert(this.length == v.length, "Number of elements doesn't match in assignment involving BelowDiagonal");
int[2] ind = [1,0];
foreach(ii; 0..v.length) {
m[ind] = v[ii];
ind = nextIndex(ind);
}
}
int length() {
return (m.rows^^2 - m.rows)/2;
}
}
// Should probably do something about the code duplication with BelowDiagonal
// Not going to worry about it right now
struct AboveDiagonal {
DoubleMatrix m;
alias elements this;
invariant {
assert(m.rows == m.cols, "AboveDiagonal is only defined for square matrices. "
~ "If you want the main diagonal, use a Submatrix.");
}
Elements elements() {
Elements result;
int[2] ind = [0, 1];
foreach(ii; 0..this.length) {
writeln(ind);
result ~= Element(m[ind], ind);
ind = nextIndex(ind);
}
return result;
}
// Since we know the previous element's index, use that information
// to calculate the next index
int[2] nextIndex(int[2] ind) {
int rowNumber = ind[0];
int colNumber = ind[1];
if (rowNumber < colNumber-1) {
return [rowNumber+1, colNumber];
} else {
return [0, colNumber+1];
}
}
DoubleMatrix mat() {
auto result = DoubleMatrix(m.rows, m.cols);
foreach(col; 0..m.cols) {
foreach(row; 0..m.rows) {
if (row < col) {
result[row, col] = m[row, col];
} else {
result[row, col] = 0.0;
}
}
}
return result;
}
double[] array() {
double[] result;
int[2] ind = [0, 1];
foreach(ii; 0..this.length) {
result ~= this[ind];
ind = nextIndex(ind);
}
return result;
}
// Don't try to call this. It's confusing to index this struct!
private double opIndex(int[2] ind) {
return m[ind];
}
private double opIndex(int row, int col) {
return m[row, col];
}
// Don't try to call this either.
private void opIndexAssign(double val, int[2] ind) {
m[ind] = val;
}
private void opIndexAssign(double val, int row, int col) {
m[row, col] = val;
}
void opAssign(Elements es) {
assert(this.length == es.length, "Number of elements doesn't match in assignment involving AboveDiagonal");
foreach(e; es) {
m[e.row, e.col] = e.val;
}
}
void opAssign(AboveDiagonal ad) {
assert(this.length == ad.length, "Cannot do AboveDiagonal assignment unless dimensions match");
int[2] ind = [0,1];
foreach(ii; 0..ad.length) {
m[ind] = ad[ind];
ind = nextIndex(ind);
}
}
void opAssign(BelowDiagonal bd) {
Elements es = bd.elements;
foreach(e; es) {
m[e.col, e.row] = bd[e.row, e.col];
}
}
void opAssign(double a) {}
// For filling with random elements
void fill(double[] v) {
assert(this.length == v.length, "Number of elements doesn't match in assignment involving AboveDiagonal");
int[2] ind = [0,1];
foreach(ii; 0..v.length) {
m[ind] = v[ii];
ind = nextIndex(ind);
}
}
int length() {
return (m.rows^^2 - m.rows)/2;
}
}
struct Diagonal {
DoubleMatrix m;
alias array this;
invariant {
assert(m.rows == m.cols, "Diagonal is only defined for square matrices. "
~ "If you want the main diagonal, use a Submatrix.");
}
double[] array() {
double[] result;
foreach(ii; 0..m.rows) {
result ~= this[ii];
}
return result;
}
DoubleMatrix mat() {
auto result = DoubleMatrix(m.rows, m.cols);
foreach(ind; ByIndex(m)) {
if (ind[0] == ind[1]) {
result[ind] = m[ind];
} else {
result[ind] = 0.0;
}
}
return result;
}
double opIndex(int ii) {
assert(ii >= 0, "Index on Diagonal cannot be negative");
assert(ii < m.cols, "Index on Diagonal exceeds dimension");
return m[ii, ii];
}
double opIndex(int[2] ind) {
assert(ind[0] == ind[1], "Index values not the same for an element on the diagonal");
return opIndex(ind[0]);
}
void opIndexAssign(double val, int ii) {
assert(ii >= 0, "Index on Diagonal cannot be negative");
assert(ii < m.cols, "Index on Diagonal exceeds dimension");
m[ii, ii] = val;
}
void opIndexAssign(double val, int[2] ind) {
assert(ind[0] == ind[1], "Index values not the same for an element on the diagonal");
return opIndexAssign(val, ind[0]);
}
void opAssign(Diagonal d) {
assert(this.length == d.length, "Dimensions for Diagonal assignment don't match");
foreach(ii; 0..d.length) {
this[ii] = d[ii];
}
}
void opAssign(double[] v) {
assert(this.length == v.length, "Dimensions for Diagonal assignment don't match");
foreach(ii; 0..this.length) {
this[ii] = v[ii];
}
}
void opAssign(double a) {
foreach(ii; 0..this.length) {
this[ii] = a;
}
}
int length() {
return m.cols;
}
}
struct ByIndex {
DoubleMatrix m;
int rr = 0;
int cc = 0;
bool empty() {
return cc >= m.cols;
}
int[2] front() {
return [rr, cc];
}
void popFront() {
if (rr == m.rows-1) {
rr = 0;
cc += 1;
} else {
rr += 1;
}
}
}
struct ByElement {
DoubleMatrix m;
int rr = 0;
int cc = 0;
bool empty() {
return cc >= m.cols;
}
Element front() {
return Element(m[rr, cc], rr, cc);
}
void popFront() {
if (rr == m.rows-1) {
rr = 0;
cc += 1;
} else {
rr += 1;
}
}
}
struct Row {
/* lastColumn is the last column of m included in this row.
* It can be less that m.cols.
* colOffset is what you use to index the first element of the row. */
DoubleMatrix m;
int row;
private int colOffset = 0;
private int lastColumn;
/* Only one way to directly create a Row, using Row(m, 4). Can also
* indirectly create a Row using multidimensional slicing of a matrix. */
this(DoubleMatrix _m, int _row) {
assert(_row >= 0, "Cannot have a negative row index in Row struct");
assert(_row < _m.rows, "Attempting to create a Row with row number that exceeds matrix dimensions");
m = _m;
row = _row;
lastColumn = _m.cols;
}
this(DoubleMatrix _m, int _row, int _colOffset, int _lastColumn) {
assert(_row >= 0, "Cannot have a negative row index in Row struct");
assert(_row < _m.rows, "Attempting to create a Row with row number that exceeds matrix dimensions");
m = _m;
row = _row;
colOffset = _colOffset;
lastColumn = _lastColumn;
}
/* Use a length function because it's too easy to forget to update
* length if it's treated as data. This always gets it right. */
int length() {
return lastColumn - colOffset;
}
// DoubleMatrix mat() {}
// alias mat this
double[] array() {
double[] result;
foreach(val; this) {
result ~= val;
}
return result;
}
DoubleMatrix dup() {
auto result = DoubleMatrix(1, this.length);
Row(result, 0) = this;
return result;
}
DoubleMatrix rowmat() {
return this.dup;
}
//~ DoubleMatrix colmat() {
//~ auto result = DoubleMatrix(this.length, 1);
//~ Col(result, 0) = this;
//~ return result;
//~ }
/* Define the index operators here and then use them everywhere else
* in order to avoid bugs. Avoid directly indexing mat as much as possible. */
double opIndex(int ii) {
assert(ii >= 0, "Index on Row struct can't be negative");
assert(ii <= this.length, "Index on Row struct out of bounds");
return m[row, ii+colOffset];
}
void opIndexAssign(double val, int ii) {
assert(ii >= 0, "Index on Row struct can't be negative");