Skip to content

Commit

Permalink
Trac #34306: Better use of graphs in src/sage/geometry/hyperplane_arr…
Browse files Browse the repository at this point in the history
…angement/library.py

Typical changes are
{{{#!diff
-        for e in G.edges(sort=True):
-            i = G.vertices(sort=True).index(e[0])
-            j = G.vertices(sort=True).index(e[1])
+        vertex_to_int = {u: i for i, u in enumerate(G)}
+        for u, v in G.edge_iterator(labels=False, sort_vertices=False):
+            i = vertex_to_int[u]
+            j = vertex_to_int[v]
}}}
On the way, we also improve some calls to `.index(...)` in
`src/sage/geometry/`.

URL: https://trac.sagemath.org/34306
Reported by: dcoudert
Ticket author(s): David Coudert
Reviewer(s): Frédéric Chapoton
  • Loading branch information
Release Manager committed Aug 29, 2022
2 parents 9005c08 + b19f265 commit 87ea5fb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/sage/geometry/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,8 @@ def result():
rays = new_rays
else:
rays = tuple(sorted(ray_set))
cones = (tuple(sorted(rays.index(ray) for ray in cone.rays()))
ray_to_index = {ray: i for i, ray in enumerate(rays)}
cones = (tuple(sorted(ray_to_index[ray] for ray in cone.rays()))
for cone in cones)
return result()
# Construct the fan from rays and "tuple cones"
Expand Down Expand Up @@ -1925,7 +1926,8 @@ def _subdivide_stellar(self, new_rays, verbose):
new_fan_rays = list(self.rays())
new_fan_rays.extend(ray for ray in new_rays
if ray not in self.rays().set())
cones = tuple(tuple(sorted(new_fan_rays.index(ray) for ray in cone))
ray_to_index = {ray: i for i, ray in enumerate(new_fan_rays)}
cones = tuple(tuple(sorted(ray_to_index[ray] for ray in cone))
for cone in cones)
fan = Fan(cones, new_fan_rays, check=False, normalize=False)
return fan
Expand Down
28 changes: 16 additions & 12 deletions src/sage/geometry/hyperplane_arrangement/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ def bigraphical(self, G, A=None, K=QQ, names=None):
H = make_parent(K, n, names)
x = H.gens()
hyperplanes = []
for e in G.edges(sort=True):
i = G.vertices(sort=True).index(e[0])
j = G.vertices(sort=True).index(e[1])
vertex_to_int = {u: i for i, u in enumerate(G)}
for u, v in G.edge_iterator(labels=False, sort_vertices=False):
i = vertex_to_int[u]
j = vertex_to_int[v]
hyperplanes.append( x[i] - x[j] - A[i][j])
hyperplanes.append(-x[i] + x[j] - A[j][i])
return H(*hyperplanes)
Expand Down Expand Up @@ -264,9 +265,10 @@ def G_semiorder(self, G, K=QQ, names=None):
H = make_parent(K, n, names)
x = H.gens()
hyperplanes = []
for e in G.edges(sort=True):
i = G.vertices(sort=True).index(e[0])
j = G.vertices(sort=True).index(e[1])
vertex_to_int = {u: i for i, u in enumerate(G.vertices(sort=True))}
for u, v in G.edge_iterator(labels=False):
i = vertex_to_int[u]
j = vertex_to_int[v]
hyperplanes.append(x[i] - x[j] - 1)
hyperplanes.append(x[i] - x[j] + 1)
return H(*hyperplanes)
Expand Down Expand Up @@ -303,9 +305,10 @@ def G_Shi(self, G, K=QQ, names=None):
H = make_parent(K, n, names)
x = H.gens()
hyperplanes = []
for e in G.edges(sort=True):
i = G.vertices(sort=True).index(e[0])
j = G.vertices(sort=True).index(e[1])
vertex_to_int = {u: i for i, u in enumerate(G.vertices(sort=True))}
for u, v in G.edge_iterator(labels=False):
i = vertex_to_int[u]
j = vertex_to_int[v]
hyperplanes.append(x[i] - x[j])
hyperplanes.append(x[i] - x[j] - 1)
return H(*hyperplanes)
Expand Down Expand Up @@ -351,9 +354,10 @@ def graphical(self, G, K=QQ, names=None):
H = make_parent(K, n, names)
x = H.gens()
hyperplanes = []
for e in G.edges(sort=True):
i = G.vertices(sort=True).index(e[0])
j = G.vertices(sort=True).index(e[1])
vertex_to_int = {u: i for i, u in enumerate(G.vertices(sort=True))}
for u, v in G.edge_iterator(labels=False):
i = vertex_to_int[u]
j = vertex_to_int[v]
hyperplanes.append(x[i] - x[j])
A = H(*hyperplanes)
charpoly = G.chromatic_polynomial()
Expand Down
12 changes: 8 additions & 4 deletions src/sage/geometry/lattice_polytope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,8 @@ def ambient_point_indices(self):
if self._ambient is self:
return tuple(range(self.npoints()))
points = self._ambient.points()
return tuple(points.index(p) for p in self.points())
point_to_index = {p: i for i, p in enumerate(points)}
return tuple(point_to_index[p] for p in self.points())

@cached_method
def ambient_ordered_point_indices(self):
Expand Down Expand Up @@ -1600,7 +1601,8 @@ def ambient_ordered_point_indices(self):
if self._ambient is self:
return tuple(range(self.npoints()))
points = self._ambient.points()
return tuple(points.index(p) for p in sorted(self.points()))
point_to_index = {p: i for i, p in enumerate(points)}
return tuple(point_to_index[p] for p in sorted(self.points()))

def ambient_vertex_indices(self):
r"""
Expand Down Expand Up @@ -3614,8 +3616,9 @@ def plot3d(self,
elif dim == 3:
if facet_colors is None:
facet_colors = [facet_color] * self.nfacets()
vertex_to_index = {v: i for i, v in enumerate(self.vertices())}
for f, c in zip(self.facets(), facet_colors):
pplot += IndexFaceSet([[self.vertices().index(v) for v in f.vertices(f.traverse_boundary())]],
pplot += IndexFaceSet([[vertex_to_index[v] for v in f.vertices(f.traverse_boundary())]],
vertices, opacity=facet_opacity, rgbcolor=c)
if show_edges:
if dim == 1:
Expand Down Expand Up @@ -4081,7 +4084,8 @@ def traverse_boundary(self):
if next == l[-2]:
next = prev
l.append(next)
return [self.vertices().index(v.vertex(0)) for v in l]
vertex_to_index = {v: i for i, v in enumerate(self.vertices())}
return [vertex_to_index[v.vertex(0)] for v in l]

def vertex(self, i):
r"""
Expand Down
3 changes: 2 additions & 1 deletion src/sage/geometry/polyhedron/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1816,9 +1816,10 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,

# Draw the facets in the front by going in cycles for every facet.
tikz_pic += '%%\n%%\n%% Drawing the facets\n%%\n'
vertex_to_index = {v: i for i, v in enumerate(vertices)}
for index_facet in front_facets:
cyclic_vert = cyclic_sort_vertices_2d(list(facets[index_facet].incident()))
cyclic_indices = [vertices.index(v) for v in cyclic_vert]
cyclic_indices = [vertex_to_index[v] for v in cyclic_vert]
tikz_pic += '\\fill[facet] '
for v in cyclic_indices:
if v in dict_drawing:
Expand Down
3 changes: 2 additions & 1 deletion src/sage/geometry/polyhedron/ppl_lattice_polytope.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ def pointsets_mod_automorphism(self, pointsets):
points = tuple(sorted(points))
Aut = self.lattice_automorphism_group(points,
point_labels=tuple(range(len(points))))
indexsets = set(frozenset(points.index(p) for p in ps)
point_to_index = {p: i for i, p in enumerate(points)}
indexsets = set(frozenset(point_to_index[p] for p in ps)
for ps in pointsets)
orbits = []
while indexsets:
Expand Down

0 comments on commit 87ea5fb

Please sign in to comment.