sympy

133 days ago by ondrej

from sympy import * var("x y z n") 
       
(x, y, z, n)

Basic things

f = (x+z)**2 
       
       
(x + z)**2
expand(f) 
       
2*x*z + x**2 + z**2
factor(_) 
       
(x + z)**2
f = expand((x+y)*(y+z)*(x+z)) 
       
factor(f) 
       
(x + y)*(y + z)*(x + z)
sin(x).series(x, 0, 10) 
       
x - x**3/6 + x**5/120 - x**7/5040 + x**9/362880 + O(x**10)
pi.n(100) 
       
3.141592653589793238462643383279502884197169399375105820974944592307\
816406286208998628034825342117068

INTEGRATION

integrate(sin(x)*exp(x), x) 
       
exp(x)*sin(x)/2 - cos(x)*exp(x)/2
diff(_, x) 
       
exp(x)*sin(x)
integrate(sin(x)*exp(2*x), x) 
       
2*exp(2*x)*sin(x)/5 - cos(x)*exp(2*x)/5
diff(_, x) 
       
exp(2*x)*sin(x)
integrate(1/(x**2+1), x) 
       
atan(x)
diff(_, x) 
       
1/(1 + x**2)
integrate(1/(x**4+1), x) 
       
-2**(1/2)*log(1 - x*2**(1/2) + x**2)/8 + 2**(1/2)*atan(1 +
x*2**(1/2))/4 + 2**(1/2)*atan(-1 + x*2**(1/2))/4 + 2**(1/2)*log(1 +
x*2**(1/2) + x**2)/8
diff(_, x) 
       
-2**(1/2)*(2*x - 2**(1/2))/(8*(1 - x*2**(1/2) + x**2)) +
2**(1/2)*(2*x + 2**(1/2))/(8*(1 + x*2**(1/2) + x**2)) + 1/(2*(1 + (1
+ x*2**(1/2))**2)) + 1/(2*(1 + (1 - x*2**(1/2))**2))
simplify(_) 
       
1/(1 + x**4)
integrate(LambertW(x), x) 
       
-x + x*LambertW(x) + x/LambertW(x)
diff(_, x) 
       
-1 + LambertW(x)/(1 + LambertW(x)) + LambertW(x) + 1/LambertW(x) -
1/((1 + LambertW(x))*LambertW(x))
simplify(_) 
       
LambertW(x)
var('a b') integrate(a*LambertW(b*x), x) 
       
a*(-x + x*LambertW(b*x) + x/LambertW(b*x))
diff(_, x) 
       
-a*(1 - LambertW(b*x) - LambertW(b*x)/(1 + LambertW(b*x)) -
1/LambertW(b*x) + 1/((1 + LambertW(b*x))*LambertW(b*x)))
simplify(_) 
       
a*LambertW(b*x)
integrate(sin(2*sqrt(x)), x) 
       
sin(2*x**(1/2))/2 - x**(1/2)*cos(2*x**(1/2))
diff(_, x) 
       
sin(2*x**(1/2))
integrate((x-tan(x))/tan(x)**2 + tan(x), x) 
       
log(1 + tan(x)**2)/2 - x/tan(x) - x**2/2
diff(_, x) 
       
-x + x*(1 + tan(x)**2)/tan(x)**2 + (2 + 2*tan(x)**2)*tan(x)/(2*(1 +
tan(x)**2)) - 1/tan(x)
simplify(_) 
       
(x - tan(x) + tan(x)**3)/tan(x)**2
(x-tan(x))/tan(x)**2 + tan(x) - (x - tan(x) + tan(x)**3)/tan(x)**2 
       
(x - tan(x))/tan(x)**2 - (x - tan(x) + tan(x)**3)/tan(x)**2 + tan(x)
simplify(_) 
       
0

Linear Solving

var('a b c d e f g h i') f_1 = a*x + b*y + c*z - 1 f_2 = d*x + e*y + f*z - 1 f_3 = g*x + h*y + i*z - 1 s = solve([f_1, f_2, f_3], x, y, z) s 
       
{x: (b*f + c*h + e*i - b*i - c*e - f*h)/(a*e*i + b*f*g + c*d*h -
a*f*h - b*d*i - c*e*g), y: (a*i + c*d + f*g - a*f - c*g -
d*i)/(a*e*i + b*f*g + c*d*h - a*f*h - b*d*i - c*e*g), z: (a*e + b*g
+ d*h - a*h - b*d - e*g)/(a*e*i + b*f*g + c*d*h - a*f*h - b*d*i -
c*e*g)}
f_1.subs(s) 
       
-1 + a*(b*f + c*h + e*i - b*i - c*e - f*h)/(a*e*i + b*f*g + c*d*h -
a*f*h - b*d*i - c*e*g) + b*(a*i + c*d + f*g - a*f - c*g -
d*i)/(a*e*i + b*f*g + c*d*h - a*f*h - b*d*i - c*e*g) + c*(a*e + b*g
+ d*h - a*h - b*d - e*g)/(a*e*i + b*f*g + c*d*h - a*f*h - b*d*i -
c*e*g)
simplify(_) 
       
0
print simplify(f_1.subs(s)) print simplify(f_2.subs(s)) print simplify(f_3.subs(s)) 
       
0
0
0

Factorization

factor(x**56-1, x) 
       
-(1 + x)*(1 + x**2)*(1 + x**4)*(1 - x)*(1 - x + x**2 - x**3 + x**4 -
x**5 + x**6)*(1 + x + x**2 + x**3 + x**4 + x**5 + x**6)*(1 - x**4 +
x**8 - x**12 + x**16 - x**20 + x**24)*(1 - x**2 + x**4 - x**6 + x**8
- x**10 + x**12)
x**16+4*x**15+14*x**14+32*x**13+47*x**12+92*x**11+66*x**10+120*x**9-50*x**8-24*x**7-132*x**6-40*x**5-52*x**4-64*x**3-64*x**2-32*x+16 
       
16 - 32*x - 64*x**2 - 64*x**3 - 52*x**4 - 40*x**5 - 132*x**6 -
24*x**7 - 50*x**8 + 120*x**9 + 66*x**10 + 92*x**11 + 47*x**12 +
32*x**13 + 14*x**14 + 4*x**15 + x**16
factor(_) 
       
(4 + 4*x**2 + 9*x**4 + 4*x**6 + x**8)*(4 - 8*x - 20*x**2 - 8*x**3 -
2*x**4 + 16*x**5 + 10*x**6 + 4*x**7 + x**8)

Term Rewriting

36/(x**5-2*x**4-2*x**3+4*x**2+x-2) 
       
-36/(2 - x - 4*x**2 + 2*x**3 + 2*x**4 - x**5)
apart(_, x) 
       
-4/(1 + x) - 4/(2 - x) - 9/(1 - x)**2 - 3/(1 + x)**2
simplify(_) 
       
-36/(2 - x - 4*x**2 + 2*x**3 + 2*x**4 - x**5)

Matrices

Matrix(4, 4, lambda i,j: i*j+1) 
       
[1, 1, 1,  1]
[1, 2, 3,  4]
[1, 3, 5,  7]
[1, 4, 7, 10]
_.eigenvals() 
       
{9 - 61**(1/2): 1, 9 + 61**(1/2): 1, 0: 2}
Matrix(4, 4, lambda i,j: i*j+3) 
       
[3, 3, 3,  3]
[3, 4, 5,  6]
[3, 5, 7,  9]
[3, 6, 9, 12]
_.eigenvals() 
       
{13 + 109**(1/2): 1, 13 - 109**(1/2): 1, 0: 2}
Matrix(4, 4, lambda i,j: i*j+x) 
       
