I want to create a small library for matrices. But I want to be fast: (no OpenMP/OpenCL)
- After testing 'virtual getValue' is 5 times slower from inline.
- virtual is not compatible with SIMD technology compiler optimizations.
So I decide to make classes with no virtual members.
I want to create these classes:
DenseMatrix, DenseSummetricalMatrix (which serves also DenseUpperTriangularMatrix/DenseLowerTriangularMatrix), DenseRowMatrix (which serves DenseColumnMatrix/DenseDiagonalMatrix).
Because each of these classes must have implemented operations for any of others, code becomes super-bloated:
1 2 3 4 5 6 7 8
|
struct DenseMatrix // ONLY FOR MUL!!!
{
DenseMatrix operator*(DenseMatrix &m) { ... }
DenseMatrix operator*(DenseSymmetricalMatrix &m) { ... }
DenseMatrix operator*(DenseDiagonalMatrix &m) { ... }
DenseColumnMatrix operator*(DenseColumnMatrix &m) { ... }
.......
};
|
I am familiar with basics of templates, but not expert.
Can I implement this with templates? (template specialization is in my mind).
Can you please write the following code with templates and without anything virtual? I write it to contain many difficult cases (for me off-course).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
struct DenseMatrixBase
{
virtual int rows() = 0;
int columns() { return width; }
int width;
virtual double getValue(int x, int y) = 0;
virtual void setValue(int x, int y, double val) = 0;
DenseMatrixBase operator*(DenseMatrixBase &m) { ... }
DenseMatrixBase operator+(DenseMatrixBase &m) { ... }
........
};
class DenseMatrix : public DenseMatrixBase
{
virtual int rows() { return height; }
int height;
virtual double getValue(int x, int y) { ... }
virtual void setValue(int x, int y, double val) { ... }
};
class DenseSymmetricalMatrix : public DenseMatrixBase
{
virtual int rows() { return columns(); }
virtual double getValue(int x, int y) { ... }
virtual void setValue(int x, int y, double val) { ... }
};
|