Cube root of negative real numbers

雨燕双飞 提交于 2019-12-10 19:47:47

问题


I'm trying to plot a quite complex function, i.e. log(x/(x-2))**Rational(1,3). I'm working only with real numbers. If I try to plot it, sympy only plots the x>2 part of it.

I found that actually complex numbers come into play and, for example,root(-8,3).n() gives:

1.0+1.73205080756888i

Which is reasonable, even though it's not what I was looking for (because I'm only interested in the real result).

Reading sympy › principle root I found that real_root(-8,3) gives -2 as expected. But I still cannot plot the x<0 part of that function; in fact it seems that real_root only works for integer roots, and real_root(-9,3).n() still gives an imaginary result, instead of -(real_root(9, 3)) as I would expect.

I thought a real result existed for (-9)^(1/3) and I don't understand why real_root gives an imaginary result instead.

Is there a simple way to get a schoolbook result for the cube root of real negative numbers, like (-x)^(1/3) = - (x)^(1/3)?

Edit:
Following @Leon 's suggestion: I updated sympy and could actually calculate the real cube root of -9. But still I cannot plot the function I mentioned at the beginning of the topic.

from sympy import *
var('x')
f=real_root((log(x/(x-2))), 3)
plot(f)

gives an error like NameError: name 'Ne' is not defined. I noticed that trying to print f results in

Piecewise((1, Ne(arg(x/(x - 2)), 0)), ((-1)**(2/3), log(x/(x - 2)) < 0), (1, True))*log(x/(x - 2))**(1/3)

Does that Ne have something to do with my error?


回答1:


It seems SymPy's plot has a bug, so for now, you'll have to use lambdify and matplotlib to plot it manually:

import numpy as np
import matplotlib.pyplot as plt

f = lambdify(x, (real_root((log(x/(x-2))), 3)), 'numpy')
vals = np.linspace(2, 10, 1000)
plt.plot(vals, f(vals))

This gives some warnings because the 2 value at the end point is a singularity, and also warns that if you have a complex number that the imaginary part is ignored.

Here is the plot



来源:https://stackoverflow.com/questions/38682936/cube-root-of-negative-real-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!