[x,     x,     x,     x]
[x, 1 + x, 2 + x, 3 + x]
[x, 2 + x, 4 + x, 6 + x]
[x, 3 + x, 6 + x, 9 + x]
_.eigenvals() 
       
{7 + 2*x + (196 + 32*x + 16*x**2)**(1/2)/2: 1, 7 + 2*x - (196 + 32*x
+ 16*x**2)**(1/2)/2: 1, 0: 2}

Integrals table

def derivative_table(functions, x): for f in functions: s = pretty(Eq(Derivative(f, x), diff(f, x))) print "<pre>" + s + "</pre>", "\n" def integral_table(functions, x): for f in functions: s = pretty(Eq(Integral(f, x), integrate(f, x))) print "<pre>" + s + "</pre>", "\n" print "<html>" print "<h3>Derivatives</h3>" derivative_table([cos(x)/(1 + sin(x)**i) for i in range(1, 5)], x) print "<h3>Integrals</h3>" integral_table([x**i * exp(i*x) for i in range(1, 5)], x) print "</html>" 
       

Derivatives

                                      2      
d ⎛  cos(x)  ⎞       sin(x)        cos (x)   
──⎜──────────⎟ = - ────────── - ─────────────
dx⎝1 + sin(x)⎠     1 + sin(x)               2
                                (1 + sin(x)) 
                                       2          
d ⎛   cos(x)  ⎞        sin(x)     2⋅cos (x)⋅sin(x)
──⎜───────────⎟ = - ─────────── - ────────────────
dx⎜       2   ⎟            2                    2 
  ⎝1 + sin (x)⎠     1 + sin (x)    ⎛       2   ⎞  
                                   ⎝1 + sin (x)⎠  
                                       2       2   
d ⎛   cos(x)  ⎞        sin(x)     3⋅cos (x)⋅sin (x)
──⎜───────────⎟ = - ─────────── - ─────────────────
dx⎜       3   ⎟            3                     2 
  ⎝1 + sin (x)⎠     1 + sin (x)     ⎛       3   ⎞  
                                    ⎝1 + sin (x)⎠  
                                       2       3   
d ⎛   cos(x)  ⎞        sin(x)     4⋅cos (x)⋅sin (x)
──⎜───────────⎟ = - ─────────── - ─────────────────
dx⎜       4   ⎟            4                     2 
  ⎝1 + sin (x)⎠     1 + sin (x)     ⎛       4   ⎞  
                                    ⎝1 + sin (x)⎠  

Integrals

⌠                      
⎮    x         x      x
⎮ x⋅ℯ  dx = - ℯ  + x⋅ℯ 
⌡                      
⌠               2⋅x    2  2⋅x      2⋅x
⎮  2  2⋅x      ℯ      x ⋅ℯ      x⋅ℯ   
⎮ x ⋅ℯ    dx = ──── + ─────── - ──────
⌡               4        2        2   
⌠                   3⋅x    2  3⋅x    3  3⋅x        3⋅x
⎮  3  3⋅x        2⋅ℯ      x ⋅ℯ      x ⋅ℯ      2⋅x⋅ℯ   
⎮ x ⋅ℯ    dx = - ────── - ─────── + ─────── + ────────
⌡                  27        3         3         9    
⌠                 4⋅x        4⋅x    3  4⋅x    4  4⋅x      2  4⋅x
⎮  4  4⋅x      3⋅ℯ      3⋅x⋅ℯ      x ⋅ℯ      x ⋅ℯ      3⋅x ⋅ℯ   
⎮ x ⋅ℯ    dx = ────── - ──────── - ─────── + ─────── + ─────────
⌡               128        32         4         4          16   

Numerical Integration

# One-liner: Integral(sin(1/x), (x, 0, 1)).transform(x, 1/x).n(10, quad="osc") 
       
0.5040670619
# Detailed steps: e = Integral(sin(1/x), (x, 0, 1)) print e print e.transform(x, 1/x) print e.transform(x, 1/x).evalf(quad="osc") print e.transform(x, 1/x).evalf(10, quad="osc") 
       
