ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 23 Apr 2020 02:48:39 -0500Compute the centroid of a polytopehttps://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/Does SAGE have a way to compute the center of mass of a polytope? I tried using polymake's centroid method, but that returned an error because the dimension of my polytope is not equal to the ambient dimension. I've read the manual and looked at the available functions but can't seem to find a SAGE command to do this. Does it exist?
EDIT: By request here's some sample _polymake_ code that fails (I'm not sure why this would be useful unless SAGE is using polymake under the hood):
polytope > $q=permutahedron(3);
polytope > print $q->CENTROID;
polymake: WARNING: could not compute 'CENTROID' probably because of unsatisfied preconditions:
precondition : DIM, AMBIENT_DIM ( CENTROID, VOLUME : VERTICES, TRIANGULATION.FACETS )
I'm guessing the error occurs because the ambient dimension of this polytope is 4 but the actual dimension of the polytope is 3; this is the same reason polymake tells me the volume of q is 0.Wed, 27 Apr 2011 08:50:26 -0500https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/Comment by cswiercz for <p>Does SAGE have a way to compute the center of mass of a polytope? I tried using polymake's centroid method, but that returned an error because the dimension of my polytope is not equal to the ambient dimension. I've read the manual and looked at the available functions but can't seem to find a SAGE command to do this. Does it exist?</p>
<p>EDIT: By request here's some sample _polymake_ code that fails (I'm not sure why this would be useful unless SAGE is using polymake under the hood):</p>
<p>polytope > $q=permutahedron(3);</p>
<p>polytope > print $q->CENTROID;</p>
<p>polymake: WARNING: could not compute 'CENTROID' probably because of unsatisfied preconditions:</p>
<p>precondition : DIM, AMBIENT_DIM ( CENTROID, VOLUME : VERTICES, TRIANGULATION.FACETS )</p>
<p>I'm guessing the error occurs because the ambient dimension of this polytope is 4 but the actual dimension of the polytope is 3; this is the same reason polymake tells me the volume of q is 0.</p>
https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=21768#post-id-21768My apologies. I hadn't read your question carefully enough to realize that you hadn't attempted to write any Sage code to compute the centroid.Sat, 30 Apr 2011 08:40:15 -0500https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=21768#post-id-21768Comment by cswiercz for <p>Does SAGE have a way to compute the center of mass of a polytope? I tried using polymake's centroid method, but that returned an error because the dimension of my polytope is not equal to the ambient dimension. I've read the manual and looked at the available functions but can't seem to find a SAGE command to do this. Does it exist?</p>
<p>EDIT: By request here's some sample _polymake_ code that fails (I'm not sure why this would be useful unless SAGE is using polymake under the hood):</p>
<p>polytope > $q=permutahedron(3);</p>
<p>polytope > print $q->CENTROID;</p>
<p>polymake: WARNING: could not compute 'CENTROID' probably because of unsatisfied preconditions:</p>
<p>precondition : DIM, AMBIENT_DIM ( CENTROID, VOLUME : VERTICES, TRIANGULATION.FACETS )</p>
<p>I'm guessing the error occurs because the ambient dimension of this polytope is 4 but the actual dimension of the polytope is 3; this is the same reason polymake tells me the volume of q is 0.</p>
https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=21784#post-id-21784Could you post some example code? It's easier to debug why a command wouldn't work.Wed, 27 Apr 2011 10:16:54 -0500https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=21784#post-id-21784Answer by Volker Braun for <p>Does SAGE have a way to compute the center of mass of a polytope? I tried using polymake's centroid method, but that returned an error because the dimension of my polytope is not equal to the ambient dimension. I've read the manual and looked at the available functions but can't seem to find a SAGE command to do this. Does it exist?</p>
<p>EDIT: By request here's some sample _polymake_ code that fails (I'm not sure why this would be useful unless SAGE is using polymake under the hood):</p>
<p>polytope > $q=permutahedron(3);</p>
<p>polytope > print $q->CENTROID;</p>
<p>polymake: WARNING: could not compute 'CENTROID' probably because of unsatisfied preconditions:</p>
<p>precondition : DIM, AMBIENT_DIM ( CENTROID, VOLUME : VERTICES, TRIANGULATION.FACETS )</p>
<p>I'm guessing the error occurs because the ambient dimension of this polytope is 4 but the actual dimension of the polytope is 3; this is the same reason polymake tells me the volume of q is 0.</p>
https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?answer=12339#post-id-12339There is no such function currently implemented. However, it would not be difficult to add. The straightforward approach would be to triangulate the polytope, find the barycenter of the simplices, and then add their individual barycenters weighted by the fraction of the total mass.
If you want to contribute a centroid method make a trac ticket and cc me, I'd be happy to help.Sat, 07 May 2011 08:52:26 -0500https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?answer=12339#post-id-12339Comment by jipilab for <p>There is no such function currently implemented. However, it would not be difficult to add. The straightforward approach would be to triangulate the polytope, find the barycenter of the simplices, and then add their individual barycenters weighted by the fraction of the total mass.</p>
<p>If you want to contribute a centroid method make a trac ticket and cc me, I'd be happy to help.</p>
https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=50950#post-id-50950... which will be accessible with the command
sage: q = polytopes.permutahedron(4)
sage: q.centroid()Thu, 23 Apr 2020 02:48:39 -0500https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=50950#post-id-50950Comment by Jonathan Kliem for <p>There is no such function currently implemented. However, it would not be difficult to add. The straightforward approach would be to triangulate the polytope, find the barycenter of the simplices, and then add their individual barycenters weighted by the fraction of the total mass.</p>
<p>If you want to contribute a centroid method make a trac ticket and cc me, I'd be happy to help.</p>
https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=50390#post-id-50390I have implemented this approach in [https://trac.sagemath.org/ticket/29382](https://trac.sagemath.org/ticket/29382).Fri, 27 Mar 2020 05:45:05 -0500https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=50390#post-id-50390Answer by benjaminfjones for <p>Does SAGE have a way to compute the center of mass of a polytope? I tried using polymake's centroid method, but that returned an error because the dimension of my polytope is not equal to the ambient dimension. I've read the manual and looked at the available functions but can't seem to find a SAGE command to do this. Does it exist?</p>
<p>EDIT: By request here's some sample _polymake_ code that fails (I'm not sure why this would be useful unless SAGE is using polymake under the hood):</p>
<p>polytope > $q=permutahedron(3);</p>
<p>polytope > print $q->CENTROID;</p>
<p>polymake: WARNING: could not compute 'CENTROID' probably because of unsatisfied preconditions:</p>
<p>precondition : DIM, AMBIENT_DIM ( CENTROID, VOLUME : VERTICES, TRIANGULATION.FACETS )</p>
<p>I'm guessing the error occurs because the ambient dimension of this polytope is 4 but the actual dimension of the polytope is 3; this is the same reason polymake tells me the volume of q is 0.</p>
https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?answer=12327#post-id-12327The "center" (average of the vertices) is available via the `center` method like so:
sage: q = polytopes.permutahedron(4)
sage: q.dim()
3
sage: q.center()
(1/40000, 1/40000, 1/20000)
You can look through the other methods available in the [Polyhedra](http://www.sagemath.org/doc/reference/sage/geometry/polyhedra.html) documentation.Thu, 28 Apr 2011 19:20:19 -0500https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?answer=12327#post-id-12327Comment by adk for <p>The "center" (average of the vertices) is available via the <code>center</code> method like so:</p>
<pre><code>sage: q = polytopes.permutahedron(4)
sage: q.dim()
3
sage: q.center()
(1/40000, 1/40000, 1/20000)
</code></pre>
<p>You can look through the other methods available in the <a href="http://www.sagemath.org/doc/reference/sage/geometry/polyhedra.html">Polyhedra</a> documentation.</p>
https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=21772#post-id-21772Thanks for the suggestion, but I'm not looking for the average of the vertices; I'm looking for the center of mass. I looked through the Polyhedra documentation and was unable to find a suitable command which is why I asked this question. Most likely no such command exists, but I thought I'd check to be sure.Thu, 28 Apr 2011 21:21:06 -0500https://ask.sagemath.org/question/8092/compute-the-centroid-of-a-polytope/?comment=21772#post-id-21772