Skip to content

Euroscipy2008 examples

certik edited this page Feb 8, 2011 · 1 revision

Table of Contents

Integration

In [1]: integrate(LambertW(x), x)
Out[1]: 
                          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      ⎞
‹…⎜-x + ‹…LambertW(‹…x) + ─────────────⎟
  âŽLambertW(‹…x)⎠

In [6]: _.diff(x)
Out[6]: 
   âŽLambertW(‹…x)           1                         1 
-‹…⎜1 - LambertW(‹…x) - ───────────────── - ───────────── + ──────────────────
   âŽ1 + LambertW(‹…x)   LambertW(‹…x)   (1 + LambertW(‹…x)

               ⎞
───────────────⎟
)â‹…LambertW(‹…x)⎠

In [7]: trim(_)
Out[7]: ‹…LambertW(‹…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]: 
   âŽ2   ⎞             2
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]: 
⎧         ‹…i + ‹…d + ‹…g - ‹…f - ‹…g - ‹…i                 ‹…f + ‹…h + ‹…i - 
⎨y: ─────────────────────────────────────────────, x: ────────────────────────
⎩   ‹…‹…i + ‹…‹…g + ‹…‹…h - ‹…‹…h - ‹…‹…i - ‹…‹…g     ‹…‹…i + ‹…‹…g + ‹…‹…h - 

‹…i - ‹…e - ‹…h                 ‹…e + ‹…g + ‹…h - ‹…h - ‹…d - ‹…g      ⎫
─────────────────────, z: ─────────────────────────────────────────────⎬
‹…‹…h - ‹…‹…i - ‹…‹…g     ‹…‹…i + ‹…‹…g + ‹…‹…h - ‹…‹…h - ‹…‹…i - ‹…‹…g⎭

In [17]: f_1.subs(s)
Out[17]: 
         ‹…(‹…f + ‹…h + ‹…i - ‹…i - ‹…e - ‹…h)           ‹…(‹…i + ‹…d + ‹…g - 
-1 + ───────────────────────────────────────────── + ─────────────────────────
     ‹…‹…i + ‹…‹…g + ‹…‹…h - ‹…‹…h - ‹…‹…i - ‹…‹…g   ‹…‹…i + ‹…‹…g + ‹…‹…h - a

‹…f - ‹…g - ‹…i)           ‹…(‹…e + ‹…g + ‹…h - ‹…h - ‹…d - ‹…g)    
──────────────────── + ─────────────────────────────────────────────
â‹…‹…h - ‹…‹…i - ‹…‹…g   ‹…‹…i + ‹…‹…g + ‹…‹…h - ‹…‹…h - ‹…‹…i - ‹…‹…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       6  
âŽ4 + 4â‹…x  + 9â‹…x  + 4â‹…x  + x ⎠⋅âŽ4 - 8â‹…x - 20â‹…x  - 8â‹…x  - 2â‹…x  + 16â‹…x  + 10â‹…x  +

    7    8⎞
 4⋅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]: 
⎧             ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                  ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽      
⎪            ╱                  2                  ╱                  2       
⎨          ╲╱  196 + 32⋅x + 16⋅x                 ╲╱  196 + 32⋅x + 16⋅x        
⎪7 + 2⋅x + ───────────────────────: 1, 7 + 2⋅x - ───────────────────────: 1, 0
⎩                     2                                     2                 

   ⎫
   ⎪
   ⎬
2⎪
⎭

category:documentation

Clone this wiki locally