Real-valued root

80 views
Skip to first unread message

Paul Royik

unread,
Dec 11, 2024, 2:15:06 PM12/11/24
to sympy
I try to use the bridge between sympy and numpy, namely, the lambdify function.

It looks ok, but there is an issue when I try to calculate the values of x^(2/5) on the interval [-5,5]. 

Of course, for negative x, the value is nan, because, I guess, the root is not real-valued.
How to make it real-valued?

Thank you.

peter.st...@gmail.com

unread,
Dec 11, 2024, 4:36:20 PM12/11/24
to sy...@googlegroups.com

If I put it as (x^2)^(1/5) it works fine.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sympy/02b3834f-d2ba-4767-a28e-4b6ae1355906n%40googlegroups.com.

Paul Royik

unread,
Dec 11, 2024, 4:37:29 PM12/11/24
to sympy
What is x^(3/5)?

peter.st...@gmail.com

unread,
Dec 11, 2024, 4:45:15 PM12/11/24
to sy...@googlegroups.com

I do not know.

What is x^(2/pi)?

Paul Royik

unread,
Dec 11, 2024, 4:48:00 PM12/11/24
to sympy
I mean what if x^(3/5) ?

peter.st...@gmail.com

unread,
Dec 11, 2024, 4:57:36 PM12/11/24
to sy...@googlegroups.com

Oscar Benjamin

unread,
Dec 11, 2024, 5:21:44 PM12/11/24
to sy...@googlegroups.com
You can use e.g.

In [22]: lambdify(x, (sign(x)*abs(x)**(1/5))**2)([-2, -1, 0, 1, 2])
Out[22]: array([1.31950791, 1. , 0. , 1. , 1.31950791])

The real_root function gives a more complicated form:

In [23]: real_root(x, 5)**2
Out[23]:
⎧ 2/5 2
⎪│x│ ⋅sign (x) for im(x) = 0

⎪ 2/5
⎩ x otherwise

That one works as well but prints out warnings when lambdified.

--
Oscar

Paul Royik

unread,
Dec 12, 2024, 1:31:07 AM12/12/24
to sympy
Thank you very much!
Reply all
Reply to author
Forward
0 new messages