Guest User

Untitled

a guest
Jun 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. class Rational(object):
  2. def __init__(self, f_list, g_list):
  3. self.f = Polynomial(f_list)
  4. self.g = Polynomial(g_list)
  5.  
  6. def __str__(self):
  7. f_str = str(self.f)
  8. g_str = str(self.g)
  9. # length of line between numerator and denominator
  10. # depends on length of strings
  11. line_length = max(len(f_str), len(g_str))
  12. # I join a list comprehension to make the line
  13. line = "".join(["-" for i in range(line_length)])
  14. # and join everything with newlines
  15. result = "\n".join([f_str, line, g_str])
  16. return result
  17.  
  18. def add(self, other_rat):
  19. # f1 f2
  20. # -- + --
  21. # g1 g2
  22. f1 = self.f
  23. f2 = other_rat.f
  24. g1 = self.g
  25. g2 = other_rat.g
  26. # f1 * g2 + f2 * g1
  27. # ------------------
  28. # g1 * g2
  29. first_term = f1.multiply(g2)
  30. second_term = f2.multiply(g1)
  31. numerator = first_term.add(second_term)
  32. denominator = g1.multiply(g2)
  33. return Rational(numerator.coefficients, denominator.coefficients)
  34.  
  35. def multiply(self, other_rat):
  36. # f1 f2
  37. # -- * --
  38. # g1 g2
  39. f1 = self.f
  40. f2 = other_rat.f
  41. g1 = self.g
  42. g2 = other_rat.g
  43. # f1 * f2
  44. # -------
  45. # g1 * g2
  46. numerator = f1.multiply(f2)
  47. denominator = g1.multiply(g2)
  48. return Rational(numerator.coefficients, denominator.coefficients)
  49.  
  50. def derivative(self):
  51. f = self.f
  52. g = self.g
  53. # Formula:
  54. # g*f' - f*g'
  55. # ----------
  56. # g^2
  57. # first term is g*f'
  58. first_term = g.multiply(f.derivative())
  59. # second term is f*g'
  60. second_term = f.multiply(g.derivative())
  61. # we need to negate the second term
  62. neg_second_term_list = [-c for c in second_term.coefficients]
  63. neg_second_term = Polynomial(neg_second_term_list)
  64. # numerator is g*f' - f*g'
  65. numerator = first_term.add(neg_second_term)
  66. # denominator is g^2
  67. denominator = g.multiply(g)
  68. return Rational(numerator.coefficients, denominator.coefficients)
  69.  
  70.  
  71. class Polynomial(object):
  72. # Constructor just takes a list of coefficients
  73. # Note that the coefficient lists go from
  74. # zero-th power to highest power
  75. # Opposite order from printing!
  76. def __init__(self, coefficients):
  77. assert type(coefficients) is list
  78. self.coefficients = coefficients
  79. self.degree = len(coefficients) - 1
  80.  
  81. # Prints polynomial from greatest power first to lowest power last
  82. # (opposite from stored list)
  83. def __str__(self):
  84. if self.coefficients == []:
  85. return "0"
  86. poly_string = []
  87. coeffs = reversed(self.coefficients)
  88. degree = self.degree
  89. for i, coeff in enumerate(coeffs):
  90. power = degree - i
  91. if coeff == 0:
  92. continue
  93. if power == 0:
  94. poly_string.append(str(coeff))
  95. else:
  96. poly_string.append(str(coeff) + "x^" + str(power))
  97. poly_string = " + ".join(poly_string)
  98. return poly_string
  99.  
  100. # This function sums two polynomials
  101. # by adding the coefficient lists element-wise
  102. def add(self, other_poly):
  103. p3 = []
  104. degree_diff = other_poly.degree - self.degree
  105. p1 = []
  106. p2 = []
  107. # One of the lists (p1 or p2) will need to be
  108. # padded with zeros
  109. # to get them to the same length
  110. # (I feel like there must be a better way though)
  111. if degree_diff >= 0:
  112. p1 = self.coefficients + [0 for i in range(degree_diff)]
  113. p2 = other_poly.coefficients
  114. else:
  115. p1 = other_poly.coefficients + [0 for i in range(-degree_diff)]
  116. p2 = self.coefficients
  117. # After padding, sum together the coefficients
  118. # element-wise
  119. for i in range(len(p1)):
  120. p3.append(p1[i] + p2[i])
  121. return Polynomial(p3)
  122.  
  123. # This function multiplies two polynomials
  124. def multiply(self, other_poly):
  125. p1 = self.coefficients
  126. p2 = other_poly.coefficients
  127. n = len(p1)
  128. m = len(p2)
  129. matrix = [[0 for j in range(m)] for i in range(n)]
  130. for j in range(m): # column index
  131. for i in range(n): # row index
  132. # populate matrix
  133. matrix[i][j] = p1[i] * p2[j]
  134. p3 = [0 for k in range(m + n - 1)]
  135. for k in range(m + n - 1):
  136. j = min(k, m - 1)
  137. i = max(0, k - (m - 1))
  138. while i < n and j >= 0:
  139. # sum over diagonals
  140. p3[k] += matrix[i][j]
  141. i += 1
  142. j -= 1
  143. return Polynomial(p3)
  144.  
  145. # This function returns the derivative of the Polynomial
  146. # Recall that each component a*x^n is mapped to (a*n)*x^(n-1)
  147. def derivative(self):
  148. coeffs = self.coefficients
  149. result = [i * coeffs[i] for i in range(self.degree + 1)]
  150. result = result[1:]
  151. return Polynomial(result)
  152.  
  153.  
  154. def main():
  155. # Here I make two polynomials, and test the
  156. # results of addition, multiplication, and
  157. # symbolic differentiation
  158. poly1 = Polynomial([1, 2, 3, 4, 5])
  159. poly2 = Polynomial([2, 4, 6])
  160. # Old testing for polynomials
  161. '''
  162. print("Poly 1: ")
  163. print(poly1)
  164. print()
  165. print("Poly 2: ")
  166. print(poly2)
  167. print()
  168. poly3 = poly1.add(poly2)
  169. print("Poly 1 + Poly 2: ")
  170. print(poly3)
  171. print()
  172. poly4 = poly2.multiply(poly1)
  173. print("Poly 1 * Poly 2: ")
  174. print(poly4)
  175. print()
  176. poly5 = poly1.derivative()
  177. poly6 = poly2.derivative()
  178. print("Derivative of Poly 1: ")
  179. print(poly5)
  180. print()
  181. print("Derivative of Poly 2: ")
  182. print(poly6)
  183. print()
  184. '''
  185. # new testing for rational functions
  186. rat1 = Rational(poly1.coefficients, poly2.coefficients)
  187. print("Rational function 1: ")
  188. print(rat1)
  189. print()
  190. rat2 = Rational(poly2.coefficients, poly1.coefficients)
  191. print("Rational function 1: ")
  192. print(rat1)
  193. print()
  194. rat3 = rat1.add(rat2)
  195. print("Rational 1 + Rational 2: ")
  196. print(rat3)
  197. print()
  198. rat4 = rat1.multiply(rat2)
  199. print("Rational 1 * Rational 2: ")
  200. print(rat4)
  201. print()
  202. rat1_diff = rat1.derivative()
  203. print("Derivative of rational function: ")
  204. print(rat1_diff)
  205. print()
  206.  
  207. '''
  208. Output:
  209.  
  210. Rational function 1:
  211. 5x^4 + 4x^3 + 3x^2 + 2x^1 + 1
  212. -----------------------------
  213. 6x^2 + 4x^1 + 2
  214.  
  215. Rational function 1:
  216. 5x^4 + 4x^3 + 3x^2 + 2x^1 + 1
  217. -----------------------------
  218. 6x^2 + 4x^1 + 2
  219.  
  220. Rational 1 + Rational 2:
  221. 25x^8 + 40x^7 + 46x^6 + 44x^5 + 71x^4 + 68x^3 + 50x^2 + 20x^1 + 5
  222. -----------------------------------------------------------------
  223. 30x^6 + 44x^5 + 44x^4 + 32x^3 + 20x^2 + 8x^1 + 2
  224.  
  225. Rational 1 * Rational 2:
  226. 30x^6 + 44x^5 + 44x^4 + 32x^3 + 20x^2 + 8x^1 + 2
  227. ------------------------------------------------
  228. 30x^6 + 44x^5 + 44x^4 + 32x^3 + 20x^2 + 8x^1 + 2
  229.  
  230. Derivative of rational function:
  231. 60x^5 + 84x^4 + 72x^3 + 24x^2
  232. ---------------------------------
  233. 36x^4 + 48x^3 + 40x^2 + 16x^1 + 4
  234. '''
  235.  
  236.  
  237. if __name__ == "__main__":
  238. main()
Add Comment
Please, Sign In to add comment