ch03-code-listing

Chapter 3: Symbolic computing

Robert Johansson

Source code listings for Numerical Python - Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib (ISBN 978-1-484242-45-2).

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]:
$\displaystyle \sqrt{x^{2}}$
In [12]:
sympy.sqrt(y ** 2)
Out[12]:
$\displaystyle 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]:
$\displaystyle \cos{\left(\pi n \right)}$
In [15]:
sympy.cos(n2 * pi)
Out[15]:
$\displaystyle \left(-1\right)^{n}$
In [16]:
sympy.cos(n3 * pi)
Out[16]:
$\displaystyle -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]:
$\displaystyle 8663234049605954426644038200675212212900743262211018069459689001$
In [33]:
sympy.factorial(100)
Out[33]:
$\displaystyle 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]:
$\displaystyle 0.2999999999999999888977698$
In [36]:
sympy.Float('0.3', 25)
Out[36]:
$\displaystyle 0.3$

Rationals

In [37]:
sympy.Rational(11, 13)
Out[37]:
$\displaystyle \frac{11}{13}$
In [38]:
r1 = sympy.Rational(2, 3)
In [39]:
r2 = sympy.Rational(4, 5)
In [40]:
r1 * r2
Out[40]:
$\displaystyle \frac{8}{15}$
In [41]:
r1 / r2
Out[41]:
$\displaystyle \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]:
$\displaystyle f{\left(x \right)}$
In [46]:
g = sympy.Function("g")(x, y, z)
In [47]:
g
Out[47]:
$\displaystyle g{\left(x,y,z \right)}$
In [48]:
g.free_symbols
Out[48]:
$\displaystyle \left\{x, y, z\right\}$
In [49]:
sympy.sin
Out[49]:
sin
In [50]:
sympy.sin(x)
Out[50]:
$\displaystyle \sin{\left(x \right)}$
In [51]:
sympy.sin(pi * 1.5)
Out[51]:
$\displaystyle -1$
In [52]:
n = sympy.Symbol("n", integer=True)
In [53]:
sympy.sin(pi * n)
Out[53]:
$\displaystyle 0$
In [54]:
h = sympy.Lambda(x, x**2)
In [55]:
h
Out[55]:
$\displaystyle \left( x \mapsto x^{2} \right)$
In [56]:
h(5)
Out[56]:
$\displaystyle 25$
In [57]:
h(1+x)
Out[57]:
$\displaystyle \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]:
$\displaystyle 3 x^{3} + 2 x^{2} + 1$
In [61]:
e.args
Out[61]:
$\displaystyle \left( 1, \ 2 x^{2}, \ 3 x^{3}\right)$
In [62]:
e.args[1]
Out[62]:
$\displaystyle 2 x^{2}$
In [63]:
e.args[1].args[1]
Out[63]:
$\displaystyle x^{2}$
In [64]:
e.args[1].args[1].args[0]
Out[64]:
$\displaystyle x$
In [65]:
e.args[1].args[1].args[0].args
Out[65]:
$\displaystyle \left( \right)$

Simplification

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

Expand

In [77]:
expr = (x + 1) * (x + 2)
In [78]:
sympy.expand(expr)
Out[78]:
$\displaystyle x^{2} + 3 x + 2$
In [79]:
sympy.sin(x + y).expand(trig=True)
Out[79]:
$\displaystyle \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]:
$\displaystyle \log{\left(a \right)} + \log{\left(b \right)}$
In [82]:
sympy.exp(I*a + b).expand(complex=True)
Out[82]:
$\displaystyle 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]:
$\displaystyle a^{x} b^{x}$
In [84]:
sympy.exp(I*(a-b)*x).expand(power_exp=True)
Out[84]:
$\displaystyle e^{i a x} e^{- i b x}$

Factor

