1 | initial version |
Samuel gave a much nicer answer that i would to test inclusion in number fields. Let me still try to explain why your method does not give the answer you expected. When you write:
sage: x = 2 + sqrt(5)
you define an element of the symbolic ring. To get convinced, just type:
sage: x.parent()
Symbolic Ring
The symbolic ring is a collection of expressions like sqrt(2) but also cos(x) for example, where Sage can do symbolic computation (addition, square root, derivative, ...). Now, when you write:
sage: K = QuadraticField(5)
You define a quadratic number field. Both worlds are quite far. When you write:
sage: x in K
this is a shortcut to:
sage: K.__contains__(x)
So you ask to K whether it contains x. You can see the source of this method by typing:
sage: sage: K.__contains__??
You can see that the method try to convert x to an element of K
source: x2 = self(x)
and since
sage: x2 = K(x)
returns a TypeError (meaning that sage is not (yet?) able to do the conversion), the method returns False.
If you want some algebraic information about your symbolic expression x, you can convert it into an algebraic number in the genuine algebraic field AA, and even to an element of a number field:
sage: x = 2 + sqrt(5)
sage: y = AA(x)
sage: z = y.as_number_field_element()
sage: z
(Number Field in a with defining polynomial y^2 - y - 1,
-2*a + 3,
Ring morphism:
From: Number Field in a with defining polynomial y^2 - y - 1
To: Algebraic Real Field
Defn: a |--> -0.618033988749895?)