-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdiagonalmatrix.d
45 lines (37 loc) · 956 Bytes
/
diagonalmatrix.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
import matrix;
import openmethods;
mixin(registerMethods);
class DiagonalMatrix : Matrix
{
@property int rows() const { return cast(int) elems.length; }
@property int cols() const { return cast(int) elems.length; }
@property double at(int i, int j) const { return i == j ? elems[i] : 0; }
double[] elems;
this()
{
}
this(double[] elems)
{
this.elems = elems.dup;
}
}
@method
DiagonalMatrix _plus(DiagonalMatrix a, DiagonalMatrix b)
{
assert(a.elems.length == b.elems.length);
auto result = new DiagonalMatrix;
result.elems.length = a.elems.length;
result.elems[] = a.elems[] + b.elems[];
return result;
}
@method("times")
DiagonalMatrix doubleTimesDiagonal(double a, DiagonalMatrix b) {
auto result = new DiagonalMatrix;
result.elems.length = b.elems.length;
result.elems[] = a * b.elems[];
return result;
}
@method("times")
DiagonalMatrix DiagonalTimesDouble(DiagonalMatrix a, double b) {
return doubleTimesDiagonal(b, a);
}