Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit d856033

Browse files
author
Release Manager
committed
Trac #31144: refresh the file polyhedron/plot.py
fixing some lgtm warnings about mutation of default option values plus a partial flake8 cleanup URL: https://trac.sagemath.org/31144 Reported by: chapoton Ticket author(s): Frédéric Chapoton Reviewer(s): David Coudert
2 parents eb9bae5 + 50cbd58 commit d856033

File tree

1 file changed

+79
-54
lines changed
  • src/sage/geometry/polyhedron

1 file changed

+79
-54
lines changed

src/sage/geometry/polyhedron/plot.py

Lines changed: 79 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def cyclic_sort_vertices_2d(Vlist):
122122
break
123123
else:
124124
result += chain
125-
chain = [ Vlist.pop() ]
125+
chain = [Vlist.pop()]
126126
result += chain
127127
return result
128128

@@ -182,16 +182,16 @@ def __init__(self, projection_point):
182182
self.psize = norm(pproj)
183183
if (self.psize).is_zero():
184184
raise ValueError("projection direction must be a non-zero vector.")
185-
v = vector(RDF, [0.0]*(self.dim-1) + [-self.psize]) - pproj
185+
v = vector(RDF, [0.0] * (self.dim - 1) + [-self.psize]) - pproj
186186
polediff = matrix(RDF, v).transpose()
187-
denom = RDF((polediff.transpose()*polediff)[0][0])
187+
denom = RDF((polediff.transpose() * polediff)[0][0])
188188
if denom.is_zero():
189189
self.house = identity_matrix(RDF, self.dim)
190190
else:
191191
house = identity_matrix(RDF, self.dim) \
192192
- 2*polediff*polediff.transpose()/denom # Householder reflector
193193
# Make it preserve orientation (chirality):
194-
self.house = diagonal_matrix(RDF,[1]*(self.dim - 1) + [-1] ) * house
194+
self.house = diagonal_matrix(RDF, [1] * (self.dim - 1) + [-1]) * house
195195

196196
def __call__(self, x):
197197
"""
@@ -225,11 +225,11 @@ def __call__(self, x):
225225
(0.5, 0.0)
226226
"""
227227
img = self.house * x
228-
denom = self.psize-img[self.dim-1]
228+
denom = self.psize - img[self.dim - 1]
229229
if denom.is_zero():
230-
raise ValueError('Point cannot coincide with ' \
231-
'coordinate singularity at ' + repr(x))
232-
return vector(RDF, [img[i]/denom for i in range(self.dim-1)])
230+
raise ValueError('Point cannot coincide with '
231+
'coordinate singularity at ' + repr(x))
232+
return vector(RDF, [img[i] / denom for i in range(self.dim - 1)])
233233

234234

235235
class ProjectionFuncSchlegel():
@@ -359,7 +359,7 @@ def __init__(self, polyhedron, proj=projection_func_identity):
359359
self.parent_polyhedron = polyhedron
360360
self.coords = Sequence([])
361361
self.points = Sequence([])
362-
self.lines = Sequence([])
362+
self.lines = Sequence([])
363363
self.arrows = Sequence([])
364364
self.polygons = Sequence([])
365365
self.polyhedron_ambient_dim = polyhedron.ambient_dim()
@@ -478,7 +478,7 @@ def schlegel(self, facet=None, position=None):
478478
from the barycenter of ``facet``. A value close to 0 will place the
479479
projection point close to the facet and a large value further away.
480480
If the given value is too large, an error is returned.
481-
If no position is given, it takes the midpoint of the possible
481+
If no position is given, it takes the midpoint of the possible
482482
point of views along a line spanned by the barycenter of the facet
483483
and a valid point outside the facet.
484484
@@ -530,12 +530,12 @@ def schlegel(self, facet=None, position=None):
530530
if position is not None and position <= 0:
531531
raise ValueError("'position' should be a positive number")
532532

533-
barycenter = ZZ.one()*sum([v.vector() for v in facet.vertices()]) / len(facet.vertices())
533+
barycenter = ZZ.one() * sum([v.vector() for v in facet.vertices()]) / len(facet.vertices())
534534
locus_polyhedron = facet.stacking_locus()
535535
repr_point = locus_polyhedron.representative_point()
536536
if position is None:
537537
# Figure out a somehow canonical point of view inside the locus
538-
# polyhedron
538+
# polyhedron
539539
from sage.geometry.polyhedron.constructor import Polyhedron
540540
the_ray = Polyhedron(vertices=[barycenter],
541541
rays=[repr_point - barycenter],
@@ -682,7 +682,7 @@ def _init_points(self, polyhedron):
682682
[0, 1, 2, 3]
683683
"""
684684
for v in polyhedron.vertex_generator():
685-
self.points.append( self.coord_index_of(v.vector()) )
685+
self.points.append(self.coord_index_of(v.vector()))
686686

687687
def _init_lines_arrows(self, polyhedron):
688688
"""
@@ -703,9 +703,10 @@ def _init_lines_arrows(self, polyhedron):
703703
"""
704704
obj = polyhedron.Vrepresentation()
705705
for i in range(len(obj)):
706-
if not obj[i].is_vertex(): continue
706+
if not obj[i].is_vertex():
707+
continue
707708
for j in range(len(obj)):
708-
if polyhedron.vertex_adjacency_matrix()[i,j] == 0:
709+
if polyhedron.vertex_adjacency_matrix()[i, j] == 0:
709710
continue
710711
if i < j and obj[j].is_vertex():
711712
l = [obj[i].vector(), obj[j].vector()]
@@ -743,8 +744,10 @@ def _init_area_2d(self, polyhedron):
743744

744745
def adjacent_vertices(i):
745746
n = len(vertices)
746-
if vertices[(i-1) % n].is_vertex(): yield vertices[(i-1) % n]
747-
if vertices[(i+1) % n].is_vertex(): yield vertices[(i+1) % n]
747+
if vertices[(i-1) % n].is_vertex():
748+
yield vertices[(i-1) % n]
749+
if vertices[(i+1) % n].is_vertex():
750+
yield vertices[(i+1) % n]
748751

749752
for i in range(len(vertices)):
750753
v = vertices[i]
@@ -755,7 +758,7 @@ def adjacent_vertices(i):
755758
coords.append(a() + v())
756759

757760
if polyhedron.n_lines() == 0:
758-
self.polygons.append( self.coord_indices_of(coords) )
761+
self.polygons.append(self.coord_indices_of(coords))
759762
return
760763

761764
polygons = []
@@ -764,18 +767,18 @@ def adjacent_vertices(i):
764767
aline = next(polyhedron.line_generator())
765768
for shift in [aline(), -aline()]:
766769
for i in range(len(coords)):
767-
polygons.append( [ coords[i-1],coords[i],
768-
coords[i]+shift, coords[i-1]+shift ] )
770+
polygons.append([coords[i - 1], coords[i],
771+
coords[i] + shift, coords[i - 1] + shift])
769772

770773
if polyhedron.n_lines() == 2:
771774
[line1, line2] = [l for l in polyhedron.lines()]
772775
assert len(coords) == 1, "Can have only a single vertex!"
773776
v = coords[0]
774777
l1 = line1()
775778
l2 = line2()
776-
polygons = [ [v-l1-l2, v+l1-l2, v+l1+l2, v-l1+l2] ]
779+
polygons = [[v-l1-l2, v+l1-l2, v+l1+l2, v-l1+l2]]
777780

778-
polygons = [ self.coord_indices_of(p) for p in polygons ]
781+
polygons = [self.coord_indices_of(p) for p in polygons]
779782
self.polygons.extend(polygons)
780783

781784
def _init_solid_3d(self, polyhedron):
@@ -848,17 +851,17 @@ def adjacent_vertices(i):
848851
for shift in [aline(), -aline()]:
849852
for coords in faces:
850853
assert len(coords) == 2, "There must be two points."
851-
polygons.append( [ coords[0],coords[1],
852-
coords[1]+shift, coords[0]+shift ] )
854+
polygons.append([coords[0], coords[1],
855+
coords[1] + shift, coords[0] + shift])
853856

854857
if polyhedron.n_lines() == 2:
855858
[line1, line2] = [l for l in polyhedron.line_generator()]
856859
l1 = line1()
857860
l2 = line2()
858861
for v in polyhedron.vertex_generator():
859-
polygons.append( [v()-l1-l2, v()+l1-l2, v()+l1+l2, v()-l1+l2] )
862+
polygons.append([v()-l1-l2, v()+l1-l2, v()+l1+l2, v()-l1+l2])
860863

861-
self.polygons.extend( [self.coord_indices_of(p) for p in polygons] )
864+
self.polygons.extend([self.coord_indices_of(p) for p in polygons])
862865

863866
def render_points_1d(self, **kwds):
864867
"""
@@ -938,10 +941,10 @@ def render_outline_2d(self, **kwds):
938941
wireframe = []
939942
for l in self.lines:
940943
l_coords = self.coordinates_of(l)
941-
wireframe.append( line2d(l_coords, **kwds) )
944+
wireframe.append(line2d(l_coords, **kwds))
942945
for a in self.arrows:
943946
a_coords = self.coordinates_of(a)
944-
wireframe.append( arrow(a_coords[0], a_coords[1], **kwds) )
947+
wireframe.append(arrow(a_coords[0], a_coords[1], **kwds))
945948
return sum(wireframe)
946949

947950
def render_fill_2d(self, **kwds):
@@ -958,7 +961,7 @@ def render_fill_2d(self, **kwds):
958961
0.8
959962
"""
960963
poly = [polygon2d(self.coordinates_of(p), **kwds)
961-
for p in self.polygons]
964+
for p in self.polygons]
962965
return sum(poly)
963966

964967
def render_vertices_3d(self, **kwds):
@@ -990,7 +993,7 @@ def render_wireframe_3d(self, **kwds):
990993
wireframe = []
991994
for l in self.lines:
992995
l_coords = self.coordinates_of(l)
993-
wireframe.append( line3d(l_coords, **kwds))
996+
wireframe.append(line3d(l_coords, **kwds))
994997
for a in self.arrows:
995998
a_coords = self.coordinates_of(a)
996999
wireframe.append(arrow3d(a_coords[0], a_coords[1], **kwds))
@@ -1011,7 +1014,7 @@ def render_solid_3d(self, **kwds):
10111014
N = max([-1] + [i for p in polys for i in p]) + 1
10121015
return polygons3d(polys, self.coordinates_of(range(N)), **kwds)
10131016

1014-
def render_0d(self, point_opts={}, line_opts={}, polygon_opts={}):
1017+
def render_0d(self, point_opts=None, line_opts=None, polygon_opts=None):
10151018
"""
10161019
Return 0d rendering of the projection of a polyhedron into
10171020
2-dimensional ambient space.
@@ -1032,15 +1035,17 @@ def render_0d(self, point_opts={}, line_opts={}, polygon_opts={}):
10321035
sage: print(Polyhedron(ieqs=[(1,)]).projection().render_0d().description())
10331036
Point set defined by 1 point(s): [(0.0, 0.0)]
10341037
"""
1038+
if point_opts is None:
1039+
point_opts = {}
10351040
if isinstance(point_opts, dict):
10361041
point_opts.setdefault('zorder', 2)
10371042
point_opts.setdefault('pointsize', 10)
10381043
if self.points:
1039-
return point2d([0,0], **point_opts)
1044+
return point2d([0, 0], **point_opts)
10401045
else:
10411046
return Graphics()
10421047

1043-
def render_1d(self, point_opts={}, line_opts={}, polygon_opts={}):
1048+
def render_1d(self, point_opts=None, line_opts=None, polygon_opts=None):
10441049
"""
10451050
Return 1d rendering of the projection of a polyhedron into
10461051
2-dimensional ambient space.
@@ -1060,6 +1065,10 @@ def render_1d(self, point_opts={}, line_opts={}, polygon_opts={}):
10601065
Graphics object consisting of 2 graphics primitives
10611066
"""
10621067
plt = Graphics()
1068+
if point_opts is None:
1069+
point_opts = {}
1070+
if line_opts is None:
1071+
line_opts = {}
10631072
if isinstance(point_opts, dict):
10641073
point_opts.setdefault('zorder', 2)
10651074
point_opts.setdefault('pointsize', 10)
@@ -1069,7 +1078,7 @@ def render_1d(self, point_opts={}, line_opts={}, polygon_opts={}):
10691078
plt += self.render_line_1d(**line_opts)
10701079
return plt
10711080

1072-
def render_2d(self, point_opts={}, line_opts={}, polygon_opts={}):
1081+
def render_2d(self, point_opts=None, line_opts=None, polygon_opts=None):
10731082
"""
10741083
Return 2d rendering of the projection of a polyhedron into
10751084
2-dimensional ambient space.
@@ -1088,6 +1097,12 @@ def render_2d(self, point_opts={}, line_opts={}, polygon_opts={}):
10881097
Graphics object consisting of 17 graphics primitives
10891098
"""
10901099
plt = Graphics()
1100+
if point_opts is None:
1101+
point_opts = {}
1102+
if line_opts is None:
1103+
line_opts = {}
1104+
if polygon_opts is None:
1105+
polygon_opts = {}
10911106
if isinstance(point_opts, dict):
10921107
point_opts.setdefault('zorder', 2)
10931108
point_opts.setdefault('pointsize', 10)
@@ -1100,7 +1115,7 @@ def render_2d(self, point_opts={}, line_opts={}, polygon_opts={}):
11001115
plt += self.render_fill_2d(**polygon_opts)
11011116
return plt
11021117

1103-
def render_3d(self, point_opts={}, line_opts={}, polygon_opts={}):
1118+
def render_3d(self, point_opts=None, line_opts=None, polygon_opts=None):
11041119
"""
11051120
Return 3d rendering of a polyhedron projected into
11061121
3-dimensional ambient space.
@@ -1146,6 +1161,12 @@ def render_3d(self, point_opts={}, line_opts={}, polygon_opts={}):
11461161
pplt = None
11471162
lplt = None
11481163
pgplt = None
1164+
if point_opts is None:
1165+
point_opts = {}
1166+
if line_opts is None:
1167+
line_opts = {}
1168+
if polygon_opts is None:
1169+
polygon_opts = {}
11491170
if isinstance(point_opts, dict):
11501171
point_opts.setdefault('width', 3)
11511172
pplt = self.render_vertices_3d(**point_opts)
@@ -1341,10 +1362,10 @@ def _tikz_2d(self, scale, edge_color, facet_color, opacity, vertex_color, axis):
13411362
edges = ''
13421363
for vert in self.points:
13431364
v = self.coords[vert]
1344-
v_vect = str(['%.5f' % i for i in v]).replace('\'','')
1365+
v_vect = str(['%.5f' % i for i in v]).replace('\'', '')
13451366
v_vect = v_vect.replace('[', '(')
13461367
v_vect = v_vect.replace(']', ')')
1347-
tag = '%s' %v_vect
1368+
tag = '%s' % v_vect
13481369
node = "\\node[%s] at %s {};\n" % ('vertex', tag)
13491370
coord = '\\coordinate %s at %s;\n' % (tag, tag)
13501371
dict_drawing[vert] = node, coord, tag
@@ -1362,7 +1383,7 @@ def _tikz_2d(self, scale, edge_color, facet_color, opacity, vertex_color, axis):
13621383
tikz_pic += '\t[scale=%f,\n' % scale
13631384
tikz_pic += '\tback/.style={loosely dotted, thin},\n'
13641385
tikz_pic += '\tedge/.style={color=%s, thick},\n' % edge_color
1365-
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color,opacity)
1386+
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color, opacity)
13661387
tikz_pic += '\tvertex/.style={inner sep=1pt,circle,draw=%s!25!black,' % vertex_color
13671388
tikz_pic += 'fill=%s!75!black,thick}]\n%%\n%%\n' % vertex_color
13681389

