Sign in to follow this  
Basiror

C++: math vector template vs explicit elaboration?

Recommended Posts

Hi doing some math lately I run across a little design problem A few months ago I implemented a vector3d class for 3 dimensional vectors, but now I need n-dimensional vectors What I thought of was creating a templated class template<uint32 dim> class vector{...}; and in order to add n-component constructors e.g.: vector4d(1,2,3,4); I derive a class vector-n-d from vector<n> Would you use templates in this case? or rather explicitly elaborate each vector class, I think I will hardly need any other vector classes than 2d,3d and 4d now comes another problem should I template my matrix class as well? in order to allow any m,n matrix or would you store the m,n dimensions inside the matrix and allocate a dynamic buffer? of course the operators for inter class operations will be made non members what would you suggest?

Share this post


Link to post
Share on other sites
I'd suggest using templates for sure. For only a max of 4-5 types (with vectors) I don't see code bloat becoming a problem.

For a more thorough study, and having been recently just brought up, I'll also link to an implementation of what you're asking about.

Share this post


Link to post
Share on other sites
I've run into this question w/ my second implementation of vector math made recently. As an addition, I've considered different scalar types I needed at least for 2D vectors. So I wrote the classes w/ the scalar type as template parameter, but also thought of introducing the dimension as a template parameter.

I've decided not to do so. First, too much routines expect a count of parameters equal to the dimension. Second, almost all routines are dependend internally on the dimension. Of course, many of them (if not all) may be written by simply using a loop. However, then I've had to trust that the compiler (which ever will be used) will be able to unroll it properly. Third, some things could be done for the one or other dimension only. The cross product comes ad-hoc into my mind. Other routines will have different implementations, e.g. the vector perpendicular to a reference vector.

But hey, its just my opinion ;-) There is no golden solution, I think.

Share this post


Link to post
Share on other sites
repetitive boilerplate code == code stink. [grin]

How to reduce repetitive boilerplate code in C++ to virtually zero requires the combination of preprocessor & template metaprogramming.

If you really need to work with arbitrary sized vectors & matrices (and linear algebra in general) efficiently seriously consider using preexisting libraries because the knowledge required to write such a library in C++ is quite advance.

You need to understand templates like the back of your hand (including template argument matching), understand what expression templates are and what they are used for, know about type traits and tag dispatching, unserstand koenig lookup and the SFINAE principle, Concept checking and static asserts, various template metaprogramming tricks, it's non trivial to get generalized and efficent code on par with Fortran in C++.

If all you want is an arbitrary sized vector/matrix base to reduce boilerplate code of 2/3/4 dimensions then thats fine, using boost preprocessor, mpl, type traits can help out alot here.

[Edited by - snk_kid on October 23, 2005 6:08:52 AM]

Share this post


Link to post
Share on other sites
I didn t even know that expression templates exist in C++
Are their supported by the latest compilers?

Here s link that seems to explain it quite good although resources about this topic seem to be pretty rare

http://osl.iu.edu/~tveldhui/papers/Expression-Templates/exprtmpl.html


Need to check that out, do you know any book that cover the topics you mentioned?

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
I didn t even know that expression templates exist in C++
Are their supported by the latest compilers?


It's not a feature of C++ it's a technique, basically the main idea is to eliminate redundant temporaries of large/complex expressions with large datatypes e.g. imagine some 30x30 matrix type with +, * overloaded, now imagine a long expression such as:


mat30x30 A, B, C;

//.....

mat30x30 D = A * B + C * A + C * B;


This would kill performance as you could imagine with typical implementation of each operator, the problem with operator overloads is that it tends to eager evalution what we can do instead is delay evaluation until the whole expression is known for assignement/construction of D. This is done by building a compile-time parse tree of this expression. Essentially it's lazy evalution.

Probably not worth it for 2/3/4 dimension vector/matrices since (named) return value opimizations (RVO/NRVO) suffice, most compilers support RVO slightly less support NRVO.

Quote:
Original post by Basiror
Here s link that seems to explain it quite good although resources about this topic seem to be pretty rare

http://osl.iu.edu/~tveldhui/papers/Expression-Templates/exprtmpl.html


There should be a thew resources online, make sure you type expression templates and not the other way round, check my Sig link for a clear introduction & explanation.

Quote:
Original post by Basiror
Need to check that out, do you know any book that cover the topics you mentioned?


Modern C++ Design: Generic Programming and Design Patterns Applied
C++ Template Metaprogramming : Concepts, Tools, and Techniques from Boost and Beyond
C++ Templates - The Complete Guide
Generative Programming: Methods, Tools, and Applications

Share this post


Link to post
Share on other sites
Here s another link with a really got description I think

http://www.langer.camelot.de/Articles/Cuj/ExpressionTemplates/ExpressionTemplates.htm


This template meta programming reminds me of recursion with prolog

So all you do is representing the expression as a bunch of nested templates

and the compiler optimizes the whole thing for maximum performance which shall eleminate redundant operations

Need to try it in a few sample programs

thx

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
This template meta programming reminds me of recursion with prolog


Recursion is a key element, template metaprogramming is like programming in a pure strict functional language (with optional lazyiness which helps to reduce compilation times) since metadata is immutable. Boost MPL has formalized the approach from an adhoc practice, containing STL style containers & algoritms plus more.

Quote:
Original post by Basiror
So all you do is representing the expression as a bunch of nested templates


Something like that, it is in fact an instance of the composite pattern in compile-time form since it is recursive composition.

Template metaprogramming isn't limited to expression templates though, you can do all kinds of things, generate code, simluate paritial evalution, etc but even template metaprogramming can have repetitive boilerplate (due to current restrictions of C++ templates) code so preprocessor metaprogramming helps out alot, boost has even formalized that too.

Quote:
Original post by Basiror
and the compiler optimizes the whole thing for maximum performance which shall eleminate redundant operations


Well not redundant operations but redundant temporaries.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this