473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Matrix template class

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
6 3908
weaknessforcats
9,208 Expert Mod 8TB
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
arnaudk
424 256MB
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
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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
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

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

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
7
by: Erik Borgstr?m | last post by:
Hi, I simply want to use simple matrices of ints or doubles in C++ and I want all the good: 1) be able to use double-index, e.g. m 2) not have to use pointers at all 3) do it fast I know...
11
by: Michael Bader | last post by:
Hi, I'm currently working on a matrix multiplication code (matrix times matrix), and have come along some interesting/confusing results concerning the running time of the (apparently) same...
1
by: zhouxn | last post by:
template <class T> class matrix { public: typedef int size_type; typedef T data_type; typedef T& reference; typedef const T& const_reference; typedef vector<T> row; typedef vector<row>...
4
by: Yudan Yi | last post by:
I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
0
by: Zoran Stipanicev | last post by:
Hi! The question is product of laziness. I want to write generic operators for matrix computation which would adapt to different types of matrices i.e. when adding square matrix and lower...
2
by: Stipanicev | last post by:
I want to write generic operators for matrix computation which would adapt to different types (shapes) of matrices i.e. when adding square matrix and lower triangle matrix it would add only...
7
by: check.checkta | last post by:
Hi, I'd like to implement a simple matrix class. I'd like to overload operator so that it returns as a vector (either the stl vector or some other Vector class of my own). The reason I want...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.