Thank you all for your help!

I got the same error as William, but it pointed me into the right 
direction. I found a way how to get the unique bounding interval of the 
root in sympy and convert this interval to a sage interval. It is not the 
prettiest solution but the following works for me:

from sage.all import *
from sympy import sympify
from sage.symbolic.expression_conversions import polynomial

expr = sympify("CRootOf(x**2 + 1, 0)")
complex_interval = expr._get_interval()
real_from = Rational(str(complex_interval.ax))._sage_()
real_to = Rational(str(complex_interval.bx))._sage_()
im_from = Rational(str(complex_interval.ay))._sage_()
im_to = Rational(str(complex_interval.by))._sage_()
sage_interval = CIF(RIF(real_from, real_to), RIF(im_from, im_to))
min_poly = polynomial(expr.expr._sage_(), base_ring=QQ)
algebraic_number = QQbar.polynomial_root(min_poly, sage_interval)


William Stein schrieb am Samstag, 1. April 2023 um 21:21:05 UTC+2:

> Thanks Vincent! What version of Sage are you using? With sage-9.8 I get:
>
> ---
>
> from sympy import sympify
> exp = sympify("CRootOf(x**5 + x + 1, 0)")
> sage_poly = QQ['x'](list(map(QQ, reversed(exp.poly.rep.rep))))
> sage_approx = RIF(exp.n()) + RIF(-1e-30, 1e-30)
> QQbar.polynomial_root(sage_poly, sage_approx)
>
> Outputs:
>
> ValueError: Refining interval that does not bound unique root!
>
> ----
>
>
>
> On Sat, Apr 1, 2023 at 12:02 PM vdelecroix <20100.d...@gmail.com> wrote:
> >
> > Dear Marcel,
> >
> > It is a pity that this does not work more directly. Here is a work 
> around to achieve what you want to do
> >
> > sage: exp = sympify("CRootOf(x**5 + x + 1, 0)")
> > sage: sage_poly = QQ['x'](list(map(QQ, reversed(exp.poly.rep.rep))))
> > sage: sage_approx = RIF(exp.n()) + RIF(-1e-30, 1e-30)
> > sage: QQbar.polynomial_root(sage_poly, sage_approx)
> > -0.7548776662466928?
> >
> > This will be fixed in https://github.com/sagemath/sage/pull/35414 and 
> hopefully included in the next sage version.
> >
> > Vincent
> > Le vendredi 31 mars 2023 à 15:00:34 UTC+2, Marcel Moosbrugger a écrit :
> >>
> >> I am trying to convert "complex_root_of" expressions to algebraic 
> numbers (elements in the field QQbar). However, I get a "ValueError" with 
> the message "to many values to unpack".
> >>
> >> A minimal example would be:
> >>
> >> from sage.all import *
> >> from sympy import sympify
> >>
> >> exp1 = sympify("CRootOf(x**2 + 1, 0)")._sage_()
> >> exp2 = sympify("CRootOf(x**2 + 1, 1)")._sage_()
> >> print(exp1)
> >> print(exp2)
> >> exp1 = QQbar(exp1)
> >> exp2 = QQbar(exp2)
> >> print(exp1)
> >> print(exp2)
> >>
> >> The context I am working in is: I have a tool that is currently written 
> in python and sympy. I am trying to use the output of that tool to do some 
> further computation using sage, and the output of the first tool may 
> contain CRootOf expressions.
> >>
> >>
> > --
> > You received this message because you are subscribed to the Google 
> Groups "sage-support" group.
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to sage-support...@googlegroups.com.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sage-support/c4602f36-3cec-44e2-81a5-67dc38a50230n%40googlegroups.com
> .
>
>
>
> -- 
> William (http://wstein.org)
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-support/88c0a8c9-427e-4341-9d35-6eb097d09892n%40googlegroups.com.

Reply via email to