In [1]:
from sympy import *
In [2]:
from sympy import init_session
init_session()
In [3]:
from sympy.stats import *
E(Die('X', 6))
Out[3]:
In [4]:
sqrt(8)
Out[4]:
In [5]:
expr = x + 2*y
In [6]:
expr2 = x*expr
In [7]:
expr2
Out[7]:
In [8]:
expand(expr2)
Out[8]:
In [9]:
factor(expand(expr2))
Out[9]:
In [10]:
diff(sin(x) * exp(x), x)
Out[10]:
In [11]:
integrate(exp(x)*sin(x) + exp(x)*cos(x), x)
Out[11]:
In [12]:
integrate(sin(x**2), (x, -oo, oo))
Out[12]:
In [13]:
dsolve(Eq(f(t).diff(t, t) - f(t), exp(t)), f(t))
Out[13]:
In [14]:
Matrix([[1,2],[2,2]]).eigenvals()
Out[14]:
In [15]:
nu = symbols('nu')
besselj(nu, z).rewrite(jn)
Out[15]:
In [16]:
latex(Integral(cos(x)**2, (x, 0, pi)))
Out[16]:
In [17]:
expr = cos(x) + 1
In [18]:
expr.subs(x, y)
Out[18]:
In [19]:
expr = x**y
expr = expr.subs(y, x**y)
expr = expr.subs(y, x**y)
expr = expr.subs(x, x**x)
In [20]:
expr
Out[20]:
In [21]:
expr = sin(2*x) + cos(2*x)
expand_trig(expr)
Out[21]:
In [22]:
expr.subs(sin(2*x), 2*sin(x)*cos(x))
Out[22]:
In [23]:
expr = x**4 - 4*x**3 + 4*x**2 - 2*x +3
In [24]:
replacements = [(x**i, y**i) for i in range(5) if i%2 == 0]
expr.subs(replacements)
Out[24]:
In [25]:
sexpr = "x**4 - 4*x**3 + 4*x**2 - 2*x +3"
sympify(sexpr)
Out[25]:
In [26]:
sqrt(8).evalf()
Out[26]:
In [27]:
pi.evalf(100)
Out[27]:
In [28]:
expr = cos(2*x)
expr.evalf(subs = {x: 2.4})
Out[28]:
In [29]:
expr = cos(x)**2 + sin(x)**2
expr.evalf(subs = {x: 1}, chop=True)
Out[29]:
In [30]:
a = range(10)
expr = sin(x)
f = lambdify(x, expr, "numpy")
f(a)
Out[30]:
In [31]:
simplify(sin(x)**2 + cos(x)**2)
Out[31]:
In [32]:
simplify(gamma(x) / gamma(x-2))
Out[32]:
In [33]:
expand((x + y)**3)
Out[33]:
In [34]:
factor(x**3 - x**2 + x - 1)
Out[34]:
In [35]:
factor_list(x**3 - x**2 + x - 1)
Out[35]:
In [36]:
expand((cos(x) + sin(x))**2)
Out[36]:
In [37]:
expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3
cexpr = collect(expr, x)
cexpr
Out[37]:
In [38]:
cexpr.coeff(x**2)
Out[38]:
In [39]:
cancel((x**2 + 2*x + 1) / (x**2 + x))
Out[39]:
In [40]:
expr = (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2)/(x**2 - 1)
In [41]:
cancel(expr)
Out[41]:
In [42]:
factor(expr)
Out[42]:
In [43]:
expr = (4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)
In [44]:
apart(expr)
Out[44]:
In [45]:
trigsimp(sin(x)*tan(x)/sec(x))
Out[45]:
In [46]:
trigsimp(cosh(x)**2 + sinh(x)**2)
Out[46]:
In [47]:
expand_trig(sin(x + y))
Out[47]:
In [48]:
x, y = symbols('x y', positive=True)
a, b = symbols('a b', real=True)
z, t, c = symbols('z t c')
In [49]:
powsimp(x**a*x**b)
Out[49]:
In [50]:
powsimp(x**a*y**a)
Out[50]:
In [51]:
powsimp(t**c*z**c)
Out[51]:
In [52]:
powsimp(t**c*z**c, force=True)
Out[52]:
In [53]:
expand_power_exp(x**(a + b))
Out[53]:
In [54]:
expand_power_base((x*y)**a)
Out[54]:
In [55]:
powdenest((x**a)**b)
Out[55]:
In [56]:
n = symbols('n', real=True)
In [57]:
expand_log(log(x*y))
Out[57]:
In [58]:
expand_log(log(x**n))
Out[58]:
In [59]:
logcombine(n*log(x))
Out[59]:
In [60]:
x, y, z = symbols('x y z')
k, m, n = symbols('k m n')
In [61]:
factorial(n)
Out[61]:
In [62]:
binomial(n, k)
Out[62]:
In [63]:
hyper([1,2], [3], z)
Out[63]:
In [64]:
factorial(x).rewrite(gamma)
Out[64]:
In [65]:
tan(x).rewrite(sin)
Out[65]:
In [66]:
expand_func(gamma(x + 3))
Out[66]:
In [67]:
hyperexpand(hyper([1, 1], [2], z))
Out[67]:
In [68]:
expr = meijerg([[1],[1]], [[1],[]], -z)
In [69]:
expr
Out[69]:
In [70]:
hyperexpand(expr)
Out[70]:
In [71]:
combsimp(binomial(n+1, k+1)/binomial(n, k))
Out[71]:
In [72]:
def list_to_frac(l):
expr = Integer(0)
for i in reversed(l[1:]):
expr += i
expr = 1/expr
return l[0] + expr
In [73]:
list_to_frac([1,2,3,4])
Out[73]:
In [74]:
syms = symbols('a0:5')
In [75]:
syms
Out[75]:
In [76]:
frac = list_to_frac(syms)
In [77]:
frac
Out[77]:
In [78]:
frac = cancel(frac)
In [79]:
frac
Out[79]:
In [80]:
from sympy.printing import print_ccode
print_ccode(frac)
In [81]:
diff(cos(x), x)
Out[81]:
In [82]:
diff(x**4, x, 3)
Out[82]:
In [83]:
expr = exp(x*y*z)
diff(expr, x, y, 2, z, 4)
Out[83]:
In [84]:
deriv = Derivative(expr, x, y, 2, z, 4)
deriv
Out[84]:
In [85]:
deriv.doit()
Out[85]:
In [86]:
integrate(cos(x), x)
Out[86]:
In [87]:
integrate(exp(-x), (x, 0, oo))
Out[87]:
In [88]:
integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))
Out[88]:
In [89]:
integrate(x**x, x)
Out[89]:
In [90]:
expr = Integral(log(x)**2, x)
expr
Out[90]:
In [91]:
expr.doit()
Out[91]:
In [92]:
integrate(sin(x**2), x)
Out[92]:
In [93]:
integrate(x**y*exp(-x), (x, 0, oo))
Out[93]:
In [94]:
limit(sin(x)/x, x, 0)
Out[94]:
In [95]:
expr = Limit((cos(x) - 1)/x, x, 0)
expr
Out[95]:
In [96]:
expr.doit()
Out[96]:
In [97]:
limit(1/x, x, 0, '-')
Out[97]:
In [98]:
expr = exp(sin(x))
expr.series(x, 0, 4)
Out[98]:
In [99]:
x + x**3 + x**6 + O(x**4)
Out[99]:
In [100]:
x * O(1)
Out[100]:
In [101]:
expr.series(x, 0, 4).removeO()
Out[101]:
In [102]:
exp(x - 6).series(x, 6)
Out[102]:
In [103]:
exp(x - 6).series(x, 6).removeO().subs(x, x - 6)
Out[103]:
In [104]:
M = Matrix([[1,2,3],[3,2,1]])
P = Matrix([0,1,1])
M*P
Out[104]:
In [105]:
M
Out[105]:
In [106]:
M.shape
Out[106]:
In [107]:
M.col(-1)
Out[107]:
In [108]:
M = Matrix([[1,3], [-2,3]])
M**-1
Out[108]:
In [109]:
M.T
Out[109]:
In [110]:
eye(3)
Out[110]:
In [111]:
diag(1,2,3)
Out[111]:
In [112]:
M.det()
Out[112]:
In [113]:
M= Matrix([[1,0,1,3],[2,3,4,7],[-1,-3,-3,-4]])
M
Out[113]:
In [114]:
M.rref()
Out[114]:
In [115]:
M = Matrix([[1,2,3,0,0],[4,10,0,0,1]])
M
Out[115]:
In [116]:
M.nullspace()
Out[116]:
In [117]:
M = Matrix([[3, -2, 4, -2], [5,3,-3,-2], [5,-2,2,-2], [5,-2,-3,3]])
M
Out[117]:
In [118]:
M.eigenvals()
Out[118]:
In [119]:
M.eigenvects()
Out[119]:
In [120]:
P, D = M.diagonalize()
In [121]:
P
Out[121]:
In [122]:
D
Out[122]:
In [123]:
P*D*P**-1
Out[123]:
In [124]:
lamda = symbols('lamda')
p = M.charpoly(lamda)
p
Out[124]:
In [125]:
factor(p)
Out[125]:
In [126]:
solve(x**2 - 1, x)
Out[126]:
In [127]:
solve((x - y + 2, x + y -3), (x, y))
Out[127]:
In [128]:
solve(x**3 - 6*x**2 + 9*x, x)
Out[128]:
In [129]:
roots(x**3 - 6*x**2 + 9*x, x)
Out[129]:
In [130]:
f, g = symbols('f g', cls=Function)
In [131]:
f(x).diff(x)
Out[131]:
In [132]:
diffeq = Eq(f(x).diff(x, 2) - 2*f(x).diff(x) + f(x), sin(x))
In [133]:
diffeq
Out[133]:
In [134]:
dsolve(diffeq, f(x))
Out[134]:
In [135]:
dsolve(f(x).diff(x)*(1 - sin(f(x))), f(x))
Out[135]:
In [136]:
a, t = symbols('a t')
f(t).diff(t)
diffeq = Eq(f(t).diff(t), a*t)
In [137]:
diffeq
Out[137]:
In [138]:
dsolve(diffeq, f(t))
Out[138]:
In [139]:
x = symbols('x', cls=Function)
diffeq = Eq(x(t).diff(t), a*x(t))
diffeq
Out[139]:
In [140]:
dsolve(diffeq, x(t))
Out[140]:
In [141]:
N(pi, 10)
Out[141]:
In [142]:
x = symbols('x')
In [143]:
expr = Integral(sin(x)/(x**2), (x, 1, oo))
In [144]:
expr.evalf()
Out[144]:
In [145]:
expr.evalf(maxn=20)
Out[145]:
In [146]:
expr.evalf(quad='osc')
Out[146]:
In [147]:
expr.evalf(20, quad='osc')
Out[147]:
In [148]:
expr = Integral(sin(1/x), (x, 0, 1))
expr
Out[148]:
In [149]:
expr.evalf()
Out[149]:
In [150]:
expr = expr.transform(x, 1/x)
expr
Out[150]:
In [151]:
expr.evalf(quad='osc')
Out[151]:
In [152]:
nsimplify(pi, tolerance=0.001)
Out[152]:
In [153]:
expr = sin(x)/x
In [154]:
%timeit expr.evalf(subs={x: 3.14})
In [155]:
f1 = lambdify(x, expr)
%timeit f1(3.14)
In [156]:
f2 = lambdify(x, expr, 'numpy')
%timeit f2(3.14)
In [157]:
%timeit f2(np.linspace(1, 10, 10000))
In [158]:
%timeit [f1(x) for x in np.linspace(1, 10, 10000)]
In [159]:
from mpmath import *
In [160]:
f = odefun(lambda x, y: [-y[1], y[0]], 0, [1, 0])
for x in [0, 1, 2.5, 10]:
nprint(f(x), 15)
nprint([cos(x), sin(x)], 15)
In [161]:
from sympy.plotting import plot
%matplotlib inline
plot(x*y**3 - y*x**3)
pass
In [162]:
from sympy.plotting import plot3d_parametric_surface
from sympy import sin, cos
u, v = symbols('u v')
plot3d_parametric_surface(cos(u + v), sin(u - v), u-v, (u, -5, 5), (v, -5, 5))
pass
In [163]:
from sympy.stats import *
In [164]:
k = Symbol("k", positive=True)
theta = Symbol("theta", positive=True)
z = Symbol("z")
X = Gamma("x", k, theta)
In [165]:
D = density(X)(z)
D
Out[165]:
In [166]:
C = cdf(X, meijerg=True)(z)
C
Out[166]:
In [167]:
E(X)
Out[167]:
In [168]:
V = variance(X)
V
Out[168]:
In [169]:
simplify(V)
Out[169]:
In [170]:
N = Normal('Gaussian', 10, 5)
density(N)(z)
Out[170]:
In [171]:
density(N)(3).evalf()
Out[171]:
In [172]:
simplify(cdf(N)(z))
Out[172]:
In [173]:
P(N > 10)
Out[173]:
In [174]:
sample(N)
Out[174]: