Ask Your Question

Quadratic number fields

asked 2013-04-12 05:32:51 -0600

pinwheel gravatar image

updated 2015-08-31 08:45:04 -0600

tmonteil gravatar image


I try to implement an inclusion test, of which I thought it must be quite simple, but until now I cannot figure out how to do it properly. Problem: Given any element x, test if x is included in some quadratic number field and return true/false. I tried the following:

x = 2 + sqrt(5)
K = QuadraticField(5)
print(x in K)

Here, the answer is 'no' or 'false' which is obviously wrong, so I assume I'm doing something completely wrong. What do I have to change?

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2013-04-14 13:00:53 -0600

Give a name to the generator of your quadratic field as follows.

sage: K.<a> = QuadraticField(5)

Taking square roots of elements in the field sometimes leaves the field, sometimes stays in it.

sage: K(2) + sqrt(K(3))
sqrt(3) + 2
sage: K(2) + sqrt(K(5))
a + 2

You can test membership in the field as follows.

sage: K(2) + sqrt(K(3)) in K
sage: K(2) + sqrt(K(5)) in K
edit flag offensive delete link more

answered 2013-04-15 02:19:29 -0600

tmonteil gravatar image

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?)
edit flag offensive delete link more

answered 2013-04-12 11:05:07 -0600

It really depends on what you mean by an element... A number is well defined in mathematics but not inside a computer: there are many ways in Sage to represent a number. What you can do is to work with the set of algebraic numbers QQbar and obtain the degree of any element there

sage: sqrt2 = QQbar(sqrt(2))
sage: sqrt3 = QQbar(sqrt(3))
sage: a = sqrt2 + sqrt3    # this element has degree 4
sage: a.minpoly()
x^4 - 10*x^2 + 1
sage: b = a **2            # this element has degree 2
sage: b.minpoly()
x^2 - 10*x + 1
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-04-12 05:32:51 -0600

Seen: 227 times

Last updated: Apr 15 '13