4 SymPy

First, import the SymPy package.

import sympy

[1] 2019-04-23 14:42:50 (5.04ms) python3 (3.30s)

4.1 Basic usage

SymPy symbols or expressions in a fenced code block are automatically rendered in display mode:

x = sympy.Symbol('x')
x**3

[2] 2019-04-23 14:42:50 (7.03ms) python3 (3.31s)

$$x^{3}$$

On the other hand, in an inline code, a SymPy object just returns a latex string like this {{x**2}} = x^{2}. This is intentional behavior. You can choose inline mode or display mode. Furthermore, you can form an expression you want.

* This is an inline mode example: ${{x**2+x+1}}$
  • This is an inline mode example: x^{2} + x + 1
* This is a display mode example:

$${{x**2+x+1}}$$
  • This is a display mode example:
x^{2} + x + 1
  • You can concatenate SymPy objects and/or normal latex source in the same line to make an expression form you want:
$${{x**2}} - {{x}} + {{x}} + \frac1y$$
x^{2} - x + x + \frac1y

Here, -x+x does not cancel out automatically.

4.2 Numbering

Pheasant uses MathJax's Automatic Equation Numbering. Use the custom header syntax like the figure and table. Also, you can add a tag for link.

#Eq f(x) = {{x**2}} {#eq-a#}

\begin{equation} f(x) = x^{2} \label{eq-a} \end{equation}

Using starred form, the equation won’t be numbered like original LaTeX.

#Eq* f(x) = {{x**2}}

\begin{equation*} f(x) = x^{2} \end{equation*}

As usual, you can refer to equation: See Eq. {#eq-a#}

As usual, you can refer to equation: See Eq. \eqref{eq-a}

Also, you can use native latex syntax. From MathJax document:

In equation \eqref{eq:sample}, we find the value of an interesting integral:

#Eq \int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15} \label{eq:sample}

In equation \eqref{eq:sample}, we find the value of an interesting integral:

\begin{equation} \int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15} \label{eq:sample} \end{equation}

4.3 Matrix

import numpy as np

xw = np.array([[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34]])
b = np.array([1000, 2000, 3000, 4000])
sympy.Matrix(xw + np.ones((3, 1), int) @ b.reshape((1, -1)))

[13] 2019-04-23 14:42:50 (11.0ms) python3 (3.40s)

$$\left[\begin{matrix}1011 & 2012 & 3013 & 4014\\1021 & 2022 & 3023 & 4024\\1031 & 2032 & 3033 & 4034\end{matrix}\right]$$