|
| 1 | +import sympy as sym |
| 2 | + |
| 3 | +class TaylorSeries: |
| 4 | + """Class for symbolic Taylor series.""" |
| 5 | + def __init__(self, f, num_terms=4): |
| 6 | + self.f = f |
| 7 | + self.N = num_terms |
| 8 | + # Introduce symbols for the derivatives |
| 9 | + self.df = [f] |
| 10 | + for i in range(1, self.N+1): |
| 11 | + self.df.append(sym.Symbol('D%d%s' % (i, f.name))) |
| 12 | + |
| 13 | + def __call__(self, h): |
| 14 | + """Return the truncated Taylor series at x+h.""" |
| 15 | + terms = self.f |
| 16 | + for i in range(1, self.N+1): |
| 17 | + terms += sym.Rational(1, sym.factorial(i))*self.df[i]*h**i |
| 18 | + return terms |
| 19 | + |
| 20 | + |
| 21 | +class DiffOp: |
| 22 | + """Class for discrete difference operators.""" |
| 23 | + def __init__(self, f, independent_variable='x', |
| 24 | + num_terms_Taylor_series=4): |
| 25 | + self.Taylor = TaylorSeries(f, num_terms_Taylor_series) |
| 26 | + self.f = self.Taylor.f |
| 27 | + self.h = sym.Symbol('d%s' % independent_variable) |
| 28 | + |
| 29 | + # Finite difference operators |
| 30 | + h, f, f_T = self.h, self.f, self.Taylor # short names |
| 31 | + theta = sym.Symbol('theta') |
| 32 | + self.diffops = { |
| 33 | + 'Dtp': (f_T(h) - f)/h, |
| 34 | + 'Dtm': (f - f_T(-h))/h, |
| 35 | + 'Dt': (f_T(h/2) - f_T(-h/2))/h, |
| 36 | + 'D2t': (f_T(h) - f_T(-h))/(2*h), |
| 37 | + 'DtDt': (f_T(h) - 2*f + f_T(-h))/h**2, |
| 38 | + 'barDt': (f_T((1-theta)*h) - f_T(-theta*h))/h, |
| 39 | + } |
| 40 | + self.diffops = {diffop: sym.simplify(self.diffops[diffop]) |
| 41 | + for diffop in self.diffops} |
| 42 | + |
| 43 | + self.diffops['weighted_arithmetic_mean'] = \ |
| 44 | + self._weighted_arithmetic_mean() |
| 45 | + self.diffops['geometric_mean'] = self._geometric_mean() |
| 46 | + self.diffops['harmonic_mean'] = self._harmonic_mean() |
| 47 | + |
| 48 | + def _weighted_arithmetic_mean(self): |
| 49 | + # The expansion is around n*h + theta*h |
| 50 | + h, f, f_T = self.h, self.f, self.Taylor |
| 51 | + theta = sym.Symbol('theta') |
| 52 | + f_n = f_T(-h*theta) |
| 53 | + f_np1 = f_T((1-theta)*h) |
| 54 | + a_mean = theta*f_np1 + (1-theta)*f_n |
| 55 | + return sym.expand(a_mean) |
| 56 | + |
| 57 | + def _geometric_mean(self): |
| 58 | + h, f, f_T = self.h, self.f, self.Taylor |
| 59 | + f_nmhalf = f_T(-h/2) |
| 60 | + f_nphalf = f_T(h/2) |
| 61 | + g_mean = f_nmhalf*f_nphalf |
| 62 | + return sym.expand(g_mean) |
| 63 | + |
| 64 | + def _harmonic_mean(self): |
| 65 | + h, f, f_T = self.h, self.f, self.Taylor |
| 66 | + f_nmhalf = f_T(-h/2) |
| 67 | + f_nphalf = f_T(h/2) |
| 68 | + h_mean = 2/(1/f_nmhalf + 1/f_nphalf) |
| 69 | + return sym.expand(h_mean) |
| 70 | + |
| 71 | + def D(self, i): |
| 72 | + """Return the symbol for the i-th derivative.""" |
| 73 | + return self.Taylor.df[i] |
| 74 | + |
| 75 | + def __getitem__(self, operator_name): |
| 76 | + return self.diffops.get(operator_name, None) |
| 77 | + |
| 78 | + def operator_names(self): |
| 79 | + """Return all names for the operators.""" |
| 80 | + return list(self.diffops.keys()) |
| 81 | + |
| 82 | +def truncation_errors(): |
| 83 | + # Make a table |
| 84 | + u, theta = sym.symbols('u theta') |
| 85 | + diffop = DiffOp(u, independent_variable='t', |
| 86 | + num_terms_Taylor_series=5) |
| 87 | + D1u = diffop.D(1) # symbol for du/dt |
| 88 | + D2u = diffop.D(2) # symbol for d^2u/dt^2 |
| 89 | + print 'R Dt:', diffop['Dt'] - D1u |
| 90 | + print 'R Dtm:', diffop['Dtm'] - D1u |
| 91 | + print 'R Dtp:', diffop['Dtp'] - D1u |
| 92 | + print 'R barDt:', diffop['barDt'] - D1u |
| 93 | + print 'R DtDt:', diffop['DtDt'] - D2u |
| 94 | + print 'R weighted arithmetic mean:', diffop['weighted_arithmetic_mean'] - u |
| 95 | + print 'R arithmetic mean:', diffop['weighted_arithmetic_mean'].subs(theta, sym.Rational(1,2)) - u |
| 96 | + print 'R geometric mean:', diffop['geometric_mean'] - u |
| 97 | + dt = diffop.h |
| 98 | + print 'R harmonic mean:', (diffop['harmonic_mean'] - u).\ |
| 99 | + series(dt, 0, 3).as_leading_term(dt) |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + truncation_errors() |
0 commit comments