Chapter 3: Symbolic computing

Robert Johansson

Source code listings for Numerical Python - A Practical Techniques Approach for Industry (ISBN 978-1-484205-54-9).

The source code listings can be downloaded from http://www.apress.com/9781484205549

In [1]:
import sympy
In [2]:
sympy.init_printing()
In [3]:
from sympy import I, pi, oo
In [4]:
x = sympy.Symbol("x")
In [5]:
y = sympy.Symbol("y", real=True)
In [6]:
y.is_real
Out[6]:
True
In [7]:
x.is_real is None
Out[7]:
True
In [8]:
sympy.Symbol("z", imaginary=True).is_real
Out[8]:
False
In [9]:
x = sympy.Symbol("x")
In [10]:
y = sympy.Symbol("y", positive=True)
In [11]:
sympy.sqrt(x ** 2)
Out[11]:
$$\sqrt{x^{2}}$$
In [12]:
sympy.sqrt(y ** 2)
Out[12]:
$$y$$
In [13]:
n1, n2, n3 = sympy.Symbol("n"), sympy.Symbol("n", integer=True), sympy.Symbol("n", odd=True)
In [14]:
sympy.cos(n1 * pi)
Out[14]:
$$\cos{\left (\pi n \right )}$$
In [15]:
sympy.cos(n2 * pi)
Out[15]:
$$\left(-1\right)^{n}$$
In [16]:
sympy.cos(n3 * pi)
Out[16]:
$$-1$$
In [17]:
a, b, c = sympy.symbols("a, b, c", negative=True)
In [18]:
d, e, f = sympy.symbols("d, e, f", positive=True)

Numbers

In [19]:
i = sympy.Integer(19)
In [20]:
"i = {} [type {}]".format(i, type(i))
Out[20]:
"i = 19 [type <class 'sympy.core.numbers.Integer'>]"
In [21]:
i.is_Integer, i.is_real, i.is_odd
Out[21]:
(True, True, True)
In [22]:
f = sympy.Float(2.3)
In [23]:
"f = {} [type {}]".format(f, type(f))
Out[23]:
"f = 2.30000000000000 [type <class 'sympy.core.numbers.Float'>]"
In [24]:
f.is_Integer, f.is_real, f.is_odd
Out[24]:
(False, True, False)
In [25]:
i, f = sympy.sympify(19), sympy.sympify(2.3)
In [26]:
type(i)
Out[26]:
sympy.core.numbers.Integer
In [27]:
type(f)
Out[27]:
sympy.core.numbers.Float
In [28]:
n = sympy.Symbol("n", integer=True)
In [29]:
n.is_integer, n.is_Integer, n.is_positive, n.is_Symbol
Out[29]:
(True, False, None, True)
In [30]:
i = sympy.Integer(19)
In [31]:
i.is_integer, i.is_Integer, i.is_positive, i.is_Symbol
Out[31]:
(True, True, True, False)
In [32]:
i ** 50
Out[32]:
$$8663234049605954426644038200675212212900743262211018069459689001$$
In [33]:
sympy.factorial(100)
Out[33]:
$$93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000$$
In [34]:
"%.25f" % 0.3  # create a string represention with 25 decimals
Out[34]:
'0.2999999999999999888977698'
In [35]:
sympy.Float(0.3, 25)
Out[35]:
$$0.2999999999999999888977698$$
In [36]:
sympy.Float('0.3', 25)
Out[36]:
$$0.3$$

Rationals

In [37]:
sympy.Rational(11, 13)
Out[37]:
$$\frac{11}{13}$$
In [38]:
r1 = sympy.Rational(2, 3)
In [39]:
r2 = sympy.Rational(4, 5)
In [40]:
r1 * r2
Out[40]:
$$\frac{8}{15}$$
In [41]:
r1 / r2
Out[41]:
$$\frac{5}{6}$$

Functions

In [42]:
x, y, z = sympy.symbols("x, y, z")
In [43]:
f = sympy.Function("f")
In [44]:
type(f)
Out[44]:
sympy.core.function.UndefinedFunction
In [45]:
f(x)
Out[45]:
$$f{\left (x \right )}$$
In [46]:
g = sympy.Function("g")(x, y, z)
In [47]:
g
Out[47]:
$$g{\left (x,y,z \right )}$$
In [48]:
g.free_symbols
Out[48]:
$$\left\{x, y, z\right\}$$
In [49]:
sympy.sin
Out[49]:
sin
In [50]:
sympy.sin(x)
Out[50]:
$$\sin{\left (x \right )}$$
In [51]:
sympy.sin(pi * 1.5)
Out[51]:
$$-1$$
In [52]:
n = sympy.Symbol("n", integer=True)
In [53]:
sympy.sin(pi * n)
Out[53]:
$$0$$
In [54]:
h = sympy.Lambda(x, x**2)
In [55]:
h
Out[55]:
$$\left( x \mapsto x^{2} \right)$$
In [56]:
h(5)
Out[56]:
$$25$$
In [57]:
h(1+x)
Out[57]:
$$\left(x + 1\right)^{2}$$

Expressions

In [58]:
x = sympy.Symbol("x")
In [59]:
e = 1 + 2 * x**2 + 3 * x**3
In [60]:
e
Out[60]:
$$3 x^{3} + 2 x^{2} + 1$$
In [61]:
e.args
Out[61]:
$$\left ( 1, \quad 2 x^{2}, \quad 3 x^{3}\right )$$
In [62]:
e.args[1]
Out[62]:
$$2 x^{2}$$
In [63]:
e.args[1].args[1]
Out[63]:
$$x^{2}$$
In [64]:
e.args[1].args[1].args[0]
Out[64]:
$$x$$
In [65]:
e.args[1].args[1].args[0].args
Out[65]:
$$\left ( \right )$$

Simplification

In [66]:
expr = 2 * (x**2 - x) - x * (x + 1)
In [67]:
expr
Out[67]:
$$2 x^{2} - x \left(x + 1\right) - 2 x$$
In [68]:
sympy.simplify(expr)
Out[68]:
$$x \left(x - 3\right)$$
In [69]:
expr.simplify()
Out[69]:
$$x \left(x - 3\right)$$
In [70]:
expr
Out[70]:
$$2 x^{2} - x \left(x + 1\right) - 2 x$$
In [71]:
expr = 2 * sympy.cos(x) * sympy.sin(x)
In [72]:
expr
Out[72]:
$$2 \sin{\left (x \right )} \cos{\left (x \right )}$$
In [73]:
sympy.trigsimp(expr)
Out[73]:
$$\sin{\left (2 x \right )}$$
In [74]:
expr = sympy.exp(x) * sympy.exp(y)
In [75]:
expr
Out[75]:
$$e^{x} e^{y}$$
In [76]:
sympy.powsimp(expr)
Out[76]:
$$e^{x + y}$$

Expand

In [77]:
expr = (x + 1) * (x + 2)
In [78]:
sympy.expand(expr)
Out[78]:
$$x^{2} + 3 x + 2$$
In [79]:
sympy.sin(x + y).expand(trig=True)
Out[79]:
$$\sin{\left (x \right )} \cos{\left (y \right )} + \sin{\left (y \right )} \cos{\left (x \right )}$$
In [80]:
a, b = sympy.symbols("a, b", positive=True)
In [81]:
sympy.log(a * b).expand(log=True)
Out[81]:
$$\log{\left (a \right )} + \log{\left (b \right )}$$
In [82]:
sympy.exp(I*a + b).expand(complex=True)
Out[82]:
$$i e^{b} \sin{\left (a \right )} + e^{b} \cos{\left (a \right )}$$
In [83]:
sympy.expand((a * b)**x, power_exp=True)
Out[83]:
$$a^{x} b^{x}$$
In [84]:
sympy.exp(I*(a-b)*x).expand(power_exp=True)
Out[84]:
$$e^{i a x} e^{- i b x}$$

Factor

