I am relatively new to C++ and am building a matrix class for carrying out with it common linear algebra operations. Some snippet of the hpp
:
template<const int rows, const int columns, typename T=float>
struct Matrix3D
{
const int shape[2] = {rows, columns};
float content[rows][columns]{};
Matrix3D()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
this->content[i][j] = 0.;
}
}
}
explicit Matrix3D(const T (&arr)[rows][columns])
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
this->content[i][j] = arr[i][j];
}
}
}
bool operator==( const Matrix3D<rows, columns> &mat ) const noexcept
{
int equal = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
equal += this->content[i][j] == mat.content[i][j];
}
}
return equal == rows * columns;
}
bool operator!=( const Matrix3D<rows, columns> &mat ) const noexcept
{
int equal = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
equal += this->content[i][j] == mat.content[i][j];
}
}
return equal != rows * columns;
}
...
};
You get the idea: for pretty much all the operations that I will need to manipulate that matrix object I am going to need these double loops in every method.
Is there a way to abstract that, for example, to a static
method, and pass the core looping expression as argument? It is worth noting that my solution must be compatible with CUDA
, meaning I cannot use std::vector
, or Eigen
, or similar.
Thanks in advance.