Integral(sin(1/x), (x, 0, 1))
Integral(sin(x)/x**2, (x, 1, oo))
0.504067061906928
0.5040670619

Numerical Summation

# quickly convergent series: Sum((2*n**3+1)/factorial(2*n+1), (n, 0, oo)).n(1000) 
       
1.652941212640472981900739198325231452667553042183503755040875167115\
36520700285477118747045228498906167383807929789641305010501152379438\
61069843772358511099213248084094702974173459412697848275449887634172\
36310807961946377892899972740673038357199917316237084560028761604522\
44335008069814657760143015685186309692763577831488062076063878821591\
47991853611021335166249970882921787645572147664874864765961272185645\
52920654866882117842205079773964081909715996765062696534198400786487\
20547181263634904386890312520113790407288117484857833912316663821965\
01485612278681568073802853219958825308722334919826628507270651306336\
14162541245606020742341275663241068292591605973877489004037593872370\
53819476975745814997936719261771459668913327102954310369427152930632\
55742056366612644881895850180191142902938099638992839007008491684002\
06843073141923590673684071292816767330876818608398596486922023934122\
51327571382250243177131636593650408691594372170313456985355199509793\
7040728520746689993201707235774309731234398779684
# slowly convergent (polynomial rate) series: Sum(n/(n**3+9), (n, 1, oo)).n(20) 
       
0.57208579952127403813

Numerical Simplification

1./7 
       
0.14285714285714285
nsimplify(_) 
       
1/7
1./81 
       
0.012345679012345678
nsimplify(_) 
       
1/81
nsimplify(pi, tolerance=0.01) 
       
22/7
nsimplify(pi, tolerance=0.001) 
       
355/113
nsimplify(0.33333, tolerance=1e-4) 
       
1/3
nsimplify(4.71, [pi], tolerance=0.01) 
       
3*pi/2
nsimplify(2.0**(1/3.), tolerance=0.001) 
       
635/504
nsimplify(2.0**(1/3.), tolerance=0.001, full=True) 
       
2**(1/3)
(4/(1+sqrt(5))).n() 
       
1.23606797749979
nsimplify(1.23606797749979, [GoldenRatio]) 
       
2/GoldenRatio
(2/GoldenRatio).n() 
       
1.23606797749979
nsimplify(2 + exp(2*atan('1/4')*I)) 
       
49/17 + 8*I/17
nsimplify((1/(exp(3*pi*I/5)+1))) 
       
1/2 - I*(1/4 + 5**(1/2)/10)**(1/2)
nsimplify(I**I, [pi]) 
       
exp(-pi/2)
Sum(1/n**2, (n, 1, oo)).n() 
       
1.64493406684823
nsimplify(Sum(1/n**2, (n, 1, oo)), [pi]) 
       
pi**2/6
(pi**2/6).n() 
       
1.64493406684823
nsimplify(gamma('1/4')*gamma('3/4'), [pi]) 
       
pi*2**(1/2)

Ordinary differential equations

f = Function("f") e = Eq(f(x).diff(x, x) + 9*f(x) + 1, 1) print e dsolve(e, f(x)) 
       
1 + 9*f(x) + D(f(x), x, x) == 1
f(x) == C1*sin(3*x) + C2*cos(3*x)
e = Eq(f(x).diff(x, x) + 8*f(x) + 1, 1) print e dsolve(e, f(x)) 
       
1 + 8*f(x) + D(f(x), x, x) == 1
f(x) == C1*sin(2*x*2**(1/2)) + C2*cos(2*x*2**(1/2))

Limits

limit(sin(x)/x, x, 0) 
       
1
sqrt3 = lambda x: x**(S(1)/3) limit((sqrt3(x**2)-2*sqrt3(x)+1)/(x-1)**2, x, 1) 
       
1/9