Public Group

# Whats needed in Vector & Matrix Classes?

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

## Recommended Posts

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?

##### Share on other sites
A Matrix class should offer methods to multiply with another matrix or a vector for example.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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!

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by SharlinIf 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.

##### Share on other sites
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.

##### Share on other sites
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.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 9
• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633348
• Total Posts
3011459
• ### Who's Online (See full list)

There are no registered users currently online

×