from sympy import *
from IPython.display import display
init_printing(use_latex='mathjax')
%matplotlib inline
x, y, z = symbols('x, y, z')
m = symbols('m', integer=True)
f = symbols('f', cls=Function)
a, b = symbols('a, b', real=True, positive=True)
expr1 = x + 1
print expr1, type(expr1)
x + 1 <class 'sympy.core.add.Add'>
plot(expr1,(x,0,3),title='simple plot', xlabel='x', ylabel='x+1')
<sympy.plotting.plot.Plot at 0xb06394ac>
expr = x+y**2*sin(x); print expr.subs(x,1)
y**2*sin(1) + 1
print expr.subs({x:1, y:2})
1 + 4*sin(1)
print expr.subs([(x,y), (y,x)], simultaneous=True)
x**2*sin(y) + y
expandedForm = expand((x+2)*(x-3)); display(expandedForm)
display(factor(expandedForm))
display(factor(expandedForm + 7 - x))
expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3; display(expr)
collected_expr = collect(expr, x); display(collected_expr)
display(expand_trig(sin(x + y))) # uses sum/double angle utilities
display(trigsimp(sin(x)**4 - 2*cos(x)**2*sin(x)**2 + cos(x)**4)) # applies trig identities
a = (x + 1)**2
b = x**2 + 2*x + 1
display(simplify(a - b))
c = x**2 - 2*x + 1
display(simplify(a - c))
f = (x-1)**m * exp(x); display(f)
diff(f,x)
diff(f,x,2)
Perhaps you want to write out an expression for Newton's rule, where xn+1=xn−f(xn)f′(xn)
g = simplify(x - f/diff(f,x)); display(g)
latex(diff(f, x))
'\\frac{m \\left(x - 1\\right)^{m} e^{x}}{x - 1} + \\left(x - 1\\right)^{m} e^{x}'
The generic integration command is integrate. Note that "infinity" is represented as "oo". Here we try the integral I=∫∞−∞sinx2dx
intg = integrate(sin(x**2), (x, -oo, oo)); display(intg)
Or perhaps another interesting one ∫10xxdx
intg = integrate(x**x, (x, 0, 1)); display(intg); plot(x**x, (x,0,1))
<sympy.plotting.plot.Plot at 0xb03ca6cc>
print intg.evalf(10) # Like N[expr,10] in Mathematica
print N(intg, 30)
0.7834305107 0.783430510712134407059264386527
expr = exp(sin(x))
display(expr.series(x, 0, 4))
display(expr.series(x, 0, 4).removeO())
expr = sin(x*cos(y)); display(expr)
expr.series(x, 0, 3).removeO().series(y, 0, 3).removeO()
j = symbols('j',integer=True, positive=True)
expr = summation(j**2, (j,1,m)); display(expr)
expr = x**2 + 3*x + 2
print solve(expr)
[-2, -1]
expr1 = x + y - 2
expr2 = x - y - 4
print solve([expr1, expr2],(x,y))
{x: 3, y: -1}
from sympy.physics import units as u
print u.watt
kg*m**2/s**3
mph = u.mile / u.hour
mps = u.meter / u.second
print N(mps/mph), "mph = 1 mps"
2.23693629205440 mph = 1 mps