How to create an array of superscripts?

How can I define an array of boost matrices as a member variable?

None of the following work.

boost::numeric::ublas::matrix<double> arrayM(1, 3)[arraySize];
boost::numeric::ublas::matrix<double>(1, 3) arrayM[arraySize];
boost::numeric::ublas::matrix<double> arrayM[arraySize](1, 3);

Thanks, Ravi.

+3
source share
4 answers

The size you initialize has nothing to do with the type. Therefore:

// this makes things easier!
typedef boost::numeric::ublas::matrix<double> matrix_type;

// this is the type (no initialization)
matrix_type arrayM[arraySize];

The problem is array initialization. You cannot do this:

TheClass::TheClass() :
arrayM(1, 3) // nope
{}

Instead, you should allow them to build by default, and then resize them:

TheClass::TheClass()
{
    std::fill(arrayM, arrayM + arraySize, matrix_type(1, 3));
}

Since you are using boost, consider using boost::arrayit as it gives a better syntax:

typedef boost::numeric::ublas::matrix<double> matrix_type;
typedef boost::array<matrix_type, arraySize> matrix_array;

matrix_array arrayM; // ah

TheClass::TheClass()
{
    arrayM.assign(matrix_type(1, 3));
}
+3
source

. :

class MyClass {
    std::vector<boost::numeric::ublas::matrix<double>> vectorM;
public:
    MyClass() : vectorM(10, boost::numeric::ublas::matrix<double>(5,7)) {
    }
};
+3

It’s not clear to me that you are trying to initialize, but accept fortunetelling (an array with arraySize entries, each entry in the array is initialized with (1, 3)), I came up with this one that compiles ....

const size_t arraySize = 3;
boost::numeric::ublas::matrix<double> arrayM[arraySize] = 
{
    boost::numeric::ublas::matrix<double>(1, 3),
    boost::numeric::ublas::matrix<double>(1, 3),
    boost::numeric::ublas::matrix<double>(1, 3)
};
+3
source

What about:

// Assume: arraySize is a constant
// Assume: #include <boost/tr1/array.hpp>

typedef boost::numeric::ublas::matrix<double> doubleMatrixT;
std::tr1::array<doubleMatrixT, arraySize> arrayM;
arrayM.assign(doubleMatrixT(1, 3));

The template std::tr1::arrayis a (very) thin shell around the main arrays, which offers convenient functions. For example, here Ive used assign(), which fills the entire array with a single value.

+1
source

Source: https://habr.com/ru/post/1738250/


All Articles