ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 13 Aug 2020 17:03:40 -0500Algebraic to symbolic expressionhttps://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/ Hello! When I write AA(sqrt(3)+sqrt(2)) i get 3.146264369941973?, and I use to recover the original symbolic expression sqrt(2)+sqrt(3) simply by asking the minimal polynomial of 3.146264369941973?, which is of degree 4, and looking for the appropiate root. But I want to know if there is a reasonable way to get the symbolic expression from an algebraic expression which I know it comes from a lot of operations, knowing that the function "minpoly()" in that case does not give an answer for hours. I hope you can help me!!!Tue, 11 Aug 2020 16:17:24 -0500https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/Answer by tmonteil for <p>Hello! When I write AA(sqrt(3)+sqrt(2)) i get 3.146264369941973?, and I use to recover the original symbolic expression sqrt(2)+sqrt(3) simply by asking the minimal polynomial of 3.146264369941973?, which is of degree 4, and looking for the appropiate root. But I want to know if there is a reasonable way to get the symbolic expression from an algebraic expression which I know it comes from a lot of operations, knowing that the function "minpoly()" in that case does not give an answer for hours. I hope you can help me!!!</p>
https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?answer=52987#post-id-52987You can use the `radical_expression` method:
sage: a = AA(sqrt(3)+sqrt(2))
sage: a
3.146264369941973?
sage: a.radical_expression()
sqrt(2*sqrt(6) + 5)
Note that this is not a "complete" feature, as there might be some algebraic numbers admitting a radical decomposition that this feature will not find.
Thu, 13 Aug 2020 13:16:25 -0500https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?answer=52987#post-id-52987Comment by rburing for <p>You can use the <code>radical_expression</code> method:</p>
<pre><code>sage: a = AA(sqrt(3)+sqrt(2))
sage: a
3.146264369941973?
sage: a.radical_expression()
sqrt(2*sqrt(6) + 5)
</code></pre>
<p>Note that this is not a "complete" feature, as there might be some algebraic numbers admitting a radical decomposition that this feature will not find.</p>
https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?comment=52993#post-id-52993The first thing this method does is compute `a.minpoly()`...Thu, 13 Aug 2020 17:03:40 -0500https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?comment=52993#post-id-52993Answer by rburing for <p>Hello! When I write AA(sqrt(3)+sqrt(2)) i get 3.146264369941973?, and I use to recover the original symbolic expression sqrt(2)+sqrt(3) simply by asking the minimal polynomial of 3.146264369941973?, which is of degree 4, and looking for the appropiate root. But I want to know if there is a reasonable way to get the symbolic expression from an algebraic expression which I know it comes from a lot of operations, knowing that the function "minpoly()" in that case does not give an answer for hours. I hope you can help me!!!</p>
https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?answer=52952#post-id-52952I'm not sure how `minpoly()` works but I'm pretty sure the following [numerical approach (using PSLQ)](http://mpmath.org/doc/1.1.0/identification.html#algebraic-identification) is different:
z = AA(sqrt(3)+sqrt(2))
max_degree = 4
from mpmath import mp, findpoly
mp.dps = 15 # decimal places
f = ZZ['x'](findpoly(z.numerical_approx(digits=mp.dps), max_degree))
print(f)
print(f(z.numerical_approx(digits=mp.dps)))
print(f(z).is_zero())
print(f.roots(SR))
Output:
x^4 - 10*x^2 + 1
1.31006316905768e-14
True
[(-sqrt(2*sqrt(6) + 5), 1), (sqrt(2*sqrt(6) + 5), 1), (-sqrt(-2*sqrt(6) + 5), 1), (sqrt(-2*sqrt(6) + 5), 1)]
It can be used to find minimal polynomials *with some degree of uncertainty*, but likely at a higher speed. I hope it can be useful.
Edit: The uncertainty of `z` being a root can be eliminated by checking `f(z).is_zero()`, and the uncertainty of minimality could be eliminated e.g. by checking `f.is_irreducible()`.Tue, 11 Aug 2020 16:45:40 -0500https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?answer=52952#post-id-52952Comment by rburing for <p>I'm not sure how <code>minpoly()</code> works but I'm pretty sure the following <a href="http://mpmath.org/doc/1.1.0/identification.html#algebraic-identification">numerical approach (using PSLQ)</a> is different:</p>
<pre><code>z = AA(sqrt(3)+sqrt(2))
max_degree = 4
from mpmath import mp, findpoly
mp.dps = 15 # decimal places
f = ZZ['x'](findpoly(z.numerical_approx(digits=mp.dps), max_degree))
print(f)
print(f(z.numerical_approx(digits=mp.dps)))
print(f(z).is_zero())
print(f.roots(SR))
</code></pre>
<p>Output:</p>
<pre><code>x^4 - 10*x^2 + 1
1.31006316905768e-14
True
[(-sqrt(2*sqrt(6) + 5), 1), (sqrt(2*sqrt(6) + 5), 1), (-sqrt(-2*sqrt(6) + 5), 1), (sqrt(-2*sqrt(6) + 5), 1)]
</code></pre>
<p>It can be used to find minimal polynomials <em>with some degree of uncertainty</em>, but likely at a higher speed. I hope it can be useful.</p>
<p>Edit: The uncertainty of <code>z</code> being a root can be eliminated by checking <code>f(z).is_zero()</code>, and the uncertainty of minimality could be eliminated e.g. by checking <code>f.is_irreducible()</code>.</p>
https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?comment=52970#post-id-52970You're welcome :) This approach is not exact but if for an algebraic number `z` you manage to find an `f` in this way, then you can check whether it is actually exact e.g. with `f(z).is_zero()`. (If this is the case, it is not guaranteed that `f` will be of *minimal* degree, but it is likely, and you could check it in another way, e.g. by `f.is_irreducible()`.)Wed, 12 Aug 2020 09:52:34 -0500https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?comment=52970#post-id-52970Comment by creyesm1992 for <p>I'm not sure how <code>minpoly()</code> works but I'm pretty sure the following <a href="http://mpmath.org/doc/1.1.0/identification.html#algebraic-identification">numerical approach (using PSLQ)</a> is different:</p>
<pre><code>z = AA(sqrt(3)+sqrt(2))
max_degree = 4
from mpmath import mp, findpoly
mp.dps = 15 # decimal places
f = ZZ['x'](findpoly(z.numerical_approx(digits=mp.dps), max_degree))
print(f)
print(f(z.numerical_approx(digits=mp.dps)))
print(f(z).is_zero())
print(f.roots(SR))
</code></pre>
<p>Output:</p>
<pre><code>x^4 - 10*x^2 + 1
1.31006316905768e-14
True
[(-sqrt(2*sqrt(6) + 5), 1), (sqrt(2*sqrt(6) + 5), 1), (-sqrt(-2*sqrt(6) + 5), 1), (sqrt(-2*sqrt(6) + 5), 1)]
</code></pre>
<p>It can be used to find minimal polynomials <em>with some degree of uncertainty</em>, but likely at a higher speed. I hope it can be useful.</p>
<p>Edit: The uncertainty of <code>z</code> being a root can be eliminated by checking <code>f(z).is_zero()</code>, and the uncertainty of minimality could be eliminated e.g. by checking <code>f.is_irreducible()</code>.</p>
https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?comment=52967#post-id-52967thanks rburing, i was looking for something exact, and "minpoly()" is exact (I verified checking if f(z))=0 with your polinomial and, as you said, it is approximately 0, and the exactitude is reached by the function obtained in minpoly()). Thanks for your help in this and my other question :-)Wed, 12 Aug 2020 09:18:25 -0500https://ask.sagemath.org/question/52951/algebraic-to-symbolic-expression/?comment=52967#post-id-52967