In [1]:
import sympy as sym
import numpy as np
from IPython.display import Math, display

sym.init_printing()
In [2]:
x
--------------------
NameErrorTraceback (most recent call last)
<ipython-input-2-6fcf9dfbd479> in <module>
----> 1 x

NameError: name 'x' is not defined
In [3]:
y+4
--------------------
NameErrorTraceback (most recent call last)
<ipython-input-3-b65f86877ec9> in <module>
----> 1 y+4

NameError: name 'y' is not defined
In [4]:
x,y = sym.symbols('x,y')
x
Out[4]:
$$x$$
In [5]:
y + 4
Out[5]:
$$y + 4$$
In [6]:
display(x**y)
$$x^{y}$$
In [7]:
display(x/y)
$$\frac{x}{y}$$
In [8]:
display(np.sqrt(2))
display(sym.sqrt(2))
$$1.4142135623730951$$
$$\sqrt{2}$$
In [9]:
display(y*x**2)
$$x^{2} y$$
In [10]:
display(sym.sqrt(4)*x)
$$2 x$$
In [11]:
display(sym.sqrt(x)*sym.sqrt(x))
$$x$$
In [12]:
display(Math('\\sigma = \\mu \\times \\sqrt{5x+3z-17u}'))
display(Math('\\sqrt{x}\\sqrt{x}'))
$\displaystyle \sigma = \mu \times \sqrt{5x+3z-17u}$
$\displaystyle \sqrt{x}\sqrt{x}$
In [13]:
display(Math('x_{mm} + y^{n+2k-15}'))
display(Math('\\text{The answer to this equation is }\\frac{1+x}{2v-s^{t+4r}}'))
$\displaystyle x_{mm} + y^{n+2k-15}$
$\displaystyle \text{The answer to this equation is }\frac{1+x}{2v-s^{t+4r}}$

This is a markdown cell

This cell is not for running actual Python code $\frac{1+x}{2v-s^{t+4r}}$ $$\frac{1+x}{2v-s^{t+4r}}$$

In [14]:
# This is a markdown cell

This cell is not for running actual Python code
$\frac{1+x}{2v-s^{t+4r}}$
$$\frac{1+x}{2v-s^{t+4r}}$$
  File "<ipython-input-14-40451478a461>", line 3
    This cell is not for running actual Python code
            ^
SyntaxError: invalid syntax

A subcell

In [15]:
## A subcell

Even smaller text

In [16]:
### Even smaller text
In [17]:
display(Math('4x+5y-8z=17'))
$\displaystyle 4x+5y-8z=17$
In [18]:
display(Math('\\sin(2\\pi f t + \\theta)'))
$\displaystyle \sin(2\pi f t + \theta)$
In [19]:
display(Math('e=mc^2'))
$\displaystyle e=mc^2$
In [20]:
display(Math('\\frac{4+5x^2}{(1+x)(1-x)}'))
$\displaystyle \frac{4+5x^2}{(1+x)(1-x)}$
In [21]:
mu, alpha, sigma = sym.symbols('mu,alpha,sigma')
expr = sym.exp((mu-alpha)**2 / (2*sigma**2))
display(expr)
$$e^{\frac{\left(- \alpha + \mu\right)^{2}}{2 \sigma^{2}}}$$
In [22]:
hello = sym.symbols('hello')
hello/3
Out[22]:
$$\frac{hello}{3}$$
In [23]:
x,y = sym.symbols('x,y')
expr = x+4+2*y
expr.subs({x:-4,y:3})
Out[23]:
$$6$$
In [24]:
expr = 3/x
display(Math(sym.latex(expr)))
$\displaystyle \frac{3}{x}$
In [25]:
sym.latex(expr)
Out[25]:
'\\frac{3}{x}'
In [26]:
sym.latex(3/4)
Out[26]:
'0.75'
In [27]:
sym.latex('3/4')
Out[27]:
'3/4'
In [28]:
sym.latex(sym.sympify('3/4'))
Out[28]:
'\\frac{3}{4}'
In [29]:
display(Math(sym.latex(sym.sympify('3/4'))))
$\displaystyle \frac{3}{4}$
In [30]:
expr = x**2+4

for i in range(-2,3):
    ans = (x+4).subs(x,i**2)
    display(Math('\\text{With }x=%g, x^2+4 \\quad \\Rightarrow \\quad %g^2+4=%g' %(i,i,ans)))
$\displaystyle \text{With }x=-2, x^2+4 \quad \Rightarrow \quad -2^2+4=8$
$\displaystyle \text{With }x=-1, x^2+4 \quad \Rightarrow \quad -1^2+4=5$
$\displaystyle \text{With }x=0, x^2+4 \quad \Rightarrow \quad 0^2+4=4$
$\displaystyle \text{With }x=1, x^2+4 \quad \Rightarrow \quad 1^2+4=5$
$\displaystyle \text{With }x=2, x^2+4 \quad \Rightarrow \quad 2^2+4=8$