In [85]:
sympy.factor(x**2 - 1)
Out[85]:
$\displaystyle \left(x - 1\right) \left(x + 1\right)$
In [86]:
sympy.factor(x * sympy.cos(y) + sympy.sin(z) * x)
Out[86]:
$\displaystyle x \left(\sin{\left(z \right)} + \cos{\left(y \right)}\right)$
In [87]:
sympy.logcombine(sympy.log(a) - sympy.log(b))
Out[87]:
$\displaystyle \log{\left(\frac{a}{b} \right)}$
In [88]:
expr = x + y + x * y * z
In [89]:
expr.factor()
Out[89]:
$\displaystyle x y z + x + y$
In [90]:
expr.collect(x)
Out[90]:
$\displaystyle x \left(y z + 1\right) + y$
In [91]:
expr.collect(y)
Out[91]:
$\displaystyle 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]:
$\displaystyle \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]:
$\displaystyle - \frac{1}{x + 2} + \frac{1}{x + 1}$
In [95]:
sympy.together(1 / (y * x + y) + 1 / (1+x))
Out[95]:
$\displaystyle \frac{y + 1}{y \left(x + 1\right)}$
In [96]:
sympy.cancel(y / (y * x + y))
Out[96]:
$\displaystyle \frac{1}{x + 1}$

Substitutions

In [97]:
(x + y).subs(x, y)
Out[97]:
$\displaystyle 2 y$
In [98]:
sympy.sin(x * sympy.exp(x)).subs(x, y)
Out[98]:
$\displaystyle \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]:
$\displaystyle \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]:
$\displaystyle 13.3$

Numerical evaluation

In [103]:
sympy.N(1 + pi)
Out[103]:
$\displaystyle 4.14159265358979$
In [104]:
sympy.N(pi, 50)
Out[104]:
$\displaystyle 3.1415926535897932384626433832795028841971693993751$
In [105]:
(x + 1/pi).evalf(7)
Out[105]:
$\displaystyle 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]:
$\displaystyle \left[ 0, \ 0.774, \ 0.642, \ 0.722, \ 0.944, \ 0.205, \ 0.974, \ 0.977, \ -0.87, \ -0.695\right]$
In [108]:
expr_func = sympy.lambdify(x, expr)
In [109]:
expr_func(1.0)
Out[109]:
$\displaystyle 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]:
$\displaystyle \frac{d}{d x} f{\left(x \right)}$
In [116]:
sympy.diff(f, x, x)
Out[116]:
$\displaystyle \frac{d^{2}}{d x^{2}} f{\left(x \right)}$
In [117]:
sympy.diff(f, x, 3)
Out[117]:
$\displaystyle \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]:
$\displaystyle \frac{\partial^{2}}{\partial y\partial x} 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]:
$\displaystyle \frac{\partial^{5}}{\partial y^{2}\partial x^{3}} g{\left(x,y \right)}$
In [121]:
expr = x**4 + x**3 + x**2 + x + 1
In [122]:
expr.diff(x)
Out[122]:
$\displaystyle 4 x^{3} + 3 x^{2} + 2 x + 1$
In [123]:
expr.diff(x, x)
Out[123]:
$\displaystyle 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]:
$\displaystyle 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]:
$\displaystyle y \cos{\left(\frac{x}{2} \right)} \cos{\left(x y \right)} - \frac{\sin{\left(\frac{x}{2} \right)} \sin{\left(x y \right)}}{2}$
In [128]:
expr = sympy.special.polynomials.hermite(x, 0)
In [129]:
expr.diff(x).doit()
Out[129]:
$\displaystyle \frac{2^{x} \sqrt{\pi} \operatorname{polygamma}{\left(0,\frac{1}{2} - \frac{x}{2} \right)}}{2 \Gamma\left(\frac{1}{2} - \frac{x}{2}\right)} + \frac{2^{x} \sqrt{\pi} \log{\left(2 \right)}}{\Gamma\left(\frac{1}{2} - \frac{x}{2}\right)}$
In [130]:
d = sympy.Derivative(sympy.exp(sympy.cos(x)), x)
In [131]:
d
Out[131]:
$\displaystyle \frac{d}{d x} e^{\cos{\left(x \right)}}$
In [132]:
d.doit()
Out[132]:
$\displaystyle - 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]:
$\displaystyle \int f{\left(x \right)}\, dx$
In [135]:
sympy.integrate(f, (x, a, b))
Out[135]:
$\displaystyle \int\limits_{a}^{b} f{\left(x \right)}\, dx$
In [136]:
sympy.integrate(sympy.sin(x))
Out[136]:
$\displaystyle - \cos{\left(x \right)}$
In [137]:
sympy.integrate(sympy.sin(x), (x, a, b))
Out[137]:
$\displaystyle \cos{\left(a \right)} - \cos{\left(b \right)}$
In [138]:
sympy.integrate(sympy.exp(-x**2), (x, 0, oo))
Out[138]:
$\displaystyle \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]:
$\displaystyle \sqrt{\pi} a c$
In [141]:
sympy.integrate(sympy.sin(x * sympy.cos(x)))
Out[141]:
$\displaystyle \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]:
$\displaystyle - e^{- y} \cos{\left(x e^{y} \right)}$
In [144]:
expr = (x + y)**2
In [145]:
sympy.integrate(expr, x)
Out[145]:
$\displaystyle \frac{x^{3}}{3} + x^{2} y + x y^{2}$
In [146]:
sympy.integrate(expr, x, y)
Out[146]:
$\displaystyle \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]:
$\displaystyle \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]:
$\displaystyle f{\left(0 \right)} + x \left. \frac{d}{d x} f{\left(x \right)} \right|_{\substack{ x=0 }} + \frac{x^{2} \left. \frac{d^{2}}{d x^{2}} f{\left(x \right)} \right|_{\substack{ x=0 }}}{2} + \frac{x^{3} \left. \frac{d^{3}}{d x^{3}} f{\left(x \right)} \right|_{\substack{ x=0 }}}{6} + \frac{x^{4} \left. \frac{d^{4}}{d x^{4}} f{\left(x \right)} \right|_{\substack{ x=0 }}}{24} + \frac{x^{5} \left. \frac{d^{5}}{d x^{5}} f{\left(x \right)} \right|_{\substack{ x=0 }}}{120} + O\left(x^{6}\right)$
In [151]:
x0 = sympy.Symbol("{x_0}")
In [152]:
f.series(x, x0, n=2)
Out[152]:
$\displaystyle 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} }} + O\left(\left(x - {x_0}\right)^{2}; x\rightarrow {x_0}\right)$
In [153]:
f.series(x, x0, n=2).removeO()
Out[153]:
$\displaystyle \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]:
$\displaystyle 1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} + O\left(x^{6}\right)$
In [155]:
sympy.sin(x).series()
Out[155]:
$\displaystyle x - \frac{x^{3}}{6} + \frac{x^{5}}{120} + O\left(x^{6}\right)$
In [156]:
sympy.exp(x).series()
Out[156]:
$\displaystyle 1 + x + \frac{x^{2}}{2} + \frac{x^{3}}{6} + \frac{x^{4}}{24} + \frac{x^{5}}{120} + O\left(x^{6}\right)$
In [157]:
(1/(1+x)).series()
Out[157]:
$\displaystyle 1 - x + x^{2} - x^{3} + x^{4} - x^{5} + 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]:
$\displaystyle 1 - x y + x^{2} \left(y^{2} - \frac{1}{2}\right) + x^{3} \left(- \frac{5 y^{3}}{6} + \frac{y}{2}\right) + O\left(x^{4}\right)$
In [160]:
expr.series(y, n=4)
Out[160]:
$\displaystyle \cos{\left(x \right)} - x y \cos{\left(x \right)} + x^{2} y^{2} \cos{\left(x \right)} - \frac{5 x^{3} y^{3} \cos{\left(x \right)}}{6} + O\left(y^{4}\right)$
In [161]:
expr.series(y).removeO().series(x).removeO().expand()
Out[161]:
$\displaystyle - \frac{61 x^{5} y^{5}}{120} + \frac{5 x^{5} y^{3}}{12} - \frac{x^{5} y}{24} + \frac{2 x^{4} y^{4}}{3} - \frac{x^{4} y^{2}}{2} + \frac{x^{4}}{24} - \frac{5 x^{3} y^{3}}{6} + \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]:
$\displaystyle 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]:
$\displaystyle - \sin{\left(x \right)}$
In [166]:
sympy.limit(diff_limit.subs(f, sympy.sin), h, 0)
Out[166]:
$\displaystyle \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]:
$\displaystyle \left( \frac{1}{2}, \ -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]:
$\displaystyle \sum_{n=1}^{\infty} \frac{1}{n^{2}}$
In [174]:
x.doit()
Out[174]:
$\displaystyle \frac{\pi^{2}}{6}$
In [175]:
x = sympy.Product(n, (n, 1, 7))
In [176]:
x
Out[176]:
$\displaystyle \prod_{n=1}^{7} n$
In [177]:
x.doit()
Out[177]:
$\displaystyle 5040$
In [178]:
x = sympy.Symbol("x")
In [179]:
sympy.Sum((x)**n/(sympy.factorial(n)), (n, 1, oo)).doit().simplify()
Out[179]:
$\displaystyle e^{x} - 1$

