-
Notifications
You must be signed in to change notification settings - Fork 4
/
matrix.d
executable file
·1355 lines (1116 loc) · 32.3 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
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
/**
* This module defines a matrix data structure and various operations
* on this data structure. All operations are @safe pure nothrow,
* so they can be used in any such function, and the results of any
* operation can be implicitly converted to an immutable type.
*/
module dstruct.matrix;
import std.traits;
// A private implementation of matrix multiplication for use in types.
private auto matrixMultiply(ResultType, T, U)
(ref ResultType result, ref const(T) left, ref const(U) right) {
foreach(row; 0 .. result.rowCount) {
foreach(column; 0 .. result.columnCount) {
Unqual!(typeof(result[0, 0])) value = left[row, 0] * right[0, column];
foreach(pivot; 1 .. left.columnCount) {
value += left[row, pivot] * right[pivot, column];
}
result[row, column] = value;
}
}
return result;
}
/**
* A matrix type. This is a 2D array of a guaranteed uniform size.
*/
struct Matrix(Number) if(isNumeric!Number) {
private:
Number[] _data;
size_t _rowCount;
size_t _columnCount;
public:
/**
* Create an matrix from an array of data.
*
* Params:
* rowCount = The number of rows for the matrix.
* columnCount = The number of columns for the matrix.
* data = The array of data for the matrix.
*/
@safe pure nothrow
this(size_t rowCount, size_t columnCount, immutable(Number[]) data) immutable {
_data = data;
_rowCount = rowCount;
_columnCount = columnCount;
}
// Copy-paste the constructors, because inout doesn't work with literals.
/// ditto
@safe pure nothrow
this(size_t rowCount, size_t columnCount, const(Number[]) data) const {
_data = data;
_rowCount = rowCount;
_columnCount = columnCount;
}
/// ditto
@safe pure nothrow
this(size_t rowCount, size_t columnCount, Number[] data) {
_data = data;
_rowCount = rowCount;
_columnCount = columnCount;
}
/**
* Create a matrix of a given size.
*
* Params:
* rowCount = The number of rows for the matrix.
* columnCount = The number of columns for the matrix.
*/
@safe pure nothrow
this(size_t rowCount, size_t columnCount) {
if (rowCount == 0 || columnCount == 0) {
return;
}
_data = new Number[](rowCount * columnCount);
_rowCount = rowCount;
_columnCount = columnCount;
}
/**
* Returns: A new duplicate of this matrix.
*/
@safe pure nothrow
Matrix!Number dup() const {
Matrix!Number mat;
// We can't .dup in a nothrow function, but we can do this...
mat._data = new Number[](_rowCount * _columnCount);
mat._data[] = _data[];
mat._rowCount = _rowCount;
mat._columnCount = _columnCount;
return mat;
}
/**
* Returns: A new immutable duplicate of this matrix.
*/
@safe pure nothrow
immutable(Matrix!Number) idup() const {
return dup();
}
/**
* When calling .idup on an already immutable matrix, the reference
* to the same immutable matrix is returned. It should be safe to
* share the immutable memory in this manner.
*
* Returns: A reference to this immutable matrix.
*/
@safe pure nothrow
immutable(Matrix!Number) idup() immutable {
// There's no need to copy immutable to immutable, share it!
return this;
}
unittest {
immutable m = immutable Matrix!int(1, 1);
// Make sure this doesn't actually duplicate.
assert(m._data is m._data);
auto o = Matrix!int(1, 1);
// Make sure this still does.
assert(o.idup._data !is o._data);
}
/// Returns: True if the matrix is empty.
@safe pure nothrow
@property bool empty() const {
return _data.length == 0;
}
/// Returns: The number of rows in this matrix.
@safe pure nothrow
@property size_t rowCount() const {
return _rowCount;
}
/// Returns: The number of columns in this matrix.
@safe pure nothrow
@property size_t columnCount() const {
return _columnCount;
}
/// Returns: true if the matrix is a square matrix.
@safe pure nothrow
@property bool isSquare() const {
return _rowCount == _columnCount;
}
/**
* Slice out a row from the matrix. Modifying this
* slice will modify the matrix, unless it is copied.
*
* Params:
* row = A row index.
*
* Returns: A slice of the row of the matrix.
*/
@trusted pure nothrow
inout(Number[]) opIndex(size_t row) inout
in {
assert(row <= rowCount, "row out of bounds!");
} body {
size_t offset = row * _columnCount;
return _data[offset .. offset + _columnCount];
}
/**
* Params:
* row = A row index.
* column = A column index.
*
* Returns: A value from the matrix
*/
@safe pure nothrow
ref inout(Number) opIndex(size_t row, size_t column) inout
in {
assert(column <= columnCount, "column out of bounds!");
} body {
return _data[row * _columnCount + column];
}
/**
* Overload for foreach(rowIndex, columnIndex, value; matrix) {}
*/
@trusted
int opApply(int delegate(ref size_t, ref size_t, ref Number) dg) {
int result = 0;
matrixLoop: foreach(row; 0 .. rowCount) {
size_t offset = row * columnCount;
foreach(column; 0 .. columnCount) {
result = dg(row, column, _data[offset + column]);
if (result) {
break matrixLoop;
}
}
}
return result;
}
/**
* Modify this matrix, adding/subtracting values from another matrix.
*
* Example:
* ---
* matrix += other_matrix;
* matrix -= yet_another_matrix;
* ---
*
* Params:
* other = Another matrix with an implicitly convertible numeric type.
*/
@safe pure nothrow
void opOpAssign(string op, OtherNumber)
(const Matrix!OtherNumber other)
if((op == "+" || op == "-") && is(OtherNumber : Number))
in {
assert(this.rowCount == other.rowCount);
assert(this.columnCount == other.columnCount);
} body {
foreach(i; 0.._data.length) {
mixin(`_data[i]` ~ op ~ `= other._data[i];`);
}
}
/**
* Add or subtract two matrices, yielding a new matrix.
*
* Params:
* other = The other matrix.
*
* Returns: A new matrix.
*/
@safe pure nothrow
Matrix!Number opBinary(string op, OtherNumber)
(ref const Matrix!OtherNumber other) const
if((op == "+" || op == "-") && is(OtherNumber : Number)) in {
assert(this.rowCount == other.rowCount);
assert(this.columnCount == other.columnCount);
} out(val) {
assert(this.rowCount == val.rowCount);
assert(this.rowCount == val.rowCount);
} body {
// Copy this matrix.
auto result = this.dup;
mixin(`result ` ~ op ~ `= other;`);
return result;
}
/// ditto
@safe pure nothrow
Matrix!Number opBinary(string op, OtherNumber)
(const Matrix!OtherNumber other) const
if((op == "+" || op == "-") && is(OtherNumber : Number)) {
opBinary!(op, OtherNumber)(other);
}
/**
* Multiply two matrices.
*
* Given a matrix of size (m, n) and a matrix of size (o, p).
* This operation can only work if n == o.
* The resulting matrix will be size (m, p).
*
* Params:
* other = Another matrix
*
* Returns: The product of two matrices.
*/
@safe pure nothrow
Matrix!Number opBinary(string op, OtherNumber)
(ref const Matrix!OtherNumber other) const
if((op == "*") && is(OtherNumber : Number)) in {
assert(this.columnCount == other.rowCount);
} out(val) {
assert(val.rowCount == this.rowCount);
assert(val.columnCount == other.columnCount);
} body {
auto result = Matrix!Number(this.rowCount, other.columnCount);
matrixMultiply(result, this, other);
return result;
}
/// ditto
@safe pure nothrow
Matrix!Number opBinary(string op, OtherNumber)
(const Matrix!OtherNumber other) const
if((op == "*") && is(OtherNumber : Number)) in {
opBinary!(op, OtherNumber)(other);
}
/// ditto
@safe pure nothrow
Matrix!OtherNumber opBinary(string op, OtherNumber)
(ref const Matrix!OtherNumber other) const
if((op == "*") && !is(Number == OtherNumber) && is(Number : OtherNumber)) in {
assert(this.columnCount == other.rowCount);
} out(val) {
assert(val.rowCount == this.rowCount);
assert(val.columnCount == other.columnCount);
} body {
auto result = Matrix!OtherNumber(this.rowCount, other.columnCount);
matrixMultiply(result, this, other);
return result;
}
/// ditto
@safe pure nothrow
Matrix!OtherNumber opBinary(string op, OtherNumber)
(const Matrix!OtherNumber other) const
if((op == "*") && !is(Number == OtherNumber) && is(Number : OtherNumber)) {
opBinary!(op, OtherNumber)(other);
}
/**
* Modify this matrix with a scalar value.
*/
@safe pure nothrow
void opOpAssign(string op, OtherNumber)(OtherNumber other)
if(op != "in" && op != "~" && is(OtherNumber : Number)) {
foreach(i; 0.._data.length) {
mixin(`_data[i]` ~ op ~ `= other;`);
}
}
/**
* Returns: A new matrix produce by combining a matrix and a scalar value.
*/
@safe pure nothrow
Matrix!Number opBinary(string op, OtherNumber)(OtherNumber other) const
if(op != "in" && op != "~" && is(OtherNumber : Number)) {
// Copy this matrix.
auto result = this.dup;
mixin(`result ` ~ op ~ `= other;`);
return result;
}
/**
* Returns: true if two matrices are equal and have the same type.
*/
@safe pure nothrow
bool opEquals(ref const Matrix!Number other) const {
return _rowCount == other._rowCount
&& _columnCount == other._columnCount
&& _data == other._data;
}
/// ditto
@safe pure nothrow
bool opEquals(const Matrix!Number other) const {
return opEquals(other);
}
}
// Test basic matrix initialisation and foreach.
unittest {
size_t rowCount = 4;
size_t columnCount = 3;
int expectedValue = 42;
auto mat = Matrix!int(rowCount, columnCount, [
42, 42, 42,
42, 42, 42,
42, 42, 42,
42, 42, 42
]);
size_t expectedRow = 0;
size_t expectedColumn = 0;
foreach(row, column, value; mat) {
assert(row == expectedRow);
assert(column == expectedColumn);
assert(value == expectedValue);
if (++expectedColumn == columnCount) {
expectedColumn = 0;
++expectedRow;
}
}
}
// Test matrix referencing and copying
unittest {
auto mat = Matrix!int(3, 3);
mat[0, 0] = 42;
auto normalCopy = mat.dup;
normalCopy[0, 0] = 27;
assert(mat[0, 0] == 42, "Matrix .dup created a data reference!");
immutable immutCopy = mat.idup;
}
// Test modifying a matrix row externally.
unittest {
auto mat = Matrix!int(3, 3);
auto row = mat[0];
row[0] = 3;
row[1] = 4;
row[2] = 7;
assert(mat[0] == [3, 4, 7]);
}
// Test immutable initialisation for a matrix
unittest {
immutable mat = immutable Matrix!int(3, 3, [
1, 2, 3,
4, 5, 6,
7, 8, 9
]);
}
// Test matrix addition/subtraction.
unittest {
void runtest(string op)() {
import std.stdio;
size_t rowCount = 4;
size_t columnCount = 4;
int leftValue = 8;
byte rightValue = 10;
auto left = Matrix!int(rowCount, columnCount, [
8, 8, 8, 8,
8, 8, 8, 8,
8, 8, 8, 8,
8, 8, 8, 8,
]);
auto right = Matrix!byte(rowCount, columnCount, [
10, 10, 10, 10,
10, 10, 10, 10,
10, 10, 10, 10,
10, 10, 10, 10,
]);
auto result = mixin(`left` ~ op ~ `right`);
auto expectedScalar = mixin(`leftValue` ~ op ~ `rightValue`);
foreach(row, column, value; result) {
assert(value == expectedScalar, `Matrix op failed: ` ~ op);
}
}
runtest!"+";
runtest!"-";
}
// Text matrix-scalar operations.
unittest {
import std.stdio;
void runtest(string op)() {
size_t rowCount = 2;
size_t columnCount = 3;
// The results for these two values are always nonzero.
long matrixValue = 1_234_567;
int scalar = 11;
auto matrix = Matrix!long(rowCount, columnCount, [
matrixValue, matrixValue, matrixValue,
matrixValue, matrixValue, matrixValue,
]);
auto result = mixin(`matrix` ~ op ~ `scalar`);
auto expectedScalar = mixin(`matrixValue` ~ op ~ `scalar`);
foreach(row, column, value; result) {
assert(value == expectedScalar, `Matirix scalar op failed: ` ~ op);
}
}
runtest!"+";
runtest!"-";
runtest!"*";
runtest!"/";
runtest!"%";
runtest!"^^";
runtest!"&";
runtest!"|";
runtest!"^";
runtest!"<<";
runtest!">>";
runtest!">>>";
}
unittest {
// Test matrix equality.
auto left = Matrix!int(3, 3, [
1, 2, 3,
4, 5, 6,
7, 8, 9
]);
auto right = immutable Matrix!int(3, 3, [
1, 2, 3,
4, 5, 6,
7, 8, 9
]);
assert(left == right);
}
// Test matrix multiplication
unittest {
// Let's test the Wikipedia example, why not?
auto left = Matrix!int(2, 3, [
2, 3, 4,
1, 0, 0
]);
auto right = Matrix!int(3, 2, [
0, 1000,
1, 100,
0, 10
]);
auto result = left * right;
int[][] expected = [
[3, 2340],
[0, 1000]
];
foreach(i; 0..2) {
foreach(j; 0..2) {
assert(result[i, j] == expected[i][j]);
}
}
}
// Test matrix multiplication, with a numeric type on the
// left which implicitly converts to the right.
unittest {
auto left = Matrix!int(1, 1);
auto right = Matrix!long(1, 1);
Matrix!long result = left * right;
}
/**
* This class defines a range of rows over a matrix.
*/
struct Rows(Number) if(isNumeric!Number) {
private:
const(Number)[] _data;
size_t _columnCount;
public:
/**
* Create a new rows range for a given matrix.
*/
@safe pure nothrow
this(ref const Matrix!Number matrix) {
_data = matrix._data;
_columnCount = matrix._columnCount;
}
/// ditto
@safe pure nothrow
this(const Matrix!Number matrix) {
this(matrix);
}
/// Returns: true if the range is empty.
@safe pure nothrow
@property bool empty() const {
return _data.length == 0;
}
/// Advance to the next row.
@safe pure nothrow
void popFront() {
assert(!empty, "Attempted popFront on an empty Rows range!");
_data = _data[_columnCount .. $];
}
/// Returns: The current row.
@safe pure nothrow
@property const(Number[]) front() const {
assert(!empty, "Cannot get the front of an empty Rows range!");
return this[0];
}
/// Save a copy of this range.
@safe pure nothrow
Rows!Number save() const {
return this;
}
/// Retreat a row backwards.
@safe pure nothrow
void popBack() {
assert(!empty, "Attempted popBack on an empty Rows range!");
_data = _data[0 .. $ - _columnCount];
}
/// Returns: The row at the end of the range.
@safe pure nothrow
@property const(Number[]) back() const {
assert(!empty, "Cannot get the back of an empty Rows range!");
return this[$ - 1];
}
/**
* Params:
* index = An index for a row in the range.
*
* Returns: A row at an index in the range.
*/
@safe pure nothrow
@property const(Number[]) opIndex(size_t index) const in {
assert(index >= 0, "Negative index given to Rows opIndex!");
assert(index < length, "Out of bounds index given to Rows opIndex!");
} body {
size_t offset = index * _columnCount;
return _data[offset .. offset + _columnCount];
}
/// Returns: The current length of the range.
@safe pure nothrow
@property size_t length() const {
if (_data.length == 0) {
return 0;
}
return _data.length / _columnCount;
}
/// ditto
@safe pure nothrow
@property size_t opDollar() const {
return length;
}
}
/**
* Returns: A range through a matrix's rows.
*/
@safe pure nothrow
Rows!Number rows(Number)(Matrix!Number matrix) {
return typeof(return)(matrix);
}
unittest {
auto mat = Matrix!int(3, 3, [
1, 2, 3,
4, 5, 6,
7, 8, 9
]);
auto expected = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
size_t rowIndex = 0;
// Test InputRange stuff
for (auto range = mat.rows; !range.empty; range.popFront) {
assert(range.front == expected[rowIndex++]);
}
// Test ForwardRange
auto range1 = mat.rows;
range1.popFront;
range1.popBack;
auto range2 = range1.save;
range1.popFront;
assert(range2.front == [4, 5, 6]);
rowIndex = 3;
// Test BidirectionalRange
for (auto range = mat.rows; !range.empty; range.popBack) {
assert(range.back == expected[--rowIndex]);
}
// Test RandomAccessRange
auto range3 = mat.rows;
range3.popFront;
range3.popBack;
assert(range3.length == 1);
assert(range3[0] == [4, 5, 6]);
}
// Test 0 size Matrix rows
unittest {
Matrix!int mat;
assert(mat.rows.length == 0);
}
/**
* A static matrix type. This is a value matrix value type created directly
* on the stack.
*/
struct Matrix(Number, size_t _rowCount, size_t _columnCount)
if(isNumeric!Number && _rowCount > 0 && _columnCount > 0) {
/// The number of rows in this matrix.
enum rowCount = _rowCount;
/// The number of columns in this matrix.
enum columnCount = _columnCount;
/// true if this matrix is a zero-sized matrix.
enum empty = false;
/// true if this matrix is a square matrix.
enum isSquare = rowCount == columnCount;
/// The data backing this matrix.
Number[columnCount][rowCount] array2D;
alias array2D this;
/**
* Construct this matrix from a 2 dimensional static array.
*
* Params:
* array2D = A 2 dimension array of the same size.
*/
@safe pure nothrow
this(ref const(Number[columnCount][rowCount]) data) inout {
array2D = data;
}
/// ditto
@safe pure nothrow
this(const(Number[columnCount][rowCount]) data) inout {
array2D = data;
}
/**
* Construct this matrix directly from a series of numbers.
* This constructor is designed to be executed at compile time.
*
* Params:
* numbers... = A series of numbers to initialise the matrix with.
*/
@safe pure nothrow
this(Number[rowCount * columnCount] numbers...) {
foreach(row; 0 .. rowCount) {
foreach(column; 0 .. columnCount) {
array2D[row][column] = numbers[row * columnCount + column];
}
}
}
/**
* Returns: A reference to this matrix's data as a 1D array.
*/
@trusted pure nothrow
@property
ref inout(Number[rowCount * columnCount]) array1D() inout {
return (cast(Number*)array2D.ptr)[0 .. rowCount * columnCount];
}
// Even with alias this, we still need this overload.
/**
* Params:
* row = A row index.
*
* Returns: A row from the matrix.
*/
@safe pure nothrow
ref inout(Number[columnCount]) opIndex(size_t row) inout {
return array2D[row];
}
/**
* Params:
* row = A row index.
* column = A column index.
*
* Returns: A value from the matrix
*/
@safe pure nothrow
ref inout(Number) opIndex(size_t row, size_t column) inout {
return array2D[row][column];
}
/**
* Overload for foreach(rowIndex, columnIndex, value; matrix) {}
*/
@trusted
int opApply(int delegate(ref size_t, ref size_t, ref Number) dg) {
int result = 0;
matrixLoop: foreach(row, rowArray; array2D) {
foreach(column, value; rowArray) {
result = dg(row, column, value);
if (result) {
break matrixLoop;
}
}
}
return result;
}
/**
* Modify this matrix, adding/subtracting values from another matrix.
*
* Example:
* ---
* matrix += other_matrix;
* matrix -= yet_another_matrix;
* ---
*
* Params:
* other = Another matrix with an implicitly convertible numeric type.
*/
@safe pure nothrow
void opOpAssign(string op, OtherNumber)
(ref const(Matrix!(OtherNumber, rowCount, columnCount)) other)
if((op == "+" || op == "-") && is(OtherNumber : Number)) {
foreach(i; 0 .. rowCount) {
foreach(j; 0 .. columnCount) {
mixin(`array2D[i][j]` ~ op ~ `= other.array2D[i][j];`);
}
}
}
/// ditto
@safe pure nothrow
void opOpAssign(string op, OtherNumber)
(const(Matrix!(OtherNumber, rowCount, columnCount)) other)
if((op == "+" || op == "-") && is(OtherNumber : Number)) {
opOpAssign(other);
}
/**
* Add or subtract two matrices, yielding a new matrix.
*
* Params:
* other = The other matrix.
*
* Returns: A new matrix.
*/
@safe pure nothrow
Matrix!(Number, rowCount, columnCount) opBinary(string op, OtherNumber)
(ref const(Matrix!(OtherNumber, rowCount, columnCount)) other)
if((op == "+" || op == "-") && is(OtherNumber : Number)) {
// Copy this matrix.
typeof(return) result = this;
mixin(`result ` ~ op ~ `= other;`);
return result;
}
/// ditto
@safe pure nothrow
Matrix!(Number, rowCount, columnCount) opBinary(string op, OtherNumber)
(const(Matrix!(OtherNumber, rowCount, columnCount)) other)
if((op == "+" || op == "-") && is(OtherNumber : Number)) {
return opBinary(other);
}
/**
* Multiply two matrices.
*
* Given a matrix of size (m, n) and a matrix of size (o, p).
* This operation can only work if n == o.
* The resulting matrix will be size (m, p).
*
* Params:
* other = Another matrix
*
* Returns: The product of two matrices.
*/
@safe pure nothrow
Matrix!(Number, rowCount, otherColumnCount)
opBinary(string op, OtherNumber, size_t otherRowCount, size_t otherColumnCount)
(ref const(Matrix!(OtherNumber, otherRowCount, otherColumnCount)) other) const
if((op == "*") && (columnCount == otherRowCount) && is(OtherNumber : Number)) {
typeof(return) result;
matrixMultiply(result, this, other);
return result;
}
/// ditto
@safe pure nothrow
Matrix!(Number, rowCount, otherColumnCount)
opBinary(string op, OtherNumber, size_t otherRowCount, size_t otherColumnCount)
(const(Matrix!(OtherNumber, otherRowCount, otherColumnCount)) other) const
if((op == "*") && (columnCount == otherRowCount) && is(OtherNumber : Number)) in {
return opBinary!(op, OtherNumber, otherRowCount, otherColumnCount)(other);
}
@safe pure nothrow
Matrix!(OtherNumber, rowCount, otherColumnCount)
opBinary(string op, OtherNumber, size_t otherRowCount, size_t otherColumnCount)
(ref const(Matrix!(OtherNumber, otherRowCount, otherColumnCount)) other) const
if((op == "*") && (columnCount == otherRowCount) && !is(Number == OtherNumber) && is(Number : OtherNumber)) {
typeof(return) result;
matrixMultiply(result, this, other);
return result;
}
@safe pure nothrow
Matrix!(OtherNumber, rowCount, otherColumnCount)
opBinary(string op, OtherNumber, size_t otherRowCount, size_t otherColumnCount)
(const(Matrix!(OtherNumber, otherRowCount, otherColumnCount)) other) const
if((op == "*") && (columnCount == otherRowCount) && !is(Number == OtherNumber) && is(Number : OtherNumber)) {
return opBinary!(op, OtherNumber, otherRowCount, otherColumnCount)(other);
}
}
// 0 size matrices are special case.
/// ditto
struct Matrix(Number, size_t _rowCount, size_t _columnCount)
if(isNumeric!Number && _rowCount == 0 && _columnCount == 0) {
/// The number of rows in this matrix.
enum rowCount = _rowCount;
/// The number of columns in this matrix.
enum columnCount = _columnCount;
/// True if this matrix is a zero-sized matrix.
enum empty = true;
/// true if this matrix is a square matrix.
enum isSquare = true;
}
/**
* Alias all (M, 0), (0, N) size static matrices into one single zero-sized
* type of size (0, 0).
*/
template Matrix(Number, size_t rowCount, size_t columnCount)
if ((rowCount > 0 && columnCount == 0) || (rowCount == 0 && columnCount > 0)) {
alias Matrix = Matrix!(Number, 0, 0);
}
// Test copy constructor for 2D arrays.
unittest {
int[3][2] data = [
[1, 2, 3],
[4, 5, 6],
];
Matrix!(int, 2, 3) matrix = data;
assert(data == matrix);
}