In [85]:
sympy.factor(x**2 - 1)
Out[85]:
$$\left(x - 1\right) \left(x + 1\right)$$
In [86]:
sympy.factor(x * sympy.cos(y) + sympy.sin(z) * x)
Out[86]:
$$x \left(\sin{\left (z \right )} + \cos{\left (y \right )}\right)$$
In [87]:
sympy.logcombine(sympy.log(a) - sympy.log(b))
Out[87]:
$$\log{\left (\frac{a}{b} \right )}$$
In [88]:
expr = x + y + x * y * z
In [89]:
expr.factor()
Out[89]:
$$x y z + x + y$$
In [90]:
expr.collect(x)
Out[90]:
$$x \left(y z + 1\right) + y$$
In [91]:
expr.collect(y)
Out[91]:
$$x + y \left(x z + 1\right)$$
In [92]:
expr = sympy.cos(x + y) + sympy.sin(x - y)
In [93]:
expr.expand(trig=True).collect([sympy.cos(x), sympy.sin(x)]).collect(sympy.cos(y) - sympy.sin(y))
Out[93]:
$$\left(\sin{\left (x \right )} + \cos{\left (x \right )}\right) \left(- \sin{\left (y \right )} + \cos{\left (y \right )}\right)$$

Together, apart, cancel

In [94]:
sympy.apart(1/(x**2 + 3*x + 2), x)
Out[94]:
$$- \frac{1}{x + 2} + \frac{1}{x + 1}$$
In [95]:
sympy.together(1 / (y * x + y) + 1 / (1+x))
Out[95]:
$$\frac{y + 1}{y \left(x + 1\right)}$$
In [96]:
sympy.cancel(y / (y * x + y))
Out[96]:
$$\frac{1}{x + 1}$$

Substitutions

In [97]:
(x + y).subs(x, y)
Out[97]:
$$2 y$$
In [98]:
sympy.sin(x * sympy.exp(x)).subs(x, y)
Out[98]:
$$\sin{\left (y e^{y} \right )}$$
In [99]:
sympy.sin(x * z).subs({z: sympy.exp(y), x: y, sympy.sin: sympy.cos})
Out[99]:
$$\cos{\left (y e^{y} \right )}$$
In [100]:
expr = x * y + z**2 *x
In [101]:
values = {x: 1.25, y: 0.4, z: 3.2}
In [102]:
expr.subs(values)
Out[102]:
$$13.3$$

Numerical evaluation

In [103]:
sympy.N(1 + pi)
Out[103]:
$$4.14159265358979$$
In [104]:
sympy.N(pi, 50)
Out[104]:
$$3.1415926535897932384626433832795028841971693993751$$
In [105]:
(x + 1/pi).evalf(7)
Out[105]:
$$x + 0.3183099$$
In [106]:
expr = sympy.sin(pi * x * sympy.exp(x))
In [107]:
[expr.subs(x, xx).evalf(3) for xx in range(0, 10)]
Out[107]:
$$\left [ 0, \quad 0.774, \quad 0.642, \quad 0.722, \quad 0.944, \quad 0.205, \quad 0.974, \quad 0.977, \quad -0.87, \quad -0.695\right ]$$
In [108]:
expr_func = sympy.lambdify(x, expr)
In [109]:
expr_func(1.0)
Out[109]:
$$0.773942685266709$$
In [110]:
expr_func = sympy.lambdify(x, expr, 'numpy')
In [111]:
import numpy as np
In [112]:
xvalues = np.arange(0, 10)
In [113]:
expr_func(xvalues)
Out[113]:
array([ 0.        ,  0.77394269,  0.64198244,  0.72163867,  0.94361635,
        0.20523391,  0.97398794,  0.97734066, -0.87034418, -0.69512687])

Calculus