@@ -1480,10 +1501,10 @@ def _tikz_2d_in_3d(self, view, angle, scale, edge_color, facet_color,
14801501
edges = ''
14811502
for vert in self.points:
14821503
v = self.coords[vert]
1483-
v_vect = str(['%.5f' % i for i in v]).replace('\'','')
1484-
v_vect = v_vect.replace('[','(')
1485-
v_vect = v_vect.replace(']',')')
1486-
tag = '%s' %v_vect
1504+
v_vect = str(['%.5f' % i for i in v]).replace('\'', '')
1505+
v_vect = v_vect.replace('[', '(')
1506+
v_vect = v_vect.replace(']', ')')
1507+
tag = '%s' % v_vect
14871508
node = "\\node[%s] at %s {};\n" % ('vertex', tag)
14881509
coord = '\\coordinate %s at %s;\n' % (tag, tag)
14891510
dict_drawing[vert] = node, coord, tag
@@ -1507,7 +1528,7 @@ def _tikz_2d_in_3d(self, view, angle, scale, edge_color, facet_color,
15071528
tikz_pic += '\tscale=%f,\n' % scale
15081529
tikz_pic += '\tback/.style={loosely dotted, thin},\n'
15091530
tikz_pic += '\tedge/.style={color=%s, thick},\n' % edge_color
1510-
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color,opacity)
1531+
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color, opacity)
15111532
tikz_pic += '\tvertex/.style={inner sep=1pt,circle,draw=%s!25!black,' % vertex_color
15121533
tikz_pic += 'fill=%s!75!black,thick}]\n%%\n%%\n' % vertex_color
15131534

