# Tutorial - SymPy in 10 minutes
    
    

## Introduction

In this tutorial we will learn the basics of [Jupyter](http://jupyter.org/) and [SymPy](https://es.wikipedia.org/wiki/SymPy). SymPy is a Python library for symbolic computation. It provides computer algebra capabilities either as a standalone application, as a library to other applications, or live on the web as [SymPy Live](http://live.sympy.org/) or [SymPy Gamma](http://www.sympygamma.com/). Sympy is similar to other CAS (Computer Algebra Software) like Mathematica, Maple or Maxima.

A more complete tutorial can be found at [http://docs.sympy.org/latest/tutorial/index.html](http://docs.sympy.org/latest/tutorial/index.html).

Before using SymPy we should load it, like any other Python libary. We will use

```python
init_session()
```

to make some imports, this will help us in its interactive use. 

For scripting it would be better to do the imports diffferently, for example

```python
import sympy as sym
```

and then call the functions from SymPy in the following manner

```python
x = sym.Symbols("x")
expr = sym.cos(x)**2 + 3*x
deriv = expr.diff(x)
```

where we computed the derivative of $\cos^2(x) + 3x$, that should be $-2\sin(x)\cos(x) + 3$.

For further information on the Jupyter Notebook you can refer to the [User Manual](https://athena.brynmawr.edu/jupyter/hub/dblank/public/Jupyter%20Notebook%20Users%20Manual.ipynb).


```python
%matplotlib notebook
```


```python
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
```


```python
init_session()
```

    IPython console for SymPy 1.7.1 (Python 3.9.2-64-bit) (ground types: python)
    
    These commands were executed:
    >>> 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)
    >>> init_printing()
    
    Documentation can be found at https://docs.sympy.org/1.7.1/
    



```python
plt.style.use("seaborn-notebook")
plt.rcParams["figure.figsize"] = 6, 4
```

Let us start with some simple calculations. Below we have a _code cell_ with an addition. Place the cursor on it and press SHIFT + ENTER to evaluate it.


```python
1 + 3
```

Let us make some calculations


```python
factorial(5)
```


```python
Out[6]* 10
```


```python
1 // 3
```


```python
1 / 3
```


```python
S(1) / 3
```

We can evaluate an expression to its floating point version


```python
sqrt(2*pi)
```


```python
float(Out[11])
```

In the previous line we used the expression **Out[11]** that stores the output from the evaluation in cell 11 (In[11]). We can also store expressions as variables, just as any Python variable.


```python
radius = 10
height = 100
area = pi * radius**2
volume = area * height
```


```python
volume
```


```python
float(volume)
```

So far, we have just used SymPy as a calculator. Let us try more advanced calculations



Definite and indefinite integrals


```python
integrate(sin(x), x)
```


```python
integrate(sin(x), (x, 0, pi))
```

We can define a function and integrate it


```python
f = lambda x: x**2 + 5
```


```python
f(5)
```


```python
integrate(f(z), z)
```


```python
integrate(1/(x**2 + y), x)
```

If we assume that the denominator is positive, the expression can be factorized further



```python
a = symbols("a", positive=True)
integrate(1/(x**2 + a), x)
```

We just learnt the basics, we can try some examples now.

**Note:** If you want to know more about a specific function you can use ``help()`` or the IPython magic command ``??``


```python
help(integrate)
```

    Help on function integrate in module sympy.integrals.integrals:
    
    integrate(*args, meijerg=None, conds='piecewise', risch=None, heurisch=None, manual=None, **kwargs)
        integrate(f, var, ...)
        
        Compute definite or indefinite integral of one or more variables
        using Risch-Norman algorithm and table lookup. This procedure is
        able to handle elementary algebraic and transcendental functions
        and also a huge class of special functions, including Airy,
        Bessel, Whittaker and Lambert.
        
        var can be:
        
        - a symbol                   -- indefinite integration
        - a tuple (symbol, a)        -- indefinite integration with result
                                        given with `a` replacing `symbol`
        - a tuple (symbol, a, b)     -- definite integration
        
        Several variables can be specified, in which case the result is
        multiple integration. (If var is omitted and the integrand is
        univariate, the indefinite integral in that variable will be performed.)
        
        Indefinite integrals are returned without terms that are independent
        of the integration variables. (see examples)
        
        Definite improper integrals often entail delicate convergence
        conditions. Pass conds='piecewise', 'separate' or 'none' to have
        these returned, respectively, as a Piecewise function, as a separate
        result (i.e. result will be a tuple), or not at all (default is
        'piecewise').
        
        **Strategy**
        
        SymPy uses various approaches to definite integration. One method is to
        find an antiderivative for the integrand, and then use the fundamental
        theorem of calculus. Various functions are implemented to integrate
        polynomial, rational and trigonometric functions, and integrands
        containing DiracDelta terms.
        
        SymPy also implements the part of the Risch algorithm, which is a decision
        procedure for integrating elementary functions, i.e., the algorithm can
        either find an elementary antiderivative, or prove that one does not
        exist.  There is also a (very successful, albeit somewhat slow) general
        implementation of the heuristic Risch algorithm.  This algorithm will
        eventually be phased out as more of the full Risch algorithm is
        implemented. See the docstring of Integral._eval_integral() for more
        details on computing the antiderivative using algebraic methods.
        
        The option risch=True can be used to use only the (full) Risch algorithm.
        This is useful if you want to know if an elementary function has an
        elementary antiderivative.  If the indefinite Integral returned by this
        function is an instance of NonElementaryIntegral, that means that the
        Risch algorithm has proven that integral to be non-elementary.  Note that
        by default, additional methods (such as the Meijer G method outlined
        below) are tried on these integrals, as they may be expressible in terms
        of special functions, so if you only care about elementary answers, use
        risch=True.  Also note that an unevaluated Integral returned by this
        function is not necessarily a NonElementaryIntegral, even with risch=True,
        as it may just be an indication that the particular part of the Risch
        algorithm needed to integrate that function is not yet implemented.
        
        Another family of strategies comes from re-writing the integrand in
        terms of so-called Meijer G-functions. Indefinite integrals of a
        single G-function can always be computed, and the definite integral
        of a product of two G-functions can be computed from zero to
        infinity. Various strategies are implemented to rewrite integrands
        as G-functions, and use this information to compute integrals (see
        the ``meijerint`` module).
        
        The option manual=True can be used to use only an algorithm that tries
        to mimic integration by hand. This algorithm does not handle as many
        integrands as the other algorithms implemented but may return results in
        a more familiar form. The ``manualintegrate`` module has functions that
        return the steps used (see the module docstring for more information).
        
        In general, the algebraic methods work best for computing
        antiderivatives of (possibly complicated) combinations of elementary
        functions. The G-function methods work best for computing definite
        integrals from zero to infinity of moderately complicated
        combinations of special functions, or indefinite integrals of very
        simple combinations of special functions.
        
        The strategy employed by the integration code is as follows:
        
        - If computing a definite integral, and both limits are real,
          and at least one limit is +- oo, try the G-function method of
          definite integration first.
        
        - Try to find an antiderivative, using all available methods, ordered
          by performance (that is try fastest method first, slowest last; in
          particular polynomial integration is tried first, Meijer
          G-functions second to last, and heuristic Risch last).
        
        - If still not successful, try G-functions irrespective of the
          limits.
        
        The option meijerg=True, False, None can be used to, respectively:
        always use G-function methods and no others, never use G-function
        methods, or use all available methods (in order as described above).
        It defaults to None.
        
        Examples
        ========
        
        >>> from sympy import integrate, log, exp, oo
        >>> from sympy.abc import a, x, y
        
        >>> integrate(x*y, x)
        x**2*y/2
        
        >>> integrate(log(x), x)
        x*log(x) - x
        
        >>> integrate(log(x), (x, 1, a))
        a*log(a) - a + 1
        
        >>> integrate(x)
        x**2/2
        
        Terms that are independent of x are dropped by indefinite integration:
        
        >>> from sympy import sqrt
        >>> integrate(sqrt(1 + x), (x, 0, x))
        2*(x + 1)**(3/2)/3 - 2/3
        >>> integrate(sqrt(1 + x), x)
        2*(x + 1)**(3/2)/3
        
        >>> integrate(x*y)
        Traceback (most recent call last):
        ...
        ValueError: specify integration variables to integrate x*y
        
        Note that ``integrate(x)`` syntax is meant only for convenience
        in interactive sessions and should be avoided in library code.
        
        >>> integrate(x**a*exp(-x), (x, 0, oo)) # same as conds='piecewise'
        Piecewise((gamma(a + 1), re(a) > -1),
            (Integral(x**a*exp(-x), (x, 0, oo)), True))
        
        >>> integrate(x**a*exp(-x), (x, 0, oo), conds='none')
        gamma(a + 1)
        
        >>> integrate(x**a*exp(-x), (x, 0, oo), conds='separate')
        (gamma(a + 1), -re(a) < 1)
        
        See Also
        ========
        
        Integral, Integral.doit
    



```python
integrate??
```

## Examples

### Solving algebraic equations

Right now, there are two main options for solution of algebraic systems, namely: 
[``solveset`` and ``solve``](http://docs.sympy.org/latest/tutorial/solvers.html).
The preferred method is ``solveset`` (see this
[explanation](http://docs.sympy.org/latest/modules/solvers/solveset.html), although
there are systems that can be solved using ``solve`` and not ``solveset``.


To solve equations using ``solveset``:


```python
a, b, c = symbols("a b c")
solveset(a*x**2 + b*x + c, x)
```

We should enter the equations as equated to 0, or as an equation


```python
solveset(Eq(a*x**2 + b*x, -c), x)
```

Currently solveset is not capable of solving the following types of equations:

- Non-linear multivariate system
- Equations solvable by LambertW (Transcendental equation solver).

solve can be used for such cases:


```python
solve([x*y - 1, x - 2], x, y)
```


```python
solve(x*exp(x) - 1, x )
```

### Linear Algebra

We use ``Matrix`` to create matrices. Matrices can contain expressions. And we use the method ``.inv()`` to compute the inverse and ``*`` to multiply matrices.


```python
A = Matrix([
        [1, -1],
        [1, sin(c)]
    ])
display(A)
```


```python
B = A.inv()
display(B)
```


```python
A * B
```

This should be the identity matrix, let us simplify the expression. There are several simplification functions, and ``simplify`` is the most general one. Simplifying is a complicated matter... if you are uncertain; use ``simplify``.


```python
simplify(A * B)
```

### Plotting

We can make 2D and 3D plots


```python
from sympy.plotting import plot3d
```


```python
plot(sin(x), (x, -pi, pi));
```


    <IPython.core.display.Javascript object>







```python
monkey_saddle = x**3 - 3*x*y**2
p = plot3d(monkey_saddle, (x, -2, 2), (y, -2, 2))
```


    <IPython.core.display.Javascript object>






### Derivatives and differential equations

We can use the function ``diff`` or the method ``.diff()`` to compute derivatives.


```python
f = lambda x: x**2
```


```python
diff(f(x), x)
```


```python
f(x).diff(x)
```


```python
g = lambda x: sin(x)
```


```python
diff(g(f(x)), x)
```

Yes, SymPy knows about the chain rule.

To finish, let us solve a second order ODE

$$ u''(t) + \omega^2 u(t) = 0$$


```python
u = symbols("u", cls=Function)
omega = symbols("omega", positive=True)
```


```python
ode = u(t).diff(t, 2) + omega**2 * u(t)
dsolve(ode, u(t))
```

## Turning Sympy expression into evaluable functions

``lambdify`` provides convenient functions to transform sympy expressions to lambda functions which can be used to calculate numerical values very fast.

Let's try a first example


```python
f = lambdify(x, x**2, "numpy")
f(3)
```


```python
f(np.array([1, 2, 3]))
```




    array([1, 4, 9], dtype=int32)



We can try a more difficult one


```python
fun = diff(sin(x)*cos(x**3) - sin(x)/x, x)
fun
```


```python
fun_numpy = lambdify(x, fun, "numpy")
```

and then evaluate it for some points in, let's say, $[0, 5]$


```python
pts = np.linspace(0, 5, 1000)
fun_pts = fun_numpy(pts + 1e-6) # To avoid division by 0
```


```python
plt.figure()
plt.plot(pts, fun_pts);
```


    <IPython.core.display.Javascript object>






## References

- Žiga Lenarčič. ["10 minute (wx)Maxima tutorial:"](https://andrejv.github.io/wxmaxima/tutorials/10minute.zip), (2008). Accessed on: July 25, 2017.
- Borland, David, and Russell M. Taylor II. ["Rainbow color map (still) considered harmful."](https://data3.mprog.nl/course/15%20Readings/40%20Reading%204/Borland_Rainbow_Color_Map.pdf) IEEE computer graphics and applications 27.2 (2007): 14-17.

The following cell change the style of the notebook.


```python
from IPython.core.display import HTML
def css_styling():
    styles = open('../styles/sympy.css', 'r').read()
    return HTML(styles)
css_styling()
```




<link href="https://fonts.googleapis.com/css?family=Gentium+Basic:400,400i,700,700i"
    rel='stylesheet' >
    <link href="https://fonts.googleapis.com/css?family=Lato:100,100i,300,300i,400,400i,600,600i:700,700i,900,900i"
    rel='stylesheet' >
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i:700,700i,800,800i"
    rel='stylesheet' >
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:300,400'
    rel='stylesheet' >



<style>

@font-face {
    font-family: "Computer Modern";
    src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');
}


#notebook_panel { /* main background */
    background: rgb(245,245,245);
}

div.cell { /* set cell width */
    width: 800px;
}

div #notebook { /* centre the content */
    background: #fff; /* white background for content */
    width: 1000px;
    margin: auto;
    padding-left: 0em;
}

#notebook li { /* More space between bullet points */
margin-top:0.5em;
}

/* draw border around running cells */
div.cell.border-box-sizing.code_cell.running { 
    border: 1px solid #111;
}

/* Put a solid color box around each cell and its output, visually linking them*/
div.cell.code_cell {
    background-color: rgb(256,256,256); 
    border-radius: 0px; 
    padding: 0.5em;
    margin-left:1em;
    margin-top: 1em;
}

/* Regular text */
div.text_cell_render{
    font-family: 'Open Sans', sans-serif;
    line-height: 140%;
    font-size: 110%;
    color: #333333; 
    width:680px;
    margin-left:auto;
    margin-right:auto;
}

/* Hyperlinks */
a:link {
    color: #4faf00;
}

a:visited {
    color: #3b5526;
}

/* Formatting for header cells */
.text_cell_render h1 {
    font-family: 'Gentium Basic', serif;
    font-style:regular;
    font-weight: 700;    
    font-size: 250%;
    line-height: 100%;
    color: #3b5526;
    margin-bottom: 1em;
    margin-top: 0.5em;
    display: block;
}	
.text_cell_render h2 {
    font-family: 'Lato', sans-serif;
    font-weight: bold; 
    font-size: 180%;
    line-height: 100%;
    color: #4e7032;
    margin-bottom: 0.5em;
    margin-top: 0.5em;
    display: block;
}	

.text_cell_render h3 {
    font-family: 'Lato', sans-serif;
	font-size: 150%;
    margin-top:12px;
    margin-bottom: 3px;
    font-style: regular;
    color: #6fa047;
}

.text_cell_render h4 {    /*Use this for captions*/
    font-family: 'Lato', sans-serif;
    font-weight: 300; 
    font-size: 100%;
    line-height: 120%;
    text-align: left;
    width:500px;
    margin-top: 1em;
    margin-bottom: 2em;
    margin-left: 80pt;
    font-style: regular;
}

.text_cell_render h5 {  /*Use this for small titles*/
    font-family: 'Lato', sans-serif;
    font-weight: regular;
    font-size: 130%;
    color: #6fa047;
    font-style: italic;
    margin-bottom: .5em;
    margin-top: 1em;
    display: block;
}

.text_cell_render h6 { /*use this for copyright note*/
    font-family: 'Open Sans', sans-serif;
    font-weight: 300;
    font-size: 9pt;
    line-height: 100%;
    color: grey;
    margin-bottom: 1px;
    margin-top: 1px;
}

    .CodeMirror{
            font-family: "Source Code Pro";
			font-size: 90%;
    }
/*    .prompt{
        display: None;
    }*/


    .warning{
        color: rgb( 240, 20, 20 )
        }  
</style>





176e6a21aa554d89b61bcad2c57070bd3e6e1ae6
ipynb
Jupyter Notebook
beginner/sympy_in_10_minutes.ipynb
nicoguaro/sympy-notebooks
c86ae135a7654280f52effe633bb7016f34593bf
BSD-3-Clause
2016-07-17T12:47:12.000Z
2021-11-30T18:49:31.000Z
beginner/sympy_in_10_minutes.ipynb
nicoguaro/sympy-notebooks
c86ae135a7654280f52effe633bb7016f34593bf
BSD-3-Clause
2017-03-18T16:19:58.000Z
2021-06-03T23:03:35.000Z
beginner/sympy_in_10_minutes.ipynb
nicoguaro/sympy-notebooks
c86ae135a7654280f52effe633bb7016f34593bf
BSD-3-Clause
2016-09-14T22:36:22.000Z
2022-03-06T04:19:44.000Z
Qwen/Qwen-72B
1. YES
2. YES