import sys; def difference_eq_print1(f, x0, N): xp = x0; for n in range(N): x = f(xp); print(x) # Update variables xp = x; def difference_eq_print2(f,x0,x1, N): xp = x1; xpp = x0; for n in range(N): x = f(xpp, xp); print(x) # Update variables xpp = xp; xp = x; def difference_eq_print3(f,x0,x1,x2, N): xp = x2; xpp = x1; xppp = x0; for n in range(N): x = f(xppp, xpp, xp); print(x) # Update variables xppp = xpp; xpp = xp; xp = x; def difference_eq_print_general(f,x_list,N): """ Assume x_list = [x0,...,xn]; """ for n in range(N): x_value = f(x_list) x_list = x_list[1:]; x_list.append(x_value); print(x_value); def fibonacci2(xpp,xp): return xp + xpp; def fibonacci3(xppp,xpp,xp): return xppp + xpp + xp; def fibonacci2_general(x): return x[0] + x[1]; def difference_eq2(f,x0,x1, N): xp = x1; xpp = x0; x_list = [] for n in range(N): x = f(xpp, xp); print(x); x_list.append(x); # Update variables xpp = xp; xp = x; return x_list; if __name__ == "__main__": if (len(sys.argv) < 2): N = 10; else: N = int(sys.argv[1]); #python3 difference_eps.py 10 "f = lambda xpp,xp: xp+xpp" x0 = 1; x1 = 1; difference_eq_print2(fibonacci2, x0, x1, N); print('---------') x_list = [x0,x1]; difference_eq_print_general(fibonacci2_general, x_list, N); print('---------') x2 = 1; difference_eq_print3(fibonacci3, x0, x1, x2, N); print('---------') if (len(sys.argv) == 3): exec(sys.argv[2]); # Someone has provided a function f(xpp,xp) on the command line difference_eq_print2(f,x0,x1,N); print('---------') x_list = difference_eq2(fibonacci2, x0, x1, N);