SymPy ----- SymPy (http://www.sympy.org/) is a Python module for symbolic mathematics. It's similiar to commercial computer algebra systems like Maple or Mathematica. Usage ~~~~~ Web service ^^^^^^^^^^^ SymPy live http://live.sympy.org/ SymPy gamma http://sympygamma.com/ Python Module ^^^^^^^^^^^^^ SymPy can be installed, imported and used like any other regular Python module. Output can be done as nicely formatted LaTeX. This is a great way to get more complicated formulae into your manuscript insted of hassling with nested LaTeX commands. Right click on the outputted formula in any ipython notebook or the above mentioned web services to extract the corresponding LaTeX commands. Basic Examples ~~~~~~~~~~~~~~ .. code:: python %matplotlib inline from sympy import * .. parsed-literal:: Value error parsing header in AFM: b'UnderlinePosition' b'-133\rUnderlineThickness 20\rVersion 7.000' init\_printing() activates nicely formatted LaTeX output within an ipython notebook. .. code:: python init_printing() Symbols ^^^^^^^ Mathematical variables have to be defined as symbols before usage .. code:: python x = Symbol('x') type(x) .. parsed-literal:: sympy.core.symbol.Symbol .. code:: python (pi + x)**2 .. math:: \left(x + \pi\right)^{2} A short form of Symbol is S. Both can be used to define serveral symbols at once. .. code:: python x,y,z = S('x,y,z') .. code:: python x**2+y**2+z**2 .. math:: x^{2} + y^{2} + z^{2} Fractions ^^^^^^^^^ Fractions of nubers must be defined with Rational. Otherwise the numbers will be evaluated as a Python expression resulting in a regular Python floating point number. .. code:: python 4/7 # regular python division .. math:: 0.5714285714285714 .. code:: python r1 = Rational(4,7) # sympy rational r1 .. math:: \frac{4}{7} .. code:: python r2 = x/y # since x and y are already sympy symbols no explicit Rational definition is required r2 .. math:: \frac{x}{y} .. code:: python r1*r2+1 .. math:: \frac{4 x}{7 y} + 1 Complex Numbers ^^^^^^^^^^^^^^^ The imaginary unit is noted as I in sympy. .. code:: python 1+I .. math:: 1 + i .. code:: python I**2 .. math:: -1 .. code:: python simplify((1+I)**2) .. math:: 2 i Numeric evaluation ^^^^^^^^^^^^^^^^^^ .. code:: python r1.evalf(100) .. math:: 0.5714285714285714285714285714285714285714285714285714285714285714285714285714285714285714285714285714 .. code:: python pi.evalf(100) .. math:: 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 Substitution ^^^^^^^^^^^^ .. code:: python f1 = x**2+x**3 subs can be used to evaluate a function at a specific point .. code:: python f1.subs(x, 2) .. math:: 12 or to substitute expressions .. code:: python a=S('a') .. code:: python f1.subs(x, a) .. math:: a^{3} + a^{2} .. code:: python f2 = f1.subs(x, sqrt(a)) f2 .. math:: a^{\frac{3}{2}} + a .. code:: python f2.subs(a, 1) .. math:: 2 Plotting ^^^^^^^^ .. code:: python from sympy.plotting import plot .. code:: python f1 .. math:: x^{3} + x^{2} .. code:: python plot(f1) .. image:: 8_sympy_files/8_sympy_37_0.png .. parsed-literal:: .. code:: python plot(f1, (x,-5,5)) .. image:: 8_sympy_files/8_sympy_38_0.png .. parsed-literal:: .. code:: python f2 .. math:: a^{\frac{3}{2}} + a .. code:: python plot( f2) .. image:: 8_sympy_files/8_sympy_40_0.png .. parsed-literal:: Simplification ^^^^^^^^^^^^^^ .. code:: python x,y=S('x,y') eq = (x**3 + x**2 - x - 1)/(x**2 + 2*x + 1) eq .. math:: \frac{x^{3} + x^{2} - x - 1}{x^{2} + 2 x + 1} .. code:: python simplify(eq) .. math:: x - 1 .. code:: python simplify(sin(x)**2 + cos(x)**2) .. math:: 1 Solving equations ^^^^^^^^^^^^^^^^^ .. code:: python x = S('x') eq = x**2 - 1 eq .. math:: x^{2} - 1 .. code:: python plot(eq, (x, -2, +2)) .. image:: 8_sympy_files/8_sympy_47_0.png .. parsed-literal:: .. code:: python solve(eq, x) .. math:: \left [ -1, \quad 1\right ] .. code:: python eq = 2*x*y + y**2/2 .. code:: python solve(eq, y) .. math:: \left [ 0, \quad - 4 x\right ] Differentiate ^^^^^^^^^^^^^ .. code:: python diff(2*x*y + y**2/2, y) .. math:: 2 x + y Integrate ^^^^^^^^^ .. code:: python integrate(2*x*y + y**2/2, y) .. math:: x y^{2} + \frac{y^{3}}{6} Matrices ^^^^^^^^ unity matrix .. code:: python eye(10) .. math:: \left[\begin{matrix}1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\end{matrix}\right] .. code:: python m11, m12, m21, m22 = symbols("m11, m12, m21, m22") b1, b2 = symbols("b1, b2") .. code:: python A = Matrix([[m11, m12],[m21, m22]]) A .. math:: \left[\begin{matrix}m_{11} & m_{12}\\m_{21} & m_{22}\end{matrix}\right] .. code:: python b = Matrix([[b1], [b2]]) b .. math:: \left[\begin{matrix}b_{1}\\b_{2}\end{matrix}\right] .. code:: python A**2 .. math:: \left[\begin{matrix}m_{11}^{2} + m_{12} m_{21} & m_{11} m_{12} + m_{12} m_{22}\\m_{11} m_{21} + m_{21} m_{22} & m_{12} m_{21} + m_{22}^{2}\end{matrix}\right] .. code:: python A * b .. math:: \left[\begin{matrix}b_{1} m_{11} + b_{2} m_{12}\\b_{1} m_{21} + b_{2} m_{22}\end{matrix}\right] .. code:: python A.det() .. math:: m_{11} m_{22} - m_{12} m_{21} .. code:: python A.inv() .. math:: \left[\begin{matrix}\frac{1}{m_{11}} + \frac{m_{12} m_{21}}{m_{11}^{2} \left(m_{22} - \frac{m_{12} m_{21}}{m_{11}}\right)} & - \frac{m_{12}}{m_{11} \left(m_{22} - \frac{m_{12} m_{21}}{m_{11}}\right)}\\- \frac{m_{21}}{m_{11} \left(m_{22} - \frac{m_{12} m_{21}}{m_{11}}\right)} & \frac{1}{m_{22} - \frac{m_{12} m_{21}}{m_{11}}}\end{matrix}\right] Example: Calculate determinant of matrices in a loop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code:: python from IPython.display import display #b01a01 A = Matrix( [[2,2,-5],[5,4,1],[4,14,3]]) # assign matrix to variable A B = Matrix( [[1,2,3],[0,4,5],[0,0,6]]) # assign matrix to variable B C = Matrix( [[1,2,-1,2],[-3,5,-5,-2],[5,4,3,-4], [-4,-3,5,5]]) # assign matrix to variable C # show the result for each matrix for M in (A, B, C): print( 'The determinant of matrix') display(M) print(' is ') display(M.det()) .. parsed-literal:: The determinant of matrix .. math:: \left[\begin{matrix}2 & 2 & -5\\5 & 4 & 1\\4 & 14 & 3\end{matrix}\right] .. parsed-literal:: is .. math:: -296 .. parsed-literal:: The determinant of matrix .. math:: \left[\begin{matrix}1 & 2 & 3\\0 & 4 & 5\\0 & 0 & 6\end{matrix}\right] .. parsed-literal:: is .. math:: 24 .. parsed-literal:: The determinant of matrix .. math:: \left[\begin{matrix}1 & 2 & -1 & 2\\-3 & 5 & -5 & -2\\5 & 4 & 3 & -4\\-4 & -3 & 5 & 5\end{matrix}\right] .. parsed-literal:: is .. math:: 1050 Example: Calculate Eigenvalues and Eigenvectors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code:: python a,b,c,d = S('a,b,c,d') A = Matrix( [[a,b], [c,d]]) A .. math:: \left[\begin{matrix}a & b\\c & d\end{matrix}\right] .. code:: python A.eigenvals() .. math:: \left \{ \frac{a}{2} + \frac{d}{2} - \frac{1}{2} \sqrt{a^{2} - 2 a d + 4 b c + d^{2}} : 1, \quad \frac{a}{2} + \frac{d}{2} + \frac{1}{2} \sqrt{a^{2} - 2 a d + 4 b c + d^{2}} : 1\right \} .. code:: python A.eigenvects() .. math:: \left [ \left ( \frac{a}{2} + \frac{d}{2} - \frac{1}{2} \sqrt{a^{2} - 2 a d + 4 b c + d^{2}}, \quad 1, \quad \left [ \left[\begin{matrix}- \frac{b}{\frac{a}{2} - \frac{d}{2} + \frac{1}{2} \sqrt{a^{2} - 2 a d + 4 b c + d^{2}}}\\1\end{matrix}\right]\right ]\right ), \quad \left ( \frac{a}{2} + \frac{d}{2} + \frac{1}{2} \sqrt{a^{2} - 2 a d + 4 b c + d^{2}}, \quad 1, \quad \left [ \left[\begin{matrix}- \frac{b}{\frac{a}{2} - \frac{d}{2} - \frac{1}{2} \sqrt{a^{2} - 2 a d + 4 b c + d^{2}}}\\1\end{matrix}\right]\right ]\right )\right ] .. code:: python B = A.subs(((a,1),(d,1))) B .. math:: \left[\begin{matrix}1 & b\\c & 1\end{matrix}\right] .. code:: python B.eigenvals() .. math:: \left \{ - \sqrt{b c} + 1 : 1, \quad \sqrt{b c} + 1 : 1\right \} .. code:: python B.eigenvects() .. math:: \left [ \left ( - \sqrt{b c} + 1, \quad 1, \quad \left [ \left[\begin{matrix}- \frac{b}{\sqrt{b c}}\\1\end{matrix}\right]\right ]\right ), \quad \left ( \sqrt{b c} + 1, \quad 1, \quad \left [ \left[\begin{matrix}\frac{b}{\sqrt{b c}}\\1\end{matrix}\right]\right ]\right )\right ]