# Ticket #9051: 9051-FpT_4.patch

File 9051-FpT_4.patch, 12.7 KB (added by robertwb, 12 years ago)
• ## sage/rings/fraction_field_FpT.pxd

```# HG changeset patch
# Date 1275212100 25200
# Node ID 3b9b429abf1fe5f51400be49ae7c8101c90cf315
# Parent  3ea4df2703a8e3a94b723377fcbbaa836e9f033b
#9051 - More speedups for GF(p)[t] and Frac(GF(p)[t]).

diff -r 3ea4df2703a8 -r 3b9b429abf1f sage/rings/fraction_field_FpT.pxd```
 a cdef FpTElement _new_c(self) cdef FpTElement _copy_c(self) cpdef numerator(self) cpdef denominator(self) cpdef FpTElement next(self) cpdef _sqrt_or_None(self) cpdef bint is_square(self)
• ## sage/rings/fraction_field_FpT.pyx

`diff -r 3ea4df2703a8 -r 3b9b429abf1f sage/rings/fraction_field_FpT.pyx`
 a sage: a.numer() t^6 + 3*t^4 + 10*t^3 + 3*t^2 + 1 """ cdef long n return self._parent.poly_ring( [zmod_poly_get_coeff_ui (self._numer, n) for n in range(0, zmod_poly_degree(self._numer)+1)]) return self.numerator() def numerator(self): cpdef numerator(self): """ Returns the numerator of this element, as an element of the polynomial ring. sage: a.numerator() t^6 + 3*t^4 + 10*t^3 + 3*t^2 + 1 """ return self.numer() cdef Polynomial_zmod_flint res = PY_NEW(Polynomial_zmod_flint) zmod_poly_init2_precomp(&res.x, self.p, self._numer.p_inv, self._numer.length) zmod_poly_set(&res.x, self._numer) res._parent = self._parent.poly_ring return res def denom(self): """ sage: a.denom() t^3 """ cdef long n return self._parent.poly_ring( [zmod_poly_get_coeff_ui (self._denom, n) for n in range(0, zmod_poly_degree(self._denom)+1)]) return self.denominator() def denominator(self): cpdef denominator(self): """ Returns the denominator of this element, as an element of the polynomial ring. sage: a.denominator() t^3 """ return self.denom() cdef Polynomial_zmod_flint res = PY_NEW(Polynomial_zmod_flint) zmod_poly_init2_precomp(&res.x, self.p, self._denom.p_inv, self._denom.length) zmod_poly_set(&res.x, self._denom) res._parent = self._parent.poly_ring return res def __call__(self, *args, **kwds): """ EXAMPLES:: sage: K = Frac(GF(5)['t']) sage: t = K.gen() sage: t(3) 3 sage: f = t^2/(1-t) sage: f(2) 1 sage: f(t) 4*t^2/(t + 4) sage: f(t^3) 4*t^6/(t^3 + 4) sage: f((t+1)/t^3) (t^2 + 2*t + 1)/(t^6 + 4*t^4 + 4*t^3) """ return self.numer()(*args, **kwds) / self.denom()(*args, **kwds) def subs(self, *args, **kwds): """ EXAMPLES:: sage: K = Frac(GF(11)['t']) sage: t = K.gen() sage: f = (t+1)/(t-1) sage: f.subs(t=2) 3 sage: f.subs(X=2) (t + 1)/(t + 10) """ return self.numer().subs(*args, **kwds) / self.denom().subs(*args, **kwds) def valuation(self, v): """ Returns the valuation of self at `v`. EXAMPLES:: sage: R. = GF(5)[] sage: f = (t+1)^2 * (t^2+t+1) / (t-1)^3 sage: f.valuation(t+1) 2 sage: f.valuation(t-1) -3 sage: f.valuation(t) 0 """ return self.numer().valuation(v) - self.denom().valuation(v) def factor(self): """ EXAMPLES:: sage: K = Frac(GF(5)['t']) sage: t = K.gen() sage: f = 2 * (t+1) * (t^2+t+1)^2 / (t-1) sage: factor(f) (2) * (t + 4)^-1 * (t + 1) * (t^2 + t + 1)^2 """ return self.numer().factor() / self.denom().factor() def _repr_(self): """
• ## sage/rings/fraction_field_element.pyx

