Basic Linear Algebra and D's Array Operation
Andrew Edwards
edwards.ac at gmail.com
Sun May 19 06:34:18 UTC 2019
Sooo... I'm trying to learn this stuff so that I can fully grasp
the content of Jens Mueller's 2019 DConf talk and its
applications in financial sector (forex and options/futures
trading). Unfortunately, I'm doing so using python but I'd like
to accomplish the same in D. Here goes:
Array (Vector) Algebra
auto V = [1, 2, 3];
auto v = V[] * 4; // v == [4, 8, 12]
Output:
Error: array operation V[] * 4 without destination memory not
allowed
Huh? What? That makes no sense, but okay... whatever.
int[3] v = V[] * 4; // v == [4, 8, 12]
v[] = v[] + v[]; // v == [8, 16, 24]
v[] = v[] / 2; // v == [4, 8, 12]
v[] = v[] - v[]; // v = [0, 0, 0]
so far so good. On to the fun stuff.
Multidimensional Array (Matrix) Algebra
auto M = [[1, 2, 3], [1, 2, 3], [1,2,3]];
int[3][3] m = M[] * 4;
Output:
Error: incompatible types for (M[]) * (4): int[][] and int
Okay, I'm lost on that one. But let's press on.
int[3][3] m = M[] * [[4]];
Output:
Error: cannot implicitly convert expression M[] * [[4]] of type
int[][] to int[]
int[3][3] m = M[] * [[4,4,4], [4,4,4], [4,4,4]];
Output:
Error: cannot implicitly convert expression M[] * [[4, 4, 4],
[4, 4, 4], [4, 4, 4]] of type int[][] to int[]
I'm so confused. Maybe it's the way I'm accessing M?
int[3][3] m = M[][] * 4;
Output:
Error: incompatible types for (M[]) * (4): int[][] and int
And... it begins again!!!
So the question is, how do I pull this off in D using just
builtin arrays and phobos?
Any assistance is appreciated.
Thanks,
Andrew
P.S. Why do we still have two sets of documentations ([1],[2])
for the language? Which is the official one and when can we get
rid of the other?
[1] https://dlang.org/library/std/array.html
[2] https://dlang.org/phobos/std_array.html
More information about the Digitalmars-d-learn
mailing list