Object oriented programming issue

Started by
10 comments, last by Misery 11 years, 9 months ago
I don't think you need or even should do everything for general matrices -- in general, don't do things that don't make sense in linear algebra.
For instance, for multiplication, operator*, you only need to concern yourself with the conformable ones.
For addition, operator+ (and subtraction, operator-), it only makes sense to look at ones of the same size. So, only implement the code for the right size, as in here: http://www.drdobbs.c...lates/184401656
// Notice that there are two ways to check for the right size -- if you encode the size in your type (for fixed-size vectors/arrays -- for instance like it's done in std::array<T,N>) you only accept the right-sized (N elements) arguments and thus you're checking at compile-time; OTOH, if you store the size (for resizable vectors/arrays -- for instance, like it's done in std::vector<T>) you have to assert on entrance to the operator (or just crash if that's your preference). All of these are choices and have various trade-offs involved :-)

You may consider how it's done in the other libraries -- a prominent example is Eigen (very fast):
http://eigen.tuxfami...EigenTypes.html
http://eigen.tuxfami...Evaluation.html
http://eigen.tuxfami...ralProduct.html
http://eigen.tuxfami...ionXprTemplates
It's open-source, so you can look at the code, but, again -- this is a state-of-the-art, highly-optimized library, so it may not be a good idea if you're still learning (it also uses plenty of SIMD extensions and, thus, intrinsics).

Perhaps consider looking at some of these resources:
http://www.issi.uned...isCzarnecki.pdf -- Section 10.3.7.1 provides sample code with discussion (and introduction in preceding sections)
http://dlib.net/matr...ons_ex.cpp.html // expression templates (ET) are thoroughly discussed in the comments -- you can click on the header files to follow the source code
http://tvmet.sourceforge.net/ // "This Tiny Vector and Matrix template library uses Meta Templates (MT) and Expression Templates (ET) to evaluate results at compile time -- which makes it fast for low order (tiny) systems."
http://www.chem.utor...ode/vecmat3v1.h
http://arxiv.org/abs/1104.1729v1 // in particular, "DMatDMatMultExpr"; note, that they propose a variation upon ET, called "smart" ET, which may or may not be what you need -- tellingly, they don't include Eigen in their benchmarks, but Boost.uBLAS (which is much slower than Eigen) and Blitz++ (which hasn't been maintained for years)
http://www10.informa...echRep09-17.pdf // again, this is also from authors "selling" the "smart" ET approach, so expect some criticism of ET -- but may still be useful; in their case they make use of the fact that a matrix-vector product is again a vector and make use of/express this fact as follows:
class DMatDVecMultExpr : public DenseVector < DMatDVecMultExpr <MT,VT> >, private Expression
- similarly, matrix-matrix product is a matrix:
class DMatDMatMultExpr : public DenseMatrix < DMatDMatMultExpr <MT1,MT2> >, private Expression

In general, rely on what you know from linear algebra and don't try to make anything too weird/fancy -- and you should be fine! :-)
Advertisement
Thanks a lot Gyus! :]

This topic is closed to new replies.

Advertisement