from sympy import * init_printing() def evaluate(exprs, x, x0): """ Evaluate each expression in exprs at the point x = x0. >>> x, y = symbols('x y') >>> exprs = [x**2, cos(x), x*y] >>> evaluate(exprs, x, 1) [1, cos(1), y] >>> evaluate(exprs, y, 0) [x**2, cos(x), 0] """ return [expr.subs(x, x0) for expr in exprs] x, y = symbols('x y') exprs = [x**2, cos(x), x*y] evaluate(exprs, x, 1) evaluate(exprs, y, 0) def uparrow(x, n): """ Computes x**(x**(...x)), with n copies of x. >>> x = symbols('x') >>> uparrow(x, 3) x**(x**x) >>> uparrow(x, 1) x >>> uparrow(x**x, 3) (x**x)**((x**x)**(x**x)) """ expr = x for i in range(n - 1): expr = x**expr return expr x = symbols('x') uparrow(x, 3) uparrow(x, 1) uparrow(x**x, 3) def nest(expr, x, n): """ Nests expr into itself (in the variable x) n times. >>> x, y = symbols('x y') >>> nest(x**x, x, 3) ((x**x)**(x**x))**((x**x)**(x**x)) >>> nest(sin(x)*cos(y), x, 2) sin(sin(x)*cos(y))*cos(y) >>> nest(sin(x)*cos(y), y, 2) sin(x)*cos(sin(x)*cos(y)) >>> nest(x**2, x, 1) x**2 """ origexpr = expr for i in range(n - 1): expr = expr.subs(x, origexpr) return expr x, y = symbols('x y') nest(x**x, x, 3) nest(sin(x)*cos(y), x, 2) nest(sin(x)*cos(y), y, 2) nest(x**2, x, 1) def trig_rewrite(expr, x): """ Rewrite all trig functions t(x) in terms of sin(x) and cos(x) >>> x, y = symbols('x y') >>> trig_rewrite(tan(x), x) sin(x)/cos(x) >>> trig_rewrite(tan(x) + cos(x)*sec(x), x) sin(x)/cos(x) + 1 >>> trig_rewrite(cot(x) + sin(x)*csc(x), x) 1 + cos(x)/sin(x) >>> trig_rewrite(tan(x)*tan(y), x) sin(x)*tan(y)/cos(x) >>> trig_rewrite(tan(x)*tan(y), y) sin(y)*tan(x)/cos(y) """ return expr.subs([(tan(x), sin(x)/cos(x)), (sec(x), 1/cos(x)), (csc(x), 1/sin(x)), (cot(x), cos(x)/sin(x))]) x, y = symbols('x y') trig_rewrite(tan(x), x) trig_rewrite(tan(x) + cos(x)*sec(x), x) trig_rewrite(cot(x) + sin(x)*csc(x), x) trig_rewrite(tan(x)*tan(y), x) trig_rewrite(tan(x)*tan(y), y) def series_reduce(expr, x, p): """ Remove all powers of x in expr with power greater than p. You may assume that there are no powers of x greater than 10. Bonus: which functions are represented by the series expansions below (you can use expr.series(x, 0, 10) to check if you are right)? >>> x, y = symbols('x y') >>> series_reduce(1 - x**2/2 + x**4/24 - x**6/720 + x**8/40320, x, 5) x**4/24 - x**2/2 + 1 >>> series_reduce(1 + x + x**2 + x**3 + x**4 + x**5 + x**6 + x**7 + x**8 + x**9 + x**10, x, 0) 1 >>> series_reduce(x*y + x**3*y**3/3 + 2*x**5*y**5/15 + 17*x**7*y**7/315 + 62*x**9*y**9/2835, x, 5) 2*x**5*y**5/15 + x**3*y**3/3 + x*y """ return expr.subs([(x**i, 0) for i in range(11) if i > p]) x, y = symbols('x y') series_reduce(1 - x**2/2 + x**4/24 - x**6/720 + x**8/40320, x, 5) series_reduce(1 + x + x**2 + x**3 + x**4 + x**5 + x**6 + x**7 + x**8 + x**9 + x**10, x, 0) series_reduce(x*y + x**3*y**3/3 + 2*x**5*y**5/15 + 17*x**7*y**7/315 + 62*x**9*y**9/2835, x, 5) from IPython.core.display import Image Image(filename='../imgs/comic2-1040.png') '1.2345'.split('.', 2)[1] str(1.2345).split('.', 2)[1].find('345') str(1.2345).split('.', 2)[1].find('345') + 1 'abcd'.find('def') def find_999999(expr, limit=100000): """ Find the first place in the decimal expr where 999999 appears. Only checks up to limit digits. Returns False when 999999 does not appear. >>> find_999999(pi) 762 >>> find_999999(E) False >>> find_999999(E, 1000000) # This one will take a few seconds to compute 384340 """ found = str(expr.evalf(limit)).split('.', 2)[1].find('999999') + 1 if found == 0: return False return found find_999999(pi) find_999999(E) find_999999(E, 1000000)