我有一个python程序,该程序找到某个函数的根,然后更改该根的符号。示例可以在下面看到:
from scipy import optimize
from numpy import sqrt
def Myfun(inputargument):
# function returns some floats
root = optimize.brentq(Myfun, -2, 0) #here I find some negative root.
y= sqrt(-root) #here I change the sign of this root
我可以运行此代码而没有任何错误,但是PyCharm在最后一行中突出显示了-x,当我检查问题出在哪里时,我得到如下消息:
类'(float,any)'没有定义'_ _ neg_ _',因此无法在其实例上使用'-'运算符
怎么了?
警告是因为PyCharm不确定optimize.brentq
将返回什么。在某些情况下,它返回浮点数,但在某些情况下,它返回一个元组。
基本上,它返回]的输出>
def results_c(full_output, r):
if full_output:
x, funcalls, iterations, flag = r
results = RootResults(root=x,
iterations=iterations,
function_calls=funcalls,
flag=flag)
return x, results
else:
return r