Skip to content

Commit 04fbd55

Browse files
committed
some cleaning in newton_polygon
1 parent 94baf41 commit 04fbd55

File tree

1 file changed

+82
-56
lines changed

1 file changed

+82
-56
lines changed

src/sage/geometry/newton_polygon.py

Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ def __init__(self, polyhedron, parent):
3838
3939
- ``polyhedron`` -- a polyhedron defining the Newton polygon
4040
41-
TESTS:
41+
TESTS::
4242
4343
sage: from sage.geometry.newton_polygon import NewtonPolygon
4444
sage: NewtonPolygon([ (0,0), (1,1), (3,5) ])
4545
Finite Newton polygon with 3 vertices: (0, 0), (1, 1), (3, 5)
4646
4747
sage: NewtonPolygon([ (0,0), (1,1), (2,8), (3,5) ], last_slope=3)
48-
Infinite Newton polygon with 3 vertices: (0, 0), (1, 1), (3, 5) ending by an infinite line of slope 3
48+
Infinite Newton polygon with 3 vertices: (0, 0), (1, 1), (3, 5)
49+
ending by an infinite line of slope 3
4950
5051
::
5152
@@ -57,7 +58,7 @@ def __init__(self, polyhedron, parent):
5758
if polyhedron.is_mutable():
5859
polyhedron._add_dependent_object(self)
5960

