Python多项式积分函数输出错误

时间:2014-06-27 03:32:41

标签: math python-3.x

def poly_integrate(terms,left_limit,right_limit):
    split_terms = terms.split('+')
    integral_fxdx = 'Integral:'
    integral = 0
    for term in split_terms:
        if (left_limit and right_limit) is None:
            #power term
            if 'x^' in term:
                split_term = term.split('x^')
                if float(split_term[1]) != -1:
                    int_coef = float(split_term[0])/(float(split_term[1])+1)
                    int_term = str(int_coef) + 'x^' + str(float(split_term[1]) + 1)
                    integral_fxdx = integral_fxdx + int_term
            if float(split_term[1]) == -1:
                int_term = split_term[0] + 'ln(x)'
                integral_fxdx = integral_fxdx + int_term
            #linear term
            if 'x' in term and ('^' not in term):
                split_term = term.split('x')
                int_term = str(float(split_term[0])/2) + 'x^2'
                integral_fxdx = integral_fxdx + int_term
            #constant
            if 'x' not in term:
                int_term = term + 'x'
                integral_fxdx = integral_fxdx + int_term
            #add plus or constant
            if term != terms[-0]:
                integral_fxdx = integral_fxdx + '+ '
            if term == terms[-0]:
                integral_fxdx = integral_fxdx + '+ C'
        return integral_fxdx

输出:

  
    
      

积分:0.25x ^ 4.0 + 1.0x ^ 3.0 + 1.0x ^ 2 + 1x + C3ln(x)+

    
  

所以基本上一切都有效,除了我整合一个x ^ -1术语。有什么不对,所以if条款让我的函数认为常数项是最后一项,因此返回" C"早期并将ln(x)放在后面?为什么ln(x)之后的加号呢?已经玩了几个小时了,无法弄明白......

当我不包含x ^ -1术语时,一切正常。如果你想知道为什么我包含了一个不定积分的左右极限,我计划在函数内做无限期或明确积分的功能。

0 个答案:

没有答案