Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FunctionalSeries:
- def __init__(self, name, term_implementation, default_sum=np.zeros_like,
- sum_implementation=None, sum_name=None, infty=None):
- '''
- name, string: label for the series in LaTeX
- term_implementation: a vectorized function calculating the array of terms
- default_sum, float: sum of initial terms (used if starting index is not 1)
- sum_estimation, float: approximate sum of the series (if known)
- sum_name, string: label for the sum of the series in LaTeX
- infty, int: a number large enough for a good estimation of the series sum
- '''
- self.name = name
- self.term_implementation = term_implementation
- self.default_sum = default_sum
- self.sum_implementation = sum_implementation
- self.sum_name = sum_name
- self.infty = infty
- def calculate_sum(self, begin, end, domain):
- '''
- Calculate the sum u_{begin}(x) + u_{begin + 1}(x) + ... + u_{end - 1}(x) for all x in domain
- '''
- # YOUR CODE HERE
- solution = self.sum_implementation(begin, end, domain)
- return solution
- def terms(self, ns, domain):
- '''
- Returns a numpy 2d-array [[a_n(x)]], n in ns, x in domain
- '''
- return self.term_implementation(ns, domain)
- def partial_sums(self, ns, domain):
- '''
- Returns a numpy 2d-array [[S_n(x)]], n in ns, x in domain
- where S_n is the partial sum of the series
- '''
- #YOUR CODE HERE
- pass
- def remainders(self, ns, domain):
- '''
- Returns a numpy array [[|r_n(x)|]], n in ns, x in domain
- where r_n(x) = S(x) - S_n(x), S is an estimation of the sum of the series
- '''
- #YOUR CODE HERE
- solution = np.absolute(self.terms(ns, domain) - self.limit_function(domain))
- return solution
- def suprema(self, ns, domain):
- '''
- ns : np.ndarray of indices
- domain : np.ndarray of points
- Returns a numpy array [max|r_n(x)|], n in ns
- '''
- # YOUR CODE HERE
- solution = np.max(self.remainders(ns,domain), axis=1)
- return solution
- def estimate_sum(self, domain, n_min=1):
- '''
- Estimate the sum of the series as a big enough partial sum:
- u_{n_min}(x) + u_{n_min + 1}(x) + ... + u_{N_big}(x) for x in domain
- '''
- # YOUR CODE HERE
- pass
- def plot_sum(self, domain, semilogy=False, y_lims=None):
- plt.figure(figsize=(14, 7))
- plot = plt.semilogy if semilogy else plt.plot
- if self.sum_implementation is not None:
- plot(domain, self.sum_implementation(domain), lw=3, c='r')
- else:
- plot(domain, self.estimate_sum(domain), lw=3, c='r')
- plt.grid(ls=':')
- plt.title('Sum of the series ' + self.name)
- plt.xlim(np.min(domain), np.max(domain))
- if y_lims is not None:
- if semilogy:
- plt.ylim(10 ** y_lims[0], 10 ** y_lims[1])
- else:
- plt.ylim(y_lims[0], y_lims[1])
- def plot(self, mode, ns, domain, semilogy=False, y_lims=None):
- plt.figure(figsize=(14, 7))
- #line_colors = np.random.choice(colors, size=len(ns), replace=False)
- # marker_edge_colors = np.random.choice(marker_colors, size=len(functions), replace=False)
- plot = plt.semilogy if semilogy else plt.plot
- if mode == 'terms':
- ys = self.terms(ns, domain).T
- elif mode == 'partial sums':
- ys = self.partial_sums(ns, domain).T
- elif mode == 'remainders':
- ys = self.remainders(ns, domain).T
- plot(domain, ys, lw=3)
- label_list = [r'$n={}$'.format(i) for i in ns]
- if mode == 'partial sums':
- if self.sum_implementation is not None:
- plot(domain, self.sum_implementation(domain), lw=3, ls='--', c='r')
- label_list.append(r"$S(x)$" if self.sum_name is None else self.sum_name)
- else:
- plot(domain, self.estimate_sum(domain), lw=3, ls='--', c='r')
- label_list.append(r"$S(x)$" if self.sum_name is None else self.sum_name)
- plt.grid(ls=':')
- title = self.name
- if self.sum_name is not None:
- title += '=' + self.sum_name
- plt.title(title + ', ' + mode)
- plt.xlim(np.min(domain), np.max(domain))
- if y_lims is not None:
- if semilogy:
- plt.ylim(10 ** y_lims[0], 10 ** y_lims[1])
- else:
- plt.ylim(y_lims[0], y_lims[1])
- plt.legend(label_list);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement