%pylab inline
import seaborn
style.use('seaborn-poster')
import sympy
sympy.init_printing()
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88 return f(*args, **kwds) /usr/lib/python3/dist-packages/matplotlib/__init__.py:874: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter. warnings.warn(self.msg_depr % (key, alt_key))
Populating the interactive namespace from numpy and matplotlib
$\mathbb{G}_2$
$\mathbb{F}_{q^k}$
$\left\{\mathrm{pk} \cdot \mathscr{H}_2(x_i)\right\}^n_{i=0}$ $\{\mathrm{pk} \cdot \mathscr{H}_2(x_i)\}^n_{i=0}$
Assume a base point $P$ with $\mathcal{Q} = (X_i, Y_i, Z_i)$.
The group $\left\{\infty\right\} \cup \left(E\left(\mathbb F_{p^2}\right) \cap \left(\mathbb F_p \times \mathbb F_p\right)\right)$
$ \alpha \beta \gamma \delta \epsilon^\Xi_\zeta $
$αβγδεσςρφ \phi τυθιοπηξχζψκλμν$
As we remember, the definition of the partial derivative is the following:
$$\frac{\partial y(x)}{\partial x} = \lim_{\varepsilon \to 0} \frac{y(x+\varepsilon) - y(x)}{\varepsilon}$$considering $y$ as a function of $x$.
The .
gets rid of the right delimiter (I was using $|$.) There's also a cases
environment, with slightly tighter vertical spacing and some extra column spacing:
$$\forall l, m \in \mathbb{Z}^2: \int_{-\infty}^\infty p(t-lT)p(t-mT) \,\mathrm{d}t = \begin{cases}
1 & \text{when $l=m$} \\
0 &\text{otherwise.}
\end{cases}$$
$$s_{\{a_0, a_1, \ldots, a_M\}}(t) = \sum_{k=0}^M a_k p(t - kT)$$
$$a_k = \int\limits_{-\infty}^\infty s(t)p(t-kT) \,\mathrm{d}t$$
You can define new $\LaTeX$ commands with \newcommand
. For example, $\newcommand{bigo}[1]{\mathcal{O}\left(#1\right)}\bigo{N^2}$. Then you can use them in later math in the same notebook (although apparently this has a problem when exporting to $\LaTeX$):
This is useful for things like derivatives, which are otherwise a real pain to write with Roman-style "d" as it should be: $\newcommand{diff}{\mathrm{d}}\newcommand{deriv}[2]{\frac{\diff#1}{\diff#2}} \deriv xt \diff t = \diff x$.
$$\deriv{\left(\deriv{\left(\deriv yx\right)}x\right)}x = \frac{\diff^3 y}{\diff x^3}$$1 + 1 / (1 + 1 / (1 + 1 / ( 1 + 1/2)))
1.625
1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / ( 1 + 1/2))))))))
1.6179775280898876
sympy.GoldenRatio
sympy.GoldenRatio.evalf(100)
str(sympy.Rational(1, 97).evalf(1000))
'0.01030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979381443298969072164948453608247422680412371134020618556701030927835051546391752577319587628865979'
phi = [sympy.Integer(1)]
for i in range(30):
phi.append(1 + 1 / phi[-1])
phi, phi[-1].evalf(20)
1 + (1 / sympy.GoldenRatio).evalf(20)
(1/(sympy.GoldenRatio - 1)).evalf(20)
array
example from LaTeX wikibook:
Hmm, is there a way to get the top line to omit the vertical separator?
A symmetric short-support element is $b(x) = \begin{cases}0, & |x| \leq 1, \\ -\infty, & \text{otherwise}\end{cases}$ in e.g. the morphological gradient operation. $G(f) = f \oplus b - f \ominus b$. $A \ominus B = \left\{z \in E \middle|B_z \subseteq A\right\}$ where $\forall z \in E: B_z = \left\{b + z \middle| b \in B\right\}$. $A \ominus B = \bigcap_{b \in B} A_{-b}$. $A \circ B = (A \ominus B) \oplus B$. $A \bullet B = (A \oplus B) \ominus B$.
$E$, the set of image positions, may be e.g. $\mathbb{Z}^2$ or $\mathbb{R}^3$. As sort of explained in the Wikipedia article the infinities are in the range of the functions so that the kernel or structuring element can have "empty" spots. $$(f \oplus b)(x) = \sup_{y \in E} \big(f(y) + b(x - y)\big) \text{ (dilation)}$$ $$(f \ominus b)(x) = \inf_{y \in E} \big(f(y) + b(y - x)\big) \text{ (erosion)}$$ $$f \circ b = (f \ominus b) \oplus b \text{ (opening)}$$ $$f \bullet b = (f \oplus b) \ominus b \text{ (closing)}$$
eps = sympy.Symbol(r"\varepsilon")
m2 = sympy.Matrix([[1, 0], [eps, 1]])
m2
m1 = sympy.Matrix([[1, -eps], [0, 1]])
m1
m3 = m2 * m1
m3
[m.det() for m in [m1, m2, m3]]
We can recover the original single-multiply-accumulate-computable matrices from this matrix using LU decomposition:
m3.LUdecomposition()
m3.eigenvects(simplify=True)
m3.inv()
Hmm, that looks like we can achieve the inverse by negating $\varepsilon$ and doing the operations in the opposite order — first $y$, then $x$. But LU decomposition doesn't discover this:
m3.inv().LUdecomposition()
P, D = m3.diagonalize()
P, D, P.inv()
sympy.simplify(P.inv())
We can get $\LaTeX$ output with sympy.latex
:
print(sympy.latex(m3.inv().LUdecomposition()))
\left ( \left[\begin{matrix}1 & 0\\- \frac{\varepsilon}{- \varepsilon^{2} + 1} & 1\end{matrix}\right], \quad \left[\begin{matrix}- \varepsilon^{2} + 1 & \varepsilon\\0 & \frac{\varepsilon^{2}}{- \varepsilon^{2} + 1} + 1\end{matrix}\right], \quad \left [ \right ]\right )
m3.replace(eps, .01).eigenvects()
m3.subs(eps, 2).eigenvects()
m3.subs(eps, 2.01).eigenvects()
m4 = (m3*m3).expand()
m4
m5 = (m4 * m4).expand()
m5
m5.subs(eps, .01)
m6 = (m5 * m5).expand()
m6
m6.subs(eps, .01)
c = m6.dot(sympy.Matrix([[1, 0]]))[0]
c
sympy.plot(c, m6.dot(sympy.Matrix([[1, 0]]))[1], (eps, -1.9, 1.9));
v = sympy.Matrix([[1, 0]])
circle = [v]
mf = m3.subs(eps, 2**-5)
for i in range(256):
v = mf.dot(v)
circle.append(v)
plot(circle)
circle[:5]
plot([x for x, y in circle], [y for x, y in circle], 'o', markersize=1)
None
sympy.Integral(sympy.sqrt(1/eps), eps)
import sympy.printing.dot
import os
os.popen('dot -Tx11', 'w').write(sympy.printing.dot.dotprint(m6[0]))
x, y = sympy.symbols('x y')
x, y
integ = sympy.Integral(x**y * sympy.exp(-x), (x, 0, sympy.oo))
integ
integ.doit()
print(sympy.latex(integ.doit()))
\begin{cases} \Gamma{\left(y + 1 \right)} & \text{for}\: - \Re{y} < 1 \\\int_{0}^{\infty} x^{y} e^{- x}\, dx & \text{otherwise} \end{cases}
print(sympy.pretty(integ.doit()))
⎧ Γ(y + 1) for -re(y) < 1 ⎪ ⎪∞ ⎪⌠ ⎨⎮ y -x ⎪⎮ x ⋅ℯ dx otherwise ⎪⌡ ⎪0 ⎩
sympy.srepr(integ.doit())
"Piecewise(ExprCondPair(gamma(Add(Symbol('y'), Integer(1))), StrictLessThan(Mul(Integer(-1), re(Symbol('y'))), Integer(1))), ExprCondPair(Integral(Mul(Pow(Symbol('x'), Symbol('y')), exp(Mul(Integer(-1), Symbol('x')))), Tuple(Symbol('x'), Integer(0), oo)), S.true))"
ido = integ.doit()
ido.func, ido.args, ido.args[0].func
(sympy.functions.elementary.piecewise.Piecewise, ((gamma(y + 1), -re(y) < 1), (Integral(x**y*exp(-x), (x, 0, oo)), True)), sympy.functions.elementary.piecewise.ExprCondPair)
from sympy.functions.elementary.piecewise import Piecewise, ExprCondPair
box = Piecewise(ExprCondPair(sympy.Integer(0), sympy.StrictLessThan(x, 0)),
ExprCondPair(sympy.Integer(1), sympy.StrictLessThan(x, 1)),
ExprCondPair(sympy.Integer(0), sympy.S.true))
box
print(sympy.latex(box))
\begin{cases} 0 & \text{for}\: x < 0 \\1 & \text{for}\: x < 1 \\0 & \text{otherwise} \end{cases}
[box.subs(x, i) for i in [-1, -0.5, 0, 0.5, 1, 1.5]]
Unfortunately, convolution doesn't exist in Sympy, and plotting doesn't seem to work.
sympy.plot(x**2, (x, -1, 2))
<sympy.plotting.plot.Plot at 0x7fcec5ed4780>
sympy.plot(box, (x, -1, 2))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-40-6d001994f0af> in <module>() ----> 1 sympy.plot(box, (x, -1, 2)) /usr/lib/python3/dist-packages/sympy/plotting/plot.py in plot(*args, **kwargs) 1278 plots = Plot(*series, **kwargs) 1279 if show: -> 1280 plots.show() 1281 return plots 1282 /usr/lib/python3/dist-packages/sympy/plotting/plot.py in show(self) 183 self._backend.close() 184 self._backend = self.backend(self) --> 185 self._backend.show() 186 187 def save(self, path): /usr/lib/python3/dist-packages/sympy/plotting/plot.py in show(self) 1016 1017 def show(self): -> 1018 self.process_series() 1019 #TODO after fixing https://github.com/ipython/ipython/issues/1255 1020 # you can uncomment the next line and remove the pyplot.show() call /usr/lib/python3/dist-packages/sympy/plotting/plot.py in process_series(self) 895 # Create the collections 896 if s.is_2Dline: --> 897 collection = self.LineCollection(s.get_segments()) 898 self.ax.add_collection(collection) 899 elif s.is_contour: /usr/lib/python3/dist-packages/sympy/plotting/plot.py in get_segments(self) 501 list_segments.append([p, q]) 502 --> 503 f_start = f(self.start) 504 f_end = f(self.end) 505 sample([self.start, f_start], [self.end, f_end], 0) /usr/lib/python3/dist-packages/sympy/plotting/experimental_lambdify.py in __call__(self, args) 226 use_evalf=True, 227 complex_wrap_evalf=True) --> 228 result = self.lambda_func(args) 229 warnings.warn('The evaluation of the expression is' 230 ' problematic. We are trying a failback method' <string> in <lambda>(x0) TypeError: unorderable types: complex() < int()
sympy.var('ohshit')
ohshit
sympy.symbols('x0 y37 foo1')
x0
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-44-57df2e63262a> in <module>() ----> 1 x0 NameError: name 'x0' is not defined
[sympy.Eq(f(x), f(x).rewrite(sympy.exp)) for f in [sympy.sin, sympy.cos, sympy.tan]]
sympy.cos(x).rewrite(sympy.exp).factor()
((x+y)**14).expand()
sympy.factorint(3801372, visual=True)
import sympy.utilities.autowrap
sympy.utilities.autowrap??
.