Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Improve accuracy of polytopes.regular_polygon()
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Oct 11, 2014
1 parent 6996fd8 commit 0001941
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/sage/geometry/polyhedron/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3605,7 +3605,7 @@ def volume(self, engine='auto', **kwds):
2.37764129...
sage: P5 = polytopes.regular_polygon(5, base_ring=QQ)
sage: P5.volume() # rational approximation
3387471714099766473500515673753476175274812279494567801326487870013/1424719417220622426561086640229666223984528142237277803327699435400
143675742936485206271005807482349119225365261915467953640852591/60427846494832899490396166935397049960830782710733164218307960
sage: _.n()
2.37764129...
Expand Down
28 changes: 21 additions & 7 deletions src/sage/geometry/polyhedron/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
########################################################################


from sage.rings.all import Integer, QQ, ZZ, RDF
from sage.rings.all import Integer, RR, QQ, ZZ, RDF
from sage.matrix.constructor import matrix
from sage.modules.free_module_element import vector
from sage.combinat.permutation import Permutations
Expand Down Expand Up @@ -121,27 +121,41 @@ def _pfunc(i,j,perm):
@rename_keyword(deprecation=11634, field='base_ring')
def regular_polygon(self, n, base_ring=QQ):
"""
Return a regular polygon with n vertices. Over the rational
Return a regular polygon with `n` vertices. Over the rational
field the vertices may not be exact.
INPUT:
- ``n`` -- a positive integer, the number of vertices.
- ``field`` -- either ``QQ`` or ``RDF``.
- ``base_ring`` -- a ring in which the coordinates will lie.
EXAMPLES::
sage: octagon = polytopes.regular_polygon(8)
sage: len(octagon.vertices())
8
sage: polytopes.regular_polygon(3).vertices()
(A vertex at (-125283617/144665060, -500399958596723/1000799917193445),
A vertex at (0, 1),
A vertex at (94875313/109552575, -1000799917193444/2001599834386889))
sage: polytopes.regular_polygon(3, base_ring=RealField(100)).vertices()
(A vertex at (0.00000000000000000000000000000, 1.0000000000000000000000000000),
A vertex at (0.86602540378443864676372317075, -0.50000000000000000000000000000),
A vertex at (-0.86602540378443864676372317076, -0.50000000000000000000000000000))
sage: polytopes.regular_polygon(3, base_ring=RealField(10)).vertices()
(A vertex at (0.00, 1.0),
A vertex at (0.87, -0.50),
A vertex at (-0.86, -0.50))
"""
npi = 3.14159265359
try:
omega = 2*base_ring.pi()/n
except AttributeError:
omega = 2*RR.pi()/n
verts = []
for i in range(n):
t = 2*npi*i/n
verts.append([sin(t),cos(t)])
verts = [[base_ring(RDF(x)) for x in y] for y in verts]
t = omega*i
verts.append([base_ring(t.sin()), base_ring(t.cos())])
return Polyhedron(vertices=verts, base_ring=base_ring)


Expand Down

0 comments on commit 0001941

Please sign in to comment.