Skip to content

Latest commit

 

History

History
333 lines (276 loc) · 6.63 KB

sympy.md

File metadata and controls

333 lines (276 loc) · 6.63 KB

Sympy

Install

$ pip3 install sympy --user

Import Library

import sympy as smp
# pretty print
from sympy import init_session
init_session(quiet=True)
# init_session will also init common symbols below
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)

Basic

  • Define symbols
    >>> x, y = smp.symbols( 'x y' )
    >>> x**2
    x**2
    >>> z = x**2 + y
    >>> z
    x**2 + y
  • Substitutes symbols
    >>> z.subs(x, 4)
    y + 16
  • Commonly used mathematical functions
    >>> smp.asin(x)
    asin(x)
    >>> smp.exp(x)
    exp(x)
    >>> smp.log(x)
    log(x)
    >>> smp.log(x, 10) # base 10
    log(x)/log(10)
  • Keep Rational
    >>> x**(3/2)
    x**1.5
    >>> # if you want keep rational
    >>> x**( smp.Rational( 3,2) )
    x**(3/2)

Limit

>>> smp.sin(x/2 + smp.sin(x) )
   ⎛xsin⎜─ + sin(x)⎟
   ⎝2>>> # take the limit as x approach to PI
>>> smp.limit( smp.sin(x/2 + smp.sin(x) ) , x, smp.pi  )
1
# +/- side
>>> z = 2*smp.exp(1/x) / (smp.exp(1/x)+1)
>>> z
2*exp(1/x)/(exp(1/x) + 1)
>>> # take limit as x approach to 0⁺
>>> smp.limit(z, x, 0, dir='+')
2
>>> # from negative side
>>> smp.limit(z, x, 0, dir='-')
0
# x -> infinite
>>> z = (smp.cos(x)-1)/x
>>> z
cos(x) - 1
──────────
    x
>>> smp.limit(z, x, smp.oo)
0

Derivatives

>>> z =  (1 + smp.sin(x))/(1 - smp.cos(x))
>>> z
1 + sin(x)
──────────
1 - cos(x)
>>> z ** 2
            2
(1 + sin(x))
─────────────
            2
(1 - cos(x))
>>> smp.diff( z**2, x)
                                      2
2⋅(sin(x) + 1)⋅cos(x)   2⋅(sin(x) + 1) ⋅sin(x)
───────────────────── - ──────────────────────
                2                       3
    (1 - cos(x))            (1 - cos(x))
>>> z = smp.log(x,5)**(x/2)
>>> z
        x2log(x)⎞
⎜──────⎟
⎝log(5)⎠
>>> smp.diff(z, x)
        x
        ─ ⎛   ⎛log(x)⎞           ⎞
        2log⎜──────⎟           ⎟
⎛log(x)⎞  ⎜   ⎝log(5)⎠      1    ⎟
⎜──────⎟ ⋅⎜─────────── + ────────⎟
⎝log(5)⎠  ⎝     2        2log(x)⎠
# chain rule
>>> f, g = symbols('f g', cls=Function)
>>> g = g(x)
>>> z = f(x+g)
>>> z
z(x + g(x))

>>> smp.diff(z, x)
⎛d           ⎞ ⎛ d        ⎞│
⎜──(g(x)) + 1⎟⋅⎜───(f(ξ₁))⎟│
⎝dx          ⎠ ⎝₁       ⎠│ξ=x + g(x)

Basci Antiderivatives

>>> z = smp.csc(x) * smp.cot(x)
>>> z
cot(x)⋅csc(x)
>>> smp.integrate( z )
 -1   
──────
sin(x)
>>> # note: smp.integrate won't show `+ C`

Initial Value Problems

Given dy/dx =  8⋅x + csc²(x) with y(π/2)=-7, solve for y(x)

>>> integral = smp.integrate( 8*x + smp.csc(x)**2, x )
>>> integral
   2   cos(x)
4⋅x  - ──────
       sin(x)
>>> integral.subs(x, smp.pi/2)
 2
π   # but we want it to be -7.
    # so we add the term C = -π² -7

>>> C = -smp.pi**2 - 7
>>> y = integral + C
>>> y
   2    2       cos(x)
4⋅x  - π  - 7 - ──────
                sin(x)
>>> y.subs(x, smp.pi/2)
-7

Definite Integrals

>>> y = smp.exp(x) / smp.sqrt( smp.exp(2*x)+9)
>>> y
       x
      
─────────────
   __________2x
╲╱      + 9
>>> # integral [0, ln(4)]
>>> smp.integrate( y, (x, 0, smp.log(4) ) ) # use a turple
-asinh(1/3) + asinh(4/3)
>>> y = (x**10) * smp.exp(x)
>>> y
 10  x
x 
>>> # integrate [1,t]
>>> smp.integrate(y, (x,1,t) )
⎛ 10       9       8        7         6          5           4           3            2tt   - 10t  + 90t  - 720t  + 5040t  - 30240t  + 151200t  - 604800t  + 1814400t  - 3628800t + 3628800⎠⋅  

           
- 1334961

Improper Integrals

>>> y = 16*smp.atan(x)/(1+x**2)
>>> y
16atan(x)
──────────
   2      
  x  + 1  
>>> # integrate [0,∞)
>>> smp.integrate(y, (x,0,smp.oo))
   2
2π 

Sequences and Series

solve ∑n=0∞ 6/4ⁿ

>>> smp.Sum(6/4**n, (n,0,smp.oo))
  ∞        
 ___       
 ╲         
  ╲      -n64  
 ╱         
 ‾‾‾       
n = 0   
>>> smp.Sum(6/4**n, (n,0,smp.oo)).doit()
8
>>> smp.Sum( (1+smp.cos(n))/n, (n,1,smp.oo) )
  ∞
 ____
 ╲
  ╲
   ╲  cos(n) + 1
   ╱  ──────────
  ╱       n
 ╱
 ‾‾‾‾
n = 1
>>> smp.Sum( (1+smp.cos(n))/n, (n,1,smp.oo) ).doit()
  ∞
 ____
 ╲
  ╲
   ╲  cos(n) + 1
   ╱  ──────────
  ╱       n
 ╱
 ‾‾‾‾
n = 1   # doit() does not get the answer

doit() will try to find a symbolic way to find the answer, but sometimes you have to use a numerical method, in other words you approximate it , you find many many terms and see what it converges to.

>>> smp.Sum( (1+smp.cos(n))/n, (n,1,smp.oo)).n()
0.e+2
>>> # is this anser correct? No, because this expression actually diverge
>>> smp.Sum( (1+smp.cos(n))/n**2, (n,1,smp.oo)).n()
1.969 # but I know it converges if I change /n to /n²