%pylab inline from sympy import * init_printing() # or with older versions of sympy/ipython, load the IPython extension #%load_ext sympy.interactive.ipythonprinting # or #%load_ext sympyprinting x = Symbol('x') (pi + x)**2 # alternative way of defining symbols a, b, c = symbols("a, b, c") type(a) x = Symbol('x', real=True) x.is_imaginary x = Symbol('x', positive=True) x > 0 1+1*I I**2 (x * I + 1)**2 r1 = Rational(4,5) r2 = Rational(5,4) r1 r1+r2 r1/r2 pi.evalf(n=50) y = (x + pi)**2 N(y, 5) # same as evalf y.subs(x, 1.5) N(y.subs(x, 1.5)) y.subs(x, a+pi) import numpy x_vec = numpy.arange(0, 10, 0.1) y_vec = numpy.array([N(((x + pi)**2).subs(x, xx)) for xx in x_vec]) fig, ax = subplots() ax.plot(x_vec, y_vec); f = lambdify([x], (x + pi)**2, 'numpy') # the first argument is a list of variables that # f will be a function of: in this case only x -> f(x) y_vec = f(x_vec) # now we can directly pass a numpy array and f(x) is efficiently evaluated %%timeit y_vec = numpy.array([N(((x + pi)**2).subs(x, xx)) for xx in x_vec]) %%timeit y_vec = f(x_vec) (x+1)*(x+2)*(x+3) expand((x+1)*(x+2)*(x+3)) sin(a+b) expand(sin(a+b), trig=True) factor(x**3 + 6 * x**2 + 11*x + 6) # simplify expands a product simplify((x+1)*(x+2)*(x+3)) # simplify uses trigonometric identities simplify(sin(a)**2 + cos(a)**2) simplify(cos(x)/sin(x)) f1 = 1/((a+1)*(a+2)) f1 apart(f1) f2 = 1/(a+2) + 1/(a+3) f2 together(f2) simplify(f2) y diff(y**2, x) diff(y**2, x, x) diff(y**2, x, 2) # same as above x, y, z = symbols("x,y,z") f = sin(x*y) + cos(y*z) diff(f, x, 1, y, 2) f integrate(f, x) integrate(f, (x, -1, 1)) integrate(exp(-x**2), (x, -oo, oo)) n = Symbol("n") Sum(1/n**2, (n, 1, 10)) Sum(1/n**2, (n,1, 10)).evalf() Sum(1/n**2, (n, 1, oo)).evalf() Product(n, (n, 1, 10)) # 10! limit(sin(x)/x, x, 0) f diff(f, x) h = Symbol("h") limit((f.subs(x, x+h) - f)/h, h, 0) limit(1/x, x, 0, dir="+") limit(1/x, x, 0, dir="-") series(exp(x), x) series(exp(x), x, 1) series(exp(x), x, 1, 10) s1 = cos(x).series(x, 0, 5) s1 s2 = sin(x).series(x, 0, 2) s2 expand(s1 * s2) expand(s1.removeO() * s2.removeO()) (cos(x)*sin(x)).series(x, 0, 6) m11, m12, m21, m22 = symbols("m11, m12, m21, m22") b1, b2 = symbols("b1, b2") A = Matrix([[m11, m12],[m21, m22]]) A b = Matrix([[b1], [b2]]) b A**2 A * b A.det() A.inv() solve(x**2 - 1, x) solve(x**4 - x**2 - 1, x) solve([x + y - 1, x - y - 1], [x,y]) solve([x + y - a, x - y - c], [x,y])