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, 3);

Thanks, Ravi.

Answers


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.


Need Your Help

How do I run libsvm on linux?

linux unix makefile libsvm svm

I'm having trouble installing libsvm, presently im running via a ubuntu virtual machine . When I follow the instructions such that on Unix systems, "type make' to build thesvm-train' and `svm-predict'

how to make text transparent without changing to an image using css

css css3

I have a navigation bar that tells the user which page is selected by a change in text colour and a black background. I want to make only the text inside the current page nav transparent.