In [114]:
f = sympy.Function('f')(x)
In [115]:
sympy.diff(f, x)
Out[115]:
$$\frac{d}{d x} f{\left (x \right )}$$
In [116]:
sympy.diff(f, x, x)
Out[116]:
$$\frac{d^{2}}{d x^{2}} f{\left (x \right )}$$
In [117]:
sympy.diff(f, x, 3)
Out[117]:
$$\frac{d^{3}}{d x^{3}} f{\left (x \right )}$$
In [118]:
g = sympy.Function('g')(x, y)
In [119]:
g.diff(x, y)
Out[119]:
$$\frac{\partial^{2}}{\partial x\partial y} g{\left (x,y \right )}$$
In [120]:
g.diff(x, 3, y, 2)         # equivalent to s.diff(g, x, x, x, y, y)
Out[120]:
$$\frac{\partial^{5}}{\partial x^{3}\partial y^{2}} g{\left (x,y \right )}$$
In [121]:
expr = x**4 + x**3 + x**2 + x + 1
In [122]:
expr.diff(x)
Out[122]:
$$4 x^{3} + 3 x^{2} + 2 x + 1$$
In [123]:
expr.diff(x, x)
Out[123]:
$$2 \left(6 x^{2} + 3 x + 1\right)$$
In [124]:
expr = (x + 1)**3 * y ** 2 * (z - 1)
In [125]:
expr.diff(x, y, z)
Out[125]:
$$6 y \left(x + 1\right)^{2}$$
In [126]:
expr = sympy.sin(x * y) * sympy.cos(x / 2)
In [127]:
expr.diff(x)
Out[127]:
$$y \cos{\left (\frac{x}{2} \right )} \cos{\left (x y \right )} - \frac{1}{2} \sin{\left (\frac{x}{2} \right )} \sin{\left (x y \right )}$$
In [128]:
expr = sympy.special.polynomials.hermite(x, 0)
In [129]:
expr.diff(x).doit()
Out[129]:
$$\frac{2^{x} \sqrt{\pi} \operatorname{polygamma}{\left (0,- \frac{x}{2} + \frac{1}{2} \right )}}{2 \Gamma{\left(- \frac{x}{2} + \frac{1}{2} \right)}} + \frac{2^{x} \sqrt{\pi} \log{\left (2 \right )}}{\Gamma{\left(- \frac{x}{2} + \frac{1}{2} \right)}}$$
In [130]:
d = sympy.Derivative(sympy.exp(sympy.cos(x)), x)
In [131]:
d
Out[131]:
$$\frac{d}{d x} e^{\cos{\left (x \right )}}$$
In [132]:
d.doit()
Out[132]:
$$- e^{\cos{\left (x \right )}} \sin{\left (x \right )}$$

Integrals

In [133]:
a, b = sympy.symbols("a, b")
x, y = sympy.symbols('x, y')
f = sympy.Function('f')(x)
In [134]:
sympy.integrate(f)
Out[134]:
$$\int f{\left (x \right )}\, dx$$
In [135]:
sympy.integrate(f, (x, a, b))
Out[135]:
$$\int_{a}^{b} f{\left (x \right )}\, dx$$
In [136]:
sympy.integrate(sympy.sin(x))
Out[136]:
$$- \cos{\left (x \right )}$$
In [137]:
sympy.integrate(sympy.sin(x), (x, a, b))
Out[137]:
$$\cos{\left (a \right )} - \cos{\left (b \right )}$$
In [138]:
sympy.integrate(sympy.exp(-x**2), (x, 0, oo))
Out[138]:
$$\frac{\sqrt{\pi}}{2}$$
In [139]:
a, b, c = sympy.symbols("a, b, c", positive=True)
In [140]:
sympy.integrate(a * sympy.exp(-((x-b)/c)**2), (x, -oo, oo))
Out[140]:
$$\sqrt{\pi} a c$$
In [141]:
sympy.integrate(sympy.sin(x * sympy.cos(x)))
Out[141]:
$$\int \sin{\left (x \cos{\left (x \right )} \right )}\, dx$$
In [142]:
expr = sympy.sin(x*sympy.exp(y))
In [143]:
sympy.integrate(expr, x)
Out[143]:
$$- e^{- y} \cos{\left (x e^{y} \right )}$$
In [144]:
expr = (x + y)**2
In [145]:
sympy.integrate(expr, x)
Out[145]:
$$\frac{x^{3}}{3} + x^{2} y + x y^{2}$$
In [146]:
sympy.integrate(expr, x, y)
Out[146]:
$$\frac{x^{3} y}{3} + \frac{x^{2} y^{2}}{2} + \frac{x y^{3}}{3}$$
In [147]:
sympy.integrate(expr, (x, 0, 1), (y, 0, 1))
Out[147]:
$$\frac{7}{6}$$

