Small vector and matrix proposed for phobos2 now on github

Gareth Charnock gareth.tpc at gmail.com
Sun Apr 25 19:17:10 PDT 2010


I've put the beginnings of my matrix-vector library up on github 
(between swizzling, generalising to N dimensions and making use of 
static foreach this project quickly turned from a cleanup to a rewrite). 
You can find it here.

http://github.com/gcharnock/phoboslinalgebra

Some highlights so far:

//Dimension and field are templatable parameters. There is no hard limit 
on N, but obviously everything is set up for N being small.
alias Vector!(float,3) vector_t;
alias Matrix!(float,3) matrix_t;

//Very natural initialisation
auto v=vector_t(1,2,3);
auto m=matrix_t(1,2,0,0,1,0,0,3,1);

//Single element access does not require opDispatch
writeln(v.x);

//Swizzling supported via opDispatch
writeln(v.yzx); //2 3 1
writeln(v.xxyyzx); //1 1 2 2 3 1

//No matter how big N, elements always have a canonical name (probably 
more important for matrices where the letters of the alphabet run out 
faster although I've only done this for vectors so far)
writeln(v.x2); //3
writeln(v.x0x1x2); //1 2 3, zero based indexing because D inherits C

//Some Math operations. Static foreach is used to unroll all loops. 
Unfortunately I ran into some problems putting the operators outside the 
struct. I'm not sure if this was me or the compiler but I'll need 
another hacking session to work out what's going on.

float s;
v*s    v/s    v*=s
v/=s   v+v    v-v
v*v    m+m    m-m
m*m    m*v

A question I'd like input on would what should the semantics of the 
Transpose(), Transposed() function. I can think of some alternatives:

Transpose:
1) Naive solution, copies N^^2-N scalars.
2) Set a bool. Switch between two element iteration schemes for nearly 
every operation (with suitable use of templates this might not be as 
painful as it sounds.)

Transposed:
1) makes a new matrix
2) creates a transposed image which is forever linked to the original 
matrix: A=B.Transpose() => Changes to A affect B
3) creates a transposed image which is copy-on-write linked to the 
original matrix: A=B.Transpose() => Changes to A do not affect B


More information about the Digitalmars-d-announce mailing list