60-
def _repr_(self):
61+
def _repr_(self) -> str:
6162
"""
6263
Return a string representation of this Newton polygon.
6364
@@ -75,17 +76,16 @@ def _repr_(self):
7576
if self.last_slope() is Infinity:
7677
if length == 0:
7778
return "Empty Newton polygon"
78-
elif length == 1:
79+
if length == 1:
7980
return "Finite Newton polygon with 1 vertex: %s" % str(vertices[0])
80-
else:
81-
return "Finite Newton polygon with %s vertices: %s" % (length, str(vertices)[1:-1])
81+
return "Finite Newton polygon with %s vertices: %s" % (length, str(vertices)[1:-1])
82+
83+
if length == 1:
84+
return "Newton Polygon consisting of a unique infinite line of slope %s starting at %s" % (self.last_slope(), str(vertices[0]))
8285
else:
83-
if length == 1:
84-
return "Newton Polygon consisting of a unique infinite line of slope %s starting at %s" % (self.last_slope(), str(vertices[0]))
85-
else:
86-
return "Infinite Newton polygon with %s vertices: %s ending by an infinite line of slope %s" % (length, str(vertices)[1:-1], self.last_slope())
86+
return "Infinite Newton polygon with %s vertices: %s ending by an infinite line of slope %s" % (length, str(vertices)[1:-1], self.last_slope())
8787

88-
def vertices(self, copy=True):
88+
def vertices(self, copy=True) -> list:
8989
"""
9090
Return the list of vertices of this Newton polygon.
9191
@@ -94,7 +94,7 @@ def vertices(self, copy=True):
9494
- ``copy`` -- boolean (default: ``True``)
9595
9696
OUTPUT: the list of vertices of this Newton polygon (or a copy of it
97-
if ``copy`` is set to True)
97+
if ``copy`` is set to ``True``)
9898
9999
EXAMPLES::
100100
@@ -105,7 +105,7 @@ def vertices(self, copy=True):
105105
sage: v = NP.vertices(); v
106106
[(0, 0), (1, 1), (2, 5)]
107107
108-
TESTS:
108+
TESTS::
109109
110110
sage: del v[0]
111111
sage: v
@@ -114,7 +114,7 @@ def vertices(self, copy=True):
114114
[(0, 0), (1, 1), (2, 5)]
115115
"""
116116
if self._vertices is None:
117-
self._vertices = [ tuple(v) for v in self._polyhedron.vertices() ]
117+
self._vertices = [tuple(v) for v in self._polyhedron.vertices()]
118118
self._vertices.sort()
119119
if copy:
120120
return list(self._vertices)
@@ -152,7 +152,7 @@ def last_slope(self):
152152
return r[1]/r[0]
153153
return Infinity
154154

155-
def slopes(self, repetition=True):
155+
def slopes(self, repetition=True) -> list:
156156
"""
157157
Return the slopes of this Newton polygon.
158158
@@ -165,7 +165,7 @@ def slopes(self, repetition=True):
165165
The consecutive slopes (not including the last slope
166166
if the polygon is infinity) of this Newton polygon.
167167
168-
If ``repetition`` is True, each slope is repeated a number of
168+
If ``repetition`` is ``True``, each slope is repeated a number of
169169
times equal to its length. Otherwise, it appears only one time.
170170
171171
EXAMPLES::
@@ -180,9 +180,9 @@ def slopes(self, repetition=True):
180180
sage: NP.slopes(repetition=False)
181181
[1, 5/2]
182182
"""
183-
slopes = [ ]
183+
slopes = []
184184
vertices = self.vertices(copy=False)
185-
for i in range(1,len(vertices)):
185+
for i in range(1, len(vertices)):
186186
dx = vertices[i][0] - vertices[i-1][0]
187187
dy = vertices[i][1] - vertices[i-1][1]
188188
slope = dy/dx
@@ -200,18 +200,23 @@ def _add_(self, other):
200200
201201
- ``other`` -- a Newton polygon
202202
203-
OUTPUT: the Newton polygon, which is the convex hull of this Newton polygon and ``other``
203+
OUTPUT:
204+
205+
the Newton polygon, which is the convex hull of this Newton polygon
206+
and ``other``
204207
205208
EXAMPLES::
206209
207210
sage: from sage.geometry.newton_polygon import NewtonPolygon
208211
sage: NP1 = NewtonPolygon([ (0,0), (1,1), (2,6) ]); NP1
209212
Finite Newton polygon with 3 vertices: (0, 0), (1, 1), (2, 6)
210213
sage: NP2 = NewtonPolygon([ (0,0), (1,3/2) ], last_slope=2); NP2
211-
Infinite Newton polygon with 2 vertices: (0, 0), (1, 3/2) ending by an infinite line of slope 2
214+
Infinite Newton polygon with 2 vertices: (0, 0), (1, 3/2)
215+
ending by an infinite line of slope 2
212216
213217
sage: NP1 + NP2
214-
Infinite Newton polygon with 2 vertices: (0, 0), (1, 1) ending by an infinite line of slope 2
218+
Infinite Newton polygon with 2 vertices: (0, 0), (1, 1)
219+
ending by an infinite line of slope 2
215220
"""
216221
polyhedron = self._polyhedron.convex_hull(other._polyhedron)
217222
return self.parent()(polyhedron)
@@ -224,26 +229,32 @@ def _mul_(self, other):
224229
225230
- ``other`` -- a Newton polygon
226231
227-
OUTPUT: the Newton polygon, which is the Minkowski sum of this Newton polygon and ``other``
232+
OUTPUT:
233+
234+
the Newton polygon, which is the Minkowski sum of this Newton polygon
235+
and ``other``
228236
229237
.. NOTE::
230238
231-
If ``self`` and ``other`` are respective Newton polygons of some polynomials
232-
`f` and `g` the self*other is the Newton polygon of the product `fg`
239+
If ``self`` and ``other`` are respective Newton polygons
240+
of some polynomials `f` and `g` the self*other is the
241+
Newton polygon of the product `fg`
233242
234243
EXAMPLES::
235244
236245
sage: from sage.geometry.newton_polygon import NewtonPolygon
237246
sage: NP1 = NewtonPolygon([ (0,0), (1,1), (2,6) ]); NP1
238247
Finite Newton polygon with 3 vertices: (0, 0), (1, 1), (2, 6)
239248
sage: NP2 = NewtonPolygon([ (0,0), (1,3/2) ], last_slope=2); NP2
240-
Infinite Newton polygon with 2 vertices: (0, 0), (1, 3/2) ending by an infinite line of slope 2
249+
Infinite Newton polygon with 2 vertices: (0, 0), (1, 3/2)
250+
ending by an infinite line of slope 2
241251
242252
sage: NP = NP1 * NP2; NP
243-
Infinite Newton polygon with 3 vertices: (0, 0), (1, 1), (2, 5/2) ending by an infinite line of slope 2
253+
Infinite Newton polygon with 3 vertices: (0, 0), (1, 1), (2, 5/2)
254+
ending by an infinite line of slope 2
244255
245-
The slopes of ``NP`` is the union of those of ``NP1`` and those of ``NP2``
246-
which are less than the last slope::
256+
The slopes of ``NP`` is the union of those of ``NP1`` and
257+
those of ``NP2`` which are less than the last slope::
247258
248259
sage: NP1.slopes()
249260
[1, 5]
@@ -301,7 +312,7 @@ def __lshift__(self, i):
301312
sage: NP << 2
302313
Finite Newton polygon with 3 vertices: (0, 2), (1, 3), (2, 8)
303314
"""
304-
polyhedron = self._polyhedron.translation((0,i))
315+
polyhedron = self._polyhedron.translation((0, i))
305316
return self.parent()(polyhedron)
306317

307318
def __rshift__(self, i):
@@ -323,7 +334,7 @@ def __rshift__(self, i):
323334
sage: NP >> 2
324335
Finite Newton polygon with 3 vertices: (0, -2), (1, -1), (2, 4)
325336
"""
326-
polyhedron = self._polyhedron.translation((0,-i))
337+
polyhedron = self._polyhedron.translation((0, -i))
327338
return self.parent()(polyhedron)
328339