Equations

In [180]:
x = sympy.symbols("x")
In [181]:
sympy.solve(x**2 + 2*x - 3)
Out[181]:
$\displaystyle \left[ -3, \ 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]:
$\displaystyle \left[ \frac{- b + \sqrt{- 4 a c + b^{2}}}{2 a}, \ - \frac{b + \sqrt{- 4 a c + b^{2}}}{2 a}\right]$
In [184]:
sympy.solve(sympy.sin(x) - sympy.cos(x), x)
Out[184]:
$\displaystyle \left[ - \frac{3 \pi}{4}, \ \frac{\pi}{4}\right]$
In [185]:
sympy.solve(sympy.exp(x) + 2 * x, x)
Out[185]:
$\displaystyle \left[ - \operatorname{LambertW}{\left(\frac{1}{2} \right)}\right]$
In [186]:
sympy.solve(x**5 - x**2 + 1, x)
Out[186]:
$\displaystyle \left[ \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 0\right)}, \ \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 1\right)}, \ \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 2\right)}, \ \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 3\right)}, \ \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 4\right)}\right]$
In [187]:
1 #s.solve(s.tan(x) - x, x)
Out[187]:
$\displaystyle 1$
In [188]:
eq1 = x + 2 * y - 1
eq2 = x - y + 1
In [189]:
sympy.solve([eq1, eq2], [x, y], dict=True)
Out[189]:
$\displaystyle \left[ \left\{ x : - \frac{1}{3}, \ 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]:
$\displaystyle \left[ \left\{ x : 0, \ y : 0\right\}, \ \left\{ x : 1, \ y : 1\right\}, \ \left\{ x : \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right)^{2}, \ y : - \frac{1}{2} - \frac{\sqrt{3} i}{2}\right\}, \ \left\{ x : \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right)^{2}, \ 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]:
$\displaystyle \left[\begin{matrix}1\\2\end{matrix}\right]$
In [195]:
sympy.Matrix([[1,2]])
Out[195]:
$\displaystyle \left[\begin{matrix}1 & 2\end{matrix}\right]$
In [196]:
sympy.Matrix([[1, 2], [3, 4]])
Out[196]:
$\displaystyle \left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]$
In [197]:
sympy.Matrix(3, 4, lambda m,n: 10 * m + n)
Out[197]:
$\displaystyle \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]:
$\displaystyle \left[\begin{matrix}a & b\\c & d\end{matrix}\right]$
In [201]:
M * M
Out[201]:
$\displaystyle \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]:
$\displaystyle \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]:
$\displaystyle \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]:
$\displaystyle \left[\begin{matrix}b_{1}\\b_{2}\end{matrix}\right]$
In [209]:
x = M.solve(b)
x
Out[209]:
$\displaystyle \left[\begin{matrix}\frac{b_{1} \left(- p q + 1\right) - p \left(- b_{1} q + b_{2}\right)}{- p q + 1}\\\frac{- b_{1} q + b_{2}}{- p q + 1}\end{matrix}\right]$
In [210]:
x = M.LUsolve(b)
In [211]:
x
Out[211]:
$\displaystyle \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]:
$\displaystyle \left[\begin{matrix}\frac{b_{1}}{- p q + 1} - \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 [214]:
%reload_ext version_information
%version_information sympy, numpy
Out[214]:
SoftwareVersion
Python3.6.8 64bit [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
IPython7.5.0
OSDarwin 18.2.0 x86_64 i386 64bit
sympy1.4
numpy1.16.3
Mon May 06 14:25:08 2019 JST