Sympy涉及绘制分段函数的问题

时间:2017-09-13 02:00:19

标签: python sympy symbolic-math

我已经定义了一个SymPy分段函数来计算2017年的联邦所得税。我知道该函数正在运行,因为我尝试了各种输入,并将其与经过验证的税收计算器进行比较,结果相同。

但是,在尝试绘制SymPy函数时,我收到错误:

TypeError: '>=' not supported between instances of 'complex' and 'int'

我从未定义任何复杂的数字。

def getFedTax(alpha,p,GI):
# alpha is an array indicating the starting dollar amount of each tax bracket, not including 0
#p is an array of the Tax Percentage amount corresponding to the interval BEFORE each alpha, i.e. [0,alpha0) corresponds to p0, [alpha0, alpha1) corresponds to p1, etc.
#x is the Gross Income for computation

# create an array of the cumulative sums for each starting point in alpha
cumsums = [0,p[0]*(alpha[0]-0)]
cumnum = cumsums[1]
for i,num in enumerate(alpha[1:-1],start=1):
    cumsums.append(p[i]*(alpha[i]-alpha[i-1]) + cumnum)
    cumnum = cumnum + p[i]*(alpha[i]-alpha[i-1])

cumsums.append(p[-2]*(alpha[-1]-alpha[-2]) + cumnum)

#Create the argument list of tuples for the SymPy.Piecewise function
argtuples = []
for n,bracstart in enumerate(alpha):
    if n == 0:
        argtuples.append((cumsums[0] + p[0]*x, And(0<=x, x<alpha[0])))
    elif 0 < n and n < len(alpha)-1:
        argtuples.append((cumsums[n] + p[n]*(x - alpha[n-1]), And(alpha[n-1] <= x, x < alpha[n])))
    else:
        argtuples.append((cumsums[-1]+p[-1]*(x-alpha[-1]), x>alpha[-1]))

t = Piecewise(*argtuples)


return round(t.subs(x,GI),2), t

from sympy import Piecewise, And
from sympy.plotting.plot import plot
from sympy.abc import x

ti = getFedTax([9325.00,37950.00,91900.00,191650.00,416700.00,418400.00],
[0.10,0.15,0.25,0.28,.33,.35,.396],1000000)

plot(ti[1],(x,1.,450000.00))  

完整追溯:

    runfile('C:/Users/galileo/Downloads/trial.py', 
wdir='C:/Users/galileo/Downloads')

  File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/galileo/Downloads/trial.py", line 39, in <module>
    plot(ti[1],(x,1.,450000.00))

  File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 1295, in plot
    plots.show()

  File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 196, in show
    self._backend.show()

  File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 1029, in show
    self.process_series()

  File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 908, in process_series
    collection = self.LineCollection(s.get_segments())

  File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 514, in get_segments
    f_start = f(self.start)

  File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\experimental_lambdify.py", line 231, in __call__
    result = self.lambda_func(args)

  File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\experimental_lambdify.py", line 316, in __call__
    return self.lambda_func(*args, **kwargs)

  File "<string>", line 1, in <lambda>


TypeError: '>=' not supported between instances of 'complex' and 'int'

1 个答案:

答案 0 :(得分:1)

分段线性的绘图为bugged, and seems to have been fixed in january 2018

升级到SymPy 1.2(默认是Anaconda附带的1.1.1)对我来说很成功。

from sympy import symbols
from sympy.plotting import plot
x = symbols('x')

from sympy import Piecewise, log
f = 2*x+3
g = x+1
p = Piecewise((-1, x < -1), (g, x <= 1), (f, True))
plot(p)

enter image description here