Skip to content

Commit

Permalink
Trac #21105: abs for number field element
Browse files Browse the repository at this point in the history
For a number field element `a`, `abs(a)` currently returns a floating
point real number
{{{
sage: K.<cbrt2> = NumberField(x^3 - 2, 'a', embedding=1.26)
sage: abs(cbrt2)
1.25992104989487
sage: parent(_)
Real Field with 53 bits of precision
}}}
If a coercion embedding is defined with value in `RR`, the absolute
value can be defined internally as it is the case for `AA`
{{{
sage: abs(AA(2).sqrt() - AA(2))
0.5857864376269049?
}}}
and also for quadratic extensions
{{{
sage: K.<sqrt2> = NumberField(x^2 - 2, 'a', embedding=1.41)
sage: abs(sqrt2)
sqrt2
}}}
We propose to change the behavior for embedded number fields. Namely
make `abs` an internal operation.

As a (minor) consequence of the current behavior, the inequalities from
`sage.geometry.polyhedron.representation.Inequality` gets badly
displayed
{{{
sage: K.<cbrt2> = NumberField(x^3 - 2, 'a', embedding=1.26)
sage: P = Polyhedron(vertices=[(1,1,cbrt2),(cbrt2,1,1)])
sage: P.inequalities()
(An inequality (-cbrt2^2 - cbrt2 - 1, 0, 0) x + 4.84732210186307 >= 0,
 An inequality (cbrt2^2 + cbrt2 + 1, 0, 0) x - 3.84732210186307 >= 0)
}}}

URL: https://trac.sagemath.org/21105
Reported by: vdelecroix
Ticket author(s): Matthias Koeppe
Reviewer(s): Vincent Delecroix
  • Loading branch information
Release Manager authored and vbraun committed Aug 6, 2016
2 parents 1a3f9a0 + 15d160e commit 6002890
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 26 deletions.
10 changes: 10 additions & 0 deletions src/sage/geometry/polyhedron/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,16 @@ def _repr_(self):
(An equation -1 == 0,)
sage: Polyhedron(eqns=[(-1,0)]).Hrepresentation()
(An equation -1 == 0,)
TESTS:
Test that :trac:`21105` has been fixed::
sage: K.<cbrt2> = NumberField(x^3 - 2, 'a', embedding=1.26)
sage: P = Polyhedron(vertices=[(1,1,cbrt2),(cbrt2,1,1)])
sage: P.inequalities()
(An inequality (-cbrt2^2 - cbrt2 - 1, 0, 0) x + cbrt2^2 + cbrt2 + 2 >= 0,
An inequality (cbrt2^2 + cbrt2 + 1, 0, 0) x - cbrt2^2 + cbrt2 + 1 >= 0)
"""
s = 'An inequality '
have_A = not self.A().is_zero()
Expand Down
101 changes: 75 additions & 26 deletions src/sage/rings/number_field/number_field_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -955,12 +955,15 @@ cdef class NumberFieldElement(FieldElement):
return 0 # No error


# TODO: this is wrong if there is a real embedding specified
def __abs__(self):
r"""
Return the numerical absolute value of this number field element
with respect to the first archimedean embedding, to double
precision.
Return the absolute value of this number field element.
If a real-valued coercion embedding is defined, the
returned absolute value is an element of the same field.
Otherwise, it is the numerical absolute value with respect to
the first archimedean embedding, to double precision.
This is the ``abs( )`` Python function. If you want a
different embedding or precision, use
Expand All @@ -971,12 +974,27 @@ cdef class NumberFieldElement(FieldElement):
sage: k.<a> = NumberField(x^3 - 2)
sage: abs(a)
1.25992104989487
sage: a.abs()
1.25992104989487
sage: abs(a)^3
2.00000000000000
sage: a.abs()^3
2.00000000000000
sage: a.abs(prec=128)
1.2599210498948731647672106072782283506
Number field with a real-valued coercion embedding
(:trac:`21105`)::
sage: k.<cbrt2> = NumberField(x^3 - 2, embedding=1.26)
sage: abs(cbrt2)
cbrt2
sage: cbrt2.abs()
cbrt2
sage: abs(cbrt2)^3
2
"""
return self.abs(prec=53, i=None)
return self.abs()

def sign(self):
r"""
Expand Down Expand Up @@ -1142,25 +1160,30 @@ cdef class NumberFieldElement(FieldElement):
upp = a.upper().ceil()
return low

def abs(self, prec=53, i=None):
r"""
Return the absolute value of this element.
def abs(self, prec=None, i=None):
r"""Return the absolute value of this element.
If ``i`` is provided, then the absolute value of the `i`-th
embedding is given.
If ``i`` is provided, then the absolute of the `i`-th embedding is
given. Otherwise, if the number field as a defined embedding into `\CC`
then the corresponding absolute value is returned and if there is none,
it corresponds to the choice ``i=0``.
Otherwise, if the number field has a coercion embedding into
`\RR`, the corresponding absolute value is returned as an
element of the same field (unless ``prec`` is given).
Otherwise, if it has a coercion embedding into
`\CC`, then the corresponding absolute value is returned.
Finally, if there is no coercion embedding, `i` defaults to 0.
If prec is 53 (the default), then the complex double field is
used; otherwise the arbitrary precision (but slow) complex
field is used.
For the computation, the complex field with ``prec`` bits of
precision is used, defaulting to 53 bits of precision if
``prec`` is not provided. The result is in the corresponding
real field.
INPUT:
- ``prec`` - (default: 53) integer bits of precision
- ``prec`` - (default: None) integer bits of precision
- ``i`` - (default: ) integer, which embedding to
- ``i`` - (default: None) integer, which embedding to
use
Expand All @@ -1181,9 +1204,7 @@ cdef class NumberFieldElement(FieldElement):
sage: a.abs(100, 2)
2.5712815906582353554531872087
Here's one where the absolute value depends on the embedding.
::
Here's one where the absolute value depends on the embedding::
sage: K.<b> = NumberField(x^2-2)
sage: a = 1 + b
Expand All @@ -1201,14 +1222,42 @@ cdef class NumberFieldElement(FieldElement):
sage: K.<b> = NumberField(f, embedding=beta)
sage: b.abs()
1.32471795724475
Check that for fields with real coercion embeddings, absolute
values are in the same field (:trac:`21105`)::
sage: x = polygen(ZZ)
sage: f = x^3 - x - 1
sage: K.<b> = NumberField(f, embedding=1.3)
sage: b.abs()
b
However, if a specific embedding is requested, the behavior reverts
to that of number fields without a coercion embedding into `\RR`::
sage: b.abs(i=2)
1.32471795724475
Also, if a precision is requested explicitly, the behavior reverts
to that of number fields without a coercion embedding into `\RR`::
sage: b.abs(prec=53)
1.32471795724475
"""
CCprec = ComplexField(prec)
if i is None and CCprec.has_coerce_map_from(self.parent()):
return CCprec(self).abs()
if (i is None and prec is None
and (<number_field_base.NumberField> self._parent)._embedded_real):
return self.sign() * self
else:
i = 0 if i is None else i
P = self.number_field().complex_embeddings(prec)[i]
return P(self).abs()
if prec is None:
prec = 53
CCprec = ComplexField(prec)
if i is None and CCprec.has_coerce_map_from(self.parent()):
return CCprec(self).abs()
else:
i = 0 if i is None else i
P = self.number_field().complex_embeddings(prec)[i]
return P(self).abs()

def abs_non_arch(self, P, prec=None):
r"""
Expand Down

0 comments on commit 6002890

Please sign in to comment.