Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

weilanddavid

macrumors newbie
Original poster
Oct 22, 2006
16
0
I'm trying to learn templates by writing some linear algebra code. I have created a vector template

template<class T, int N>
class Vektor {
T entry[N];
etc...

and then a matrix template that consists of column vectors:

template <class T, int N, int M>
class Matrix {
Vektor<T,N> column[M];
etc...

My problem is overloading the multiplication operator to define matrix multiplication. Seems I would need something to the effect of

Matrix<T,N,K> Matrix<T,N,M>:: operator*(const Matrix<T,M,K>& B) etc...

given that the matrices are often of different sizes. Putting this in the template definition doesn't work because of the extra K parameter. Defining it in a whole new template of the form

template<class T, int N, int M, int K>
Matrix<T,N,K> Matrix<T,N,M>:: operator*(const Matrix<T,M,K>& B) etc..

doesn't work because this operator wasn't defined in the original matrix class. Seems like a an elemntary issue, but I can't decipher the literature I have on this subject.
Many thanks, David
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Unless you're only going to have a small set of matrix sizes, I would actually suggest that you don't use integers for the dimensions in the template. It would probably work, but here's the problem: you're using C++...

When C++ templates are defined and compiled, there really isn't any code that is created until the template is actually used. However, whenever an instance of the template class is declared, the compiler will essentially copy and paste all the correct values in to the template and compile a new class. And it does this for every unique declaration. So, for example, using your code, all of the following will create different classes:

Code:
Matrix<int, 2, 2> ...
Matrix<int, 3, 2> ...
Matrix<int, 500, 200> ...
// etc ...

That will likely create problems in size and memory as your program grows.

In addition to that, correct me if I'm wrong, but I believe that template declarations are always static -- that is, defined at compile time. You can't have the user say he wants a 42 x 12 matrix and then create a matrix with the template with the specified sizes. You have to predict the sizes that the user will want before you even compile the program... I'm not sure, but that's probably not what you want.

Sorry not to answer your question directly, but C++ gives me a headache. :p
 

LtRammstein

macrumors 6502a
Jun 20, 2006
570
0
Denver, CO
Kpua, you're actually hitting the nail right on the head.

Working with C++, and using templates (not for matrix calculations, but rather data lists and what not), a matrix of it is kinda bizzare...

It looks like what you know what you are doing, but I think you are approaching it in the wrong way.

Matrix Coding in C++

CodeProject.com is a great site that gives great code samples and hints on how to do certain projects. I've used it when my professor only taught us 1 day of hash, hash_maps, maps.

Steve
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.