By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

Matrix template class

P: 32
Hello,

I created a template matrix class and some appropriate binary operators like:

Expand|Select|Wrap|Line Numbers
  1. template <typename ContainedType>
  2. Matrix<ContainedType> operator+ (const Matrix<ContainedType>& lhs, const Matrix<ContainedType>& rhs) 
  3. { ... }
  4.  
The first line of each such binary operator is a check, like:

Expand|Select|Wrap|Line Numbers
  1. assert(lhs.rows == rhs.rows && lhs.columns == rhs.columns);
  2.  
But I was wondering if one could model the mathematical behavior of matrices? By that I mean that (unless you define them) there are no valid operations with matrices of different dimensions.
I think this could be well modeled with types because usually there are no valid operations between different types. And with templates one could define a type "on demand".

So I'm thinking of a syntax like:

Expand|Select|Wrap|Line Numbers
  1. Matrix<float, 4, 4> matrix_4x4;
  2.  
would define a 4 by 4 matrix whose elements are floats, and it would be a different type than a

Matrix<float, 3, 3> ...

So is there a way to do this with templates? Or any other way?

Thanks in advance,

Harinezumi
Sep 9 '08 #1
Share this Question
Share on Google+
6 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Why not use a vector of vectors?

Expand|Select|Wrap|Line Numbers
  1. vector< vector<ContainedType> > v;
  2.  
That way your code is already written.
Sep 9 '08 #2

100+
P: 424
I'm not sure that representing matrices of different rank by different types is a good way to do things. Consider the transpose of a 2x3 matrix, for example, it is a 3x2 matrix, and two are related a lot more than an int and a char is related. You can generate matrices of one rank using matrices from another rank, using a tensor product, for example, which is a binary operation you may want to implement.

Now of course a matrix of int and one of char are certainly unrelated so it makes sense that the type of their contents should appear in the template specifier but not the rank. The point is that you may encounter situations when the rank of a matrix vary when you perform certain operations on it, but it's highly unlikely that type that it contains would change.
Sep 9 '08 #3

Banfa
Expert Mod 5K+
P: 8,916
I think this could be well modeled with types because usually there are no valid operations between different types. And with templates one could define a type "on demand".
This statement is just not true, a trivial example is matrix multiplication

2x2 matrix * 2x2 matrix = 2x2 matrix

however

3x2 matrix * 2x3 matrix = 2x2 matrix

There is no constraint for the operands to be the same as each other or the result in matrix operations.
Sep 10 '08 #4

Banfa
Expert Mod 5K+
P: 8,916
Why not use a vector of vectors?

Expand|Select|Wrap|Line Numbers
  1. vector< vector<ContainedType> > v;
  2.  
That way your code is already written.
Personally I would call that a way to manage the data internally to the Matrix class. The "code" is going to be all the binary operations required that operate on the matrices.
Sep 10 '08 #5

Banfa
Expert Mod 5K+
P: 8,916
Oh one final thought having looked up tensor products is that clearly then a matrix of any order can be operated on by a matrix of any other order. That would make it very hard to program into a template class (you would need a method that would accept any other sized template). Although if you had a template class then I think an external template function to perform the operation would be possible because the function would be instantiated at the time of the function call based on both matrices not when the matrix is constructed based on the matrix being constructed.

However I would have to ask why are you doing this? As a programming exercise or because you have a need for a matrix class?

In the latter case you could just try looking on the internet for a class that already does what you need, for example the Boost matrix class.

In the former case looking at the Boost library and noting that in their template you only set the type of each element and the dimensions of the matrix are parameters to the constructor and also noting that generally those Boost guys know what they are doing may give you a clue as to how to go about writing your own class.
Sep 10 '08 #6

P: 32
There is no constraint for the operands to be the same as each other or the result in matrix operations.
You're right, I forgot about that kind of multiplication!

However I would have to ask why are you doing this? As a programming exercise or because you have a need for a matrix class?
It is a programming exercise for me. I already have the class, it just occurred to me that coding the rank of the matrix as template parameter may be a good idea, but now I see it is obviously not.
Thanks for the posts!
Sep 10 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.