# Revision history [back]

First, we can define the quadrati field of coefficients:

sage: R.<sqrt2> = QuadraticField(2)
sage: R
Number Field in sqrt2 with defining polynomial x^2 - 2 with sqrt2 = 1.414213562373095?
sage: sqrt2^2
2


An interesting method on those objects is the vector method:

sage: a = 3*sqrt2 + 1
sage: a
3*sqrt2 + 1
sage: a.vector()
(1, 3)
sage: a^2
6*sqrt2 + 19
sage: (a^2).vector()
(19, 6)


Now let us define you polynomial, let me use only 3 variables for the example:

sage: S.<x,y,z> = R[]
sage: S
Multivariate Polynomial Ring in x, y, z over Number Field in sqrt2 with defining polynomial x^2 - 2 with sqrt2 = 1.414213562373095?

sage: p = (1+3*sqrt2)*x*y + y*z^2
sage: p
y*z^2 + (3*sqrt2 + 1)*x*y


We can iterate over the coefficients as follows:

sage: for coeff, monom in p:
....:     print(coeff)
....:     print(monom)
....:     print(coeff*monom)
1
y*z^2
y*z^2
3*sqrt2 + 1
x*y
(3*sqrt2 + 1)*x*y


Now, we just have to combine such an iteration and the vector method to get what you want:

sage: q = sum(coeff.vector()[0]*monom for coeff, monom in p)
sage: q
y*z^2 + x*y
sage: r = sum(coeff.vector()[1]*monom for coeff, monom in p)
sage: r
3*x*y
sage: q+sqrt2*r
y*z^2 + (3*sqrt2 + 1)*x*y
sage: q+sqrt2*r == p
True


That's it !

sage: R.<sqrt2> = QuadraticField(2)
sage: R
Number Field in sqrt2 with defining polynomial x^2 - 2 with sqrt2 = 1.414213562373095?
sage: sqrt2^2
2


An interesting method on those objects is the vector method:

sage: a = 3*sqrt2 + 1
sage: a
3*sqrt2 + 1
sage: a.vector()
(1, 3)
sage: a^2
6*sqrt2 + 19
sage: (a^2).vector()
(19, 6)


Now let us define you polynomial, let me use only 3 variables for the example:

sage: S.<x,y,z> = R[]
sage: S
Multivariate Polynomial Ring in x, y, z over Number Field in sqrt2 with defining polynomial x^2 - 2 with sqrt2 = 1.414213562373095?

sage: p = (1+3*sqrt2)*x*y + y*z^2
sage: p
y*z^2 + (3*sqrt2 + 1)*x*y


We can iterate over the coefficients as follows:

sage: for coeff, monom in p:
....:     print(coeff)
....:     print(monom)
....:     print(coeff*monom)
1
y*z^2
y*z^2
3*sqrt2 + 1
x*y
(3*sqrt2 + 1)*x*y


Now, we just have to combine such an iteration and the vector method to get what you want:

sage: q = sum(coeff.vector()[0]*monom for coeff, monom in p)
sage: q
y*z^2 + x*y
sage: r = sum(coeff.vector()[1]*monom for coeff, monom in p)
sage: r
3*x*y
sage: q+sqrt2*r
y*z^2 + (3*sqrt2 + 1)*x*y
sage: q+sqrt2*r == p
True


That's it !