Sympy : Symbolic Mathematics in Python

http://www.scipy-lectures.org/packages/sympy.html


In [3]:
from sympy import *
x = Symbol('x')
y = Symbol('y')

Integration


In [4]:
integrate(x**3, (x, -1, 1))


Out[4]:
0

In [5]:
integrate( 1/x )


Out[5]:
log(x)

In [7]:
integrate( 1/(1 +2*x) )


Out[7]:
log(2*x + 1)/2

In [8]:
integrate( 1/(1 - 2*x) )


Out[8]:
-log(2*x - 1)/2

Equation solving


In [9]:
solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y])


Out[9]:
{x: -3, y: 1}

Differential Equations


In [12]:
f, g = symbols('f g', cls=Function)

In [14]:
f(x).diff(x, x) + f(x)


Out[14]:
f(x) + Derivative(f(x), x, x)

In [15]:
dsolve(f(x).diff(x, x) + f(x), f(x))


Out[15]:
Eq(f(x), C1*sin(x) + C2*cos(x))

In [16]:
dsolve(sin(x)*cos(f(x)) + cos(x)*sin(f(x))*f(x).diff(x), f(x), hint='separable')


Out[16]:
[Eq(f(x), -asin(sqrt(C1/(sin(x)**2 - 1) + 1)) + pi),
 Eq(f(x), asin(sqrt(C1/(sin(x)**2 - 1) + 1)) + pi),
 Eq(f(x), -asin(sqrt(C1/(sin(x)**2 - 1) + 1))),
 Eq(f(x), asin(sqrt(C1/(sin(x)**2 - 1) + 1)))]

In [17]:
diff(x, x)


Out[17]:
1

In [19]:
diff(1/x, x)


Out[19]:
-1/x**2

In [21]:
diff(1/(2*y), y)


Out[21]:
-1/(2*y**2)

In [25]:
diff(1/x, x) - diff(1/(2*y), y)


Out[25]:
1/(2*y**2) - 1/x**2

In [28]:
from __future__ import division
from sympy import *
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)

In [30]:
eq = (Eq(Derivative(x(t),t), 12*t*x(t) + 8*y(t)), Eq(Derivative(y(t),t), 21*x(t) + 7*t*y(t)))
eq


Out[30]:
(Eq(Derivative(x(t), t), 12*t*x(t) + 8*y(t)),
 Eq(Derivative(y(t), t), 7*t*y(t) + 21*x(t)))

In [31]:
dsolve(eq)


Out[31]:
[Eq(x(t), C1*x0 + C2*x0*Integral(8*exp(Integral(7*t, t))*exp(Integral(12*t, t))/x0**2, t)),
 Eq(y(t), C1*y0 + C2(y0*Integral(8*exp(Integral(7*t, t))*exp(Integral(12*t, t))/x0**2, t) + exp(Integral(7*t, t))*exp(Integral(12*t, t))/x0))]

In [32]:
eq1 = (Eq(Derivative(x(t),t), 12*t*x(t) + 8*y(t)))
eq1


Out[32]:
Eq(Derivative(x(t), t), 12*t*x(t) + 8*y(t))

In [33]:
dsolve(eq1)


Out[33]:
Eq(-4*Integral(2*y(t)*exp(-6*t**2), t) - 4*Integral(3*t*x(t)*exp(-6*t**2), t), C1)

In [56]:
eq1 = (Eq(Derivative(1/x,x)- Derivative((2*y)**(-1), y)))
eq1


Out[56]:
Eq(Derivative(1/x, x) - Derivative(1/(2*y), y), 0)

In [57]:
dsolve(eq1)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-57-b4355e87dcda> in <module>()
----> 1 dsolve(eq1)

/Users/chengjun/anaconda/lib/python2.7/site-packages/sympy/solvers/ode.py in dsolve(eq, func, hint, simplify, ics, xi, eta, x0, n, **kwargs)
    623         hints = _desolve(eq, func=func,
    624             hint=hint, simplify=True, xi=xi, eta=eta, type='ode', ics=ics,
--> 625             x0=x0, n=n, **kwargs)
    626 
    627         eq = hints.pop('eq', eq)

/Users/chengjun/anaconda/lib/python2.7/site-packages/sympy/solvers/deutils.py in _desolve(eq, func, hint, ics, simplify, **kwargs)
    173     # preprocess the equation and find func if not given
    174     if prep or func is None:
--> 175         eq, func = _preprocess(eq, func)
    176         prep = False
    177 

/Users/chengjun/anaconda/lib/python2.7/site-packages/sympy/solvers/deutils.py in _preprocess(expr, func, hint)
     75         if len(funcs) != 1:
     76             raise ValueError('The function cannot be '
---> 77                 'automatically detected for %s.' % expr)
     78         func = funcs.pop()
     79     fvars = set(func.args)

ValueError: The function cannot be automatically detected for Derivative(1/x, x) - Derivative(1/(2*y), y).

In [ ]: