-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.d
64 lines (54 loc) · 1.01 KB
/
app.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
import std.stdio;
import matrix;
import diagonalmatrix;
import densematrix;
import openmethods;
mixin(registerMethods);
void print(virtual!Matrix m);
@method
void _print(Matrix m)
{
const int nr = m.rows;
const int nc = m.cols;
for (int i = 0; i < nr; ++i) {
for (int j = 0; j < nc; ++j) {
writef("%3g", m.at(i, j));
}
writeln();
}
}
@method
void _print(DiagonalMatrix m)
{
import std.algorithm;
import std.format;
import std.array;
writeln("diag(", m.elems.map!(x => format("%g", x)).join(", "), ")");
}
void main()
{
Matrix dense1 =
new DenseMatrix(2, 3,
[ 1, 2, 3,
4, 5, 6 ]
);
writeln("dense1 =");
print(dense1);
writeln();
Matrix diag1 = new DiagonalMatrix([ 7, 8, 9 ]);
write("diag1 = ");
print(diag1);
writeln();
Matrix dense2 =
new DenseMatrix(2, 3,
[ 2, 3, 4,
5, 6, 7 ]
);
writeln("dense1 + dense2 =");
print(plus(dense1, dense2));
writeln();
Matrix diag2 = new DiagonalMatrix([ 1, 2, 3 ]);
write("diag1 + diag2 = ");
print(plus(diag1, diag2));
writeln();
}