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

Questions and problems about using polymake go here.
mws
Posts: 32
Joined: 16 Jan 2014, 22:57

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

Postby mws » 04 Sep 2015, 13:54

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?

paffenholz
Developer
Posts: 211
Joined: 24 Dec 2010, 13:47

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

Postby paffenholz » 07 Sep 2015, 10:41

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

User avatar
gawrilow
Main Author
Posts: 423
Joined: 25 Dec 2010, 17:40

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

Postby gawrilow » 07 Sep 2015, 14:31

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().

mws
Posts: 32
Joined: 16 Jan 2014, 22:57

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

Postby mws » 07 Sep 2015, 21:32

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?

User avatar
gawrilow
Main Author
Posts: 423
Joined: 25 Dec 2010, 17:40

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

Postby gawrilow » 07 Sep 2015, 22:07

Exactly.


Return to “Helpdesk”