# Ticket #2329: trac-2329-pt2.patch

File trac-2329-pt2.patch, 11.5 KB (added by craigcitro, 14 years ago)
• ## sage/libs/pari/gen.pyx

```# HG changeset patch
# User Craig Citro <craigcitro@gmail.com>
# Date 1207438572 25200
# Node ID 662372103fcc363bcd37657d823cbcabe94b003b
# Parent  4cfc0b6188739fa51afed788a8bb38ab00433fe1
Clean up trac #2329.

diff -r 4cfc0b618873 -r 662372103fcc sage/libs/pari/gen.pyx```
 a cdef class gen(sage.structure.element.Ri _sig_on return self.new_gen(bnfisintnorm(self.g, t0)) def bnfisnorm(self, gen x, long flag=0): t0GEN(x) _sig_on return self.new_gen(bnfisnorm(t0, self.g, flag, prec)) def bnfisprincipal(self, x, long flag=1): t0GEN(x) _sig_on
• ## sage/rings/integer.pyx

`diff -r 4cfc0b618873 -r 662372103fcc sage/rings/integer.pyx`
 a cdef class Integer(sage.structure.elemen def is_norm(self, K, certify=True): r""" Determine whether or not self is a norm from K. If so, return an element of K whose norm is self. If not, return False. Determine whether or not self is an absolute norm from K. This uses Pari's rnfisnorm, whose results (in some cases) are dependent on GRH. If certify=True, we have Pari that the cdef class Integer(sage.structure.elemen EXAMPLES: sage: K = NumberField(x**2-2,'beta') sage: 4.is_norm(K) sage: n = 4 sage: n.is_norm(K) True sage: 5.is_norm(K) False sage: 7.is_norm(QQ) True """ try: self.element_of_norm(K, certify=certify) return True except ValueError: return False def element_of_norm(self, K, certify=True): """ Return an element of the number field K whose absolute norm is self. If no such element exists, raise a ValueError. This uses Pari's bnfisnorm, whose results (in some cases) are dependent on GRH. If certify=True, we have Pari that the results are correct independent of GRH. In the case that K/QQ is Galois, the results are valid even without certification. EXAMPLES: sage: K = NumberField(x**2-2,'beta') sage: n = 4 sage: n.element_of_norm(K) -4*beta + 6 sage: 4.is_norm(K).norm() sage: n.element_of_norm(K).norm() 4 sage: 5.is_norm(K) False sage: 7.is_norm(QQ) sage: n = 5 sage: n.element_of_norm(K) Traceback (most recent call last): ... ValueError: 5 is not a norm from Number Field in beta with defining polynomial x^2 - 2 sage: n = 7 sage: n.element_of_norm(QQ) 7 """ from sage.rings.number_field.all import is_NumberField if not is_NumberField(K): raise ValueError, "K must be a NumberField" if K.degree() == 1 or self.is_zero(): return self if certify: K.pari_bnf_certify() rel_poly = K.pari_bnf() base_poly = sage.libs.pari.gen.pari('y') rnf_data = base_poly.rnfisnorminit(rel_poly) x, q = self._pari_().rnfisnorm(rnf_data) x, q = self._pari_().bnfisnorm(K.pari_bnf()) if not q.is_one(): return False raise ValueError, "%s is not a norm from %s"%(self, K) else: deg = x[0].poldegree()+1 coeffs = [ x[0].polcoeff(i)._sage_() for i in range(deg) ] return K(coeffs) def elements_of_norm(self, K, proof=True): """ Alias for K.elements_of_norm(self, proof). EXAMPLES: sage: K = NumberField(x**3+5, 'alpha') ; n = 5 sage: n.elements_of_norm(K) [-alpha] sage: K.elements_of_norm(n) [-alpha] sage: [ y.norm() for y in n.elements_of_norm(K) ] [5] """ return K.elements_of_norm(self, proof=proof) def jacobi(self, b): r"""
• ## sage/rings/number_field/number_field.py

`diff -r 4cfc0b618873 -r 662372103fcc sage/rings/number_field/number_field.py`
 a class NumberField_generic(number_field_b sage: K.elements_of_norm(50) [7*a - 1, -5*a + 5, a - 7]           # 32-bit [7*a - 1, -5*a + 5, -7*a - 1]        # 64-bit sage: [ y.norm() for y in K.elements_of_norm(50) ] [50, 50, 50] """ proof = proof_flag(proof) B = self.pari_bnf(proof).bnfisintnorm(n) R = self.polynomial().parent() return [self(QQ['x'](R(g))) for g in B] # At this point, we should just coerce these solutions # into self. However, Pari's bnfisintnorm sometimes returns # elements off by -1, so we correct this. elems = [ self(R(g)) for g in B ] for i in range(len(elems)): x = elems[i] val = x.norm() if val == QQ(-1*n) and (self.degree()%2): elems[i] = -1*elems[i] elif val != QQ(n): raise ValueError, "bug in value returned by Pari's bnfisintnorm. Please report this to sage-devel@googlegroups.com." return elems def extension(self, poly, name=None, names=None, check=True): """
• ## sage/rings/number_field/number_field_element.pyx

`diff -r 4cfc0b618873 -r 662372103fcc sage/rings/number_field/number_field_element.pyx`
 a cdef class NumberFieldElement(FieldEleme def is_norm(self, L, certify=True, rnf_data=None): r""" Determine whether or not self is a norm from L. If so, return an element of L whose norm is self. If not, return False. Determine whether or not self is a norm from L. This uses Pari's rnfisnorm, whose results (in some cases) are dependent on GRH. If certify=True, we have Pari cdef class NumberFieldElement(FieldEleme that the results are valid even without certification. EXAMPLES: sage: K = NumberField(x**3+5, 'beta') sage: K = NumberField(x**3+5, 'beta') ; beta = K.gen(0) sage: L = K.extension(x**2+x+1, 'gamma') sage: beta.is_norm(L) False sage: (beta**2).is_norm(L) beta*gamma + beta sage: (beta**2).is_norm(L).norm(K) True """ try: self.element_of_norm(L, certify=certify, rnf_data=rnf_data) return True except ValueError: return False def element_of_norm(self, L, certify=True, rnf_data=None): r""" Return an element of L whose relative norm to self.parent() is self. If no such element exists, raise a ValueError. This uses Pari's rnfisnorm, whose results (in some cases) are dependent on GRH. If certify=True, we have Pari that the results are correct independent of GRH. In the case that L/self.parent() is Galois, the Pari documentation suggests that the results are valid even without certification. EXAMPLES: sage: K = NumberField(x**3+5, 'beta') ; beta = K.gen(0) sage: L = K.extension(x**2+x+1, 'gamma') sage: beta.element_of_norm(L) Traceback (most recent call last): ... ValueError: beta is not a norm from Number Field in gamma with defining polynomial x^2 + x + 1 over its base field sage: (beta**2).element_of_norm(L) -beta sage: (beta**2).element_of_norm(L).norm(K) beta^2 """ K = self.parent() cdef class NumberFieldElement(FieldEleme x,q = self._pari_('y').rnfisnorm(rnf_data) if not q.is_one(): return False raise ValueError, "%s is not a norm from %s"%(self, L) else: deg = x[0].poldegree()+1 rep = [ x[0].polcoeff(i) for i in range(deg) ]
• ## sage/rings/rational.pyx

`diff -r 4cfc0b618873 -r 662372103fcc sage/rings/rational.pyx`
 a cdef class Rational(sage.structure.eleme def is_norm(self, K, certify=True): r""" Determine whether or not self is a norm from K. If so, return an element of K whose norm is self. If not, return False. Determine whether or not self is an absolute norm from K. This uses Pari's rnfisnorm, whose results (in some cases) are dependent on GRH. If certify=True, we have Pari that the cdef class Rational(sage.structure.eleme EXAMPLES: sage: K = NumberField(x**2-2,'beta') sage: (1/7).is_norm(K) 1/7*beta + 3/7 True sage: (1/10).is_norm(K) False sage: (1/691).is_norm(QQ) True sage: 0.is_norm(K) True """ try: self.element_of_norm(K, certify=certify) return True except ValueError: return False def element_of_norm(self, K, certify=True): r""" Return an element of the number field K whose absolute norm is self. If no such element exists, raise a ValueError. This uses Pari's bnfisnorm, whose results (in some cases) are dependent on GRH. If certify=True, we have Pari that the results are correct independent of GRH. In the case that K/QQ is Galois, the results are valid even without certification. EXAMPLES: sage: K = NumberField(x**2-2,'beta') sage: (1/7).element_of_norm(K) 1/7*beta + 3/7 sage: (1/10).element_of_norm(K) Traceback (most recent call last): ... ValueError: 1/10 is not a norm from Number Field in beta with defining polynomial x^2 - 2 sage: (1/691).element_of_norm(QQ) 1/691 sage: 0.is_norm(K) sage: n = 0 ; n.element_of_norm(K) 0 """ from sage.rings.number_field.all import is_NumberField cdef class Rational(sage.structure.eleme if not is_NumberField(K): raise ValueError, "K must be a NumberField" if K.degree() == 1 or self.is_zero(): return self if certify: K.pari_bnf_certify() rel_poly = K.pari_bnf() base_poly = sage.libs.pari.gen.pari('y') rnf_data = base_poly.rnfisnorminit(rel_poly) x, q = self._pari_().rnfisnorm(rnf_data) x, q = self._pari_().bnfisnorm(K.pari_bnf()) if not q.is_one(): return False raise ValueError, "%s is not a norm from %s"%(self, K) else: deg = x[0].poldegree()+1 coeffs = [ QQ(x[0].polcoeff(i)) for i in range(deg) ] return K(coeffs) def squarefree_part(self): """