def __pow__(self, n): """ Returns this species to the power n. This uses a binary exponentiation algorithm to perform the powering. EXAMPLES:: sage: X = species.SingletonSpecies() sage: (X^2).generating_series().coefficients(4) [0, 0, 1, 0] sage: X^1 is X True sage: A = X^32 sage: A.digraph() Multi-digraph on 6 vertices """ from sage.rings.all import Integer import operator n = Integer(n) if n <= 0: raise ValueError, "only positive exponents are currently supported" digits = n.digits(2) squares = [self] for i in range(len(digits)-1): squares.append(squares[-1]*squares[-1]) return reduce(operator.add, (s for i,s in zip(digits, squares) if i != 0))
def __pow__(self, n): r""" Returns this species to the power `n`. This uses a binary exponentiation algorithm to perform the powering. EXAMPLES:: sage: One = species.EmptySetSpecies() sage: X = species.SingletonSpecies() sage: X^2 Product of (Singleton species) and (Singleton species) sage: X^5 Product of (Singleton species) and (Product of (Product of (Singleton species) and (Singleton species)) and (Product of (Singleton species) and (Singleton species))) sage: (X^2).generating_series().coefficients(4) [0, 0, 1, 0] sage: (X^3).generating_series().coefficients(4) [0, 0, 0, 1] sage: ((One+X)^3).generating_series().coefficients(4) [1, 3, 3, 1] sage: ((One+X)^7).generating_series().coefficients(8) [1, 7, 21, 35, 35, 21, 7, 1] sage: x = QQ[['x']].gen() sage: coeffs = ((1+x+x+x**2)**25+O(x**10)).padded_list() sage: T = ((One+X+X+X^2)^25) sage: T.generating_series().coefficients(10) == coeffs True sage: X^1 is X True sage: A = X^32 sage: A.digraph() Multi-digraph on 6 vertices TESTS:: sage: X**(-1) Traceback (most recent call last): ... ValueError: only positive exponents are currently supported """ from sage.rings.all import Integer import operator n = Integer(n) if n <= 0: raise ValueError("only positive exponents are currently supported") digits = n.digits(2) squares = [self] for i in range(len(digits) - 1): squares.append(squares[-1] * squares[-1]) return reduce(operator.mul, (s for i, s in zip(digits, squares) if i != 0))