-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.d
888 lines (750 loc) · 20.4 KB
/
matrix.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
module gretl.matrix;
public import gretl.matfunctions, gretl.vector;
import gretl.base;
import std.conv, std.stdio;
import core.stdc.stdlib;
version(r) {
import embedr.r;
}
version(standalone) {
import std.exception;
}
version(inline) {
private alias enforce = embedr.r.assertR;
}
private struct matrix_info {
int t1;
int t2;
char **colnames;
char **rownames;
}
struct GretlMatrix {
int rows;
int cols;
double * ptr;
matrix_info * info;
GretlMatrix * matptr() {
return &this;
}
// The struct that wraps the underlying array should be handled by the D garbage collector
// gretl_matrix_alloc uses malloc and leaves management of the struct itself (not just the
// array) to the user. Therefore we copy the struct data into a D struct and destroy the
// malloced one.
this(int r, int c) {
GretlMatrix * m = gretl_matrix_alloc(r, c);
rows = m.rows;
cols = m.cols;
ptr = m.ptr;
info = m.info;
core.stdc.stdlib.free(m);
}
// Does not include col1
this(GretlMatrix gm, int col0, int col1) {
rows = gm.rows;
cols = col1-col0;
ptr = &(gm.ptr[rows*col0]);
}
this(double[] v, int c=1) {
rows = to!int(v.length)/c;
cols = c;
ptr = v.ptr;
}
// ptr and info need to be freed before destroying the struct
// otherwise you've got a memory leak
void free() {
core.stdc.stdlib.free(ptr);
core.stdc.stdlib.free(info);
}
double opIndex(int r, int c) {
return ptr[c*rows+r];
}
// multidimensional slicing
// requires the Submatrix struct
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]);
}
Submatrix opIndex(int row, int[2] cc) {
return Submatrix(this, row, cc[0], row, cc[1]);
}
Submatrix opIndex(int[2] rr, int col) {
return Submatrix(this, rr[0], col, rr[1], col);
}
Submatrix opIndex(int row, AllElements cc) {
return opIndex([row, row+1], [0, cols]);
}
Submatrix opIndex(int[2] rr, AllElements cc) {
return opIndex(rr, [0, cols]);
}
Submatrix opIndex(AllElements rr, int col) {
return opIndex([0, rows], [col, col+1]);
}
Submatrix opIndex(AllElements rr, int[2] cc) {
return opIndex([0, rows], cc);
}
void opIndexAssign(double v, int r, int c) {
ptr[c*rows+r] = v;
}
void opIndexAssign(double v, int[2] rr, int[2] cc) {
foreach(col; cc[0]..cc[1]) {
foreach(row; rr[0]..rr[1]) {
this[row, col] = v;
}
}
}
void opIndexAssign(double v, int row, int[2] cc) {
opIndexAssign(v, [row, row+1], cc);
}
void opIndexAssign(double v, int[2] rr, int col) {
opIndexAssign(v, rr, [col, col+1]);
}
void opIndexAssign(double v, AllElements rr, int[2] cc) {
opIndexAssign(v, [0, this.rows], cc);
}
void opIndexAssign(double v, int[2] rr, AllElements cc) {
opIndexAssign(v, rr, [0, this.cols]);
}
void opIndexAssign(double v, AllElements rr, int col) {
opIndexAssign(v, [0, this.rows], [col, col+1]);
}
void opIndexAssign(double v, int row, AllElements cc) {
opIndexAssign(v, [row, row+1], [0, this.cols]);
}
// Assign a matrix
void opIndexAssign(GretlMatrix m, int[2] rr, int[2] cc) {
enforce(m.rows == rr[1]-rr[0], "Rows do not match");
enforce(m.cols == cc[1]-cc[0], "Columns do not match");
foreach(col; cc[0]..cc[1]) {
foreach(row; rr[0]..rr[1]) {
this[row, col] = m[row-rr[0], col-cc[0]];
}
}
}
void opIndexAssign(GretlMatrix m, int row, int[2] cc) {
opIndexAssign(m, [row, row+1], cc);
}
void opIndexAssign(GretlMatrix m, int[2] rr, int col) {
opIndexAssign(m, rr, [col, col+1]);
}
void opIndexAssign(GretlMatrix m, AllElements rr, int[2] cc) {
opIndexAssign(m, [0, this.rows], cc);
}
void opIndexAssign(GretlMatrix m, int[2] rr, AllElements cc) {
opIndexAssign(m, rr, [0, this.cols]);
}
void opIndexAssign(GretlMatrix m, AllElements rr, int col) {
opIndexAssign(m, [0, this.rows], [col, col+1]);
}
void opIndexAssign(GretlMatrix m, int row, AllElements cc) {
opIndexAssign(m, [row, row+1], [0, this.cols]);
}
// Assign a Submatrix
void opIndexAssign(Submatrix m, int[2] rr, int[2] cc) {
enforce(m.subRows == rr[1]-rr[0], "Rows do not match");
enforce(m.subCols == cc[1]-cc[0], "Columns do not match");
foreach(col; cc[0]..cc[1]) {
foreach(row; rr[0]..rr[1]) {
this[row, col] = m[row-rr[0], col-cc[0]];
}
}
}
void opIndexAssign(Submatrix m, int row, int[2] cc) {
opIndexAssign(m, [row, row+1], cc);
}
void opIndexAssign(Submatrix m, int[2] rr, int col) {
opIndexAssign(m, rr, [col, col+1]);
}
void opIndexAssign(Submatrix m, AllElements rr, int[2] cc) {
opIndexAssign(m, [0, this.rows], cc);
}
void opIndexAssign(Submatrix m, int[2] rr, AllElements cc) {
opIndexAssign(m, rr, [0, this.cols]);
}
void opIndexAssign(Submatrix m, AllElements rr, int col) {
opIndexAssign(m, [0, this.rows], [col, col+1]);
}
void opIndexAssign(Submatrix m, int row, AllElements cc) {
opIndexAssign(m, [row, row+1], [0, this.cols]);
}
// This copies, which is the expected behavior.
void opAssign(GretlMatrix m) {
enforce(this.rows == m.rows, "Number of rows is different");
enforce(this.cols == m.cols, "Number of columns is different");
foreach(ii; 0..rows*cols) {
ptr[ii] = m.ptr[ii];
}
}
// Allows constructing a matrix by sending an array of rows
void opAssign(double[][] m) {
enforce(this.rows == m.length, "Number of rows is different");
foreach(row, elements; m) {
enforce(this.cols == elements.length, "Number of columns is different");
foreach(col, val; elements) {
this[row.to!int, col.to!int] = val;
}
}
}
// fills by column
void opAssign(double[] v) {
enforce(this.rows*this.cols == to!int(v.length), "double[] has different number of elements from matrix");
foreach(ii; 0..rows*cols) {
ptr[ii] = v[ii];
}
}
version(r) {
void opAssign(RMatrix m) {
enforce(m.rows == this.rows, "Number of rows is different");
enforce(m.cols == this.cols, "Number of columns is different");
foreach(ii; 0..m.rows*m.cols) {
ptr[ii] = m.ptr[ii];
}
}
}
void opAssign(DoubleMatrix m) {
enforce(m.rows == this.rows, "Number of rows is different");
enforce(m.cols == this.cols, "Number of columns is different");
foreach(ii; 0..m.rows*m.cols) {
ptr[ii] = m.data[ii];
}
}
void opAssign(double a) {
ptr[0..this.rows*this.cols] = a;
}
// Return a DoubleMatrix because returning a GretlMatrix would mean the user
// has to manage the memory, and these functions would be very hard to use correctly.
// How would you do x*y + z*w without some form of GC?
DoubleMatrix opBinary(string op)(double a) {
static if(op == "+") {
return matrixAddition(this, a);
}
static if(op == "-") {
return matrixSubtraction(this, a);
}
static if(op == "*") {
return matrixMultiplication(this, a);
}
static if(op == "/") {
return matrixDivision(this, a);
}
}
DoubleMatrix opBinaryRight(string op)(double a) {
static if(op == "+") {
return matrixAddition(this, a);
}
static if(op == "-") {
return matrixSubtraction(a, this);
}
static if(op == "*") {
return matrixMultiplication(this, a);
}
static if(op == "/") {
return matrixDivision(a, this);
}
}
DoubleMatrix opBinary(string op)(GretlMatrix m) {
static if(op == "+") {
return matrixAddition(this, m);
}
static if(op == "-") {
return matrixSubtraction(this, m);
}
static if(op == "*") {
return matrixMultiplication(this, m);
}
}
}
struct DoubleMatrix {
double[] data;
int rows;
int cols;
private GretlMatrix temp;
alias mat this;
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) {
data = new double[r*c];
rows = r;
cols = c;
}
version(r) {
this(RMatrix m) {
data = new double[m.rows*m.cols];
rows = m.rows;
cols = m.cols;
foreach(ii; 0..m.rows*m.cols) {
data[ii] = m.ptr[ii];
}
}
}
this(double[][] m) {
data = new double[m.length*m[0].length];
rows = to!int(m.length);
cols = to!int(m[0].length);
foreach(row, vals; m) {
foreach(col; 0..cols) {
data[col*rows+row.to!int] = vals[col];
}
}
}
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[col*rows+row] = m.ptr[col*rows+row];
}
}
}
this(double[] v) {
data = v;
rows = to!int(v.length);
cols = 1;
}
version(r) {
RMatrix opCast(T: RMatrix)() {
auto result = RMatrix(rows, cols);
result.mat = this.mat;
return result;
}
}
double opIndex(int r, int c) {
enforce(r < this.rows, "First index exceeds the number of rows");
enforce(c < this.cols, "Second index exceed the number of columns");
return data[c*this.rows + r];
}
// 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]);
}
Submatrix opIndex(int row, int[2] cc) {
return opIndex([row, row+1], cc);
}
Submatrix opIndex(int[2] rr, int col) {
return opIndex(rr, [col, col+1]);
}
Submatrix opIndex(int row, AllElements cc) {
return opIndex([row, row+1], [0, cols]);
}
Submatrix opIndex(int[2] rr, AllElements cc) {
return opIndex(rr, [0, cols]);
}
Submatrix opIndex(AllElements rr, int col) {
return opIndex([0, rows], [col, col+1]);
}
Submatrix opIndex(AllElements rr, int[2] cc) {
return opIndex([0, rows], cc);
}
// Assign a double to a submatrix
void opIndexAssign(double v, int[2] rr, int[2] cc) {
foreach(col; cc[0]..cc[1]) {
foreach(row; rr[0]..rr[1]) {
this[row, col] = v;
}
}
}
void opIndexAssign(double v, int r, int c) {
enforce(r < this.rows, "Row dimension out of bounds");
enforce(c < this.cols, "Column dimension out of bounds");
ptr[c*rows+r] = v;
}
void opIndexAssign(double v, int row, int[2] cc) {
opIndexAssign(v, [row, row+1], cc);
}
void opIndexAssign(double v, int[2] rr, int col) {
opIndexAssign(v, rr, [col, col+1]);
}
void opIndexAssign(double v, AllElements rr, int[2] cc) {
opIndexAssign(v, [0, this.rows], cc);
}
void opIndexAssign(double v, int[2] rr, AllElements cc) {
opIndexAssign(v, rr, [0, this.cols]);
}
void opIndexAssign(double v, AllElements rr, int col) {
opIndexAssign(v, [0, this.rows], [col, col+1]);
}
void opIndexAssign(double v, int row, AllElements cc) {
opIndexAssign(v, [row, row+1], [0, this.cols]);
}
// Assign a matrix
void opIndexAssign(GretlMatrix m, int[2] rr, int[2] cc) {
enforce(m.rows == rr[1]-rr[0], "Rows do not match");
enforce(m.cols == cc[1]-cc[0], "Columns do not match");
foreach(col; cc[0]..cc[1]) {
foreach(row; rr[0]..rr[1]) {
this[row, col] = m[row-rr[0], col-cc[0]];
}
}
}
void opIndexAssign(GretlMatrix m, int row, int[2] cc) {
opIndexAssign(m, [row, row+1], cc);
}
void opIndexAssign(GretlMatrix m, int[2] rr, int col) {
opIndexAssign(m, rr, [col, col+1]);
}
void opIndexAssign(GretlMatrix m, AllElements rr, int[2] cc) {
opIndexAssign(m, [0, this.rows], cc);
}
void opIndexAssign(GretlMatrix m, int[2] rr, AllElements cc) {
opIndexAssign(m, rr, [0, this.cols]);
}
void opIndexAssign(GretlMatrix m, AllElements rr, int col) {
opIndexAssign(m, [0, this.rows], [col, col+1]);
}
void opIndexAssign(GretlMatrix m, int row, AllElements cc) {
opIndexAssign(m, [row, row+1], [0, this.cols]);
}
// Assign a Submatrix
void opIndexAssign(Submatrix m, int[2] rr, int[2] cc) {
enforce(m.subRows == rr[1]-rr[0], "Rows do not match");
enforce(m.subCols == cc[1]-cc[0], "Columns do not match");
foreach(col; cc[0]..cc[1]) {
foreach(row; rr[0]..rr[1]) {
this[row, col] = m[row-rr[0], col-cc[0]];
}
}
}
void opIndexAssign(Submatrix m, int row, int[2] cc) {
opIndexAssign(m, [row, row+1], cc);
}
void opIndexAssign(Submatrix m, int[2] rr, int col) {
opIndexAssign(m, rr, [col, col+1]);
}
void opIndexAssign(Submatrix m, AllElements rr, int[2] cc) {
opIndexAssign(m, [0, this.rows], cc);
}
void opIndexAssign(Submatrix m, int[2] rr, AllElements cc) {
opIndexAssign(m, rr, [0, this.cols]);
}
void opIndexAssign(Submatrix m, AllElements rr, int col) {
opIndexAssign(m, [0, this.rows], [col, col+1]);
}
void opIndexAssign(Submatrix m, int row, AllElements cc) {
opIndexAssign(m, [row, row+1], [0, this.cols]);
}
// This copies, which is the expected behavior.
void opAssign(GretlMatrix m) {
enforce(this.rows == m.rows, "Number of rows is different");
enforce(this.cols == m.cols, "Number of columns is different");
foreach(ii; 0..rows*cols) {
ptr[ii] = m.ptr[ii];
}
}
// Allows constructing a matrix by sending an array of rows
void opAssign(double[][] m) {
enforce(this.rows == m.length, "Number of rows is different");
foreach(row, elements; m) {
enforce(this.cols == elements.length, "Number of columns is different");
foreach(col, val; elements) {
this[row.to!int, col.to!int] = val;
}
}
}
// fills by column
void opAssign(double[] v) {
enforce(this.rows*this.cols == to!int(v.length), "double[] has different number of elements from matrix");
foreach(ii; 0..rows*cols) {
ptr[ii] = v[ii];
}
}
version(r) {
void opAssign(RMatrix m) {
enforce(m.rows == this.rows, "Number of rows is different");
enforce(m.cols == this.cols, "Number of columns is different");
foreach(ii; 0..m.rows*m.cols) {
data[ii] = m.ptr[ii];
}
}
}
void opAssign(double a) {
data[0..this.rows*this.cols] = a;
}
void opAssign(DoubleMatrix m) {
data = m.data;
rows = m.rows;
cols = m.cols;
}
}
// Row, Col, Submatrix do not do any reference counting.
// Up to you 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.
struct Submatrix {
// Original matrix
double * ptr;
int rows;
// The submatrix
int rowOffset;
int colOffset;
int subRows;
int subCols;
DoubleMatrix dup() {
auto result = DoubleMatrix(subRows, subCols);
foreach(col; 0..subCols) {
foreach(row; 0..subRows) {
result[row, col] = this[row, col];
}
}
return result;
}
alias dup this;
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(GretlMatrix m) {
ptr = m.ptr;
rows = m.rows;
subRows = m.rows;
subCols = m.cols;
rowOffset = 0;
colOffset = 0;
}
/* 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;
}
double opIndex(int r, int c) {
enforce(r < subRows, "First index on Submatrix has to be less than the number of rows");
enforce(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];
}
void opIndexAssign(double v, int r, int c) {
enforce(r < subRows, "First index on Submatrix has to be less than the number of rows");
enforce(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 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;
}
version(r) {
RMatrix rmat() {
auto result = RMatrix(subRows, subCols);
foreach(col; 0..subCols) {
foreach(row; 0..subRows) {
result[row, col] = this[row, col];
}
}
return result;
}
}
}
struct Row {
GretlMatrix mat;
int row;
double * data;
int length;
this(GretlMatrix m, int r) {
mat = m;
row = r;
data = &m.ptr[r];
length = m.cols;
}
double opIndex(int c) {
enforce(c < length, "Row index out of bounds");
return data[c*mat.rows];
}
void opIndexAssign(double val, int ii) {
mat[row, ii] = val;
}
void opAssign(T)(T v) {
enforce(this.length == v.length, "Attempting to copy into Row an object with the wrong number of elements");
foreach(ii; 0..this.length) {
mat[row, ii] = v[ii];
}
}
void opAssign(double x) {
foreach(ii; 0..this.length) {
mat[row, ii] = x;
}
}
double[] opSlice(int i0, int i1) {
double[] result;
foreach(col; i0..i1) {
result ~= this[col];
}
return result;
}
bool empty() { return length == 0; }
double front() { return data[0]; }
void popFront() {
data = &data[mat.rows];
length -= 1;
}
}
struct Col {
GretlMatrix mat;
int col;
double * data;
int length;
this(GretlMatrix m, int c) {
mat = m;
col = c;
data = &m.ptr[m.rows*c];
length = m.rows;
}
double opIndex(int r) {
enforce(r < length, "Column index out of bounds");
return data[r];
}
void opIndexAssign(double val, int ii) {
mat[ii, col] = val;
}
void opAssign(T)(T v) {
enforce(this.length == v.length, "Attempting to copy into Column an object with the wrong number of elements");
foreach(ii; 0..to!int(this.length)) {
mat[ii, col] = v[ii];
}
}
void opAssign(double x) {
foreach(ii; 0..this.length) {
mat[ii, col] = x;
}
}
void opAssign(GretlMatrix m) {
enforce(length == m.rows, "Wrong number of elements to copy into Column");
enforce(m.cols == 1, "Cannot copy into a Column from a matrix with more than one column");
foreach(ii; 0..this.length) {
mat[ii, col] = m[ii, 0];
}
}
double[] opSlice(int i0, int i1) {
double[] result;
foreach(row; i0..i1) {
result ~= this[row];
}
return result;
}
bool empty() { return length == 0; }
double front() { return data[0]; }
void popFront() {
data = &data[1];
length -= 1;
}
}
struct ByRow {
GretlMatrix mat;
int length;
private int rowno = 0;
this(GretlMatrix m) {
mat = m;
length = m.rows;
}
bool empty() {
return length == 0;
}
Row front() {
return Row(mat, rowno);
}
void popFront() {
rowno += 1;
length -= 1;
}
}
struct ByColumn {
GretlMatrix mat;
int colno;
int length;
this(GretlMatrix m) {
writeln("start of constructor");
mat = m;
colno = 0;
length = m.cols;
writeln("exiting constructor");
}
bool empty() {
return length == 0;
}
Col front() {
return Col(mat, colno);
}
void popFront() {
colno += 1;
length -= 1;
}
}