EuroSciPy2008 examples

From SymPy

Jump to: navigation, search

Contents

Integration

In [1]: integrate(LambertW(x), x)
Out[1]: 
                          x     
-x + x⋅LambertW(x) + ───────────
                     LambertW(x)
 
In [2]: _.diff(x)
Out[2]: 
       LambertW(x)                        1                      1            
-1 + ─────────────── + LambertW(x) + ─────────── - ───────────────────────────
     1 + LambertW(x)                 LambertW(x)   (1 + LambertW(x))⋅LambertW(
 
 
──
x)
 
In [3]: trim(_)
Out[3]: LambertW(x)
 
In [4]: var('a b')
Out[4]: (a, b)
 
In [5]: integrate(a*LambertW(b*x), x)
Out[5]: 
  ⎛                             x      ⎞
a⋅⎜-x + x⋅LambertW(b⋅x) + ─────────────⎟
  ⎝                       LambertW(b⋅x)⎠
 
In [6]: _.diff(x)
Out[6]: 
   ⎛                      LambertW(b⋅x)           1                         1 
-a⋅⎜1 - LambertW(b⋅x) - ───────────────── - ───────────── + ──────────────────
   ⎝                    1 + LambertW(b⋅x)   LambertW(b⋅x)   (1 + LambertW(b⋅x)
 
               ⎞
───────────────⎟
)⋅LambertW(b⋅x)⎠
 
In [7]: trim(_)
Out[7]: a⋅LambertW(b⋅x)
 
In [8]: integrate(sin(2*sqrt(x)), x)
Out[8]: 
   ⎛    ⎽⎽⎽⎞                     
sin⎝2⋅╲╱ x ⎠     ⎽⎽⎽    ⎛    ⎽⎽⎽⎞
──────────── - ╲╱ x ⋅cos⎝2⋅╲╱ x ⎠
     2                           
 
In [9]: f = (x-tan(x))/tan(x)**2 + tan(x)
 
In [10]: integrate(f, x)
Out[10]: 
   ⎛       22
log⎝1 + tan (x)⎠     x      x 
──────────────── - ────── - ──
       2           tan(x)   2

Linear Solving

In [11]: var('a b c d e f g h i')
Out[11]: (a, b, c, d, e, f, g, h, i)
 
In [12]: f_1 = a*x + b*y + c*z - 1
 
In [13]: f_2 = d*x + e*y + f*z - 1
 
In [14]: f_3 = g*x + h*y + i*z - 1
 
In [15]: s = solve([f_1, f_2, f_3], x, y, z)
 
In [16]: s
Out[16]: 
⎧         a⋅i + c⋅d + f⋅g - a⋅f - c⋅g - d⋅i                 b⋅f + c⋅h + e⋅i - 
⎨y: ─────────────────────────────────────────────, x: ────────────────────────
⎩   a⋅e⋅i + b⋅f⋅g + c⋅d⋅h - a⋅f⋅h - b⋅d⋅i - c⋅e⋅g     a⋅e⋅i + b⋅f⋅g + c⋅d⋅h - 
 
b⋅i - c⋅e - f⋅h                 a⋅e + b⋅g + d⋅h - a⋅h - b⋅d - e⋅g      ⎫
─────────────────────, z: ─────────────────────────────────────────────⎬
a⋅f⋅h - b⋅d⋅i - c⋅e⋅g     a⋅e⋅i + b⋅f⋅g + c⋅d⋅h - a⋅f⋅h - b⋅d⋅i - c⋅e⋅g⎭
 
In [17]: f_1.subs(s)
Out[17]: 
         a⋅(b⋅f + c⋅h + e⋅i - b⋅i - c⋅e - f⋅h)           b⋅(a⋅i + c⋅d + f⋅g - 
-1 + ───────────────────────────────────────────── + ─────────────────────────
     a⋅e⋅i + b⋅f⋅g + c⋅d⋅h - a⋅f⋅h - b⋅d⋅i - c⋅e⋅g   a⋅e⋅i + b⋅f⋅g + c⋅d⋅h - a
 
a⋅f - c⋅g - d⋅i)           c⋅(a⋅e + b⋅g + d⋅h - a⋅h - b⋅d - e⋅g)    
──────────────────── + ─────────────────────────────────────────────
⋅f⋅h - b⋅d⋅i - c⋅e⋅g   a⋅e⋅i + b⋅f⋅g + c⋅d⋅h - a⋅f⋅h - b⋅d⋅i - c⋅e⋅g
 
In [18]: simplify(f_1.subs(s))
Out[18]: 0
 
