Here is a possible scenario, depending on the situation, of the construction of the field `F`

, and of the given algebraic number `p`

, this may / must be adapted. I am working in the interpreter, so the answer is based on the fact that the sage interpreter consumes my tries, and i see which objects are constructed at each step. The plan to be implemented is simple, start with a field $F=\Bbb Q(a,b,c,\dots,u)$, and an algebraic number $\xi$, build the polynomial ring $F[X]$, consider the minimal polynomial $f$ of $\xi$ over $\Bbb Z$, and factorize $f(X)$ over $F$. Well, if there is only one factor, then we hand it to the output, we are done. If not, two or more factor appear. They have coefficients in $F$. Let $f_1$ be one such factor, $f_1\in F[X]$. And now we want $f(\xi)$, but there is a question. In which field or algebra shall we perform this operation?

**Some experiments:**

To see what may happen, let us experiment first.

Assume our field is the field $F=\Bbb Q(\sqrt{-1},\sqrt 7)$. We would like to see which is the minimal polynomial of the algebraic numbers
$$p=\cos\frac{2pi}7\ ,\ q=\cos\frac{2pi}7\ $$
over this field. So let us introduce these objects.

```
F.<j, a> = QQ[sqrt(-1), sqrt(7)]
RF.<X> = PolynomialRing(F)
# p = cos( 2*pi*i / 7 ) # does not work with cos...
p = 1/2 * ( exp(2*pi*i /7) + exp(-2*pi*i /7) )
q = 1/2/i * ( exp(2*pi*i /7) - exp(-2*pi*i /7) )
```

The definition of $p$ using $\cos$ has no `minpoly`

, but the above try to introduce `p`

, mathematically equivalent, was giving me an object with a minimal polynomial.

```
sage: p.minpoly()
x^3 + 1/2*x^2 - 1/2*x - 1/8
```

And we can construct the extension:

```
sage: K.<b> = F.extension(p.minpoly()(X))
sage: K
Number Field in b with defining polynomial X^3 + 1/2*X^2 - 1/2*X - 1/8 over its base field
```

So this is also the minimal polynomial over `F`

. Else sage would complain.
Doing the same with `q`

gives an error.

```
sage: q.minpoly()(X)
X^6 - 7/4*X^4 + 7/8*X^2 - 7/64
sage: K.<b> = F.extension(q.minpoly()(X))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
```

and the long error traceback message ends with:

```
ValueError: defining polynomial (X^6 - 7/4*X^4 + 7/8*X^2 - 7/64) must be irreducible
```

Well, if it is not, then let us factorize it! It is important to factorize over `F`

. This is done implicitly, tacitly if we have a polynomial in `X`

, the transcendental variable of $F[X]$.

```
factors = [f for f, mul in q.minpoly()(X).factor()]
```

And the factors are:

```
sage: for f in factors:
....: print(f)
....:
X^3 - 1/2*sqrt7*X^2 + 1/8*sqrt7
X^3 + 1/2*sqrt7*X^2 - 1/8*sqrt7
```

Exactly one of them is annihilating the algebraic number $q$. Can we evaluate each factor in `q`

. Well, we may suspect a problem here, since there is no implicit coercion when computing the above polynomials in $q$.
But the error message is irritating:

```
sage: f = factors[0]
sage: f(q)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: no common canonical parent for objects with parents:
'Number Field in I with defining polynomial x^2 + 1 over its base field' and 'Symbolic Ring'
```

What kind of number field is `F`

?
It turns out that there is no chance to convert $j=\sqrt{-1}$, $a=\sqrt 7$ to elements in $\bar{\Bbb Q}$.
What happens here?

Well, there is a difference... Let us compare:

```
sage: F.<j,a> = QQ[sqrt(-1), sqrt(7)]
sage: try:
....: print(f'{a} is {QQbar(a)} in QQbar.')
....: except:
....: print(f'{a} could not be converted to QQbar.')
....:
sqrt7 could not be converted to QQbar.
```

The constructor of `F`

does not provide the coercion.

```
sage: RQ.<x> = PolynomialRing(QQ)
sage: F.<j,a> = NumberField([x^2 + 1, x^2 - 7], embedding=[1.*i, 2])
sage: try:
....: print(f'{a} is {QQbar(a)} in QQbar.')
....: except:
....: print(f'{a} could not be converted to QQbar.')
....:
a could not be converted to QQbar.
```

And finally...

```
sage: F.<a> = NumberField(x^2 - 7, embedding=2)
sage: QQbar(a)
2.645751311064591?
```

so over $\Bbb Q(a)=\Bbb Q(\sqrt 7)$ the whole story **is working**:

```
RQ.<x> = PolynomialRing(QQ)
F.<a> = NumberField(x^2 - 7, embedding=2.64)
RF.<X> = PolynomialRing(F)
q = 1/2/i * ( exp(2*pi*i /7) - exp(-2*pi*i /7) )
f = [f for f, mul in q.minpoly()(X).factor() if f.base_extend(QQbar)(QQbar(q)) == 0][0]
print(f'f is the following polynomial: {f}')
```

And we get:

```
f is the following polynomial: X^3 - 1/2*a*X^2 + 1/8*a
```

What?! What is going on here?

**Note:** I have added from the start also $\sqrt{-1}$ to get an error. It is better to work with errors the whole time, when sage is not supporting (without issues) a special functionality yet. (Else readers in hurry can get the wrong message.) However, the positive result in a special case lets us hope that things can be manufactured in a more general setting.

**Solution by example:**

From the experiments so far we see that the initial plan has issues, is not well defined. So we insist below to have one common explicit world to move all algebraic numbers. Yes, it is $\bar{\Bbb Q}$. And we define $F$ somehow, so that a base extension to $\bar{\Bbb Q}$ is possible. And the following works:

```
R.<x> = PolynomialRing(QQ)
S.<Y> = PolynomialRing(QQbar)
F0.<a> = NumberField(x^2 - 7, embedding=2.64)
F.<j> = Fa.extension(x^2 + 1)
RF.<X> = PolynomialRing(F)
q = 1/2/i * ( exp(2*pi*i /7) - exp(-2*pi*i /7) )
Q = QQbar(q)
e = F.Hom(QQbar)(i)
# so e is an embedding F -> QQbar, e(a) is sqrt7, e(j) is I.
# i need a ring homomorphism F[x] -> QQbar[x] - no implemented functionality could be adapted to work
# doing this with bare hands...
def ex(f):
"""For a polynomial f in R = QQ[x] construct the map of f in S = QQbar[X],
determined by a -> e(a), j -> e(j) = I, and e(x) = X.
"""
coeffs = f.coefficients(sparse=False)
return sum([e(coeffs[k]) * X^k for k in range(len(coeffs)) ])
Irr_F_q = [f for f, mul in q.minpoly()(X).factor() if ex(f) (Q) == 0][0]
```

And we have the needed polynomial:

```
sage: Irr_F_q
X^3 - 1/2*a*X^2 + 1/8*a
```

A similar scheme should work also for other towers of fields...

Then define

`p`

as an element of extension of`F`

first.Not a full answer, but you could start by doing

`QQbar(sqrt(6)).minpoly().change_ring(F).factor()`