Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. class FunctionalSeries:
  2. def __init__(self, name, term_implementation, default_sum=np.zeros_like,
  3. sum_implementation=None, sum_name=None, infty=None):
  4. '''
  5. name, string: label for the series in LaTeX
  6. term_implementation: a vectorized function calculating the array of terms
  7. default_sum, float: sum of initial terms (used if starting index is not 1)
  8. sum_estimation, float: approximate sum of the series (if known)
  9. sum_name, string: label for the sum of the series in LaTeX
  10. infty, int: a number large enough for a good estimation of the series sum
  11. '''
  12. self.name = name
  13. self.term_implementation = term_implementation
  14. self.default_sum = default_sum
  15. self.sum_implementation = sum_implementation
  16. self.sum_name = sum_name
  17. self.infty = infty
  18.  
  19. def calculate_sum(self, begin, end, domain):
  20. '''
  21. Calculate the sum u_{begin}(x) + u_{begin + 1}(x) + ... + u_{end - 1}(x) for all x in domain
  22. '''
  23. # YOUR CODE HERE
  24. solution = self.sum_implementation(begin, end, domain)
  25. return solution
  26.  
  27. def terms(self, ns, domain):
  28. '''
  29. Returns a numpy 2d-array [[a_n(x)]], n in ns, x in domain
  30. '''
  31. return self.term_implementation(ns, domain)
  32.  
  33. def partial_sums(self, ns, domain):
  34. '''
  35. Returns a numpy 2d-array [[S_n(x)]], n in ns, x in domain
  36. where S_n is the partial sum of the series
  37. '''
  38. #YOUR CODE HERE
  39. pass
  40.  
  41. def remainders(self, ns, domain):
  42. '''
  43. Returns a numpy array [[|r_n(x)|]], n in ns, x in domain
  44. where r_n(x) = S(x) - S_n(x), S is an estimation of the sum of the series
  45. '''
  46. #YOUR CODE HERE
  47. solution = np.absolute(self.terms(ns, domain) - self.limit_function(domain))
  48. return solution
  49.  
  50. def suprema(self, ns, domain):
  51. '''
  52. ns : np.ndarray of indices
  53. domain : np.ndarray of points
  54. Returns a numpy array [max|r_n(x)|], n in ns
  55. '''
  56. # YOUR CODE HERE
  57. solution = np.max(self.remainders(ns,domain), axis=1)
  58. return solution
  59.  
  60. def estimate_sum(self, domain, n_min=1):
  61. '''
  62. Estimate the sum of the series as a big enough partial sum:
  63. u_{n_min}(x) + u_{n_min + 1}(x) + ... + u_{N_big}(x) for x in domain
  64. '''
  65. # YOUR CODE HERE
  66. pass
  67.  
  68. def plot_sum(self, domain, semilogy=False, y_lims=None):
  69. plt.figure(figsize=(14, 7))
  70. plot = plt.semilogy if semilogy else plt.plot
  71. if self.sum_implementation is not None:
  72. plot(domain, self.sum_implementation(domain), lw=3, c='r')
  73. else:
  74. plot(domain, self.estimate_sum(domain), lw=3, c='r')
  75. plt.grid(ls=':')
  76. plt.title('Sum of the series ' + self.name)
  77. plt.xlim(np.min(domain), np.max(domain))
  78. if y_lims is not None:
  79. if semilogy:
  80. plt.ylim(10 ** y_lims[0], 10 ** y_lims[1])
  81. else:
  82. plt.ylim(y_lims[0], y_lims[1])
  83.  
  84. def plot(self, mode, ns, domain, semilogy=False, y_lims=None):
  85. plt.figure(figsize=(14, 7))
  86.  
  87. #line_colors = np.random.choice(colors, size=len(ns), replace=False)
  88. # marker_edge_colors = np.random.choice(marker_colors, size=len(functions), replace=False)
  89. plot = plt.semilogy if semilogy else plt.plot
  90.  
  91. if mode == 'terms':
  92. ys = self.terms(ns, domain).T
  93. elif mode == 'partial sums':
  94. ys = self.partial_sums(ns, domain).T
  95. elif mode == 'remainders':
  96. ys = self.remainders(ns, domain).T
  97. plot(domain, ys, lw=3)
  98.  
  99. label_list = [r'$n={}$'.format(i) for i in ns]
  100. if mode == 'partial sums':
  101. if self.sum_implementation is not None:
  102. plot(domain, self.sum_implementation(domain), lw=3, ls='--', c='r')
  103. label_list.append(r"$S(x)$" if self.sum_name is None else self.sum_name)
  104. else:
  105. plot(domain, self.estimate_sum(domain), lw=3, ls='--', c='r')
  106. label_list.append(r"$S(x)$" if self.sum_name is None else self.sum_name)
  107.  
  108. plt.grid(ls=':')
  109. title = self.name
  110. if self.sum_name is not None:
  111. title += '=' + self.sum_name
  112. plt.title(title + ', ' + mode)
  113. plt.xlim(np.min(domain), np.max(domain))
  114. if y_lims is not None:
  115. if semilogy:
  116. plt.ylim(10 ** y_lims[0], 10 ** y_lims[1])
  117. else:
  118. plt.ylim(y_lims[0], y_lims[1])
  119. plt.legend(label_list);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement