How to calculate sum of two polynomials?

后端 未结 3 2029
傲寒
傲寒 2020-12-22 09:42

For instance 3x^4 - 17x^2 - 3x + 5. Each term of the polynomial can be represented as a pair of integers (coefficient,exponent). The polynomial itself is then a list of su

3条回答
  •  太阳男子
    2020-12-22 10:23

    This python code worked for me,hope this works for u too...

    Addition func

    def addpoly(p1,p2):
    i=0
    su=0
    j=0
    c=[]
    if len(p1)==0:
        #if p1 empty
        return p2
    if len(p2)==0:
        #if p2 is empty
        return p1
    while ip2[j][1]:
            c.append((p1[i]))
            i=i+1
        elif p1[i][1]

    Multiply func

    def multipoly(p1,p2):
    p=[]
    s=0
    for i in p1:
        c=[]
        for j in p2:
            s=i[0]*j[0]
            e=i[1]+j[1]
            c.append((s,e))
        p=addpoly(c,p)
    return p 
    

提交回复
热议问题