@@ -1653,7 +1674,7 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
16531674
A = facets[index_facet].vector()[1:]
16541675
B = facets[index_facet].vector()[0]
16551676
for v in self.points:
1656-
if A*self.coords[v]+B < 0.0005 and v not in back_vertices:
1677+
if A * self.coords[v] + B < 0.0005 and v not in back_vertices:
16571678
back_vertices += [v]
16581679

16591680
# Creates the nodes, coordinate and tag for every vertex of the polytope.
@@ -1664,12 +1685,12 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
16641685

16651686
for vert in self.points:
16661687
v = self.coords[vert]
1667-
v_vect = str(['%.5f' % i for i in v]).replace('\'','')
1668-
v_vect = v_vect.replace('[','(')
1669-
v_vect = v_vect.replace(']',')')
1670-
tag = '%s' %v_vect
1688+
v_vect = str(['%.5f' % i for i in v]).replace('\'', '')
1689+
v_vect = v_vect.replace('[', '(')
1690+
v_vect = v_vect.replace(']', ')')
1691+
tag = '%s' % v_vect
16711692
node = "\\node[%s] at %s {};\n" % ('vertex', tag)
1672-
coord = '\\coordinate %s at %s;\n' %(tag, tag)
1693+
coord = '\\coordinate %s at %s;\n' % (tag, tag)
16731694
dict_drawing[vert] = node, coord, tag
16741695

