# How to check if a complex number belongs to a quadratic field

I am working with lists of complex numbers and want to be able to determine which out of these lists contains only elements from a certain quadratic field, $$\mathbb{Q}(\sqrt D) = \{ a+b\sqrt D :a,b \in \mathbb{Q}\}$$. Here, $$D$$ is an integer, positive or negative.

What I want: I am trying to write a function checkQ[D_,z_] which returns True if a complex number z belongs to $$\mathbb{Q}(\sqrt{D})$$ and False otherwise.

I have a piecemeal function written currently to do this, but it (a) sometimes fails, and (b) is slow. Unfortunately, I am working with quite large lists, so it does me no good to have a slow function to check this (or a function that doesn't always work, for that matter.)

A simple method:

ToNumberField[Sqrt[2], ToNumberField[{Sqrt[2], I}, All][[1, 1]]]
ToNumberField[Sqrt[3], ToNumberField[{Sqrt[2], I}, All][[1, 1]]]
ToNumberField[Sqrt[2] + Sqrt[3] I,
ToNumberField[{Sqrt[2], I}, All][[1, 1]]]


The above codes determine in turn whether $$\sqrt{2}$$, $$\sqrt{2}+\sqrt{3} I$$ are in the double quadratic extension domain $$\mathbb{Q}(\sqrt 2,I)$$.

Or use the following Python code to make some judgments:

import os
import sympy as sy
from sympy import minimal_polynomial,sqrt,solve,QQ,Rational,poly
#Floating point numbers are best represented in rational

from sympy.abc import x,y,z
x1=sqrt(2)
x2=sqrt(3)
x3=sqrt(2)+sqrt(3)
x4=2**Rational(1,3)*(Rational(-1,2)+Rational(1,2)*3**Rational(1,2)*sqrt(-1))

s=minimal_polynomial(x3,x,domain=QQ.algebraic_field(x1,x2))
print(s)
print(sy.latex(s))
s2=minimal_polynomial(x2,x,domain=QQ.algebraic_field(x1,x4))
print(s2)


The above method may not be able to deal with complex algebraic numbers.

We can also use the function MinimalPolynomial of MMA to make relevant judgment, but there may be bugs in function MinimalPolynomial that have not been fixed:

F = ToNumberField[{Sqrt[3], I}, All][[1, 1]]
MinimalPolynomial[Sqrt[3] + 2 I, x, Extension -> F]
MinimalPolynomial[Sqrt[2] + 2 I, x, Extension -> F]

• I had not known that was a function. This is perfect, thank you. Commented Nov 7, 2020 at 3:37