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.Tue, 30 Jun 2020 23:42:57 +0200nearby algebraic number / convert polynomial type to expression typehttps://ask.sagemath.org/question/52263/nearby-algebraic-number-convert-polynomial-type-to-expression-type/Given a real number, I am trying to find a nearby algebraic number. This can be done in Mathematica with the RootApproximant function.
I couldn't find a similar function in Sage, so I am using algdep() to find an irreducible polynomial that is approximately satisfied by that number. Then I am using roots() to find the roots of that polynomial, and I can identify which root is closest to my number.
The problem is that the output of algdep() has a type that seems to be incompatible with roots().
When I execute this code, p.roots() returns an empty list:
p=algdep(sqrt(5), 10);
print(p.roots())
When I execute this code, a list of the roots is returned:
q = x^2 - 5
print(q.roots())
The output of algdep() has type sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint
In the second block of code, q has type sage.symbolic.expression.Expression
Is there a way to convert the first type into the Expression type?
Mon, 29 Jun 2020 23:17:05 +0200https://ask.sagemath.org/question/52263/nearby-algebraic-number-convert-polynomial-type-to-expression-type/Answer by tmonteil for <p>Given a real number, I am trying to find a nearby algebraic number. This can be done in Mathematica with the RootApproximant function.</p>
<p>I couldn't find a similar function in Sage, so I am using algdep() to find an irreducible polynomial that is approximately satisfied by that number. Then I am using roots() to find the roots of that polynomial, and I can identify which root is closest to my number.</p>
<p>The problem is that the output of algdep() has a type that seems to be incompatible with roots().</p>
<p>When I execute this code, p.roots() returns an empty list:</p>
<pre><code>p=algdep(sqrt(5), 10);
print(p.roots())
</code></pre>
<p>When I execute this code, a list of the roots is returned:</p>
<pre><code>q = x^2 - 5
print(q.roots())
</code></pre>
<p>The output of algdep() has type sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint</p>
<p>In the second block of code, q has type sage.symbolic.expression.Expression</p>
<p>Is there a way to convert the first type into the Expression type? </p>
https://ask.sagemath.org/question/52263/nearby-algebraic-number-convert-polynomial-type-to-expression-type/?answer=52275#post-id-52275Since there are no integer roots, `p.roots()` does not gies anything since it is a polynomial over the integers:
sage: p.parent()
Univariate Polynomial Ring in x over Integer Ring
You can however ask for its roots as algebraic numbers:
sage: p.roots(QQbar)
[(-2.236067977499790?, 1), (2.236067977499790?, 1)]
or as symbolic expressions:
sage: p.roots(SR)
[(-sqrt(5), 1), (sqrt(5), 1)]
or even as floating-point real numbers:
sage: p.roots(RDF)
[(-2.23606797749979, 1), (2.23606797749979, 1)]Tue, 30 Jun 2020 23:42:57 +0200https://ask.sagemath.org/question/52263/nearby-algebraic-number-convert-polynomial-type-to-expression-type/?answer=52275#post-id-52275