4

How can I create a const boost matrix?

The following did not work:

const boost::numeric::ublas::matrix<double> arrayM(1, 3) = { {1.0, 2.0, 3.0} };
2
  • It doesn't work because you're declaring a const matrix and then attempting to assign values to it using a mutable assignment operator. GMan's solution below will do what you need. Commented Apr 2, 2010 at 22:48
  • I should have been clear about my question: Is there a way to create it in a similar way we create a const array? For example: int billy [5] = { 16, 2, 77, 40, 12071 }; Commented Apr 2, 2010 at 22:51

1 Answer 1

8

Usually something akin to:

typedef boost::numeric::ublas::matrix<double> matrix_type;

const matrix_type get_matrix(void)
{
    matrix_type result(1, 3);
    result(0, 0) = 1;
    result(0, 1) = 2;
    result(0, 2) = 3;

    return result;
}

const matrix_type arrayM = get_matrix();

You might also try something like this (mostly untested):

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

template <typename T, typename L = boost::numeric::ublas::row_major,
            typename A = boost::numeric::ublas::unbounded_array<T> >
class matrix_builder
{
public:
    // types
    typedef boost::numeric::ublas::matrix<T, L, A> matrix_type;
    typedef typename matrix_type::size_type size_type;

    // creation
    matrix_builder(size_type pRows, size_type pColumns) :
    mMatrix(pRows, pColumns),
    mRow(0),
    mColumn(0)
    {}

    matrix_builder& operator()(const T& pValue)
    {
        mMatrix(mRow, mColumn) = pValue;
        if (++mColumn == mMatrix.size2())
        {
            mColumn = 0;
            mRow++;
        }

        return *this;
    }

    // access
    operator const matrix_type&(void) const
    {
        return mMatrix;
    }

private:
    // non copyable
    matrix_builder(const matrix_builder&);
    matrix_builder& operator=(const matrix_builder&);

    // members
    matrix_type mMatrix;
    size_type mRow;
    size_type mColumn;
};

typedef boost::numeric::ublas::matrix<double> matrix_type;

static const matrix_type m1 = matrix_builder<double>(3, 1)
                                (1)(2)(3);

static const matrix_type m2 = matrix_builder<double>(3, 3)
                                (1)(2)(3)
                                (4)(5)(6)
                                (7)(8)(9);

int main(void)
{
    std::cout << m1 << std::endl;
    std::cout << m2 << std::endl;
}

Same idea, more generic. Also a bit more visual, which can be nice.

Sign up to request clarification or add additional context in comments.

3 Comments

Though this works, I will have tor create a function for every const matrix type I need to create.
@Venkata: I'll write a more generic way now.
@Stacked: Thanks. I admit it could probably be done a bit better, but this should work for any case I can think of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.