329340
def __call__(self, x):
@@ -368,7 +379,7 @@ def __call__(self, x):
368379
xd, yd = vertices[b]
369380
return ((x-xg)*yd + (xd-x)*yg) / (xd-xg)
370381

371-
def _richcmp_(self, other, op):
382+
def _richcmp_(self, other, op) -> bool:
372383
r"""
373384
Comparisons of two Newton polygons.
374385
@@ -457,18 +468,24 @@ def plot(self, **kwargs):
457468
if len(vertices) == 0:
458469
from sage.plot.graphics import Graphics
459470
return Graphics()
471+
472+
from sage.plot.line import line
473+
xstart, ystart = vertices[0]
474+
xend, yend = vertices[-1]
475+
if self.last_slope() is Infinity:
476+
return line([(xstart, ystart+1), (xstart, ystart+0.5)],
477+
linestyle='--', **kwargs) \
478+
+ line([(xstart, ystart+0.5)] + vertices
479+
+ [(xend, yend+0.5)], **kwargs) \
480+
+ line([(xend, yend+0.5), (xend, yend+1)],
481+
linestyle='--', **kwargs)
460482
else:
461-
from sage.plot.line import line
462-
(xstart,ystart) = vertices[0]
463-
(xend,yend) = vertices[-1]
464-
if self.last_slope() is Infinity:
465-
return line([(xstart, ystart+1), (xstart,ystart+0.5)], linestyle='--', **kwargs) \
466-
+ line([(xstart, ystart+0.5)] + vertices + [(xend, yend+0.5)], **kwargs) \
467-
+ line([(xend, yend+0.5), (xend, yend+1)], linestyle='--', **kwargs)
468-
else:
469-
return line([(xstart, ystart+1), (xstart,ystart+0.5)], linestyle='--', **kwargs) \
470-
+ line([(xstart, ystart+0.5)] + vertices + [(xend+0.5, yend + 0.5*self.last_slope())], **kwargs) \
471-
+ line([(xend+0.5, yend + 0.5*self.last_slope()), (xend+1, yend+self.last_slope())], linestyle='--', **kwargs)
483+
return line([(xstart, ystart+1), (xstart, ystart+0.5)],
484+
linestyle='--', **kwargs) \
485+
+ line([(xstart, ystart+0.5)] + vertices
486+
+ [(xend+0.5, yend + 0.5*self.last_slope())], **kwargs) \
487+
+ line([(xend+0.5, yend + 0.5*self.last_slope()), (xend+1, yend+self.last_slope())],
488+
linestyle='--', **kwargs)
472489

473490
def reverse(self, degree=None):
474491
r"""
@@ -503,10 +520,11 @@ def reverse(self, degree=None):
503520
raise ValueError("Can only reverse *finite* Newton polygons")
504521
if degree is None:
505522
degree = self.vertices()[-1][0]
506-
vertices = [ (degree-x,y) for (x,y) in self.vertices() ]
523+
vertices = [(degree - x, y) for x, y in self.vertices()]
507524
vertices.reverse()
508525
parent = self.parent()
509-
polyhedron = Polyhedron(base_ring=parent.base_ring(), vertices=vertices, rays=[(0,1)])
526+
polyhedron = Polyhedron(base_ring=parent.base_ring(),
527+
vertices=vertices, rays=[(0, 1)])
510528
return parent(polyhedron)
511529

512530

