# Vector\Matrix library design help...

This topic is 4122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, Ummm... I want to write a library to help me make things simpler by adding usefull functions and classes, but I need help... The thing is that I am thinking combining basic C functions with Classes, meaning do a Class and a basic Function collection, now for the real question: Should I do a specific Class/Collection for a specific sized Matrix/Vector or should I just use a template for a class and many overriden functions for the basic C collection? It might help your advice if I say that for now it is stricly to help me simplify my OpenGL programs, although they still may need 2\3\4 dimensional vectors... Thanks.

##### Share on other sites
You could probably make a C++ class with all the fucntionalities and make a C wrapper for it.

##### Share on other sites
Are you, in fact, using C? As in malloc(), stdio.h, variable declarations must be at the top of their scope, implicit int return types , etc.?

If so, why?

If not, have you considered std::vector, boost::array and boost::multi_array?

##### Share on other sites
what can I say? I'm an old fasioned guy with a crazy mind :)
It is more efficient to write the code for specific sized vectors instead of a general one, I don't need the heavy C++ stl classes... I like it simple yet with the power options of C++

##### Share on other sites
Quote:
 Original post by Fimbulvetrwhat can I say? I'm an old fasioned guy with a crazy mind :)It is more efficient to write the code for specific sized vectors instead of a general one, I don't need the heavy C++ stl classes... I like it simple yet with the power options of C++

1) Do you *know* what the overhead of a std::vector is (both speed and size)? Prove it to me.

2) By "specific sized vector", do you mean something not-resizable, with a size known at compile-time, that still behaves like an object (i.e. won't decay to a pointer when passed to a function, can be returned from a function etc.)? With zero overhead (since there isn't anything that's needed)? If so, that is exactly what boost::array is. It's even designed to let you initialize it with array/aggregate-initialization syntax (at the cost that you don't get nice constructor syntaxes; C++ forces you to choose). If you need something multi-dimensional, but with each dimension's size fixed at compile time, then you can just make a boost::array of boost::arrays (i.e. nest the templates). There will still be zero overhead (except possibly for struct padding - which you would not be able to avoid by Doing It Yourself).

##### Share on other sites
This particular wheel has been reinvented so many times, it's not funny anymore.

If your objective is LearningTM, then you really shouldn't care about performance: if you're in a position to learn, it implies that you don't know much to start with, so expecting to get anything fast on the first try is pretty ridiculous — you might get somewhat decent performance after one or two tries and reading a book about numerical optimizations. Besides, if you're learning, then feel free to question anything we say, including what Zahlman says (although experience shows he's either correct, or temporarily low on caffeine), but don't contradict unconstructively: it's silly and not very serious.

If your objective is getting your hands on a library that handles vectors and matrices, then Google. As I've said, this particular wheel has been reinvented so many times, you can be pretty sure that many implementations are above average, and above what you could come up with on short notice (or do you intend to spend a few months full-time documenting yourself about it, and then tweaking the code, without doing anything else?) For instance, SVL is idiomatic, designed for CPU-heavy computations and free. The D3DX library that comes with DirectX also has some optimized matrix and vector computation capabilities, and can be used with OpenGL too.

##### Share on other sites
Umm... Zalhman... I don't think he's talking about making his own std::vector but a vector math library... Like rotating vectors and such. Not sure how multiarray etc is any use here.

Anyways, OP:

Our Matrix and Vector classes contain just basic functionality. Addition, subtraction, division, etc.

Anything like Normalize, Length, etc are all global overloaded functions in a namespace.

So you don't do a Vector3Df v(1.2, 2.2, 3.2); v.Normalize(). But instead Normalize(v);

We found it much easier and more intuitive that way.

##### Share on other sites
Quote:
 Original post by Anonymous PosterUmm... Zalhman... I don't think he's talking about making his own std::vector but a vector math library... Like rotating vectors and such. Not sure how multiarray etc is any use here.

Which is why I also mentioned boost::array. If you don't need the resizing - if it will get in the way, or even damage productivity by making it harder to prove things at compile-time - then you can always create your multi-dimensional arrays by nesting boost::array templates. You can mix and match them perfectly well, too. For example, some complex types and possible (creative ;) ) interpretations:

std::vector<std::vector<boost::array<double, 2> > >;// a sequence of sequences (each associated with a particle generator)// of particle positions (for all particles generated by the corresponding// generator).boost::multi_array<std::vector<boost::array<int, 3> >, 2>;// a 3-d terrain of unknown size but rectangular bounds, where each point on the// 2-d map is represented by a stack of coloured dots (in turn indicating the // soil characteristics at the corresponding depth; the height of the stack// gives the elevation at that point, thus providing the 3rd dimension).

And nothing prevents you from using boost::array as a storage data member for your vector or matrix class, implementing operators as free functions upon boost::arrays, or even inheriting from boost::array (of course, it's not designed for polymorphism; you likely want to inherit privately).

##### Share on other sites
Zahlman, what would be wrong with using std::valarray<> over boost::array? I.e., what does the latter give that makes up for the additional dependency on the boost library at compile time?

(...and no, I'm not being argumentative here... I've used std::valarray<> a lot in my own maths code when I've needed vectors and arrays... so I want to know if I'm missing something I should be looking in to.)

Cheers,

Timkin

##### Share on other sites
Quote:
 Original post by ToohrVykThis particular wheel has been reinvented so many times, it's not funny anymore.
what's wrong with writing your own vector library? since you probably are doing this on your free time, you gotta love the syntax. For me, that rules out Direct X. SVL seemed nice on that page, but after I downloaded it, it seems to have it's own problems: "everything" is inlined and live in the global namespace.

I wrote a game and a engine in roughly a month(202 hours), and given the fact that there is at least a few pages that tell you how you implement each method, I don't think it should pose any problem. I haven't noticed any performance problem whatsoever, and if I did, it's probably not my math library.

• 18
• 11
• 16
• 9
• 50
• ### Forum Statistics

• Total Topics
631396
• Total Posts
2999783
×