| 1 | """ |
| 2 | Number Field Elements |
| 3 | |
| 4 | AUTHORS: |
| 5 | |
| 6 | - William Stein: version before it got Cython'd |
| 7 | - Joel B. Mohler (2007-03-09): First reimplementation in Cython |
| 8 | - William Stein (2007-09-04): add doctests |
| 9 | - Robert Bradshaw (2007-09-15): specialized classes for relative and |
| 10 | absolute elements |
| 11 | - John Cremona (2009-05-15): added support for local and global |
| 12 | logarithmic heights. |
| 13 | - William Stein (2010-07-18): total rewrite/refactoring |
| 14 | """ |
| 15 | |
| 16 | ############################################################################### |
| 17 | # Copyright (C) 2004, 2007, 2010 William Stein <wstein@gmail.com> |
| 18 | # |
| 19 | # Distributed under the terms of the GNU General Public License (GPL) |
| 20 | # |
| 21 | # This code is distributed in the hope that it will be useful, |
| 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | # General Public License for more details. |
| 25 | # |
| 26 | # The full text of the GPL is available at: |
| 27 | # |
| 28 | # http://www.gnu.org/licenses/ |
| 29 | ############################################################################### |
| 30 | |
| 31 | include '../../ext/interrupt.pxi' |
| 32 | include '../../ext/python_int.pxi' |
| 33 | include "../../ext/stdsage.pxi" |
| 34 | |
| 35 | import operator |
| 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 | |
| 57 | QQ = sage.rings.rational_field.QQ |
| 58 | ZZ = sage.rings.integer_ring.ZZ |
| 59 | Integer_sage = sage.rings.integer.Integer |
| 60 | |
| 61 | from sage.rings.real_mpfi import RealInterval |
| 62 | |
| 63 | from sage.rings.complex_field import ComplexField |
| 64 | CC = ComplexField(53) |
| 65 | |
| 66 | # this is a threshold for the charpoly() methods in this file |
| 67 | # for degrees <= this threshold, pari is used |
| 68 | # for degrees > this threshold, sage matrices are used |
| 69 | # the value was decided by running a tuning script on a number of |
| 70 | # architectures; you can find this script attached to trac |
| 71 | # ticket 5213 |
| 72 | TUNE_CHARPOLY_NF = 25 |
| 73 | |
| 74 | def is_NumberFieldElement(x): |
| 75 | """ |
| 76 | Return True if x is of type NumberFieldElement, i.e., an element of |
| 77 | a number field. |
| 78 | |
| 79 | EXAMPLES:: |
| 80 | |
| 81 | sage: from sage.rings.number_field.number_field_element import is_NumberFieldElement |
| 82 | sage: is_NumberFieldElement(2) |
| 83 | False |
| 84 | sage: k.<a> = NumberField(x^7 + 17*x + 1) |
| 85 | sage: is_NumberFieldElement(a+1) |
| 86 | True |
| 87 | """ |
| 88 | return PY_TYPE_CHECK(x, NumberFieldElement) |
| 89 | |
| 90 | def __create__NumberFieldElement_version1(parent, cls, poly): |
| 91 | """ |
| 92 | Used in unpickling elements of number fields. |
| 93 | |
| 94 | EXAMPLES: |
| 95 | |
| 96 | Since this is just used in unpickling, we unpickle. |
| 97 | |
| 98 | :: |
| 99 | |
| 100 | sage: k.<a> = NumberField(x^3 - 2) |
| 101 | sage: loads(dumps(a+1)) == a + 1 |
| 102 | True |
| 103 | |
| 104 | This also gets called for unpickling order elements; we check that #6462 is |
| 105 | fixed:: |
| 106 | |
| 107 | sage: L = NumberField(x^3 - x - 1,'a'); OL = L.maximal_order(); w = OL.0 |
| 108 | sage: loads(dumps(w)) == w |
| 109 | True |
| 110 | """ |
| 111 | return cls(parent, poly) |
| 112 | |
| 113 | def _inverse_mod_generic(elt, I): |
| 114 | r""" |
| 115 | Return an inverse of elt modulo the given ideal. This is a separate |
| 116 | function called from each of the OrderElement_xxx classes, since |
| 117 | otherwise we'd have to have the same code three times over (there |
| 118 | is no OrderElement_generic class - no multiple inheritance). See |
| 119 | trac 4190. |
| 120 | |
| 121 | EXAMPLES:: |
| 122 | |
| 123 | sage: OE = NumberField(x^3 - x + 2, 'w').ring_of_integers() |
| 124 | sage: w = OE.ring_generators()[0] |
| 125 | sage: from sage.rings.number_field.number_field_element import _inverse_mod_generic |
| 126 | sage: _inverse_mod_generic(w, 13*OE) |
| 127 | 6*w^2 - 6 |
| 128 | """ |
| 129 | from sage.matrix.constructor import matrix |
| 130 | R = elt.parent() |
| 131 | try: |
| 132 | I = R.ideal(I) |
| 133 | except ValueError: |
| 134 | raise ValueError, "inverse is only defined modulo integral ideals" |
| 135 | if I == 0: |
| 136 | raise ValueError, "inverse is not defined modulo the zero ideal" |
| 137 | n = R.absolute_degree() |
| 138 | m = matrix(ZZ, map(R.coordinates, I.integral_basis() + [elt*s for s in R.gens()])) |
| 139 | a, b = m.echelon_form(transformation=True) |
| 140 | if a[0:n] != 1: |
| 141 | raise ZeroDivisionError, "%s is not invertible modulo %s" % (elt, I) |
| 142 | v = R.coordinates(1) |
| 143 | y = R(0) |
| 144 | for j in xrange(n): |
| 145 | if v[j] != 0: |
| 146 | y += v[j] * sum([b[j,i+n] * R.gen(i) for i in xrange(n)]) |
| 147 | return I.small_residue(y) |
| 148 | |
| 149 | cdef class NumberFieldElement(FieldElement): |
| 150 | """ |
| 151 | An element of a number field. |
| 152 | |
| 153 | EXAMPLES:: |
| 154 | |
| 155 | sage: k.<a> = NumberField(x^3 + x + 1) |
| 156 | sage: a^3 |
| 157 | -a - 1 |
| 158 | """ |
| 159 | cdef _new(self): |
| 160 | raise NotImplementedError |
| 161 | |
| 162 | cdef number_field(self): |
| 163 | return self._parent |
| 164 | |
| 165 | def _number_field(self): |
| 166 | return self.number_field() |
| 167 | |
| 168 | def _lift_cyclotomic_element(self, new_parent, bint check=True, int rel=0): |
| 169 | """ |
| 170 | Creates an element of the passed field from this field. This is |
| 171 | specific to creating elements in a cyclotomic field from elements |
| 172 | in another cyclotomic field, in the case that |
| 173 | self.number_field()._n() divides new_parent()._n(). This |
| 174 | function aims to make this common coercion extremely fast! |
| 175 | |
| 176 | More general coercion (i.e. of zeta6 into CyclotomicField(3)) is |
| 177 | implemented in the _coerce_from_other_cyclotomic_field method |
| 178 | of a CyclotomicField. |
| 179 | |
| 180 | EXAMPLES:: |
| 181 | |
| 182 | sage: C.<zeta5>=CyclotomicField(5) |
| 183 | sage: CyclotomicField(10)(zeta5+1) # The function _lift_cyclotomic_element does the heavy lifting in the background |
| 184 | zeta10^2 + 1 |
| 185 | sage: (zeta5+1)._lift_cyclotomic_element(CyclotomicField(10)) # There is rarely a purpose to call this function directly |
| 186 | zeta10^2 + 1 |
| 187 | sage: cf4 = CyclotomicField(4) |
| 188 | sage: cf1 = CyclotomicField(1) ; one = cf1.0 |
| 189 | sage: cf4(one) |
| 190 | 1 |
| 191 | sage: type(cf4(1)) |
| 192 | <type 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic'> |
| 193 | sage: cf33 = CyclotomicField(33) ; z33 = cf33.0 |
| 194 | sage: cf66 = CyclotomicField(66) ; z66 = cf66.0 |
| 195 | sage: z33._lift_cyclotomic_element(cf66) |
| 196 | zeta66^2 |
| 197 | sage: z66._lift_cyclotomic_element(cf33) |
| 198 | Traceback (most recent call last): |
| 199 | ... |
| 200 | TypeError: The zeta_order of the new field must be a multiple of the zeta_order of the original. |
| 201 | sage: cf33(z66) |
| 202 | -zeta33^17 |
| 203 | |
| 204 | AUTHORS: |
| 205 | |
| 206 | - Joel B. Mohler |
| 207 | |
| 208 | - Craig Citro (fixed behavior for different representation of |
| 209 | quadratic field elements) |
| 210 | """ |
| 211 | if check: |
| 212 | if not isinstance(self.number_field(), number_field.NumberField_cyclotomic) \ |
| 213 | or not isinstance(new_parent, number_field.NumberField_cyclotomic): |
| 214 | raise TypeError, "The field and the new parent field must both be cyclotomic fields." |
| 215 | |
| 216 | if rel == 0: |
| 217 | small_order = self.number_field()._n() |
| 218 | large_order = new_parent._n() |
| 219 | |
| 220 | try: |
| 221 | rel = ZZ(large_order / small_order) |
| 222 | except TypeError: |
| 223 | raise TypeError, "The zeta_order of the new field must be a multiple of the zeta_order of the original." |
| 224 | |
| 225 | ## degree 2 is handled differently, because elements are |
| 226 | ## represented differently |
| 227 | if new_parent.degree() == 2: |
| 228 | return new_parent._element_class(new_parent, self) |
| 229 | |
| 230 | # TODO -- rewrite; see original number_field_element.pyx function |
| 231 | raise NotImplementedError |
| 232 | |
| 233 | def __reduce__(self): |
| 234 | """ |
| 235 | Used in pickling number field elements. |
| 236 | |
| 237 | EXAMPLES:: |
| 238 | |
| 239 | sage: k.<a> = NumberField(x^3 - 17*x^2 + 1) |
| 240 | sage: t = a.__reduce__(); t |
| 241 | (<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)) |
| 242 | sage: t[0](*t[1]) == a |
| 243 | True |
| 244 | """ |
| 245 | return __create__NumberFieldElement_version1, \ |
| 246 | (self.parent(), type(self), self.polynomial()) |
| 247 | |
| 248 | def __repr__(self): |
| 249 | """ |
| 250 | String representation of this number field element, which is just a |
| 251 | polynomial in the generator. |
| 252 | |
| 253 | EXAMPLES:: |
| 254 | |
| 255 | sage: k.<a> = NumberField(x^2 + 2) |
| 256 | sage: b = (2/3)*a + 3/5 |
| 257 | sage: b.__repr__() |
| 258 | '2/3*a + 3/5' |
| 259 | """ |
| 260 | x = self.polynomial() |
| 261 | K = self.number_field() |
| 262 | return str(x).replace(x.parent().variable_name(), K.variable_name()) |
| 263 | |
| 264 | def _im_gens_(self, codomain, im_gens): |
| 265 | """ |
| 266 | This is used in computing homomorphisms between number fields. |
| 267 | |
| 268 | EXAMPLES:: |
| 269 | |
| 270 | sage: k.<a> = NumberField(x^2 - 2) |
| 271 | sage: m.<b> = NumberField(x^4 - 2) |
| 272 | sage: phi = k.hom([b^2]) |
| 273 | sage: phi(a+1) |
| 274 | b^2 + 1 |
| 275 | sage: (a+1)._im_gens_(m, [b^2]) |
| 276 | b^2 + 1 |
| 277 | """ |
| 278 | # NOTE -- if you ever want to change this so relative number |
| 279 | # fields are in terms of a root of a poly. The issue is that |
| 280 | # elements of a relative number field are represented in terms |
| 281 | # of a generator for the absolute field. However the morphism |
| 282 | # gives the image of gen, which need not be a generator for |
| 283 | # the absolute field. The morphism has to be *over* the |
| 284 | # relative element. |
| 285 | return codomain(self.polynomial()(im_gens[0])) |
| 286 | |
| 287 | def _latex_(self): |
| 288 | """ |
| 289 | Returns the latex representation for this element. |
| 290 | |
| 291 | EXAMPLES:: |
| 292 | |
| 293 | sage: C,zeta12=CyclotomicField(12).objgen() |
| 294 | sage: latex(zeta12^4-zeta12) |
| 295 | \zeta_{12}^{2} - \zeta_{12} - 1 |
| 296 | """ |
| 297 | return self.polynomial()._latex_(name=self.number_field().latex_variable_name()) |
| 298 | |
| 299 | def _gap_init_(self): |
| 300 | """ |
| 301 | Return gap string representation of self. |
| 302 | |
| 303 | EXAMPLES:: |
| 304 | |
| 305 | sage: F=CyclotomicField(8) |
| 306 | sage: p=F.gen()^2+2*F.gen()-3 |
| 307 | sage: p |
| 308 | zeta8^2 + 2*zeta8 - 3 |
| 309 | sage: p._gap_init_() # The variable name $sage2 belongs to the gap(F) and is somehow random |
| 310 | 'GeneratorsOfField($sage2)[1]^2 + 2*GeneratorsOfField($sage2)[1] - 3' |
| 311 | sage: gap(p._gap_init_()) |
| 312 | zeta8^2+2*zeta8-3 |
| 313 | """ |
| 314 | s = self.__repr__() |
| 315 | c = 'GeneratorsOfField(%s)[1]'%sage.interfaces.gap.gap(self.parent()).name() |
| 316 | return s.replace(str(self.parent().gen()), c) |
| 317 | |
| 318 | def _pari_(self, var='x'): |
| 319 | """ |
| 320 | EXAMPLES:: |
| 321 | |
| 322 | sage: ? |
| 323 | """ |
| 324 | raise NotImplementedError, "NumberFieldElement sub-classes must override _pari_" |
| 325 | |
| 326 | def _pari_init_(self, var='x'): |
| 327 | """ |
| 328 | Return GP/PARI string representation of self. This is used for |
| 329 | converting this number field element to GP/PARI. The returned |
| 330 | string defines a pari Mod in the variable is var, which is by |
| 331 | default 'x' - not the name of the generator of the number field. |
| 332 | |
| 333 | INPUT: |
| 334 | - ``var`` - (default: 'x') the variable of the pari Mod. |
| 335 | |
| 336 | |
| 337 | EXAMPLES:: |
| 338 | |
| 339 | sage: K.<a> = NumberField(x^5 - x - 1) |
| 340 | sage: ((1 + 1/3*a)^4)._pari_init_() |
| 341 | 'Mod(1/81*x^4 + 4/27*x^3 + 2/3*x^2 + 4/3*x + 1, x^5 - x - 1)' |
| 342 | sage: ((1 + 1/3*a)^4)._pari_init_('a') |
| 343 | 'Mod(1/81*a^4 + 4/27*a^3 + 2/3*a^2 + 4/3*a + 1, a^5 - a - 1)' |
| 344 | |
| 345 | Note that _pari_init_ can fail because of reserved words in |
| 346 | PARI, and since it actually works by obtaining the PARI |
| 347 | representation of something. |
| 348 | |
| 349 | :: |
| 350 | |
| 351 | sage: K.<theta> = NumberField(x^5 - x - 1) |
| 352 | sage: b = (1/2 - 2/3*theta)^3; b |
| 353 | -8/27*theta^3 + 2/3*theta^2 - 1/2*theta + 1/8 |
| 354 | sage: b._pari_init_('theta') |
| 355 | Traceback (most recent call last): |
| 356 | ... |
| 357 | PariError: unexpected character (2) |
| 358 | |
| 359 | Fortunately pari_init returns everything in terms of x by |
| 360 | default. |
| 361 | |
| 362 | :: |
| 363 | sage: pari(b) |
| 364 | Mod(-8/27*x^3 + 2/3*x^2 - 1/2*x + 1/8, x^5 - x - 1) |
| 365 | """ |
| 366 | return repr(self._pari_(var=var)) |
| 367 | |
| 368 | def __getitem__(self, n): |
| 369 | """ |
| 370 | Return the n-th coefficient of this number field element, |
| 371 | written as a polynomial in the generator. |
| 372 | |
| 373 | Note that `n` must be between 0 and `d-1`, where `d` is the |
| 374 | degree of the number field. |
| 375 | |
| 376 | EXAMPLES:: |
| 377 | |
| 378 | sage: m.<b> = NumberField(x^4 - 1789) |
| 379 | sage: c = (2/3-4/5*b)^3; c |
| 380 | -64/125*b^3 + 32/25*b^2 - 16/15*b + 8/27 |
| 381 | sage: c[0] |
| 382 | 8/27 |
| 383 | sage: c[2] |
| 384 | 32/25 |
| 385 | sage: c[3] |
| 386 | -64/125 |
| 387 | |
| 388 | We illustrate bounds checking:: |
| 389 | |
| 390 | sage: c[-1] |
| 391 | Traceback (most recent call last): |
| 392 | ... |
| 393 | IndexError: index must be between 0 and degree minus 1. |
| 394 | sage: c[4] |
| 395 | Traceback (most recent call last): |
| 396 | ... |
| 397 | IndexError: index must be between 0 and degree minus 1. |
| 398 | |
| 399 | The list method implicitly calls ``__getitem__``:: |
| 400 | |
| 401 | sage: list(c) |
| 402 | [8/27, -16/15, 32/25, -64/125] |
| 403 | sage: m(list(c)) == c |
| 404 | True |
| 405 | """ |
| 406 | if n < 0 or n >= self.number_field().degree(): # make this faster. |
| 407 | raise IndexError, "index must be between 0 and degree minus 1." |
| 408 | return self.polynomial()[n] |
| 409 | |
| 410 | def __richcmp__(left, right, int op): |
| 411 | return (<Element>left)._richcmp(right, op) |
| 412 | |
| 413 | cdef int _cmp_c_impl(left, sage.structure.element.Element right) except -2: |
| 414 | raise NotImplementedError, "derived class must implement" |
| 415 | |
| 416 | def _random_element(self, num_bound=None, den_bound=None, distribution=None): |
| 417 | """ |
| 418 | Return a new random element with the same parent as self. |
| 419 | |
| 420 | INPUT: |
| 421 | - ``num_bound`` - Bound for the numerator of coefficients of result |
| 422 | - ``den_bound`` - Bound for the denominator of coefficients of result |
| 423 | - ``distribution`` - Distribution to use for coefficients of result |
| 424 | |
| 425 | EXAMPLES:: |
| 426 | |
| 427 | sage: K.<a> = NumberField(x^3-2) |
| 428 | sage: a._random_element() |
| 429 | -1/2*a^2 - 4 |
| 430 | sage: K.<a> = NumberField(x^2-5) |
| 431 | sage: a._random_element() |
| 432 | -2*a - 1 |
| 433 | """ |
| 434 | cdef NumberFieldElement elt = self._new() |
| 435 | elt._randomize(num_bound, den_bound, distribution) |
| 436 | return elt |
| 437 | |
| 438 | cdef void _randomize(self, num_bound, den_bound, distribution): |
| 439 | raise NotImplementedError, "derived class must implement" |
| 440 | |
| 441 | def __abs__(self): |
| 442 | r""" |
| 443 | Return the numerical absolute value of this number field element |
| 444 | with respect to the first archimedean embedding, to double |
| 445 | precision. |
| 446 | |
| 447 | This is the ``abs( )`` Python function. If you want a |
| 448 | different embedding or precision, use |
| 449 | ``self.abs(...)``. |
| 450 | |
| 451 | EXAMPLES:: |
| 452 | |
| 453 | sage: k.<a> = NumberField(x^3 - 2) |
| 454 | sage: abs(a) |
| 455 | 1.25992104989487 |
| 456 | sage: abs(a)^3 |
| 457 | 2.00000000000000 |
| 458 | sage: a.abs(prec=128) |
| 459 | 1.2599210498948731647672106072782283506 |
| 460 | """ |
| 461 | return self.abs(prec=53, i=0) |
| 462 | |
| 463 | def abs(self, prec=53, i=0): |
| 464 | r""" |
| 465 | Return the absolute value of this element with respect to the |
| 466 | `i`-th complex embedding of parent, to the given precision. |
| 467 | |
| 468 | If prec is 53 (the default), then the complex double field is |
| 469 | used; otherwise the arbitrary precision (but slow) complex |
| 470 | field is used. |
| 471 | |
| 472 | INPUT: |
| 473 | |
| 474 | |
| 475 | - ``prec`` - (default: 53) integer bits of precision |
| 476 | |
| 477 | - ``i`` - (default: ) integer, which embedding to |
| 478 | use |
| 479 | |
| 480 | |
| 481 | EXAMPLES:: |
| 482 | |
| 483 | sage: z = CyclotomicField(7).gen() |
| 484 | sage: abs(z) |
| 485 | 1.00000000000000 |
| 486 | sage: abs(z^2 + 17*z - 3) |
| 487 | 16.0604426799931 |
| 488 | sage: K.<a> = NumberField(x^3+17) |
| 489 | sage: abs(a) |
| 490 | 2.57128159065824 |
| 491 | sage: a.abs(prec=100) |
| 492 | 2.5712815906582353554531872087 |
| 493 | sage: a.abs(prec=100,i=1) |
| 494 | 2.5712815906582353554531872087 |
| 495 | sage: a.abs(100, 2) |
| 496 | 2.5712815906582353554531872087 |
| 497 | |
| 498 | Here's one where the absolute value depends on the embedding. |
| 499 | |
| 500 | :: |
| 501 | |
| 502 | sage: K.<b> = NumberField(x^2-2) |
| 503 | sage: a = 1 + b |
| 504 | sage: a.abs(i=0) |
| 505 | 0.414213562373095 |
| 506 | sage: a.abs(i=1) |
| 507 | 2.41421356237309 |
| 508 | """ |
| 509 | P = self.number_field().complex_embeddings(prec)[i] |
| 510 | return abs(P(self)) |
| 511 | |
| 512 | def abs_non_arch(self, P, prec=None): |
| 513 | r""" |
| 514 | Return the non-archimedean absolute value of this element with |
| 515 | respect to the prime `P`, to the given precision. |
| 516 | |
| 517 | INPUT: |
| 518 | |
| 519 | - ``P`` - a prime ideal of the parent of self |
| 520 | |
| 521 | - ``prec`` (int) -- desired floating point precision (default: |
| 522 | default RealField precision). |
| 523 | |
| 524 | OUTPUT: |
| 525 | |
| 526 | (real) the non-archimedean absolute value of this element with |
| 527 | respect to the prime `P`, to the given precision. This is the |
| 528 | normalised absolute value, so that the underlying prime number |
| 529 | `p` has absolute value `1/p`. |
| 530 | |
| 531 | |
| 532 | EXAMPLES:: |
| 533 | |
| 534 | sage: K.<a> = NumberField(x^2+5) |
| 535 | sage: [1/K(2).abs_non_arch(P) for P in K.primes_above(2)] |
| 536 | [2.00000000000000] |
| 537 | sage: [1/K(3).abs_non_arch(P) for P in K.primes_above(3)] |
| 538 | [3.00000000000000, 3.00000000000000] |
| 539 | sage: [1/K(5).abs_non_arch(P) for P in K.primes_above(5)] |
| 540 | [5.00000000000000] |
| 541 | |
| 542 | A relative example:: |
| 543 | |
| 544 | sage: L.<b> = K.extension(x^2-5) |
| 545 | sage: [b.abs_non_arch(P) for P in L.primes_above(b)] |
| 546 | [0.447213595499958, 0.447213595499958] |
| 547 | """ |
| 548 | from sage.rings.real_mpfr import RealField |
| 549 | if prec is None: |
| 550 | R = RealField() |
| 551 | else: |
| 552 | R = RealField(prec) |
| 553 | |
| 554 | if self.is_zero(): |
| 555 | return R.zero_element() |
| 556 | val = self.valuation(P) |
| 557 | nP = P.residue_class_degree()*P.absolute_ramification_index() |
| 558 | return R(P.absolute_norm()) ** (-R(val) / R(nP)) |
| 559 | |
| 560 | def coordinates_in_terms_of_powers(self): |
| 561 | r""" |
| 562 | Let `\alpha` be self. Return a Python function that takes |
| 563 | any element of the parent of self in `\QQ(\alpha)` |
| 564 | and writes it in terms of the powers of `\alpha`: |
| 565 | `1, \alpha, \alpha^2, ...`. |
| 566 | |
| 567 | (NOT CACHED). |
| 568 | |
| 569 | EXAMPLES: |
| 570 | |
| 571 | This function allows us to write elements of a number |
| 572 | field in terms of a different generator without having to construct |
| 573 | a whole separate number field. |
| 574 | |
| 575 | :: |
| 576 | |
| 577 | sage: y = polygen(QQ,'y'); K.<beta> = NumberField(y^3 - 2); K |
| 578 | Number Field in beta with defining polynomial y^3 - 2 |
| 579 | sage: alpha = beta^2 + beta + 1 |
| 580 | sage: c = alpha.coordinates_in_terms_of_powers(); c |
| 581 | Coordinate function that writes elements in terms of the powers of beta^2 + beta + 1 |
| 582 | sage: c(beta) |
| 583 | [-2, -3, 1] |
| 584 | sage: c(alpha) |
| 585 | [0, 1, 0] |
| 586 | sage: c((1+beta)^5) |
| 587 | [3, 3, 3] |
| 588 | sage: c((1+beta)^10) |
| 589 | [54, 162, 189] |
| 590 | |
| 591 | This function works even if self only generates a subfield of this |
| 592 | number field. |
| 593 | |
| 594 | :: |
| 595 | |
| 596 | sage: k.<a> = NumberField(x^6 - 5) |
| 597 | sage: alpha = a^3 |
| 598 | sage: c = alpha.coordinates_in_terms_of_powers() |
| 599 | sage: c((2/3)*a^3 - 5/3) |
| 600 | [-5/3, 2/3] |
| 601 | sage: c |
| 602 | Coordinate function that writes elements in terms of the powers of a^3 |
| 603 | sage: c(a) |
| 604 | Traceback (most recent call last): |
| 605 | ... |
| 606 | ArithmeticError: vector is not in free module |
| 607 | """ |
| 608 | K = self.number_field() |
| 609 | V, from_V, to_V = K.absolute_vector_space() |
| 610 | h = K(1) |
| 611 | B = [to_V(h)] |
| 612 | f = self.minpoly() |
| 613 | for i in range(f.degree()-1): |
| 614 | h *= self |
| 615 | B.append(to_V(h)) |
| 616 | W = V.span_of_basis(B) |
| 617 | return CoordinateFunction(self, W, to_V) |
| 618 | |
| 619 | def complex_embeddings(self, prec=53): |
| 620 | """ |
| 621 | Return the images of this element in the floating point complex |
| 622 | numbers, to the given bits of precision. |
| 623 | |
| 624 | INPUT: |
| 625 | |
| 626 | |
| 627 | - ``prec`` - integer (default: 53) bits of precision |
| 628 | |
| 629 | |
| 630 | EXAMPLES:: |
| 631 | |
| 632 | sage: k.<a> = NumberField(x^3 - 2) |
| 633 | sage: a.complex_embeddings() |
| 634 | [-0.629960524947437 - 1.09112363597172*I, -0.629960524947437 + 1.09112363597172*I, 1.25992104989487] |
| 635 | sage: a.complex_embeddings(10) |
| 636 | [-0.63 - 1.1*I, -0.63 + 1.1*I, 1.3] |
| 637 | sage: a.complex_embeddings(100) |
| 638 | [-0.62996052494743658238360530364 - 1.0911236359717214035600726142*I, -0.62996052494743658238360530364 + 1.0911236359717214035600726142*I, 1.2599210498948731647672106073] |
| 639 | """ |
| 640 | phi = self.number_field().complex_embeddings(prec) |
| 641 | return [f(self) for f in phi] |
| 642 | |
| 643 | def complex_embedding(self, prec=53, i=0): |
| 644 | """ |
| 645 | Return the i-th embedding of self in the complex numbers, to the |
| 646 | given precision. |
| 647 | |
| 648 | EXAMPLES:: |
| 649 | |
| 650 | sage: k.<a> = NumberField(x^3 - 2) |
| 651 | sage: a.complex_embedding() |
| 652 | -0.629960524947437 - 1.09112363597172*I |
| 653 | sage: a.complex_embedding(10) |
| 654 | -0.63 - 1.1*I |
| 655 | sage: a.complex_embedding(100) |
| 656 | -0.62996052494743658238360530364 - 1.0911236359717214035600726142*I |
| 657 | sage: a.complex_embedding(20, 1) |
| 658 | -0.62996 + 1.0911*I |
| 659 | sage: a.complex_embedding(20, 2) |
| 660 | 1.2599 |
| 661 | """ |
| 662 | return self.number_field().complex_embeddings(prec)[i](self) |
| 663 | |
| 664 | def _mpfr_(self, R): |
| 665 | """ |
| 666 | EXAMPLES:: |
| 667 | |
| 668 | sage: k.<a> = NumberField(x^2 + 1) |
| 669 | sage: RR(a^2) |
| 670 | -1.00000000000000 |
| 671 | sage: RR(a) |
| 672 | Traceback (most recent call last): |
| 673 | ... |
| 674 | TypeError: cannot convert a to real number |
| 675 | |
| 676 | sage: (a^2)._mpfr_(RR) |
| 677 | -1.00000000000000 |
| 678 | """ |
| 679 | C = R.complex_field() |
| 680 | tres = C(self) |
| 681 | try: |
| 682 | return R(tres) |
| 683 | except TypeError: |
| 684 | raise TypeError, "cannot convert %s to real number"%(self) |
| 685 | |
| 686 | def _complex_double_(self, CDF): |
| 687 | """ |
| 688 | EXAMPLES:: |
| 689 | |
| 690 | sage: k.<a> = NumberField(x^2 + 1) |
| 691 | sage: abs(CDF(a)) |
| 692 | 1.0 |
| 693 | """ |
| 694 | return CDF(CC(self)) |
| 695 | |
| 696 | def __complex__(self): |
| 697 | """ |
| 698 | EXAMPLES:: |
| 699 | |
| 700 | sage: k.<a> = NumberField(x^2 + 1) |
| 701 | sage: complex(a) |
| 702 | 1j |
| 703 | sage: a.__complex__() |
| 704 | 1j |
| 705 | """ |
| 706 | return complex(CC(self)) |
| 707 | |
| 708 | def is_totally_positive(self): |
| 709 | """ |
| 710 | Returns True if self is positive for all real embeddings of its |
| 711 | parent number field. We do nothing at complex places, so e.g. any |
| 712 | element of a totally complex number field will return True. |
| 713 | |
| 714 | EXAMPLES:: |
| 715 | |
| 716 | sage: F.<b> = NumberField(x^3-3*x-1) |
| 717 | sage: b.is_totally_positive() |
| 718 | False |
| 719 | sage: (b^2).is_totally_positive() |
| 720 | True |
| 721 | """ |
| 722 | for v in self.number_field().real_embeddings(): |
| 723 | if v(self) <= 0: |
| 724 | return False |
| 725 | return True |
| 726 | |
| 727 | def is_square(self, root=False): |
| 728 | """ |
| 729 | Return True if self is a square in its parent number field and |
| 730 | otherwise return False. |
| 731 | |
| 732 | INPUT: |
| 733 | |
| 734 | |
| 735 | - ``root`` - if True, also return a square root (or |
| 736 | None if self is not a perfect square) |
| 737 | |
| 738 | |
| 739 | EXAMPLES:: |
| 740 | |
| 741 | sage: m.<b> = NumberField(x^4 - 1789) |
| 742 | sage: b.is_square() |
| 743 | False |
| 744 | sage: c = (2/3*b + 5)^2; c |
| 745 | 4/9*b^2 + 20/3*b + 25 |
| 746 | sage: c.is_square() |
| 747 | True |
| 748 | sage: c.is_square(True) |
| 749 | (True, 2/3*b + 5) |
| 750 | |
| 751 | We also test the functional notation. |
| 752 | |
| 753 | :: |
| 754 | |
| 755 | sage: is_square(c, True) |
| 756 | (True, 2/3*b + 5) |
| 757 | sage: is_square(c) |
| 758 | True |
| 759 | sage: is_square(c+1) |
| 760 | False |
| 761 | """ |
| 762 | v = self.sqrt(all=True) |
| 763 | t = len(v) > 0 |
| 764 | if root: |
| 765 | if t: |
| 766 | return t, v[0] |
| 767 | else: |
| 768 | return False, None |
| 769 | else: |
| 770 | return t |
| 771 | |
| 772 | def sqrt(self, all=False): |
| 773 | """ |
| 774 | Returns the square root of this number in the given number field. |
| 775 | |
| 776 | EXAMPLES:: |
| 777 | |
| 778 | sage: K.<a> = NumberField(x^2 - 3) |
| 779 | sage: K(3).sqrt() |
| 780 | a |
| 781 | sage: K(3).sqrt(all=True) |
| 782 | [a, -a] |
| 783 | sage: K(a^10).sqrt() |
| 784 | 9*a |
| 785 | sage: K(49).sqrt() |
| 786 | 7 |
| 787 | sage: K(1+a).sqrt() |
| 788 | Traceback (most recent call last): |
| 789 | ... |
| 790 | ValueError: a + 1 not a square in Number Field in a with defining polynomial x^2 - 3 |
| 791 | sage: K(0).sqrt() |
| 792 | 0 |
| 793 | sage: K((7+a)^2).sqrt(all=True) |
| 794 | [a + 7, -a - 7] |
| 795 | |
| 796 | :: |
| 797 | |
| 798 | sage: K.<a> = CyclotomicField(7) |
| 799 | sage: a.sqrt() |
| 800 | a^4 |
| 801 | |
| 802 | :: |
| 803 | |
| 804 | sage: K.<a> = NumberField(x^5 - x + 1) |
| 805 | sage: (a^4 + a^2 - 3*a + 2).sqrt() |
| 806 | a^3 - a^2 |
| 807 | |
| 808 | ALGORITHM: Use Pari to factor `x^2` - ``self`` |
| 809 | in K. |
| 810 | """ |
| 811 | # For now, use pari's factoring abilities |
| 812 | R = self.number_field()['t'] |
| 813 | f = R([-self, 0, 1]) |
| 814 | roots = f.roots() |
| 815 | if all: |
| 816 | return [r[0] for r in roots] |
| 817 | elif len(roots) > 0: |
| 818 | return roots[0][0] |
| 819 | else: |
| 820 | try: |
| 821 | # This is what integers, rationals do... |
| 822 | from sage.all import SR, sqrt |
| 823 | return sqrt(SR(self)) |
| 824 | except TypeError: |
| 825 | raise ValueError, "%s not a square in %s"%(self, self._parent) |
| 826 | |
| 827 | def nth_root(self, n, all=False): |
| 828 | r""" |
| 829 | Return an nth root of self in the given number field. |
| 830 | |
| 831 | EXAMPLES:: |
| 832 | |
| 833 | sage: K.<a> = NumberField(x^4-7) |
| 834 | sage: K(7).nth_root(2) |
| 835 | a^2 |
| 836 | sage: K((a-3)^5).nth_root(5) |
| 837 | a - 3 |
| 838 | |
| 839 | ALGORITHM: Use Pari to factor `x^n` - ``self`` |
| 840 | in K. |
| 841 | """ |
| 842 | R = self.number_field()['t'] |
| 843 | if not self: |
| 844 | return [self] if all else self |
| 845 | f = (R.gen(0) << (n-1)) - self |
| 846 | roots = f.roots() |
| 847 | if all: |
| 848 | return [r[0] for r in roots] |
| 849 | elif len(roots) > 0: |
| 850 | return roots[0][0] |
| 851 | else: |
| 852 | raise ValueError, "%s not a %s-th root in %s"%(self, n, self._parent) |
| 853 | |
| 854 | def __pow__(base, exp, dummy): |
| 855 | """ |
| 856 | EXAMPLES:: |
| 857 | |
| 858 | sage: K.<sqrt2> = QuadraticField(2) |
| 859 | sage: sqrt2^2 |
| 860 | 2 |
| 861 | sage: sqrt2^5 |
| 862 | 4*sqrt2 |
| 863 | sage: (1+sqrt2)^100 |
| 864 | 66992092050551637663438906713182313772*sqrt2 + 94741125149636933417873079920900017937 |
| 865 | sage: (1+sqrt2)^-1 |
| 866 | sqrt2 - 1 |
| 867 | |
| 868 | If the exponent is not integral, perform this operation in |
| 869 | the symbolic ring:: |
| 870 | |
| 871 | sage: sqrt2^(1/5) |
| 872 | 2^(1/10) |
| 873 | sage: sqrt2^sqrt2 |
| 874 | 2^(1/2*sqrt(2)) |
| 875 | |
| 876 | TESTS:: |
| 877 | |
| 878 | sage: 2^I |
| 879 | 2^I |
| 880 | """ |
| 881 | if (PY_TYPE_CHECK(base, NumberFieldElement) and |
| 882 | (PY_TYPE_CHECK(exp, Integer) or PY_TYPE_CHECK_EXACT(exp, int) or exp in ZZ)): |
| 883 | return generic_power_c(base, exp, None) |
| 884 | else: |
| 885 | from sage.symbolic.power_helper import try_symbolic_power |
| 886 | return try_symbolic_power(base, exp) |
| 887 | |
| 888 | def __floordiv__(self, other): |
| 889 | """ |
| 890 | Return the quotient of self and other. Since these are field |
| 891 | elements the floor division is exactly the same as usual division. |
| 892 | |
| 893 | EXAMPLES:: |
| 894 | |
| 895 | sage: m.<b> = NumberField(x^4 + x^2 + 2/3) |
| 896 | sage: c = (1+b) // (1-b); c |
| 897 | 3/4*b^3 + 3/4*b^2 + 3/2*b + 1/2 |
| 898 | sage: (1+b) / (1-b) == c |
| 899 | True |
| 900 | sage: c * (1-b) |
| 901 | b + 1 |
| 902 | """ |
| 903 | return self / other |
| 904 | |
| 905 | def __nonzero__(self): |
| 906 | """ |
| 907 | Return True if this number field element is nonzero. |
| 908 | |
| 909 | EXAMPLES:: |
| 910 | |
| 911 | sage: m.<b> = CyclotomicField(17) |
| 912 | sage: m(0).__nonzero__() |
| 913 | False |
| 914 | sage: b.__nonzero__() |
| 915 | True |
| 916 | |
| 917 | Nonzero is used by the bool command:: |
| 918 | |
| 919 | sage: bool(b + 1) |
| 920 | True |
| 921 | sage: bool(m(0)) |
| 922 | False |
| 923 | """ |
| 924 | # Should be overloaded in derived class! |
| 925 | return self != 0 |
| 926 | |
| 927 | def __int__(self): |
| 928 | """ |
| 929 | Attempt to convert this number field element to a Python integer, |
| 930 | if possible. |
| 931 | |
| 932 | EXAMPLES:: |
| 933 | |
| 934 | sage: C.<I>=CyclotomicField(4) |
| 935 | sage: int(1/I) |
| 936 | Traceback (most recent call last): |
| 937 | ... |
| 938 | TypeError: cannot coerce nonconstant polynomial to int |
| 939 | sage: int(I*I) |
| 940 | -1 |
| 941 | |
| 942 | :: |
| 943 | |
| 944 | sage: K.<a> = NumberField(x^10 - x - 1) |
| 945 | sage: int(a) |
| 946 | Traceback (most recent call last): |
| 947 | ... |
| 948 | TypeError: cannot coerce nonconstant polynomial to int |
| 949 | sage: int(K(9390283)) |
| 950 | 9390283 |
| 951 | |
| 952 | The semantics are like in Python, so the value does not have to |
| 953 | preserved. |
| 954 | |
| 955 | :: |
| 956 | |
| 957 | sage: int(K(393/29)) |
| 958 | 13 |
| 959 | """ |
| 960 | return int(self.polynomial()) |
| 961 | |
| 962 | def __long__(self): |
| 963 | """ |
| 964 | Attempt to convert this number field element to a Python long, if |
| 965 | possible. |
| 966 | |
| 967 | EXAMPLES:: |
| 968 | |
| 969 | sage: K.<a> = NumberField(x^10 - x - 1) |
| 970 | sage: long(a) |
| 971 | Traceback (most recent call last): |
| 972 | ... |
| 973 | TypeError: cannot coerce nonconstant polynomial to long |
| 974 | sage: long(K(1234)) |
| 975 | 1234L |
| 976 | |
| 977 | The value does not have to be preserved, in the case of fractions. |
| 978 | |
| 979 | :: |
| 980 | |
| 981 | sage: long(K(393/29)) |
| 982 | 13L |
| 983 | """ |
| 984 | return long(self.polynomial()) |
| 985 | |
| 986 | def __invert__(self): |
| 987 | """ |
| 988 | Returns the multiplicative inverse of self in the number field. |
| 989 | |
| 990 | EXAMPLES:: |
| 991 | |
| 992 | sage: C.<I>=CyclotomicField(4) |
| 993 | sage: ~I |
| 994 | -I |
| 995 | sage: (2*I).__invert__() |
| 996 | -1/2*I |
| 997 | """ |
| 998 | raise NotImplementedError |
| 999 | |
| 1000 | def _integer_(self, Z=None): |
| 1001 | """ |
| 1002 | Returns an integer if this element is actually an integer. |
| 1003 | |
| 1004 | EXAMPLES:: |
| 1005 | |
| 1006 | sage: C.<I>=CyclotomicField(4) |
| 1007 | sage: (~I)._integer_() |
| 1008 | Traceback (most recent call last): |
| 1009 | ... |
| 1010 | TypeError: Unable to coerce -I to an integer |
| 1011 | sage: (2*I*I)._integer_() |
| 1012 | -2 |
| 1013 | """ |
| 1014 | raise NotImplementedError |
| 1015 | |
| 1016 | |
| 1017 | def _rational_(self): |
| 1018 | """ |
| 1019 | Returns a rational number if this element is actually a rational |
| 1020 | number. |
| 1021 | |
| 1022 | EXAMPLES:: |
| 1023 | |
| 1024 | sage: C.<I>=CyclotomicField(4) |
| 1025 | sage: (~I)._rational_() |
| 1026 | Traceback (most recent call last): |
| 1027 | ... |
| 1028 | TypeError: Unable to coerce -I to a rational |
| 1029 | sage: (I*I/2)._rational_() |
| 1030 | -1/2 |
| 1031 | """ |
| 1032 | raise NotImplementedError |
| 1033 | |
| 1034 | def _symbolic_(self, SR): |
| 1035 | """ |
| 1036 | If an embedding into CC is specified, then a representation of this |
| 1037 | element can be made in the symbolic ring (assuming roots of the |
| 1038 | minimal polynomial can be found symbolically). |
| 1039 | |
| 1040 | EXAMPLES:: |
| 1041 | |
| 1042 | sage: K.<a> = QuadraticField(2) |
| 1043 | sage: SR(a) |
| 1044 | sqrt(2) |
| 1045 | sage: SR(3*a-5) |
| 1046 | 3*sqrt(2) - 5 |
| 1047 | sage: K.<a> = QuadraticField(2, embedding=-1.4) |
| 1048 | sage: SR(a) |
| 1049 | -sqrt(2) |
| 1050 | sage: K.<a> = NumberField(x^2 - 2) |
| 1051 | sage: SR(a) |
| 1052 | Traceback (most recent call last): |
| 1053 | ... |
| 1054 | TypeError: An embedding into RR or CC must be specified. |
| 1055 | |
| 1056 | Now a more complicated example:: |
| 1057 | |
| 1058 | sage: K.<a> = NumberField(x^3 + x - 1, embedding=0.68) |
| 1059 | sage: b = SR(a); b |
| 1060 | 1/3*(3*(1/18*sqrt(3)*sqrt(31) + 1/2)^(2/3) - 1)/(1/18*sqrt(3)*sqrt(31) + 1/2)^(1/3) |
| 1061 | |
| 1062 | sage: (b^3 + b - 1).simplify_radical() |
| 1063 | 0 |
| 1064 | |
| 1065 | Make sure we got the right one:: |
| 1066 | |
| 1067 | sage: CC(a) |
| 1068 | 0.682327803828019 |
| 1069 | sage: CC(b) |
| 1070 | 0.682327803828019 |
| 1071 | |
| 1072 | Special case for cyclotomic fields:: |
| 1073 | |
| 1074 | sage: K.<zeta> = CyclotomicField(19) |
| 1075 | sage: SR(zeta) |
| 1076 | e^(2/19*I*pi) |
| 1077 | sage: CC(zeta) |
| 1078 | 0.945817241700635 + 0.324699469204683*I |
| 1079 | sage: CC(SR(zeta)) |
| 1080 | 0.945817241700635 + 0.324699469204683*I |
| 1081 | |
| 1082 | sage: SR(zeta^5 + 2) |
| 1083 | e^(10/19*I*pi) + 2 |
| 1084 | |
| 1085 | For degree greater than 5, sometimes Galois theory prevents a |
| 1086 | closed-form solution. In this case, a numerical approximation |
| 1087 | is used:: |
| 1088 | |
| 1089 | sage: K.<a> = NumberField(x^5-x+1, embedding=-1) |
| 1090 | sage: SR(a) |
| 1091 | -1.1673040153 |
| 1092 | |
| 1093 | :: |
| 1094 | |
| 1095 | sage: K.<a> = NumberField(x^6-x^3-1, embedding=1) |
| 1096 | sage: SR(a) |
| 1097 | 1/2*(sqrt(5) + 1)^(1/3)*2^(2/3) |
| 1098 | """ |
| 1099 | if self.__symbolic is None: |
| 1100 | |
| 1101 | K = self._parent.fraction_field() |
| 1102 | |
| 1103 | gen = K.gen() |
| 1104 | if not self is gen: |
| 1105 | try: |
| 1106 | # share the hard work... |
| 1107 | gen_image = gen._symbolic_(SR) |
| 1108 | self.__symbolic = self.polynomial()(gen_image) |
| 1109 | return self.__symbolic |
| 1110 | except TypeError: |
| 1111 | pass # we may still be able to do this particular element... |
| 1112 | |
| 1113 | embedding = K.specified_complex_embedding() |
| 1114 | if embedding is None: |
| 1115 | raise TypeError, "An embedding into RR or CC must be specified." |
| 1116 | |
| 1117 | if isinstance(K, number_field.NumberField_cyclotomic): |
| 1118 | # solution by radicals may be difficult, but we have a closed form |
| 1119 | from sage.all import exp, I, pi, ComplexField, RR |
| 1120 | CC = ComplexField(53) |
| 1121 | two_pi_i = 2 * pi * I |
| 1122 | k = ( K._n()*CC(K.gen()).log() / CC(two_pi_i) ).real().round() # n ln z / (2 pi i) |
| 1123 | gen_image = exp(k*two_pi_i/K._n()) |
| 1124 | if self is gen: |
| 1125 | self.__symbolic = gen_image |
| 1126 | else: |
| 1127 | self.__symbolic = self.polynomial()(gen_image) |
| 1128 | else: |
| 1129 | # try to solve the minpoly and choose the closest root |
| 1130 | poly = self.minpoly() |
| 1131 | roots = [] |
| 1132 | var = SR(poly.variable_name()) |
| 1133 | for soln in SR(poly).solve(var, to_poly_solve=True): |
| 1134 | if soln.lhs() == var: |
| 1135 | roots.append(soln.rhs()) |
| 1136 | if len(roots) != poly.degree(): |
| 1137 | raise TypeError, "Unable to solve by radicals." |
| 1138 | from number_field_morphisms import matching_root |
| 1139 | from sage.rings.complex_field import ComplexField |
| 1140 | gen_image = matching_root(roots, self, ambient_field=ComplexField(53), margin=2) |
| 1141 | if gen_image is not None: |
| 1142 | self.__symbolic = gen_image |
| 1143 | else: |
| 1144 | # should be rare, e.g. if there is insufficient precision |
| 1145 | raise TypeError, "Unable to determine which root in SR is this element." |
| 1146 | |
| 1147 | return self.__symbolic |
| 1148 | |
| 1149 | def galois_conjugates(self, K): |
| 1150 | r""" |
| 1151 | Return all Gal(Qbar/Q)-conjugates of this number field element in |
| 1152 | the field K. |
| 1153 | |
| 1154 | EXAMPLES: |
| 1155 | |
| 1156 | In the first example the conjugates are obvious:: |
| 1157 | |
| 1158 | sage: K.<a> = NumberField(x^2 - 2) |
| 1159 | sage: a.galois_conjugates(K) |
| 1160 | [a, -a] |
| 1161 | sage: K(3).galois_conjugates(K) |
| 1162 | [3] |
| 1163 | |
| 1164 | In this example the field is not Galois, so we have to pass to an |
| 1165 | extension to obtain the Galois conjugates. |
| 1166 | |
| 1167 | :: |
| 1168 | |
| 1169 | sage: K.<a> = NumberField(x^3 - 2) |
| 1170 | sage: c = a.galois_conjugates(K); c |
| 1171 | [a] |
| 1172 | sage: K.<a> = NumberField(x^3 - 2) |
| 1173 | sage: c = a.galois_conjugates(K.galois_closure('a1')); c |
| 1174 | [1/84*a1^4 + 13/42*a1, -1/252*a1^4 - 55/126*a1, -1/126*a1^4 + 8/63*a1] |
| 1175 | sage: c[0]^3 |
| 1176 | 2 |
| 1177 | sage: parent(c[0]) |
| 1178 | Number Field in a1 with defining polynomial x^6 + 40*x^3 + 1372 |
| 1179 | sage: parent(c[0]).is_galois() |
| 1180 | True |
| 1181 | |
| 1182 | There is only one Galois conjugate of `\sqrt[3]{2}` in |
| 1183 | `\QQ(\sqrt[3]{2})`. |
| 1184 | |
| 1185 | :: |
| 1186 | |
| 1187 | sage: a.galois_conjugates(K) |
| 1188 | [a] |
| 1189 | |
| 1190 | Galois conjugates of `\sqrt[3]{2}` in the field |
| 1191 | `\QQ(\zeta_3,\sqrt[3]{2})`:: |
| 1192 | |
| 1193 | sage: L.<a> = CyclotomicField(3).extension(x^3 - 2) |
| 1194 | sage: a.galois_conjugates(L) |
| 1195 | [a, (-zeta3 - 1)*a, zeta3*a] |
| 1196 | """ |
| 1197 | f = self.absolute_minpoly() |
| 1198 | g = K['x'](f) |
| 1199 | return [a for a,_ in g.roots()] |
| 1200 | |
| 1201 | def conjugate(self): |
| 1202 | """ |
| 1203 | Return the complex conjugate of the number field element. |
| 1204 | Currently, this is implemented for cyclotomic fields and quadratic |
| 1205 | extensions of Q. It seems likely that there are other number fields |
| 1206 | for which the idea of a conjugate would be easy to compute. |
| 1207 | |
| 1208 | EXAMPLES:: |
| 1209 | |
| 1210 | sage: k.<I> = QuadraticField(-1) |
| 1211 | sage: I.conjugate() |
| 1212 | -I |
| 1213 | sage: (I/(1+I)).conjugate() |
| 1214 | -1/2*I + 1/2 |
| 1215 | sage: z6=CyclotomicField(6).gen(0) |
| 1216 | sage: (2*z6).conjugate() |
| 1217 | -2*zeta6 + 2 |
| 1218 | sage: K.<j,b> = QQ[sqrt(-1), sqrt(2)] |
| 1219 | sage: j.conjugate() |
| 1220 | Traceback (most recent call last): |
| 1221 | ... |
| 1222 | NotImplementedError: complex conjugation is not implemented (or doesn't make sense). |
| 1223 | |
| 1224 | :: |
| 1225 | |
| 1226 | sage: K.<b> = NumberField(x^3 - 2) |
| 1227 | sage: b.conjugate() |
| 1228 | Traceback (most recent call last): |
| 1229 | ... |
| 1230 | NotImplementedError: complex conjugation is not implemented (or doesn't make sense). |
| 1231 | """ |
| 1232 | coeffs = self.number_field().absolute_polynomial().list() |
| 1233 | if len(coeffs) == 3 and coeffs[2] == 1 and coeffs[1] == 0: |
| 1234 | # polynomial looks like x^2+d |
| 1235 | # i.e. we live in a quadratic extension of QQ |
| 1236 | if coeffs[0] > 0: |
| 1237 | gen = self.number_field().gen() |
| 1238 | return self.polynomial()(-gen) |
| 1239 | else: |
| 1240 | return self |
| 1241 | elif isinstance(self.number_field(), number_field.NumberField_cyclotomic): |
| 1242 | # We are in a cyclotomic field |
| 1243 | # Replace the generator zeta_n with (zeta_n)^(n-1) |
| 1244 | gen = self.number_field().gen() |
| 1245 | return self.polynomial()(gen ** (gen.multiplicative_order()-1)) |
| 1246 | else: |
| 1247 | raise NotImplementedError, "complex conjugation is not implemented (or doesn't make sense)." |
| 1248 | |
| 1249 | def polynomial(self, var='x'): |
| 1250 | """ |
| 1251 | Return the underlying polynomial corresponding to this number field |
| 1252 | element. |
| 1253 | |
| 1254 | The resulting polynomial is currently *not* cached. |
| 1255 | |
| 1256 | EXAMPLES:: |
| 1257 | |
| 1258 | sage: K.<a> = NumberField(x^5 - x - 1) |
| 1259 | sage: f = (-2/3 + 1/3*a)^4; f |
| 1260 | 1/81*a^4 - 8/81*a^3 + 8/27*a^2 - 32/81*a + 16/81 |
| 1261 | sage: g = f.polynomial(); g |
| 1262 | 1/81*x^4 - 8/81*x^3 + 8/27*x^2 - 32/81*x + 16/81 |
| 1263 | sage: parent(g) |
| 1264 | Univariate Polynomial Ring in x over Rational Field |
| 1265 | |
| 1266 | Note that the result of this function is not cached (should this be |
| 1267 | changed?):: |
| 1268 | |
| 1269 | sage: g is f.polynomial() |
| 1270 | False |
| 1271 | """ |
| 1272 | return QQ[var](self._coefficients()) |
| 1273 | |
| 1274 | def __hash__(self): |
| 1275 | """ |
| 1276 | Return hash of this number field element, which is just the |
| 1277 | hash of the underlying polynomial. |
| 1278 | """ |
| 1279 | return hash(self.polynomial()) |
| 1280 | |
| 1281 | def _coefficients(self): |
| 1282 | """ |
| 1283 | Return the coefficients of the underlying polynomial corresponding |
| 1284 | to this number field element. |
| 1285 | |
| 1286 | OUTPUT: |
| 1287 | |
| 1288 | - a list whose length corresponding to the degree of this |
| 1289 | element written in terms of a generator. |
| 1290 | |
| 1291 | EXAMPLES: |
| 1292 | """ |
| 1293 | return self.polynomial().coefficients() |
| 1294 | |
| 1295 | def denominator(self): |
| 1296 | """ |
| 1297 | Return the denominator of this element, which is by definition the |
| 1298 | denominator of the corresponding polynomial representation. I.e., |
| 1299 | elements of number fields are represented as a polynomial (in |
| 1300 | reduced form) modulo the modulus of the number field, and the |
| 1301 | denominator is the denominator of this polynomial. |
| 1302 | |
| 1303 | EXAMPLES:: |
| 1304 | |
| 1305 | sage: K.<z> = CyclotomicField(3) |
| 1306 | sage: a = 1/3 + (1/5)*z |
| 1307 | sage: print a.denominator() |
| 1308 | 15 |
| 1309 | """ |
| 1310 | return self.polynomial().denominator() |
| 1311 | |
| 1312 | |
| 1313 | def _set_multiplicative_order(self, n): |
| 1314 | """ |
| 1315 | Set the multiplicative order of this number field element. |
| 1316 | |
| 1317 | .. warning:: |
| 1318 | |
| 1319 | Use with caution - only for internal use! End users should |
| 1320 | never call this unless they have a very good reason to do |
| 1321 | so. |
| 1322 | |
| 1323 | EXAMPLES:: |
| 1324 | |
| 1325 | sage: K.<a> = NumberField(x^2 + x + 1) |
| 1326 | sage: a._set_multiplicative_order(3) |
| 1327 | sage: a.multiplicative_order() |
| 1328 | 3 |
| 1329 | |
| 1330 | You can be evil with this so be careful. That's why the function |
| 1331 | name begins with an underscore. |
| 1332 | |
| 1333 | :: |
| 1334 | |
| 1335 | sage: a._set_multiplicative_order(389) |
| 1336 | sage: a.multiplicative_order() |
| 1337 | 389 |
| 1338 | """ |
| 1339 | self.__multiplicative_order = n |
| 1340 | |
| 1341 | def multiplicative_order(self): |
| 1342 | """ |
| 1343 | Return the multiplicative order of this number field element. |
| 1344 | |
| 1345 | EXAMPLES:: |
| 1346 | |
| 1347 | sage: K.<z> = CyclotomicField(5) |
| 1348 | sage: z.multiplicative_order() |
| 1349 | 5 |
| 1350 | sage: (-z).multiplicative_order() |
| 1351 | 10 |
| 1352 | sage: (1+z).multiplicative_order() |
| 1353 | +Infinity |
| 1354 | |
| 1355 | sage: x = polygen(QQ) |
| 1356 | sage: K.<a>=NumberField(x^40 - x^20 + 4) |
| 1357 | sage: u = 1/4*a^30 + 1/4*a^10 + 1/2 |
| 1358 | sage: u.multiplicative_order() |
| 1359 | 6 |
| 1360 | sage: a.multiplicative_order() |
| 1361 | +Infinity |
| 1362 | |
| 1363 | An example in a relative extension:: |
| 1364 | |
| 1365 | sage: K.<a, b> = NumberField([x^2 + x + 1, x^2 - 3]) |
| 1366 | sage: z = (a - 1)*b/3 |
| 1367 | sage: z.multiplicative_order() |
| 1368 | 12 |
| 1369 | sage: z^12==1 and z^6!=1 and z^4!=1 |
| 1370 | True |
| 1371 | |
| 1372 | """ |
| 1373 | if self.__multiplicative_order is not None: |
| 1374 | return self.__multiplicative_order |
| 1375 | |
| 1376 | one = self.number_field().one_element() |
| 1377 | infinity = sage.rings.infinity.infinity |
| 1378 | |
| 1379 | if self == one: |
| 1380 | self.__multiplicative_order = ZZ(1) |
| 1381 | return self.__multiplicative_order |
| 1382 | if self == -one: |
| 1383 | self.__multiplicative_order = ZZ(2) |
| 1384 | return self.__multiplicative_order |
| 1385 | |
| 1386 | if isinstance(self.number_field(), number_field.NumberField_cyclotomic): |
| 1387 | t = self.number_field()._multiplicative_order_table() |
| 1388 | f = self.polynomial() |
| 1389 | if t.has_key(f): |
| 1390 | self.__multiplicative_order = t[f] |
| 1391 | return self.__multiplicative_order |
| 1392 | else: |
| 1393 | self.__multiplicative_order = sage.rings.infinity.infinity |
| 1394 | return self.__multiplicative_order |
| 1395 | |
| 1396 | if self.is_rational() or not self.is_integral() or not self.norm() ==1: |
| 1397 | self.__multiplicative_order = infinity |
| 1398 | return self.__multiplicative_order |
| 1399 | |
| 1400 | # Now we have a unit of norm 1, and check if it is a root of unity |
| 1401 | |
| 1402 | n = self.number_field().zeta_order() |
| 1403 | if not self**n ==1: |
| 1404 | self.__multiplicative_order = infinity |
| 1405 | return self.__multiplicative_order |
| 1406 | from sage.groups.generic import order_from_multiple |
| 1407 | self.__multiplicative_order = order_from_multiple(self,n,operation='*') |
| 1408 | return self.__multiplicative_order |
| 1409 | |
| 1410 | def additive_order(self): |
| 1411 | r""" |
| 1412 | Return the additive order of this element (i.e. infinity if |
| 1413 | self != 0, 1 if self == 0) |
| 1414 | |
| 1415 | EXAMPLES:: |
| 1416 | |
| 1417 | sage: K.<u> = NumberField(x^4 - 3*x^2 + 3) |
| 1418 | sage: u.additive_order() |
| 1419 | +Infinity |
| 1420 | sage: K(0).additive_order() |
| 1421 | 1 |
| 1422 | sage: K.ring_of_integers().characteristic() # implicit doctest |
| 1423 | 0 |
| 1424 | """ |
| 1425 | if self == 0: return 1 |
| 1426 | else: return sage.rings.infinity.infinity |
| 1427 | |
| 1428 | cpdef bint is_rational(self): |
| 1429 | return self in QQ |
| 1430 | |
| 1431 | def trace(self, K=None): |
| 1432 | """ |
| 1433 | Return the absolute or relative trace of this number field |
| 1434 | element. |
| 1435 | |
| 1436 | If K is given then K must be a subfield of the parent L of self, in |
| 1437 | which case the trace is the relative trace from L to K. In all |
| 1438 | other cases, the trace is the absolute trace down to QQ. |
| 1439 | |
| 1440 | EXAMPLES:: |
| 1441 | |
| 1442 | sage: K.<a> = NumberField(x^3 -132/7*x^2 + x + 1); K |
| 1443 | Number Field in a with defining polynomial x^3 - 132/7*x^2 + x + 1 |
| 1444 | sage: a.trace() |
| 1445 | 132/7 |
| 1446 | sage: (a+1).trace() == a.trace() + 3 |
| 1447 | True |
| 1448 | |
| 1449 | If we are in an order, the trace is an integer:: |
| 1450 | |
| 1451 | sage: K.<zeta> = CyclotomicField(17) |
| 1452 | sage: R = K.ring_of_integers() |
| 1453 | sage: R(zeta).trace().parent() |
| 1454 | Integer Ring |
| 1455 | |
| 1456 | TESTS:: |
| 1457 | |
| 1458 | sage: F.<z> = CyclotomicField(5) ; t = 3*z**3 + 4*z**2 + 2 |
| 1459 | sage: t.trace(F) |
| 1460 | 3*z^3 + 4*z^2 + 2 |
| 1461 | """ |
| 1462 | if K is None: |
| 1463 | trace = self._pari_('x').trace() |
| 1464 | return QQ(trace) if self._parent.is_field() else ZZ(trace) |
| 1465 | return self.matrix(K).trace() |
| 1466 | |
| 1467 | def norm(self, K=None): |
| 1468 | """ |
| 1469 | Return the absolute or relative norm of this number field element. |
| 1470 | |
| 1471 | If K is given then K must be a subfield of the parent L of self, in |
| 1472 | which case the norm is the relative norm from L to K. In all other |
| 1473 | cases, the norm is the absolute norm down to QQ. |
| 1474 | |
| 1475 | EXAMPLES:: |
| 1476 | |
| 1477 | sage: K.<a> = NumberField(x^3 + x^2 + x - 132/7); K |
| 1478 | Number Field in a with defining polynomial x^3 + x^2 + x - 132/7 |
| 1479 | sage: a.norm() |
| 1480 | 132/7 |
| 1481 | sage: factor(a.norm()) |
| 1482 | 2^2 * 3 * 7^-1 * 11 |
| 1483 | sage: K(0).norm() |
| 1484 | 0 |
| 1485 | |
| 1486 | Some complicated relatives norms in a tower of number fields. |
| 1487 | |
| 1488 | :: |
| 1489 | |
| 1490 | sage: K.<a,b,c> = NumberField([x^2 + 1, x^2 + 3, x^2 + 5]) |
| 1491 | sage: L = K.base_field(); M = L.base_field() |
| 1492 | sage: a.norm() |
| 1493 | 1 |
| 1494 | sage: a.norm(L) |
| 1495 | 1 |
| 1496 | sage: a.norm(M) |
| 1497 | 1 |
| 1498 | sage: a |
| 1499 | a |
| 1500 | sage: (a+b+c).norm() |
| 1501 | 121 |
| 1502 | sage: (a+b+c).norm(L) |
| 1503 | 2*c*b - 7 |
| 1504 | sage: (a+b+c).norm(M) |
| 1505 | -11 |
| 1506 | |
| 1507 | We illustrate that norm is compatible with towers:: |
| 1508 | |
| 1509 | sage: z = (a+b+c).norm(L); z.norm(M) |
| 1510 | -11 |
| 1511 | |
| 1512 | If we are in an order, the norm is an integer:: |
| 1513 | |
| 1514 | sage: K.<a> = NumberField(x^3-2) |
| 1515 | sage: a.norm().parent() |
| 1516 | Rational Field |
| 1517 | sage: R = K.ring_of_integers() |
| 1518 | sage: R(a).norm().parent() |
| 1519 | Integer Ring |
| 1520 | |
| 1521 | TESTS:: |
| 1522 | |
| 1523 | sage: F.<z> = CyclotomicField(5) |
| 1524 | sage: t = 3*z**3 + 4*z**2 + 2 |
| 1525 | sage: t.norm(F) |
| 1526 | 3*z^3 + 4*z^2 + 2 |
| 1527 | """ |
| 1528 | if K is None: |
| 1529 | norm = self._pari_('x').norm() |
| 1530 | return QQ(norm) if self._parent.is_field() else ZZ(norm) |
| 1531 | return self.matrix(K).determinant() |
| 1532 | |
| 1533 | def vector(self): |
| 1534 | """ |
| 1535 | Return vector representation of self in terms of the basis for the |
| 1536 | ambient number field. |
| 1537 | |
| 1538 | EXAMPLES:: |
| 1539 | |
| 1540 | sage: K.<a> = NumberField(x^2 + 1) |
| 1541 | sage: (2/3*a - 5/6).vector() |
| 1542 | (-5/6, 2/3) |
| 1543 | sage: (-5/6, 2/3) |
| 1544 | (-5/6, 2/3) |
| 1545 | sage: O = K.order(2*a) |
| 1546 | sage: (O.1).vector() |
| 1547 | (0, 2) |
| 1548 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
| 1549 | sage: (a + b).vector() |
| 1550 | (b, 1) |
| 1551 | sage: O = K.order([a,b]) |
| 1552 | sage: (O.1).vector() |
| 1553 | (-b, 1) |
| 1554 | sage: (O.2).vector() |
| 1555 | (1, -b) |
| 1556 | """ |
| 1557 | return self.number_field().relative_vector_space()[2](self) |
| 1558 | |
| 1559 | def charpoly(self, var='x'): |
| 1560 | raise NotImplementedError, "Subclasses of NumberFieldElement must override charpoly()" |
| 1561 | |
| 1562 | def minpoly(self, var='x'): |
| 1563 | """ |
| 1564 | Return the minimal polynomial of this number field element. |
| 1565 | |
| 1566 | EXAMPLES:: |
| 1567 | |
| 1568 | sage: K.<a> = NumberField(x^2+3) |
| 1569 | sage: a.minpoly('x') |
| 1570 | x^2 + 3 |
| 1571 | sage: R.<X> = K['X'] |
| 1572 | sage: L.<b> = K.extension(X^2-(22 + a)) |
| 1573 | sage: b.minpoly('t') |
| 1574 | t^2 - a - 22 |
| 1575 | sage: b.absolute_minpoly('t') |
| 1576 | t^4 - 44*t^2 + 487 |
| 1577 | sage: b^2 - (22+a) |
| 1578 | 0 |
| 1579 | """ |
| 1580 | return self.charpoly(var).radical() # square free part of charpoly |
| 1581 | |
| 1582 | def is_integral(self): |
| 1583 | r""" |
| 1584 | Determine if a number is in the ring of integers of this number |
| 1585 | field. |
| 1586 | |
| 1587 | EXAMPLES:: |
| 1588 | |
| 1589 | sage: K.<a> = NumberField(x^2 + 23) |
| 1590 | sage: a.is_integral() |
| 1591 | True |
| 1592 | sage: t = (1+a)/2 |
| 1593 | sage: t.is_integral() |
| 1594 | True |
| 1595 | sage: t.minpoly() |
| 1596 | x^2 - x + 6 |
| 1597 | sage: t = a/2 |
| 1598 | sage: t.is_integral() |
| 1599 | False |
| 1600 | sage: t.minpoly() |
| 1601 | x^2 + 23/4 |
| 1602 | |
| 1603 | An example in a relative extension:: |
| 1604 | |
| 1605 | sage: K.<a,b> = NumberField([x^2+1, x^2+3]) |
| 1606 | sage: (a+b).is_integral() |
| 1607 | True |
| 1608 | sage: ((a-b)/2).is_integral() |
| 1609 | False |
| 1610 | """ |
| 1611 | return all([a in ZZ for a in self.absolute_minpoly()]) |
| 1612 | |
| 1613 | def matrix(self, base=None): |
| 1614 | r""" |
| 1615 | If base is None, return the matrix of right multiplication by the |
| 1616 | element on the power basis `1, x, x^2, \ldots, x^{d-1}` for |
| 1617 | the number field. Thus the *rows* of this matrix give the images of |
| 1618 | each of the `x^i`. |
| 1619 | |
| 1620 | If base is not None, then base must be either a field that embeds |
| 1621 | in the parent of self or a morphism to the parent of self, in which |
| 1622 | case this function returns the matrix of multiplication by self on |
| 1623 | the power basis, where we view the parent field as a field over |
| 1624 | base. |
| 1625 | |
| 1626 | INPUT: |
| 1627 | |
| 1628 | |
| 1629 | - ``base`` - field or morphism |
| 1630 | |
| 1631 | |
| 1632 | EXAMPLES: |
| 1633 | |
| 1634 | Regular number field:: |
| 1635 | |
| 1636 | sage: K.<a> = NumberField(QQ['x'].0^3 - 5) |
| 1637 | sage: M = a.matrix(); M |
| 1638 | [0 1 0] |
| 1639 | [0 0 1] |
| 1640 | [5 0 0] |
| 1641 | sage: M.base_ring() is QQ |
| 1642 | True |
| 1643 | |
| 1644 | Relative number field:: |
| 1645 | |
| 1646 | sage: L.<b> = K.extension(K['x'].0^2 - 2) |
| 1647 | sage: M = b.matrix(); M |
| 1648 | [0 1] |
| 1649 | [2 0] |
| 1650 | sage: M.base_ring() is K |
| 1651 | True |
| 1652 | |
| 1653 | Absolute number field:: |
| 1654 | |
| 1655 | sage: M = L.absolute_field('c').gen().matrix(); M |
| 1656 | [ 0 1 0 0 0 0] |
| 1657 | [ 0 0 1 0 0 0] |
| 1658 | [ 0 0 0 1 0 0] |
| 1659 | [ 0 0 0 0 1 0] |
| 1660 | [ 0 0 0 0 0 1] |
| 1661 | [-17 -60 -12 -10 6 0] |
| 1662 | sage: M.base_ring() is QQ |
| 1663 | True |
| 1664 | |
| 1665 | More complicated relative number field:: |
| 1666 | |
| 1667 | sage: L.<b> = K.extension(K['x'].0^2 - a); L |
| 1668 | Number Field in b with defining polynomial x^2 - a over its base field |
| 1669 | sage: M = b.matrix(); M |
| 1670 | [0 1] |
| 1671 | [a 0] |
| 1672 | sage: M.base_ring() is K |
| 1673 | True |
| 1674 | |
| 1675 | An example where we explicitly give the subfield or the embedding:: |
| 1676 | |
| 1677 | sage: K.<a> = NumberField(x^4 + 1); L.<a2> = NumberField(x^2 + 1) |
| 1678 | sage: a.matrix(L) |
| 1679 | [ 0 1] |
| 1680 | [a2 0] |
| 1681 | |
| 1682 | Notice that if we compute all embeddings and choose a different |
| 1683 | one, then the matrix is changed as it should be:: |
| 1684 | |
| 1685 | sage: v = L.embeddings(K) |
| 1686 | sage: a.matrix(v[1]) |
| 1687 | [ 0 1] |
| 1688 | [-a2 0] |
| 1689 | |
| 1690 | The norm is also changed:: |
| 1691 | |
| 1692 | sage: a.norm(v[1]) |
| 1693 | a2 |
| 1694 | sage: a.norm(v[0]) |
| 1695 | -a2 |
| 1696 | |
| 1697 | TESTS:: |
| 1698 | |
| 1699 | sage: F.<z> = CyclotomicField(5) ; t = 3*z**3 + 4*z**2 + 2 |
| 1700 | sage: t.matrix(F) |
| 1701 | [3*z^3 + 4*z^2 + 2] |
| 1702 | """ |
| 1703 | if base is not None: |
| 1704 | if number_field.is_NumberField(base): |
| 1705 | return self._matrix_over_base(base) |
| 1706 | else: |
| 1707 | return self._matrix_over_base_morphism(base) |
| 1708 | # Multiply each power of field generator on |
| 1709 | # the left by this element; make matrix |
| 1710 | # whose rows are the coefficients of the result, |
| 1711 | # and transpose. |
| 1712 | if self.__matrix is None: |
| 1713 | K = self.number_field() |
| 1714 | v = [] |
| 1715 | x = K.gen() |
| 1716 | a = K(1) |
| 1717 | d = K.relative_degree() |
| 1718 | for n in range(d): |
| 1719 | v += (a*self).list() |
| 1720 | a *= x |
| 1721 | k = K.base_ring() |
| 1722 | import sage.matrix.matrix_space |
| 1723 | M = sage.matrix.matrix_space.MatrixSpace(k, d) |
| 1724 | self.__matrix = M(v) |
| 1725 | return self.__matrix |
| 1726 | |
| 1727 | def valuation(self, P): |
| 1728 | """ |
| 1729 | Returns the valuation of self at a given prime ideal P. |
| 1730 | |
| 1731 | INPUT: |
| 1732 | |
| 1733 | |
| 1734 | - ``P`` - a prime ideal of the parent of self |
| 1735 | |
| 1736 | |
| 1737 | EXAMPLES:: |
| 1738 | |
| 1739 | sage: R.<x> = QQ[] |
| 1740 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
| 1741 | sage: P = K.ideal(61).factor()[0][0] |
| 1742 | sage: b = a^2 + 30 |
| 1743 | sage: b.valuation(P) |
| 1744 | 1 |
| 1745 | sage: type(b.valuation(P)) |
| 1746 | <type 'sage.rings.integer.Integer'> |
| 1747 | |
| 1748 | The function can be applied to elements in relative number fields:: |
| 1749 | |
| 1750 | sage: L.<b> = K.extension(x^2 - 3) |
| 1751 | sage: [L(6).valuation(P) for P in L.primes_above(2)] |
| 1752 | [4] |
| 1753 | sage: [L(6).valuation(P) for P in L.primes_above(3)] |
| 1754 | [2, 2] |
| 1755 | """ |
| 1756 | from number_field_ideal import is_NumberFieldIdeal |
| 1757 | from sage.rings.infinity import infinity |
| 1758 | if not is_NumberFieldIdeal(P): |
| 1759 | if is_NumberFieldElement(P): |
| 1760 | P = self.number_field().fractional_ideal(P) |
| 1761 | else: |
| 1762 | raise TypeError, "P must be an ideal" |
| 1763 | if not P.is_prime(): |
| 1764 | # We always check this because it caches the pari prime representation of this ideal. |
| 1765 | raise ValueError, "P must be prime" |
| 1766 | if self == 0: |
| 1767 | return infinity |
| 1768 | return Integer_sage(self.number_field()._pari_().elementval(self._pari_(), P._pari_prime)) |
| 1769 | |
| 1770 | def local_height(self, P, prec=None, weighted=False): |
| 1771 | r""" |
| 1772 | Returns the local height of self at a given prime ideal `P`. |
| 1773 | |
| 1774 | INPUT: |
| 1775 | |
| 1776 | |
| 1777 | - ``P`` - a prime ideal of the parent of self |
| 1778 | |
| 1779 | - ``prec`` (int) -- desired floating point precision (defult: |
| 1780 | default RealField precision). |
| 1781 | |
| 1782 | - ``weighted`` (bool, default False) -- if True, apply local |
| 1783 | degree weighting. |
| 1784 | |
| 1785 | OUTPUT: |
| 1786 | |
| 1787 | (real) The local height of this number field element at the |
| 1788 | place `P`. If ``weighted`` is True, this is multiplied by the |
| 1789 | local degree (as required for global heights). |
| 1790 | |
| 1791 | EXAMPLES:: |
| 1792 | |
| 1793 | sage: R.<x> = QQ[] |
| 1794 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
| 1795 | sage: P = K.ideal(61).factor()[0][0] |
| 1796 | sage: b = 1/(a^2 + 30) |
| 1797 | sage: b.local_height(P) |
| 1798 | 4.11087386417331 |
| 1799 | sage: b.local_height(P, weighted=True) |
| 1800 | 8.22174772834662 |
| 1801 | sage: b.local_height(P, 200) |
| 1802 | 4.1108738641733112487513891034256147463156817430812610629374 |
| 1803 | sage: (b^2).local_height(P) |
| 1804 | 8.22174772834662 |
| 1805 | sage: (b^-1).local_height(P) |
| 1806 | 0.000000000000000 |
| 1807 | |
| 1808 | A relative example:: |
| 1809 | |
| 1810 | sage: PK.<y> = K[] |
| 1811 | sage: L.<c> = NumberField(y^2 + a) |
| 1812 | sage: L(1/4).local_height(L.ideal(2, c-a+1)) |
| 1813 | 1.38629436111989 |
| 1814 | """ |
| 1815 | if self.valuation(P) >= 0: ## includes the case self=0 |
| 1816 | from sage.rings.real_mpfr import RealField |
| 1817 | if prec is None: |
| 1818 | return RealField().zero_element() |
| 1819 | else: |
| 1820 | return RealField(prec).zero_element() |
| 1821 | ht = self.abs_non_arch(P,prec).log() |
| 1822 | if not weighted: |
| 1823 | return ht |
| 1824 | nP = P.residue_class_degree()*P.absolute_ramification_index() |
| 1825 | return nP*ht |
| 1826 | |
| 1827 | def local_height_arch(self, i, prec=None, weighted=False): |
| 1828 | r""" |
| 1829 | Returns the local height of self at the `i`'th infinite place. |
| 1830 | |
| 1831 | INPUT: |
| 1832 | |
| 1833 | |
| 1834 | - ``i`` (int) - an integer in ``range(r+s)`` where `(r,s)` is the |
| 1835 | signature of the parent field (so `n=r+2s` is the degree). |
| 1836 | |
| 1837 | - ``prec`` (int) -- desired floating point precision (default: |
| 1838 | default RealField precision). |
| 1839 | |
| 1840 | - ``weighted`` (bool, default False) -- if True, apply local |
| 1841 | degree weighting, i.e. double the value for complex places. |
| 1842 | |
| 1843 | OUTPUT: |
| 1844 | |
| 1845 | (real) The archimedean local height of this number field |
| 1846 | element at the `i`'th infinite place. If ``weighted`` is |
| 1847 | True, this is multiplied by the local degree (as required for |
| 1848 | global heights), i.e. 1 for real places and 2 for complex |
| 1849 | places. |
| 1850 | |
| 1851 | EXAMPLES:: |
| 1852 | |
| 1853 | sage: R.<x> = QQ[] |
| 1854 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
| 1855 | sage: [p.codomain() for p in K.places()] |
| 1856 | [Real Field with 106 bits of precision, |
| 1857 | Real Field with 106 bits of precision, |
| 1858 | Complex Field with 53 bits of precision] |
| 1859 | sage: [a.local_height_arch(i) for i in range(3)] |
| 1860 | [0.5301924545717755083366563897519, |
| 1861 | 0.5301924545717755083366563897519, |
| 1862 | 0.886414217456333] |
| 1863 | sage: [a.local_height_arch(i, weighted=True) for i in range(3)] |
| 1864 | [0.5301924545717755083366563897519, |
| 1865 | 0.5301924545717755083366563897519, |
| 1866 | 1.77282843491267] |
| 1867 | |
| 1868 | A relative example:: |
| 1869 | |
| 1870 | sage: L.<b, c> = NumberFieldTower([x^2 - 5, x^3 + x + 3]) |
| 1871 | sage: [(b + c).local_height_arch(i) for i in range(4)] |
| 1872 | [1.238223390757884911842206617439, |
| 1873 | 0.02240347229957875780769746914391, |
| 1874 | 0.780028961749618, |
| 1875 | 1.16048938497298] |
| 1876 | """ |
| 1877 | K = self.number_field() |
| 1878 | emb = K.places(prec=prec)[i] |
| 1879 | a = emb(self).abs() |
| 1880 | Kv = emb.codomain() |
| 1881 | if a <= Kv.one_element(): |
| 1882 | return Kv.zero_element() |
| 1883 | ht = a.log() |
| 1884 | from sage.rings.real_mpfr import is_RealField |
| 1885 | if weighted and not is_RealField(Kv): |
| 1886 | ht*=2 |
| 1887 | return ht |
| 1888 | |
| 1889 | def global_height_non_arch(self, prec=None): |
| 1890 | """ |
| 1891 | Returns the total non-archimedean component of the height of self. |
| 1892 | |
| 1893 | INPUT: |
| 1894 | |
| 1895 | - ``prec`` (int) -- desired floating point precision (default: |
| 1896 | default RealField precision). |
| 1897 | |
| 1898 | OUTPUT: |
| 1899 | |
| 1900 | (real) The total non-archimedean component of the height of |
| 1901 | this number field element; that is, the sum of the local |
| 1902 | heights at all finite places, weighted by the local degrees. |
| 1903 | |
| 1904 | ALGORITHM: |
| 1905 | |
| 1906 | An alternative formula is `\log(d)` where `d` is the norm of |
| 1907 | the denominator ideal; this is used to avoid factorization. |
| 1908 | |
| 1909 | EXAMPLES:: |
| 1910 | |
| 1911 | sage: R.<x> = QQ[] |
| 1912 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
| 1913 | sage: b = a/6 |
| 1914 | sage: b.global_height_non_arch() |
| 1915 | 7.16703787691222 |
| 1916 | |
| 1917 | Check that this is equal to the sum of the non-archimedean |
| 1918 | local heights:: |
| 1919 | |
| 1920 | sage: [b.local_height(P) for P in b.support()] |
| 1921 | [0.000000000000000, 0.693147180559945, 1.09861228866811, 1.09861228866811] |
| 1922 | sage: [b.local_height(P, weighted=True) for P in b.support()] |
| 1923 | [0.000000000000000, 2.77258872223978, 2.19722457733622, 2.19722457733622] |
| 1924 | sage: sum([b.local_height(P,weighted=True) for P in b.support()]) |
| 1925 | 7.16703787691222 |
| 1926 | |
| 1927 | A relative example:: |
| 1928 | |
| 1929 | sage: PK.<y> = K[] |
| 1930 | sage: L.<c> = NumberField(y^2 + a) |
| 1931 | sage: (c/10).global_height_non_arch() |
| 1932 | 18.4206807439524 |
| 1933 | """ |
| 1934 | from sage.rings.real_mpfr import RealField |
| 1935 | if prec is None: |
| 1936 | R = RealField() |
| 1937 | else: |
| 1938 | R = RealField(prec) |
| 1939 | if self.is_zero(): |
| 1940 | return R.zero_element() |
| 1941 | return R(self.denominator_ideal().absolute_norm()).log() |
| 1942 | |
| 1943 | def global_height_arch(self, prec=None): |
| 1944 | """ |
| 1945 | Returns the total archimedean component of the height of self. |
| 1946 | |
| 1947 | INPUT: |
| 1948 | |
| 1949 | - ``prec`` (int) -- desired floating point precision (defult: |
| 1950 | default RealField precision). |
| 1951 | |
| 1952 | OUTPUT: |
| 1953 | |
| 1954 | (real) The total archimedean component of the height of |
| 1955 | this number field element; that is, the sum of the local |
| 1956 | heights at all infinite places. |
| 1957 | |
| 1958 | EXAMPLES:: |
| 1959 | |
| 1960 | sage: R.<x> = QQ[] |
| 1961 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
| 1962 | sage: b = a/2 |
| 1963 | sage: b.global_height_arch() |
| 1964 | 0.38653407379277... |
| 1965 | """ |
| 1966 | r,s = self.number_field().signature() |
| 1967 | hts = [self.local_height_arch(i, prec, weighted=True) for i in range(r+s)] |
| 1968 | return sum(hts, hts[0].parent().zero_element()) |
| 1969 | |
| 1970 | def global_height(self, prec=None): |
| 1971 | """ |
| 1972 | Returns the absolute logarithmic height of this number field element. |
| 1973 | |
| 1974 | INPUT: |
| 1975 | |
| 1976 | - ``prec`` (int) -- desired floating point precision (defult: |
| 1977 | default RealField precision). |
| 1978 | |
| 1979 | OUTPUT: |
| 1980 | |
| 1981 | (real) The absolute logarithmic height of this number field |
| 1982 | element; that is, the sum of the local heights at all finite |
| 1983 | and infinite places, with the contributions from the infinite |
| 1984 | places scaled by the degree to make the result independent of |
| 1985 | the parent field. |
| 1986 | |
| 1987 | EXAMPLES:: |
| 1988 | |
| 1989 | sage: R.<x> = QQ[] |
| 1990 | sage: K.<a> = NumberField(x^4+3*x^2-17) |
| 1991 | sage: b = a/2 |
| 1992 | sage: b.global_height() |
| 1993 | 2.869222240687... |
| 1994 | sage: b.global_height(prec=200) |
| 1995 | 2.8692222406879748488543678846959454765968722137813736080066 |
| 1996 | |
| 1997 | The global height of an algebraic number is absolute, i.e. it |
| 1998 | does not depend on th parent field:: |
| 1999 | |
| 2000 | sage: QQ(6).global_height() |
| 2001 | 1.79175946922805 |
| 2002 | sage: K(6).global_height() |
| 2003 | 1.79175946922805 |
| 2004 | |
| 2005 | sage: L.<b> = NumberField((a^2).minpoly()) |
| 2006 | sage: L.degree() |
| 2007 | 2 |
| 2008 | sage: b.global_height() # element of L (degree 2 field) |
| 2009 | 1.41660667202811 |
| 2010 | sage: (a^2).global_height() # element of K (degree 4 field) |
| 2011 | 1.41660667202811 |
| 2012 | """ |
| 2013 | return self.global_height_non_arch(prec)+self.global_height_arch(prec)/self.number_field().absolute_degree() |
| 2014 | |
| 2015 | def numerator_ideal(self): |
| 2016 | """ |
| 2017 | Return the numerator ideal of this number field element. |
| 2018 | |
| 2019 | .. note:: |
| 2020 | |
| 2021 | A ValueError will be raised if this function is called on |
| 2022 | 0. |
| 2023 | |
| 2024 | .. note:: |
| 2025 | |
| 2026 | See also ``denominator_ideal()`` |
| 2027 | |
| 2028 | OUTPUT: |
| 2029 | |
| 2030 | (integral ideal) The numerator ideal `N` of this element, |
| 2031 | where for a non-zero number field element `a`, the principal |
| 2032 | ideal generated by `a` has the form `N/D` where `N` and `D` |
| 2033 | are coprime integral ideals. An error is raised if the |
| 2034 | element is zero. |
| 2035 | |
| 2036 | EXAMPLES:: |
| 2037 | |
| 2038 | sage: K.<a> = NumberField(x^2+5) |
| 2039 | sage: b = (1+a)/2 |
| 2040 | sage: b.norm() |
| 2041 | 3/2 |
| 2042 | sage: N = b.numerator_ideal(); N |
| 2043 | Fractional ideal (3, a + 1) |
| 2044 | sage: N.norm() |
| 2045 | 3 |
| 2046 | sage: (1/b).numerator_ideal() |
| 2047 | Fractional ideal (2, a + 1) |
| 2048 | |
| 2049 | TESTS: |
| 2050 | |
| 2051 | Undefined for 0:: |
| 2052 | |
| 2053 | sage: K(0).numerator_ideal() |
| 2054 | Traceback (most recent call last): |
| 2055 | ... |
| 2056 | ValueError: numerator ideal of 0 is not defined. |
| 2057 | """ |
| 2058 | if self.is_zero(): |
| 2059 | raise ValueError, "numerator ideal of 0 is not defined." |
| 2060 | return self.number_field().ideal(self).numerator() |
| 2061 | |
| 2062 | def denominator_ideal(self): |
| 2063 | """ |
| 2064 | Return the denominator ideal of this number field element. |
| 2065 | |
| 2066 | .. note:: |
| 2067 | |
| 2068 | A ValueError will be raised if this function is called on |
| 2069 | 0. |
| 2070 | |
| 2071 | .. note:: |
| 2072 | |
| 2073 | See also ``numerator_ideal()`` |
| 2074 | |
| 2075 | OUTPUT: |
| 2076 | |
| 2077 | (integral ideal) The denominator ideal `D` of this element, |
| 2078 | where for a non-zero number field element `a`, the principal |
| 2079 | ideal generated by `a` has the form `N/D` where `N` and `D` |
| 2080 | are coprime integral ideals. An error is raised if the |
| 2081 | element is zero. |
| 2082 | |
| 2083 | EXAMPLES:: |
| 2084 | |
| 2085 | sage: K.<a> = NumberField(x^2+5) |
| 2086 | sage: b = (1+a)/2 |
| 2087 | sage: b.norm() |
| 2088 | 3/2 |
| 2089 | sage: D = b.denominator_ideal(); D |
| 2090 | Fractional ideal (2, a + 1) |
| 2091 | sage: D.norm() |
| 2092 | 2 |
| 2093 | sage: (1/b).denominator_ideal() |
| 2094 | Fractional ideal (3, a + 1) |
| 2095 | |
| 2096 | TESTS: |
| 2097 | |
| 2098 | Undefined for 0:: |
| 2099 | |
| 2100 | sage: K(0).denominator_ideal() |
| 2101 | Traceback (most recent call last): |
| 2102 | ... |
| 2103 | ValueError: denominator ideal of 0 is not defined. |
| 2104 | """ |
| 2105 | if self.is_zero(): |
| 2106 | raise ValueError, "denominator ideal of 0 is not defined." |
| 2107 | return self.number_field().ideal(self).denominator() |
| 2108 | |
| 2109 | def support(self): |
| 2110 | """ |
| 2111 | Return the support of this number field element. |
| 2112 | |
| 2113 | OUTPUT: A sorted list of the primes ideals at which this number |
| 2114 | field element has nonzero valuation. An error is raised if the |
| 2115 | element is zero. |
| 2116 | |
| 2117 | EXAMPLES:: |
| 2118 | |
| 2119 | sage: x = ZZ['x'].gen() |
| 2120 | sage: F.<t> = NumberField(x^3 - 2) |
| 2121 | |
| 2122 | :: |
| 2123 | |
| 2124 | sage: P5s = F(5).support() |
| 2125 | sage: P5s |
| 2126 | [Fractional ideal (-t^2 - 1), Fractional ideal (t^2 - 2*t - 1)] |
| 2127 | sage: all(5 in P5 for P5 in P5s) |
| 2128 | True |
| 2129 | sage: all(P5.is_prime() for P5 in P5s) |
| 2130 | True |
| 2131 | sage: [ P5.norm() for P5 in P5s ] |
| 2132 | [5, 25] |
| 2133 | |
| 2134 | TESTS: |
| 2135 | |
| 2136 | It doesn't make sense to factor the ideal (0):: |
| 2137 | |
| 2138 | sage: F(0).support() |
| 2139 | Traceback (most recent call last): |
| 2140 | ... |
| 2141 | ArithmeticError: Support of 0 is not defined. |
| 2142 | """ |
| 2143 | if self.is_zero(): |
| 2144 | raise ArithmeticError, "Support of 0 is not defined." |
| 2145 | return self.number_field().primes_above(self) |
| 2146 | |
| 2147 | def _matrix_over_base(self, L): |
| 2148 | """ |
| 2149 | Return the matrix of self over the base field L. |
| 2150 | |
| 2151 | EXAMPLES:: |
| 2152 | |
| 2153 | sage: K.<a> = NumberField(ZZ['x'].0^3-2, 'a') |
| 2154 | sage: L.<b> = K.extension(ZZ['x'].0^2+3, 'b') |
| 2155 | sage: L(a)._matrix_over_base(K) == L(a).matrix() |
| 2156 | True |
| 2157 | """ |
| 2158 | K = self.number_field() |
| 2159 | E = L.embeddings(K) |
| 2160 | if len(E) == 0: |
| 2161 | raise ValueError, "no way to embed L into parent's base ring K" |
| 2162 | phi = E[0] |
| 2163 | return self._matrix_over_base_morphism(phi) |
| 2164 | |
| 2165 | def _matrix_over_base_morphism(self, phi): |
| 2166 | """ |
| 2167 | Return the matrix of self over a specified base, where phi gives a |
| 2168 | map from the specified base to self.parent(). |
| 2169 | |
| 2170 | EXAMPLES:: |
| 2171 | |
| 2172 | sage: F.<alpha> = NumberField(ZZ['x'].0^5-2) |
| 2173 | sage: h = Hom(QQ,F)([1]) |
| 2174 | sage: alpha._matrix_over_base_morphism(h) == alpha.matrix() |
| 2175 | True |
| 2176 | sage: alpha._matrix_over_base_morphism(h) == alpha.matrix(QQ) |
| 2177 | True |
| 2178 | """ |
| 2179 | L = phi.domain() |
| 2180 | |
| 2181 | ## the code below doesn't work if the morphism is |
| 2182 | ## over QQ, since QQ.primitive_element() doesn't |
| 2183 | ## make sense |
| 2184 | if L is QQ: |
| 2185 | K = phi.codomain() |
| 2186 | if K != self.number_field(): |
| 2187 | raise ValueError, "codomain of phi must be parent of self" |
| 2188 | ## the variable name is irrelevant below, because the |
| 2189 | ## matrix is over QQ |
| 2190 | F = K.absolute_field('alpha') |
| 2191 | from_f, to_F = F.structure() |
| 2192 | return to_F(self).matrix() |
| 2193 | |
| 2194 | alpha = L.primitive_element() |
| 2195 | beta = phi(alpha) |
| 2196 | K = phi.codomain() |
| 2197 | if K != self.number_field(): |
| 2198 | raise ValueError, "codomain of phi must be parent of self" |
| 2199 | |
| 2200 | # Construct a relative extension over L (= QQ(beta)) |
| 2201 | M = K.relativize(beta, ('a','b')) |
| 2202 | # variable name a is OK, since this is temporary |
| 2203 | |
| 2204 | # Carry self over to M. |
| 2205 | from_M, to_M = M.structure() |
| 2206 | try: |
| 2207 | z = to_M(self) |
| 2208 | except Exception: |
| 2209 | return to_M, self, K, beta |
| 2210 | |
| 2211 | # Compute the relative matrix of self, but in M |
| 2212 | R = z.matrix() |
| 2213 | |
| 2214 | # Map back to L. |
| 2215 | psi = M.base_field().hom([alpha]) |
| 2216 | return R.apply_morphism(psi) |
| 2217 | |
| 2218 | |
| 2219 | def list(self): |
| 2220 | """ |
| 2221 | Return the list of coefficients of self written in terms of a power |
| 2222 | basis. |
| 2223 | """ |
| 2224 | # Power basis list is total nonsense, unless the parent of self is an |
| 2225 | # absolute extension. |
| 2226 | raise NotImplementedError |
| 2227 | |
| 2228 | def inverse_mod(self, I): |
| 2229 | """ |
| 2230 | Returns the inverse of self mod the integral ideal I. |
| 2231 | |
| 2232 | INPUT: |
| 2233 | |
| 2234 | - ``I`` - may be an ideal of self.parent(), or an element or list |
| 2235 | of elements of self.parent() generating a nonzero ideal. A ValueError |
| 2236 | is raised if I is non-integral or zero. A ZeroDivisionError is |
| 2237 | raised if I + (x) != (1). |
| 2238 | |
| 2239 | NOTE: It's not implemented yet for non-integral elements. |
| 2240 | |
| 2241 | EXAMPLES:: |
| 2242 | |
| 2243 | sage: k.<a> = NumberField(x^2 + 23) |
| 2244 | sage: N = k.ideal(3) |
| 2245 | sage: d = 3*a + 1 |
| 2246 | sage: d.inverse_mod(N) |
| 2247 | 1 |
| 2248 | |
| 2249 | :: |
| 2250 | |
| 2251 | sage: k.<a> = NumberField(x^3 + 11) |
| 2252 | sage: d = a + 13 |
| 2253 | sage: d.inverse_mod(a^2)*d - 1 in k.ideal(a^2) |
| 2254 | True |
| 2255 | sage: d.inverse_mod((5, a + 1))*d - 1 in k.ideal(5, a + 1) |
| 2256 | True |
| 2257 | sage: K.<b> = k.extension(x^2 + 3) |
| 2258 | sage: b.inverse_mod([37, a - b]) |
| 2259 | 7 |
| 2260 | sage: 7*b - 1 in K.ideal(37, a - b) |
| 2261 | True |
| 2262 | sage: b.inverse_mod([37, a - b]).parent() == K |
| 2263 | True |
| 2264 | """ |
| 2265 | R = self.number_field().ring_of_integers() |
| 2266 | try: |
| 2267 | return _inverse_mod_generic(R(self), I) |
| 2268 | except TypeError: # raised by failure of R(self) |
| 2269 | raise NotImplementedError, "inverse_mod is not implemented for non-integral elements" |
| 2270 | |
| 2271 | |
| 2272 | cdef class NumberFieldElement_absolute(NumberFieldElement): |
| 2273 | |
| 2274 | def _pari_(self, var='x'): |
| 2275 | """ |
| 2276 | Return PARI C-library object corresponding to self. |
| 2277 | |
| 2278 | EXAMPLES:: |
| 2279 | |
| 2280 | sage: k.<j> = QuadraticField(-1) |
| 2281 | sage: j._pari_('j') |
| 2282 | Mod(j, j^2 + 1) |
| 2283 | sage: pari(j) |
| 2284 | Mod(x, x^2 + 1) |
| 2285 | |
| 2286 | :: |
| 2287 | |
| 2288 | sage: y = QQ['y'].gen() |
| 2289 | sage: k.<j> = NumberField(y^3 - 2) |
| 2290 | sage: pari(j) |
| 2291 | Mod(x, x^3 - 2) |
| 2292 | |
| 2293 | By default the variable name is 'x', since in PARI many variable |
| 2294 | names are reserved:: |
| 2295 | |
| 2296 | sage: theta = polygen(QQ, 'theta') |
| 2297 | sage: M.<theta> = NumberField(theta^2 + 1) |
| 2298 | sage: pari(theta) |
| 2299 | Mod(x, x^2 + 1) |
| 2300 | |
| 2301 | If you try do coerce a generator called I to PARI, hell may break |
| 2302 | loose:: |
| 2303 | |
| 2304 | sage: k.<I> = QuadraticField(-1) |
| 2305 | sage: I._pari_('I') |
| 2306 | Traceback (most recent call last): |
| 2307 | ... |
| 2308 | PariError: forbidden (45) |
| 2309 | |
| 2310 | Instead, request the variable be named different for the coercion:: |
| 2311 | |
| 2312 | sage: pari(I) |
| 2313 | Mod(x, x^2 + 1) |
| 2314 | sage: I._pari_('i') |
| 2315 | Mod(i, i^2 + 1) |
| 2316 | sage: I._pari_('II') |
| 2317 | Mod(II, II^2 + 1) |
| 2318 | """ |
| 2319 | try: |
| 2320 | return self.__pari[var] |
| 2321 | except KeyError: |
| 2322 | pass |
| 2323 | except TypeError: |
| 2324 | self.__pari = {} |
| 2325 | if var is None: |
| 2326 | var = self.number_field().variable_name() |
| 2327 | |
| 2328 | f = self.polynomial()._pari_().subst('x', var) |
| 2329 | g = self.number_field().pari_polynomial().subst('x', var) |
| 2330 | h = f.Mod(g) |
| 2331 | self.__pari[var] = h |
| 2332 | return h |
| 2333 | |
| 2334 | def _magma_init_(self, magma): |
| 2335 | """ |
| 2336 | Return Magma version of this number field element. |
| 2337 | |
| 2338 | INPUT: |
| 2339 | |
| 2340 | |
| 2341 | - ``magma`` - a Magma interpreter |
| 2342 | |
| 2343 | |
| 2344 | OUTPUT: MagmaElement that has parent the Magma object corresponding |
| 2345 | to the parent number field. |
| 2346 | |
| 2347 | EXAMPLES:: |
| 2348 | |
| 2349 | sage: K.<a> = NumberField(x^3 + 2) |
| 2350 | sage: a._magma_init_(magma) # optional - magma |
| 2351 | '(_sage_[...]![0, 1, 0])' |
| 2352 | sage: magma((2/3)*a^2 - 17/3) # optional - magma |
| 2353 | 1/3*(2*a^2 - 17) |
| 2354 | |
| 2355 | An element of a cyclotomic field. |
| 2356 | |
| 2357 | :: |
| 2358 | |
| 2359 | sage: K = CyclotomicField(9) |
| 2360 | sage: K.gen() |
| 2361 | zeta9 |
| 2362 | sage: K.gen()._magma_init_(magma) # optional - magma |
| 2363 | '(_sage_[...]![0, 1, 0, 0, 0, 0])' |
| 2364 | sage: magma(K.gen()) # optional - magma |
| 2365 | zeta9 |
| 2366 | """ |
| 2367 | K = magma(self.parent()) |
| 2368 | return '(%s!%s)'%(K.name(), self.list()) |
| 2369 | |
| 2370 | def absolute_charpoly(self, var='x', algorithm=None): |
| 2371 | r""" |
| 2372 | Return the characteristic polynomial of this element over |
| 2373 | |
| 2374 | For the meaning of the optional argument algorithm, see :meth:`charpoly`. |
| 2375 | |
| 2376 | EXAMPLES:: |
| 2377 | |
| 2378 | sage: x = ZZ['x'].0 |
| 2379 | sage: K.<a> = NumberField(x^4 + 2, 'a') |
| 2380 | sage: a.absolute_charpoly() |
| 2381 | x^4 + 2 |
| 2382 | sage: a.absolute_charpoly('y') |
| 2383 | y^4 + 2 |
| 2384 | sage: (-a^2).absolute_charpoly() |
| 2385 | x^4 + 4*x^2 + 4 |
| 2386 | sage: (-a^2).absolute_minpoly() |
| 2387 | x^2 + 2 |
| 2388 | |
| 2389 | sage: a.absolute_charpoly(algorithm='pari') == a.absolute_charpoly(algorithm='sage') |
| 2390 | True |
| 2391 | """ |
| 2392 | # this hack is necessary because quadratic fields override |
| 2393 | # charpoly(), and they don't take the argument 'algorithm' |
| 2394 | if algorithm is None: |
| 2395 | return self.charpoly(var) |
| 2396 | return self.charpoly(var, algorithm) |
| 2397 | |
| 2398 | def absolute_minpoly(self, var='x', algorithm=None): |
| 2399 | r""" |
| 2400 | Return the minimal polynomial of this element over |
| 2401 | `\QQ`. |
| 2402 | |
| 2403 | For the meaning of the optional argument algorithm, see :meth:`charpoly`. |
| 2404 | |
| 2405 | EXAMPLES:: |
| 2406 | |
| 2407 | sage: x = ZZ['x'].0 |
| 2408 | 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 |
| 2409 | sage: K.<a> = NumberField(f, 'a') |
| 2410 | sage: a.absolute_charpoly() |
| 2411 | 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 |
| 2412 | sage: a.absolute_charpoly('y') |
| 2413 | 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 |
| 2414 | 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 |
| 2415 | sage: b.absolute_charpoly() |
| 2416 | 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 |
| 2417 | sage: b.absolute_minpoly() |
| 2418 | x^5 + 5*x^4 - 40*x^2 - 19*x + 135 |
| 2419 | |
| 2420 | sage: b.absolute_minpoly(algorithm='pari') == b.absolute_minpoly(algorithm='sage') |
| 2421 | True |
| 2422 | """ |
| 2423 | # this hack is necessary because quadratic fields override |
| 2424 | # minpoly(), and they don't take the argument 'algorithm' |
| 2425 | if algorithm is None: |
| 2426 | return self.minpoly(var) |
| 2427 | return self.minpoly(var, algorithm) |
| 2428 | |
| 2429 | def charpoly(self, var='x', algorithm=None): |
| 2430 | r""" |
| 2431 | The characteristic polynomial of this element, over |
| 2432 | `\QQ` if self is an element of a field, and over |
| 2433 | `\ZZ` is self is an element of an order. |
| 2434 | |
| 2435 | This is the same as ``self.absolute_charpoly`` since |
| 2436 | this is an element of an absolute extension. |
| 2437 | |
| 2438 | The optional argument algorithm controls how the |
| 2439 | characteristic polynomial is computed: 'pari' uses Pari, |
| 2440 | 'sage' uses charpoly for Sage matrices. The default value |
| 2441 | None means that 'pari' is used for small degrees (up to the |
| 2442 | value of the constant TUNE_CHARPOLY_NF, currently at 25), |
| 2443 | otherwise 'sage' is used. The constant TUNE_CHARPOLY_NF |
| 2444 | should give reasonable performance on all architectures; |
| 2445 | however, if you feel the need to customize it to your own |
| 2446 | machine, see trac ticket 5213 for a tuning script. |
| 2447 | |
| 2448 | EXAMPLES: |
| 2449 | |
| 2450 | We compute the characteristic polynomial of the cube root of `2`. |
| 2451 | |
| 2452 | :: |
| 2453 | |
| 2454 | sage: R.<x> = QQ[] |
| 2455 | sage: K.<a> = NumberField(x^3-2) |
| 2456 | sage: a.charpoly('x') |
| 2457 | x^3 - 2 |
| 2458 | sage: a.charpoly('y').parent() |
| 2459 | Univariate Polynomial Ring in y over Rational Field |
| 2460 | |
| 2461 | TESTS:: |
| 2462 | |
| 2463 | sage: R = K.ring_of_integers() |
| 2464 | sage: R(a).charpoly() |
| 2465 | x^3 - 2 |
| 2466 | sage: R(a).charpoly().parent() |
| 2467 | Univariate Polynomial Ring in x over Integer Ring |
| 2468 | |
| 2469 | sage: R(a).charpoly(algorithm='pari') == R(a).charpoly(algorithm='sage') |
| 2470 | True |
| 2471 | """ |
| 2472 | if algorithm is None: |
| 2473 | if self._parent.degree() <= TUNE_CHARPOLY_NF: |
| 2474 | algorithm = 'pari' |
| 2475 | else: |
| 2476 | algorithm = 'sage' |
| 2477 | R = self._parent.base_ring()[var] |
| 2478 | if algorithm == 'pari': |
| 2479 | return R(self._pari_('x').charpoly()) |
| 2480 | if algorithm == 'sage': |
| 2481 | return R(self.matrix().charpoly()) |
| 2482 | |
| 2483 | def minpoly(self, var='x', algorithm=None): |
| 2484 | """ |
| 2485 | Return the minimal polynomial of this number field element. |
| 2486 | |
| 2487 | For the meaning of the optional argument algorithm, see charpoly(). |
| 2488 | |
| 2489 | EXAMPLES: |
| 2490 | |
| 2491 | We compute the characteristic polynomial of cube root of `2`. |
| 2492 | |
| 2493 | :: |
| 2494 | |
| 2495 | sage: R.<x> = QQ[] |
| 2496 | sage: K.<a> = NumberField(x^3-2) |
| 2497 | sage: a.minpoly('x') |
| 2498 | x^3 - 2 |
| 2499 | sage: a.minpoly('y').parent() |
| 2500 | Univariate Polynomial Ring in y over Rational Field |
| 2501 | |
| 2502 | TESTS:: |
| 2503 | |
| 2504 | sage: R = K.ring_of_integers() |
| 2505 | sage: R(a).minpoly() |
| 2506 | x^3 - 2 |
| 2507 | sage: R(a).minpoly().parent() |
| 2508 | Univariate Polynomial Ring in x over Integer Ring |
| 2509 | |
| 2510 | sage: R(a).minpoly(algorithm='pari') == R(a).minpoly(algorithm='sage') |
| 2511 | True |
| 2512 | |
| 2513 | """ |
| 2514 | return self.charpoly(var, algorithm).radical() # square free part of charpoly |
| 2515 | |
| 2516 | def list(self): |
| 2517 | """ |
| 2518 | Return the list of coefficients of self written in terms of a power |
| 2519 | basis. |
| 2520 | |
| 2521 | EXAMPLE:: |
| 2522 | |
| 2523 | sage: K.<z> = CyclotomicField(3) |
| 2524 | sage: (2+3/5*z).list() |
| 2525 | [2, 3/5] |
| 2526 | sage: (5*z).list() |
| 2527 | [0, 5] |
| 2528 | sage: K(3).list() |
| 2529 | [3, 0] |
| 2530 | """ |
| 2531 | n = self.number_field().degree() |
| 2532 | v = self._coefficients() |
| 2533 | z = sage.rings.rational.Rational(0) |
| 2534 | return v + [z]*(n - len(v)) |
| 2535 | |
| 2536 | def lift(self, var='x'): |
| 2537 | """ |
| 2538 | Return an element of QQ[x], where this number field element |
| 2539 | lives in QQ[x]/(f(x)). |
| 2540 | |
| 2541 | EXAMPLES:: |
| 2542 | sage: K.<a> = QuadraticField(-3) |
| 2543 | sage: a.lift() |
| 2544 | x |
| 2545 | |
| 2546 | """ |
| 2547 | R = self.number_field().base_field()[var] |
| 2548 | return R(self.list()) |
| 2549 | |
| 2550 | def is_real_positive(self, min_prec=53): |
| 2551 | r""" |
| 2552 | Using the ``n`` method of approximation, return ``True`` if |
| 2553 | ``self`` is a real positive number and ``False`` otherwise. |
| 2554 | This method is completely dependent of the embedding used by |
| 2555 | the ``n`` method. |
| 2556 | |
| 2557 | The algorithm first checks that ``self`` is not a strictly |
| 2558 | complex number. Then if ``self`` is not zero, by approximation |
| 2559 | more and more precise, the method answers True if the |
| 2560 | number is positive. Using `RealInterval`, the result is |
| 2561 | guaranteed to be correct. |
| 2562 | |
| 2563 | For CyclotomicField, the embedding is the natural one |
| 2564 | sending `zetan` on `cos(2*pi/n)`. |
| 2565 | |
| 2566 | EXAMPLES:: |
| 2567 | |
| 2568 | sage: K.<a> = CyclotomicField(3) |
| 2569 | sage: (a+a^2).is_real_positive() |
| 2570 | False |
| 2571 | sage: (-a-a^2).is_real_positive() |
| 2572 | True |
| 2573 | sage: K.<a> = CyclotomicField(1000) |
| 2574 | sage: (a+a^(-1)).is_real_positive() |
| 2575 | True |
| 2576 | sage: K.<a> = CyclotomicField(1009) |
| 2577 | sage: d = a^252 |
| 2578 | sage: (d+d.conjugate()).is_real_positive() |
| 2579 | True |
| 2580 | sage: d = a^253 |
| 2581 | sage: (d+d.conjugate()).is_real_positive() |
| 2582 | False |
| 2583 | sage: K.<a> = QuadraticField(3) |
| 2584 | sage: a.is_real_positive() |
| 2585 | True |
| 2586 | sage: K.<a> = QuadraticField(-3) |
| 2587 | sage: a.is_real_positive() |
| 2588 | False |
| 2589 | sage: (a-a).is_real_positive() |
| 2590 | False |
| 2591 | """ |
| 2592 | if self != self.conjugate() or self.is_zero(): |
| 2593 | return False |
| 2594 | else: |
| 2595 | approx = RealInterval(self.n(min_prec).real()) |
| 2596 | if approx.lower() > 0: |
| 2597 | return True |
| 2598 | else: |
| 2599 | if approx.upper() < 0: |
| 2600 | return False |
| 2601 | else: |
| 2602 | return self.is_real_positive(min_prec+20) |
| 2603 | |
| 2604 | cdef class NumberFieldElement_relative(NumberFieldElement): |
| 2605 | r""" |
| 2606 | The current relative number field element implementation |
| 2607 | does everything in terms of absolute polynomials. |
| 2608 | |
| 2609 | All conversions from relative polynomials, lists, vectors, etc |
| 2610 | should happen in the parent. |
| 2611 | """ |
| 2612 | def __init__(self, parent, f): |
| 2613 | NumberFieldElement.__init__(self, parent, f) |
| 2614 | |
| 2615 | def __getitem__(self, n): |
| 2616 | """ |
| 2617 | Return the n-th coefficient of this relative number field element, written |
| 2618 | as a polynomial in the generator. |
| 2619 | |
| 2620 | Note that `n` must be between 0 and `d-1`, where |
| 2621 | `d` is the relative degree of the number field. |
| 2622 | |
| 2623 | EXAMPLES:: |
| 2624 | |
| 2625 | sage: K.<a, b> = NumberField([x^3 - 5, x^2 + 3]) |
| 2626 | sage: c = (a + b)^3; c |
| 2627 | 3*b*a^2 - 9*a - 3*b + 5 |
| 2628 | sage: c[0] |
| 2629 | -3*b + 5 |
| 2630 | |
| 2631 | We illustrate bounds checking:: |
| 2632 | |
| 2633 | sage: c[-1] |
| 2634 | Traceback (most recent call last): |
| 2635 | ... |
| 2636 | IndexError: index must be between 0 and the relative degree minus 1. |
| 2637 | sage: c[4] |
| 2638 | Traceback (most recent call last): |
| 2639 | ... |
| 2640 | IndexError: index must be between 0 and the relative degree minus 1. |
| 2641 | |
| 2642 | The list method implicitly calls ``__getitem__``:: |
| 2643 | |
| 2644 | sage: list(c) |
| 2645 | [-3*b + 5, -9, 3*b] |
| 2646 | sage: K(list(c)) == c |
| 2647 | True |
| 2648 | """ |
| 2649 | if n < 0 or n >= self.parent().relative_degree(): |
| 2650 | raise IndexError, "index must be between 0 and the relative degree minus 1." |
| 2651 | return self.vector()[n] |
| 2652 | |
| 2653 | def list(self): |
| 2654 | """ |
| 2655 | Return the list of coefficients of self written in terms of a power |
| 2656 | basis. |
| 2657 | |
| 2658 | EXAMPLES:: |
| 2659 | |
| 2660 | sage: K.<a,b> = NumberField([x^3+2, x^2+1]) |
| 2661 | sage: a.list() |
| 2662 | [0, 1, 0] |
| 2663 | sage: v = (K.base_field().0 + a)^2 ; v |
| 2664 | a^2 + 2*b*a - 1 |
| 2665 | sage: v.list() |
| 2666 | [-1, 2*b, 1] |
| 2667 | """ |
| 2668 | return self.vector().list() |
| 2669 | |
| 2670 | def lift(self, var='x'): |
| 2671 | """ |
| 2672 | Return an element of K[x], where this number field element |
| 2673 | lives in the relative number field K[x]/(f(x)). |
| 2674 | |
| 2675 | EXAMPLES:: |
| 2676 | |
| 2677 | sage: K.<a> = QuadraticField(-3) |
| 2678 | sage: x = polygen(K) |
| 2679 | sage: L.<b> = K.extension(x^7 + 5) |
| 2680 | sage: u = L(1/2*a + 1/2 + b + (a-9)*b^5) |
| 2681 | sage: u.lift() |
| 2682 | (a - 9)*x^5 + x + 1/2*a + 1/2 |
| 2683 | """ |
| 2684 | K = self.number_field() |
| 2685 | # Compute representation of self in terms of relative vector space. |
| 2686 | R = K.base_field()[var] |
| 2687 | return R(self.list()) |
| 2688 | |
| 2689 | def _pari_(self, var='x'): |
| 2690 | """ |
| 2691 | Return PARI C-library object corresponding to self. |
| 2692 | |
| 2693 | EXAMPLES: |
| 2694 | |
| 2695 | By default the variable name is 'x', since in PARI many |
| 2696 | variable names are reserved. |
| 2697 | |
| 2698 | :: |
| 2699 | |
| 2700 | sage: y = QQ['y'].gen() |
| 2701 | sage: k.<j> = NumberField([y^2 - 7, y^3 - 2]) |
| 2702 | sage: pari(j) |
| 2703 | Mod(42/5515*x^5 - 9/11030*x^4 - 196/1103*x^3 + 273/5515*x^2 + 10281/5515*x + 4459/11030, x^6 - 21*x^4 + 4*x^3 + 147*x^2 + 84*x - 339) |
| 2704 | sage: j^2 |
| 2705 | 7 |
| 2706 | sage: pari(j)^2 |
| 2707 | Mod(7, x^6 - 21*x^4 + 4*x^3 + 147*x^2 + 84*x - 339) |
| 2708 | sage: (j^2)._pari_('y') |
| 2709 | Mod(7, y^6 - 21*y^4 + 4*y^3 + 147*y^2 + 84*y - 339) |
| 2710 | |
| 2711 | sage: K.<a> = NumberField(x^2 + 2, 'a') |
| 2712 | sage: K(1)._pari_() |
| 2713 | Mod(1, x^2 + 2) |
| 2714 | sage: K(1)._pari_('t') |
| 2715 | Mod(1, t^2 + 2) |
| 2716 | |
| 2717 | sage: K.gen()._pari_() |
| 2718 | Mod(x, x^2 + 2) |
| 2719 | sage: K.gen()._pari_('t') |
| 2720 | Mod(t, t^2 + 2) |
| 2721 | |
| 2722 | At this time all elements, even relative elements, are |
| 2723 | represented as absolute polynomials: |
| 2724 | |
| 2725 | sage: K.<a> = NumberField(x^2 + 2, 'a') |
| 2726 | sage: L.<b> = NumberField(K['x'].0^2 + a, 'b') |
| 2727 | sage: L(1)._pari_() |
| 2728 | Mod(1, x^4 + 2) |
| 2729 | sage: L(1)._pari_('t') |
| 2730 | Mod(1, t^4 + 2) |
| 2731 | sage: L.gen()._pari_() |
| 2732 | Mod(x, x^4 + 2) |
| 2733 | sage: L.gen()._pari_('t') |
| 2734 | Mod(t, t^4 + 2) |
| 2735 | |
| 2736 | sage: M.<c> = NumberField(L['x'].0^3 + b, 'c') |
| 2737 | sage: M(1)._pari_() |
| 2738 | Mod(1, x^12 + 2) |
| 2739 | sage: M(1)._pari_('t') |
| 2740 | Mod(1, t^12 + 2) |
| 2741 | sage: M.gen()._pari_() |
| 2742 | Mod(x, x^12 + 2) |
| 2743 | sage: M.gen()._pari_('t') |
| 2744 | Mod(t, t^12 + 2) |
| 2745 | """ |
| 2746 | try: |
| 2747 | return self.__pari[var] |
| 2748 | except KeyError: |
| 2749 | pass |
| 2750 | except TypeError: |
| 2751 | self.__pari = {} |
| 2752 | g = self.parent().pari_polynomial(var) |
| 2753 | f = self.polynomial()._pari_() |
| 2754 | f = f.subst('x', var) |
| 2755 | h = f.Mod(g) |
| 2756 | self.__pari[var] = h |
| 2757 | return h |
| 2758 | |
| 2759 | def __repr__(self): |
| 2760 | K = self.number_field() |
| 2761 | # Compute representation of self in terms of relative vector space. |
| 2762 | R = K.base_field()[K.variable_name()] |
| 2763 | return repr(R(self.list())) |
| 2764 | |
| 2765 | def _latex_(self): |
| 2766 | r""" |
| 2767 | Returns the latex representation for this element. |
| 2768 | |
| 2769 | EXAMPLES:: |
| 2770 | |
| 2771 | sage: C.<zeta> = CyclotomicField(12) |
| 2772 | sage: PC.<x> = PolynomialRing(C) |
| 2773 | sage: K.<alpha> = NumberField(x^2 - 7) |
| 2774 | sage: latex((alpha + zeta)^4) |
| 2775 | \left(4 \zeta_{12}^{3} + 28 \zeta_{12}\right) \alpha + 43 \zeta_{12}^{2} + 48 |
| 2776 | sage: PK.<y> = PolynomialRing(K) |
| 2777 | sage: L.<beta> = NumberField(y^3 + y + alpha) |
| 2778 | sage: latex((beta + zeta)^3) |
| 2779 | 3 \zeta_{12} \beta^{2} + \left(3 \zeta_{12}^{2} - 1\right) \beta - \alpha + \zeta_{12}^{3} |
| 2780 | """ |
| 2781 | K = self.number_field() |
| 2782 | R = K.base_field()[K.variable_name()] |
| 2783 | return R(self.list())._latex_() |
| 2784 | |
| 2785 | def charpoly(self, var='x'): |
| 2786 | r""" |
| 2787 | The characteristic polynomial of this element over its base field. |
| 2788 | |
| 2789 | EXAMPLES:: |
| 2790 | |
| 2791 | sage: x = ZZ['x'].0 |
| 2792 | sage: K.<a, b> = QQ.extension([x^2 + 2, x^5 + 400*x^4 + 11*x^2 + 2]) |
| 2793 | sage: a.charpoly() |
| 2794 | x^2 + 2 |
| 2795 | sage: b.charpoly() |
| 2796 | x^2 - 2*b*x + b^2 |
| 2797 | sage: b.minpoly() |
| 2798 | x - b |
| 2799 | |
| 2800 | sage: K.<a, b> = NumberField([x^2 + 2, x^2 + 1000*x + 1]) |
| 2801 | sage: y = K['y'].0 |
| 2802 | sage: L.<c> = K.extension(y^2 + a*y + b) |
| 2803 | sage: c.charpoly() |
| 2804 | x^2 + a*x + b |
| 2805 | sage: c.minpoly() |
| 2806 | x^2 + a*x + b |
| 2807 | sage: L(a).charpoly() |
| 2808 | x^2 - 2*a*x - 2 |
| 2809 | sage: L(a).minpoly() |
| 2810 | x - a |
| 2811 | sage: L(b).charpoly() |
| 2812 | x^2 - 2*b*x - 1000*b - 1 |
| 2813 | sage: L(b).minpoly() |
| 2814 | x - b |
| 2815 | """ |
| 2816 | R = self._parent.base_ring()[var] |
| 2817 | return R(self.matrix().charpoly()) |
| 2818 | |
| 2819 | def absolute_charpoly(self, var='x', algorithm=None): |
| 2820 | r""" |
| 2821 | The characteristic polynomial of this element over |
| 2822 | `\QQ`. |
| 2823 | |
| 2824 | We construct a relative extension and find the characteristic |
| 2825 | polynomial over `\QQ`. |
| 2826 | |
| 2827 | The optional argument algorithm controls how the |
| 2828 | characteristic polynomial is computed: 'pari' uses Pari, |
| 2829 | 'sage' uses charpoly for Sage matrices. The default value |
| 2830 | None means that 'pari' is used for small degrees (up to the |
| 2831 | value of the constant TUNE_CHARPOLY_NF, currently at 25), |
| 2832 | otherwise 'sage' is used. The constant TUNE_CHARPOLY_NF |
| 2833 | should give reasonable performance on all architectures; |
| 2834 | however, if you feel the need to customize it to your own |
| 2835 | machine, see trac ticket 5213 for a tuning script. |
| 2836 | |
| 2837 | EXAMPLES:: |
| 2838 | |
| 2839 | sage: R.<x> = QQ[] |
| 2840 | sage: K.<a> = NumberField(x^3-2) |
| 2841 | sage: S.<X> = K[] |
| 2842 | sage: L.<b> = NumberField(X^3 + 17); L |
| 2843 | Number Field in b with defining polynomial X^3 + 17 over its base field |
| 2844 | sage: b.absolute_charpoly() |
| 2845 | x^9 + 51*x^6 + 867*x^3 + 4913 |
| 2846 | sage: b.charpoly()(b) |
| 2847 | 0 |
| 2848 | sage: a = L.0; a |
| 2849 | b |
| 2850 | sage: a.absolute_charpoly('x') |
| 2851 | x^9 + 51*x^6 + 867*x^3 + 4913 |
| 2852 | sage: a.absolute_charpoly('y') |
| 2853 | y^9 + 51*y^6 + 867*y^3 + 4913 |
| 2854 | |
| 2855 | sage: a.absolute_charpoly(algorithm='pari') == a.absolute_charpoly(algorithm='sage') |
| 2856 | True |
| 2857 | """ |
| 2858 | if algorithm is None: |
| 2859 | # this might not be the optimal condition; maybe it should |
| 2860 | # be .degree() instead of .absolute_degree() |
| 2861 | # there are too many bugs in relative number fields to |
| 2862 | # figure this out now |
| 2863 | if self._parent.absolute_degree() <= TUNE_CHARPOLY_NF: |
| 2864 | algorithm = 'pari' |
| 2865 | else: |
| 2866 | algorithm = 'sage' |
| 2867 | g = self.polynomial() # in QQ[x] |
| 2868 | R = QQ[var] |
| 2869 | if algorithm == 'pari': |
| 2870 | f = self.number_field().pari_polynomial() # # field is QQ[x]/(f) |
| 2871 | return R((g._pari_().Mod(f)).charpoly()).change_variable_name(var) |
| 2872 | if algorithm == 'sage': |
| 2873 | return R(self.matrix(QQ).charpoly()) |
| 2874 | |
| 2875 | def absolute_minpoly(self, var='x', algorithm=None): |
| 2876 | r""" |
| 2877 | Return the minimal polynomial over `\QQ` of this element. |
| 2878 | |
| 2879 | For the meaning of the optional argument algorithm, see :meth:`absolute_charpoly`. |
| 2880 | |
| 2881 | EXAMPLES:: |
| 2882 | |
| 2883 | sage: K.<a, b> = NumberField([x^2 + 2, x^2 + 1000*x + 1]) |
| 2884 | sage: y = K['y'].0 |
| 2885 | sage: L.<c> = K.extension(y^2 + a*y + b) |
| 2886 | sage: c.absolute_charpoly() |
| 2887 | x^8 - 1996*x^6 + 996006*x^4 + 1997996*x^2 + 1 |
| 2888 | sage: c.absolute_minpoly() |
| 2889 | x^8 - 1996*x^6 + 996006*x^4 + 1997996*x^2 + 1 |
| 2890 | sage: L(a).absolute_charpoly() |
| 2891 | x^8 + 8*x^6 + 24*x^4 + 32*x^2 + 16 |
| 2892 | sage: L(a).absolute_minpoly() |
| 2893 | x^2 + 2 |
| 2894 | sage: L(b).absolute_charpoly() |
| 2895 | x^8 + 4000*x^7 + 6000004*x^6 + 4000012000*x^5 + 1000012000006*x^4 + 4000012000*x^3 + 6000004*x^2 + 4000*x + 1 |
| 2896 | sage: L(b).absolute_minpoly() |
| 2897 | x^2 + 1000*x + 1 |
| 2898 | """ |
| 2899 | return self.absolute_charpoly(var, algorithm).radical() |
| 2900 | |
| 2901 | def valuation(self, P): |
| 2902 | """ |
| 2903 | Returns the valuation of self at a given prime ideal P. |
| 2904 | |
| 2905 | INPUT: |
| 2906 | |
| 2907 | |
| 2908 | - ``P`` - a prime ideal of relative number field which is the parent of self |
| 2909 | |
| 2910 | |
| 2911 | EXAMPLES:: |
| 2912 | |
| 2913 | sage: K.<a, b, c> = NumberField([x^2 - 2, x^2 - 3, x^2 - 5]) |
| 2914 | sage: P = K.prime_factors(5)[0] |
| 2915 | sage: (2*a + b - c).valuation(P) |
| 2916 | 1 |
| 2917 | """ |
| 2918 | P_abs = P.absolute_ideal() |
| 2919 | abs = P_abs.number_field() |
| 2920 | to_abs = abs.structure()[1] |
| 2921 | return to_abs(self).valuation(P_abs) |
| 2922 | |
| 2923 | |
| 2924 | cdef class OrderElement_absolute(NumberFieldElement_absolute): |
| 2925 | """ |
| 2926 | Element of an order in an absolute number field. |
| 2927 | |
| 2928 | EXAMPLES:: |
| 2929 | |
| 2930 | sage: K.<a> = NumberField(x^2 + 1) |
| 2931 | sage: O2 = K.order(2*a) |
| 2932 | sage: w = O2.1; w |
| 2933 | 2*a |
| 2934 | sage: parent(w) |
| 2935 | Order in Number Field in a with defining polynomial x^2 + 1 |
| 2936 | |
| 2937 | sage: w.absolute_charpoly() |
| 2938 | x^2 + 4 |
| 2939 | sage: w.absolute_charpoly().parent() |
| 2940 | Univariate Polynomial Ring in x over Integer Ring |
| 2941 | sage: w.absolute_minpoly() |
| 2942 | x^2 + 4 |
| 2943 | sage: w.absolute_minpoly().parent() |
| 2944 | Univariate Polynomial Ring in x over Integer Ring |
| 2945 | |
| 2946 | TESTS:: |
| 2947 | |
| 2948 | This verifies that trac #4190 is fixed:: |
| 2949 | |
| 2950 | sage: K = NumberField(x^3 - 17, 'a') |
| 2951 | sage: OK = K.ring_of_integers() |
| 2952 | sage: a = OK(K.gen()) |
| 2953 | sage: (17/a) in OK |
| 2954 | True |
| 2955 | sage: (17/a).parent() is K |
| 2956 | True |
| 2957 | sage: (17/(2*a)).parent() is K |
| 2958 | True |
| 2959 | sage: (17/(2*a)) in OK |
| 2960 | False |
| 2961 | """ |
| 2962 | def __init__(self, order, f): |
| 2963 | K = order.number_field() |
| 2964 | NumberFieldElement_absolute.__init__(self, K, f) |
| 2965 | self._number_field = K |
| 2966 | (<Element>self)._parent = order |
| 2967 | |
| 2968 | cdef number_field(self): |
| 2969 | return self._number_field |
| 2970 | |
| 2971 | def inverse_mod(self, I): |
| 2972 | r""" |
| 2973 | Return an inverse of self modulo the given ideal. |
| 2974 | |
| 2975 | INPUT: |
| 2976 | |
| 2977 | - ``I`` - may be an ideal of self.parent(), or an element |
| 2978 | or list of elements of self.parent() generating a |
| 2979 | nonzero ideal. A ValueError is raised if I is |
| 2980 | non-integral or is zero. A ZeroDivisionError is raised |
| 2981 | if I + (x) != (1). |
| 2982 | |
| 2983 | EXAMPLES:: |
| 2984 | |
| 2985 | sage: OE = NumberField(x^3 - x + 2, 'w').ring_of_integers() |
| 2986 | sage: w = OE.ring_generators()[0] |
| 2987 | sage: w.inverse_mod(13*OE) |
| 2988 | 6*w^2 - 6 |
| 2989 | sage: w * (w.inverse_mod(13)) - 1 in 13*OE |
| 2990 | True |
| 2991 | sage: w.inverse_mod(13).parent() == OE |
| 2992 | True |
| 2993 | sage: w.inverse_mod(2*OE) |
| 2994 | Traceback (most recent call last): |
| 2995 | ... |
| 2996 | ZeroDivisionError: w is not invertible modulo Fractional ideal (2) |
| 2997 | """ |
| 2998 | R = self.parent() |
| 2999 | return R(_inverse_mod_generic(self, I)) |
| 3000 | |
| 3001 | def __invert__(self): |
| 3002 | r""" |
| 3003 | Implement inversion, checking that the return value has the right |
| 3004 | parent. See trac #4190. |
| 3005 | |
| 3006 | EXAMPLE:: |
| 3007 | |
| 3008 | sage: K = NumberField(x^3 -x + 2, 'a') |
| 3009 | sage: OK = K.ring_of_integers() |
| 3010 | sage: a = OK(K.gen()) |
| 3011 | sage: (~a).parent() is K |
| 3012 | True |
| 3013 | sage: (~a) in OK |
| 3014 | False |
| 3015 | sage: a**(-1) in OK |
| 3016 | False |
| 3017 | """ |
| 3018 | return self._parent.number_field()(NumberFieldElement_absolute.__invert__(self)) |
| 3019 | |
| 3020 | cdef class OrderElement_relative(NumberFieldElement_relative): |
| 3021 | """ |
| 3022 | Element of an order in a relative number field. |
| 3023 | |
| 3024 | EXAMPLES:: |
| 3025 | |
| 3026 | sage: O = EquationOrder([x^2 + x + 1, x^3 - 2],'a,b') |
| 3027 | sage: c = O.1; c |
| 3028 | (-2*b^2 - 2)*a - 2*b^2 - b |
| 3029 | sage: type(c) |
| 3030 | <type 'sage.rings.number_field.number_field_element.OrderElement_relative'> |
| 3031 | """ |
| 3032 | def __init__(self, order, f): |
| 3033 | K = order.number_field() |
| 3034 | NumberFieldElement_relative.__init__(self, K, f) |
| 3035 | (<Element>self)._parent = order |
| 3036 | self._number_field = K |
| 3037 | |
| 3038 | cdef number_field(self): |
| 3039 | return self._number_field |
| 3040 | |
| 3041 | cdef _new(self): |
| 3042 | """ |
| 3043 | Quickly creates a new initialized NumberFieldElement with the same |
| 3044 | parent as self. |
| 3045 | |
| 3046 | EXAMPLES: |
| 3047 | |
| 3048 | This is called implicitly in multiplication:: |
| 3049 | |
| 3050 | sage: O = EquationOrder([x^2 + 18, x^3 + 2], 'a,b') |
| 3051 | sage: c = O.1 * O.2; c |
| 3052 | (-23321*b^2 - 9504*b + 10830)*a + 10152*b^2 - 104562*b - 110158 |
| 3053 | sage: parent(c) == O |
| 3054 | True |
| 3055 | """ |
| 3056 | raise NotImplementedError |
| 3057 | |
| 3058 | cpdef RingElement _div_(self, RingElement other): |
| 3059 | r""" |
| 3060 | Implement division, checking that the result has the right parent. |
| 3061 | It's not so crucial what the parent actually is, but it is crucial |
| 3062 | that the returned value really is an element of its supposed |
| 3063 | parent. This fixes trac #4190. |
| 3064 | |
| 3065 | EXAMPLES:: |
| 3066 | |
| 3067 | sage: K1.<a> = NumberField(x^3 - 17) |
| 3068 | sage: R.<y> = K1[] |
| 3069 | sage: K2 = K1.extension(y^2 - a, 'b') |
| 3070 | sage: OK2 = K2.order(K2.gen()) # (not maximal) |
| 3071 | sage: b = OK2.gens()[1]; b |
| 3072 | b |
| 3073 | sage: (17/b).parent() is K2 |
| 3074 | True |
| 3075 | sage: (17/b) in OK2 # not implemented (#4193) |
| 3076 | True |
| 3077 | sage: (17/b^7) in OK2 |
| 3078 | False |
| 3079 | """ |
| 3080 | cdef NumberFieldElement_relative x |
| 3081 | x = NumberFieldElement_relative._div_(self, other) |
| 3082 | return self._parent.number_field()(x) |
| 3083 | |
| 3084 | def __invert__(self): |
| 3085 | r""" |
| 3086 | Implement division, checking that the result has the right parent. |
| 3087 | See trac #4190. |
| 3088 | |
| 3089 | EXAMPLES:: |
| 3090 | |
| 3091 | sage: K1.<a> = NumberField(x^3 - 17) |
| 3092 | sage: R.<y> = K1[] |
| 3093 | sage: K2 = K1.extension(y^2 - a, 'b') |
| 3094 | sage: OK2 = K2.order(K2.gen()) # (not maximal) |
| 3095 | sage: b = OK2.gens()[1]; b |
| 3096 | b |
| 3097 | sage: b.parent() is OK2 |
| 3098 | True |
| 3099 | sage: (~b).parent() is K2 |
| 3100 | True |
| 3101 | sage: (~b) in OK2 # not implemented (#4193) |
| 3102 | False |
| 3103 | sage: b**(-1) in OK2 # not implemented (#4193) |
| 3104 | False |
| 3105 | """ |
| 3106 | return self._parent.number_field()(NumberFieldElement_relative.__invert__(self)) |
| 3107 | |
| 3108 | def inverse_mod(self, I): |
| 3109 | r""" |
| 3110 | Return an inverse of self modulo the given ideal. |
| 3111 | |
| 3112 | INPUT: |
| 3113 | |
| 3114 | |
| 3115 | - ``I`` - may be an ideal of self.parent(), or an |
| 3116 | element or list of elements of self.parent() generating a nonzero |
| 3117 | ideal. A ValueError is raised if I is non-integral or is zero. |
| 3118 | A ZeroDivisionError is raised if I + (x) != (1). |
| 3119 | |
| 3120 | |
| 3121 | EXAMPLES:: |
| 3122 | |
| 3123 | sage: E.<a,b> = NumberField([x^2 - x + 2, x^2 + 1]) |
| 3124 | sage: OE = E.ring_of_integers() |
| 3125 | sage: t = OE(b - a).inverse_mod(17*b) |
| 3126 | sage: t*(b - a) - 1 in E.ideal(17*b) |
| 3127 | True |
| 3128 | sage: t.parent() == OE |
| 3129 | True |
| 3130 | """ |
| 3131 | R = self.parent() |
| 3132 | return R(_inverse_mod_generic(self, I)) |
| 3133 | |
| 3134 | def charpoly(self, var='x'): |
| 3135 | r""" |
| 3136 | The characteristic polynomial of this order element over its base ring. |
| 3137 | |
| 3138 | This special implementation works around bug \#4738. At this |
| 3139 | time the base ring of relative order elements is ZZ; it should |
| 3140 | be the ring of integers of the base field. |
| 3141 | |
| 3142 | EXAMPLES:: |
| 3143 | |
| 3144 | sage: x = ZZ['x'].0 |
| 3145 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
| 3146 | sage: OK = K.maximal_order(); OK.basis() |
| 3147 | [1, 1/2*a - 1/2*b, -1/2*b*a + 1/2, a] |
| 3148 | sage: charpoly(OK.1) |
| 3149 | x^2 + b*x + 1 |
| 3150 | sage: charpoly(OK.1).parent() |
| 3151 | Univariate Polynomial Ring in x over Maximal Order in Number Field in b with defining polynomial x^2 - 3 |
| 3152 | sage: [ charpoly(t) for t in OK.basis() ] |
| 3153 | [x^2 - 2*x + 1, x^2 + b*x + 1, x^2 - x + 1, x^2 + 1] |
| 3154 | """ |
| 3155 | R = self.parent().number_field().base_field().ring_of_integers()[var] |
| 3156 | return R(self.matrix().charpoly(var)) |
| 3157 | |
| 3158 | def minpoly(self, var='x'): |
| 3159 | r""" |
| 3160 | The minimal polynomial of this order element over its base ring. |
| 3161 | |
| 3162 | This special implementation works around bug \#4738. At this |
| 3163 | time the base ring of relative order elements is ZZ; it should |
| 3164 | be the ring of integers of the base field. |
| 3165 | |
| 3166 | EXAMPLES:: |
| 3167 | |
| 3168 | sage: x = ZZ['x'].0 |
| 3169 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
| 3170 | sage: OK = K.maximal_order(); OK.basis() |
| 3171 | [1, 1/2*a - 1/2*b, -1/2*b*a + 1/2, a] |
| 3172 | sage: minpoly(OK.1) |
| 3173 | x^2 + b*x + 1 |
| 3174 | sage: charpoly(OK.1).parent() |
| 3175 | Univariate Polynomial Ring in x over Maximal Order in Number Field in b with defining polynomial x^2 - 3 |
| 3176 | sage: _, u, _, v = OK.basis() |
| 3177 | sage: t = 2*u - v; t |
| 3178 | -b |
| 3179 | sage: t.charpoly() |
| 3180 | x^2 + 2*b*x + 3 |
| 3181 | sage: t.minpoly() |
| 3182 | x + b |
| 3183 | |
| 3184 | sage: t.absolute_charpoly() |
| 3185 | x^4 - 6*x^2 + 9 |
| 3186 | sage: t.absolute_minpoly() |
| 3187 | x^2 - 3 |
| 3188 | """ |
| 3189 | K = self.parent().number_field() |
| 3190 | R = K.base_field().ring_of_integers()[var] |
| 3191 | return R(K(self).minpoly(var)) |
| 3192 | |
| 3193 | def absolute_charpoly(self, var='x'): |
| 3194 | r""" |
| 3195 | The absolute characteristic polynomial of this order element over ZZ. |
| 3196 | |
| 3197 | EXAMPLES:: |
| 3198 | |
| 3199 | sage: x = ZZ['x'].0 |
| 3200 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
| 3201 | sage: OK = K.maximal_order() |
| 3202 | sage: _, u, _, v = OK.basis() |
| 3203 | sage: t = 2*u - v; t |
| 3204 | -b |
| 3205 | sage: t.absolute_charpoly() |
| 3206 | x^4 - 6*x^2 + 9 |
| 3207 | sage: t.absolute_minpoly() |
| 3208 | x^2 - 3 |
| 3209 | sage: t.absolute_charpoly().parent() |
| 3210 | Univariate Polynomial Ring in x over Integer Ring |
| 3211 | """ |
| 3212 | K = self.parent().number_field() |
| 3213 | R = ZZ[var] |
| 3214 | return R(K(self).absolute_charpoly(var)) |
| 3215 | |
| 3216 | def absolute_minpoly(self, var='x'): |
| 3217 | r""" |
| 3218 | The absolute minimal polynomial of this order element over ZZ. |
| 3219 | |
| 3220 | EXAMPLES:: |
| 3221 | |
| 3222 | sage: x = ZZ['x'].0 |
| 3223 | sage: K.<a,b> = NumberField([x^2 + 1, x^2 - 3]) |
| 3224 | sage: OK = K.maximal_order() |
| 3225 | sage: _, u, _, v = OK.basis() |
| 3226 | sage: t = 2*u - v; t |
| 3227 | -b |
| 3228 | sage: t.absolute_charpoly() |
| 3229 | x^4 - 6*x^2 + 9 |
| 3230 | sage: t.absolute_minpoly() |
| 3231 | x^2 - 3 |
| 3232 | sage: t.absolute_minpoly().parent() |
| 3233 | Univariate Polynomial Ring in x over Integer Ring |
| 3234 | """ |
| 3235 | K = self.parent().number_field() |
| 3236 | R = ZZ[var] |
| 3237 | return R(K(self).absolute_minpoly(var)) |
| 3238 | |
| 3239 | |
| 3240 | |
| 3241 | class CoordinateFunction: |
| 3242 | def __init__(self, NumberFieldElement alpha, W, to_V): |
| 3243 | self.__alpha = alpha |
| 3244 | self.__W = W |
| 3245 | self.__to_V = to_V |
| 3246 | self.__K = alpha.number_field() |
| 3247 | |
| 3248 | def __repr__(self): |
| 3249 | return "Coordinate function that writes elements in terms of the powers of %s"%self.__alpha |
| 3250 | |
| 3251 | def alpha(self): |
| 3252 | return self.__alpha |
| 3253 | |
| 3254 | def __call__(self, x): |
| 3255 | return self.__W.coordinates(self.__to_V(self.__K(x))) |
| 3256 | |
| 3257 | |
| 3258 | |
| 3259 | |
| 3260 | |