Can you explain the clear code for polynomial expression in python and add two polynomial expression in python
def DispPoly(C,n):
C = C[::-1]
s = ""
k=n-1
for r in range(0,len(C)-2):
if(r==0):
if(C[r]>0): s = s + str(C[r])+"x^"+str(k)
if(C[r]<0): s = s + str(C[r])+"x^"+str(k)
if(r>0):
if(C[r]>0): s = s + "+" + str(C[r])+"x^"+str(k)
if(C[r]<0): s = s + str(C[r])+"x^"+str(k)
k=k-1
if(C[n-2]>0): s = s + "+" + str(C[n-2]) + "x"
if(C[n-2]<0): s = s + str(C[n-2]) + "x"
if(C[n-1]>0): s = s + "+" + str(C[n-1])
if(C[n-1]<0): s = s + str(C[n-1])
print(s)
N = int(input("Enter the Order of Polynomial: "))
N = N+1
print("Enter coefficients of the two polynomiuals separated by SPACE: ")
Coeff=[]
AddP=[]
for r in range(0,N):
s = "Enter coeff. of x^" + str(r) + str(": ")
t = (str(input(s))).split(" ")
temp = []
temp.append(int(t[0]))
temp.append(int(t[1]))
Coeff.append(temp)
AddP.append(sum(temp))
Coeff = np.array(Coeff)
Coeff=np.transpose(Coeff)
print("\nPolynomial-1:")
DispPoly(Coeff[0],N)
print("\nPolynomial-2:")
DispPoly(Coeff[1],N)
print("\nAfter Adding two polynomials: ")
DispPoly(AddP,N)
Comments
Leave a comment