Is there a single-expression way to assign a scalar to all elements of a boost matrix or vector? I'm trying to find a more compact way of representing:
boost::numeric::ublas::c_vector<float, N> v;
for (size_t i=0; i<N; i++) {
v[i] = myScalar;
}
The following do not work:
boost::numeric::ublas::c_vector<float, N>
v(myScalar, myScalar, ...and so on..., myScalar);
boost::numeric::ublas::c_vector<float, N> v;
v = myScalar;
The recommended way looks like this:
The same is for matrix types:
Have you tried this?
ublas::c_vector v = ublas::scalar_vector(N, myScalar);
Been a while since I used C++. Does the following work?
I have started using
boost::assign
for cases that I want to statically assign specific values (examples lifted from link above).You can also use
boost::assign
for maps.You can allow do direct assignment with
list_of()
andmap_list_of()
There are also functions for
repeat()
,repeat_fun()
, andrange()
which allows you to add repeating values or ranges of values.Because the vector models a standard random access container you should be able to use the standard STL algorithms. Something like:
or
It probably also is compatible with Boost.Assign but you'd have to check.