Series

In [148]:
x = sympy.Symbol("x")
In [149]:
f = sympy.Function("f")(x)
In [150]:
sympy.series(f, x)
Out[150]:
$$f{\left (0 \right )} + x \left. \frac{d}{d x} f{\left (x \right )} \right|_{\substack{ x=0 }} + \frac{x^{2}}{2} \left. \frac{d^{2}}{d x^{2}} f{\left (x \right )} \right|_{\substack{ x=0 }} + \frac{x^{3}}{6} \left. \frac{d^{3}}{d x^{3}} f{\left (x \right )} \right|_{\substack{ x=0 }} + \frac{x^{4}}{24} \left. \frac{d^{4}}{d x^{4}} f{\left (x \right )} \right|_{\substack{ x=0 }} + \frac{x^{5}}{120} \left. \frac{d^{5}}{d x^{5}} f{\left (x \right )} \right|_{\substack{ x=0 }} + \mathcal{O}\left(x^{6}\right)$$
In [151]:
x0 = sympy.Symbol("{x_0}")
In [152]:
f.series(x, x0, n=2)
Out[152]:
$$f{\left ({x_{0}} \right )} + \left(x - {x_{0}}\right) \left. \frac{d}{d \xi_{1}} f{\left (\xi_{1} \right )} \right|_{\substack{ \xi_{1}={x_{0}} }} + \mathcal{O}\left(\left(x - {x_{0}}\right)^{2}; x\rightarrow{x_{0}}\right)$$
In [153]:
f.series(x, x0, n=2).removeO()
Out[153]:
$$\left(x - {x_{0}}\right) \left. \frac{d}{d \xi_{1}} f{\left (\xi_{1} \right )} \right|_{\substack{ \xi_{1}={x_{0}} }} + f{\left ({x_{0}} \right )}$$
In [154]:
sympy.cos(x).series()
Out[154]:
$$1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} + \mathcal{O}\left(x^{6}\right)$$
In [155]:
sympy.sin(x).series()
Out[155]:
$$x - \frac{x^{3}}{6} + \frac{x^{5}}{120} + \mathcal{O}\left(x^{6}\right)$$
In [156]:
sympy.exp(x).series()
Out[156]:
$$1 + x + \frac{x^{2}}{2} + \frac{x^{3}}{6} + \frac{x^{4}}{24} + \frac{x^{5}}{120} + \mathcal{O}\left(x^{6}\right)$$
In [157]:
(1/(1+x)).series()
Out[157]:
$$1 - x + x^{2} - x^{3} + x^{4} - x^{5} + \mathcal{O}\left(x^{6}\right)$$
In [158]:
expr = sympy.cos(x) / (1 + sympy.sin(x * y))
In [159]:
expr.series(x, n=4)
Out[159]:
$$1 - x y + x^{2} \left(y^{2} - \frac{1}{2}\right) + x^{3} \left(- \frac{5 y^{3}}{6} + \frac{y}{2}\right) + \mathcal{O}\left(x^{4}\right)$$
In [160]:
expr.series(y, n=4)
Out[160]:
$$\cos{\left (x \right )} - x y \cos{\left (x \right )} + x^{2} y^{2} \cos{\left (x \right )} - \frac{5 x^{3}}{6} y^{3} \cos{\left (x \right )} + \mathcal{O}\left(y^{4}\right)$$
In [161]:
expr.series(y).removeO().series(x).removeO().expand()
Out[161]:
$$- \frac{61 x^{5}}{120} y^{5} + \frac{5 x^{5}}{12} y^{3} - \frac{x^{5} y}{24} + \frac{2 x^{4}}{3} y^{4} - \frac{x^{4} y^{2}}{2} + \frac{x^{4}}{24} - \frac{5 x^{3}}{6} y^{3} + \frac{x^{3} y}{2} + x^{2} y^{2} - \frac{x^{2}}{2} - x y + 1$$

