A class for computing and caching powers of the same integer.
This class is designed to be used as a field of p-adic rings and fields. Since elements of p-adic rings and fields need to use powers of p over and over, this class precomputes and stores powers of p. There is no reason that the base has to be prime however.
EXAMPLES:
sage: X = PowComputer(3, 4, 10)
sage: X(3)
27
sage: X(10) == 3^10
True
AUTHORS:
Returns a PowComputer that caches the values $1, m, m^2, ldots, m^cache_limit$.
Once you create a PowComputer, merely call it to get values out.
You can input any integer, even if it’s outside of the precomputed range.
INPUT:
* m -- An integer, the base that you want to exponentiate.
* cache_limit -- A positive integer that you want to cache powers up to.
EXAMPLES:
sage: PC = PowComputer(3, 5, 10)
sage: PC
PowComputer for 3
sage: PC(4)
81
sage: PC(6)
729
sage: PC(-1)
1/3
Pickling.
EXAMPLES:
sage: P = PowComputer(5, 7, 10)
sage: R = loads(dumps(P))
sage: P == R
True
Returns self.prime^n.
EXAMPLES:
sage: P = PowComputer(3, 4, 6)
sage: P(3)
27
sage: P(6)
729
sage: P(5)
243
sage: P(7)
2187
sage: P(0)
1
sage: P(-2)
1/9
Returns a string representation of self.
EXAMPLES:
sage: PC = PowComputer(3, 5, 10); PC
PowComputer for 3
Returns the limit to which powers of prime are computed.
EXAMPLES:
sage: P = PowComputer(3, 5, 10)
sage: P._cache_limit()
5
Returns whether or not self is attached to a field.
EXAMPLES:
sage: P = PowComputer(3, 5, 10)
sage: P._in_field()
False
This function demonstrates a danger in using pow_mpz_t_tmp.
EXAMPLES:
sage: PC = PowComputer(5, 5, 10)
When you cal pow_mpz_t_tmp with an input that is not stored
(ie n > self.cache_limit and n != self.prec_cap),
it stores the result in self.temp_m and returns a pointer
to that mpz_t. So if you try to use the results of two
calls at once, things will break.
sage: PC._pow_mpz_t_tmp_demo(6, 8) # 244140625 on some architectures and 152587890625 on others: random
244140625
sage: 5^6*5^8
6103515625
sage: 5^6*5^6
244140625
Note that this does not occur if you try a stored value,
because the result of one of the calls points to that
stored value.
sage: PC._pow_mpz_t_tmp_demo(6, 10)
152587890625
sage: 5^6*5^10
152587890625
Tests the pow_mpz_t_tmp function.
EXAMPLES:
sage: PC = PowComputer(3, 5, 10)
sage: PC._pow_mpz_t_tmp_test(4)
81
sage: PC._pow_mpz_t_tmp_test(6)
729
sage: PC._pow_mpz_t_tmp_test(0)
1
sage: PC._pow_mpz_t_tmp_test(10)
59049
sage: PC = PowComputer_ext_maker(3, 5, 10, 20, False, ntl.ZZ_pX([-3,0,1], 3^10), 'big','e',ntl.ZZ_pX([1],3^10))
sage: PC._pow_mpz_t_tmp_test(4)
81
sage: PC._pow_mpz_t_tmp_test(6)
729
sage: PC._pow_mpz_t_tmp_test(0)
1
sage: PC._pow_mpz_t_tmp_test(10)
59049
Tests the pow_mpz_t_top function.
EXAMPLES:
sage: PC = PowComputer(3, 5, 10)
sage: PC._pow_mpz_t_top_test()
59049
sage: PC = PowComputer_ext_maker(3, 5, 10, 20, False, ntl.ZZ_pX([-3,0,1], 3^10), 'big','e',ntl.ZZ_pX([1],3^10))
sage: PC._pow_mpz_t_top_test()
59049
Returns prec_cap, a single value that for which self._prime()^prec_cap is stored
EXAMPLES:
sage: P = PowComputer(3, 5, 10)
sage: P._prec_cap()
10
Returns the base that the PowComputer is exponentiating.
EXAMPLES:
sage: P = PowComputer(6, 10, 15)
sage: P._prime()
6
Returns self._prime()^self._prec_cap()
EXAMPLES:
sage: P = PowComputer(3, 4, 6)
sage: P._top_power()
729
Tests the pow_Integer function.
EXAMPLES:
sage: PC = PowComputer(3, 5, 10)
sage: PC.pow_Integer_Integer(4)
81
sage: PC.pow_Integer_Integer(6)
729
sage: PC.pow_Integer_Integer(0)
1
sage: PC.pow_Integer_Integer(10)
59049
sage: PC = PowComputer_ext_maker(3, 5, 10, 20, False, ntl.ZZ_pX([-3,0,1], 3^10), 'big','e',ntl.ZZ_pX([1],3^10))
sage: PC.pow_Integer_Integer(4)
81
sage: PC.pow_Integer_Integer(6)
729
sage: PC.pow_Integer_Integer(0)
1
sage: PC.pow_Integer_Integer(10)
59049