Following the question in Pointer to portions of array, a structure that does operations with portions of an array was proposed.
I would like to request one further question within this issue.
I would like to create a structure for blockMatrices using std::vector
and would require to change the implementation of the structure for getting a 3x3 matrix out of a 4x4 matrix.
The current test case is:
#include <vector>
#include <array>
#include <iostream>
// define matrix 4x4
typedef std::array<double, 16> matrix4;
// define matrix 3x3
typedef std::array<double, 9> matrix3;
// get 3x3 matrix out of a 4x4 matrix
struct subMat
{
matrix4& matrix_;
const double& operator[](size_t index) const
{
static size_t mapping[] = {0, 1, 2, 4, 5, 6, 8, 9, 10};
return matrix_[mapping[index]];
}
subMat (matrix4& A): matrix_(A){}
};
template <typename T>
double sum_of_elements(const T& arr)
{
double res = 0;
for (int i=0;i < 9; ++i)
{
res += arr[i];
}
return res;
}
int main(int argCount, char *args[])
{
std::vector<matrix4> myBlockMatrix(5);
for (int i=0; i < myBlockMatrix.size(); i++)
{
for (int j = 0; j<myBlockMatrix[0].size(); j++)
{
myBlockMatrix[i][j] = i*j;
}
}
for (int i = 0; i<myBlockMatrix.size(); i++)
{
std::cout << sum_of_elements(subMat(myBlockMatrix[i])) << std::endl; // this works
}
subBlockMatrix subBlock (myBlockMatrix);
for (int i = 0; i<myBlockMatrix.size(); i++)
{
std::cout << sum_of_elements(subBlock[i])) << std::endl;
}
return 0;
}
For overloading the []
operator, I have:
struct subBlockMatrix : std::vector<matrix4>
{
std::vector<matrix4>& blockMatrix_;
const matrix4& operator[](std::size_t index) const
{
static size_t mapping[] = {0, 1, 2, 4, 5, 6, 8, 9, 10};
return blockMatrix_[mapping[index]];
}
subBlockMatrix(std::vector<matrix4>& A) : blockMatrix_(A) {}
};
But this does not work... I am having difficulty understanding how to make it work and would really appreciate the help!
Best Regards
Aucun commentaire:
Enregistrer un commentaire