ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 04 Mar 2015 11:35:30 +0100Interpreting an element of CyclotomicField as an element of polynomial ringhttps://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/ I am trying to write a function that sums up numbers in different cyclotomic field formally, i.e., as expressions in $z$ where $z$ is the corresponding primitive root of unity. I run into a type error, however, as sage wouldn't let me sum elements of two different cyclotomic fields. Is there a way to typecast an expression in one cyclotomic field into the other, or, better yet, to a polynomial ring?Tue, 03 Mar 2015 15:12:21 +0100https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/Answer by tmonteil for <p>I am trying to write a function that sums up numbers in different cyclotomic field formally, i.e., as expressions in $z$ where $z$ is the corresponding primitive root of unity. I run into a type error, however, as sage wouldn't let me sum elements of two different cyclotomic fields. Is there a way to typecast an expression in one cyclotomic field into the other, or, better yet, to a polynomial ring?</p>
https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?answer=26024#post-id-26024**EDIT**: The comment of @BGS indicates that the question migh be understood as follows : given an element of a cyclotomic field, how to recover it as a polynomial of the generator, so that we can "recast" the generator into a generator of another cyclotomic field. Here is a simple solution, involving the `.polynomial()` method of cyclotomic field elements.
Finding the polynomial:
sage: C7 = CyclotomicField(7)
sage: z7 = C7.gen()
sage: a7 = z7 + 3*z7^2 + 1
sage: a7.polynomial()
x^3 + 3*x^2 + 1
sage: a7.polynomial().parent()
Univariate Polynomial Ring in x over Rational Field
Recasting into another cyclotomic field:
sage: C5 = CyclotomicField(5)
sage: z5 = C5.gen()
sage: a5 = a7.polynomial()(z5)
sage: a5
zeta5^3 + 3*zeta5^2 + 1
That said, the polynomial is not uniquely determined (unless you need the minimal one) and the recasting will depend on the polynomial, since for example:
sage: z5^5 + z5 == z5 + 1
True
sage: z7^5 + z7 == z7 + 1
False
**PREVIOUS ANSWER** (answering a question about embedding cyclotomic field generators into the algebraic field)
The problem is that the cyclotomic field is not well embedded into the complex plane (more precisely into the field of complex algebraic numbers), see [my answer of ask question 25822](http://ask.sagemath.org/question/25822/number-field-containing-realimaginary-part-of-algebraic-number/?answer=25832#post-id-25832) for more details about this.
sage: CC(z3)
-0.500000000000000 + 0.866025403784439*I
sage: C3 = CyclotomicField(3)
sage: z3 = C3.gen()
sage: z3
zeta3
sage: CC(z3)
-0.500000000000000 + 0.866025403784439*I
but
sage: QQbar(z3)
TypeError: Illegal initializer for algebraic number
Which is a pity. Let me propose the following fix:
sage: def repair(z):
....: F = z.parent()
....: for f in F.embeddings(QQbar):
....: if CLF(f.im_gens()[0]) - CLF(z) < 1e-14:
....: return f(z)
The function `repair` put an generator of some cyclotomic field into the algebraic field `QQbar` (if `n` is not too big, so that two generators are at distance larger than `2e-14`, but you can adapt the bound if needed).
So you can do:
sage: C3 = CyclotomicField(3)
sage: z3 = C3.gen()
sage: Z3 = repair(z3)
sage: Z3
-0.50000000000000000? - 0.866025403784439?*I
sage: Z3.parent()
Algebraic Field
sage: C4 = CyclotomicField(4)
sage: z4 = C4.gen()
sage: Z4 = repair(z4)
sage: Z4
-1*I
sage: Z4.parent()
Algebraic Field
Then you can do:
sage: Z3 + Z4
-0.50000000000000000? - 1.866025403784439?*I
sage: (Z3^2 + 2*Z3 + 1) + (Z4^3 + 7)
6.5000000000000000? + 0.1339745962155614?*I
sage: ((Z3^2 + 2*Z3 + 1) + (Z4^3 + 7)).minpoly()
x^4 - 26*x^3 + 257*x^2 - 1144*x + 1933
Tue, 03 Mar 2015 19:13:08 +0100https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?answer=26024#post-id-26024Comment by tmonteil for <p><strong>EDIT</strong>: The comment of <a href="/users/8675/bgs/">@BGS</a> indicates that the question migh be understood as follows : given an element of a cyclotomic field, how to recover it as a polynomial of the generator, so that we can "recast" the generator into a generator of another cyclotomic field. Here is a simple solution, involving the <code>.polynomial()</code> method of cyclotomic field elements.</p>
<p>Finding the polynomial: </p>
<pre><code>sage: C7 = CyclotomicField(7)
sage: z7 = C7.gen()
sage: a7 = z7 + 3*z7^2 + 1
sage: a7.polynomial()
x^3 + 3*x^2 + 1
sage: a7.polynomial().parent()
Univariate Polynomial Ring in x over Rational Field
</code></pre>
<p>Recasting into another cyclotomic field:</p>
<pre><code>sage: C5 = CyclotomicField(5)
sage: z5 = C5.gen()
sage: a5 = a7.polynomial()(z5)
sage: a5
zeta5^3 + 3*zeta5^2 + 1
</code></pre>
<p>That said, the polynomial is not uniquely determined (unless you need the minimal one) and the recasting will depend on the polynomial, since for example:</p>
<pre><code>sage: z5^5 + z5 == z5 + 1
True
sage: z7^5 + z7 == z7 + 1
False
</code></pre>
<p><strong>PREVIOUS ANSWER</strong> (answering a question about embedding cyclotomic field generators into the algebraic field)</p>
<p>The problem is that the cyclotomic field is not well embedded into the complex plane (more precisely into the field of complex algebraic numbers), see <a href="http://ask.sagemath.org/question/25822/number-field-containing-realimaginary-part-of-algebraic-number/?answer=25832#post-id-25832">my answer of ask question 25822</a> for more details about this.</p>
<pre><code>sage: CC(z3)
-0.500000000000000 + 0.866025403784439*I
sage: C3 = CyclotomicField(3)
sage: z3 = C3.gen()
sage: z3
zeta3
sage: CC(z3)
-0.500000000000000 + 0.866025403784439*I
</code></pre>
<p>but</p>
<pre><code>sage: QQbar(z3)
TypeError: Illegal initializer for algebraic number
</code></pre>
<p>Which is a pity. Let me propose the following fix:</p>
<pre><code>sage: def repair(z):
....: F = z.parent()
....: for f in F.embeddings(QQbar):
....: if CLF(f.im_gens()[0]) - CLF(z) < 1e-14:
....: return f(z)
</code></pre>
<p>The function <code>repair</code> put an generator of some cyclotomic field into the algebraic field <code>QQbar</code> (if <code>n</code> is not too big, so that two generators are at distance larger than <code>2e-14</code>, but you can adapt the bound if needed).</p>
<p>So you can do:</p>
<pre><code>sage: C3 = CyclotomicField(3)
sage: z3 = C3.gen()
sage: Z3 = repair(z3)
sage: Z3
-0.50000000000000000? - 0.866025403784439?*I
sage: Z3.parent()
Algebraic Field
sage: C4 = CyclotomicField(4)
sage: z4 = C4.gen()
sage: Z4 = repair(z4)
sage: Z4
-1*I
sage: Z4.parent()
Algebraic Field
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: Z3 + Z4
-0.50000000000000000? - 1.866025403784439?*I
sage: (Z3^2 + 2*Z3 + 1) + (Z4^3 + 7)
6.5000000000000000? + 0.1339745962155614?*I
sage: ((Z3^2 + 2*Z3 + 1) + (Z4^3 + 7)).minpoly()
x^4 - 26*x^3 + 257*x^2 - 1144*x + 1933
</code></pre>
https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?comment=26027#post-id-26027I do not understand, Z3 is different from Z4, it is not even an element of C4. Could you please give a concrete example of the behavior you would like to see, so that i can understand ? I edited my answer according to how i understand the question now.Wed, 04 Mar 2015 11:08:37 +0100https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?comment=26027#post-id-26027Comment by BGS for <p><strong>EDIT</strong>: The comment of <a href="/users/8675/bgs/">@BGS</a> indicates that the question migh be understood as follows : given an element of a cyclotomic field, how to recover it as a polynomial of the generator, so that we can "recast" the generator into a generator of another cyclotomic field. Here is a simple solution, involving the <code>.polynomial()</code> method of cyclotomic field elements.</p>
<p>Finding the polynomial: </p>
<pre><code>sage: C7 = CyclotomicField(7)
sage: z7 = C7.gen()
sage: a7 = z7 + 3*z7^2 + 1
sage: a7.polynomial()
x^3 + 3*x^2 + 1
sage: a7.polynomial().parent()
Univariate Polynomial Ring in x over Rational Field
</code></pre>
<p>Recasting into another cyclotomic field:</p>
<pre><code>sage: C5 = CyclotomicField(5)
sage: z5 = C5.gen()
sage: a5 = a7.polynomial()(z5)
sage: a5
zeta5^3 + 3*zeta5^2 + 1
</code></pre>
<p>That said, the polynomial is not uniquely determined (unless you need the minimal one) and the recasting will depend on the polynomial, since for example:</p>
<pre><code>sage: z5^5 + z5 == z5 + 1
True
sage: z7^5 + z7 == z7 + 1
False
</code></pre>
<p><strong>PREVIOUS ANSWER</strong> (answering a question about embedding cyclotomic field generators into the algebraic field)</p>
<p>The problem is that the cyclotomic field is not well embedded into the complex plane (more precisely into the field of complex algebraic numbers), see <a href="http://ask.sagemath.org/question/25822/number-field-containing-realimaginary-part-of-algebraic-number/?answer=25832#post-id-25832">my answer of ask question 25822</a> for more details about this.</p>
<pre><code>sage: CC(z3)
-0.500000000000000 + 0.866025403784439*I
sage: C3 = CyclotomicField(3)
sage: z3 = C3.gen()
sage: z3
zeta3
sage: CC(z3)
-0.500000000000000 + 0.866025403784439*I
</code></pre>
<p>but</p>
<pre><code>sage: QQbar(z3)
TypeError: Illegal initializer for algebraic number
</code></pre>
<p>Which is a pity. Let me propose the following fix:</p>
<pre><code>sage: def repair(z):
....: F = z.parent()
....: for f in F.embeddings(QQbar):
....: if CLF(f.im_gens()[0]) - CLF(z) < 1e-14:
....: return f(z)
</code></pre>
<p>The function <code>repair</code> put an generator of some cyclotomic field into the algebraic field <code>QQbar</code> (if <code>n</code> is not too big, so that two generators are at distance larger than <code>2e-14</code>, but you can adapt the bound if needed).</p>
<p>So you can do:</p>
<pre><code>sage: C3 = CyclotomicField(3)
sage: z3 = C3.gen()
sage: Z3 = repair(z3)
sage: Z3
-0.50000000000000000? - 0.866025403784439?*I
sage: Z3.parent()
Algebraic Field
sage: C4 = CyclotomicField(4)
sage: z4 = C4.gen()
sage: Z4 = repair(z4)
sage: Z4
-1*I
sage: Z4.parent()
Algebraic Field
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: Z3 + Z4
-0.50000000000000000? - 1.866025403784439?*I
sage: (Z3^2 + 2*Z3 + 1) + (Z4^3 + 7)
6.5000000000000000? + 0.1339745962155614?*I
sage: ((Z3^2 + 2*Z3 + 1) + (Z4^3 + 7)).minpoly()
x^4 - 26*x^3 + 257*x^2 - 1144*x + 1933
</code></pre>
https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?comment=26025#post-id-26025Thank you for your prompt reply. This is useful to know, but I had in mind something else. Specifically, what I was trying to do was to recast Z3 as the generator of Z4. I am trying to compute a certain semiclassical approximation, i.e., I try to successively recast a given expression in z as expressions in various cyclotomic fields.Tue, 03 Mar 2015 22:04:35 +0100https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?comment=26025#post-id-26025Answer by vdelecroix for <p>I am trying to write a function that sums up numbers in different cyclotomic field formally, i.e., as expressions in $z$ where $z$ is the corresponding primitive root of unity. I run into a type error, however, as sage wouldn't let me sum elements of two different cyclotomic fields. Is there a way to typecast an expression in one cyclotomic field into the other, or, better yet, to a polynomial ring?</p>
https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?answer=26026#post-id-26026Hello,
In Sage there is the universal cyclotomic field implemented. It would be much faster than QQbar as proposed by @tmonteil
sage: UCF = UniversalCyclotomicField()
sage: UCF.gen(3)
E(3)
sage: e3 = UCF.gen(3)
sage: e4 = UCF.gen(4)
sage: e3 + e4
E(12)^4 - E(12)^7 - E(12)^11
VincentWed, 04 Mar 2015 10:27:14 +0100https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?answer=26026#post-id-26026Comment by tmonteil for <p>Hello,</p>
<p>In Sage there is the universal cyclotomic field implemented. It would be much faster than QQbar as proposed by <a href="/users/1305/tmonteil/">@tmonteil</a></p>
<pre><code>sage: UCF = UniversalCyclotomicField()
sage: UCF.gen(3)
E(3)
sage: e3 = UCF.gen(3)
sage: e4 = UCF.gen(4)
sage: e3 + e4
E(12)^4 - E(12)^7 - E(12)^11
</code></pre>
<p>Vincent</p>
https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?comment=26029#post-id-26029Weird, in particular the special case of cyclotomic fields, there is a map from them to `QQbar`, just do:
sage: C3 = CyclotomicField(3)
sage: z3 = C3.gen()
sage: Z3 = QQbar(UniversalCyclotomicField()(z3))
sage: Z3
-0.500000000000000? + 0.866025403784439?*I
There is definitely an issue with embedded number fields.Wed, 04 Mar 2015 11:25:07 +0100https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?comment=26029#post-id-26029Comment by vdelecroix for <p>Hello,</p>
<p>In Sage there is the universal cyclotomic field implemented. It would be much faster than QQbar as proposed by <a href="/users/1305/tmonteil/">@tmonteil</a></p>
<pre><code>sage: UCF = UniversalCyclotomicField()
sage: UCF.gen(3)
E(3)
sage: e3 = UCF.gen(3)
sage: e4 = UCF.gen(4)
sage: e3 + e4
E(12)^4 - E(12)^7 - E(12)^11
</code></pre>
<p>Vincent</p>
https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?comment=26030#post-id-26030Indeed! There is a problem
sage: C3 = CyclotomicField(3)
sage: UCF = UniversalCyclotomicField()
sage: UCF.has_coerce_map_from(C3)
True
sage: QQbar.has_coerce_map_from(UCF)
True
sage: QQbar.has_coerce_map_from(C3)
FalseWed, 04 Mar 2015 11:35:30 +0100https://ask.sagemath.org/question/26017/interpreting-an-element-of-cyclotomicfield-as-an-element-of-polynomial-ring/?comment=26030#post-id-26030