Limits

In [162]:
sympy.limit(sympy.sin(x) / x, x, 0)
Out[162]:
$$1$$
In [163]:
f = sympy.Function('f')
x, h = sympy.symbols("x, h")
In [164]:
diff_limit = (f(x + h) - f(x))/h
In [165]:
sympy.limit(diff_limit.subs(f, sympy.cos), h, 0)
Out[165]:
$$- \sin{\left (x \right )}$$
In [166]:
sympy.limit(diff_limit.subs(f, sympy.sin), h, 0)
Out[166]:
$$\cos{\left (x \right )}$$
In [167]:
expr = (x**2 - 3*x) / (2*x - 2)
In [168]:
p = sympy.limit(expr/x, x, oo)
In [169]:
q = sympy.limit(expr - p*x, x, oo)
In [170]:
p, q
Out[170]:
$$\left ( \frac{1}{2}, \quad -1\right )$$

Sums and products

In [171]:
n = sympy.symbols("n", integer=True)
In [172]:
x = sympy.Sum(1/(n**2), (n, 1, oo))
In [173]:
x
Out[173]:
$$\sum_{n=1}^{\infty} \frac{1}{n^{2}}$$
In [174]:
x.doit()
Out[174]:
$$\frac{\pi^{2}}{6}$$
In [175]:
x = sympy.Product(n, (n, 1, 7))
In [176]:
x
Out[176]:
$$\prod_{n=1}^{7} n$$
In [177]:
x.doit()
Out[177]:
$$5040$$
In [178]:
x = sympy.Symbol("x")
In [179]:
sympy.Sum((x)**n/(sympy.factorial(n)), (n, 1, oo)).doit().simplify()
Out[179]:
$$e^{x} - 1$$

Equations

In [180]:
x = sympy.symbols("x")
In [181]:
sympy.solve(x**2 + 2*x - 3)
Out[181]:
$$\left [ -3, \quad 1\right ]$$
In [182]:
a, b, c = sympy.symbols("a, b, c")
In [183]:
sympy.solve(a * x**2 + b * x + c, x)
Out[183]:
$$\left [ \frac{1}{2 a} \left(- b + \sqrt{- 4 a c + b^{2}}\right), \quad - \frac{1}{2 a} \left(b + \sqrt{- 4 a c + b^{2}}\right)\right ]$$
In [184]:
sympy.solve(sympy.sin(x) - sympy.cos(x), x)
Out[184]:
$$\left [ - \frac{3 \pi}{4}, \quad \frac{\pi}{4}\right ]$$
In [185]:
sympy.solve(sympy.exp(x) + 2 * x, x)
Out[185]:
$$\left [ - \operatorname{LambertW}{\left (\frac{1}{2} \right )}\right ]$$
In [186]:
sympy.solve(x**5 - x**2 + 1, x)
Out[186]:
$$\left [ \operatorname{RootOf} {\left(x^{5} - x^{2} + 1, 0\right)}, \quad \operatorname{RootOf} {\left(x^{5} - x^{2} + 1, 1\right)}, \quad \operatorname{RootOf} {\left(x^{5} - x^{2} + 1, 2\right)}, \quad \operatorname{RootOf} {\left(x^{5} - x^{2} + 1, 3\right)}, \quad \operatorname{RootOf} {\left(x^{5} - x^{2} + 1, 4\right)}\right ]$$
In [187]:
1 #s.solve(s.tan(x) - x, x)
Out[187]:
$$1$$
In [188]:
eq1 = x + 2 * y - 1
eq2 = x - y + 1
In [189]:
sympy.solve([eq1, eq2], [x, y], dict=True)
Out[189]:
$$\left [ \left \{ x : - \frac{1}{3}, \quad y : \frac{2}{3}\right \}\right ]$$
In [190]:
eq1 = x**2 - y
eq2 = y**2 - x
In [191]:
sols = sympy.solve([eq1, eq2], [x, y], dict=True)
In [192]:
sols
Out[192]:
$$\left [ \left \{ x : 0, \quad y : 0\right \}, \quad \left \{ x : 1, \quad y : 1\right \}, \quad \left \{ x : - \frac{1}{2} + \frac{\sqrt{3} i}{2}, \quad y : - \frac{1}{2} - \frac{\sqrt{3} i}{2}\right \}, \quad \left \{ x : \frac{1}{4} \left(1 - \sqrt{3} i\right)^{2}, \quad y : - \frac{1}{2} + \frac{\sqrt{3} i}{2}\right \}\right ]$$
In [193]:
[eq1.subs(sol).simplify() == 0 and eq2.subs(sol).simplify() == 0 for sol in sols]
Out[193]:
[True, True, True, True]

