Whats needed in Vector & Matrix Classes?

Started by
8 comments, last by Leo_E_49 18 years ago
I want to have a go at creating my own Vector and Matrix classes but I'm not sure where to start. I tried searching for an article on it but couldn't fine one. What do I need to provide in these classes?
Advertisement
A Matrix class should offer methods to multiply with another matrix or a vector for example.
I would suggest that, if you don't have any idea of how to build a vector class or a matrix class, you either don't know what a vector or matrix is well enough, or don't know your language well enough.

What is a vector? The most common definition is "an indicator of direction and magnitude", usually as some combination of two scalars. What is a matrix? Generally, a rectangular array of scalars. In each case, you'd need your class to store the pertinent values -- variables -- that define a specific vector or matrix.

What operations can be performed on a vector, or a pair of vectors? Dot-product (scalar product), cross-product (vector product), translation, and rotation are a few that come to mind. What operations can be performed on a matrix, or a pair of matrices? Matrix multiplication, inversion, and so forth. In each case, you'd need your class to provide functions to perform these operations.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Such classes can be arbitrarily complex, more or less. If this is your first go at it, you might just start with the basic arithmetic and algebraic operations, which includes:

1. Vector and matrix +, -, +=, -=, * and *= (a scalar), and / and /= (a scalar)
2. Matrix-matrix multiplication
3. Matrix-vector multiplication

Next would come vector products such as dot and cross, as has been suggested. Also, you'll have to decide what dimensions you're interested in. 2, 3 and 4 are common for vectors, and 3x3 and 4x4 are most common for matrices. Again, the options for how to approach writing a math library and what functionality to incorporate are practically endless, so you just have to dive in.
Why bother writing one, there are optimised (many) linear algebra libraries around, and by itself it's rather boring. Writing a basic matrix/vector multiplication/addition etc. class is trivial, writing a good (i.e. fast) one is advanced science. Look at LAPACK\BLAS, ATLAS and GSL or if you're on AMD64, try ACML (on the AMD site).

My 2 cents...

PS: Whatever you plan on doing, looking at these libraries wiil show you whta's needed! Once you hit the eigenvalues, you'll see you're barking up the wrong tree!
If you are only going to use matrices to represent affine transformations, pretty much the only matrix operations you are going to need are matrix-matrix and matrix-vector multiplications (and possibly matrix inversion and transposition). No need to bother with matrix-matrix additions, for example.
struct matrix44 {    float m[4][4];    float& operator[](int i, int j) {return m[j];}    const float& operator[](int i, int j) const  {return m[j];}    float& operator[](int i) {return (float*(m));}    const float& operator[](int i) const  {return ((float*)(m));}};struct vector3 {    union {       float v[3];       struct {          float x;          float y;          float z;       };    };    float& operator[](int i) {return v;}    const float& operator[](int i) const  {return v;}};

Operations on the data ought to be in non-member functions.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Quote:Original post by Sharlin
If you are only going to use matrices to represent affine transformations, pretty much the only matrix operations you are going to need are matrix-matrix and matrix-vector multiplications (and possibly matrix inversion and transposition). No need to bother with matrix-matrix additions, for example.


Matrix-matrix additions might be useful for interpolating transformations.

alpha*M + (1-alpha)*N

or would that just produce junk? Don't have enough intuition.
Unfortunately, yes, although you can linearly interpolate between two transformations like that, it won't look good in the general case. To interpolate between two orientations you'd usually convert to quaternion representation, interpolate, and convert back.
Might want to throw in quaternions while you're at it. When I wrote my math libraries, I included quaternions and SLERP. It turns out that it was a really good idea because quaternions are really handy for rotations and mesh animation.

This topic is closed to new replies.

Advertisement