@@ -557,12 +575,14 @@ class ParentNewtonPolygon(Parent, UniqueRepresentation):
557575
and ends with an infinite line having the specified slope::
558576
559577
sage: NewtonPolygon([ (0,0), (1,1), (2,8), (3,5) ], last_slope=3)
560-
Infinite Newton polygon with 3 vertices: (0, 0), (1, 1), (3, 5) ending by an infinite line of slope 3
578+
Infinite Newton polygon with 3 vertices: (0, 0), (1, 1), (3, 5)
579+
ending by an infinite line of slope 3
561580
562581
Specifying a last slope may discard some vertices::
563582
564583
sage: NewtonPolygon([ (0,0), (1,1), (2,8), (3,5) ], last_slope=3/2)
565-
Infinite Newton polygon with 2 vertices: (0, 0), (1, 1) ending by an infinite line of slope 3/2
584+
Infinite Newton polygon with 2 vertices: (0, 0), (1, 1)
585+
ending by an infinite line of slope 3/2
566586
567587
Next, we define a Newton polygon by its slopes::
568588
@@ -589,7 +609,8 @@ class ParentNewtonPolygon(Parent, UniqueRepresentation):
589609
590610
sage: NP = NewtonPolygon([0, 1/2, 1/2, 2/3, 2/3, 2/3, 1, 1], last_slope=2/3)
591611
sage: NP
592-
Infinite Newton polygon with 3 vertices: (0, 0), (1, 0), (3, 1) ending by an infinite line of slope 2/3
612+
Infinite Newton polygon with 3 vertices: (0, 0), (1, 0), (3, 1)
613+
ending by an infinite line of slope 2/3
593614
sage: NP.slopes()
594615
[0, 1/2, 1/2]
595616
@@ -602,14 +623,15 @@ class ParentNewtonPolygon(Parent, UniqueRepresentation):
602623
sage: x, y = polygen(QQ,'x, y')
603624
sage: p = 1 + x*y**45 + x**3*y**6
604625
sage: p.newton_polytope()
605-
A 2-dimensional polyhedron in ZZ^2 defined as the convex hull of 3 vertices
626+
A 2-dimensional polyhedron in ZZ^2 defined as the convex hull
627+
of 3 vertices
606628
sage: p.newton_polytope().vertices()
607629
(A vertex at (0, 0), A vertex at (1, 45), A vertex at (3, 6))
608630
"""
609631

610632
Element = NewtonPolygon_element
611633

612-
def __init__(self):
634+
def __init__(self) -> None:
613635
"""
614636
Parent class for all Newton polygons.
615637
@@ -634,12 +656,12 @@ def __init__(self):
634656
from sage.rings.rational_field import QQ
635657
Parent.__init__(self, category=Semirings(), base=QQ)
636658

637-
def _repr_(self):
659+
def _repr_(self) -> str:
638660
"""
639661
Return the string representation of this parent,
640662
which is ``Parent for Newton polygons``.
641663
642-
TESTS:
664+
TESTS::
643665
644666
sage: from sage.geometry.newton_polygon import NewtonPolygon
645667
sage: NewtonPolygon
@@ -654,15 +676,16 @@ def _an_element_(self):
654676
"""
655677
Return a Newton polygon (which is the empty one).
656678
657-
TESTS:
679+
TESTS::
658680
659681
sage: from sage.geometry.newton_polygon import NewtonPolygon
660682
sage: NewtonPolygon._an_element_()
661683
Empty Newton polygon
662684
"""
663685
return self(Polyhedron(base_ring=self.base_ring(), ambient_dim=2))
664686

665-
def _element_constructor_(self, arg, sort_slopes=True, last_slope=Infinity):
687+
def _element_constructor_(self, arg, sort_slopes=True,
688+
last_slope=Infinity):
666689
r"""
667690
INPUT:
668691
@@ -712,15 +735,17 @@ def _element_constructor_(self, arg, sort_slopes=True, last_slope=Infinity):
712735
try:
713736
arg = list(arg)
714737
except TypeError:
715-
raise TypeError("argument must be a list of coordinates or a list of (rational) slopes")
738+
raise TypeError("argument must be a list of coordinates "
739+
"or a list of (rational) slopes")
716740
if arg and arg[0] in self.base_ring():
717741
if sort_slopes:
718742
arg.sort()
719743
x = y = 0
720744
vertices = [(x, y)]
721745
for slope in arg:
722746
if slope not in self.base_ring():
723-
raise TypeError("argument must be a list of coordinates or a list of (rational) slopes")
747+
raise TypeError("argument must be a list of coordinates "
748+
"or a list of (rational) slopes")
724749
x += 1
725750
y += slope
726751
vertices.append((x, y))
@@ -733,7 +758,8 @@ def _element_constructor_(self, arg, sort_slopes=True, last_slope=Infinity):
733758
rays = [(0, 1)]
734759
if last_slope is not Infinity:
735760
rays.append((1, last_slope))
736-
polyhedron = Polyhedron(base_ring=self.base_ring(), vertices=vertices, rays=rays)
761+
polyhedron = Polyhedron(base_ring=self.base_ring(),
762+
vertices=vertices, rays=rays)
737763
return self.element_class(polyhedron, parent=self)
738764

739765

0 commit comments

Comments
 (0)