Linear algebra

In [194]:
sympy.Matrix([1,2])
Out[194]:
$$\left[\begin{matrix}1\\2\end{matrix}\right]$$
In [195]:
sympy.Matrix([[1,2]])
Out[195]:
$$\left[\begin{matrix}1 & 2\end{matrix}\right]$$
In [196]:
sympy.Matrix([[1, 2], [3, 4]])
Out[196]:
$$\left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]$$
In [197]:
sympy.Matrix(3, 4, lambda m,n: 10 * m + n)
Out[197]:
$$\left[\begin{matrix}0 & 1 & 2 & 3\\10 & 11 & 12 & 13\\20 & 21 & 22 & 23\end{matrix}\right]$$
In [198]:
a, b, c, d = sympy.symbols("a, b, c, d")
In [199]:
M = sympy.Matrix([[a, b], [c, d]])
In [200]:
M
Out[200]:
$$\left[\begin{matrix}a & b\\c & d\end{matrix}\right]$$
In [201]:
M * M
Out[201]:
$$\left[\begin{matrix}a^{2} + b c & a b + b d\\a c + c d & b c + d^{2}\end{matrix}\right]$$
In [202]:
x = sympy.Matrix(sympy.symbols("x_1, x_2"))
In [203]:
M * x
Out[203]:
$$\left[\begin{matrix}a x_{1} + b x_{2}\\c x_{1} + d x_{2}\end{matrix}\right]$$
In [204]:
p, q = sympy.symbols("p, q")
In [205]:
M = sympy.Matrix([[1, p], [q, 1]])
In [206]:
M
Out[206]:
$$\left[\begin{matrix}1 & p\\q & 1\end{matrix}\right]$$
In [207]:
b = sympy.Matrix(sympy.symbols("b_1, b_2"))
In [208]:
b
Out[208]:
$$\left[\begin{matrix}b_{1}\\b_{2}\end{matrix}\right]$$
In [209]:
x = M.solve(b)
x
Out[209]:
$$\left[\begin{matrix}b_{1} \left(\frac{p q}{- p q + 1} + 1\right) - \frac{b_{2} p}{- p q + 1}\\- \frac{b_{1} q}{- p q + 1} + \frac{b_{2}}{- p q + 1}\end{matrix}\right]$$
In [210]:
x = M.LUsolve(b)
In [211]:
x
Out[211]:
$$\left[\begin{matrix}b_{1} - \frac{p \left(- b_{1} q + b_{2}\right)}{- p q + 1}\\\frac{- b_{1} q + b_{2}}{- p q + 1}\end{matrix}\right]$$
In [212]:
x = M.inv() * b
In [213]:
x
Out[213]:
$$\left[\begin{matrix}b_{1} \left(\frac{p q}{- p q + 1} + 1\right) - \frac{b_{2} p}{- p q + 1}\\- \frac{b_{1} q}{- p q + 1} + \frac{b_{2}}{- p q + 1}\end{matrix}\right]$$

Versions

In [1]:
%reload_ext version_information
%version_information sympy, numpy
Out[1]:
SoftwareVersion
Python3.4.3 64bit [GCC 4.2.1 (Apple Inc. build 5577)]
IPython3.2.0
OSDarwin 14.5.0 x86_64 i386 64bit
sympy0.7.6
numpy1.9.2