开发者

How to create a array of boost matrices?

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

None of the following worked.

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, 开发者_JS百科3);

Thanks, Ravi.


The size you initialize it with 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 comes with initializing the array. You can't do this:

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

Instead, you have to let them default-construct, then resize them all:

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

Since you're using boost, consider using boost::array, since it gives a nicer 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));
}


Array initialization use the default constructor. You can use a vector instead:

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


It's not clear to me exactly what you're trying to initialize, but taking a guess (array with arraySize entries; each entry in the array is initialized with (1, 3)), I came up with this, which 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)
};


How 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 std::tr1::array template is a (very) thin wrapper around basic arrays that offers convenience functions. For example, here I’ve used assign(), which fills the entire array with a single value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