Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. class Polynomial:
  2.     def __init__(self, coefficients):
  3.         self.coeffs = []
  4.         for item in coefficients:
  5.             item = float(item)
  6.             self.coeffs.insert(0, item)
  7.     # list of coefficients is in ascending power
  8.         self.length = len(self.coeffs)
  9.     def __str__(self):
  10.         string = ''
  11.         coeffs = self.coeffs
  12.         coeffs.reverse()
  13.         i = self.length - 1
  14.         for coeff in coeffs:
  15.             if i != (self.length - 1 or coeff > 0):
  16.                 string += ' + '
  17.             if i == 0:
  18.                 string += (str(coeff))
  19.             elif i == 1:
  20.                 string += (str(coeff) + ' x ')
  21.             elif coeff == 0:
  22.                 pass
  23.             else:
  24.                 string += (str(coeff) + ' x**' + str(i))
  25.             i -= 1
  26.         return string
  27.            
  28.     def coeff(self, i):
  29.         return self.coeffs[i]
  30.     def add(self, other):
  31.         if self.length > other.length:
  32.             shorter = other.coeffs
  33.             longer = self.coeffs
  34.         else:
  35.             shorter = self.coeffs
  36.             longer = other.coeffs
  37.         i = 0
  38.         sumPoly = []
  39.         while i < len(shorter):
  40.             sumPoly.append(shorter[i] + longer[i])
  41.             i += 1
  42.         while i < len(longer):
  43.             sumPoly.append(longer[i])
  44.             i += 1
  45.         sumPoly.reverse()
  46.         poly = Polynomial(sumPoly)
  47.         return poly
  48.     def mul(self, other):
  49.         i = 0
  50.         product = Polynomial([])
  51.         for item1 in self.coeffs:
  52.  
  53.             new_list = []
  54.             n = i
  55.         # if item is coef. to x^power, move values up list
  56.             while i > 0:
  57.                 new_list.append(0)
  58.                 i -= 1
  59.             for item2 in other.coeffs:
  60.                 new_list.append(item1*item2)
  61.             i = n + 1
  62.             new_list.reverse()
  63.             # for each iteration, add produced polynomial to product
  64.             newPoly = Polynomial(new_list)
  65.             product = product.add(newPoly)
  66.         return product
  67.     def val(self, v):
  68.         value = 0
  69.         for exp in range(0, self.length):
  70.             value += self.coeffs[exp]*(v**exp)
  71.         return value
  72.     def roots(self):
  73.         def root(x, y):
  74.                 if det < 0:                  
  75.                     roots = [(complex(x, y)), (complex(x, -y))]
  76.                 elif det == 0:
  77.                     roots = [x]
  78.                 else:
  79.                     roots = [(x + y), (x - y)]
  80.                 return roots
  81.         if self.length == 2:
  82.             c = self.coeffs[0]
  83.             b = self.coeffs[1]
  84.             roots = [-c/b]
  85.             return roots
  86.         elif self.length == 3:
  87.             a = self.coeffs[2]
  88.             b = self.coeffs[1]
  89.             c = self.coeffs[0]
  90.             det = b**2 - 4*a*c
  91.             y = (abs(det)**0.5)/(2*a)
  92.             x = -b/(2*a)
  93.             return root(x, y)
  94.         else:
  95.             return 'Error! First or second order polynomials only.'      
  96.     def __add__(self, other):
  97.         return self.add(other)
  98.     def __mul__(self, other):
  99.         return self.mul(other)
  100.     def __call__(self, x):
  101.         return self.val(x)
  102.     def __repr__(self):
  103.         return str(self)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement