Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Generic implementation of powering
This implements powering of arbitrary objects using a square-and-multiply algorithm. """
#***************************************************************************** # Copyright (C) 2017 Jeroen Demeyer <J.Demeyer@UGent.be> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
from cysignals.signals cimport sig_check
from .long cimport integer_check_long
cpdef generic_power(a, n): """ Return `a^n`.
If `n` is negative, return `(1/a)^(-n)`.
INPUT:
- ``a`` -- any object supporting multiplication (and division if n < 0)
- ``n`` -- any integer (in the duck typing sense)
EXAMPLES::
sage: from sage.arith.power import generic_power sage: generic_power(int(12), int(0)) 1 sage: generic_power(int(0), int(100)) 0 sage: generic_power(Integer(10), Integer(0)) 1 sage: generic_power(Integer(0), Integer(23)) 0 sage: sum([generic_power(2,i) for i in range(17)]) #test all 4-bit combinations 131071 sage: F = Zmod(5) sage: a = generic_power(F(2), 5); a 2 sage: a.parent() is F True sage: a = generic_power(F(1), 2) sage: a.parent() is F True
sage: generic_power(int(5), 0) 1 sage: generic_power(2, 5/4) Traceback (most recent call last): ... NotImplementedError: non-integral exponents not supported
::
sage: class SymbolicMul(str): ....: def __mul__(self, other): ....: s = "({}*{})".format(self, other) ....: return type(self)(s) sage: x = SymbolicMul("x") sage: print(generic_power(x, 7)) (((x*x)*(x*x))*((x*x)*x)) """
cdef int err
cdef generic_power_long(a, long n): """ As ``generic_power`` but where ``n`` is a C long. """ return one(a)
cdef generic_power_pos(a, ulong_or_object n): """ Return `a^n` where `n > 0`. """ # Find least significant set bit as starting point
# Now multiply together the correct factors a^(2^i)
|