ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 28 Jun 2017 12:56:33 -0500Vertices of Polytopehttp://ask.sagemath.org/question/38104/vertices-of-polytope/ Hi!
I have a list of points that I'm using to construct a convex, finite-sided, compact polytope:
P1 = Polyhedron(vertices = [list_of_points])
I know how to extract from P1 the list of vertices that define it:
vertex_list=[]
for q in P1.Vrepresentation():
vertex_list.append(q)
print('vertices ='), vertex_list
This returns the following output:
vertices = [A vertex at (0.5, 0.0), A vertex at (-0.5, 0.0), A vertex at (-0.5, 1.0), A vertex at (0.5, -1.0)]
But what I'd like to do is have a **list of the coordinates of the vertices only**: i.e have sage tell me a list of the form [(.5,0), (-.5, 0), (-.5, 1), (.5, -1)], so that I can do some computations about P1 that are dependent on the coordinates of the vertices. Is there a way to do this?
Thanks!Tue, 27 Jun 2017 15:15:34 -0500http://ask.sagemath.org/question/38104/vertices-of-polytope/Answer by dan_fulea for <p>Hi!</p>
<p>I have a list of points that I'm using to construct a convex, finite-sided, compact polytope:</p>
<pre><code>P1 = Polyhedron(vertices = [list_of_points])
</code></pre>
<p>I know how to extract from P1 the list of vertices that define it:</p>
<pre><code>vertex_list=[]
for q in P1.Vrepresentation():
vertex_list.append(q)
print('vertices ='), vertex_list
</code></pre>
<p>This returns the following output:</p>
<pre><code>vertices = [A vertex at (0.5, 0.0), A vertex at (-0.5, 0.0), A vertex at (-0.5, 1.0), A vertex at (0.5, -1.0)]
</code></pre>
<p>But what I'd like to do is have a <strong>list of the coordinates of the vertices only</strong>: i.e have sage tell me a list of the form [(.5,0), (-.5, 0), (-.5, 1), (.5, -1)], so that I can do some computations about P1 that are dependent on the coordinates of the vertices. Is there a way to do this? </p>
<p>Thanks!</p>
http://ask.sagemath.org/question/38104/vertices-of-polytope/?answer=38108#post-id-38108The following code gets the (3D) vertices as list, starting from a special polyhedron:
[ v.vector().list() for v in t.vertices() ]
For this, let us compare...
sage: t = polytopes.tetrahedron()
sage: t.vertices()
(A vertex at (0, 0, 0),
A vertex at (0, 1, 1),
A vertex at (1, 0, 1),
A vertex at (1, 1, 0))
sage: [ v.vector() for v in t.vertices() ]
[(0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 0)]
sage: [ v.vector().list() for v in t.vertices() ]
[[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]]
sage: v = t.vertices()[0]
sage: print "v has type", type(v)
v has type <class 'sage.geometry.polyhedron.representation.Vertex'>
sage: print "v.vector() has type", type( v.vector() )
v.vector() has type <type 'sage.modules.vector_integer_dense.Vector_integer_dense'>
sage: print "v.vector().list() has type", type( v.vector().list() )
v.vector().list() has type <type 'list'>
It is always useful to look at one element, a vertex, instantiated as a variable `v` above, then to inspect its methods. Very often, the name will already be enough to guess the right needed method. Note that the vector representation of the vertices may also be enough.
Wed, 28 Jun 2017 06:57:42 -0500http://ask.sagemath.org/question/38104/vertices-of-polytope/?answer=38108#post-id-38108Comment by krishna for <p>The following code gets the (3D) vertices as list, starting from a special polyhedron:</p>
<pre><code>[ v.vector().list() for v in t.vertices() ]
</code></pre>
<p>For this, let us compare...</p>
<pre><code>sage: t = polytopes.tetrahedron()
sage: t.vertices()
(A vertex at (0, 0, 0),
A vertex at (0, 1, 1),
A vertex at (1, 0, 1),
A vertex at (1, 1, 0))
sage: [ v.vector() for v in t.vertices() ]
[(0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 0)]
sage: [ v.vector().list() for v in t.vertices() ]
[[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]]
sage: v = t.vertices()[0]
sage: print "v has type", type(v)
v has type <class 'sage.geometry.polyhedron.representation.Vertex'>
sage: print "v.vector() has type", type( v.vector() )
v.vector() has type <type 'sage.modules.vector_integer_dense.Vector_integer_dense'>
sage: print "v.vector().list() has type", type( v.vector().list() )
v.vector().list() has type <type 'list'>
</code></pre>
<p>It is always useful to look at one element, a vertex, instantiated as a variable <code>v</code> above, then to inspect its methods. Very often, the name will already be enough to guess the right needed method. Note that the vector representation of the vertices may also be enough. </p>
http://ask.sagemath.org/question/38104/vertices-of-polytope/?comment=38120#post-id-38120This is very helpful, thanks for your detailed response!Wed, 28 Jun 2017 12:56:33 -0500http://ask.sagemath.org/question/38104/vertices-of-polytope/?comment=38120#post-id-38120Answer by mforets for <p>Hi!</p>
<p>I have a list of points that I'm using to construct a convex, finite-sided, compact polytope:</p>
<pre><code>P1 = Polyhedron(vertices = [list_of_points])
</code></pre>
<p>I know how to extract from P1 the list of vertices that define it:</p>
<pre><code>vertex_list=[]
for q in P1.Vrepresentation():
vertex_list.append(q)
print('vertices ='), vertex_list
</code></pre>
<p>This returns the following output:</p>
<pre><code>vertices = [A vertex at (0.5, 0.0), A vertex at (-0.5, 0.0), A vertex at (-0.5, 1.0), A vertex at (0.5, -1.0)]
</code></pre>
<p>But what I'd like to do is have a <strong>list of the coordinates of the vertices only</strong>: i.e have sage tell me a list of the form [(.5,0), (-.5, 0), (-.5, 1), (.5, -1)], so that I can do some computations about P1 that are dependent on the coordinates of the vertices. Is there a way to do this? </p>
<p>Thanks!</p>
http://ask.sagemath.org/question/38104/vertices-of-polytope/?answer=38109#post-id-38109Hint: use tab-completion, `P.vert[TAB]`.
---
The method [P.vertices_list()](http://doc.sagemath.org/html/en/reference/discrete_geometry/sage/geometry/polyhedron/base.html#sage.geometry.polyhedron.base.Polyhedron_base.vertices_list) returns a list of vertices of the polyhedron, and [P.vertex_generator()](http://doc.sagemath.org/html/en/reference/discrete_geometry/sage/geometry/polyhedron/base.html#sage.geometry.polyhedron.base.Polyhedron_base.vertex_generator) allows to conveniently iterate over them.Wed, 28 Jun 2017 07:03:03 -0500http://ask.sagemath.org/question/38104/vertices-of-polytope/?answer=38109#post-id-38109