16751696
# Separate the edges between back and front
@@ -1684,10 +1705,14 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
16841705

16851706
# The back edge has to be between two vertices in the Back
16861707
# AND such that the 2 facets touching them are in the Back
1687-
if index1 in back_vertices and index2 in back_vertices and len(H_v12)==2:
1688-
back_part += "\\draw[%s,back] %s -- %s;\n" % ('edge', dict_drawing[index1][2], dict_drawing[index2][2])
1708+
if index1 in back_vertices and index2 in back_vertices and len(H_v12) == 2:
1709+
back_part += "\\draw[%s,back] %s -- %s;\n" % ('edge',
1710+
dict_drawing[index1][2],
1711+
dict_drawing[index2][2])
16891712
else:
1690-
front_part += "\\draw[%s] %s -- %s;\n" % ('edge',dict_drawing[index1][2],dict_drawing[index2][2])
1713+
front_part += "\\draw[%s] %s -- %s;\n" % ('edge',
1714+
dict_drawing[index1][2],
1715+
dict_drawing[index2][2])
16911716

16921717
# Start to write the output
16931718
tikz_pic = ''
@@ -1701,7 +1726,7 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
17011726
tikz_pic += '\tscale=%f,\n' % scale
17021727
tikz_pic += '\tback/.style={loosely dotted, thin},\n'
17031728
tikz_pic += '\tedge/.style={color=%s, thick},\n' % edge_color
1704-
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color,opacity)
1729+
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color, opacity)
17051730
tikz_pic += '\tvertex/.style={inner sep=1pt,circle,draw=%s!25!black,' % vertex_color
17061731
tikz_pic += 'fill=%s!75!black,thick}]\n%%\n%%\n' % vertex_color
17071732

@@ -1737,7 +1762,7 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
17371762
# Draw the vertices on top of the back-edges
17381763
tikz_pic += '%%\n%%\n%% Drawing vertices in the back\n%%\n'
17391764
for v in back_vertices:
1740-
if not v in front_vertices and v in dict_drawing:
1765+
if v not in front_vertices and v in dict_drawing:
17411766
tikz_pic += dict_drawing[v][0]
17421767

17431768
# Draw the facets in the front by going in cycles for every facet.

0 commit comments

Comments
 (0)