On Sun, 25 Apr 2010 23:17:10 -0300, Gareth Charnock <gareth....@gmail.com>
wrote:
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
Some quick comments:
- The version I'm seeing on github doesn't seem to have all the features
you're referencing (i.e. v*v). Why are some ops limited to */ and other +-?
- Static foreach isn't a good way to do loop unrolling (at least
historically).
- In terms of naming, I prefer float3 instead of vector3f.
- Return type and argument types need to be dynamically deduced: i.e. int3
* float3 should work.
- length should mean the number of vector elements. I'd recommend norm or
L2 instead.
- For the matrix, Field[D][D] raw; will let you get out of manual indexing
issues.
- I've never seen matrix swizzling and don't really know what it would be
used for.
- You should be able to set matrix layout to either C or Fortran style
- Re: Transpose: this should be an in place swap. Given the value-type
semantics of small matrices, make the copies.
- Re: Transposed: this function should be named T (or have an equivalent
alias) and return a reference to the current matrix casted to its C or
Fortran layout counterpart.
In general, this feels like a decent start, although it's still very alpha
and feature incomplete. Keep up the good work.