`diff -r 3ea4df2703a8 -r 3b9b429abf1f sage/rings/fraction_field_element.pyx`
 a from sage.structure.element cimport FieldElement, ModuleElement, RingElement, \ Element from sage.structure.element import parent import integer_ring from integer_ring import ZZ (other).__denominator, self.__denominator*(other).__numerator) def valuation(self): def valuation(self, v=None): """ Return the valuation of self, assuming that the numerator and denominator have valuation functions defined on them. EXAMPLES:: sage: x = PolynomialRing(RationalField(),'x').gen() sage: f = (x**3 + x)/(x**2 - 2*x**3) sage: f = (x^3 + x)/(x^2 - 2*x^3) sage: f (x^2 + 1)/(-2*x^2 + x) sage: f.valuation() -1 sage: f.valuation(x^2+1) 1 """ return self.__numerator.valuation() - self.__denominator.valuation() return self.__numerator.valuation(v) - self.__denominator.valuation(v) def __nonzero__(self): """
• ## sage/rings/polynomial/polynomial_element.pyx

`diff -r 3ea4df2703a8 -r 3b9b429abf1f sage/rings/polynomial/polynomial_element.pyx`
 a #                  http://www.gnu.org/licenses/ ################################################################################ cdef is_FractionField, is_RealField, is_ComplexField cdef coerce_binop, generic_power, parent cdef ZZ, QQ, RR, CC, RDF, CDF import operator, copy, re import sage.rings.rational from sage.interfaces.all import singular as singular_default, is_SingularElement from sage.libs.all import pari, pari_gen, PariError from sage.rings.real_mpfr import RealField, is_RealNumber, is_RealField, RR from sage.rings.real_mpfr import RealField, is_RealField, RR from sage.rings.complex_field import is_ComplexField, ComplexField CC = ComplexField() from sage.rings.complex_double import is_ComplexDoubleField, CDF from sage.rings.real_mpfi import is_RealIntervalField from sage.structure.element import RingElement, generic_power from sage.structure.element import RingElement, generic_power, parent from sage.structure.element cimport Element, RingElement, ModuleElement, MonoidElement from sage.rings.rational_field import QQ, is_RationalField from sage.rings.integer_ring import ZZ, is_IntegerRing from sage.rings.fraction_field import is_FractionField from sage.rings.integral_domain import is_IntegralDomain from sage.structure.parent_gens cimport ParentWithGens sage: f(2, 1/2) 15/8 Some special cases are optimized. :: sage: R. = QQ[] sage: f = x^3-2*x sage: f(x) is f True sage: f(1/x) (-2*x^2 + 1)/x^3 sage: f = x^100 + 3 sage: f(0) 3 sage: parent(f(0)) Rational Field sage: parent(f(Qp(5)(0))) 5-adic Field with capped relative precision 20 TESTS: The following shows that \#2360 is indeed fixed. return a.parent()(1) * result except AttributeError: return result if a == self._parent.gen(): return self elif is_FractionField(parent(a)): try: a_inverse = ~a if a_inverse.denominator() == 1: a_inverse = a_inverse.numerator() return self.reverse()(a_inverse) / a_inverse**self.degree() except AttributeError: pass i = d - 1 if len(x) > 1: while i >= 0: result = result * a + self[i](other_args) i -= 1 elif not a: c = self[0] return c + c*a elif d < 4 and self._compiled is None: while i >= 0: result = result * a + self[i]
• ## sage/rings/polynomial/polynomial_zmod_flint.pyx

`diff -r 3ea4df2703a8 -r 3b9b429abf1f sage/rings/polynomial/polynomial_zmod_flint.pyx`
 a from sage.libs.ntl.ntl_lzz_pX import ntl_zz_pX from sage.structure.factorization import Factorization from sage.structure.element import coerce_binop from sage.structure.element import coerce_binop, parent # We need to define this stuff before including the templating stuff # to make sure the function get_cparent is found since it is used in EXAMPLE:: sage: P. = PolynomialRing(GF(7)) sage: f= x^2 + 1 sage: f = x^2 + 1 sage: f(0) 1 sage: f(2) sage: f(x+1) x^2 + 2*x + 2 Test some simple (but important) optimizations:: sage: f(2) == f(P(2)) True sage: f(x) is f True sage: f(1/x) (x^2 + 1)/x^2 """ cdef Polynomial_zmod_flint t cdef Polynomial_zmod_flint t, y cdef long c K = self._parent.base_ring() if len(kwds) == 0 and len(x) == 1: try: x = K._coerce_(x[0]) return K(zmod_poly_evaluate(&self.x, int(x))) except TypeError: pass try: x = self.parent().coerce(x[0]) if not kwds and len(x) == 1: P = parent(x[0]) if K.has_coerce_map_from(P): x = K(x[0]) return K(zmod_poly_evaluate(&self.x, x)) elif self._parent.has_coerce_map_from(P): y = self._parent(x[0]) t = self._new() zmod_poly_compose_horner(&t.x, &self.x, &(x).x) if zmod_poly_degree(&y.x) == 0: c = zmod_poly_evaluate(&self.x, zmod_poly_get_coeff_ui(&y.x, 0)) zmod_poly_set_coeff_ui(&t.x, 0, c) elif zmod_poly_degree(&y.x) == 1 and zmod_poly_get_coeff_ui(&y.x, 0) == 0: c = zmod_poly_get_coeff_ui(&y.x, 1) if c == 1: return self zmod_poly_compose_horner(&t.x, &self.x, &y.x) return t except TypeError: pass return Polynomial.__call__(self, *x, **kwds) def reverse(self, long degree=-1): """ Return self with the coefficients reversed. EXAMPLES:: sage: R. = GF(101)[] sage: f = x^3 - x + 2; f x^3 + 100*x + 2 sage: f.reverse() 2*x^3 + 100*x^2 + 1 sage: f.reverse() == f(1/x) * x^f.degree() True Note that if `f` has zero constant coefficient, its reverse will have lower degree. :: sage: f = x^3 + 2*x sage: f.reverse() 2*x^2 + 1 In this case, reverse is not an involution unless we explicitly specify a degree. :: sage: f x^3 + 2*x sage: f.reverse().reverse() x^2 + 2 sage: f.reverse(5).reverse(5) x^3 + 2*x """ if degree == -1: degree = zmod_poly_degree(&self.x) cdef Polynomial_zmod_flint t = self._new() zmod_poly_reverse(&t.x, &self.x, degree+1) return t @coerce_binop def resultant(self, Polynomial_zmod_flint other):