diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py index bb804bcfd1b..acbf386833f 100644 --- a/src/sage/graphs/generic_graph.py +++ b/src/sage/graphs/generic_graph.py @@ -763,20 +763,40 @@ def __mul__(self, n): sage: G = graphs.CycleGraph(3) sage: H = G * 3; H - Cycle graph disjoint_union Cycle graph disjoint_union Cycle graph: Graph on 9 vertices + Disjoint union of 3 copies of Cycle graph: Graph on 9 vertices sage: H.vertices(sort=True) [0, 1, 2, 3, 4, 5, 6, 7, 8] sage: H = G * 1; H Cycle graph: Graph on 3 vertices + + TESTS:: + + sage: Graph(1) * -1 + Traceback (most recent call last): + ... + TypeError: multiplication of a graph and a nonpositive integer is not defined + sage: Graph(1) * 2.5 + Traceback (most recent call last): + ... + TypeError: multiplication of a graph and something other than an integer is not defined """ if isinstance(n, (int, Integer)): if n < 1: raise TypeError('multiplication of a graph and a nonpositive integer is not defined') if n == 1: return copy(self) - return sum([self] * (n - 1), self) - else: - raise TypeError('multiplication of a graph and something other than an integer is not defined') + ns = self.order() + ntot = n * ns + vint = {u: i for i, u in enumerate(self)} + edges = ((i, j, l) for u, v, l in self.edge_iterator() + for i, j in zip(range(vint[u], ntot, ns), + range(vint[v], ntot, ns))) + return self.__class__([range(ntot), edges], format='vertices_and_edges', + loops=self.allows_loops(), + multiedges=self.allows_multiple_edges(), + immutable=self.is_immutable(), + name=f"Disjoint union of {n} copies of {str(self)}") + raise TypeError('multiplication of a graph and something other than an integer is not defined') def __ne__(self, other): """ @@ -809,7 +829,7 @@ def __rmul__(self, n): sage: G = graphs.CycleGraph(3) sage: H = int(3) * G; H - Cycle graph disjoint_union Cycle graph disjoint_union Cycle graph: Graph on 9 vertices + Disjoint union of 3 copies of Cycle graph: Graph on 9 vertices sage: H.vertices(sort=True) [0, 1, 2, 3, 4, 5, 6, 7, 8] """