In [19]: simplify(f_2.subs(s))
Out[19]: 0
 
In [20]: simplify(f_3.subs(s))
Out[20]: 0

Nonlinear Solving

In [21]: f_1 = x**2 + y + z - 1
 
In [22]: f_2 = x + y**2 + z - 1
 
In [23]: f_3 = x + y + z**2 - 1
 
In [24]: solve([f_1, f_2, f_3], x, y, z)
Out[24]: 
⎡⎛       ⎽⎽⎽         ⎽⎽⎽         ⎽⎽⎽⎞                        ⎛       ⎽⎽⎽      
⎣⎝-1 - ╲╱ 2 , -1 - ╲╱ 2 , -1 - ╲╱ 2 ⎠, (0, 0, 1), (0, 1, 0), ⎝-1 + ╲╱ 2 , -1 +
 
   ⎽⎽⎽         ⎽⎽⎽⎞           ⎤
 ╲╱ 2 , -1 + ╲╱ 2 ⎠, (1, 0, 0)

Factorization

In [1]: factor(x**56-1, x)
Out[1]: 
                 ⎛     2⎞ ⎛     4⎞ ⎛         2    3    4    5    6⎞ ⎛         
-(1 + x)(1 - x)⋅⎝1 + x ⎠⋅⎝1 + x ⎠⋅⎝1 + x + x  + x  + x  + x  + x ⎠⋅⎝1 - x + x
 
2    3    4    5    6⎞ ⎛     2    4    6    8    10    12⎞ ⎛     4    8    12 
  - x  + x  - x  + x ⎠⋅⎝1 - x  + x  - x  + x  - x   + x  ⎠⋅⎝1 - x  + x  - x   
 
   16    20    24⎞
+ x   - x   + x  ⎠
 
In [2]: f = 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
 
In [3]: f
Out[3]: 
                2       3       4       5        6       7       8        9   
16 - 32⋅x - 64⋅x  - 64⋅x  - 52⋅x  - 40⋅x  - 132⋅x  - 24⋅x  - 50⋅x  + 120⋅x  + 
 
    10       11       12       13       14      15    16
66⋅x   + 92⋅x   + 47⋅x   + 32⋅x   + 14⋅x   + 4⋅x   + x  
 
In [4]: factor(f, x)
Out[4]: 
⎛       2      4      6    8⎞ ⎛              2      3      4       5       64 + 4⋅x  + 9⋅x  + 4⋅x  + x ⎠⋅⎝4 - 8⋅x - 20⋅x  - 8⋅x  - 2⋅x  + 16⋅x  + 10⋅x  +
 
    7    84⋅x  + x ⎠

Term Rewriting

In [29]: 36/(x**5-2*x**4-2*x**3+4*x**2+x-2)
Out[29]: 
               36               
────────────────────────────────
            2      3      4    5
-2 + x + 4⋅x  - 2⋅x  - 2⋅x  + x 
 
In [30]: f = apart(36/(x**5-2*x**4-2*x**3+4*x**2+x-2), x)
 
In [31]: f
Out[31]: 
    4       4         9          3    
- ───── + ────── - ──────── - ────────
  1 + x   -2 + x          2          2
                   (1 - x)    (1 + x) 
 
In [32]: f = apart(36/(x**5-2*x**4-2*x**3+4*x**2+x-2), x, evaluate=False)
 
In [33]: f
Out[33]: 
RootSum(Lambda(_a, 4/(x - _a)), x - 2, x) + RootSum(Lambda(_a, -1/(x - _a)**2*
(6 + 3*_a)), x**2 - 1, x) + RootSum(Lambda(_a, -4/(x - _a)), x + 1, x)
 
In [34]: Add(*[ g.doit() for g in f.args ])
Out[34]: 
    4       4         9          3    
- ───── + ────── - ──────── - ────────
  1 + x   -2 + x          2          2
                   (1 - x)    (1 + x)

Matrices

In [35]: M = Matrix(4, 4, lambda i,j: i*j+x)
 
In [36]: M
Out[36]: 
⎡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⎦
 
In [37]: M.eigenvals()
Out[37]: 
⎧             ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                  ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽      
⎪            ╱                  22       
⎨          ╲╱  196 + 32⋅x + 16⋅x                 ╲╱  196 + 32⋅x + 16⋅x        
⎪7 + 2⋅x + ───────────────────────: 1, 7 + 2⋅x - ───────────────────────: 1, 02                                     2                 
 
   ⎫
   ⎪
   ⎬
: 2⎪
   ⎭
Personal tools