4

Is it possible to create a c_vector<int, 3> with the values [12 398 -34] (as an example), in a single line?

As far as I can see the only viable constructor is:

c_vector(vector_expression<AE> const&)

which takes a VectorExpression, which seem to be all the other kind of vectors, like zero_vector and scalar_vector that are dynamically allocated.

Is there something like a std::initializer_list<T> constructor that I can use? Or what VectorExpression should I use for this simple task?

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 1
    http://stackoverflow.com/q/2742549 – Robert Harvey May 17 '15 at 00:48
  • @RobertHarvery Erm. Sorry, no. This is not a duplicate of that question. `std::vector` and `boost::numeric::ublas::c_vector` are entirely different beasts. Yes, the answers there make use of Boost.Assign which might or may not be relevant here, but the question is really regarding what `VectorExpression` can be used here. Good to see you though. – Shoe May 17 '15 at 00:49
  • @RobertHarvey It has literally the exact same problems as the one you closed the question with. And I've highlighted them in my previous comment. – Shoe May 17 '15 at 00:50
  • How come you're not using std::vector? Seems like it has a leg up here. – Robert Harvey May 17 '15 at 00:51
  • Have you had a look here? http://www.boost.org/doc/libs/1_35_0/libs/assign/doc/index.html – Robert Harvey May 17 '15 at 00:52
  • @RobertHarvey 1) `std::vector` is dynamically allocated. 2) `boost::numeric::ublas::c_vector` is a vector in the mathematical sense. Specifically it allows vector addition, cross product, and all that fun mathy stuff. – Shoe May 17 '15 at 00:52
  • Give it another 30 seconds or so, and this is going to be less trouble to just write as a two-liner. And probably more standard. – Robert Harvey May 17 '15 at 00:53
  • @RobertHarvey Yes, I've browser through Boost.Assign, and I haven't noticed anything related to `c_vector` or similar. The problem here is that the constructor accepts a `VectorExpression`, which seems to be something very specific to Boost.uBLAS. – Shoe May 17 '15 at 00:55
  • Have you tried implementing a vector expression that can be constructed (maybe from a helper function) from an initializer list? http://www.boost.org/doc/libs/1_50_0/libs/numeric/ublas/doc/expression_concept.htm#2VectorExpression Boring boilerplate, but should work, no? – Yakk - Adam Nevraumont May 21 '15 at 15:14

1 Answers1

2

I stumbled upon this in the assignment.hpp header:

v <<= 1,2,3;

I suppose there might be more useful methods in there, if you look closely. E.g.

vector<double> a(6, 0);
a <<= 1, 2, move_to(5), 3;

will result in: 1 2 0 0 0 3 and

matrix<double> A(3, 3, 0);
A <<= 1, 2, next_row(),
3, 4, begin1(), 1;

will result in:

1 2 1
3 4 0
0 0 0

Live On Coliru

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/vector_expression.hpp>
#include <boost/numeric/ublas/traits/c_array.hpp>
#include <boost/numeric/ublas/assignment.hpp>

namespace ublas = boost::numeric::ublas;

int main() {
    using V = ublas::c_vector<int, 3>;

    V v;
    v <<= 1,2,3;

    for (auto i : v)
        std::cout << i << " ";
}

Prints

1 2 3 
sehe
  • 374,641
  • 47
  • 450
  • 633
  • I see. My problem was with construction more than anything though (being able to construct a vector with those values). – Shoe May 17 '15 at 11:02