Page 1 of 1

Callable library question: How to truncate a polymake::Vector?

Posted: 04 Sep 2015, 13:54
by mws
I am using the callable library of Polymake 2.14.

Let's say I define a vector of size 4 in my C++ program, i.e.,

Code: Select all

polymake::Vector<Rational> vec {4};
After doing some computations I need to discard the last element. If vec would be of type std::vector, I'd just use .pop_back(), but this doesn't seem to work. What's the "right" way to do it?

Re: Callable library question: How to truncate a polymake::Vector?

Posted: 07 Sep 2015, 10:41
by paffenholz
You can use the "slice" method for this. There are two variants

Code: Select all

vector.slice(<Set>); vector.slice(a,b);
The first requires a (Generic)Set giving a valid set of indices of the vector, the second cuts out a continuous part of length b of the vector, starting at index a. Also here, a+b-1 must still be a valid index. If you put b=0, then you get the slice from a to the end of the vector. In this case you can also omit b (0 is the default for b).

Andreas

Re: Callable library question: How to truncate a polymake::Vector?

Posted: 07 Sep 2015, 14:31
by gawrilow
Suggestions made by Andreas would produce a new object, the initial vector would not change. I guess, the original question was more aiming at a mutating method. For that, the answer is resize().

Re: Callable library question: How to truncate a polymake::Vector?

Posted: 07 Sep 2015, 21:32
by mws
Yes, I am interested in changing the existing vector. So

Code: Select all

vec.resize(n)
keeps the first n elements and discards the rest?

Re: Callable library question: How to truncate a polymake::Vector?

Posted: 07 Sep 2015, 22:07
by gawrilow
Exactly.