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.Fri, 17 May 2013 05:51:46 +0200convert expression to QuadraticFieldhttps://ask.sagemath.org/question/10126/convert-expression-to-quadraticfield/I tried (in the Sage shell)
K = QuadraticField(-3)
a = K(sqrt(-3))
But that fails with a TypeError exception. What is the problem?
I know that I can get the generator by `K.gen()` but I want to be able to convert expressions to `K`.Thu, 16 May 2013 11:52:39 +0200https://ask.sagemath.org/question/10126/convert-expression-to-quadraticfield/Comment by kcrisman for <p>I tried (in the Sage shell)</p>
<pre><code>K = QuadraticField(-3)
a = K(sqrt(-3))
</code></pre>
<p>But that fails with a TypeError exception. What is the problem?</p>
<p>I know that I can get the generator by <code>K.gen()</code> but I want to be able to convert expressions to <code>K</code>.</p>
https://ask.sagemath.org/question/10126/convert-expression-to-quadraticfield/?comment=17681#post-id-17681I don't know that it will be so easy to coerce from the symbolic ring to here in general, but more power to you if someone can... I do find the following behavior a little disturbing - try `sage: K.gen()`, which yields `a`, which no one ever asked for... this *is* documented in `QuadraticField?` but only as the default variable name, which of course isn't injected into the global namespace.Thu, 16 May 2013 15:42:06 +0200https://ask.sagemath.org/question/10126/convert-expression-to-quadraticfield/?comment=17681#post-id-17681Answer by vdelecroix for <p>I tried (in the Sage shell)</p>
<pre><code>K = QuadraticField(-3)
a = K(sqrt(-3))
</code></pre>
<p>But that fails with a TypeError exception. What is the problem?</p>
<p>I know that I can get the generator by <code>K.gen()</code> but I want to be able to convert expressions to <code>K</code>.</p>
https://ask.sagemath.org/question/10126/convert-expression-to-quadraticfield/?answer=14938#post-id-14938Hi,
You should avoid usage of the symbolic ring as much as possible. It is useful to deal with functions, derivations and such but not to deal with arithmetic questions.
There is a class called `AlgebraicConverter` in `sage.symbolic.expression_conversions` which is implemented and called in such case. It works well for the field of algebraic numbers `QQbar`::
sage: QQbar(sqrt(-3))
1.732050807568878?*I
sage: QQbar(sin(pi/4))
0.7071067811865475?
But not for other number fields. Nevertheless it is possible to do
sage: K = QuadraticField(-3)
sage: K(3).sqrt()
a
The method `sqrt` above actually uses Pari in the background: if your number field contains such a root it returns it, otherwise the answer will be an element of the symbolic ring.
sage: K(3).sqrt().sqrt()
3^(1/4)Fri, 17 May 2013 05:48:47 +0200https://ask.sagemath.org/question/10126/convert-expression-to-quadraticfield/?answer=14938#post-id-14938Comment by vdelecroix for <p>Hi,</p>
<p>You should avoid usage of the symbolic ring as much as possible. It is useful to deal with functions, derivations and such but not to deal with arithmetic questions.</p>
<p>There is a class called <code>AlgebraicConverter</code> in <code>sage.symbolic.expression_conversions</code> which is implemented and called in such case. It works well for the field of algebraic numbers <code>QQbar</code>::</p>
<pre><code>sage: QQbar(sqrt(-3))
1.732050807568878?*I
sage: QQbar(sin(pi/4))
0.7071067811865475?
</code></pre>
<p>But not for other number fields. Nevertheless it is possible to do</p>
<pre><code>sage: K = QuadraticField(-3)
sage: K(3).sqrt()
a
</code></pre>
<p>The method <code>sqrt</code> above actually uses Pari in the background: if your number field contains such a root it returns it, otherwise the answer will be an element of the symbolic ring.</p>
<pre><code>sage: K(3).sqrt().sqrt()
3^(1/4)
</code></pre>
https://ask.sagemath.org/question/10126/convert-expression-to-quadraticfield/?comment=17675#post-id-17675I create a ticket for that http://trac.sagemath.org/sage_trac/ticket/14602Fri, 17 May 2013 05:51:46 +0200https://ask.sagemath.org/question/10126/convert-expression-to-quadraticfield/?comment=17675#post-id-17675