Registered Member
|
Hi,
I am using the templated versions of the Matrix class (e.g. Matrix<double, K, K>), because in the majority of cases K will be rather small, and I read that these are much faster than the general classes. However, the actual value of K depends on user input. This causes a lot of issues, because most of my code became templated on K, and as a consequence I can't dynamically create a correct object. Currently I came up with a nasty switch statement to create matrices: switch(K) { case 1: //create Matrix<double, 1, 1> case 2: //create Matrix<double, 2, 2> //etc.. default: throw "number too big"; } but this doesn't really feel satisfying. In my code I actually copy the contents of the result matrix to my own container to avoid the dependence on K. (1) Is there a better way to create these classes? (2) Is it reasonable to use templated code only to create these matrices, and then refer to them using some base class---so that I could get rid of the template parameter on most of my classes? (3) Can I create a non-templated matrix as the default for "big" K? What threshold would you use? Kind regards, Grzegorz |
Moderator
|
Your question is not very clear. If your matrices have varying sizes, then the best is to start with dynamic sized matrices. Then if the performance are not sufficient and that matrices appear to be small most of the time (let's say < 8 to 16 depending of your algorithm) then it is a good idea to make your time consuming algorithm exploit fixed size objects. In this case, it's not the creation that must be templated but only the most time consuming part of your algorithm. And then, of course you have no choice than doing a big switch like:
switch(K) { case 1: func<K>(arg0, arg1, ...); break; ... default: func<Dynamic>(arg0, arg1, ...); break; }; An alternative to the switch is to fill an array of function pointers or functors: if(K<MAXK) funcs[K](arg0, arg1, ...); else func<Dynamic>(arg0, arg1, ...); break; |
Registered Member
|
Hi,
In my case I just know that the matrices will be very small (with dimensions <=4 in possibly all cases), so I thought that using fixed sized matrices from start is a good idea. I guess my question is whether there is a common interface for all the matrix classes, and if performance is impacted when mixing fixed sized and dynamic matrices. Where could I find an example of "only the most time consuming part of your algorithm [must be templated]"? Possibly that is the design pattern I am looking for. Thanks for your reply, Grzegorz |
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]