| 1 | """ |
|---|
| 2 | Number Field Elements |
|---|
| 3 | |
|---|
| 4 | AUTHORS: |
|---|
| 5 | |
|---|
| 6 | - William Stein: version before it got Cython'd |
|---|
| 7 | |
|---|
| 8 | - Joel B. Mohler (2007-03-09): First reimplementation in Cython |
|---|
| 9 | |
|---|
| 10 | - William Stein (2007-09-04): add doctests |
|---|
| 11 | |
|---|
| 12 | - Robert Bradshaw (2007-09-15): specialized classes for relative and |
|---|
| 13 | absolute elements |
|---|
| 14 | |
|---|
| 15 | - John Cremona (2009-05-15): added support for local and global |
|---|
| 16 | logarithmic heights. |
|---|
| 17 | |
|---|
| 18 | - Robert Harron (2012-08): conjugate() now works for all fields contained in |
|---|
| 19 | CM fields |
|---|
| 20 | |
|---|
| 21 | """ |
|---|
| 22 | #***************************************************************************** |
|---|
| 23 | # Copyright (C) 2004, 2007 William Stein <wstein@gmail.com> |
|---|
| 24 | # |
|---|
| 25 | # Distributed under the terms of the GNU General Public License (GPL) |
|---|
| 26 | # as published by the Free Software Foundation; either version 2 of |
|---|
| 27 | # the License, or (at your option) any later version. |
|---|
| 28 | # http://www.gnu.org/licenses/ |
|---|
| 29 | #***************************************************************************** |
|---|
| 30 | |
|---|
| 31 | import operator |
|---|
| 32 | |
|---|
| 33 | include '../../ext/interrupt.pxi' |
|---|
| 34 | include '../../ext/python_int.pxi' |
|---|
| 35 | include "../../ext/stdsage.pxi" |
|---|
| 36 | |
|---|
| 37 | import sage.rings.field_element |
|---|
| 38 | import sage.rings.infinity |
|---|
| 39 | import sage.rings.polynomial.polynomial_element |
|---|
| 40 | import sage.rings.rational_field |
|---|
| 41 | import sage.rings.rational |
|---|
| 42 | import sage.rings.integer_ring |
|---|
| 43 | import sage.rings.integer |
|---|
| 44 | import sage.rings.arith |
|---|
| 45 | |
|---|
| 46 | import number_field |
|---|
| 47 | |
|---|
| 48 | from sage.rings.integer_ring cimport IntegerRing_class |
|---|
| 49 | from sage.rings.rational cimport Rational |
|---|
| 50 | |
|---|
| 51 | from sage.modules.free_module_element import vector |
|---|
| 52 | |
|---|
| 53 | from sage.libs.all import pari_gen, pari |
|---|
| 54 | from sage.libs.pari.gen import PariError |
|---|
| 55 | from sage.structure.element cimport Element, generic_power_c |
|---|
| 56 | from sage.structure.element import canonical_coercion, parent |
|---|
| 57 | |
|---|
| 58 | QQ = sage.rings.rational_field.QQ |
|---|
| 59 | ZZ = sage.rings.integer_ring.ZZ |
|---|
| 60 | Integer_sage = sage.rings.integer.Integer |
|---|
| 61 | |
|---|
| 62 | from sage.rings.real_mpfi import RealInterval |
|---|
| 63 | |
|---|
| 64 | from sage.rings.complex_field import ComplexField |
|---|
| 65 | CC = ComplexField(53) |
|---|
| 66 | |
|---|
| 67 | # this is a threshold for the charpoly() methods in this file |
|---|
| 68 | # for degrees <= this threshold, pari is used |
|---|
| 69 | # for degrees > this threshold, sage matrices are used |
|---|
| 70 | # the value was decided by running a tuning script on a number of |
|---|
| 71 | # architectures; you can find this script attached to trac |
|---|
| 72 | # ticket 5213 |
|---|
| 73 | TUNE_CHARPOLY_NF = 25 |
|---|
| 74 | |
|---|
| 75 | def is_NumberFieldElement(x): |
|---|
| 76 | """ |
|---|
| 77 | Return True if x is of type NumberFieldElement, i.e., an element of |
|---|
| 78 | a number field. |
|---|
| 79 | |
|---|
| 80 | EXAMPLES:: |
|---|
| 81 | |
|---|
| 82 | sage: from sage.rings.number_field.number_field_element import is_NumberFieldElement |
|---|
| 83 | sage: is_NumberFieldElement(2) |
|---|
| 84 | False |
|---|
| 85 | sage: k.<a> = NumberField(x^7 + 17*x + 1) |
|---|
| 86 | sage: is_NumberFieldElement(a+1) |
|---|
| 87 | True |
|---|
| 88 | """ |
|---|
| 89 | return PY_TYPE_CHECK(x, NumberFieldElement) |
|---|
| 90 | |
|---|
| 91 | def __create__NumberFieldElement_version0(parent, poly): |
|---|
| 92 | """ |
|---|
| 93 | Used in unpickling elements of number fields pickled under very old Sage versions. |
|---|
| 94 | |
|---|
| 95 | EXAMPLE:: |
|---|
| 96 | |
|---|
| 97 | sage: k.<a> = NumberField(x^3 - 2) |
|---|
| 98 | sage: R.<z> = QQ[] |
|---|
| 99 | sage: sage.rings.number_field.number_field_element.__create__NumberFieldElement_version0(k, z^2 + z + 1) |
|---|
| 100 | a^2 + a + 1 |
|---|
| 101 | """ |
|---|
| 102 | return NumberFieldElement(parent, poly) |
|---|
| 103 | |
|---|
| 104 | def __create__NumberFieldElement_version1(parent, cls, poly): |
|---|
| 105 | """ |
|---|
| 106 | Used in unpickling elements of number fields. |
|---|
| 107 | |
|---|
| 108 | EXAMPLES: |
|---|
| 109 | |
|---|
| 110 | Since this is just used in unpickling, we unpickle. |
|---|
| 111 | |
|---|
| 112 | :: |
|---|
| 113 | |
|---|
| 114 | sage: k.<a> = NumberField(x^3 - 2) |
|---|
| 115 | sage: loads(dumps(a+1)) == a + 1 # indirect doctest |
|---|
| 116 | True |
|---|
| 117 | |
|---|
| 118 | This also gets called for unpickling order elements; we check that #6462 is |
|---|
| 119 | fixed:: |
|---|
| 120 | |
|---|
| 121 | sage: L = NumberField(x^3 - x - 1,'a'); OL = L.maximal_order(); w = OL.0 |
|---|
| 122 | sage: loads(dumps(w)) == w # indirect doctest |
|---|
| 123 | True |
|---|
| 124 | """ |
|---|
| 125 | return cls(parent, poly) |
|---|
| 126 | |
|---|
| 127 | def _inverse_mod_generic(elt, I): |
|---|
| 128 | r""" |
|---|
| 129 | Return an inverse of elt modulo the given ideal. This is a separate |
|---|
| 130 | function called from each of the OrderElement_xxx classes, since |
|---|
| 131 | otherwise we'd have to have the same code three times over (there |
|---|
| 132 | is no OrderElement_generic class - no multiple inheritance). See |
|---|
| 133 | trac 4190. |
|---|
| 134 | |
|---|
| 135 | EXAMPLES:: |
|---|
| 136 | |
|---|
| 137 | sage: OE = NumberField(x^3 - x + 2, 'w').ring_of_integers() |
|---|
| 138 | sage: w = OE.ring_generators()[0] |
|---|
| 139 | sage: from sage.rings.number_field.number_field_element import _inverse_mod_generic |
|---|
| 140 | sage: _inverse_mod_generic(w, 13*OE) |
|---|
| 141 | 6*w^2 - 6 |
|---|
| 142 | """ |
|---|
| 143 | from sage.matrix.constructor import matrix |
|---|
| 144 | R = elt.parent() |
|---|
| 145 | try: |
|---|
| 146 | I = R.ideal(I) |
|---|
| 147 | except ValueError: |
|---|
| 148 | raise ValueError, "inverse is only defined modulo integral ideals" |
|---|
| 149 | if I == 0: |
|---|
| 150 | raise ValueError, "inverse is not defined modulo the zero ideal" |
|---|
| 151 | n = R.absolute_degree() |
|---|
| 152 | m = matrix(ZZ, map(R.coordinates, I.integral_basis() + [elt*s for s in R.gens()])) |
|---|
| 153 | a, b = m.echelon_form(transformation=True) |
|---|
| 154 | if a[0:n] != 1: |
|---|
| 155 | raise ZeroDivisionError, "%s is not invertible modulo %s" % (elt, I) |
|---|
| 156 | v = R.coordinates(1) |
|---|
| 157 | y = R(0) |
|---|
| 158 | for j in xrange(n): |
|---|
| 159 | if v[j] != 0: |
|---|
| 160 | y += v[j] * sum([b[j,i+n] * R.gen(i) for i in xrange(n)]) |
|---|
| 161 | return I.small_residue(y) |
|---|
| 162 | |
|---|
| 163 | __pynac_pow = False |
|---|
| 164 | |
|---|
| 165 | cdef class NumberFieldElement(FieldElement): |
|---|
| 166 | """ |
|---|
| 167 | An element of a number field. |
|---|
| 168 | |
|---|
| 169 | EXAMPLES:: |
|---|
| 170 | |
|---|
| 171 | sage: k.<a> = NumberField(x^3 + x + 1) |
|---|
| 172 | sage: a^3 |
|---|
| 173 | -a - 1 |
|---|
| 174 | """ |
|---|
| 175 | cdef _new(self): |
|---|
| 176 | """ |
|---|
| 177 | Quickly creates a new initialized NumberFieldElement with the same |
|---|
| 178 | parent as self. |
|---|
| 179 | """ |
|---|
| 180 | cdef NumberFieldElement x |
|---|
| 181 | x = <NumberFieldElement>PY_NEW_SAME_TYPE(self) |
|---|
| 182 | x._parent = self._parent |
|---|
| 183 | x.__fld_numerator = self.__fld_numerator |
|---|
| 184 | x.__fld_denominator = self.__fld_denominator |
|---|
| 185 | return x |
|---|
| 186 | |
|---|
| 187 | cdef number_field(self): |
|---|
| 188 | r""" |
|---|
| 189 | |
|---|
| 190 | Return the number field of self. Only accessible from Cython. |
|---|
| 191 | EXAMPLE:: |
|---|
| 192 | |
|---|
| 193 | sage: K.<a> = NumberField(x^3 + 3) |
|---|
| 194 | sage: a._number_field() # indirect doctest |
|---|
| 195 | Number Field in a with defining polynomial x^3 + 3 |
|---|
| 196 | """ |
|---|
| 197 | return self._parent |
|---|
| 198 | |
|---|
| 199 | def _number_field(self): |
|---|
| 200 | r""" |
|---|
| 201 | EXAMPLE:: |
|---|
| 202 | |
|---|
| 203 | sage: K.<a> = NumberField(x^3 + 3) |
|---|
| 204 | sage: a._number_field() |
|---|
| 205 | Number Field in a with defining polynomial x^3 + 3 |
|---|
| 206 | """ |
|---|
| 207 | return self.number_field() |
|---|
| 208 | |
|---|
| 209 | def __init__(self, parent, f): |
|---|
| 210 | """ |
|---|
| 211 | INPUT: |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | - ``parent`` - a number field |
|---|
| 215 | |
|---|
| 216 | - ``f`` - defines an element of a number field. |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | EXAMPLES: |
|---|
| 220 | |
|---|
| 221 | The following examples illustrate creation of elements of |
|---|
| 222 | number fields, and some basic arithmetic. |
|---|
| 223 | |
|---|
| 224 | First we define a polynomial over Q:: |
|---|
| 225 | |
|---|
| 226 | sage: R.<x> = PolynomialRing(QQ) |
|---|
| 227 | sage: f = x^2 + 1 |
|---|
| 228 | |
|---|
| 229 | Next we use f to define the number field:: |
|---|
| 230 | |
|---|
| 231 | sage: K.<a> = NumberField(f); K |
|---|
| 232 | Number Field in a with defining polynomial x^2 + 1 |
|---|
| 233 | sage: a = K.gen() |
|---|
| 234 | sage: a^2 |
|---|
| 235 | -1 |
|---|
| 236 | sage: (a+1)^2 |
|---|
| 237 | 2*a |
|---|
| 238 | sage: a^2 |
|---|
| 239 | -1 |
|---|
| 240 | sage: z = K(5); 1/z |
|---|
| 241 | 1/5 |
|---|
| 242 | |
|---|
| 243 | We create a cube root of 2:: |
|---|
| 244 | |
|---|
| 245 | sage: K.<b> = NumberField(x^3 - 2) |
|---|
| 246 | sage: b = K.gen() |
|---|
| 247 | sage: b^3 |
|---|
| 248 | 2 |
|---|
| 249 | sage: (b^2 + b + 1)^3 |
|---|
| 250 | 12*b^2 + 15*b + 19 |
|---|
| 251 | |
|---|
| 252 | We can create number field elements from PARI:: |
|---|
| 253 | |
|---|
| 254 | sage: K.<a> = NumberField(x^3 - 17) |
|---|
| 255 | sage: K(pari(42)) |
|---|
| 256 | 42 |
|---|
| 257 | sage: K(pari("5/3")) |
|---|
| 258 | 5/3 |
|---|
| 259 | sage: K(pari("[3/2, -5, 0]~")) # Uses Z-basis |
|---|
| 260 | -5/3*a^2 + 5/3*a - 1/6 |
|---|
| 261 | |
|---|
| 262 | From a PARI polynomial or ``POLMOD``, note that the variable |
|---|
| 263 | name does not matter:: |
|---|
| 264 | |
|---|
| 265 | sage: K(pari("-5/3*q^2 + 5/3*q - 1/6")) |
|---|
| 266 | -5/3*a^2 + 5/3*a - 1/6 |
|---|
| 267 | sage: K(pari("Mod(-5/3*q^2 + 5/3*q - 1/6, q^3 - 17)")) |
|---|
| 268 | -5/3*a^2 + 5/3*a - 1/6 |
|---|
| 269 | sage: K(pari("x^5/17")) |
|---|
| 270 | a^2 |
|---|
| 271 | sage: K(pari("Mod(-5/3*q^2 + 5/3*q - 1/6, q^3 - 999)")) # Wrong modulus |
|---|
| 272 | Traceback (most recent call last): |
|---|
| 273 | ... |
|---|
| 274 | TypeError: Coercion of PARI polmod with modulus q^3 - 999 into number field with defining polynomial x^3 - 17 failed |
|---|
| 275 | |
|---|
| 276 | This example illustrates save and load:: |
|---|
| 277 | |
|---|
| 278 | sage: K.<a> = NumberField(x^17 - 2) |
|---|
| 279 | sage: s = a^15 - 19*a + 3 |
|---|
| 280 | sage: loads(s.dumps()) == s |
|---|
| 281 | True |
|---|
| 282 | |
|---|
| 283 | TESTS: |
|---|
| 284 | |
|---|
| 285 | Test round-trip conversion to PARI and back:: |
|---|
| 286 | |
|---|
| 287 | sage: x = polygen(QQ) |
|---|
| 288 | sage: K.<a> = NumberField(x^3 - 1/2*x + 1/3) |
|---|
| 289 | sage: b = K.random_element() |
|---|
| 290 | sage: K(pari(b)) == b |
|---|
| 291 | True |
|---|
| 292 | """ |
|---|
| 293 | sage.rings.field_element.FieldElement.__init__(self, parent) |
|---|
| 294 | self.__fld_numerator, self.__fld_denominator = parent.absolute_polynomial_ntl() |
|---|
| 295 | |
|---|
| 296 | cdef ZZ_c coeff |
|---|
| 297 | if isinstance(f, (int, long, Integer_sage)): |
|---|
| 298 | # set it up and exit immediately |
|---|
| 299 | # fast pathway |
|---|
| 300 | (<Integer>ZZ(f))._to_ZZ(&coeff) |
|---|
| 301 | ZZX_SetCoeff( self.__numerator, 0, coeff ) |
|---|
| 302 | ZZ_conv_from_int( self.__denominator, 1 ) |
|---|
| 303 | return |
|---|
| 304 | |
|---|
| 305 | elif isinstance(f, NumberFieldElement): |
|---|
| 306 | if PY_TYPE(self) is PY_TYPE(f): |
|---|
| 307 | self.__numerator = (<NumberFieldElement>f).__numerator |
|---|
| 308 | self.__denominator = (<NumberFieldElement>f).__denominator |
|---|
| 309 | return |
|---|
| 310 | else: |
|---|
| 311 | f = f.polynomial() |
|---|
| 312 | |
|---|
| 313 | from sage.rings.number_field import number_field_rel |
|---|
| 314 | if isinstance(parent, number_field_rel.NumberField_relative): |
|---|
| 315 | ppr = parent.base_field().polynomial_ring() |
|---|
| 316 | else: |
|---|
| 317 | ppr = parent.polynomial_ring() |
|---|
| 318 | |
|---|
| 319 | cdef long i |
|---|
| 320 | if isinstance(f, pari_gen): |
|---|
| 321 | if f.type() in ["t_INT", "t_FRAC", "t_POL"]: |
|---|
| 322 | pass |
|---|
| 323 | elif f.type() == "t_POLMOD": |
|---|
| 324 | # Check whether we are dealing with a *relative* |
|---|
| 325 | # number field element |
|---|
| 326 | if parent.is_relative(): |
|---|
| 327 | # If the modulus is a polynomial with polynomial |
|---|
| 328 | # coefficients, then the element is relative. |
|---|
| 329 | fmod = f.mod() |
|---|
| 330 | for i from 0 <= i <= fmod.poldegree(): |
|---|
| 331 | if fmod.polcoeff(i).type() in ["t_POL", "t_POLMOD"]: |
|---|
| 332 | # Convert relative element to absolute |
|---|
| 333 | # This returns a polynomial, not a polmod |
|---|
| 334 | f = parent.pari_rnf().rnfeltreltoabs(f) |
|---|
| 335 | break |
|---|
| 336 | # Check that the modulus is actually the defining polynomial |
|---|
| 337 | # of the number field. |
|---|
| 338 | # Unfortunately, this check only works for absolute elements |
|---|
| 339 | # since the rnfeltreltoabs() destroys all information about |
|---|
| 340 | # the number field. |
|---|
| 341 | if f.type() == "t_POLMOD": |
|---|
| 342 | fmod = f.mod() |
|---|
| 343 | if fmod != parent.pari_polynomial(fmod.variable()): |
|---|
| 344 | raise TypeError("Coercion of PARI polmod with modulus %s into number field with defining polynomial %s failed"%(fmod, parent.pari_polynomial())) |
|---|
| 345 | f = f.lift() |
|---|
| 346 | else: |
|---|
| 347 | f = parent.pari_nf().nfbasistoalg_lift(f) |
|---|
| 348 | f = ppr(f) |
|---|
| 349 | if f.degree() >= parent.absolute_degree(): |
|---|
| 350 | from sage.rings.number_field import number_field_rel |
|---|
| 351 | if isinstance(parent, number_field_rel.NumberField_relative): |
|---|
| 352 | f %= ppr(parent.absolute_polynomial()) |
|---|
| 353 | else: |
|---|
| 354 | f %= parent.polynomial() |
|---|
| 355 | |
|---|
| 356 | # Set Denominator |
|---|
| 357 | den = f.denominator() |
|---|
| 358 | (<Integer>ZZ(den))._to_ZZ(&self.__denominator) |
|---|
| 359 | |
|---|
| 360 | num = f * den |
|---|
| 361 | for i from 0 <= i <= num.degree(): |
|---|
| 362 | (<Integer>ZZ(num[i]))._to_ZZ(&coeff) |
|---|
| 363 | ZZX_SetCoeff( self.__numerator, i, coeff ) |
|---|
| 364 | |
|---|
| 365 | def __cinit__(self): |
|---|
| 366 | r""" |
|---|
| 367 | Initialise C variables. |
|---|
| 368 | |
|---|
| 369 | EXAMPLE:: |
|---|
| 370 | |
|---|
| 371 | sage: K.<b> = NumberField(x^3 - 2) |
|---|
| 372 | sage: b = K.gen(); b # indirect doctest |
|---|
| 373 | b |
|---|
| 374 | """ |
|---|
| 375 | ZZX_construct(&self.__numerator) |
|---|
| 376 | ZZ_construct(&self.__denominator) |
|---|
| 377 | |
|---|
| 378 | def __dealloc__(self): |
|---|
| 379 | ZZX_destruct(&self.__numerator) |
|---|
| 380 | ZZ_destruct(&self.__denominator) |
|---|
| 381 | |
|---|
| 382 | def _lift_cyclotomic_element(self, new_parent, bint check=True, int rel=0): |
|---|
| 383 | """ |
|---|
| 384 | Creates an element of the passed field from this field. This is |
|---|
| 385 | specific to creating elements in a cyclotomic field from elements |
|---|
| 386 | in another cyclotomic field, in the case that |
|---|
| 387 | self.number_field()._n() divides new_parent()._n(). This |
|---|
| 388 | function aims to make this common coercion extremely fast! |
|---|
| 389 | |
|---|
| 390 | More general coercion (i.e. of zeta6 into CyclotomicField(3)) is |
|---|
| 391 | implemented in the _coerce_from_other_cyclotomic_field method |
|---|
| 392 | of a CyclotomicField. |
|---|
| 393 | |
|---|
| 394 | EXAMPLES:: |
|---|
| 395 | |
|---|
| 396 | sage: C.<zeta5>=CyclotomicField(5) |
|---|
| 397 | sage: CyclotomicField(10)(zeta5+1) # The function _lift_cyclotomic_element does the heavy lifting in the background |
|---|
| 398 | zeta10^2 + 1 |
|---|
| 399 | sage: (zeta5+1)._lift_cyclotomic_element(CyclotomicField(10)) # There is rarely a purpose to call this function directly |
|---|
| 400 | zeta10^2 + 1 |
|---|
| 401 | sage: cf4 = CyclotomicField(4) |
|---|
| 402 | sage: cf1 = CyclotomicField(1) ; one = cf1.0 |
|---|
| 403 | sage: cf4(one) |
|---|
| 404 | 1 |
|---|
| 405 | sage: type(cf4(1)) |
|---|
| 406 | <type 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic'> |
|---|
| 407 | sage: cf33 = CyclotomicField(33) ; z33 = cf33.0 |
|---|
| 408 | sage: cf66 = CyclotomicField(66) ; z66 = cf66.0 |
|---|
| 409 | sage: z33._lift_cyclotomic_element(cf66) |
|---|
| 410 | zeta66^2 |
|---|
| 411 | sage: z66._lift_cyclotomic_element(cf33) |
|---|
| 412 | Traceback (most recent call last): |
|---|
| 413 | ... |
|---|
| 414 | TypeError: The zeta_order of the new field must be a multiple of the zeta_order of the original. |
|---|
| 415 | sage: cf33(z66) |
|---|
| 416 | -zeta33^17 |
|---|
| 417 | |
|---|
| 418 | AUTHORS: |
|---|
| 419 | |
|---|
| 420 | - Joel B. Mohler |
|---|
| 421 | |
|---|
| 422 | - Craig Citro (fixed behavior for different representation of |
|---|
| 423 | quadratic field elements) |
|---|
| 424 | """ |
|---|
| 425 | if check: |
|---|
| 426 | if not isinstance(self.number_field(), number_field.NumberField_cyclotomic) \ |
|---|
| 427 | or not isinstance(new_parent, number_field.NumberField_cyclotomic): |
|---|
| 428 | raise TypeError, "The field and the new parent field must both be cyclotomic fields." |
|---|
| 429 | |
|---|
| 430 | if rel == 0: |
|---|
| 431 | small_order = self.number_field()._n() |
|---|
| 432 | large_order = new_parent._n() |
|---|
| 433 | |
|---|
| 434 | try: |
|---|
| 435 | rel = ZZ(large_order / small_order) |
|---|
| 436 | except TypeError: |
|---|
| 437 | raise TypeError, "The zeta_order of the new field must be a multiple of the zeta_order of the original." |
|---|
| 438 | |
|---|
| 439 | ## degree 2 is handled differently, because elements are |
|---|
| 440 | ## represented differently |
|---|
| 441 | if new_parent.degree() == 2: |
|---|
| 442 | if rel == 1: |
|---|
| 443 | return new_parent._element_class(new_parent, self) |
|---|
| 444 | else: |
|---|
| 445 | return self.polynomial()(new_parent.gen()**rel) |
|---|
| 446 | |
|---|
| 447 | cdef NumberFieldElement x = <NumberFieldElement>PY_NEW_SAME_TYPE(self) |
|---|
| 448 | x._parent = <ParentWithBase>new_parent |
|---|
| 449 | x.__fld_numerator, x.__fld_denominator = new_parent.polynomial_ntl() |
|---|
| 450 | x.__denominator = self.__denominator |
|---|
| 451 | cdef ZZX_c result |
|---|
| 452 | cdef ZZ_c tmp |
|---|
| 453 | cdef int i |
|---|
| 454 | cdef ntl_ZZX _num |
|---|
| 455 | cdef ntl_ZZ _den |
|---|
| 456 | for i from 0 <= i <= ZZX_deg(self.__numerator): |
|---|
| 457 | tmp = ZZX_coeff(self.__numerator, i) |
|---|
| 458 | ZZX_SetCoeff(result, i*rel, tmp) |
|---|
| 459 | ZZX_rem(x.__numerator, result, x.__fld_numerator.x) |
|---|
| 460 | return x |
|---|
| 461 | |
|---|
| 462 | def __reduce__(self): |
|---|
| 463 | """ |
|---|
| 464 | Used in pickling number field elements. |
|---|
| 465 | |
|---|
| 466 | Note for developers: If this is changed, please also change the doctests of __create__NumberFieldElement_version1. |
|---|
| 467 | |
|---|
| 468 | EXAMPLES:: |
|---|
| 469 | |
|---|
| 470 | sage: k.<a> = NumberField(x^3 - 17*x^2 + 1) |
|---|
| 471 | sage: t = a.__reduce__(); t |
|---|
| 472 | (<built-in function __create__NumberFieldElement_version1>, (Number Field in a with defining polynomial x^3 - 17*x^2 + 1, <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>, x)) |
|---|
| 473 | sage: t[0](*t[1]) == a |
|---|
| 474 | True |
|---|
| 475 | """ |
|---|
| 476 | return __create__NumberFieldElement_version1, \ |
|---|
| 477 | (self.parent(), type(self), self.polynomial()) |
|---|
| 478 | |
|---|
| 479 | def _repr_(self): |
|---|
| 480 | """ |
|---|
| 481 | String representation of this number field element, which is just a |
|---|
| 482 | polynomial in the generator. |
|---|
| 483 | |
|---|
| 484 | EXAMPLES:: |
|---|
| 485 | |
|---|
| 486 | sage: k.<a> = NumberField(x^2 + 2) |
|---|
| 487 | sage: b = (2/3)*a + 3/5 |
|---|
| 488 | sage: b._repr_() |
|---|
| 489 | '2/3*a + 3/5' |
|---|
| 490 | """ |
|---|
| 491 | x = self.polynomial() |
|---|
| 492 | K = self.number_field() |
|---|
| 493 | return str(x).replace(x.parent().variable_name(), K.variable_name()) |
|---|
| 494 | |
|---|
| 495 | def _im_gens_(self, codomain, im_gens): |
|---|
| 496 | """ |
|---|
| 497 | This is used in computing homomorphisms between number fields. |
|---|
| 498 | |
|---|
| 499 | EXAMPLES:: |
|---|
| 500 | |
|---|
| 501 | sage: k.<a> = NumberField(x^2 - 2) |
|---|
| 502 | sage: m.<b> = NumberField(x^4 - 2) |
|---|
| 503 | sage: phi = k.hom([b^2]) |
|---|
| 504 | sage: phi(a+1) |
|---|
| 505 | b^2 + 1 |
|---|
| 506 | sage: (a+1)._im_gens_(m, [b^2]) |
|---|
| 507 | b^2 + 1 |
|---|
| 508 | """ |
|---|
| 509 | # NOTE -- if you ever want to change this so relative number |
|---|
| 510 | # fields are in terms of a root of a poly. The issue is that |
|---|
| 511 | # elements of a relative number field are represented in terms |
|---|
| 512 | # of a generator for the absolute field. However the morphism |
|---|
| 513 | # gives the image of gen, which need not be a generator for |
|---|
| 514 | # the absolute field. The morphism has to be *over* the |
|---|
| 515 | # relative element. |
|---|
| 516 | return codomain(self.polynomial()(im_gens[0])) |
|---|
| 517 | |
|---|
| 518 | def _latex_(self): |
|---|
| 519 | """ |
|---|
| 520 | Returns the latex representation for this element. |
|---|
| 521 | |
|---|
| 522 | EXAMPLES:: |
|---|
| 523 | |
|---|
| 524 | sage: C,zeta12=CyclotomicField(12).objgen() |
|---|
| 525 | sage: latex(zeta12^4-zeta12) # indirect doctest |
|---|
| 526 | \zeta_{12}^{2} - \zeta_{12} - 1 |
|---|
| 527 | """ |
|---|
| 528 | return self.polynomial()._latex_(name=self.number_field().latex_variable_name()) |
|---|
| 529 | |
|---|
| 530 | def _gap_init_(self): |
|---|
| 531 | """ |
|---|
| 532 | Return gap string representation of self. |
|---|
| 533 | |
|---|
| 534 | EXAMPLES:: |
|---|
| 535 | |
|---|
| 536 | sage: F=CyclotomicField(8) |
|---|
| 537 | sage: F.gen() |
|---|
| 538 | zeta8 |
|---|
| 539 | sage: F._gap_init_() |
|---|
| 540 | 'CyclotomicField(8)' |
|---|
| 541 | sage: f = gap(F) |
|---|
| 542 | sage: f.GeneratorsOfDivisionRing() |
|---|
| 543 | [ E(8) ] |
|---|
| 544 | sage: p=F.gen()^2+2*F.gen()-3 |
|---|
| 545 | sage: p |
|---|
| 546 | zeta8^2 + 2*zeta8 - 3 |
|---|
| 547 | sage: p._gap_init_() # The variable name $sage2 belongs to the gap(F) and is somehow random |
|---|
| 548 | 'GeneratorsOfField($sage2)[1]^2 + 2*GeneratorsOfField($sage2)[1] - 3' |
|---|
| 549 | sage: gap(p._gap_init_()) |
|---|
| 550 | -3+2*E(8)+E(8)^2 |
|---|
| 551 | """ |
|---|
| 552 | s = self._repr_() |
|---|
| 553 | return s.replace(str(self.parent().gen()), 'GeneratorsOfField(%s)[1]'%sage.interfaces.gap.gap(self.parent()).name()) |
|---|
| 554 | |
|---|
| 555 | def _pari_(self, name='y'): |
|---|
| 556 | r""" |
|---|
| 557 | Return PARI representation of self. |
|---|
| 558 | |
|---|
| 559 | The returned element is a PARI ``POLMOD`` in the variable |
|---|
| 560 | ``name``, which is by default 'y' - not the name of the generator |
|---|
| 561 | of the number field. |
|---|
| 562 | |
|---|
| 563 | INPUT: |
|---|
| 564 | |
|---|
| 565 | - ``name`` -- (default: 'y') the PARI variable name used. |
|---|
| 566 | |
|---|
| 567 | EXAMPLES:: |
|---|
| 568 | |
|---|
| 569 | sage: K.<a> = NumberField(x^3 + 2) |
|---|
| 570 | sage: K(1)._pari_() |
|---|
| 571 | Mod(1, y^3 + 2) |
|---|
| 572 | sage: (a + 2)._pari_() |
|---|
| 573 | Mod(y + 2, y^3 + 2) |
|---|
| 574 | sage: L.<b> = K.extension(x^2 + 2) |
|---|
| 575 | sage: (b + a)._pari_() |
|---|
| 576 | Mod(24/101*y^5 - 9/101*y^4 + 160/101*y^3 - 156/101*y^2 + 397/101*y + 364/101, y^6 + 6*y^4 - 4*y^3 + 12*y^2 + 24*y + 12) |
|---|
| 577 | |
|---|
| 578 | :: |
|---|
| 579 | |
|---|
| 580 | sage: k.<j> = QuadraticField(-1) |
|---|
| 581 | sage: j._pari_('j') |
|---|
| 582 | Mod(j, j^2 + 1) |
|---|
| 583 | sage: pari(j) |
|---|
| 584 | Mod(y, y^2 + 1) |
|---|
| 585 | |
|---|
| 586 | By default the variable name is 'y'. This allows 'x' to be used |
|---|
| 587 | as polynomial variable:: |
|---|
| 588 | |
|---|
| 589 | sage: P.<a> = PolynomialRing(QQ) |
|---|
| 590 | sage: K.<b> = NumberField(a^2 + 1) |
|---|
| 591 | sage: R.<x> = PolynomialRing(K) |
|---|
| 592 | sage: pari(b*x) |
|---|
| 593 | Mod(y, y^2 + 1)*x |
|---|
| 594 | |
|---|
| 595 | In PARI many variable names are reserved, for example ``theta`` |
|---|
| 596 | and ``I``:: |
|---|
| 597 | |
|---|
| 598 | sage: R.<theta> = PolynomialRing(QQ) |
|---|
| 599 | sage: K.<theta> = NumberField(theta^2 + 1) |
|---|
| 600 | sage: theta._pari_('theta') |
|---|
| 601 | Traceback (most recent call last): |
|---|
| 602 | ... |
|---|
| 603 | PariError: (5) |
|---|
| 604 | sage: theta._pari_() |
|---|
| 605 | Mod(y, y^2 + 1) |
|---|
| 606 | sage: k.<I> = QuadraticField(-1) |
|---|
| 607 | sage: I._pari_('I') |
|---|
| 608 | Traceback (most recent call last): |
|---|
| 609 | ... |
|---|
| 610 | PariError: (5) |
|---|
| 611 | |
|---|
| 612 | Instead, request the variable be named different for the coercion:: |
|---|
| 613 | |
|---|
| 614 | sage: pari(I) |
|---|
| 615 | Mod(y, y^2 + 1) |
|---|
| 616 | sage: I._pari_('i') |
|---|
| 617 | Mod(i, i^2 + 1) |
|---|
| 618 | sage: I._pari_('II') |
|---|
| 619 | Mod(II, II^2 + 1) |
|---|
| 620 | |
|---|
| 621 | Examples with relative number fields, which always yield an |
|---|
| 622 | *absolute* representation of the element:: |
|---|
| 623 | |
|---|
| 624 | sage: y = QQ['y'].gen() |
|---|
| 625 | sage: k.<j> = NumberField([y^2 - 7, y^3 - 2]) |
|---|
| 626 | sage: pari(j) |
|---|
| 627 | Mod(42/5515*y^5 - 9/11030*y^4 - 196/1103*y^3 + 273/5515*y^2 + 10281/5515*y + 4459/11030, y^6 - 21*y^4 + 4*y^3 + 147*y^2 + 84*y - 339) |
|---|
| 628 | sage: j^2 |
|---|
| 629 | 7 |
|---|
| 630 | sage: pari(j)^2 |
|---|
| 631 | Mod(7, y^6 - 21*y^4 + 4*y^3 + 147*y^2 + 84*y - 339) |
|---|
| 632 | sage: (j^2)._pari_('x') |
|---|
| 633 | Mod(7, x^6 - 21*x^4 + 4*x^3 + 147*x^2 + 84*x - 339) |
|---|
| 634 | |
|---|
| 635 | A tower of three number fields:: |
|---|
| 636 | |
|---|
| 637 | sage: x = polygen(QQ) |
|---|
| 638 | sage: K.<a> = NumberField(x^2 + 2) |
|---|
| 639 | sage: L.<b> = NumberField(polygen(K)^2 + a) |
|---|
| 640 | sage: M.<c> = NumberField(polygen(L)^3 + b) |
|---|
| 641 | sage: L(b)._pari_() |
|---|
| 642 | Mod(y, y^4 + 2) |
|---|
| 643 | sage: M(b)._pari_('c') |
|---|
| 644 | Mod(-c^3, c^12 + 2) |
|---|
| 645 | sage: c._pari_('c') |
|---|
| 646 | Mod(c, c^12 + 2) |
|---|
| 647 | """ |
|---|
| 648 | try: |
|---|
| 649 | return self.__pari[name] |
|---|
| 650 | except KeyError: |
|---|
| 651 | pass |
|---|
| 652 | except TypeError: |
|---|
| 653 | self.__pari = {} |
|---|
| 654 | f = self.polynomial()._pari_or_constant(name) |
|---|
| 655 | g = self.number_field().pari_polynomial(name) |
|---|
| 656 | h = f.Mod(g) |
|---|
| 657 | self.__pari[name] = h |
|---|
| 658 | return h |
|---|
| 659 | |
|---|
| 660 | def _pari_init_(self, name='y'): |
|---|
| 661 | """ |
|---|
| 662 | Return PARI/GP string representation of self. |
|---|
| 663 | |
|---|
| 664 | The returned string defines a PARI ``POLMOD`` in the variable |
|---|
| 665 | ``name``, which is by default 'y' - not the name of the generator |
|---|
| 666 | of the number field. |
|---|
| 667 | |
|---|
| 668 | INPUT: |
|---|
| 669 | |
|---|
| 670 | - ``name`` -- (default: 'y') the PARI variable name used. |
|---|
| 671 | |
|---|
| 672 | EXAMPLES:: |
|---|
| 673 | |
|---|
| 674 | sage: K.<a> = NumberField(x^5 - x - 1) |
|---|
| 675 | sage: ((1 + 1/3*a)^4)._pari_init_() |
|---|
| 676 | 'Mod(1/81*y^4 + 4/27*y^3 + 2/3*y^2 + 4/3*y + 1, y^5 - y - 1)' |
|---|
| 677 | sage: ((1 + 1/3*a)^4)._pari_init_('a') |
|---|
| 678 | 'Mod(1/81*a^4 + 4/27*a^3 + 2/3*a^2 + 4/3*a + 1, a^5 - a - 1)' |
|---|
| 679 | |
|---|
| 680 | Note that _pari_init_ can fail because of reserved words in |
|---|
| 681 | PARI, and since it actually works by obtaining the PARI |
|---|
| 682 | representation of something:: |
|---|
| 683 | |
|---|
| 684 | sage: K.<theta> = NumberField(x^5 - x - 1) |
|---|
| 685 | sage: b = (1/2 - 2/3*theta)^3; b |
|---|
| 686 | -8/27*theta^3 + 2/3*theta^2 - 1/2*theta + 1/8 |
|---|
| 687 | sage: b._pari_init_('theta') |
|---|
| 688 | Traceback (most recent call last): |
|---|
| 689 | ... |
|---|
| 690 | PariError: (5) |
|---|
| 691 | |
|---|
| 692 | Fortunately pari_init returns everything in terms of y by |
|---|
| 693 | default:: |
|---|
| 694 | |
|---|
| 695 | sage: pari(b) |
|---|
| 696 | Mod(-8/27*y^3 + 2/3*y^2 - 1/2*y + 1/8, y^5 - y - 1) |
|---|
| 697 | """ |
|---|
| 698 | return repr(self._pari_(name=name)) |
|---|
| 699 | |
|---|
| 700 | def __getitem__(self, n): |
|---|
| 701 | """ |
|---|
| 702 | Return the n-th coefficient of this number field element, written |
|---|
| 703 | as a polynomial in the generator. |
|---|
| 704 | |
|---|
| 705 | Note that `n` must be between 0 and `d-1`, where |
|---|
| 706 | `d` is the degree of the number field. |
|---|
| 707 | |
|---|
| 708 | EXAMPLES:: |
|---|
| 709 | |
|---|
| 710 | sage: m.<b> = NumberField(x^4 - 1789) |
|---|
| 711 | sage: c = (2/3-4/5*b)^3; c |
|---|
| 712 | -64/125*b^3 + 32/25*b^2 - 16/15*b + 8/27 |
|---|
| 713 | sage: c[0] |
|---|
| 714 | 8/27 |
|---|
| 715 | sage: c[2] |
|---|
| 716 | 32/25 |
|---|
| 717 | sage: c[3] |
|---|
| 718 | -64/125 |
|---|
| 719 | |
|---|
| 720 | We illustrate bounds checking:: |
|---|
| 721 | |
|---|
| 722 | sage: c[-1] |
|---|
| 723 | Traceback (most recent call last): |
|---|
| 724 | ... |
|---|
| 725 | IndexError: index must be between 0 and degree minus 1. |
|---|
| 726 | sage: c[4] |
|---|
| 727 | Traceback (most recent call last): |
|---|
| 728 | ... |
|---|
| 729 | IndexError: index must be between 0 and degree minus 1. |
|---|
| 730 | |
|---|
| 731 | The list method implicitly calls ``__getitem__``:: |
|---|
| 732 | |
|---|
| 733 | sage: list(c) |
|---|
| 734 | [8/27, -16/15, 32/25, -64/125] |
|---|
| 735 | sage: m(list(c)) == c |
|---|
| 736 | True |
|---|
| 737 | """ |
|---|
| 738 | if n < 0 or n >= self.number_field().degree(): # make this faster. |
|---|
| 739 | raise IndexError, "index must be between 0 and degree minus 1." |
|---|
| 740 | return self.polynomial()[n] |
|---|
| 741 | |
|---|
| 742 | def __richcmp__(left, right, int op): |
|---|
| 743 | r""" |
|---|
| 744 | EXAMPLE:: |
|---|
| 745 | |
|---|
| 746 | sage: K.<a> = NumberField(x^3 - 3*x + 8) |
|---|
| 747 | sage: a + 1 > a # indirect doctest |
|---|
| 748 | True |
|---|
| 749 | sage: a + 1 < a # indirect doctest |
|---|
| 750 | False |
|---|
| 751 | """ |
|---|
| 752 | return (<Element>left)._richcmp(right, op) |
|---|
| 753 | |
|---|
| 754 | cdef int _cmp_c_impl(left, sage.structure.element.Element right) except -2: |
|---|
| 755 | cdef NumberFieldElement _right = right |
|---|
| 756 | return not (ZZX_equal(left.__numerator, _right.__numerator) and ZZ_equal(left.__denominator, _right.__denominator)) |
|---|
| 757 | |
|---|
| 758 | def _random_element(self, num_bound=None, den_bound=None, distribution=None): |
|---|
| 759 | """ |
|---|
| 760 | Return a new random element with the same parent as self. |
|---|
| 761 | |
|---|
| 762 | INPUT: |
|---|
| 763 | |
|---|
| 764 | - ``num_bound`` - Bound for the numerator of coefficients of result |
|---|
| 765 | |
|---|
| 766 | - ``den_bound`` - Bound for the denominator of coefficients of result |
|---|
| 767 | |
|---|
| 768 | - ``distribution`` - Distribution to use for coefficients of result |
|---|
| 769 | |
|---|
| 770 | EXAMPLES:: |
|---|
| 771 | |
|---|
| 772 | sage: K.<a> = NumberField(x^3-2) |
|---|
| 773 | sage: a._random_element() |
|---|
| 774 | -1/2*a^2 - 4 |
|---|
| 775 | sage: K.<a> = NumberField(x^2-5) |
|---|
| 776 | sage: a._random_element() |
|---|
| 777 | -2*a - 1 |
|---|
| 778 | """ |
|---|
| 779 | cdef NumberFieldElement elt = self._new() |
|---|
| 780 | elt._randomize(num_bound, den_bound, distribution) |
|---|
| 781 | return elt |
|---|
| 782 | |
|---|
| 783 | cdef void _randomize(self, num_bound, den_bound, distribution): |
|---|
| 784 | cdef int i |
|---|
| 785 | cdef Integer denom_temp = PY_NEW(Integer) |
|---|
| 786 | cdef Integer tmp_integer = PY_NEW(Integer) |
|---|
| 787 | cdef ZZ_c ntl_temp |
|---|
| 788 | cdef list coeff_list |
|---|
| 789 | cdef Rational tmp_rational |
|---|
| 790 | |
|---|
| 791 | # It seems like a simpler approach would be to simply generate |
|---|
| 792 | # random integers for each coefficient of self.__numerator |
|---|
| 793 | # and an integer for self.__denominator. However, this would |
|---|
| 794 | # generate things with a fairly fixed shape: in particular, |
|---|
| 795 | # we'd be very unlikely to get elements like 1/3*a^3 + 1/7, |
|---|
| 796 | # or anything where the denominators are actually unrelated |
|---|
| 797 | # to one another. The extra code below is to make exactly |
|---|
| 798 | # these kinds of results possible. |
|---|
| 799 | |
|---|
| 800 | if den_bound == 1: |
|---|
| 801 | # in this case, we can skip all the business with LCMs, |
|---|
| 802 | # storing a list of rationals, etc. this gives a factor of |
|---|
| 803 | # two or so speedup ... |
|---|
| 804 | |
|---|
| 805 | # set the denominator |
|---|
| 806 | mpz_set_si(denom_temp.value, 1) |
|---|
| 807 | denom_temp._to_ZZ(&self.__denominator) |
|---|
| 808 | for i from 0 <= i < ZZX_deg(self.__fld_numerator.x): |
|---|
| 809 | tmp_integer = <Integer>(ZZ.random_element(x=num_bound, |
|---|
| 810 | distribution=distribution)) |
|---|
| 811 | tmp_integer._to_ZZ(&ntl_temp) |
|---|
| 812 | ZZX_SetCoeff(self.__numerator, i, ntl_temp) |
|---|
| 813 | |
|---|
| 814 | else: |
|---|
| 815 | coeff_list = [] |
|---|
| 816 | mpz_set_si(denom_temp.value, 1) |
|---|
| 817 | tmp_integer = PY_NEW(Integer) |
|---|
| 818 | |
|---|
| 819 | for i from 0 <= i < ZZX_deg(self.__fld_numerator.x): |
|---|
| 820 | tmp_rational = <Rational>(QQ.random_element(num_bound=num_bound, |
|---|
| 821 | den_bound=den_bound, |
|---|
| 822 | distribution=distribution)) |
|---|
| 823 | coeff_list.append(tmp_rational) |
|---|
| 824 | mpz_lcm(denom_temp.value, denom_temp.value, |
|---|
| 825 | mpq_denref(tmp_rational.value)) |
|---|
| 826 | |
|---|
| 827 | # now denom_temp has the denominator, and we just need to |
|---|
| 828 | # scale the numerators and set everything appropriately |
|---|
| 829 | |
|---|
| 830 | # first, the denominator (easy) |
|---|
| 831 | denom_temp._to_ZZ(&self.__denominator) |
|---|
| 832 | |
|---|
| 833 | # now the coefficients themselves. |
|---|
| 834 | for i from 0 <= i < ZZX_deg(self.__fld_numerator.x): |
|---|
| 835 | # calculate the new numerator. if our old entry is |
|---|
| 836 | # p/q, and the lcm is k, it's just pk/q, which we |
|---|
| 837 | # also know is integral -- so we can use mpz_divexact |
|---|
| 838 | # below |
|---|
| 839 | tmp_rational = <Rational>(coeff_list[i]) |
|---|
| 840 | mpz_mul(tmp_integer.value, mpq_numref(tmp_rational.value), |
|---|
| 841 | denom_temp.value) |
|---|
| 842 | mpz_divexact(tmp_integer.value, tmp_integer.value, |
|---|
| 843 | mpq_denref(tmp_rational.value)) |
|---|
| 844 | |
|---|
| 845 | # now set the coefficient of self |
|---|
| 846 | tmp_integer._to_ZZ(&ntl_temp) |
|---|
| 847 | ZZX_SetCoeff(self.__numerator, i, ntl_temp) |
|---|
| 848 | |
|---|
| 849 | def __abs__(self): |
|---|
| 850 | r""" |
|---|
| 851 | Return the numerical absolute value of this number field element |
|---|
| 852 | with respect to the first archimedean embedding, to double |
|---|
| 853 | precision. |
|---|
| 854 | |
|---|
| 855 | This is the ``abs( )`` Python function. If you want a |
|---|
| 856 | different embedding or precision, use |
|---|
| 857 | ``self.abs(...)``. |
|---|
| 858 | |
|---|
| 859 | EXAMPLES:: |
|---|
| 860 | |
|---|
| 861 | sage: k.<a> = NumberField(x^3 - 2) |
|---|
| 862 | sage: abs(a) |
|---|
| 863 | 1.25992104989487 |
|---|
| 864 | sage: abs(a)^3 |
|---|
| 865 | 2.00000000000000 |
|---|
| 866 | sage: a.abs(prec=128) |
|---|
| 867 | 1.2599210498948731647672106072782283506 |
|---|
| 868 | """ |
|---|
| 869 | return self.abs(prec=53, i=0) |
|---|
| 870 | |
|---|
| 871 | def abs(self, prec=53, i=0): |
|---|
| 872 | r""" |
|---|
| 873 | Return the absolute value of this element with respect to the |
|---|
| 874 | `i`-th complex embedding of parent, to the given precision. |
|---|
| 875 | |
|---|
| 876 | If prec is 53 (the default), then the complex double field is |
|---|
| 877 | used; otherwise the arbitrary precision (but slow) complex |
|---|
| 878 | field is used. |
|---|
| 879 | |
|---|
| 880 | INPUT: |
|---|
| 881 | |
|---|
| 882 | |
|---|
| 883 | - ``prec`` - (default: 53) integer bits of precision |
|---|
| 884 | |
|---|
| 885 | - ``i`` - (default: ) integer, which embedding to |
|---|
| 886 | use |
|---|
| 887 | |
|---|
| 888 | |
|---|
| 889 | EXAMPLES:: |
|---|
| 890 | |
|---|
| 891 | sage: z = CyclotomicField(7).gen() |
|---|
| 892 | sage: abs(z) |
|---|
| 893 | 1.00000000000000 |
|---|
| 894 | sage: abs(z^2 + 17*z - 3) |
|---|
| 895 | 16.0604426799931 |
|---|
| 896 | sage: K.<a> = NumberField(x^3+17) |
|---|
| 897 | sage: abs(a) |
|---|
| 898 | 2.57128159065824 |
|---|
| 899 | sage: a.abs(prec=100) |
|---|
| 900 | 2.5712815906582353554531872087 |
|---|
| 901 | sage: a.abs(prec=100,i=1) |
|---|
| 902 | 2.5712815906582353554531872087 |
|---|
| 903 | sage: a.abs(100, 2) |
|---|
| 904 | 2.5712815906582353554531872087 |
|---|
| 905 | |
|---|
| 906 | Here's one where the absolute value depends on the embedding. |
|---|
| 907 | |
|---|
| 908 | :: |
|---|
| 909 | |
|---|
| 910 | sage: K.<b> = NumberField(x^2-2) |
|---|
| 911 | sage: a = 1 + b |
|---|
| 912 | sage: a.abs(i=0) |
|---|
| 913 | 0.414213562373095 |
|---|
| 914 | sage: a.abs(i=1) |
|---|
| 915 | 2.41421356237309 |
|---|
| 916 | """ |
|---|
| 917 | P = self.number_field().complex_embeddings(prec)[i] |
|---|
| 918 | return abs(P(self)) |
|---|
| 919 | |
|---|
| 920 | def abs_non_arch(self, P, prec=None): |
|---|
| 921 | r""" |
|---|
| 922 | Return the non-archimedean absolute value of this element with |
|---|
| 923 | respect to the prime `P`, to the given precision. |
|---|
| 924 | |
|---|
| 925 | INPUT: |
|---|
| 926 | |
|---|
| 927 | - ``P`` - a prime ideal of the parent of self |
|---|
| 928 | |
|---|
| 929 | - ``prec`` (int) -- desired floating point precision (default: |
|---|
| 930 | default RealField precision). |
|---|
| 931 | |
|---|
| 932 | OUTPUT: |
|---|
| 933 | |
|---|
| 934 | (real) the non-archimedean absolute value of this element with |
|---|
| 935 | respect to the prime `P`, to the given precision. This is the |
|---|
| 936 | normalised absolute value, so that the underlying prime number |
|---|
| 937 | `p` has absolute value `1/p`. |
|---|
| 938 | |
|---|
| 939 | |
|---|
| 940 | EXAMPLES:: |
|---|
| 941 | |
|---|
| 942 | sage: K.<a> = NumberField(x^2+5) |
|---|
| 943 | sage: [1/K(2).abs_non_arch(P) for P in K.primes_above(2)] |
|---|
| 944 | [2.00000000000000] |
|---|
| 945 | sage: [1/K(3).abs_non_arch(P) for P in K.primes_above(3)] |
|---|
| 946 | [3.00000000000000, 3.00000000000000] |
|---|
| 947 | sage: [1/K(5).abs_non_arch(P) for P in K.primes_above(5)] |
|---|
| 948 | [5.00000000000000] |
|---|
| 949 | |
|---|
| 950 | A relative example:: |
|---|
| 951 | |
|---|
| 952 | sage: L.<b> = K.extension(x^2-5) |
|---|
| 953 | sage: [b.abs_non_arch(P) for P in L.primes_above(b)] |
|---|
| 954 | [0.447213595499958, 0.447213595499958] |
|---|
| 955 | """ |
|---|
| 956 | from sage.rings.real_mpfr import RealField |
|---|
| 957 | if prec is None: |
|---|
| 958 | R = RealField() |
|---|
| 959 | else: |
|---|
| 960 | R = RealField(prec) |
|---|
| 961 | |
|---|
| 962 | if self.is_zero(): |
|---|
| 963 | return R.zero_element() |
|---|
| 964 | val = self.valuation(P) |
|---|
| 965 | nP = P.residue_class_degree()*P.absolute_ramification_index() |
|---|
| 966 | return R(P.absolute_norm()) ** (-R(val) / R(nP)) |
|---|
| 967 | |
|---|
| 968 | def coordinates_in_terms_of_powers(self): |
|---|
| 969 | r""" |
|---|
| 970 | Let `\alpha` be self. Return a callable object (of type |
|---|
| 971 | :class:`~CoordinateFunction`) that takes any element of the |
|---|
| 972 | parent of self in `\QQ(\alpha)` and writes it in terms of the |
|---|
| 973 | powers of `\alpha`: `1, \alpha, \alpha^2, ...`. |
|---|
| 974 | |
|---|
| 975 | (NOT CACHED). |
|---|
| 976 | |
|---|
| 977 | EXAMPLES: |
|---|
| 978 | |
|---|
| 979 | This function allows us to write elements of a number |
|---|
| 980 | field in terms of a different generator without having to construct |
|---|
| 981 | a whole separate number field. |
|---|
| 982 | |
|---|
| 983 | :: |
|---|
| 984 | |
|---|
| 985 | sage: y = polygen(QQ,'y'); K.<beta> = NumberField(y^3 - 2); K |
|---|
| 986 | Number Field in beta with defining polynomial y^3 - 2 |
|---|
| 987 | sage: alpha = beta^2 + beta + 1 |
|---|
| 988 | sage: c = alpha.coordinates_in_terms_of_powers(); c |
|---|
| 989 | Coordinate function that writes elements in terms of the powers of beta^2 + beta + 1 |
|---|
| 990 | sage: c(beta) |
|---|
| 991 | [-2, -3, 1] |
|---|
| 992 | sage: c(alpha) |
|---|
| 993 | [0, 1, 0] |
|---|
| 994 | sage: c((1+beta)^5) |
|---|
| 995 | [3, 3, 3] |
|---|
| 996 | sage: c((1+beta)^10) |
|---|
| 997 | [54, 162, 189] |
|---|
| 998 | |
|---|
| 999 | This function works even if self only generates a subfield of this |
|---|
| 1000 | number field. |
|---|
| 1001 | |
|---|
| 1002 | :: |
|---|
| 1003 | |
|---|
| 1004 | sage: k.<a> = NumberField(x^6 - 5) |
|---|
| 1005 | sage: alpha = a^3 |
|---|
| 1006 | sage: c = alpha.coordinates_in_terms_of_powers() |
|---|
| 1007 | sage: c((2/3)*a^3 - 5/3) |
|---|
| 1008 | [-5/3, 2/3] |
|---|
| 1009 | sage: c |
|---|
| 1010 | Coordinate function that writes elements in terms of the powers of a^3 |
|---|
| 1011 | sage: c(a) |
|---|
| 1012 | Traceback (most recent call last): |
|---|
| 1013 | ... |
|---|
| 1014 | ArithmeticError: vector is not in free module |
|---|
| 1015 | """ |
|---|
| 1016 | K = self.number_field() |
|---|
| 1017 | V, from_V, to_V = K.absolute_vector_space() |
|---|
| 1018 | h = K(1) |
|---|
| 1019 | B = [to_V(h)] |
|---|
| 1020 | f = self.absolute_minpoly() |
|---|
| 1021 | for i in range(f.degree()-1): |
|---|
| 1022 | h *= self |
|---|
| 1023 | B.append(to_V(h)) |
|---|
| 1024 | W = V.span_of_basis(B) |
|---|
| 1025 | return CoordinateFunction(self, W, to_V) |
|---|
| 1026 | |
|---|
| 1027 | def complex_embeddings(self, prec=53): |
|---|
| 1028 | """ |
|---|
| 1029 | Return the images of this element in the floating point complex |
|---|
| 1030 | numbers, to the given bits of precision. |
|---|
| 1031 | |
|---|
| 1032 | INPUT: |
|---|
| 1033 | |
|---|
| 1034 | |
|---|
| 1035 | - ``prec`` - integer (default: 53) bits of precision |
|---|
| 1036 | |
|---|
| 1037 | |
|---|
| 1038 | EXAMPLES:: |
|---|
| 1039 | |
|---|
| 1040 | sage: k.<a> = NumberField(x^3 - 2) |
|---|
| 1041 | sage: a.complex_embeddings() |
|---|
| 1042 | [-0.629960524947437 - 1.09112363597172*I, -0.629960524947437 + 1.09112363597172*I, 1.25992104989487] |
|---|
| 1043 | sage: a.complex_embeddings(10) |
|---|
| 1044 | [-0.63 - 1.1*I, -0.63 + 1.1*I, 1.3] |
|---|
| 1045 | sage: a.complex_embeddings(100) |
|---|
| 1046 | [-0.62996052494743658238360530364 - 1.0911236359717214035600726142*I, -0.62996052494743658238360530364 + 1.0911236359717214035600726142*I, 1.2599210498948731647672106073] |
|---|
| 1047 | """ |
|---|
| 1048 | phi = self.number_field().complex_embeddings(prec) |
|---|
| 1049 | return [f(self) for f in phi] |
|---|
| 1050 | |
|---|
| 1051 | def complex_embedding(self, prec=53, i=0): |
|---|
| 1052 | """ |
|---|
| 1053 | Return the i-th embedding of self in the complex numbers, to the |
|---|
| 1054 | given precision. |
|---|
| 1055 | |
|---|
| 1056 | EXAMPLES:: |
|---|
| 1057 | |
|---|
| 1058 | sage: k.<a> = NumberField(x^3 - 2) |
|---|
| 1059 | sage: a.complex_embedding() |
|---|
| 1060 | -0.629960524947437 - 1.09112363597172*I |
|---|
| 1061 | sage: a.complex_embedding(10) |
|---|
| 1062 | -0.63 - 1.1*I |
|---|
| 1063 | sage: a.complex_embedding(100) |
|---|
| 1064 | -0.62996052494743658238360530364 - 1.0911236359717214035600726142*I |
|---|
| 1065 | sage: a.complex_embedding(20, 1) |
|---|
| 1066 | -0.62996 + 1.0911*I |
|---|
| 1067 | sage: a.complex_embedding(20, 2) |
|---|
| 1068 | 1.2599 |
|---|
| 1069 | """ |
|---|
| 1070 | return self.number_field().complex_embeddings(prec)[i](self) |
|---|
| 1071 | |
|---|
| 1072 | def is_unit(self): |
|---|
| 1073 | """ |
|---|
| 1074 | Return ``True`` if ``self`` is a unit in the ring where it is defined. |
|---|
| 1075 | |
|---|
| 1076 | EXAMPLES:: |
|---|
| 1077 | |
|---|
| 1078 | sage: K.<a> = NumberField(x^2 - x - 1) |
|---|
| 1079 | sage: OK = K.ring_of_integers() |
|---|
| 1080 | sage: OK(a).is_unit() |
|---|
| 1081 | True |
|---|
| 1082 | sage: OK(13).is_unit() |
|---|
| 1083 | False |
|---|
| 1084 | sage: K(13).is_unit() |
|---|
| 1085 | True |
|---|
| 1086 | |
|---|
| 1087 | It also works for relative fields and orders:: |
|---|
| 1088 | |
|---|
| 1089 | sage: K.<a,b> = NumberField([x^2 - 3, x^4 + x^3 + x^2 + x + 1]) |
|---|
| 1090 | sage: OK = K.ring_of_integers() |
|---|
| 1091 | sage: OK(b).is_unit() |
|---|
| 1092 | True |
|---|
| 1093 | sage: OK(a).is_unit() |
|---|
| 1094 | False |
|---|
| 1095 | sage: a.is_unit() |
|---|
| 1096 | True |
|---|
| 1097 | """ |
|---|
| 1098 | if self.parent().is_field(): |
|---|
| 1099 | return bool(self) |
|---|
| 1100 | return self.norm().is_unit() |
|---|
| 1101 | |
|---|
| 1102 | def is_norm(self, L, element=False, proof=True): |
|---|
| 1103 | r""" |
|---|
| 1104 | Determine whether self is the relative norm of an element |
|---|
| 1105 | of L/K, where K is self.parent(). |
|---|
| 1106 | |
|---|
| 1107 | INPUT: |
|---|
| 1108 | |
|---|
| 1109 | - L -- a number field containing K=self.parent() |
|---|
| 1110 | - element -- True or False, whether to also output an element |
|---|
| 1111 | of which self is a norm |
|---|
| 1112 | - proof -- If True, then the output is correct unconditionally. |
|---|
| 1113 | If False, then the output is correct under GRH. |
|---|
| 1114 | |
|---|
| 1115 | OUTPUT: |
|---|
| 1116 | |
|---|
| 1117 | If element is False, then the output is a boolean B, which is |
|---|
| 1118 | True if and only if self is the relative norm of an element of L |
|---|
| 1119 | to K. |
|---|
| 1120 | If element is False, then the output is a pair (B, x), where |
|---|
| 1121 | B is as above. If B is True, then x is an element of L such that |
|---|
| 1122 | self == x.norm(K). Otherwise, x is None. |
|---|
| 1123 | |
|---|
| 1124 | ALGORITHM: |
|---|
| 1125 | |
|---|
| 1126 | Uses PARI's rnfisnorm. See self._rnfisnorm(). |
|---|
| 1127 | |
|---|
| 1128 | EXAMPLES:: |
|---|
| 1129 | |
|---|
| 1130 | sage: K.<beta> = NumberField(x^3+5) |
|---|
| 1131 | sage: Q.<X> = K[] |
|---|
| 1132 | sage: L = K.extension(X^2+X+beta, 'gamma') |
|---|
| 1133 | sage: (beta/2).is_norm(L) |
|---|
| 1134 | False |
|---|
| 1135 | sage: beta.is_norm(L) |
|---|
| 1136 | True |
|---|
| 1137 | |
|---|
| 1138 | With a relative base field:: |
|---|
| 1139 | |
|---|
| 1140 | sage: K.<a, b> = NumberField([x^2 - 2, x^2 - 3]) |
|---|
| 1141 | sage: L.<c> = K.extension(x^2 - 5) |
|---|
| 1142 | sage: (2*a*b).is_norm(L) |
|---|
| 1143 | True |
|---|
| 1144 | sage: _, v = (2*b*a).is_norm(L, element=True) |
|---|
| 1145 | sage: v.norm(K) == 2*a*b |
|---|
| 1146 | True |
|---|
| 1147 | |
|---|
| 1148 | Non-Galois number fields:: |
|---|
| 1149 | |
|---|
| 1150 | sage: K.<a> = NumberField(x^2 + x + 1) |
|---|
| 1151 | sage: Q.<X> = K[] |
|---|
| 1152 | sage: L.<b> = NumberField(X^4 + a + 2) |
|---|
| 1153 | sage: (a/4).is_norm(L) |
|---|
| 1154 | True |
|---|
| 1155 | sage: (a/2).is_norm(L) |
|---|
| 1156 | Traceback (most recent call last): |
|---|
| 1157 | ... |
|---|
| 1158 | NotImplementedError: is_norm is not implemented unconditionally for norms from non-Galois number fields |
|---|
| 1159 | sage: (a/2).is_norm(L, proof=False) |
|---|
| 1160 | False |
|---|
| 1161 | |
|---|
| 1162 | sage: K.<a> = NumberField(x^3 + x + 1) |
|---|
| 1163 | sage: Q.<X> = K[] |
|---|
| 1164 | sage: L.<b> = NumberField(X^4 + a) |
|---|
| 1165 | sage: t = (-a).is_norm(L, element=True); t |
|---|
| 1166 | (True, -b^3 + 1) |
|---|
| 1167 | sage: t[1].norm(K) |
|---|
| 1168 | -a |
|---|
| 1169 | |
|---|
| 1170 | AUTHORS: |
|---|
| 1171 | |
|---|
| 1172 | - Craig Citro (2008-04-05) |
|---|
| 1173 | |
|---|
| 1174 | - Marco Streng (2010-12-03) |
|---|
| 1175 | """ |
|---|
| 1176 | if not element: |
|---|
| 1177 | return self.is_norm(L, element=True, proof=proof)[0] |
|---|
| 1178 | |
|---|
| 1179 | K = self.parent() |
|---|
| 1180 | from sage.rings.number_field.all import is_AbsoluteNumberField, \ |
|---|
| 1181 | is_NumberField |
|---|
| 1182 | if not is_NumberField(L): |
|---|
| 1183 | raise ValueError, "L (=%s) must be a NumberField in is_norm" % L |
|---|
| 1184 | |
|---|
| 1185 | if is_AbsoluteNumberField(L): |
|---|
| 1186 | Lrel = L.relativize(K.hom(L), ('a', 'b')) |
|---|
| 1187 | b, x = self.is_norm(Lrel, element=True, proof=proof) |
|---|
| 1188 | h = Lrel.structure()[0] |
|---|
| 1189 | return b, h(x) |
|---|
| 1190 | |
|---|
| 1191 | if L.relative_degree() == 1 or self.is_zero(): |
|---|
| 1192 | return True, L(self) |
|---|
| 1193 | |
|---|
| 1194 | a, b = self._rnfisnorm(L, proof=proof) |
|---|
| 1195 | if b == 1: |
|---|
| 1196 | assert a.norm(K) == self |
|---|
| 1197 | return True, a |
|---|
| 1198 | |
|---|
| 1199 | if L.is_galois_relative(): |
|---|
| 1200 | return False, None |
|---|
| 1201 | |
|---|
| 1202 | # The following gives the galois closure of K/QQ, but the galois |
|---|
| 1203 | # closure of K/self.parent() would suffice. |
|---|
| 1204 | M = L.galois_closure('a') |
|---|
| 1205 | from sage.functions.log import log |
|---|
| 1206 | from sage.functions.other import floor |
|---|
| 1207 | extra_primes = floor(12*log(abs(M.discriminant()))**2) |
|---|
| 1208 | a, b = self._rnfisnorm(L, proof=proof, extra_primes=extra_primes) |
|---|
| 1209 | if b == 1: |
|---|
| 1210 | assert a.norm(K) == self |
|---|
| 1211 | return True, a |
|---|
| 1212 | |
|---|
| 1213 | if proof: |
|---|
| 1214 | raise NotImplementedError, "is_norm is not implemented unconditionally for norms from non-Galois number fields" |
|---|
| 1215 | return False, None |
|---|
| 1216 | |
|---|
| 1217 | def _rnfisnorm(self, L, proof=True, extra_primes=0): |
|---|
| 1218 | r""" |
|---|
| 1219 | Gives the output of the PARI function rnfisnorm. |
|---|
| 1220 | |
|---|
| 1221 | This tries to decide whether the number field element self is |
|---|
| 1222 | the norm of some x in the extension L/K (with K = self.parent()). |
|---|
| 1223 | |
|---|
| 1224 | The output is a pair (x, q), where self = Norm(x)*q. The |
|---|
| 1225 | algorithm looks for a solution x that is an S-integer, with S |
|---|
| 1226 | a list of places of L containing at least the ramified primes, |
|---|
| 1227 | the generators of the class group of L, as well as those primes |
|---|
| 1228 | dividing self. |
|---|
| 1229 | |
|---|
| 1230 | If L/K is Galois, then this is enough; otherwise, |
|---|
| 1231 | extra_primes is used to add more primes to S: all the places |
|---|
| 1232 | above the primes p <= extra_primes (resp. p|extra_primes) if |
|---|
| 1233 | extra_primes > 0 (resp. extra_primes < 0). |
|---|
| 1234 | |
|---|
| 1235 | The answer is guaranteed (i.e., self is a norm iff q = 1) if the |
|---|
| 1236 | field is Galois, or, under GRH, if S contains all primes less |
|---|
| 1237 | than 12log^2|\disc(M)|, where M is the normal closure of L/K. |
|---|
| 1238 | |
|---|
| 1239 | INPUT: |
|---|
| 1240 | |
|---|
| 1241 | - L -- a relative number field with base field self.parent() |
|---|
| 1242 | - proof -- whether to certify outputs of PARI init functions. |
|---|
| 1243 | If false, truth of the output depends on GRH. |
|---|
| 1244 | - extra_primes -- an integer as explained above. |
|---|
| 1245 | |
|---|
| 1246 | OUTPUT: |
|---|
| 1247 | |
|---|
| 1248 | A pair (x, q) with x in L and q in K as explained above |
|---|
| 1249 | such that self == x.norm(K)*q. |
|---|
| 1250 | |
|---|
| 1251 | ALGORITHM: |
|---|
| 1252 | |
|---|
| 1253 | Uses PARI's rnfisnorm. |
|---|
| 1254 | |
|---|
| 1255 | EXAMPLES:: |
|---|
| 1256 | |
|---|
| 1257 | sage: K.<a> = NumberField(x^3 + x^2 - 2*x - 1, 'a') |
|---|
| 1258 | sage: P.<X> = K[] |
|---|
| 1259 | sage: L = NumberField(X^2 + a^2 + 2*a + 1, 'b') |
|---|
| 1260 | sage: K(17)._rnfisnorm(L) |
|---|
| 1261 | ((a^2 - 2)*b - 4, 1) |
|---|
| 1262 | |
|---|
| 1263 | sage: K.<a> = NumberField(x^3 + x + 1) |
|---|
| 1264 | sage: Q.<X> = K[] |
|---|
| 1265 | sage: L.<b> = NumberField(X^4 + a) |
|---|
| 1266 | sage: t = (-a)._rnfisnorm(L); t |
|---|
| 1267 | (-b^3 + 1, 1) |
|---|
| 1268 | sage: t[0].norm(K) |
|---|
| 1269 | -a |
|---|
| 1270 | sage: t = K(3)._rnfisnorm(L); t |
|---|
| 1271 | ((a^2 + 1)*b^3 + b^2 - a*b + a^2 + 1, -3*a) |
|---|
| 1272 | sage: t[0].norm(K)*t[1] |
|---|
| 1273 | 3 |
|---|
| 1274 | |
|---|
| 1275 | An example where the base field is a relative field:: |
|---|
| 1276 | |
|---|
| 1277 | sage: K.<a, b> = NumberField([x^2 - 2, x^2 - 3]) |
|---|
| 1278 | sage: L.<c> = K.extension(x^3 + 2) |
|---|
| 1279 | sage: s = 2*a + b |
|---|
| 1280 | sage: t = s._rnfisnorm(L) |
|---|
| 1281 | sage: t[1] == 1 # True iff s is a norm |
|---|
| 1282 | False |
|---|
| 1283 | sage: s == t[0].norm(K)*t[1] |
|---|
| 1284 | True |
|---|
| 1285 | |
|---|
| 1286 | AUTHORS: |
|---|
| 1287 | |
|---|
| 1288 | - Craig Citro (2008-04-05) |
|---|
| 1289 | |
|---|
| 1290 | - Marco Streng (2010-12-03) |
|---|
| 1291 | |
|---|
| 1292 | - Francis Clarke (2010-12-26) |
|---|
| 1293 | """ |
|---|
| 1294 | K = self.parent() |
|---|
| 1295 | from sage.rings.number_field.all import is_RelativeNumberField |
|---|
| 1296 | if (not is_RelativeNumberField(L)) or L.base_field() != K: |
|---|
| 1297 | raise ValueError, "L (=%s) must be a relative number field with base field K (=%s) in rnfisnorm" % (L, K) |
|---|
| 1298 | |
|---|
| 1299 | rnf_data = K.pari_rnfnorm_data(L, proof=proof) |
|---|
| 1300 | x, q = self._pari_().rnfisnorm(rnf_data) |
|---|
| 1301 | return L(x), K(q) |
|---|
| 1302 | |
|---|
| 1303 | def _mpfr_(self, R): |
|---|
| 1304 | """ |
|---|
| 1305 | EXAMPLES:: |
|---|
| 1306 | |
|---|
| 1307 | sage: k.<a> = NumberField(x^2 + 1) |
|---|
| 1308 | sage: RR(a^2) |
|---|
| 1309 | -1.00000000000000 |
|---|
| 1310 | sage: RR(a) |
|---|
| 1311 | Traceback (most recent call last): |
|---|
| 1312 | ... |
|---|
| 1313 | TypeError: Unable to coerce a to a rational |
|---|
| 1314 | sage: (a^2)._mpfr_(RR) |
|---|
| 1315 | -1.00000000000000 |
|---|
| 1316 | |
|---|
| 1317 | Verify that :trac:`#13005` has been fixed:: |
|---|
| 1318 | |
|---|
| 1319 | sage: K.<a> = NumberField(x^2-5) |
|---|
| 1320 | sage: RR(K(1)) |
|---|
| 1321 | 1.00000000000000 |
|---|
| 1322 | sage: RR(a) |
|---|
| 1323 | Traceback (most recent call last): |
|---|
| 1324 | ... |
|---|
| 1325 | TypeError: Unable to coerce a to a rational |
|---|
| 1326 | sage: K.<a> = NumberField(x^3+2, embedding=-1.25) |
|---|
| 1327 | sage: RR(a) |
|---|
| 1328 | -1.25992104989487 |
|---|
| 1329 | sage: RealField(prec=100)(a) |
|---|
| 1330 | -1.2599210498948731647672106073 |
|---|
| 1331 | """ |
|---|
| 1332 | if self.parent().coerce_embedding() is None: |
|---|
| 1333 | return R(self.base_ring()(self)) |
|---|
| 1334 | else: |
|---|
| 1335 | return R(R.complex_field()(self)) |
|---|
| 1336 | |
|---|
| 1337 | def __float__(self): |
|---|
| 1338 | """ |
|---|
| 1339 | EXAMPLES:: |
|---|
| 1340 | |
|---|
| 1341 | sage: k.<a> = NumberField(x^2 + 1) |
|---|
| 1342 | sage: float(a^2) |
|---|
| 1343 | -1.0 |
|---|
| 1344 | sage: float(a) |
|---|
| 1345 | Traceback (most recent call last): |
|---|
| 1346 | ... |
|---|
| 1347 | TypeError: Unable to coerce a to a rational |
|---|
| 1348 | sage: (a^2).__float__() |
|---|
| 1349 | -1.0 |
|---|
| 1350 | sage: k.<a> = NumberField(x^2 + 1,embedding=I) |
|---|
| 1351 | sage: float(a) |
|---|
| 1352 | Traceback (most recent call last): |
|---|
| 1353 | ... |
|---|
| 1354 | TypeError: unable to coerce to a real number |
|---|
| 1355 | """ |
|---|
| 1356 | if self.parent().coerce_embedding() is None: |
|---|
| 1357 | return float(self.base_ring()(self)) |
|---|
| 1358 | else: |
|---|
| 1359 | c = complex(self) |
|---|
| 1360 | if c.imag == 0: |
|---|
| 1361 | return c.real |
|---|
| 1362 | raise TypeError('unable to coerce to a real number') |
|---|
| 1363 | |
|---|
| 1364 | def _complex_double_(self, CDF): |
|---|
| 1365 | """ |
|---|
| 1366 | EXAMPLES:: |
|---|
| 1367 | |
|---|
| 1368 | sage: k.<a> = NumberField(x^2 + 1) |
|---|
| 1369 | sage: abs(CDF(a)) |
|---|
| 1370 | 1.0 |
|---|
| 1371 | """ |
|---|
| 1372 | return CDF(CC(self)) |
|---|
| 1373 | |
|---|
| 1374 | def __complex__(self): |
|---|
| 1375 | """ |
|---|
| 1376 | EXAMPLES:: |
|---|
| 1377 | |
|---|
| 1378 | sage: k.<a> = NumberField(x^2 + 1) |
|---|
| 1379 | sage: complex(a) |
|---|
| 1380 | 1j |
|---|
| 1381 | sage: a.__complex__() |
|---|
| 1382 | 1j |
|---|
| 1383 | """ |
|---|
| 1384 | return complex(CC(self)) |
|---|
| 1385 | |
|---|
| 1386 | def factor(self): |
|---|
| 1387 | """ |
|---|
| 1388 | Return factorization of this element into prime elements and a unit. |
|---|
| 1389 | |
|---|
| 1390 | OUTPUT: |
|---|
| 1391 | |
|---|
| 1392 | (Factorization) If all the prime ideals in the support are |
|---|
| 1393 | principal, the output is a Factorization as a product of prime |
|---|
| 1394 | elements raised to appropriate powers, with an appropriate |
|---|
| 1395 | unit factor. |
|---|
| 1396 | |
|---|
| 1397 | Raise ValueError if the factorization of the |
|---|
| 1398 | ideal (self) contains a non-principal prime ideal. |
|---|
| 1399 | |
|---|
| 1400 | EXAMPLES:: |
|---|
| 1401 | |
|---|
| 1402 | sage: K.<i> = NumberField(x^2+1) |
|---|
| 1403 | sage: (6*i + 6).factor() |
|---|
| 1404 | (-i) * (i + 1)^3 * 3 |
|---|
| 1405 | |
|---|
| 1406 | In the following example, the class number is 2. If a factorization |
|---|
| 1407 | in prime elements exists, we will find it:: |
|---|
| 1408 | |
|---|
| 1409 | sage: K.<a> = NumberField(x^2-10) |
|---|
| 1410 | sage: factor(169*a + 531) |
|---|
| 1411 | (-6*a - 19) * (-2*a + 9) * (-3*a - 1) |
|---|
| 1412 | sage: factor(K(3)) |
|---|
| 1413 | Traceback (most recent call last): |
|---|
| 1414 | ... |
|---|
| 1415 | ValueError: Non-principal ideal in factorization |
|---|
| 1416 | |
|---|
| 1417 | Factorization of 0 is not allowed:: |
|---|
| 1418 | |
|---|
| 1419 | sage: K.<i> = QuadraticField(-1) |
|---|
| 1420 | sage: K(0).factor() |
|---|
| 1421 | Traceback (most recent call last): |
|---|
| 1422 | ... |
|---|
| 1423 | ArithmeticError: Factorization of 0 not defined. |
|---|
| 1424 | |
|---|
| 1425 | """ |
|---|
| 1426 | if self.is_zero(): |
|---|
| 1427 | raise ArithmeticError, "Factorization of 0 not defined." |
|---|
| 1428 | |
|---|
| 1429 | K = self.parent() |
|---|
| 1430 | fac = K.ideal(self).factor() |
|---|
| 1431 | # Check whether all prime ideals in `fac` are principal |
|---|
| 1432 | for P,e in fac: |
|---|
| 1433 | if not P.is_principal(): |
|---|
| 1434 | raise ValueError, "Non-principal ideal in factorization" |
|---|
| 1435 | element_fac = [(P.gens_reduced()[0],e) for P,e in fac] |
|---|
| 1436 | # Compute the product of the p^e to figure out the unit |
|---|
| 1437 | from sage.misc.all import prod |
|---|
| 1438 | element_product = prod([p**e for p,e in element_fac], K(1)) |
|---|
| 1439 | from sage.structure.all import Factorization |
|---|
| 1440 | return Factorization(element_fac, unit=self/element_product) |
|---|
| 1441 | |
|---|
| 1442 | def is_totally_positive(self): |
|---|
| 1443 | """ |
|---|
| 1444 | Returns True if self is positive for all real embeddings of its |
|---|
| 1445 | parent number field. We do nothing at complex places, so e.g. any |
|---|
| 1446 | element of a totally complex number field will return True. |
|---|
| 1447 | |
|---|
| 1448 | EXAMPLES:: |
|---|
| 1449 | |
|---|
| 1450 | sage: F.<b> = NumberField(x^3-3*x-1) |
|---|
| 1451 | sage: b.is_totally_positive() |
|---|
| 1452 | False |
|---|
| 1453 | sage: (b^2).is_totally_positive() |
|---|
| 1454 | True |
|---|
| 1455 | |
|---|
| 1456 | TESTS: |
|---|
| 1457 | |
|---|
| 1458 | Check that the output is correct even for numbers that are |
|---|
| 1459 | very close to zero (ticket #9596):: |
|---|
| 1460 | |
|---|
| 1461 | sage: K.<sqrt2> = QuadraticField(2) |
|---|
| 1462 | sage: a = 30122754096401; b = 21300003689580 |
|---|
| 1463 | sage: (a/b)^2 > 2 |
|---|
| 1464 | True |
|---|
| 1465 | sage: (a/b+sqrt2).is_totally_positive() |
|---|
| 1466 | True |
|---|
| 1467 | sage: r = RealField(3020)(2).sqrt()*2^3000 |
|---|
| 1468 | sage: a = floor(r)/2^3000 |
|---|
| 1469 | sage: b = ceil(r)/2^3000 |
|---|
| 1470 | sage: (a+sqrt2).is_totally_positive() |
|---|
| 1471 | False |
|---|
| 1472 | sage: (b+sqrt2).is_totally_positive() |
|---|
| 1473 | True |
|---|
| 1474 | |
|---|
| 1475 | Check that 0 is handled correctly:: |
|---|
| 1476 | |
|---|
| 1477 | sage: K.<a> = NumberField(x^5+4*x+1) |
|---|
| 1478 | sage: K(0).is_totally_positive() |
|---|
| 1479 | False |
|---|
| 1480 | """ |
|---|
| 1481 | for v in self.number_field().embeddings(sage.rings.qqbar.AA): |
|---|
| 1482 | if v(self) <= 0: |
|---|
| 1483 | return False |
|---|
| 1484 | return True |
|---|
| 1485 | |
|---|
| 1486 | def is_square(self, root=False): |
|---|
| 1487 | """ |
|---|
| 1488 | Return True if self is a square in its parent number field and |
|---|
| 1489 | otherwise return False. |
|---|
| 1490 | |
|---|
| 1491 | INPUT: |
|---|
| 1492 | |
|---|
| 1493 | |
|---|
| 1494 | - ``root`` - if True, also return a square root (or |
|---|
| 1495 | None if self is not a perfect square) |
|---|
| 1496 | |
|---|
| 1497 | |
|---|
| 1498 | EXAMPLES:: |
|---|
| 1499 | |
|---|
| 1500 | sage: m.<b> = NumberField(x^4 - 1789) |
|---|
| 1501 | sage: b.is_square() |
|---|
| 1502 | False |
|---|
| 1503 | sage: c = (2/3*b + 5)^2; c |
|---|
| 1504 | 4/9*b^2 + 20/3*b + 25 |
|---|
| 1505 | sage: c.is_square() |
|---|
| 1506 | True |
|---|
| 1507 | sage: c.is_square(True) |
|---|
| 1508 | (True, 2/3*b + 5) |
|---|
| 1509 | |
|---|
| 1510 | We also test the functional notation. |
|---|
| 1511 | |
|---|
| 1512 | :: |
|---|
| 1513 | |
|---|
| 1514 | sage: is_square(c, True) |
|---|
| 1515 | (True, 2/3*b + 5) |
|---|
| 1516 | sage: is_square(c) |
|---|
| 1517 | True |
|---|
| 1518 | sage: is_square(c+1) |
|---|
| 1519 | False |
|---|
| 1520 | """ |
|---|
| 1521 | v = self.sqrt(all=True) |
|---|
| 1522 | t = len(v) > 0 |
|---|
| 1523 | if root: |
|---|
| 1524 | if t: |
|---|
| 1525 | return t, v[0] |
|---|
| 1526 | else: |
|---|
| 1527 | return False, None |
|---|
| 1528 | else: |
|---|
| 1529 | return t |
|---|
| 1530 | |
|---|
| 1531 | def sqrt(self, all=False): |
|---|
| 1532 | """ |
|---|
| 1533 | Returns the square root of this number in the given number field. |
|---|
| 1534 | |
|---|
| 1535 | EXAMPLES:: |
|---|
| 1536 | |
|---|
| 1537 | sage: K.<a> = NumberField(x^2 - 3) |
|---|
| 1538 | sage: K(3).sqrt() |
|---|
| 1539 | a |
|---|
| 1540 | sage: K(3).sqrt(all=True) |
|---|
| 1541 | [a, -a] |
|---|
| 1542 | sage: K(a^10).sqrt() |
|---|
| 1543 | 9*a |
|---|
| 1544 | sage: K(49).sqrt() |
|---|
| 1545 | 7 |
|---|
| 1546 | sage: K(1+a).sqrt() |
|---|
| 1547 | Traceback (most recent call last): |
|---|
| 1548 | ... |
|---|
| 1549 | ValueError: a + 1 not a square in Number Field in a with defining polynomial x^2 - 3 |
|---|
| 1550 | sage: K(0).sqrt() |
|---|
| 1551 | 0 |
|---|
| 1552 | sage: K((7+a)^2).sqrt(all=True) |
|---|
| 1553 | [a + 7, -a - 7] |
|---|
| 1554 | |
|---|
| 1555 | :: |
|---|
| 1556 | |
|---|
| 1557 | sage: K.<a> = CyclotomicField(7) |
|---|
| 1558 | sage: a.sqrt() |
|---|
| 1559 | a^4 |
|---|
| 1560 | |
|---|
| 1561 | :: |
|---|
| 1562 | |
|---|
| 1563 | sage: K.<a> = NumberField(x^5 - x + 1) |
|---|
| 1564 | sage: (a^4 + a^2 - 3*a + 2).sqrt() |
|---|
| 1565 | a^3 - a^2 |
|---|
| 1566 | |
|---|
| 1567 | ALGORITHM: Use PARI to factor `x^2` - ``self`` in `K`. |
|---|
| 1568 | """ |
|---|
| 1569 | # For now, use pari's factoring abilities |
|---|
| 1570 | R = self.number_field()['t'] |
|---|
| 1571 | f = R([-self, 0, 1]) |
|---|
| 1572 | roots = f.roots() |
|---|
| 1573 | if all: |
|---|
| 1574 | return [r[0] for r in roots] |
|---|
| 1575 | elif len(roots) > 0: |
|---|
| 1576 | return roots[0][0] |
|---|
| 1577 | else: |
|---|
| 1578 | try: |
|---|
| 1579 | # This is what integers, rationals do... |
|---|
| 1580 | from sage.all import SR, sqrt |
|---|
| 1581 | return sqrt(SR(self)) |
|---|
| 1582 | except TypeError: |
|---|
| 1583 | raise ValueError, "%s not a square in %s"%(self, self._parent) |
|---|
| 1584 | |
|---|
| 1585 | def nth_root(self, n, all=False): |
|---|
| 1586 | r""" |
|---|
| 1587 | Return an nth root of self in the given number field. |
|---|
| 1588 | |
|---|
| 1589 | EXAMPLES:: |
|---|
| 1590 | |
|---|
| 1591 | sage: K.<a> = NumberField(x^4-7) |
|---|
| 1592 | sage: K(7).nth_root(2) |
|---|
| 1593 | a^2 |
|---|
| 1594 | sage: K((a-3)^5).nth_root(5) |
|---|
| 1595 | a - 3 |
|---|
| 1596 | |
|---|
| 1597 | ALGORITHM: Use PARI to factor `x^n` - ``self`` in `K`. |
|---|
| 1598 | """ |
|---|
| 1599 | R = self.number_field()['t'] |
|---|
| 1600 | if not self: |
|---|
| 1601 | return [self] if all else self |
|---|
| 1602 | f = (R.gen(0) << (n-1)) - self |
|---|
| 1603 | roots = f.roots() |
|---|
| 1604 | if all: |
|---|
| 1605 | return [r[0] for r in roots] |
|---|
| 1606 | elif len(roots) > 0: |
|---|
| 1607 | return roots[0][0] |
|---|
| 1608 | else: |
|---|
| 1609 | raise ValueError, "%s not a %s-th root in %s"%(self, n, self._parent) |
|---|
| 1610 | |
|---|
| 1611 | def __pow__(base, exp, dummy): |
|---|
| 1612 | """ |
|---|
| 1613 | EXAMPLES:: |
|---|
| 1614 | |
|---|
| 1615 | sage: K.<sqrt2> = QuadraticField(2) |
|---|
| 1616 | sage: sqrt2^2 |
|---|
| 1617 | 2 |
|---|
| 1618 | sage: sqrt2^5 |
|---|
| 1619 | 4*sqrt2 |
|---|
| 1620 | sage: (1+sqrt2)^100 |
|---|
| 1621 | 66992092050551637663438906713182313772*sqrt2 + 94741125149636933417873079920900017937 |
|---|
| 1622 | sage: (1+sqrt2)^-1 |
|---|
| 1623 | sqrt2 - 1 |
|---|
| 1624 | |
|---|
| 1625 | If the exponent is not integral, perform this operation in |
|---|
| 1626 | the symbolic ring:: |
|---|
| 1627 | |
|---|
| 1628 | sage: sqrt2^(1/5) |
|---|
| 1629 | 2^(1/10) |
|---|
| 1630 | sage: sqrt2^sqrt2 |
|---|
| 1631 | 2^(1/2*sqrt(2)) |
|---|
| 1632 | |
|---|
| 1633 | Sage follows Python's convention 0^0 = 1:: |
|---|
| 1634 | |
|---|
| 1635 | sage: a = K(0)^0; a |
|---|
| 1636 | 1 |
|---|
| 1637 | sage: a.parent() |
|---|
| 1638 | Number Field in sqrt2 with defining polynomial x^2 - 2 |
|---|
| 1639 | |
|---|
| 1640 | TESTS:: |
|---|
| 1641 | |
|---|
| 1642 | sage: 2^I |
|---|
| 1643 | 2^I |
|---|
| 1644 | """ |
|---|
| 1645 | if (PY_TYPE_CHECK(base, NumberFieldElement) and |
|---|
| 1646 | (PY_TYPE_CHECK(exp, Integer) or PY_TYPE_CHECK_EXACT(exp, int) or exp in ZZ)): |
|---|
| 1647 | return generic_power_c(base, exp, None) |
|---|
| 1648 | else: |
|---|
| 1649 | cbase, cexp = canonical_coercion(base, exp) |
|---|
| 1650 | if not isinstance(cbase, NumberFieldElement): |
|---|
| 1651 | return cbase ** cexp |
|---|
| 1652 | # Return a symbolic expression. |
|---|
| 1653 | # We use the hold=True keyword argument to prevent the |
|---|
| 1654 | # symbolics library from trying to simplify this expression |
|---|
| 1655 | # again. This would lead to infinite loops otherwise. |
|---|
| 1656 | from sage.symbolic.ring import SR |
|---|
| 1657 | try: |
|---|
| 1658 | res = QQ(base)**exp |
|---|
| 1659 | except TypeError: |
|---|
| 1660 | pass |
|---|
| 1661 | else: |
|---|
| 1662 | if res.parent() is not SR: |
|---|
| 1663 | return parent(cbase)(res) |
|---|
| 1664 | return res |
|---|
| 1665 | sbase = SR(base) |
|---|
| 1666 | if sbase.operator() is operator.pow: |
|---|
| 1667 | nbase, pexp = sbase.operands() |
|---|
| 1668 | return nbase.power(pexp * exp, hold=True) |
|---|
| 1669 | else: |
|---|
| 1670 | return sbase.power(exp, hold=True) |
|---|
| 1671 | |
|---|
| 1672 | cdef void _reduce_c_(self): |
|---|
| 1673 | """ |
|---|
| 1674 | Pull out common factors from the numerator and denominator! |
|---|
| 1675 | """ |
|---|
| 1676 | cdef ZZ_c gcd |
|---|
| 1677 | cdef ZZ_c t1 |
|---|
| 1678 | cdef ZZX_c t2 |
|---|
| 1679 | ZZX_content(t1, self.__numerator) |
|---|
| 1680 | ZZ_GCD(gcd, t1, self.__denominator) |
|---|
| 1681 | if ZZ_sign(gcd) != ZZ_sign(self.__denominator): |
|---|
| 1682 | ZZ_negate(t1, gcd) |
|---|
| 1683 | gcd = t1 |
|---|
| 1684 | ZZX_div_ZZ(t2, self.__numerator, gcd) |
|---|
| 1685 | ZZ_div(t1, self.__denominator, gcd) |
|---|
| 1686 | self.__numerator = t2 |
|---|
| 1687 | self.__denominator = t1 |
|---|
| 1688 | |
|---|
| 1689 | cpdef ModuleElement _add_(self, ModuleElement right): |
|---|
| 1690 | r""" |
|---|
| 1691 | EXAMPLE:: |
|---|
| 1692 | |
|---|
| 1693 | sage: K.<s> = QuadraticField(2) |
|---|
| 1694 | sage: s + s # indirect doctest |
|---|
| 1695 | 2*s |
|---|
| 1696 | sage: s + ZZ(3) # indirect doctest |
|---|
| 1697 | s + 3 |
|---|
| 1698 | """ |
|---|
| 1699 | cdef NumberFieldElement x |
|---|
| 1700 | cdef NumberFieldElement _right = right |
|---|
| 1701 | x = self._new() |
|---|
| 1702 | ZZ_mul(x.__denominator, self.__denominator, _right.__denominator) |
|---|
| 1703 | cdef ZZX_c t1, t2 |
|---|
| 1704 | ZZX_mul_ZZ(t1, self.__numerator, _right.__denominator) |
|---|
| 1705 | ZZX_mul_ZZ(t2, _right.__numerator, self.__denominator) |
|---|
| 1706 | ZZX_add(x.__numerator, t1, t2) |
|---|
| 1707 | x._reduce_c_() |
|---|
| 1708 | return x |
|---|
| 1709 | |
|---|
| 1710 | cpdef ModuleElement _sub_(self, ModuleElement right): |
|---|
| 1711 | r""" |
|---|
| 1712 | EXAMPLES:: |
|---|
| 1713 | |
|---|
| 1714 | sage: K.<a> = NumberField(x^3 + 2) |
|---|
| 1715 | sage: (a/2) - (a + 3) # indirect doctest |
|---|
| 1716 | -1/2*a - 3 |
|---|
| 1717 | """ |
|---|
| 1718 | cdef NumberFieldElement x |
|---|
| 1719 | cdef NumberFieldElement _right = right |
|---|
| 1720 | x = self._new() |
|---|
| 1721 | ZZ_mul(x.__denominator, self.__denominator, _right.__denominator) |
|---|
| 1722 | cdef ZZX_c t1, t2 |
|---|
| 1723 | ZZX_mul_ZZ(t1, self.__numerator, _right.__denominator) |
|---|
| 1724 | ZZX_mul_ZZ(t2, _right.__numerator, self.__denominator) |
|---|
| 1725 | ZZX_sub(x.__numerator, t1, t2) |
|---|
| 1726 | x._reduce_c_() |
|---|
| 1727 | return x |
|---|
| 1728 | |
|---|
| 1729 | cpdef RingElement _mul_(self, RingElement right): |
|---|
| 1730 | """ |
|---|
| 1731 | Returns the product of self and other as elements of a number |
|---|
| 1732 | field. |
|---|
| 1733 | |
|---|
| 1734 | EXAMPLES:: |
|---|
| 1735 | |
|---|
| 1736 | sage: C.<zeta12>=CyclotomicField(12) |
|---|
| 1737 | sage: zeta12*zeta12^11 |
|---|
| 1738 | 1 |
|---|
| 1739 | sage: G.<a> = NumberField(x^3 + 2/3*x + 1) |
|---|
| 1740 | sage: a^3 # indirect doctest |
|---|
| 1741 | -2/3*a - 1 |
|---|
| 1742 | sage: a^3+a # indirect doctest |
|---|
| 1743 | 1/3*a - 1 |
|---|
| 1744 | """ |
|---|
| 1745 | cdef NumberFieldElement x |
|---|
| 1746 | cdef NumberFieldElement _right = right |
|---|
| 1747 | cdef ZZX_c temp |
|---|
| 1748 | cdef ZZ_c temp1 |
|---|
| 1749 | x = self._new() |
|---|
| 1750 | sig_on() |
|---|
| 1751 | # MulMod doesn't handle non-monic polynomials. |
|---|
| 1752 | # Therefore, we handle the non-monic case entirely separately. |
|---|
| 1753 | |
|---|
| 1754 | if ZZ_IsOne(ZZX_LeadCoeff(self.__fld_numerator.x)): |
|---|
| 1755 | ZZ_mul(x.__denominator, self.__denominator, _right.__denominator) |
|---|
| 1756 | ZZX_MulMod(x.__numerator, self.__numerator, _right.__numerator, self.__fld_numerator.x) |
|---|
| 1757 | else: |
|---|
| 1758 | ZZ_mul(x.__denominator, self.__denominator, _right.__denominator) |
|---|
| 1759 | ZZX_mul(x.__numerator, self.__numerator, _right.__numerator) |
|---|
| 1760 | if ZZX_deg(x.__numerator) >= ZZX_deg(self.__fld_numerator.x): |
|---|
| 1761 | ZZX_mul_ZZ( x.__numerator, x.__numerator, self.__fld_denominator.x ) |
|---|
| 1762 | ZZX_mul_ZZ( temp, self.__fld_numerator.x, x.__denominator ) |
|---|
| 1763 | ZZ_power(temp1,ZZX_LeadCoeff(temp),ZZX_deg(x.__numerator)-ZZX_deg(self.__fld_numerator.x)+1) |
|---|
| 1764 | ZZX_PseudoRem(x.__numerator, x.__numerator, temp) |
|---|
| 1765 | ZZ_mul(x.__denominator, x.__denominator, self.__fld_denominator.x) |
|---|
| 1766 | ZZ_mul(x.__denominator, x.__denominator, temp1) |
|---|
| 1767 | sig_off() |
|---|
| 1768 | x._reduce_c_() |
|---|
| 1769 | return x |
|---|
| 1770 | |
|---|
| 1771 | #NOTES: In LiDIA, they build a multiplication table for the |
|---|
| 1772 | #number field, so it's not necessary to reduce modulo the |
|---|
| 1773 | #defining polynomial every time: |
|---|
| 1774 | # src/number_fields/algebraic_num/order.cc: compute_table |
|---|
| 1775 | # but asymptotically fast poly multiplication means it's |
|---|
| 1776 | # actually faster to *not* build a table!?! |
|---|
| 1777 | |
|---|
| 1778 | cpdef RingElement _div_(self, RingElement right): |
|---|
| 1779 | """ |
|---|
| 1780 | Returns the quotient of self and other as elements of a number |
|---|
| 1781 | field. |
|---|
| 1782 | |
|---|
| 1783 | EXAMPLES:: |
|---|
| 1784 | |
|---|
| 1785 | sage: C.<I>=CyclotomicField(4) |
|---|
| 1786 | sage: 1/I # indirect doctest |
|---|
| 1787 | -I |
|---|
| 1788 | sage: I/0 # indirect doctest |
|---|
| 1789 | Traceback (most recent call last): |
|---|
| 1790 | ... |
|---|
| 1791 | ZeroDivisionError: rational division by zero |
|---|
| 1792 | |
|---|
| 1793 | :: |
|---|
| 1794 | |
|---|
| 1795 | sage: G.<a> = NumberField(x^3 + 2/3*x + 1) |
|---|
| 1796 | sage: a/a # indirect doctest |
|---|
| 1797 | 1 |
|---|
| 1798 | sage: 1/a # indirect doctest |
|---|
| 1799 | -a^2 - 2/3 |
|---|
| 1800 | sage: a/0 # indirect doctest |
|---|
| 1801 | Traceback (most recent call last): |
|---|
| 1802 | ... |
|---|
| 1803 | ZeroDivisionError: Number field element division by zero |
|---|
| 1804 | """ |
|---|
| 1805 | cdef NumberFieldElement x |
|---|
| 1806 | cdef NumberFieldElement _right = right |
|---|
| 1807 | cdef ZZX_c inv_num |
|---|
| 1808 | cdef ZZ_c inv_den |
|---|
| 1809 | cdef ZZX_c temp |
|---|
| 1810 | cdef ZZ_c temp1 |
|---|
| 1811 | if not _right: |
|---|
| 1812 | raise ZeroDivisionError, "Number field element division by zero" |
|---|
| 1813 | x = self._new() |
|---|
| 1814 | sig_on() |
|---|
| 1815 | _right._invert_c_(&inv_num, &inv_den) |
|---|
| 1816 | if ZZ_IsOne(ZZX_LeadCoeff(self.__fld_numerator.x)): |
|---|
| 1817 | ZZ_mul(x.__denominator, self.__denominator, inv_den) |
|---|
| 1818 | ZZX_MulMod(x.__numerator, self.__numerator, inv_num, self.__fld_numerator.x) |
|---|
| 1819 | else: |
|---|
| 1820 | ZZ_mul(x.__denominator, self.__denominator, inv_den) |
|---|
| 1821 | ZZX_mul(x.__numerator, self.__numerator, inv_num) |
|---|
| 1822 | if ZZX_deg(x.__numerator) >= ZZX_deg(self.__fld_numerator.x): |
|---|
| 1823 | ZZX_mul_ZZ( x.__numerator, x.__numerator, self.__fld_denominator.x ) |
|---|
| 1824 | ZZX_mul_ZZ( temp, self.__fld_numerator.x, x.__denominator ) |
|---|
| 1825 | ZZ_power(temp1,ZZX_LeadCoeff(temp),ZZX_deg(x.__numerator)-ZZX_deg(self.__fld_numerator.x)+1) |
|---|
| 1826 | ZZX_PseudoRem(x.__numerator, x.__numerator, temp) |
|---|
| 1827 | ZZ_mul(x.__denominator, x.__denominator, self.__fld_denominator.x) |
|---|
| 1828 | ZZ_mul(x.__denominator, x.__denominator, temp1) |
|---|
| 1829 | x._reduce_c_() |
|---|
| 1830 | sig_off() |
|---|
| 1831 | return x |
|---|
| 1832 | |
|---|
| 1833 | def __floordiv__(self, other): |
|---|
| 1834 | """ |
|---|
| 1835 | Return the quotient of self and other. Since these are field |
|---|
| 1836 | elements the floor division is exactly the same as usual division. |
|---|
| 1837 | |
|---|
| 1838 | EXAMPLES:: |
|---|
| 1839 | |
|---|
| 1840 | sage: m.<b> = NumberField(x^4 + x^2 + 2/3) |
|---|
| 1841 | sage: c = (1+b) // (1-b); c |
|---|
| 1842 | 3/4*b^3 + 3/4*b^2 + 3/2*b + 1/2 |
|---|
| 1843 | sage: (1+b) / (1-b) == c |
|---|
| 1844 | True |
|---|
| 1845 | sage: c * (1-b) |
|---|
| 1846 | b + 1 |
|---|
| 1847 | """ |
|---|
| 1848 | return self / other |
|---|
| 1849 | |
|---|
| 1850 | def __nonzero__(self): |
|---|
| 1851 | """ |
|---|
| 1852 | Return True if this number field element is nonzero. |
|---|
| 1853 | |
|---|
| 1854 | EXAMPLES:: |
|---|
| 1855 | |
|---|
| 1856 | sage: m.<b> = CyclotomicField(17) |
|---|
| 1857 | sage: m(0).__nonzero__() |
|---|
| 1858 | False |
|---|
| 1859 | sage: b.__nonzero__() |
|---|
| 1860 | True |
|---|
| 1861 | |
|---|
| 1862 | Nonzero is used by the bool command:: |
|---|
| 1863 | |
|---|
| 1864 | sage: bool(b + 1) |
|---|
| 1865 | True |
|---|
| 1866 | sage: bool(m(0)) |
|---|
| 1867 | False |
|---|
| 1868 | """ |
|---|
| 1869 | return not IsZero_ZZX(self.__numerator) |
|---|
| 1870 | |
|---|
| 1871 | cpdef ModuleElement _neg_(self): |
|---|
| 1872 | r""" |
|---|
| 1873 | EXAMPLE:: |
|---|
| 1874 | |
|---|
| 1875 | sage: K.<a> = NumberField(x^3 + 2) |
|---|
| 1876 | sage: -a # indirect doctest |
|---|
| 1877 | -a |
|---|
| 1878 | """ |
|---|
| 1879 | cdef NumberFieldElement x |
|---|
| 1880 | x = self._new() |
|---|
| 1881 | ZZX_mul_long(x.__numerator, self.__numerator, -1) |
|---|
| 1882 | x.__denominator = self.__denominator |
|---|
| 1883 | return x |
|---|
| 1884 | |
|---|
| 1885 | def __copy__(self): |
|---|
| 1886 | r""" |
|---|
| 1887 | EXAMPLE:: |
|---|
| 1888 | |
|---|
| 1889 | sage: K.<a> = NumberField(x^3 + 2) |
|---|
| 1890 | sage: b = copy(a) |
|---|
| 1891 | sage: b == a |
|---|
| 1892 | True |
|---|
| 1893 | sage: b is a |
|---|
| 1894 | False |
|---|
| 1895 | """ |
|---|
| 1896 | cdef NumberFieldElement x |
|---|
| 1897 | x = self._new() |
|---|
| 1898 | x.__numerator = self.__numerator |
|---|
| 1899 | x.__denominator = self.__denominator |
|---|
| 1900 | return x |
|---|
| 1901 | |
|---|
| 1902 | def __int__(self): |
|---|
| 1903 | """ |
|---|
| 1904 | Attempt to convert this number field element to a Python integer, |
|---|
| 1905 | if possible. |
|---|
| 1906 | |
|---|
| 1907 | EXAMPLES:: |
|---|
| 1908 | |
|---|
| 1909 | sage: C.<I>=CyclotomicField(4) |
|---|
| 1910 | sage: int(1/I) |
|---|
| 1911 | Traceback (most recent call last): |
|---|
| 1912 | ... |
|---|
| 1913 | TypeError: cannot coerce nonconstant polynomial to int |
|---|
| 1914 | sage: int(I*I) |
|---|
| 1915 | -1 |
|---|
| 1916 | |
|---|
| 1917 | :: |
|---|
| 1918 | |
|---|
| 1919 | sage: K.<a> = NumberField(x^10 - x - 1) |
|---|
| 1920 | sage: int(a) |
|---|
| 1921 | Traceback (most recent call last): |
|---|
| 1922 | ... |
|---|
| 1923 | TypeError: cannot coerce nonconstant polynomial to int |
|---|
| 1924 | sage: int(K(9390283)) |
|---|
| 1925 | 9390283 |
|---|
| 1926 | |
|---|
| 1927 | The semantics are like in Python, so the value does not have to |
|---|
| 1928 | preserved. |
|---|
| 1929 | |
|---|
| 1930 | :: |
|---|
| 1931 | |
|---|
| 1932 | sage: int(K(393/29)) |
|---|
| 1933 | 13 |
|---|
| 1934 | """ |
|---|
| 1935 | return int(self.polynomial()) |
|---|
| 1936 | |
|---|
| 1937 | def __long__(self): |
|---|
| 1938 | """ |
|---|
| 1939 | Attempt to convert this number field element to a Python long, if |
|---|
| 1940 | possible. |
|---|
| 1941 | |
|---|
| 1942 | EXAMPLES:: |
|---|
| 1943 | |
|---|
| 1944 | sage: K.<a> = NumberField(x^10 - x - 1) |
|---|
| 1945 | sage: long(a) |
|---|
| 1946 | Traceback (most recent call last): |
|---|
| 1947 | ... |
|---|
| 1948 | TypeError: cannot coerce nonconstant polynomial to long |
|---|
| 1949 | sage: long(K(1234)) |
|---|
| 1950 | 1234L |
|---|
| 1951 | |
|---|
| 1952 | The value does not have to be preserved, in the case of fractions. |
|---|
| 1953 | |
|---|
| 1954 | :: |
|---|
| 1955 | |
|---|
| 1956 | sage: long(K(393/29)) |
|---|
| 1957 | 13L |
|---|
| 1958 | """ |
|---|
| 1959 | return long(self.polynomial()) |
|---|
| 1960 | |
|---|
| 1961 | cdef void _invert_c_(self, ZZX_c *num, ZZ_c *den): |
|---|
| 1962 | """ |
|---|
| 1963 | Computes the numerator and denominator of the multiplicative |
|---|
| 1964 | inverse of this element. |
|---|
| 1965 | |
|---|
| 1966 | Suppose that this element is x/d and the parent mod'ding polynomial |
|---|
| 1967 | is M/D. The NTL function XGCD( r, s, t, a, b ) computes r,s,t such |
|---|
| 1968 | that `r=s*a+t*b`. We compute XGCD( r, s, t, x\*D, M\*d ) |
|---|
| 1969 | and set num=s\*D\*d den=r |
|---|
| 1970 | |
|---|
| 1971 | EXAMPLES: |
|---|
| 1972 | |
|---|
| 1973 | I'd love to, but since we are dealing with c-types, I |
|---|
| 1974 | can't at this level. Check __invert__ for doc-tests that rely |
|---|
| 1975 | on this functionality. |
|---|
| 1976 | """ |
|---|
| 1977 | cdef ZZX_c t # unneeded except to be there |
|---|
| 1978 | cdef ZZX_c a, b |
|---|
| 1979 | ZZX_mul_ZZ( a, self.__numerator, self.__fld_denominator.x ) |
|---|
| 1980 | ZZX_mul_ZZ( b, self.__fld_numerator.x, self.__denominator ) |
|---|
| 1981 | ZZX_XGCD( den[0], num[0], t, a, b, 1 ) |
|---|
| 1982 | ZZX_mul_ZZ( num[0], num[0], self.__fld_denominator.x ) |
|---|
| 1983 | ZZX_mul_ZZ( num[0], num[0], self.__denominator ) |
|---|
| 1984 | |
|---|
| 1985 | def __invert__(self): |
|---|
| 1986 | """ |
|---|
| 1987 | Returns the multiplicative inverse of self in the number field. |
|---|
| 1988 | |
|---|
| 1989 | EXAMPLES:: |
|---|
| 1990 | |
|---|
| 1991 | sage: C.<I>=CyclotomicField(4) |
|---|
| 1992 | sage: ~I |
|---|
| 1993 | -I |
|---|
| 1994 | sage: (2*I).__invert__() |
|---|
| 1995 | -1/2*I |
|---|
| 1996 | """ |
|---|
| 1997 | if IsZero_ZZX(self.__numerator): |
|---|
| 1998 | raise ZeroDivisionError |
|---|
| 1999 | cdef NumberFieldElement x |
|---|
| 2000 | x = self._new() |
|---|
| 2001 | self._invert_c_(&x.__numerator, &x.__denominator) |
|---|
| 2002 | x._reduce_c_() |
|---|
| 2003 | return x |
|---|
| 2004 | |
|---|
| 2005 | def _integer_(self, Z=None): |
|---|
| 2006 | """ |
|---|
| 2007 | Returns an integer if this element is actually an integer. |
|---|
| 2008 | |
|---|
| 2009 | EXAMPLES:: |
|---|
| 2010 | |
|---|
| 2011 | sage: C.<I>=CyclotomicField(4) |
|---|
| 2012 | sage: (~I)._integer_() |
|---|
| 2013 | Traceback (most recent call last): |
|---|
| 2014 | ... |
|---|
| 2015 | TypeError: Unable to coerce -I to an integer |
|---|
| 2016 | sage: (2*I*I)._integer_() |
|---|
| 2017 | -2 |
|---|
| 2018 | """ |
|---|
| 2019 | if ZZX_deg(self.__numerator) >= 1: |
|---|
| 2020 | raise TypeError, "Unable to coerce %s to an integer"%self |
|---|
| 2021 | return ZZ(self._rational_()) |
|---|
| 2022 | |
|---|
| 2023 | def _rational_(self): |
|---|
| 2024 | """ |
|---|
| 2025 | Returns a rational number if this element is actually a rational |
|---|
| 2026 | number. |
|---|
| 2027 | |
|---|
| 2028 | EXAMPLES:: |
|---|
| 2029 | |
|---|
| 2030 | sage: C.<I>=CyclotomicField(4) |
|---|
| 2031 | sage: (~I)._rational_() |
|---|
| 2032 | Traceback (most recent call last): |
|---|
| 2033 | ... |
|---|
| 2034 | TypeError: Unable to coerce -I to a rational |
|---|
| 2035 | sage: (I*I/2)._rational_() |
|---|
| 2036 | -1/2 |
|---|
| 2037 | """ |
|---|
| 2038 | if ZZX_deg(self.__numerator) >= 1: |
|---|
| 2039 | raise TypeError, "Unable to coerce %s to a rational"%self |
|---|
| 2040 | cdef Integer num |
|---|
| 2041 | num = PY_NEW(Integer) |
|---|
| 2042 | ZZX_getitem_as_mpz(&num.value, &self.__numerator, 0) |
|---|
| 2043 | return num / (<IntegerRing_class>ZZ)._coerce_ZZ(&self.__denominator) |
|---|
| 2044 | |
|---|
| 2045 | def _symbolic_(self, SR): |
|---|
| 2046 | """ |
|---|
| 2047 | If an embedding into CC is specified, then a representation of this |
|---|
| 2048 | element can be made in the symbolic ring (assuming roots of the |
|---|
| 2049 | minimal polynomial can be found symbolically). |
|---|
| 2050 | |
|---|
| 2051 | EXAMPLES:: |
|---|
| 2052 | |
|---|
| 2053 | sage: K.<a> = QuadraticField(2) |
|---|
| 2054 | sage: SR(a) # indirect doctest |
|---|
| 2055 | sqrt(2) |
|---|
| 2056 | sage: SR(3*a-5) # indirect doctest |
|---|
| 2057 | 3*sqrt(2) - 5 |
|---|
| 2058 | sage: K.<a> = QuadraticField(2, embedding=-1.4) |
|---|
| 2059 | sage: SR(a) # indirect doctest |
|---|
| 2060 | -sqrt(2) |
|---|
| 2061 | sage: K.<a> = NumberField(x^2 - 2) |
|---|
| 2062 | sage: SR(a) # indirect doctest |
|---|
| 2063 | Traceback (most recent call last): |
|---|
| 2064 | ... |
|---|
| 2065 | TypeError: An embedding into RR or CC must be specified. |
|---|
| 2066 | |
|---|
| 2067 | Now a more complicated example:: |
|---|
| 2068 | |
|---|
| 2069 | sage: K.<a> = NumberField(x^3 + x - 1, embedding=0.68) |
|---|
| 2070 | sage: b = SR(a); b # indirect doctest |
|---|
| 2071 | 1/3*(3*(1/18*sqrt(3)*sqrt(31) + 1/2)^(2/3) - 1)/(1/18*sqrt(3)*sqrt(31) + 1/2)^(1/3) |
|---|
| 2072 | |
|---|
| 2073 | sage: (b^3 + b - 1).simplify_radical() |
|---|
| 2074 | 0 |
|---|
| 2075 | |
|---|
| 2076 | Make sure we got the right one:: |
|---|
| 2077 | |
|---|
| 2078 | sage: CC(a) |
|---|
| 2079 | 0.682327803828019 |
|---|
| 2080 | sage: CC(b) |
|---|
| 2081 | 0.682327803828019 |
|---|
| 2082 | |
|---|
| 2083 | Special case for cyclotomic fields:: |
|---|
| 2084 | |
|---|
| 2085 | sage: K.<zeta> = CyclotomicField(19) |
|---|
| 2086 | sage: SR(zeta) # indirect doctest |
|---|
| 2087 | e^(2/19*I*pi) |
|---|
| 2088 | sage: CC(zeta) |
|---|
| 2089 | 0.945817241700635 + 0.324699469204683*I |
|---|
| 2090 | sage: CC(SR(zeta)) |
|---|
| 2091 | 0.945817241700635 + 0.324699469204683*I |
|---|
| 2092 | |
|---|
| 2093 | sage: SR(zeta^5 + 2) |
|---|
| 2094 | e^(10/19*I*pi) + 2 |
|---|
| 2095 | |
|---|
| 2096 | For degree greater than 5, sometimes Galois theory prevents a |
|---|
| 2097 | closed-form solution. In this case, a numerical approximation |
|---|
| 2098 | is used:: |
|---|
| 2099 | |
|---|
| 2100 | sage: K.<a> = NumberField(x^5-x+1, embedding=-1) |
|---|
| 2101 | sage: SR(a) |
|---|
| 2102 | -1.1673040153 |
|---|
| 2103 | |
|---|
| 2104 | :: |
|---|
| 2105 | |
|---|
| 2106 | sage: K.<a> = NumberField(x^6-x^3-1, embedding=1) |
|---|
| 2107 | sage: SR(a) |
|---|
| 2108 | (1/2*sqrt(5) + 1/2)^(1/3) |
|---|
| 2109 | """ |
|---|
| 2110 | if self.__symbolic is None: |
|---|
| 2111 | |
|---|
| 2112 | K = self._parent.fraction_field() |
|---|
| 2113 | |
|---|
| 2114 | gen = K.gen() |
|---|
| 2115 | if not self is gen: |
|---|
| 2116 | try: |
|---|
| 2117 | # share the hard work... |
|---|
| 2118 | gen_image = gen._symbolic_(SR) |
|---|
| 2119 | self.__symbolic = self.polynomial()(gen_image) |
|---|
| 2120 | return self.__symbolic |
|---|
| 2121 | except TypeError: |
|---|
| 2122 | pass # we may still be able to do this particular element... |
|---|
| 2123 | |
|---|
| 2124 | embedding = K.specified_complex_embedding() |
|---|
| 2125 | if embedding is None: |
|---|
| 2126 | raise TypeError, "An embedding into RR or CC must be specified." |
|---|
| 2127 | |
|---|
| 2128 | if isinstance(K, number_field.NumberField_cyclotomic): |
|---|
| 2129 | # solution by radicals may be difficult, but we have a closed form |
|---|
| 2130 | from sage.all import exp, I, pi, ComplexField, RR |
|---|
| 2131 | CC = ComplexField(53) |
|---|
| 2132 | two_pi_i = 2 * pi * I |
|---|
| 2133 | k = ( K._n()*CC(K.gen()).log() / CC(two_pi_i) ).real().round() # n ln z / (2 pi i) |
|---|
| 2134 | gen_image = exp(k*two_pi_i/K._n()) |
|---|
| 2135 | if self is gen: |
|---|
| 2136 | self.__symbolic = gen_image |
|---|
| 2137 | else: |
|---|
| 2138 | self.__symbolic = self.polynomial()(gen_image) |
|---|
| 2139 | else: |
|---|
| 2140 | # try to solve the minpoly and choose the closest root |
|---|
| 2141 | poly = self.minpoly() |
|---|
| 2142 | roots = [] |
|---|
| 2143 | var = SR(poly.variable_name()) |
|---|
| 2144 | for soln in SR(poly).solve(var, to_poly_solve=True): |
|---|
| 2145 | if soln.lhs() == var: |
|---|
| 2146 | roots.append(soln.rhs()) |
|---|
| 2147 | if len(roots) != poly.degree(): |
|---|
| 2148 | raise TypeError, "Unable to solve by radicals." |
|---|
| 2149 | from number_field_morphisms import matching_root |
|---|
| 2150 | from sage.rings.complex_field import ComplexField |
|---|
| 2151 | gen_image = matching_root(roots, self, ambient_field=ComplexField(53), margin=2) |
|---|
| 2152 | if gen_image is not None: |
|---|
| 2153 | self.__symbolic = gen_image |
|---|
| 2154 | else: |
|---|
| 2155 | # should be rare, e.g. if there is insufficient precision |
|---|
| 2156 | raise TypeError, "Unable to determine which root in SR is this element." |
|---|
| 2157 | |
|---|
| 2158 | return self.__symbolic |
|---|
| 2159 | |
|---|
| 2160 | def galois_conjugates(self, K): |
|---|
| 2161 | r""" |
|---|
| 2162 | Return all Gal(Qbar/Q)-conjugates of this number field element in |
|---|
| 2163 | the field K. |
|---|
| 2164 | |
|---|
| 2165 | EXAMPLES: |
|---|
| 2166 | |
|---|
| 2167 | In the first example the conjugates are obvious:: |
|---|
| 2168 | |
|---|
| 2169 | sage: K.<a> = NumberField(x^2 - 2) |
|---|
| 2170 | sage: a.galois_conjugates(K) |
|---|
| 2171 | [a, -a] |
|---|
| 2172 | sage: K(3).galois_conjugates(K) |
|---|
| 2173 | [3] |
|---|
| 2174 | |
|---|
| 2175 | In this example the field is not Galois, so we have to pass to an |
|---|
| 2176 | extension to obtain the Galois conjugates. |
|---|
| 2177 | |
|---|
| 2178 | :: |
|---|
| 2179 | |
|---|
| 2180 | sage: K.<a> = NumberField(x^3 - 2) |
|---|
| 2181 | sage: c = a.galois_conjugates(K); c |
|---|
| 2182 | [a] |
|---|
| 2183 | sage: K.<a> = NumberField(x^3 - 2) |
|---|
| 2184 | sage: c = a.galois_conjugates(K.galois_closure('a1')); c |
|---|
| 2185 | [1/84*a1^4 + 13/42*a1, -1/252*a1^4 - 55/126*a1, -1/126*a1^4 + 8/63*a1] |
|---|
| 2186 | sage: c[0]^3 |
|---|
| 2187 | 2 |
|---|
| 2188 | sage: parent(c[0]) |
|---|
| 2189 | Number Field in a1 with defining polynomial x^6 + 40*x^3 + 1372 |
|---|
| 2190 | sage: parent(c[0]).is_galois() |
|---|
| 2191 | True |
|---|
| 2192 | |
|---|
| 2193 | There is only one Galois conjugate of `\sqrt[3]{2}` in |
|---|
| 2194 | `\QQ(\sqrt[3]{2})`. |
|---|
| 2195 | |
|---|
| 2196 | :: |
|---|
| 2197 | |
|---|
| 2198 | sage: a.galois_conjugates(K) |
|---|
| 2199 | [a] |
|---|
| 2200 | |
|---|
| 2201 | Galois conjugates of `\sqrt[3]{2}` in the field |
|---|
| 2202 | `\QQ(\zeta_3,\sqrt[3]{2})`:: |
|---|
| 2203 | |
|---|
| 2204 | sage: L.<a> = CyclotomicField(3).extension(x^3 - 2) |
|---|
| 2205 | sage: a.galois_conjugates(L) |
|---|
| 2206 | [a, (-zeta3 - 1)*a, zeta3*a] |
|---|
| 2207 | """ |
|---|
| 2208 | f = self.absolute_minpoly() |
|---|
| 2209 | g = K['x'](f) |
|---|
| 2210 | return [a for a,_ in g.roots()] |
|---|
| 2211 | |
|---|
| 2212 | def conjugate(self): |
|---|
| 2213 | """ |
|---|
| 2214 | Return the complex conjugate of the number field element. |
|---|
| 2215 | |
|---|
| 2216 | This is only well-defined for fields contained in CM fields |
|---|
| 2217 | (i.e. for totally real fields and CM fields). Recall that a CM |
|---|
| 2218 | field is a totally imaginary quadratic extension of a totally |
|---|
| 2219 | real field. For other fields, a ValueError is raised. |
|---|
| 2220 | |
|---|
| 2221 | EXAMPLES:: |
|---|
| 2222 | |
|---|
| 2223 | sage: k.<I> = QuadraticField(-1) |
|---|
| 2224 | sage: I.conjugate() |
|---|
| 2225 | -I |
|---|
| 2226 | sage: (I/(1+I)).conjugate() |
|---|
| 2227 | -1/2*I + 1/2 |
|---|
| 2228 | sage: z6 = CyclotomicField(6).gen(0) |
|---|
| 2229 | sage: (2*z6).conjugate() |
|---|
| 2230 | -2*zeta6 + 2 |
|---|
| 2231 | |
|---|
| 2232 | The following example now works. |
|---|
| 2233 | |
|---|
| 2234 | :: |
|---|
| 2235 | |
|---|
| 2236 | sage: F.<b> = NumberField(x^2 - 2) |
|---|
| 2237 | sage: K.<j> = F.extension(x^2 + 1) |
|---|
| 2238 | sage: j.conjugate() |
|---|
| 2239 | -j |
|---|
| 2240 | |
|---|
| 2241 | Raise a ValueError if the field is not contained in a CM field. |
|---|
| 2242 | |
|---|
| 2243 | :: |
|---|
| 2244 | |
|---|
| 2245 | sage: K.<b> = NumberField(x^3 - 2) |
|---|
| 2246 | sage: b.conjugate() |
|---|
| 2247 | Traceback (most recent call last): |
|---|
| 2248 | ... |
|---|
| 2249 | ValueError: Complex conjugation is only well-defined for fields contained in CM fields. |
|---|
| 2250 | |
|---|
| 2251 | An example of a non-quadratic totally real field. |
|---|
| 2252 | |
|---|
| 2253 | :: |
|---|
| 2254 | |
|---|
| 2255 | sage: F.<a> = NumberField(x^4 + x^3 - 3*x^2 - x + 1) |
|---|
| 2256 | sage: a.conjugate() |
|---|
| 2257 | a |
|---|
| 2258 | |
|---|
| 2259 | An example of a non-cyclotomic CM field. |
|---|
| 2260 | |
|---|
| 2261 | :: |
|---|
| 2262 | |
|---|
| 2263 | sage: K.<a> = NumberField(x^4 - x^3 + 2*x^2 + x + 1) |
|---|
| 2264 | sage: a.conjugate() |
|---|
| 2265 | -1/2*a^3 - a - 1/2 |
|---|
| 2266 | sage: (2*a^2 - 1).conjugate() |
|---|
| 2267 | a^3 - 2*a^2 - 2 |
|---|
| 2268 | |
|---|
| 2269 | """ |
|---|
| 2270 | |
|---|
| 2271 | nf = self.number_field() |
|---|
| 2272 | return nf.complex_conjugation()(self) |
|---|
| 2273 | |
|---|
| 2274 | def polynomial(self, var='x'): |
|---|
| 2275 | """ |
|---|
| 2276 | Return the underlying polynomial corresponding to this number field |
|---|
| 2277 | element. |
|---|
| 2278 | |
|---|
| 2279 | The resulting polynomial is currently *not* cached. |
|---|
| 2280 | |
|---|
| 2281 | EXAMPLES:: |
|---|
| 2282 | |
|---|
| 2283 | sage: K.<a> = NumberField(x^5 - x - 1) |
|---|
| 2284 | sage: f = (-2/3 + 1/3*a)^4; f |
|---|
| 2285 | 1/81*a^4 - 8/81*a^3 + 8/27*a^2 - 32/81*a + 16/81 |
|---|
| 2286 | sage: g = f.polynomial(); g |
|---|
| 2287 | 1/81*x^4 - 8/81*x^3 + 8/27*x^2 - 32/81*x + 16/81 |
|---|
| 2288 | sage: parent(g) |
|---|
| 2289 | Univariate Polynomial Ring in x over Rational Field |
|---|
| 2290 | |
|---|
| 2291 | Note that the result of this function is not cached (should this be |
|---|
| 2292 | changed?):: |
|---|
| 2293 | |
|---|
| 2294 | sage: g is f.polynomial() |
|---|
| 2295 | False |
|---|
| 2296 | """ |
|---|
| 2297 | return QQ[var](self._coefficients()) |
|---|
| 2298 | |
|---|
| 2299 | def __hash__(self): |
|---|
| 2300 | """ |
|---|
| 2301 | Return hash of this number field element, which is just the |
|---|
| 2302 | hash of the underlying polynomial. |
|---|
| 2303 | |
|---|
| 2304 | EXAMPLE:: |
|---|
| 2305 | |
|---|
| 2306 | sage: K.<b> = NumberField(x^3 - 2) |
|---|
| 2307 | sage: hash(b^2 + 1) == hash((b^2 + 1).polynomial()) # indirect doctest |
|---|
| 2308 | True |
|---|
| 2309 | """ |
|---|
| 2310 | return hash(self.polynomial()) |
|---|
| 2311 | |
|---|
| 2312 | def _coefficients(self): |
|---|
| 2313 | """ |
|---|
| 2314 | Return the coefficients of the underlying polynomial corresponding |
|---|
| 2315 | to this number field element. |
|---|
| 2316 | |
|---|
| 2317 | OUTPUT: |
|---|
| 2318 | |
|---|
| 2319 | - a list whose length corresponding to the degree of this |
|---|
| 2320 | element written in terms of a generator. |
|---|
| 2321 | |
|---|
| 2322 | EXAMPLES: |
|---|
| 2323 | |
|---|
| 2324 | sage: K.<b> = NumberField(x^3 - 2) |
|---|
| 2325 | sage: (b^2 + 1)._coefficients() |
|---|
| 2326 | [1, 0, 1] |
|---|
| 2327 | """ |
|---|
| 2328 | coeffs = [] |
|---|
| 2329 | cdef Integer den = (<IntegerRing_class>ZZ)._coerce_ZZ(&self.__denominator) |
|---|
| 2330 | cdef Integer numCoeff |
|---|
| 2331 | cdef int i |
|---|
| 2332 | for i from 0 <= i <= ZZX_deg(self.__numerator): |
|---|
| 2333 | numCoeff = PY_NEW(Integer) |
|---|
| 2334 | ZZX_getitem_as_mpz(&numCoeff.value, &self.__numerator, i) |
|---|
| 2335 | coeffs.append( numCoeff / den ) |
|---|
| 2336 | return coeffs |
|---|
| 2337 | |
|---|
| 2338 | cdef void _ntl_coeff_as_mpz(self, mpz_t* z, long i): |
|---|
| 2339 | if i > ZZX_deg(self.__numerator): |
|---|
| 2340 | mpz_set_ui(z[0], 0) |
|---|
| 2341 | else: |
|---|
| 2342 | ZZX_getitem_as_mpz(z, &self.__numerator, i) |
|---|
| 2343 | |
|---|
| 2344 | cdef void _ntl_denom_as_mpz(self, mpz_t* z): |
|---|
| 2345 | cdef Integer denom = (<IntegerRing_class>ZZ)._coerce_ZZ(&self.__denominator) |
|---|
| 2346 | mpz_set(z[0], denom.value) |
|---|
| 2347 | |
|---|
| 2348 | def denominator(self): |
|---|
| 2349 | """ |
|---|
| 2350 | Return the denominator of this element, which is by definition the |
|---|
| 2351 | denominator of the corresponding polynomial representation. I.e., |
|---|
| 2352 | elements of number fields are represented as a polynomial (in |
|---|
| 2353 | reduced form) modulo the modulus of the number field, and the |
|---|
| 2354 | denominator is the denominator of this polynomial. |
|---|
| 2355 | |
|---|
| 2356 | EXAMPLES:: |
|---|
| 2357 | |
|---|
| 2358 | sage: K.<z> = CyclotomicField(3) |
|---|
| 2359 | sage: a = 1/3 + (1/5)*z |
|---|
| 2360 | sage: print a.denominator() |
|---|
| 2361 | 15 |
|---|
| 2362 | """ |
|---|
| 2363 | return (<IntegerRing_class>ZZ)._coerce_ZZ(&self.__denominator) |
|---|
| 2364 | |
|---|
| 2365 | def _set_multiplicative_order(self, n): |
|---|
| 2366 | """ |
|---|
| 2367 | Set the multiplicative order of this number field element. |
|---|
| 2368 | |
|---|
| 2369 | .. warning:: |
|---|
| 2370 | |
|---|
| 2371 | Use with caution - only for internal use! End users should |
|---|
| 2372 | never call this unless they have a very good reason to do |
|---|
| 2373 | so. |
|---|
| 2374 | |
|---|
| 2375 | EXAMPLES:: |
|---|
| 2376 | |
|---|
| 2377 | sage: K.<a> = NumberField(x^2 + x + 1) |
|---|
| 2378 | sage: a._set_multiplicative_order(3) |
|---|
| 2379 | sage: a.multiplicative_order() |
|---|
| 2380 | 3 |
|---|
| 2381 | |
|---|
| 2382 | You can be evil with this so be careful. That's why the function |
|---|
| 2383 | name begins with an underscore. |
|---|
| 2384 | |
|---|
| 2385 | :: |
|---|
| 2386 | |
|---|
| 2387 | sage: a._set_multiplicative_order(389) |
|---|
| 2388 | sage: a.multiplicative_order() |
|---|
| 2389 | 389 |
|---|
| 2390 | """ |
|---|
| 2391 | self.__multiplicative_order = n |
|---|
| 2392 | |
|---|
| 2393 | def multiplicative_order(self): |
|---|
| 2394 | """ |
|---|
| 2395 | Return the multiplicative order of this number field element. |
|---|
| 2396 | |
|---|
| 2397 | EXAMPLES:: |
|---|
| 2398 | |
|---|
| 2399 | sage: K.<z> = CyclotomicField(5) |
|---|
| 2400 | sage: z.multiplicative_order() |
|---|
| 2401 | 5 |
|---|
| 2402 | sage: (-z).multiplicative_order() |
|---|
| 2403 | 10 |
|---|
| 2404 | sage: (1+z).multiplicative_order() |
|---|
| 2405 | +Infinity |
|---|
| 2406 | |
|---|
| 2407 | sage: x = polygen(QQ) |
|---|
| 2408 | sage: K.<a>=NumberField(x^40 - x^20 + 4) |
|---|
| 2409 | sage: u = 1/4*a^30 + 1/4*a^10 + 1/2 |
|---|
| 2410 | sage: u.multiplicative_order() |
|---|
| 2411 | 6 |
|---|
| 2412 | sage: a.multiplicative_order() |
|---|
| 2413 | +Infinity |
|---|
| 2414 | |
|---|
| 2415 | An example in a relative extension:: |
|---|
| 2416 | |
|---|
| 2417 | sage: K.<a, b> = NumberField([x^2 + x + 1, x^2 - 3]) |
|---|
| 2418 | sage: z = (a - 1)*b/3 |
|---|
| 2419 | sage: z.multiplicative_order() |
|---|
| 2420 | 12 |
|---|
| 2421 | sage: z^12==1 and z^6!=1 and z^4!=1 |
|---|
| 2422 | True |
|---|
| 2423 | |
|---|
| 2424 | """ |
|---|
| 2425 | if self.__multiplicative_order is not None: |
|---|
| 2426 | return self.__multiplicative_order |
|---|
| 2427 | |
|---|
| 2428 | one = self.number_field().one_element() |
|---|
| 2429 | infinity = sage.rings.infinity.infinity |
|---|
| 2430 | |
|---|
| 2431 | if self == one: |
|---|
| 2432 | self.__multiplicative_order = ZZ(1) |
|---|
| 2433 | return self.__multiplicative_order |
|---|
| 2434 | if self == -one: |
|---|
| 2435 | self.__multiplicative_order = ZZ(2) |
|---|
| 2436 | return self.__multiplicative_order |
|---|
| 2437 | |
|---|
| 2438 | if isinstance(self.number_field(), number_field.NumberField_cyclotomic): |
|---|
| 2439 | t = self.number_field()._multiplicative_order_table() |
|---|
| 2440 | f = self.polynomial() |
|---|
| 2441 | if t.has_key(f): |
|---|
| 2442 | self.__multiplicative_order = t[f] |
|---|
| 2443 | return self.__multiplicative_order |
|---|
| 2444 | else: |
|---|
| 2445 | self.__multiplicative_order = sage.rings.infinity.infinity |
|---|
| 2446 | return self.__multiplicative_order |
|---|
| 2447 | |
|---|
| 2448 | if self.is_rational_c() or not self.is_integral() or not self.norm() ==1: |
|---|
| 2449 | self.__multiplicative_order = infinity |
|---|
| 2450 | return self.__multiplicative_order |
|---|
| 2451 | |
|---|
| 2452 | # Now we have a unit of norm 1, and check if it is a root of unity |
|---|
| 2453 | |
|---|
| 2454 | n = self.number_field().zeta_order() |
|---|
| 2455 | if not self**n ==1: |
|---|
| 2456 | self.__multiplicative_order = infinity |
|---|
| 2457 | return self.__multiplicative_order |
|---|
| 2458 | from sage.groups.generic import order_from_multiple |
|---|
| 2459 | self.__multiplicative_order = order_from_multiple(self,n,operation='*') |
|---|
| 2460 | return self.__multiplicative_order |
|---|
| 2461 | |
|---|
| 2462 | def additive_order(self): |
|---|
| 2463 | r""" |
|---|
| 2464 | Return the additive order of this element (i.e. infinity if |
|---|
| 2465 | self != 0, 1 if self == 0) |
|---|
| 2466 | |
|---|
| 2467 | EXAMPLES:: |
|---|
| 2468 | |
|---|
| 2469 | sage: K.<u> = NumberField(x^4 - 3*x^2 + 3) |
|---|
| 2470 | sage: u.additive_order() |
|---|
| 2471 | +Infinity |
|---|
| 2472 | sage: K(0).additive_order() |
|---|
| 2473 | 1 |
|---|
| 2474 | sage: K.ring_of_integers().characteristic() # implicit doctest |
|---|
| 2475 | 0 |
|---|
| 2476 | """ |
|---|
| 2477 | if self == 0: return 1 |
|---|
| 2478 | else: return sage.rings.infinity.infinity |
|---|
| 2479 | |
|---|
| 2480 | cdef bint is_rational_c(self): |
|---|
| 2481 | return ZZX_deg(self.__numerator) == 0 |
|---|
| 2482 | |
|---|
| 2483 | def trace(self, K=None): |
|---|
| 2484 | """ |
|---|
| 2485 | Return the absolute or relative trace of this number field |
|---|
| 2486 | element. |
|---|
| 2487 | |
|---|
| 2488 | If K is given then K must be a subfield of the parent L of self, in |
|---|
| 2489 | which case the trace is the relative trace from L to K. In all |
|---|
| 2490 | other cases, the trace is the absolute trace down to QQ. |
|---|
| 2491 | |
|---|
| 2492 | EXAMPLES:: |
|---|
| 2493 | |
|---|
| 2494 | sage: K.<a> = NumberField(x^3 -132/7*x^2 + x + 1); K |
|---|
| 2495 | Number Field in a with defining polynomial x^3 - 132/7*x^2 + x + 1 |
|---|
| 2496 | sage: a.trace() |
|---|
| 2497 | 132/7 |
|---|
| 2498 | sage: (a+1).trace() == a.trace() + 3 |
|---|
| 2499 | True |
|---|
| 2500 | |
|---|
| 2501 | If we are in an order, the trace is an integer:: |
|---|
| 2502 | |
|---|
| 2503 | sage: K.<zeta> = CyclotomicField(17) |
|---|
| 2504 | sage: R = K.ring_of_integers() |
|---|
| 2505 | sage: R(zeta).trace().parent() |
|---|
| 2506 | Integer Ring |
|---|
| 2507 | |
|---|
| 2508 | TESTS:: |
|---|
| 2509 | |
|---|
| 2510 | sage: F.<z> = CyclotomicField(5) ; t = 3*z**3 + 4*z**2 + 2 |
|---|
| 2511 | sage: t.trace(F) |
|---|
| 2512 | 3*z^3 + 4*z^2 + 2 |
|---|
| 2513 | """ |
|---|
| 2514 | if K is None: |
|---|
| 2515 | trace = self._pari_('x').trace() |
|---|
| 2516 | return QQ(trace) if self._parent.is_field() else ZZ(trace) |
|---|
| 2517 | return self.matrix(K).trace() |
|---|
| 2518 | |
|---|
| 2519 | def norm(self, K=None): |
|---|
| 2520 | """ |
|---|
| 2521 | Return the absolute or relative norm of this number field element. |
|---|
| 2522 | |
|---|
| 2523 | If K is given then K must be a subfield of the parent L of self, in |
|---|
| 2524 | which case the norm is the relative norm from L to K. In all other |
|---|
| 2525 | cases, the norm is the absolute norm down to QQ. |
|---|
| 2526 | |
|---|
| 2527 | EXAMPLES:: |
|---|
| 2528 | |
|---|
| 2529 | sage: K.<a> = NumberField(x^3 + x^2 + x - 132/7); K |
|---|
| 2530 | Number Field in a with defining polynomial x^3 + x^2 + x - 132/7 |
|---|
| 2531 | sage: a.norm() |
|---|
| 2532 | 132/7 |
|---|
| 2533 | sage: factor(a.norm()) |
|---|
| 2534 | 2^2 * 3 * 7^-1 * 11 |
|---|
| 2535 | sage: K(0).norm() |
|---|
| 2536 | 0 |
|---|
| 2537 | |
|---|
| 2538 | Some complicated relatives norms in a tower of number fields. |
|---|
| 2539 | |
|---|
| 2540 | :: |
|---|
| 2541 | |
|---|
| 2542 | sage: K.<a,b,c> = NumberField([x^2 + 1, x^2 + 3, x^2 + 5]) |
|---|
| 2543 | sage: L = K.base_field(); M = L.base_field() |
|---|
| 2544 | sage: a.norm() |
|---|
| 2545 | 1 |
|---|
| 2546 | sage: a.norm(L) |
|---|
| 2547 | 1 |
|---|
| 2548 | sage: a.norm(M) |
|---|
| 2549 | 1 |
|---|
| 2550 | sage: a |
|---|
| 2551 | a |
|---|
| 2552 | sage: (a+b+c).norm() |
|---|
| 2553 | 121 |
|---|
| 2554 | sage: (a+b+c).norm(L) |
|---|
| 2555 | 2*c*b - 7 |
|---|
| 2556 | sage: (a+b+c).norm(M) |
|---|
| 2557 | -11 |
|---|
| 2558 | |
|---|
| 2559 | We illustrate that norm is compatible with towers:: |
|---|
| 2560 | |
|---|
| 2561 | sage: z = (a+b+c).norm(L); z.norm(M) |
|---|
| 2562 | -11 |
|---|
| 2563 | |
|---|
| 2564 | If we are in an order, the norm is an integer:: |
|---|
| 2565 | |
|---|
| 2566 | sage: K.<a> = NumberField(x^3-2) |
|---|
| 2567 | sage: a.norm().parent() |
|---|
| 2568 | Rational Field |
|---|
| 2569 | sage: R = K.ring_of_integers() |
|---|
| 2570 | sage: R(a).norm().parent() |
|---|
| 2571 | Integer Ring |
|---|
| 2572 | |
|---|
| 2573 | TESTS:: |
|---|
| 2574 | |
|---|
| 2575 | sage: F.<z> = CyclotomicField(5) |
|---|
| 2576 | sage: t = 3*z**3 + 4*z**2 + 2 |
|---|
| 2577 | sage: t.norm(F) |
|---|
| 2578 | 3*z^3 + 4*z^2 + 2 |
|---|
| 2579 | """ |
|---|
| 2580 | if K is None: |
|---|
| 2581 | norm = self._pari_('x').norm() |
|---|
| 2582 | return QQ(norm) if self._parent.is_field() else ZZ(norm) |
|---|
| 2583 | return self.matrix(K).determinant() |
|---|
| 2584 | |
|---|
| 2585 | def vector(self): |
|---|
| 2586 | """ |
|---|
| 2587 | Return vector representation of self in terms of the basis for the |
|---|
| 2588 | ambient number field. |
|---|
| 2589 | |
|---|
| 2590 | EXAMPLES:: |
|---|
| 2591 | |
|---|
| 2592 | sage: K.<a> = NumberField(x^2 + 1) |
|---|
| 2593 | sage: (2/3*a - 5/6).vector() |
|---|
| 2594 | (-5/6, 2/3) |
|---|
| 2595 | sage: (-5/6, 2/3) |
|---|
| 2596 | (-5/6, 2/3) |
|---|
| 2597 | sage: O = K.order(2*a) |
|---|
| 2598 | sage: (O.1).vector() |
|---|
| 2599 | (0, 2) |
|---|
| 2600 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
|---|
| 2601 | sage: (a + b).vector() |
|---|
| 2602 | (b, 1) |
|---|
| 2603 | sage: O = K.order([a,b]) |
|---|
| 2604 | sage: (O.1).vector() |
|---|
| 2605 | (-b, 1) |
|---|
| 2606 | sage: (O.2).vector() |
|---|
| 2607 | (1, -b) |
|---|
| 2608 | """ |
|---|
| 2609 | return self.number_field().relative_vector_space()[2](self) |
|---|
| 2610 | |
|---|
| 2611 | def charpoly(self, var='x'): |
|---|
| 2612 | r""" |
|---|
| 2613 | Return the characteristic polynomial of this number field element. |
|---|
| 2614 | |
|---|
| 2615 | EXAMPLE:: |
|---|
| 2616 | |
|---|
| 2617 | sage: K.<a> = NumberField(x^3 + 7) |
|---|
| 2618 | sage: a.charpoly() |
|---|
| 2619 | x^3 + 7 |
|---|
| 2620 | sage: K(1).charpoly() |
|---|
| 2621 | x^3 - 3*x^2 + 3*x - 1 |
|---|
| 2622 | """ |
|---|
| 2623 | raise NotImplementedError, "Subclasses of NumberFieldElement must override charpoly()" |
|---|
| 2624 | |
|---|
| 2625 | def minpoly(self, var='x'): |
|---|
| 2626 | """ |
|---|
| 2627 | Return the minimal polynomial of this number field element. |
|---|
| 2628 | |
|---|
| 2629 | EXAMPLES:: |
|---|
| 2630 | |
|---|
| 2631 | sage: K.<a> = NumberField(x^2+3) |
|---|
| 2632 | sage: a.minpoly('x') |
|---|
| 2633 | x^2 + 3 |
|---|
| 2634 | sage: R.<X> = K['X'] |
|---|
| 2635 | sage: L.<b> = K.extension(X^2-(22 + a)) |
|---|
| 2636 | sage: b.minpoly('t') |
|---|
| 2637 | t^2 - a - 22 |
|---|
| 2638 | sage: b.absolute_minpoly('t') |
|---|
| 2639 | t^4 - 44*t^2 + 487 |
|---|
| 2640 | sage: b^2 - (22+a) |
|---|
| 2641 | 0 |
|---|
| 2642 | """ |
|---|
| 2643 | return self.charpoly(var).radical() # square free part of charpoly |
|---|
| 2644 | |
|---|
| 2645 | def is_integral(self): |
|---|
| 2646 | r""" |
|---|
| 2647 | Determine if a number is in the ring of integers of this number |
|---|
| 2648 | field. |
|---|
| 2649 | |
|---|
| 2650 | EXAMPLES:: |
|---|
| 2651 | |
|---|
| 2652 | sage: K.<a> = NumberField(x^2 + 23) |
|---|
| 2653 | sage: a.is_integral() |
|---|
| 2654 | True |
|---|
| 2655 | sage: t = (1+a)/2 |
|---|
| 2656 | sage: t.is_integral() |
|---|
| 2657 | True |
|---|
| 2658 | sage: t.minpoly() |
|---|
| 2659 | x^2 - x + 6 |
|---|
| 2660 | sage: t = a/2 |
|---|
| 2661 | sage: t.is_integral() |
|---|
| 2662 | False |
|---|
| 2663 | sage: t.minpoly() |
|---|
| 2664 | x^2 + 23/4 |
|---|
| 2665 | |
|---|
| 2666 | An example in a relative extension:: |
|---|
| 2667 | |
|---|
| 2668 | sage: K.<a,b> = NumberField([x^2+1, x^2+3]) |
|---|
| 2669 | sage: (a+b).is_integral() |
|---|
| 2670 | True |
|---|
| 2671 | sage: ((a-b)/2).is_integral() |
|---|
| 2672 | False |
|---|
| 2673 | """ |
|---|
| 2674 | return all([a in ZZ for a in self.absolute_minpoly()]) |
|---|
| 2675 | |
|---|
| 2676 | def matrix(self, base=None): |
|---|
| 2677 | r""" |
|---|
| 2678 | If base is None, return the matrix of right multiplication by the |
|---|
| 2679 | element on the power basis `1, x, x^2, \ldots, x^{d-1}` for |
|---|
| 2680 | the number field. Thus the *rows* of this matrix give the images of |
|---|
| 2681 | each of the `x^i`. |
|---|
| 2682 | |
|---|
| 2683 | If base is not None, then base must be either a field that embeds |
|---|
| 2684 | in the parent of self or a morphism to the parent of self, in which |
|---|
| 2685 | case this function returns the matrix of multiplication by self on |
|---|
| 2686 | the power basis, where we view the parent field as a field over |
|---|
| 2687 | base. |
|---|
| 2688 | |
|---|
| 2689 | Specifying base as the base field over which the parent of self |
|---|
| 2690 | is a relative extension is equivalent to base being None |
|---|
| 2691 | |
|---|
| 2692 | INPUT: |
|---|
| 2693 | |
|---|
| 2694 | |
|---|
| 2695 | - ``base`` - field or morphism |
|---|
| 2696 | |
|---|
| 2697 | |
|---|
| 2698 | EXAMPLES: |
|---|
| 2699 | |
|---|
| 2700 | Regular number field:: |
|---|
| 2701 | |
|---|
| 2702 | sage: K.<a> = NumberField(QQ['x'].0^3 - 5) |
|---|
| 2703 | sage: M = a.matrix(); M |
|---|
| 2704 | [0 1 0] |
|---|
| 2705 | [0 0 1] |
|---|
| 2706 | [5 0 0] |
|---|
| 2707 | sage: M.base_ring() is QQ |
|---|
| 2708 | True |
|---|
| 2709 | |
|---|
| 2710 | Relative number field:: |
|---|
| 2711 | |
|---|
| 2712 | sage: L.<b> = K.extension(K['x'].0^2 - 2) |
|---|
| 2713 | sage: M = b.matrix(); M |
|---|
| 2714 | [0 1] |
|---|
| 2715 | [2 0] |
|---|
| 2716 | sage: M.base_ring() is K |
|---|
| 2717 | True |
|---|
| 2718 | |
|---|
| 2719 | Absolute number field:: |
|---|
| 2720 | |
|---|
| 2721 | sage: M = L.absolute_field('c').gen().matrix(); M |
|---|
| 2722 | [ 0 1 0 0 0 0] |
|---|
| 2723 | [ 0 0 1 0 0 0] |
|---|
| 2724 | [ 0 0 0 1 0 0] |
|---|
| 2725 | [ 0 0 0 0 1 0] |
|---|
| 2726 | [ 0 0 0 0 0 1] |
|---|
| 2727 | [-17 -60 -12 -10 6 0] |
|---|
| 2728 | sage: M.base_ring() is QQ |
|---|
| 2729 | True |
|---|
| 2730 | |
|---|
| 2731 | More complicated relative number field:: |
|---|
| 2732 | |
|---|
| 2733 | sage: L.<b> = K.extension(K['x'].0^2 - a); L |
|---|
| 2734 | Number Field in b with defining polynomial x^2 - a over its base field |
|---|
| 2735 | sage: M = b.matrix(); M |
|---|
| 2736 | [0 1] |
|---|
| 2737 | [a 0] |
|---|
| 2738 | sage: M.base_ring() is K |
|---|
| 2739 | True |
|---|
| 2740 | |
|---|
| 2741 | An example where we explicitly give the subfield or the embedding:: |
|---|
| 2742 | |
|---|
| 2743 | sage: K.<a> = NumberField(x^4 + 1); L.<a2> = NumberField(x^2 + 1) |
|---|
| 2744 | sage: a.matrix(L) |
|---|
| 2745 | [ 0 1] |
|---|
| 2746 | [a2 0] |
|---|
| 2747 | |
|---|
| 2748 | Notice that if we compute all embeddings and choose a different |
|---|
| 2749 | one, then the matrix is changed as it should be:: |
|---|
| 2750 | |
|---|
| 2751 | sage: v = L.embeddings(K) |
|---|
| 2752 | sage: a.matrix(v[1]) |
|---|
| 2753 | [ 0 1] |
|---|
| 2754 | [-a2 0] |
|---|
| 2755 | |
|---|
| 2756 | The norm is also changed:: |
|---|
| 2757 | |
|---|
| 2758 | sage: a.norm(v[1]) |
|---|
| 2759 | a2 |
|---|
| 2760 | sage: a.norm(v[0]) |
|---|
| 2761 | -a2 |
|---|
| 2762 | |
|---|
| 2763 | TESTS:: |
|---|
| 2764 | |
|---|
| 2765 | sage: F.<z> = CyclotomicField(5) ; t = 3*z**3 + 4*z**2 + 2 |
|---|
| 2766 | sage: t.matrix(F) |
|---|
| 2767 | [3*z^3 + 4*z^2 + 2] |
|---|
| 2768 | sage: x=QQ['x'].gen() |
|---|
| 2769 | sage: K.<v>=NumberField(x^4 + 514*x^2 + 64321) |
|---|
| 2770 | sage: R.<r>=NumberField(x^2 + 4*v*x + 5*v^2 + 514) |
|---|
| 2771 | sage: r.matrix() |
|---|
| 2772 | [ 0 1] |
|---|
| 2773 | [-5*v^2 - 514 -4*v] |
|---|
| 2774 | sage: r.matrix(K) |
|---|
| 2775 | [ 0 1] |
|---|
| 2776 | [-5*v^2 - 514 -4*v] |
|---|
| 2777 | sage: r.matrix(R) |
|---|
| 2778 | [r] |
|---|
| 2779 | sage: foo=R.random_element() |
|---|
| 2780 | sage: foo.matrix(R) == matrix(1,1,[foo]) |
|---|
| 2781 | True |
|---|
| 2782 | """ |
|---|
| 2783 | from sage.matrix.constructor import matrix |
|---|
| 2784 | if base is self.parent(): |
|---|
| 2785 | return matrix(1,1,[self]) |
|---|
| 2786 | if base is not None and base is not self.base_ring(): |
|---|
| 2787 | if number_field.is_NumberField(base): |
|---|
| 2788 | return self._matrix_over_base(base) |
|---|
| 2789 | else: |
|---|
| 2790 | return self._matrix_over_base_morphism(base) |
|---|
| 2791 | # Multiply each power of field generator on |
|---|
| 2792 | # the left by this element; make matrix |
|---|
| 2793 | # whose rows are the coefficients of the result, |
|---|
| 2794 | # and transpose. |
|---|
| 2795 | if self.__matrix is None: |
|---|
| 2796 | K = self.number_field() |
|---|
| 2797 | v = [] |
|---|
| 2798 | x = K.gen() |
|---|
| 2799 | a = K(1) |
|---|
| 2800 | d = K.relative_degree() |
|---|
| 2801 | for n in range(d): |
|---|
| 2802 | v += (a*self).list() |
|---|
| 2803 | a *= x |
|---|
| 2804 | k = K.base_ring() |
|---|
| 2805 | import sage.matrix.matrix_space |
|---|
| 2806 | M = sage.matrix.matrix_space.MatrixSpace(k, d) |
|---|
| 2807 | self.__matrix = M(v) |
|---|
| 2808 | return self.__matrix |
|---|
| 2809 | |
|---|
| 2810 | def valuation(self, P): |
|---|
| 2811 | """ |
|---|
| 2812 | Returns the valuation of self at a given prime ideal P. |
|---|
| 2813 | |
|---|
| 2814 | INPUT: |
|---|
| 2815 | |
|---|
| 2816 | |
|---|
| 2817 | - ``P`` - a prime ideal of the parent of self |
|---|
| 2818 | |
|---|
| 2819 | |
|---|
| 2820 | .. note:: |
|---|
| 2821 | |
|---|
| 2822 | The function ``ord()`` is an alias for ``valuation()``. |
|---|
| 2823 | |
|---|
| 2824 | EXAMPLES:: |
|---|
| 2825 | |
|---|
| 2826 | sage: R.<x> = QQ[] |
|---|
| 2827 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
|---|
| 2828 | sage: P = K.ideal(61).factor()[0][0] |
|---|
| 2829 | sage: b = a^2 + 30 |
|---|
| 2830 | sage: b.valuation(P) |
|---|
| 2831 | 1 |
|---|
| 2832 | sage: b.ord(P) |
|---|
| 2833 | 1 |
|---|
| 2834 | sage: type(b.valuation(P)) |
|---|
| 2835 | <type 'sage.rings.integer.Integer'> |
|---|
| 2836 | |
|---|
| 2837 | The function can be applied to elements in relative number fields:: |
|---|
| 2838 | |
|---|
| 2839 | sage: L.<b> = K.extension(x^2 - 3) |
|---|
| 2840 | sage: [L(6).valuation(P) for P in L.primes_above(2)] |
|---|
| 2841 | [4] |
|---|
| 2842 | sage: [L(6).valuation(P) for P in L.primes_above(3)] |
|---|
| 2843 | [2, 2] |
|---|
| 2844 | """ |
|---|
| 2845 | from number_field_ideal import is_NumberFieldIdeal |
|---|
| 2846 | from sage.rings.infinity import infinity |
|---|
| 2847 | if not is_NumberFieldIdeal(P): |
|---|
| 2848 | if is_NumberFieldElement(P): |
|---|
| 2849 | P = self.number_field().fractional_ideal(P) |
|---|
| 2850 | else: |
|---|
| 2851 | raise TypeError, "P must be an ideal" |
|---|
| 2852 | if not P.is_prime(): |
|---|
| 2853 | raise ValueError, "P must be prime" |
|---|
| 2854 | if self == 0: |
|---|
| 2855 | return infinity |
|---|
| 2856 | return Integer_sage(self.number_field().pari_nf().elementval(self._pari_(), P.pari_prime())) |
|---|
| 2857 | |
|---|
| 2858 | ord = valuation |
|---|
| 2859 | |
|---|
| 2860 | def local_height(self, P, prec=None, weighted=False): |
|---|
| 2861 | r""" |
|---|
| 2862 | Returns the local height of self at a given prime ideal `P`. |
|---|
| 2863 | |
|---|
| 2864 | INPUT: |
|---|
| 2865 | |
|---|
| 2866 | |
|---|
| 2867 | - ``P`` - a prime ideal of the parent of self |
|---|
| 2868 | |
|---|
| 2869 | - ``prec`` (int) -- desired floating point precision (defult: |
|---|
| 2870 | default RealField precision). |
|---|
| 2871 | |
|---|
| 2872 | - ``weighted`` (bool, default False) -- if True, apply local |
|---|
| 2873 | degree weighting. |
|---|
| 2874 | |
|---|
| 2875 | OUTPUT: |
|---|
| 2876 | |
|---|
| 2877 | (real) The local height of this number field element at the |
|---|
| 2878 | place `P`. If ``weighted`` is True, this is multiplied by the |
|---|
| 2879 | local degree (as required for global heights). |
|---|
| 2880 | |
|---|
| 2881 | EXAMPLES:: |
|---|
| 2882 | |
|---|
| 2883 | sage: R.<x> = QQ[] |
|---|
| 2884 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
|---|
| 2885 | sage: P = K.ideal(61).factor()[0][0] |
|---|
| 2886 | sage: b = 1/(a^2 + 30) |
|---|
| 2887 | sage: b.local_height(P) |
|---|
| 2888 | 4.11087386417331 |
|---|
| 2889 | sage: b.local_height(P, weighted=True) |
|---|
| 2890 | 8.22174772834662 |
|---|
| 2891 | sage: b.local_height(P, 200) |
|---|
| 2892 | 4.1108738641733112487513891034256147463156817430812610629374 |
|---|
| 2893 | sage: (b^2).local_height(P) |
|---|
| 2894 | 8.22174772834662 |
|---|
| 2895 | sage: (b^-1).local_height(P) |
|---|
| 2896 | 0.000000000000000 |
|---|
| 2897 | |
|---|
| 2898 | A relative example:: |
|---|
| 2899 | |
|---|
| 2900 | sage: PK.<y> = K[] |
|---|
| 2901 | sage: L.<c> = NumberField(y^2 + a) |
|---|
| 2902 | sage: L(1/4).local_height(L.ideal(2, c-a+1)) |
|---|
| 2903 | 1.38629436111989 |
|---|
| 2904 | """ |
|---|
| 2905 | if self.valuation(P) >= 0: ## includes the case self=0 |
|---|
| 2906 | from sage.rings.real_mpfr import RealField |
|---|
| 2907 | if prec is None: |
|---|
| 2908 | return RealField().zero_element() |
|---|
| 2909 | else: |
|---|
| 2910 | return RealField(prec).zero_element() |
|---|
| 2911 | ht = self.abs_non_arch(P,prec).log() |
|---|
| 2912 | if not weighted: |
|---|
| 2913 | return ht |
|---|
| 2914 | nP = P.residue_class_degree()*P.absolute_ramification_index() |
|---|
| 2915 | return nP*ht |
|---|
| 2916 | |
|---|
| 2917 | def local_height_arch(self, i, prec=None, weighted=False): |
|---|
| 2918 | r""" |
|---|
| 2919 | Returns the local height of self at the `i`'th infinite place. |
|---|
| 2920 | |
|---|
| 2921 | INPUT: |
|---|
| 2922 | |
|---|
| 2923 | |
|---|
| 2924 | - ``i`` (int) - an integer in ``range(r+s)`` where `(r,s)` is the |
|---|
| 2925 | signature of the parent field (so `n=r+2s` is the degree). |
|---|
| 2926 | |
|---|
| 2927 | - ``prec`` (int) -- desired floating point precision (default: |
|---|
| 2928 | default RealField precision). |
|---|
| 2929 | |
|---|
| 2930 | - ``weighted`` (bool, default False) -- if True, apply local |
|---|
| 2931 | degree weighting, i.e. double the value for complex places. |
|---|
| 2932 | |
|---|
| 2933 | OUTPUT: |
|---|
| 2934 | |
|---|
| 2935 | (real) The archimedean local height of this number field |
|---|
| 2936 | element at the `i`'th infinite place. If ``weighted`` is |
|---|
| 2937 | True, this is multiplied by the local degree (as required for |
|---|
| 2938 | global heights), i.e. 1 for real places and 2 for complex |
|---|
| 2939 | places. |
|---|
| 2940 | |
|---|
| 2941 | EXAMPLES:: |
|---|
| 2942 | |
|---|
| 2943 | sage: R.<x> = QQ[] |
|---|
| 2944 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
|---|
| 2945 | sage: [p.codomain() for p in K.places()] |
|---|
| 2946 | [Real Field with 106 bits of precision, |
|---|
| 2947 | Real Field with 106 bits of precision, |
|---|
| 2948 | Complex Field with 53 bits of precision] |
|---|
| 2949 | sage: [a.local_height_arch(i) for i in range(3)] |
|---|
| 2950 | [0.5301924545717755083366563897519, |
|---|
| 2951 | 0.5301924545717755083366563897519, |
|---|
| 2952 | 0.886414217456333] |
|---|
| 2953 | sage: [a.local_height_arch(i, weighted=True) for i in range(3)] |
|---|
| 2954 | [0.5301924545717755083366563897519, |
|---|
| 2955 | 0.5301924545717755083366563897519, |
|---|
| 2956 | 1.77282843491267] |
|---|
| 2957 | |
|---|
| 2958 | A relative example:: |
|---|
| 2959 | |
|---|
| 2960 | sage: L.<b, c> = NumberFieldTower([x^2 - 5, x^3 + x + 3]) |
|---|
| 2961 | sage: [(b + c).local_height_arch(i) for i in range(4)] |
|---|
| 2962 | [1.238223390757884911842206617439, |
|---|
| 2963 | 0.02240347229957875780769746914391, |
|---|
| 2964 | 0.780028961749618, |
|---|
| 2965 | 1.16048938497298] |
|---|
| 2966 | """ |
|---|
| 2967 | K = self.number_field() |
|---|
| 2968 | emb = K.places(prec=prec)[i] |
|---|
| 2969 | a = emb(self).abs() |
|---|
| 2970 | Kv = emb.codomain() |
|---|
| 2971 | if a <= Kv.one_element(): |
|---|
| 2972 | return Kv.zero_element() |
|---|
| 2973 | ht = a.log() |
|---|
| 2974 | from sage.rings.real_mpfr import is_RealField |
|---|
| 2975 | if weighted and not is_RealField(Kv): |
|---|
| 2976 | ht*=2 |
|---|
| 2977 | return ht |
|---|
| 2978 | |
|---|
| 2979 | def global_height_non_arch(self, prec=None): |
|---|
| 2980 | """ |
|---|
| 2981 | Returns the total non-archimedean component of the height of self. |
|---|
| 2982 | |
|---|
| 2983 | INPUT: |
|---|
| 2984 | |
|---|
| 2985 | - ``prec`` (int) -- desired floating point precision (default: |
|---|
| 2986 | default RealField precision). |
|---|
| 2987 | |
|---|
| 2988 | OUTPUT: |
|---|
| 2989 | |
|---|
| 2990 | (real) The total non-archimedean component of the height of |
|---|
| 2991 | this number field element; that is, the sum of the local |
|---|
| 2992 | heights at all finite places, weighted by the local degrees. |
|---|
| 2993 | |
|---|
| 2994 | ALGORITHM: |
|---|
| 2995 | |
|---|
| 2996 | An alternative formula is `\log(d)` where `d` is the norm of |
|---|
| 2997 | the denominator ideal; this is used to avoid factorization. |
|---|
| 2998 | |
|---|
| 2999 | EXAMPLES:: |
|---|
| 3000 | |
|---|
| 3001 | sage: R.<x> = QQ[] |
|---|
| 3002 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
|---|
| 3003 | sage: b = a/6 |
|---|
| 3004 | sage: b.global_height_non_arch() |
|---|
| 3005 | 7.16703787691222 |
|---|
| 3006 | |
|---|
| 3007 | Check that this is equal to the sum of the non-archimedean |
|---|
| 3008 | local heights:: |
|---|
| 3009 | |
|---|
| 3010 | sage: [b.local_height(P) for P in b.support()] |
|---|
| 3011 | [0.000000000000000, 0.693147180559945, 1.09861228866811, 1.09861228866811] |
|---|
| 3012 | sage: [b.local_height(P, weighted=True) for P in b.support()] |
|---|
| 3013 | [0.000000000000000, 2.77258872223978, 2.19722457733622, 2.19722457733622] |
|---|
| 3014 | sage: sum([b.local_height(P,weighted=True) for P in b.support()]) |
|---|
| 3015 | 7.16703787691222 |
|---|
| 3016 | |
|---|
| 3017 | A relative example:: |
|---|
| 3018 | |
|---|
| 3019 | sage: PK.<y> = K[] |
|---|
| 3020 | sage: L.<c> = NumberField(y^2 + a) |
|---|
| 3021 | sage: (c/10).global_height_non_arch() |
|---|
| 3022 | 18.4206807439524 |
|---|
| 3023 | """ |
|---|
| 3024 | from sage.rings.real_mpfr import RealField |
|---|
| 3025 | if prec is None: |
|---|
| 3026 | R = RealField() |
|---|
| 3027 | else: |
|---|
| 3028 | R = RealField(prec) |
|---|
| 3029 | if self.is_zero(): |
|---|
| 3030 | return R.zero_element() |
|---|
| 3031 | return R(self.denominator_ideal().absolute_norm()).log() |
|---|
| 3032 | |
|---|
| 3033 | def global_height_arch(self, prec=None): |
|---|
| 3034 | """ |
|---|
| 3035 | Returns the total archimedean component of the height of self. |
|---|
| 3036 | |
|---|
| 3037 | INPUT: |
|---|
| 3038 | |
|---|
| 3039 | - ``prec`` (int) -- desired floating point precision (defult: |
|---|
| 3040 | default RealField precision). |
|---|
| 3041 | |
|---|
| 3042 | OUTPUT: |
|---|
| 3043 | |
|---|
| 3044 | (real) The total archimedean component of the height of |
|---|
| 3045 | this number field element; that is, the sum of the local |
|---|
| 3046 | heights at all infinite places. |
|---|
| 3047 | |
|---|
| 3048 | EXAMPLES:: |
|---|
| 3049 | |
|---|
| 3050 | sage: R.<x> = QQ[] |
|---|
| 3051 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
|---|
| 3052 | sage: b = a/2 |
|---|
| 3053 | sage: b.global_height_arch() |
|---|
| 3054 | 0.38653407379277... |
|---|
| 3055 | """ |
|---|
| 3056 | r,s = self.number_field().signature() |
|---|
| 3057 | hts = [self.local_height_arch(i, prec, weighted=True) for i in range(r+s)] |
|---|
| 3058 | return sum(hts, hts[0].parent().zero_element()) |
|---|
| 3059 | |
|---|
| 3060 | def global_height(self, prec=None): |
|---|
| 3061 | """ |
|---|
| 3062 | Returns the absolute logarithmic height of this number field element. |
|---|
| 3063 | |
|---|
| 3064 | INPUT: |
|---|
| 3065 | |
|---|
| 3066 | - ``prec`` (int) -- desired floating point precision (defult: |
|---|
| 3067 | default RealField precision). |
|---|
| 3068 | |
|---|
| 3069 | OUTPUT: |
|---|
| 3070 | |
|---|
| 3071 | (real) The absolute logarithmic height of this number field |
|---|
| 3072 | element; that is, the sum of the local heights at all finite |
|---|
| 3073 | and infinite places, scaled by the degree to make the result independent of |
|---|
| 3074 | the parent field. |
|---|
| 3075 | |
|---|
| 3076 | EXAMPLES:: |
|---|
| 3077 | |
|---|
| 3078 | sage: R.<x> = QQ[] |
|---|
| 3079 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
|---|
| 3080 | sage: b = a/2 |
|---|
| 3081 | sage: b.global_height() |
|---|
| 3082 | 0.789780699008... |
|---|
| 3083 | sage: b.global_height(prec=200) |
|---|
| 3084 | 0.78978069900813892060267152032141577237037181070060784564457 |
|---|
| 3085 | |
|---|
| 3086 | The global height of an algebraic number is absolute, i.e. it |
|---|
| 3087 | does not depend on the parent field:: |
|---|
| 3088 | |
|---|
| 3089 | sage: QQ(6).global_height() |
|---|
| 3090 | 1.79175946922805 |
|---|
| 3091 | sage: K(6).global_height() |
|---|
| 3092 | 1.79175946922805 |
|---|
| 3093 | |
|---|
| 3094 | sage: L.<b> = NumberField((a^2).minpoly()) |
|---|
| 3095 | sage: L.degree() |
|---|
| 3096 | 2 |
|---|
| 3097 | sage: b.global_height() # element of L (degree 2 field) |
|---|
| 3098 | 1.41660667202811 |
|---|
| 3099 | sage: (a^2).global_height() # element of K (degree 4 field) |
|---|
| 3100 | 1.41660667202811 |
|---|
| 3101 | |
|---|
| 3102 | And of course every element has the same height as it's inverse:: |
|---|
| 3103 | |
|---|
| 3104 | sage: K.<s> = QuadraticField(2) |
|---|
| 3105 | sage: s.global_height() |
|---|
| 3106 | 0.346573590279973 |
|---|
| 3107 | sage: (1/s).global_height() #make sure that 11758 is fixed |
|---|
| 3108 | 0.346573590279973 |
|---|
| 3109 | |
|---|
| 3110 | """ |
|---|
| 3111 | return (self.global_height_non_arch(prec)+self.global_height_arch(prec))/self.number_field().absolute_degree() |
|---|
| 3112 | |
|---|
| 3113 | def numerator_ideal(self): |
|---|
| 3114 | """ |
|---|
| 3115 | Return the numerator ideal of this number field element. |
|---|
| 3116 | |
|---|
| 3117 | .. note:: |
|---|
| 3118 | |
|---|
| 3119 | A ValueError will be raised if this function is called on |
|---|
| 3120 | 0. |
|---|
| 3121 | |
|---|
| 3122 | .. seealso:: |
|---|
| 3123 | |
|---|
| 3124 | :meth:`~denominator_ideal` |
|---|
| 3125 | |
|---|
| 3126 | OUTPUT: |
|---|
| 3127 | |
|---|
| 3128 | (integral ideal) The numerator ideal `N` of this element, |
|---|
| 3129 | where for a non-zero number field element `a`, the principal |
|---|
| 3130 | ideal generated by `a` has the form `N/D` where `N` and `D` |
|---|
| 3131 | are coprime integral ideals. An error is raised if the |
|---|
| 3132 | element is zero. |
|---|
| 3133 | |
|---|
| 3134 | EXAMPLES:: |
|---|
| 3135 | |
|---|
| 3136 | sage: K.<a> = NumberField(x^2+5) |
|---|
| 3137 | sage: b = (1+a)/2 |
|---|
| 3138 | sage: b.norm() |
|---|
| 3139 | 3/2 |
|---|
| 3140 | sage: N = b.numerator_ideal(); N |
|---|
| 3141 | Fractional ideal (3, a + 1) |
|---|
| 3142 | sage: N.norm() |
|---|
| 3143 | 3 |
|---|
| 3144 | sage: (1/b).numerator_ideal() |
|---|
| 3145 | Fractional ideal (2, a + 1) |
|---|
| 3146 | |
|---|
| 3147 | TESTS: |
|---|
| 3148 | |
|---|
| 3149 | Undefined for 0:: |
|---|
| 3150 | |
|---|
| 3151 | sage: K(0).numerator_ideal() |
|---|
| 3152 | Traceback (most recent call last): |
|---|
| 3153 | ... |
|---|
| 3154 | ValueError: numerator ideal of 0 is not defined. |
|---|
| 3155 | """ |
|---|
| 3156 | if self.is_zero(): |
|---|
| 3157 | raise ValueError, "numerator ideal of 0 is not defined." |
|---|
| 3158 | return self.number_field().ideal(self).numerator() |
|---|
| 3159 | |
|---|
| 3160 | def denominator_ideal(self): |
|---|
| 3161 | """ |
|---|
| 3162 | Return the denominator ideal of this number field element. |
|---|
| 3163 | |
|---|
| 3164 | .. note:: |
|---|
| 3165 | |
|---|
| 3166 | A ValueError will be raised if this function is called on |
|---|
| 3167 | 0. |
|---|
| 3168 | |
|---|
| 3169 | .. seealso:: |
|---|
| 3170 | |
|---|
| 3171 | :meth:`~numerator_ideal` |
|---|
| 3172 | |
|---|
| 3173 | OUTPUT: |
|---|
| 3174 | |
|---|
| 3175 | (integral ideal) The denominator ideal `D` of this element, |
|---|
| 3176 | where for a non-zero number field element `a`, the principal |
|---|
| 3177 | ideal generated by `a` has the form `N/D` where `N` and `D` |
|---|
| 3178 | are coprime integral ideals. An error is raised if the |
|---|
| 3179 | element is zero. |
|---|
| 3180 | |
|---|
| 3181 | EXAMPLES:: |
|---|
| 3182 | |
|---|
| 3183 | sage: K.<a> = NumberField(x^2+5) |
|---|
| 3184 | sage: b = (1+a)/2 |
|---|
| 3185 | sage: b.norm() |
|---|
| 3186 | 3/2 |
|---|
| 3187 | sage: D = b.denominator_ideal(); D |
|---|
| 3188 | Fractional ideal (2, a + 1) |
|---|
| 3189 | sage: D.norm() |
|---|
| 3190 | 2 |
|---|
| 3191 | sage: (1/b).denominator_ideal() |
|---|
| 3192 | Fractional ideal (3, a + 1) |
|---|
| 3193 | |
|---|
| 3194 | TESTS: |
|---|
| 3195 | |
|---|
| 3196 | Undefined for 0:: |
|---|
| 3197 | |
|---|
| 3198 | sage: K(0).denominator_ideal() |
|---|
| 3199 | Traceback (most recent call last): |
|---|
| 3200 | ... |
|---|
| 3201 | ValueError: denominator ideal of 0 is not defined. |
|---|
| 3202 | """ |
|---|
| 3203 | if self.is_zero(): |
|---|
| 3204 | raise ValueError, "denominator ideal of 0 is not defined." |
|---|
| 3205 | return self.number_field().ideal(self).denominator() |
|---|
| 3206 | |
|---|
| 3207 | def support(self): |
|---|
| 3208 | """ |
|---|
| 3209 | Return the support of this number field element. |
|---|
| 3210 | |
|---|
| 3211 | OUTPUT: A sorted list of the primes ideals at which this number |
|---|
| 3212 | field element has nonzero valuation. An error is raised if the |
|---|
| 3213 | element is zero. |
|---|
| 3214 | |
|---|
| 3215 | EXAMPLES:: |
|---|
| 3216 | |
|---|
| 3217 | sage: x = ZZ['x'].gen() |
|---|
| 3218 | sage: F.<t> = NumberField(x^3 - 2) |
|---|
| 3219 | |
|---|
| 3220 | :: |
|---|
| 3221 | |
|---|
| 3222 | sage: P5s = F(5).support() |
|---|
| 3223 | sage: P5s |
|---|
| 3224 | [Fractional ideal (-t^2 - 1), Fractional ideal (t^2 - 2*t - 1)] |
|---|
| 3225 | sage: all(5 in P5 for P5 in P5s) |
|---|
| 3226 | True |
|---|
| 3227 | sage: all(P5.is_prime() for P5 in P5s) |
|---|
| 3228 | True |
|---|
| 3229 | sage: [ P5.norm() for P5 in P5s ] |
|---|
| 3230 | [5, 25] |
|---|
| 3231 | |
|---|
| 3232 | TESTS: |
|---|
| 3233 | |
|---|
| 3234 | It doesn't make sense to factor the ideal (0):: |
|---|
| 3235 | |
|---|
| 3236 | sage: F(0).support() |
|---|
| 3237 | Traceback (most recent call last): |
|---|
| 3238 | ... |
|---|
| 3239 | ArithmeticError: Support of 0 is not defined. |
|---|
| 3240 | """ |
|---|
| 3241 | if self.is_zero(): |
|---|
| 3242 | raise ArithmeticError, "Support of 0 is not defined." |
|---|
| 3243 | return self.number_field().primes_above(self) |
|---|
| 3244 | |
|---|
| 3245 | def _matrix_over_base(self, L): |
|---|
| 3246 | """ |
|---|
| 3247 | Return the matrix of self over the base field L. |
|---|
| 3248 | |
|---|
| 3249 | EXAMPLES:: |
|---|
| 3250 | |
|---|
| 3251 | sage: K.<a> = NumberField(ZZ['x'].0^3-2, 'a') |
|---|
| 3252 | sage: L.<b> = K.extension(ZZ['x'].0^2+3, 'b') |
|---|
| 3253 | sage: L(a)._matrix_over_base(K) == L(a).matrix() |
|---|
| 3254 | True |
|---|
| 3255 | """ |
|---|
| 3256 | K = self.number_field() |
|---|
| 3257 | E = L.embeddings(K) |
|---|
| 3258 | if len(E) == 0: |
|---|
| 3259 | raise ValueError, "no way to embed L into parent's base ring K" |
|---|
| 3260 | phi = E[0] |
|---|
| 3261 | return self._matrix_over_base_morphism(phi) |
|---|
| 3262 | |
|---|
| 3263 | def _matrix_over_base_morphism(self, phi): |
|---|
| 3264 | """ |
|---|
| 3265 | Return the matrix of self over a specified base, where phi gives a |
|---|
| 3266 | map from the specified base to self.parent(). |
|---|
| 3267 | |
|---|
| 3268 | EXAMPLES:: |
|---|
| 3269 | |
|---|
| 3270 | sage: F.<alpha> = NumberField(ZZ['x'].0^5-2) |
|---|
| 3271 | sage: h = Hom(QQ,F)([1]) |
|---|
| 3272 | sage: alpha._matrix_over_base_morphism(h) == alpha.matrix() |
|---|
| 3273 | True |
|---|
| 3274 | sage: alpha._matrix_over_base_morphism(h) == alpha.matrix(QQ) |
|---|
| 3275 | True |
|---|
| 3276 | """ |
|---|
| 3277 | L = phi.domain() |
|---|
| 3278 | |
|---|
| 3279 | ## the code below doesn't work if the morphism is |
|---|
| 3280 | ## over QQ, since QQ.primitive_element() doesn't |
|---|
| 3281 | ## make sense |
|---|
| 3282 | if L is QQ: |
|---|
| 3283 | K = phi.codomain() |
|---|
| 3284 | if K != self.number_field(): |
|---|
| 3285 | raise ValueError, "codomain of phi must be parent of self" |
|---|
| 3286 | ## the variable name is irrelevant below, because the |
|---|
| 3287 | ## matrix is over QQ |
|---|
| 3288 | F = K.absolute_field('alpha') |
|---|
| 3289 | from_f, to_F = F.structure() |
|---|
| 3290 | return to_F(self).matrix() |
|---|
| 3291 | |
|---|
| 3292 | alpha = L.primitive_element() |
|---|
| 3293 | beta = phi(alpha) |
|---|
| 3294 | K = phi.codomain() |
|---|
| 3295 | if K != self.number_field(): |
|---|
| 3296 | raise ValueError, "codomain of phi must be parent of self" |
|---|
| 3297 | |
|---|
| 3298 | # Construct a relative extension over L (= QQ(beta)) |
|---|
| 3299 | M = K.relativize(beta, ('a','b')) |
|---|
| 3300 | # variable name a is OK, since this is temporary |
|---|
| 3301 | |
|---|
| 3302 | # Carry self over to M. |
|---|
| 3303 | from_M, to_M = M.structure() |
|---|
| 3304 | try: |
|---|
| 3305 | z = to_M(self) |
|---|
| 3306 | except Exception: |
|---|
| 3307 | return to_M, self, K, beta |
|---|
| 3308 | |
|---|
| 3309 | # Compute the relative matrix of self, but in M |
|---|
| 3310 | R = z.matrix() |
|---|
| 3311 | |
|---|
| 3312 | # Map back to L. |
|---|
| 3313 | psi = M.base_field().hom([alpha]) |
|---|
| 3314 | return R.apply_morphism(psi) |
|---|
| 3315 | |
|---|
| 3316 | |
|---|
| 3317 | def list(self): |
|---|
| 3318 | """ |
|---|
| 3319 | Return the list of coefficients of self written in terms of a power |
|---|
| 3320 | basis. |
|---|
| 3321 | |
|---|
| 3322 | EXAMPLE:: |
|---|
| 3323 | |
|---|
| 3324 | sage: K.<a> = NumberField(x^3 - x + 2); ((a + 1)/(a + 2)).list() |
|---|
| 3325 | [1/4, 1/2, -1/4] |
|---|
| 3326 | sage: K.<a, b> = NumberField([x^3 - x + 2, x^2 + 23]); ((a + b)/(a + 2)).list() |
|---|
| 3327 | [3/4*b - 1/2, -1/2*b + 1, 1/4*b - 1/2] |
|---|
| 3328 | """ |
|---|
| 3329 | raise NotImplementedError |
|---|
| 3330 | |
|---|
| 3331 | def inverse_mod(self, I): |
|---|
| 3332 | """ |
|---|
| 3333 | Returns the inverse of self mod the integral ideal I. |
|---|
| 3334 | |
|---|
| 3335 | INPUT: |
|---|
| 3336 | |
|---|
| 3337 | - ``I`` - may be an ideal of self.parent(), or an element or list |
|---|
| 3338 | of elements of self.parent() generating a nonzero ideal. A ValueError |
|---|
| 3339 | is raised if I is non-integral or zero. A ZeroDivisionError is |
|---|
| 3340 | raised if I + (x) != (1). |
|---|
| 3341 | |
|---|
| 3342 | NOTE: It's not implemented yet for non-integral elements. |
|---|
| 3343 | |
|---|
| 3344 | EXAMPLES:: |
|---|
| 3345 | |
|---|
| 3346 | sage: k.<a> = NumberField(x^2 + 23) |
|---|
| 3347 | sage: N = k.ideal(3) |
|---|
| 3348 | sage: d = 3*a + 1 |
|---|
| 3349 | sage: d.inverse_mod(N) |
|---|
| 3350 | 1 |
|---|
| 3351 | |
|---|
| 3352 | :: |
|---|
| 3353 | |
|---|
| 3354 | sage: k.<a> = NumberField(x^3 + 11) |
|---|
| 3355 | sage: d = a + 13 |
|---|
| 3356 | sage: d.inverse_mod(a^2)*d - 1 in k.ideal(a^2) |
|---|
| 3357 | True |
|---|
| 3358 | sage: d.inverse_mod((5, a + 1))*d - 1 in k.ideal(5, a + 1) |
|---|
| 3359 | True |
|---|
| 3360 | sage: K.<b> = k.extension(x^2 + 3) |
|---|
| 3361 | sage: b.inverse_mod([37, a - b]) |
|---|
| 3362 | 7 |
|---|
| 3363 | sage: 7*b - 1 in K.ideal(37, a - b) |
|---|
| 3364 | True |
|---|
| 3365 | sage: b.inverse_mod([37, a - b]).parent() == K |
|---|
| 3366 | True |
|---|
| 3367 | """ |
|---|
| 3368 | R = self.number_field().ring_of_integers() |
|---|
| 3369 | try: |
|---|
| 3370 | return _inverse_mod_generic(R(self), I) |
|---|
| 3371 | except TypeError: # raised by failure of R(self) |
|---|
| 3372 | raise NotImplementedError, "inverse_mod is not implemented for non-integral elements" |
|---|
| 3373 | |
|---|
| 3374 | |
|---|
| 3375 | def residue_symbol(self, P, m, check=True): |
|---|
| 3376 | r""" |
|---|
| 3377 | The m-th power residue symbol for an element self and proper ideal P. |
|---|
| 3378 | |
|---|
| 3379 | .. math:: \left(\frac{\alpha}{\mathbf{P}}\right) \equiv \alpha^{\frac{N(\mathbf{P})-1}{m}} \operatorname{mod} \mathbf{P} |
|---|
| 3380 | |
|---|
| 3381 | .. note:: accepts m=1, in which case returns 1 |
|---|
| 3382 | |
|---|
| 3383 | .. note:: can also be called for an ideal from sage.rings.number_field_ideal.residue_symbol |
|---|
| 3384 | |
|---|
| 3385 | .. note:: self is coerced into the number field of the ideal P |
|---|
| 3386 | |
|---|
| 3387 | .. note:: if m=2, self is an integer, and P is an ideal of a number field of absolute degree 1 (i.e. it is a copy of the rationals), then this calls kronecker_symbol, which is implemented using GMP. |
|---|
| 3388 | |
|---|
| 3389 | INPUT: |
|---|
| 3390 | |
|---|
| 3391 | - ``P`` - proper ideal of the number field (or an extension) |
|---|
| 3392 | |
|---|
| 3393 | - ``m`` - positive integer |
|---|
| 3394 | |
|---|
| 3395 | OUTPUT: |
|---|
| 3396 | |
|---|
| 3397 | - an m-th root of unity in the number field |
|---|
| 3398 | |
|---|
| 3399 | EXAMPLES: |
|---|
| 3400 | |
|---|
| 3401 | Quadratic Residue (11 is not a square modulo 17):: |
|---|
| 3402 | |
|---|
| 3403 | sage: K.<a> = NumberField(x - 1) |
|---|
| 3404 | sage: K(11).residue_symbol(K.ideal(17),2) |
|---|
| 3405 | -1 |
|---|
| 3406 | sage: kronecker_symbol(11,17) |
|---|
| 3407 | -1 |
|---|
| 3408 | |
|---|
| 3409 | The result depends on the number field of the ideal:: |
|---|
| 3410 | |
|---|
| 3411 | sage: K.<a> = NumberField(x - 1) |
|---|
| 3412 | sage: L.<b> = K.extension(x^2 + 1) |
|---|
| 3413 | sage: K(7).residue_symbol(K.ideal(11),2) |
|---|
| 3414 | -1 |
|---|
| 3415 | sage: K(7).residue_symbol(L.ideal(11),2) |
|---|
| 3416 | 1 |
|---|
| 3417 | |
|---|
| 3418 | Cubic Residue:: |
|---|
| 3419 | |
|---|
| 3420 | sage: K.<w> = NumberField(x^2 - x + 1) |
|---|
| 3421 | sage: (w^2 + 3).residue_symbol(K.ideal(17),3) |
|---|
| 3422 | -w |
|---|
| 3423 | |
|---|
| 3424 | The field must contain the m-th roots of unity:: |
|---|
| 3425 | |
|---|
| 3426 | sage: K.<w> = NumberField(x^2 - x + 1) |
|---|
| 3427 | sage: (w^2 + 3).residue_symbol(K.ideal(17),5) |
|---|
| 3428 | Traceback (most recent call last): |
|---|
| 3429 | ... |
|---|
| 3430 | ValueError: The residue symbol to that power is not defined for the number field |
|---|
| 3431 | |
|---|
| 3432 | """ |
|---|
| 3433 | return P.residue_symbol(self,m,check) |
|---|
| 3434 | |
|---|
| 3435 | |
|---|
| 3436 | |
|---|
| 3437 | cdef class NumberFieldElement_absolute(NumberFieldElement): |
|---|
| 3438 | |
|---|
| 3439 | def _magma_init_(self, magma): |
|---|
| 3440 | """ |
|---|
| 3441 | Return Magma version of this number field element. |
|---|
| 3442 | |
|---|
| 3443 | INPUT: |
|---|
| 3444 | |
|---|
| 3445 | |
|---|
| 3446 | - ``magma`` - a Magma interpreter |
|---|
| 3447 | |
|---|
| 3448 | |
|---|
| 3449 | OUTPUT: MagmaElement that has parent the Magma object corresponding |
|---|
| 3450 | to the parent number field. |
|---|
| 3451 | |
|---|
| 3452 | EXAMPLES:: |
|---|
| 3453 | |
|---|
| 3454 | sage: K.<a> = NumberField(x^3 + 2) |
|---|
| 3455 | sage: a._magma_init_(magma) # optional - magma |
|---|
| 3456 | '(_sage_[...]![0, 1, 0])' |
|---|
| 3457 | sage: m = magma((2/3)*a^2 - 17/3); m # optional - magma |
|---|
| 3458 | 1/3*(2*a^2 - 17) |
|---|
| 3459 | sage: m.sage() # optional - magma |
|---|
| 3460 | 2/3*a^2 - 17/3 |
|---|
| 3461 | |
|---|
| 3462 | An element of a cyclotomic field. |
|---|
| 3463 | |
|---|
| 3464 | :: |
|---|
| 3465 | |
|---|
| 3466 | sage: K = CyclotomicField(9) |
|---|
| 3467 | sage: K.gen() |
|---|
| 3468 | zeta9 |
|---|
| 3469 | sage: K.gen()._magma_init_(magma) # optional - magma |
|---|
| 3470 | '(_sage_[...]![0, 1, 0, 0, 0, 0])' |
|---|
| 3471 | sage: magma(K.gen()) # optional - magma |
|---|
| 3472 | zeta9 |
|---|
| 3473 | sage: _.sage() # optional - magma |
|---|
| 3474 | zeta9 |
|---|
| 3475 | """ |
|---|
| 3476 | K = magma(self.parent()) |
|---|
| 3477 | return '(%s!%s)'%(K.name(), self.list()) |
|---|
| 3478 | |
|---|
| 3479 | def absolute_charpoly(self, var='x', algorithm=None): |
|---|
| 3480 | r""" |
|---|
| 3481 | Return the characteristic polynomial of this element over `\QQ`. |
|---|
| 3482 | |
|---|
| 3483 | For the meaning of the optional argument ``algorithm``, see :meth:`charpoly`. |
|---|
| 3484 | |
|---|
| 3485 | EXAMPLES:: |
|---|
| 3486 | |
|---|
| 3487 | sage: x = ZZ['x'].0 |
|---|
| 3488 | sage: K.<a> = NumberField(x^4 + 2, 'a') |
|---|
| 3489 | sage: a.absolute_charpoly() |
|---|
| 3490 | x^4 + 2 |
|---|
| 3491 | sage: a.absolute_charpoly('y') |
|---|
| 3492 | y^4 + 2 |
|---|
| 3493 | sage: (-a^2).absolute_charpoly() |
|---|
| 3494 | x^4 + 4*x^2 + 4 |
|---|
| 3495 | sage: (-a^2).absolute_minpoly() |
|---|
| 3496 | x^2 + 2 |
|---|
| 3497 | |
|---|
| 3498 | sage: a.absolute_charpoly(algorithm='pari') == a.absolute_charpoly(algorithm='sage') |
|---|
| 3499 | True |
|---|
| 3500 | """ |
|---|
| 3501 | # this hack is necessary because quadratic fields override |
|---|
| 3502 | # charpoly(), and they don't take the argument 'algorithm' |
|---|
| 3503 | if algorithm is None: |
|---|
| 3504 | return self.charpoly(var) |
|---|
| 3505 | return self.charpoly(var, algorithm) |
|---|
| 3506 | |
|---|
| 3507 | def absolute_minpoly(self, var='x', algorithm=None): |
|---|
| 3508 | r""" |
|---|
| 3509 | Return the minimal polynomial of this element over |
|---|
| 3510 | `\QQ`. |
|---|
| 3511 | |
|---|
| 3512 | For the meaning of the optional argument algorithm, see :meth:`charpoly`. |
|---|
| 3513 | |
|---|
| 3514 | EXAMPLES:: |
|---|
| 3515 | |
|---|
| 3516 | sage: x = ZZ['x'].0 |
|---|
| 3517 | sage: f = x^10 - 5*x^9 + 15*x^8 - 68*x^7 + 81*x^6 - 221*x^5 + 141*x^4 - 242*x^3 - 13*x^2 - 33*x - 135 |
|---|
| 3518 | sage: K.<a> = NumberField(f, 'a') |
|---|
| 3519 | sage: a.absolute_charpoly() |
|---|
| 3520 | x^10 - 5*x^9 + 15*x^8 - 68*x^7 + 81*x^6 - 221*x^5 + 141*x^4 - 242*x^3 - 13*x^2 - 33*x - 135 |
|---|
| 3521 | sage: a.absolute_charpoly('y') |
|---|
| 3522 | y^10 - 5*y^9 + 15*y^8 - 68*y^7 + 81*y^6 - 221*y^5 + 141*y^4 - 242*y^3 - 13*y^2 - 33*y - 135 |
|---|
| 3523 | sage: b = -79/9995*a^9 + 52/9995*a^8 + 271/9995*a^7 + 1663/9995*a^6 + 13204/9995*a^5 + 5573/9995*a^4 + 8435/1999*a^3 - 3116/9995*a^2 + 7734/1999*a + 1620/1999 |
|---|
| 3524 | sage: b.absolute_charpoly() |
|---|
| 3525 | x^10 + 10*x^9 + 25*x^8 - 80*x^7 - 438*x^6 + 80*x^5 + 2950*x^4 + 1520*x^3 - 10439*x^2 - 5130*x + 18225 |
|---|
| 3526 | sage: b.absolute_minpoly() |
|---|
| 3527 | x^5 + 5*x^4 - 40*x^2 - 19*x + 135 |
|---|
| 3528 | |
|---|
| 3529 | sage: b.absolute_minpoly(algorithm='pari') == b.absolute_minpoly(algorithm='sage') |
|---|
| 3530 | True |
|---|
| 3531 | """ |
|---|
| 3532 | # this hack is necessary because quadratic fields override |
|---|
| 3533 | # minpoly(), and they don't take the argument 'algorithm' |
|---|
| 3534 | if algorithm is None: |
|---|
| 3535 | return self.minpoly(var) |
|---|
| 3536 | return self.minpoly(var, algorithm) |
|---|
| 3537 | |
|---|
| 3538 | def charpoly(self, var='x', algorithm=None): |
|---|
| 3539 | r""" |
|---|
| 3540 | The characteristic polynomial of this element, over |
|---|
| 3541 | `\QQ` if self is an element of a field, and over |
|---|
| 3542 | `\ZZ` is self is an element of an order. |
|---|
| 3543 | |
|---|
| 3544 | This is the same as ``self.absolute_charpoly`` since |
|---|
| 3545 | this is an element of an absolute extension. |
|---|
| 3546 | |
|---|
| 3547 | The optional argument algorithm controls how the |
|---|
| 3548 | characteristic polynomial is computed: 'pari' uses PARI, |
|---|
| 3549 | 'sage' uses charpoly for Sage matrices. The default value |
|---|
| 3550 | None means that 'pari' is used for small degrees (up to the |
|---|
| 3551 | value of the constant TUNE_CHARPOLY_NF, currently at 25), |
|---|
| 3552 | otherwise 'sage' is used. The constant TUNE_CHARPOLY_NF |
|---|
| 3553 | should give reasonable performance on all architectures; |
|---|
| 3554 | however, if you feel the need to customize it to your own |
|---|
| 3555 | machine, see trac ticket 5213 for a tuning script. |
|---|
| 3556 | |
|---|
| 3557 | EXAMPLES: |
|---|
| 3558 | |
|---|
| 3559 | We compute the characteristic polynomial of the cube root of `2`. |
|---|
| 3560 | |
|---|
| 3561 | :: |
|---|
| 3562 | |
|---|
| 3563 | sage: R.<x> = QQ[] |
|---|
| 3564 | sage: K.<a> = NumberField(x^3-2) |
|---|
| 3565 | sage: a.charpoly('x') |
|---|
| 3566 | x^3 - 2 |
|---|
| 3567 | sage: a.charpoly('y').parent() |
|---|
| 3568 | Univariate Polynomial Ring in y over Rational Field |
|---|
| 3569 | |
|---|
| 3570 | TESTS:: |
|---|
| 3571 | |
|---|
| 3572 | sage: R = K.ring_of_integers() |
|---|
| 3573 | sage: R(a).charpoly() |
|---|
| 3574 | x^3 - 2 |
|---|
| 3575 | sage: R(a).charpoly().parent() |
|---|
| 3576 | Univariate Polynomial Ring in x over Integer Ring |
|---|
| 3577 | |
|---|
| 3578 | sage: R(a).charpoly(algorithm='pari') == R(a).charpoly(algorithm='sage') |
|---|
| 3579 | True |
|---|
| 3580 | """ |
|---|
| 3581 | if algorithm is None: |
|---|
| 3582 | if self._parent.degree() <= TUNE_CHARPOLY_NF: |
|---|
| 3583 | algorithm = 'pari' |
|---|
| 3584 | else: |
|---|
| 3585 | algorithm = 'sage' |
|---|
| 3586 | R = self._parent.base_ring()[var] |
|---|
| 3587 | if algorithm == 'pari': |
|---|
| 3588 | return R(self._pari_('x').charpoly()) |
|---|
| 3589 | if algorithm == 'sage': |
|---|
| 3590 | return R(self.matrix().charpoly()) |
|---|
| 3591 | |
|---|
| 3592 | def minpoly(self, var='x', algorithm=None): |
|---|
| 3593 | """ |
|---|
| 3594 | Return the minimal polynomial of this number field element. |
|---|
| 3595 | |
|---|
| 3596 | For the meaning of the optional argument algorithm, see charpoly(). |
|---|
| 3597 | |
|---|
| 3598 | EXAMPLES: |
|---|
| 3599 | |
|---|
| 3600 | We compute the characteristic polynomial of cube root of `2`. |
|---|
| 3601 | |
|---|
| 3602 | :: |
|---|
| 3603 | |
|---|
| 3604 | sage: R.<x> = QQ[] |
|---|
| 3605 | sage: K.<a> = NumberField(x^3-2) |
|---|
| 3606 | sage: a.minpoly('x') |
|---|
| 3607 | x^3 - 2 |
|---|
| 3608 | sage: a.minpoly('y').parent() |
|---|
| 3609 | Univariate Polynomial Ring in y over Rational Field |
|---|
| 3610 | |
|---|
| 3611 | TESTS:: |
|---|
| 3612 | |
|---|
| 3613 | sage: R = K.ring_of_integers() |
|---|
| 3614 | sage: R(a).minpoly() |
|---|
| 3615 | x^3 - 2 |
|---|
| 3616 | sage: R(a).minpoly().parent() |
|---|
| 3617 | Univariate Polynomial Ring in x over Integer Ring |
|---|
| 3618 | |
|---|
| 3619 | sage: R(a).minpoly(algorithm='pari') == R(a).minpoly(algorithm='sage') |
|---|
| 3620 | True |
|---|
| 3621 | |
|---|
| 3622 | """ |
|---|
| 3623 | return self.charpoly(var, algorithm).radical() # square free part of charpoly |
|---|
| 3624 | |
|---|
| 3625 | def list(self): |
|---|
| 3626 | """ |
|---|
| 3627 | Return the list of coefficients of self written in terms of a power |
|---|
| 3628 | basis. |
|---|
| 3629 | |
|---|
| 3630 | EXAMPLE:: |
|---|
| 3631 | |
|---|
| 3632 | sage: K.<z> = CyclotomicField(3) |
|---|
| 3633 | sage: (2+3/5*z).list() |
|---|
| 3634 | [2, 3/5] |
|---|
| 3635 | sage: (5*z).list() |
|---|
| 3636 | [0, 5] |
|---|
| 3637 | sage: K(3).list() |
|---|
| 3638 | [3, 0] |
|---|
| 3639 | """ |
|---|
| 3640 | n = self.number_field().degree() |
|---|
| 3641 | v = self._coefficients() |
|---|
| 3642 | z = sage.rings.rational.Rational(0) |
|---|
| 3643 | return v + [z]*(n - len(v)) |
|---|
| 3644 | |
|---|
| 3645 | def lift(self, var='x'): |
|---|
| 3646 | """ |
|---|
| 3647 | Return an element of QQ[x], where this number field element |
|---|
| 3648 | lives in QQ[x]/(f(x)). |
|---|
| 3649 | |
|---|
| 3650 | EXAMPLES:: |
|---|
| 3651 | |
|---|
| 3652 | sage: K.<a> = QuadraticField(-3) |
|---|
| 3653 | sage: a.lift() |
|---|
| 3654 | x |
|---|
| 3655 | |
|---|
| 3656 | """ |
|---|
| 3657 | R = self.number_field().base_field()[var] |
|---|
| 3658 | return R(self.list()) |
|---|
| 3659 | |
|---|
| 3660 | def is_real_positive(self, min_prec=53): |
|---|
| 3661 | r""" |
|---|
| 3662 | Using the ``n`` method of approximation, return ``True`` if |
|---|
| 3663 | ``self`` is a real positive number and ``False`` otherwise. |
|---|
| 3664 | This method is completely dependent of the embedding used by |
|---|
| 3665 | the ``n`` method. |
|---|
| 3666 | |
|---|
| 3667 | The algorithm first checks that ``self`` is not a strictly |
|---|
| 3668 | complex number. Then if ``self`` is not zero, by approximation |
|---|
| 3669 | more and more precise, the method answers True if the |
|---|
| 3670 | number is positive. Using `RealInterval`, the result is |
|---|
| 3671 | guaranteed to be correct. |
|---|
| 3672 | |
|---|
| 3673 | For CyclotomicField, the embedding is the natural one |
|---|
| 3674 | sending `zetan` on `cos(2*pi/n)`. |
|---|
| 3675 | |
|---|
| 3676 | EXAMPLES:: |
|---|
| 3677 | |
|---|
| 3678 | sage: K.<a> = CyclotomicField(3) |
|---|
| 3679 | sage: (a+a^2).is_real_positive() |
|---|
| 3680 | False |
|---|
| 3681 | sage: (-a-a^2).is_real_positive() |
|---|
| 3682 | True |
|---|
| 3683 | sage: K.<a> = CyclotomicField(1000) |
|---|
| 3684 | sage: (a+a^(-1)).is_real_positive() |
|---|
| 3685 | True |
|---|
| 3686 | sage: K.<a> = CyclotomicField(1009) |
|---|
| 3687 | sage: d = a^252 |
|---|
| 3688 | sage: (d+d.conjugate()).is_real_positive() |
|---|
| 3689 | True |
|---|
| 3690 | sage: d = a^253 |
|---|
| 3691 | sage: (d+d.conjugate()).is_real_positive() |
|---|
| 3692 | False |
|---|
| 3693 | sage: K.<a> = QuadraticField(3) |
|---|
| 3694 | sage: a.is_real_positive() |
|---|
| 3695 | True |
|---|
| 3696 | sage: K.<a> = QuadraticField(-3) |
|---|
| 3697 | sage: a.is_real_positive() |
|---|
| 3698 | False |
|---|
| 3699 | sage: (a-a).is_real_positive() |
|---|
| 3700 | False |
|---|
| 3701 | """ |
|---|
| 3702 | if self != self.conjugate() or self.is_zero(): |
|---|
| 3703 | return False |
|---|
| 3704 | else: |
|---|
| 3705 | approx = RealInterval(self.n(min_prec).real()) |
|---|
| 3706 | if approx.lower() > 0: |
|---|
| 3707 | return True |
|---|
| 3708 | else: |
|---|
| 3709 | if approx.upper() < 0: |
|---|
| 3710 | return False |
|---|
| 3711 | else: |
|---|
| 3712 | return self.is_real_positive(min_prec+20) |
|---|
| 3713 | |
|---|
| 3714 | cdef class NumberFieldElement_relative(NumberFieldElement): |
|---|
| 3715 | r""" |
|---|
| 3716 | The current relative number field element implementation |
|---|
| 3717 | does everything in terms of absolute polynomials. |
|---|
| 3718 | |
|---|
| 3719 | All conversions from relative polynomials, lists, vectors, etc |
|---|
| 3720 | should happen in the parent. |
|---|
| 3721 | """ |
|---|
| 3722 | def __init__(self, parent, f): |
|---|
| 3723 | r""" |
|---|
| 3724 | EXAMPLE:: |
|---|
| 3725 | |
|---|
| 3726 | sage: L.<a, b> = NumberField([x^2 + 1, x^2 + 2]) |
|---|
| 3727 | sage: type(a) # indirect doctest |
|---|
| 3728 | <type 'sage.rings.number_field.number_field_element.NumberFieldElement_relative'> |
|---|
| 3729 | |
|---|
| 3730 | We can create relative number field elements from PARI:: |
|---|
| 3731 | |
|---|
| 3732 | sage: y = polygen(QQ) |
|---|
| 3733 | sage: K.<a> = NumberField(y^2 + y + 1) |
|---|
| 3734 | sage: x = polygen(K) |
|---|
| 3735 | sage: L.<b> = NumberField(x^4 + a*x + 2) |
|---|
| 3736 | sage: e = pari(a*b); e |
|---|
| 3737 | Mod(-y^4 - 2, y^8 - y^5 + 4*y^4 + y^2 - 2*y + 4) |
|---|
| 3738 | sage: L(e) # Conversion from PARI absolute number field element |
|---|
| 3739 | a*b |
|---|
| 3740 | sage: e = L.pari_rnf().rnfeltabstorel(e); e |
|---|
| 3741 | Mod(Mod(y, y^2 + y + 1)*x, x^4 + y*x + 2) |
|---|
| 3742 | sage: L(e) # Conversion from PARI relative number field element |
|---|
| 3743 | a*b |
|---|
| 3744 | sage: e = pari('Mod(0, x^8 + 1)'); L(e) # Wrong modulus |
|---|
| 3745 | Traceback (most recent call last): |
|---|
| 3746 | ... |
|---|
| 3747 | TypeError: Coercion of PARI polmod with modulus x^8 + 1 into number field with defining polynomial x^8 - x^5 + 4*x^4 + x^2 - 2*x + 4 failed |
|---|
| 3748 | |
|---|
| 3749 | We test a relative number field element created "by hand":: |
|---|
| 3750 | |
|---|
| 3751 | sage: e = pari("Mod(Mod(y, y^2 + y + 1)*x^2 + Mod(1, y^2 + y + 1), x^4 + y*x + 2)") |
|---|
| 3752 | sage: L(e) |
|---|
| 3753 | a*b^2 + 1 |
|---|
| 3754 | |
|---|
| 3755 | Currently, conversions of PARI relative number fields are not checked:: |
|---|
| 3756 | |
|---|
| 3757 | sage: e = pari('Mod(y*x, x^4 + y^2*x + 2)'); L(e) # Wrong modulus, but succeeds anyway |
|---|
| 3758 | a*b |
|---|
| 3759 | """ |
|---|
| 3760 | NumberFieldElement.__init__(self, parent, f) |
|---|
| 3761 | |
|---|
| 3762 | def __getitem__(self, n): |
|---|
| 3763 | """ |
|---|
| 3764 | Return the n-th coefficient of this relative number field element, written |
|---|
| 3765 | as a polynomial in the generator. |
|---|
| 3766 | |
|---|
| 3767 | Note that `n` must be between 0 and `d-1`, where |
|---|
| 3768 | `d` is the relative degree of the number field. |
|---|
| 3769 | |
|---|
| 3770 | EXAMPLES:: |
|---|
| 3771 | |
|---|
| 3772 | sage: K.<a, b> = NumberField([x^3 - 5, x^2 + 3]) |
|---|
| 3773 | sage: c = (a + b)^3; c |
|---|
| 3774 | 3*b*a^2 - 9*a - 3*b + 5 |
|---|
| 3775 | sage: c[0] |
|---|
| 3776 | -3*b + 5 |
|---|
| 3777 | |
|---|
| 3778 | We illustrate bounds checking:: |
|---|
| 3779 | |
|---|
| 3780 | sage: c[-1] |
|---|
| 3781 | Traceback (most recent call last): |
|---|
| 3782 | ... |
|---|
| 3783 | IndexError: index must be between 0 and the relative degree minus 1. |
|---|
| 3784 | sage: c[4] |
|---|
| 3785 | Traceback (most recent call last): |
|---|
| 3786 | ... |
|---|
| 3787 | IndexError: index must be between 0 and the relative degree minus 1. |
|---|
| 3788 | |
|---|
| 3789 | The list method implicitly calls ``__getitem__``:: |
|---|
| 3790 | |
|---|
| 3791 | sage: list(c) |
|---|
| 3792 | [-3*b + 5, -9, 3*b] |
|---|
| 3793 | sage: K(list(c)) == c |
|---|
| 3794 | True |
|---|
| 3795 | """ |
|---|
| 3796 | if n < 0 or n >= self.parent().relative_degree(): |
|---|
| 3797 | raise IndexError, "index must be between 0 and the relative degree minus 1." |
|---|
| 3798 | return self.vector()[n] |
|---|
| 3799 | |
|---|
| 3800 | def _magma_init_(self, magma): |
|---|
| 3801 | """ |
|---|
| 3802 | EXAMPLES:: |
|---|
| 3803 | |
|---|
| 3804 | sage: K.<a, b> = NumberField([x^3 - 5, x^2 + 3]) |
|---|
| 3805 | sage: a._magma_init_(magma) |
|---|
| 3806 | Traceback (most recent call last): |
|---|
| 3807 | ... |
|---|
| 3808 | TypeError: coercion of relative number field elements to Magma is not implemented |
|---|
| 3809 | """ |
|---|
| 3810 | raise TypeError, "coercion of relative number field elements to Magma is not implemented" |
|---|
| 3811 | |
|---|
| 3812 | def list(self): |
|---|
| 3813 | """ |
|---|
| 3814 | Return the list of coefficients of self written in terms of a power |
|---|
| 3815 | basis. |
|---|
| 3816 | |
|---|
| 3817 | EXAMPLES:: |
|---|
| 3818 | |
|---|
| 3819 | sage: K.<a,b> = NumberField([x^3+2, x^2+1]) |
|---|
| 3820 | sage: a.list() |
|---|
| 3821 | [0, 1, 0] |
|---|
| 3822 | sage: v = (K.base_field().0 + a)^2 ; v |
|---|
| 3823 | a^2 + 2*b*a - 1 |
|---|
| 3824 | sage: v.list() |
|---|
| 3825 | [-1, 2*b, 1] |
|---|
| 3826 | """ |
|---|
| 3827 | return self.vector().list() |
|---|
| 3828 | |
|---|
| 3829 | def lift(self, var='x'): |
|---|
| 3830 | """ |
|---|
| 3831 | Return an element of K[x], where this number field element |
|---|
| 3832 | lives in the relative number field K[x]/(f(x)). |
|---|
| 3833 | |
|---|
| 3834 | EXAMPLES:: |
|---|
| 3835 | |
|---|
| 3836 | sage: K.<a> = QuadraticField(-3) |
|---|
| 3837 | sage: x = polygen(K) |
|---|
| 3838 | sage: L.<b> = K.extension(x^7 + 5) |
|---|
| 3839 | sage: u = L(1/2*a + 1/2 + b + (a-9)*b^5) |
|---|
| 3840 | sage: u.lift() |
|---|
| 3841 | (a - 9)*x^5 + x + 1/2*a + 1/2 |
|---|
| 3842 | |
|---|
| 3843 | """ |
|---|
| 3844 | K = self.number_field() |
|---|
| 3845 | # Compute representation of self in terms of relative vector space. |
|---|
| 3846 | R = K.base_field()[var] |
|---|
| 3847 | return R(self.list()) |
|---|
| 3848 | |
|---|
| 3849 | def _repr_(self): |
|---|
| 3850 | r""" |
|---|
| 3851 | EXAMPLE:: |
|---|
| 3852 | |
|---|
| 3853 | sage: L.<a, b> = NumberField([x^3 - x + 1, x^2 + 23]) |
|---|
| 3854 | sage: repr(a^4*b) # indirect doctest |
|---|
| 3855 | 'b*a^2 - b*a' |
|---|
| 3856 | """ |
|---|
| 3857 | K = self.number_field() |
|---|
| 3858 | # Compute representation of self in terms of relative vector space. |
|---|
| 3859 | R = K.base_field()[K.variable_name()] |
|---|
| 3860 | return repr(R(self.list())) |
|---|
| 3861 | |
|---|
| 3862 | def _latex_(self): |
|---|
| 3863 | r""" |
|---|
| 3864 | Returns the latex representation for this element. |
|---|
| 3865 | |
|---|
| 3866 | EXAMPLES:: |
|---|
| 3867 | |
|---|
| 3868 | sage: C.<zeta> = CyclotomicField(12) |
|---|
| 3869 | sage: PC.<x> = PolynomialRing(C) |
|---|
| 3870 | sage: K.<alpha> = NumberField(x^2 - 7) |
|---|
| 3871 | sage: latex((alpha + zeta)^4) # indirect doctest |
|---|
| 3872 | \left(4 \zeta_{12}^{3} + 28 \zeta_{12}\right) \alpha + 43 \zeta_{12}^{2} + 48 |
|---|
| 3873 | sage: PK.<y> = PolynomialRing(K) |
|---|
| 3874 | sage: L.<beta> = NumberField(y^3 + y + alpha) |
|---|
| 3875 | sage: latex((beta + zeta)^3) # indirect doctest |
|---|
| 3876 | 3 \zeta_{12} \beta^{2} + \left(3 \zeta_{12}^{2} - 1\right) \beta - \alpha + \zeta_{12}^{3} |
|---|
| 3877 | """ |
|---|
| 3878 | K = self.number_field() |
|---|
| 3879 | R = K.base_field()[K.variable_name()] |
|---|
| 3880 | return R(self.list())._latex_() |
|---|
| 3881 | |
|---|
| 3882 | def charpoly(self, var='x'): |
|---|
| 3883 | r""" |
|---|
| 3884 | The characteristic polynomial of this element over its base field. |
|---|
| 3885 | |
|---|
| 3886 | EXAMPLES:: |
|---|
| 3887 | |
|---|
| 3888 | sage: x = ZZ['x'].0 |
|---|
| 3889 | sage: K.<a, b> = QQ.extension([x^2 + 2, x^5 + 400*x^4 + 11*x^2 + 2]) |
|---|
| 3890 | sage: a.charpoly() |
|---|
| 3891 | x^2 + 2 |
|---|
| 3892 | sage: b.charpoly() |
|---|
| 3893 | x^2 - 2*b*x + b^2 |
|---|
| 3894 | sage: b.minpoly() |
|---|
| 3895 | x - b |
|---|
| 3896 | |
|---|
| 3897 | sage: K.<a, b> = NumberField([x^2 + 2, x^2 + 1000*x + 1]) |
|---|
| 3898 | sage: y = K['y'].0 |
|---|
| 3899 | sage: L.<c> = K.extension(y^2 + a*y + b) |
|---|
| 3900 | sage: c.charpoly() |
|---|
| 3901 | x^2 + a*x + b |
|---|
| 3902 | sage: c.minpoly() |
|---|
| 3903 | x^2 + a*x + b |
|---|
| 3904 | sage: L(a).charpoly() |
|---|
| 3905 | x^2 - 2*a*x - 2 |
|---|
| 3906 | sage: L(a).minpoly() |
|---|
| 3907 | x - a |
|---|
| 3908 | sage: L(b).charpoly() |
|---|
| 3909 | x^2 - 2*b*x - 1000*b - 1 |
|---|
| 3910 | sage: L(b).minpoly() |
|---|
| 3911 | x - b |
|---|
| 3912 | """ |
|---|
| 3913 | R = self._parent.base_ring()[var] |
|---|
| 3914 | return R(self.matrix().charpoly()) |
|---|
| 3915 | |
|---|
| 3916 | def absolute_charpoly(self, var='x', algorithm=None): |
|---|
| 3917 | r""" |
|---|
| 3918 | The characteristic polynomial of this element over |
|---|
| 3919 | `\QQ`. |
|---|
| 3920 | |
|---|
| 3921 | We construct a relative extension and find the characteristic |
|---|
| 3922 | polynomial over `\QQ`. |
|---|
| 3923 | |
|---|
| 3924 | The optional argument algorithm controls how the |
|---|
| 3925 | characteristic polynomial is computed: 'pari' uses PARI, |
|---|
| 3926 | 'sage' uses charpoly for Sage matrices. The default value |
|---|
| 3927 | None means that 'pari' is used for small degrees (up to the |
|---|
| 3928 | value of the constant TUNE_CHARPOLY_NF, currently at 25), |
|---|
| 3929 | otherwise 'sage' is used. The constant TUNE_CHARPOLY_NF |
|---|
| 3930 | should give reasonable performance on all architectures; |
|---|
| 3931 | however, if you feel the need to customize it to your own |
|---|
| 3932 | machine, see trac ticket 5213 for a tuning script. |
|---|
| 3933 | |
|---|
| 3934 | EXAMPLES:: |
|---|
| 3935 | |
|---|
| 3936 | sage: R.<x> = QQ[] |
|---|
| 3937 | sage: K.<a> = NumberField(x^3-2) |
|---|
| 3938 | sage: S.<X> = K[] |
|---|
| 3939 | sage: L.<b> = NumberField(X^3 + 17); L |
|---|
| 3940 | Number Field in b with defining polynomial X^3 + 17 over its base field |
|---|
| 3941 | sage: b.absolute_charpoly() |
|---|
| 3942 | x^9 + 51*x^6 + 867*x^3 + 4913 |
|---|
| 3943 | sage: b.charpoly()(b) |
|---|
| 3944 | 0 |
|---|
| 3945 | sage: a = L.0; a |
|---|
| 3946 | b |
|---|
| 3947 | sage: a.absolute_charpoly('x') |
|---|
| 3948 | x^9 + 51*x^6 + 867*x^3 + 4913 |
|---|
| 3949 | sage: a.absolute_charpoly('y') |
|---|
| 3950 | y^9 + 51*y^6 + 867*y^3 + 4913 |
|---|
| 3951 | |
|---|
| 3952 | sage: a.absolute_charpoly(algorithm='pari') == a.absolute_charpoly(algorithm='sage') |
|---|
| 3953 | True |
|---|
| 3954 | """ |
|---|
| 3955 | if algorithm is None: |
|---|
| 3956 | # this might not be the optimal condition; maybe it should |
|---|
| 3957 | # be .degree() instead of .absolute_degree() |
|---|
| 3958 | # there are too many bugs in relative number fields to |
|---|
| 3959 | # figure this out now |
|---|
| 3960 | if self._parent.absolute_degree() <= TUNE_CHARPOLY_NF: |
|---|
| 3961 | algorithm = 'pari' |
|---|
| 3962 | else: |
|---|
| 3963 | algorithm = 'sage' |
|---|
| 3964 | R = QQ[var] |
|---|
| 3965 | if algorithm == 'pari': |
|---|
| 3966 | return R(self._pari_().charpoly()) |
|---|
| 3967 | if algorithm == 'sage': |
|---|
| 3968 | return R(self.matrix(QQ).charpoly()) |
|---|
| 3969 | |
|---|
| 3970 | def absolute_minpoly(self, var='x', algorithm=None): |
|---|
| 3971 | r""" |
|---|
| 3972 | Return the minimal polynomial over `\QQ` of this element. |
|---|
| 3973 | |
|---|
| 3974 | For the meaning of the optional argument ``algorithm``, see :meth:`absolute_charpoly`. |
|---|
| 3975 | |
|---|
| 3976 | EXAMPLES:: |
|---|
| 3977 | |
|---|
| 3978 | sage: K.<a, b> = NumberField([x^2 + 2, x^2 + 1000*x + 1]) |
|---|
| 3979 | sage: y = K['y'].0 |
|---|
| 3980 | sage: L.<c> = K.extension(y^2 + a*y + b) |
|---|
| 3981 | sage: c.absolute_charpoly() |
|---|
| 3982 | x^8 - 1996*x^6 + 996006*x^4 + 1997996*x^2 + 1 |
|---|
| 3983 | sage: c.absolute_minpoly() |
|---|
| 3984 | x^8 - 1996*x^6 + 996006*x^4 + 1997996*x^2 + 1 |
|---|
| 3985 | sage: L(a).absolute_charpoly() |
|---|
| 3986 | x^8 + 8*x^6 + 24*x^4 + 32*x^2 + 16 |
|---|
| 3987 | sage: L(a).absolute_minpoly() |
|---|
| 3988 | x^2 + 2 |
|---|
| 3989 | sage: L(b).absolute_charpoly() |
|---|
| 3990 | x^8 + 4000*x^7 + 6000004*x^6 + 4000012000*x^5 + 1000012000006*x^4 + 4000012000*x^3 + 6000004*x^2 + 4000*x + 1 |
|---|
| 3991 | sage: L(b).absolute_minpoly() |
|---|
| 3992 | x^2 + 1000*x + 1 |
|---|
| 3993 | """ |
|---|
| 3994 | return self.absolute_charpoly(var, algorithm).radical() |
|---|
| 3995 | |
|---|
| 3996 | def valuation(self, P): |
|---|
| 3997 | """ |
|---|
| 3998 | Returns the valuation of self at a given prime ideal P. |
|---|
| 3999 | |
|---|
| 4000 | INPUT: |
|---|
| 4001 | |
|---|
| 4002 | |
|---|
| 4003 | - ``P`` - a prime ideal of relative number field which is the parent of self |
|---|
| 4004 | |
|---|
| 4005 | |
|---|
| 4006 | EXAMPLES:: |
|---|
| 4007 | |
|---|
| 4008 | sage: K.<a, b, c> = NumberField([x^2 - 2, x^2 - 3, x^2 - 5]) |
|---|
| 4009 | sage: P = K.prime_factors(5)[0] |
|---|
| 4010 | sage: (2*a + b - c).valuation(P) |
|---|
| 4011 | 1 |
|---|
| 4012 | """ |
|---|
| 4013 | P_abs = P.absolute_ideal() |
|---|
| 4014 | abs = P_abs.number_field() |
|---|
| 4015 | to_abs = abs.structure()[1] |
|---|
| 4016 | return to_abs(self).valuation(P_abs) |
|---|
| 4017 | |
|---|
| 4018 | |
|---|
| 4019 | cdef class OrderElement_absolute(NumberFieldElement_absolute): |
|---|
| 4020 | """ |
|---|
| 4021 | Element of an order in an absolute number field. |
|---|
| 4022 | |
|---|
| 4023 | EXAMPLES:: |
|---|
| 4024 | |
|---|
| 4025 | sage: K.<a> = NumberField(x^2 + 1) |
|---|
| 4026 | sage: O2 = K.order(2*a) |
|---|
| 4027 | sage: w = O2.1; w |
|---|
| 4028 | 2*a |
|---|
| 4029 | sage: parent(w) |
|---|
| 4030 | Order in Number Field in a with defining polynomial x^2 + 1 |
|---|
| 4031 | |
|---|
| 4032 | sage: w.absolute_charpoly() |
|---|
| 4033 | x^2 + 4 |
|---|
| 4034 | sage: w.absolute_charpoly().parent() |
|---|
| 4035 | Univariate Polynomial Ring in x over Integer Ring |
|---|
| 4036 | sage: w.absolute_minpoly() |
|---|
| 4037 | x^2 + 4 |
|---|
| 4038 | sage: w.absolute_minpoly().parent() |
|---|
| 4039 | Univariate Polynomial Ring in x over Integer Ring |
|---|
| 4040 | """ |
|---|
| 4041 | def __init__(self, order, f): |
|---|
| 4042 | r""" |
|---|
| 4043 | EXAMPLE:: |
|---|
| 4044 | |
|---|
| 4045 | sage: K.<a> = NumberField(x^3 + 2) |
|---|
| 4046 | sage: O2 = K.order(2*a) |
|---|
| 4047 | sage: type(O2.1) # indirect doctest |
|---|
| 4048 | <type 'sage.rings.number_field.number_field_element.OrderElement_absolute'> |
|---|
| 4049 | """ |
|---|
| 4050 | K = order.number_field() |
|---|
| 4051 | NumberFieldElement_absolute.__init__(self, K, f) |
|---|
| 4052 | self._number_field = K |
|---|
| 4053 | (<Element>self)._parent = order |
|---|
| 4054 | |
|---|
| 4055 | cdef _new(self): |
|---|
| 4056 | """ |
|---|
| 4057 | Quickly creates a new initialized NumberFieldElement with the same |
|---|
| 4058 | parent as self. |
|---|
| 4059 | |
|---|
| 4060 | EXAMPLES: |
|---|
| 4061 | |
|---|
| 4062 | This is called implicitly in multiplication:: |
|---|
| 4063 | |
|---|
| 4064 | sage: O = EquationOrder(x^3 + 18, 'a') |
|---|
| 4065 | sage: O.1 * O.1 * O.1 |
|---|
| 4066 | -18 |
|---|
| 4067 | """ |
|---|
| 4068 | cdef OrderElement_absolute x |
|---|
| 4069 | x = <OrderElement_absolute>PY_NEW_SAME_TYPE(self) |
|---|
| 4070 | x._parent = self._parent |
|---|
| 4071 | x._number_field = self._parent.number_field() |
|---|
| 4072 | x.__fld_numerator = self.__fld_numerator |
|---|
| 4073 | x.__fld_denominator = self.__fld_denominator |
|---|
| 4074 | return x |
|---|
| 4075 | |
|---|
| 4076 | cdef number_field(self): |
|---|
| 4077 | r""" |
|---|
| 4078 | Return the number field of self. Only accessible from Cython. |
|---|
| 4079 | |
|---|
| 4080 | EXAMPLE:: |
|---|
| 4081 | |
|---|
| 4082 | sage: K = NumberField(x^3 - 17, 'a') |
|---|
| 4083 | sage: OK = K.ring_of_integers() |
|---|
| 4084 | sage: a = OK(K.gen()) |
|---|
| 4085 | sage: a._number_field() is K # indirect doctest |
|---|
| 4086 | True |
|---|
| 4087 | """ |
|---|
| 4088 | return self._number_field |
|---|
| 4089 | |
|---|
| 4090 | cpdef RingElement _div_(self, RingElement other): |
|---|
| 4091 | r""" |
|---|
| 4092 | Implement division, checking that the result has the right parent. |
|---|
| 4093 | It's not so crucial what the parent actually is, but it is crucial |
|---|
| 4094 | that the returned value really is an element of its supposed |
|---|
| 4095 | parent! This fixes trac #4190. |
|---|
| 4096 | |
|---|
| 4097 | EXAMPLES:: |
|---|
| 4098 | |
|---|
| 4099 | sage: K = NumberField(x^3 - 17, 'a') |
|---|
| 4100 | sage: OK = K.ring_of_integers() |
|---|
| 4101 | sage: a = OK(K.gen()) |
|---|
| 4102 | sage: (17/a) in OK # indirect doctest |
|---|
| 4103 | True |
|---|
| 4104 | sage: (17/a).parent() is K # indirect doctest |
|---|
| 4105 | True |
|---|
| 4106 | sage: (17/(2*a)).parent() is K # indirect doctest |
|---|
| 4107 | True |
|---|
| 4108 | sage: (17/(2*a)) in OK # indirect doctest |
|---|
| 4109 | False |
|---|
| 4110 | """ |
|---|
| 4111 | cdef NumberFieldElement_absolute x |
|---|
| 4112 | x = NumberFieldElement_absolute._div_(self, other) |
|---|
| 4113 | return self._parent.number_field()(x) |
|---|
| 4114 | |
|---|
| 4115 | def inverse_mod(self, I): |
|---|
| 4116 | r""" |
|---|
| 4117 | Return an inverse of self modulo the given ideal. |
|---|
| 4118 | |
|---|
| 4119 | INPUT: |
|---|
| 4120 | |
|---|
| 4121 | |
|---|
| 4122 | - ``I`` - may be an ideal of self.parent(), or an |
|---|
| 4123 | element or list of elements of self.parent() generating a nonzero |
|---|
| 4124 | ideal. A ValueError is raised if I is non-integral or is zero. |
|---|
| 4125 | A ZeroDivisionError is raised if I + (x) != (1). |
|---|
| 4126 | |
|---|
| 4127 | |
|---|
| 4128 | EXAMPLES:: |
|---|
| 4129 | |
|---|
| 4130 | sage: OE = NumberField(x^3 - x + 2, 'w').ring_of_integers() |
|---|
| 4131 | sage: w = OE.ring_generators()[0] |
|---|
| 4132 | sage: w.inverse_mod(13*OE) |
|---|
| 4133 | 6*w^2 - 6 |
|---|
| 4134 | sage: w * (w.inverse_mod(13)) - 1 in 13*OE |
|---|
| 4135 | True |
|---|
| 4136 | sage: w.inverse_mod(13).parent() == OE |
|---|
| 4137 | True |
|---|
| 4138 | sage: w.inverse_mod(2*OE) |
|---|
| 4139 | Traceback (most recent call last): |
|---|
| 4140 | ... |
|---|
| 4141 | ZeroDivisionError: w is not invertible modulo Fractional ideal (2) |
|---|
| 4142 | """ |
|---|
| 4143 | R = self.parent() |
|---|
| 4144 | return R(_inverse_mod_generic(self, I)) |
|---|
| 4145 | |
|---|
| 4146 | def __invert__(self): |
|---|
| 4147 | r""" |
|---|
| 4148 | Implement inversion, checking that the return value has the right |
|---|
| 4149 | parent. See trac #4190. |
|---|
| 4150 | |
|---|
| 4151 | EXAMPLE:: |
|---|
| 4152 | |
|---|
| 4153 | sage: K = NumberField(x^3 -x + 2, 'a') |
|---|
| 4154 | sage: OK = K.ring_of_integers() |
|---|
| 4155 | sage: a = OK(K.gen()) |
|---|
| 4156 | sage: (~a).parent() is K |
|---|
| 4157 | True |
|---|
| 4158 | sage: (~a) in OK |
|---|
| 4159 | False |
|---|
| 4160 | sage: a**(-1) in OK |
|---|
| 4161 | False |
|---|
| 4162 | """ |
|---|
| 4163 | return self._parent.number_field()(NumberFieldElement_absolute.__invert__(self)) |
|---|
| 4164 | |
|---|
| 4165 | cdef class OrderElement_relative(NumberFieldElement_relative): |
|---|
| 4166 | """ |
|---|
| 4167 | Element of an order in a relative number field. |
|---|
| 4168 | |
|---|
| 4169 | EXAMPLES:: |
|---|
| 4170 | |
|---|
| 4171 | sage: O = EquationOrder([x^2 + x + 1, x^3 - 2],'a,b') |
|---|
| 4172 | sage: c = O.1; c |
|---|
| 4173 | (-2*b^2 - 2)*a - 2*b^2 - b |
|---|
| 4174 | sage: type(c) |
|---|
| 4175 | <type 'sage.rings.number_field.number_field_element.OrderElement_relative'> |
|---|
| 4176 | """ |
|---|
| 4177 | def __init__(self, order, f): |
|---|
| 4178 | r""" |
|---|
| 4179 | EXAMPLE:: |
|---|
| 4180 | |
|---|
| 4181 | sage: O = EquationOrder([x^2 + x + 1, x^3 - 2],'a,b') |
|---|
| 4182 | sage: type(O.1) # indirect doctest |
|---|
| 4183 | <type 'sage.rings.number_field.number_field_element.OrderElement_relative'> |
|---|
| 4184 | """ |
|---|
| 4185 | K = order.number_field() |
|---|
| 4186 | NumberFieldElement_relative.__init__(self, K, f) |
|---|
| 4187 | (<Element>self)._parent = order |
|---|
| 4188 | self._number_field = K |
|---|
| 4189 | |
|---|
| 4190 | cdef number_field(self): |
|---|
| 4191 | return self._number_field |
|---|
| 4192 | |
|---|
| 4193 | cdef _new(self): |
|---|
| 4194 | """ |
|---|
| 4195 | Quickly creates a new initialized NumberFieldElement with the same |
|---|
| 4196 | parent as self. |
|---|
| 4197 | |
|---|
| 4198 | EXAMPLES: |
|---|
| 4199 | |
|---|
| 4200 | This is called implicitly in multiplication:: |
|---|
| 4201 | |
|---|
| 4202 | sage: O = EquationOrder([x^2 + 18, x^3 + 2], 'a,b') |
|---|
| 4203 | sage: c = O.1 * O.2; c |
|---|
| 4204 | (-23321*b^2 - 9504*b + 10830)*a + 10152*b^2 - 104562*b - 110158 |
|---|
| 4205 | sage: parent(c) == O |
|---|
| 4206 | True |
|---|
| 4207 | """ |
|---|
| 4208 | cdef OrderElement_relative x |
|---|
| 4209 | x = <OrderElement_relative>PY_NEW_SAME_TYPE(self) |
|---|
| 4210 | x._parent = self._parent |
|---|
| 4211 | x._number_field = self._parent.number_field() |
|---|
| 4212 | x.__fld_numerator = self.__fld_numerator |
|---|
| 4213 | x.__fld_denominator = self.__fld_denominator |
|---|
| 4214 | return x |
|---|
| 4215 | |
|---|
| 4216 | cpdef RingElement _div_(self, RingElement other): |
|---|
| 4217 | r""" |
|---|
| 4218 | Implement division, checking that the result has the right parent. |
|---|
| 4219 | It's not so crucial what the parent actually is, but it is crucial |
|---|
| 4220 | that the returned value really is an element of its supposed |
|---|
| 4221 | parent. This fixes trac #4190. |
|---|
| 4222 | |
|---|
| 4223 | EXAMPLES:: |
|---|
| 4224 | |
|---|
| 4225 | sage: K1.<a> = NumberField(x^3 - 17) |
|---|
| 4226 | sage: R.<y> = K1[] |
|---|
| 4227 | sage: K2 = K1.extension(y^2 - a, 'b') |
|---|
| 4228 | sage: OK2 = K2.order(K2.gen()) # (not maximal) |
|---|
| 4229 | sage: b = OK2.gens()[1]; b |
|---|
| 4230 | b |
|---|
| 4231 | sage: (17/b).parent() is K2 # indirect doctest |
|---|
| 4232 | True |
|---|
| 4233 | sage: (17/b) in OK2 # indirect doctest |
|---|
| 4234 | True |
|---|
| 4235 | sage: (17/b^7) in OK2 # indirect doctest |
|---|
| 4236 | False |
|---|
| 4237 | """ |
|---|
| 4238 | cdef NumberFieldElement_relative x |
|---|
| 4239 | x = NumberFieldElement_relative._div_(self, other) |
|---|
| 4240 | return self._parent.number_field()(x) |
|---|
| 4241 | |
|---|
| 4242 | def __invert__(self): |
|---|
| 4243 | r""" |
|---|
| 4244 | Implement division, checking that the result has the right parent. |
|---|
| 4245 | See trac #4190. |
|---|
| 4246 | |
|---|
| 4247 | EXAMPLES:: |
|---|
| 4248 | |
|---|
| 4249 | sage: K1.<a> = NumberField(x^3 - 17) |
|---|
| 4250 | sage: R.<y> = K1[] |
|---|
| 4251 | sage: K2 = K1.extension(y^2 - a, 'b') |
|---|
| 4252 | sage: OK2 = K2.order(K2.gen()) # (not maximal) |
|---|
| 4253 | sage: b = OK2.gens()[1]; b |
|---|
| 4254 | b |
|---|
| 4255 | sage: b.parent() is OK2 |
|---|
| 4256 | True |
|---|
| 4257 | sage: (~b).parent() is K2 |
|---|
| 4258 | True |
|---|
| 4259 | sage: (~b) in OK2 # indirect doctest |
|---|
| 4260 | False |
|---|
| 4261 | sage: b**(-1) in OK2 # indirect doctest |
|---|
| 4262 | False |
|---|
| 4263 | """ |
|---|
| 4264 | return self._parent.number_field()(NumberFieldElement_relative.__invert__(self)) |
|---|
| 4265 | |
|---|
| 4266 | def inverse_mod(self, I): |
|---|
| 4267 | r""" |
|---|
| 4268 | Return an inverse of self modulo the given ideal. |
|---|
| 4269 | |
|---|
| 4270 | INPUT: |
|---|
| 4271 | |
|---|
| 4272 | |
|---|
| 4273 | - ``I`` - may be an ideal of self.parent(), or an |
|---|
| 4274 | element or list of elements of self.parent() generating a nonzero |
|---|
| 4275 | ideal. A ValueError is raised if I is non-integral or is zero. |
|---|
| 4276 | A ZeroDivisionError is raised if I + (x) != (1). |
|---|
| 4277 | |
|---|
| 4278 | |
|---|
| 4279 | EXAMPLES:: |
|---|
| 4280 | |
|---|
| 4281 | sage: E.<a,b> = NumberField([x^2 - x + 2, x^2 + 1]) |
|---|
| 4282 | sage: OE = E.ring_of_integers() |
|---|
| 4283 | sage: t = OE(b - a).inverse_mod(17*b) |
|---|
| 4284 | sage: t*(b - a) - 1 in E.ideal(17*b) |
|---|
| 4285 | True |
|---|
| 4286 | sage: t.parent() == OE |
|---|
| 4287 | True |
|---|
| 4288 | """ |
|---|
| 4289 | R = self.parent() |
|---|
| 4290 | return R(_inverse_mod_generic(self, I)) |
|---|
| 4291 | |
|---|
| 4292 | def charpoly(self, var='x'): |
|---|
| 4293 | r""" |
|---|
| 4294 | The characteristic polynomial of this order element over its base ring. |
|---|
| 4295 | |
|---|
| 4296 | This special implementation works around bug \#4738. At this |
|---|
| 4297 | time the base ring of relative order elements is ZZ; it should |
|---|
| 4298 | be the ring of integers of the base field. |
|---|
| 4299 | |
|---|
| 4300 | EXAMPLES:: |
|---|
| 4301 | |
|---|
| 4302 | sage: x = ZZ['x'].0 |
|---|
| 4303 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
|---|
| 4304 | sage: OK = K.maximal_order(); OK.basis() |
|---|
| 4305 | [1, 1/2*a - 1/2*b, -1/2*b*a + 1/2, a] |
|---|
| 4306 | sage: charpoly(OK.1) |
|---|
| 4307 | x^2 + b*x + 1 |
|---|
| 4308 | sage: charpoly(OK.1).parent() |
|---|
| 4309 | Univariate Polynomial Ring in x over Maximal Order in Number Field in b with defining polynomial x^2 - 3 |
|---|
| 4310 | sage: [ charpoly(t) for t in OK.basis() ] |
|---|
| 4311 | [x^2 - 2*x + 1, x^2 + b*x + 1, x^2 - x + 1, x^2 + 1] |
|---|
| 4312 | """ |
|---|
| 4313 | R = self.parent().number_field().base_field().ring_of_integers()[var] |
|---|
| 4314 | return R(self.matrix().charpoly(var)) |
|---|
| 4315 | |
|---|
| 4316 | def minpoly(self, var='x'): |
|---|
| 4317 | r""" |
|---|
| 4318 | The minimal polynomial of this order element over its base ring. |
|---|
| 4319 | |
|---|
| 4320 | This special implementation works around bug \#4738. At this |
|---|
| 4321 | time the base ring of relative order elements is ZZ; it should |
|---|
| 4322 | be the ring of integers of the base field. |
|---|
| 4323 | |
|---|
| 4324 | EXAMPLES:: |
|---|
| 4325 | |
|---|
| 4326 | sage: x = ZZ['x'].0 |
|---|
| 4327 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
|---|
| 4328 | sage: OK = K.maximal_order(); OK.basis() |
|---|
| 4329 | [1, 1/2*a - 1/2*b, -1/2*b*a + 1/2, a] |
|---|
| 4330 | sage: minpoly(OK.1) |
|---|
| 4331 | x^2 + b*x + 1 |
|---|
| 4332 | sage: charpoly(OK.1).parent() |
|---|
| 4333 | Univariate Polynomial Ring in x over Maximal Order in Number Field in b with defining polynomial x^2 - 3 |
|---|
| 4334 | sage: _, u, _, v = OK.basis() |
|---|
| 4335 | sage: t = 2*u - v; t |
|---|
| 4336 | -b |
|---|
| 4337 | sage: t.charpoly() |
|---|
| 4338 | x^2 + 2*b*x + 3 |
|---|
| 4339 | sage: t.minpoly() |
|---|
| 4340 | x + b |
|---|
| 4341 | |
|---|
| 4342 | sage: t.absolute_charpoly() |
|---|
| 4343 | x^4 - 6*x^2 + 9 |
|---|
| 4344 | sage: t.absolute_minpoly() |
|---|
| 4345 | x^2 - 3 |
|---|
| 4346 | """ |
|---|
| 4347 | K = self.parent().number_field() |
|---|
| 4348 | R = K.base_field().ring_of_integers()[var] |
|---|
| 4349 | return R(K(self).minpoly(var)) |
|---|
| 4350 | |
|---|
| 4351 | def absolute_charpoly(self, var='x'): |
|---|
| 4352 | r""" |
|---|
| 4353 | The absolute characteristic polynomial of this order element over ZZ. |
|---|
| 4354 | |
|---|
| 4355 | EXAMPLES:: |
|---|
| 4356 | |
|---|
| 4357 | sage: x = ZZ['x'].0 |
|---|
| 4358 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
|---|
| 4359 | sage: OK = K.maximal_order() |
|---|
| 4360 | sage: _, u, _, v = OK.basis() |
|---|
| 4361 | sage: t = 2*u - v; t |
|---|
| 4362 | -b |
|---|
| 4363 | sage: t.absolute_charpoly() |
|---|
| 4364 | x^4 - 6*x^2 + 9 |
|---|
| 4365 | sage: t.absolute_minpoly() |
|---|
| 4366 | x^2 - 3 |
|---|
| 4367 | sage: t.absolute_charpoly().parent() |
|---|
| 4368 | Univariate Polynomial Ring in x over Integer Ring |
|---|
| 4369 | """ |
|---|
| 4370 | K = self.parent().number_field() |
|---|
| 4371 | R = ZZ[var] |
|---|
| 4372 | return R(K(self).absolute_charpoly(var)) |
|---|
| 4373 | |
|---|
| 4374 | def absolute_minpoly(self, var='x'): |
|---|
| 4375 | r""" |
|---|
| 4376 | The absolute minimal polynomial of this order element over ZZ. |
|---|
| 4377 | |
|---|
| 4378 | EXAMPLES:: |
|---|
| 4379 | |
|---|
| 4380 | sage: x = ZZ['x'].0 |
|---|
| 4381 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
|---|
| 4382 | sage: OK = K.maximal_order() |
|---|
| 4383 | sage: _, u, _, v = OK.basis() |
|---|
| 4384 | sage: t = 2*u - v; t |
|---|
| 4385 | -b |
|---|
| 4386 | sage: t.absolute_charpoly() |
|---|
| 4387 | x^4 - 6*x^2 + 9 |
|---|
| 4388 | sage: t.absolute_minpoly() |
|---|
| 4389 | x^2 - 3 |
|---|
| 4390 | sage: t.absolute_minpoly().parent() |
|---|
| 4391 | Univariate Polynomial Ring in x over Integer Ring |
|---|
| 4392 | """ |
|---|
| 4393 | K = self.parent().number_field() |
|---|
| 4394 | R = ZZ[var] |
|---|
| 4395 | return R(K(self).absolute_minpoly(var)) |
|---|
| 4396 | |
|---|
| 4397 | |
|---|
| 4398 | |
|---|
| 4399 | class CoordinateFunction: |
|---|
| 4400 | r""" |
|---|
| 4401 | This class provides a callable object which expresses |
|---|
| 4402 | elements in terms of powers of a fixed field generator `\alpha`. |
|---|
| 4403 | |
|---|
| 4404 | EXAMPLE:: |
|---|
| 4405 | |
|---|
| 4406 | sage: K.<a> = NumberField(x^2 + x + 3) |
|---|
| 4407 | sage: f = (a + 1).coordinates_in_terms_of_powers(); f |
|---|
| 4408 | Coordinate function that writes elements in terms of the powers of a + 1 |
|---|
| 4409 | sage: f.__class__ |
|---|
| 4410 | <class sage.rings.number_field.number_field_element.CoordinateFunction at ...> |
|---|
| 4411 | sage: f(a) |
|---|
| 4412 | [-1, 1] |
|---|
| 4413 | sage: f == loads(dumps(f)) |
|---|
| 4414 | True |
|---|
| 4415 | """ |
|---|
| 4416 | def __init__(self, NumberFieldElement alpha, W, to_V): |
|---|
| 4417 | r""" |
|---|
| 4418 | EXAMPLE:: |
|---|
| 4419 | |
|---|
| 4420 | sage: K.<a> = NumberField(x^2 + x + 3) |
|---|
| 4421 | sage: f = (a + 1).coordinates_in_terms_of_powers(); f # indirect doctest |
|---|
| 4422 | Coordinate function that writes elements in terms of the powers of a + 1 |
|---|
| 4423 | """ |
|---|
| 4424 | self.__alpha = alpha |
|---|
| 4425 | self.__W = W |
|---|
| 4426 | self.__to_V = to_V |
|---|
| 4427 | self.__K = alpha.number_field() |
|---|
| 4428 | |
|---|
| 4429 | def __repr__(self): |
|---|
| 4430 | r""" |
|---|
| 4431 | EXAMPLE:: |
|---|
| 4432 | |
|---|
| 4433 | sage: K.<a> = NumberField(x^2 + x + 3) |
|---|
| 4434 | sage: f = (a + 1).coordinates_in_terms_of_powers(); repr(f) # indirect doctest |
|---|
| 4435 | 'Coordinate function that writes elements in terms of the powers of a + 1' |
|---|
| 4436 | """ |
|---|
| 4437 | return "Coordinate function that writes elements in terms of the powers of %s"%self.__alpha |
|---|
| 4438 | |
|---|
| 4439 | def alpha(self): |
|---|
| 4440 | r""" |
|---|
| 4441 | EXAMPLE:: |
|---|
| 4442 | |
|---|
| 4443 | sage: K.<a> = NumberField(x^3 + 2) |
|---|
| 4444 | sage: (a + 2).coordinates_in_terms_of_powers().alpha() |
|---|
| 4445 | a + 2 |
|---|
| 4446 | """ |
|---|
| 4447 | return self.__alpha |
|---|
| 4448 | |
|---|
| 4449 | def __call__(self, x): |
|---|
| 4450 | r""" |
|---|
| 4451 | EXAMPLE:: |
|---|
| 4452 | |
|---|
| 4453 | sage: K.<a> = NumberField(x^3 + 2) |
|---|
| 4454 | sage: f = (a + 2).coordinates_in_terms_of_powers() |
|---|
| 4455 | sage: f(1/a) |
|---|
| 4456 | [-2, 2, -1/2] |
|---|
| 4457 | sage: f(ZZ(2)) |
|---|
| 4458 | [2, 0, 0] |
|---|
| 4459 | sage: L.<b> = K.extension(x^2 + 7) |
|---|
| 4460 | sage: g = (a + b).coordinates_in_terms_of_powers() |
|---|
| 4461 | sage: g(a/b) |
|---|
| 4462 | [-3379/5461, -371/10922, -4125/38227, -15/5461, -14/5461, -9/76454] |
|---|
| 4463 | sage: g(a) |
|---|
| 4464 | [4459/10922, -4838/5461, -273/5461, -980/5461, -9/10922, -42/5461] |
|---|
| 4465 | sage: f(b) |
|---|
| 4466 | Traceback (most recent call last): |
|---|
| 4467 | ... |
|---|
| 4468 | TypeError: Cannot coerce element into this number field |
|---|
| 4469 | """ |
|---|
| 4470 | from sage.all import parent |
|---|
| 4471 | if not self.__K.has_coerce_map_from(parent(x)): |
|---|
| 4472 | raise TypeError, "Cannot coerce element into this number field" |
|---|
| 4473 | return self.__W.coordinates(self.__to_V(self.__K(x))) |
|---|
| 4474 | |
|---|
| 4475 | def __cmp__(self, other): |
|---|
| 4476 | r""" |
|---|
| 4477 | EXAMPLE:: |
|---|
| 4478 | |
|---|
| 4479 | sage: K.<a> = NumberField(x^4 + 1) |
|---|
| 4480 | sage: f = (a + 1).coordinates_in_terms_of_powers() |
|---|
| 4481 | sage: f == loads(dumps(f)) |
|---|
| 4482 | True |
|---|
| 4483 | sage: f == (a + 2).coordinates_in_terms_of_powers() |
|---|
| 4484 | False |
|---|
| 4485 | sage: f == NumberField(x^2 + 3,'b').gen().coordinates_in_terms_of_powers() |
|---|
| 4486 | False |
|---|
| 4487 | """ |
|---|
| 4488 | return cmp(self.__class__, other.__class__) or cmp(self.__K, other.__K) or cmp(self.__alpha, other.__alpha) |
|---|
| 4489 | |
|---|
| 4490 | |
|---|
| 4491 | |
|---|
| 4492 | ################# |
|---|
| 4493 | |
|---|
| 4494 | cdef void _ntl_poly(f, ZZX_c *num, ZZ_c *den): |
|---|
| 4495 | cdef long i |
|---|
| 4496 | cdef ZZ_c coeff |
|---|
| 4497 | cdef ntl_ZZX _num |
|---|
| 4498 | cdef ntl_ZZ _den |
|---|
| 4499 | |
|---|
| 4500 | __den = f.denominator() |
|---|
| 4501 | (<Integer>ZZ(__den))._to_ZZ(den) |
|---|
| 4502 | |
|---|
| 4503 | __num = f * __den |
|---|
| 4504 | for i from 0 <= i <= __num.degree(): |
|---|
| 4505 | (<Integer>ZZ(__num[i]))._to_ZZ(&coeff) |
|---|
| 4506 | ZZX_SetCoeff( num[0], i, coeff ) |
|---|
| 4507 | |
|---|
| 4508 | |
|---|