负实数的立方根 [英] Cube root of negative real numbers
问题描述
我正在尝试绘制一个非常复杂的函数,即 log(x/(x-2))**Rational(1,3)
.我只使用实数.如果我尝试绘制它,sympy
只会绘制它的 x>2 部分.
我发现实际上复数开始起作用,例如,root(-8,3).n()
给出:
1.0+1.73205080756888i
这是合理的,即使这不是我想要的(因为我只对真实结果感兴趣).
阅读
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?
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
这篇关于负实数的立方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!