def test_latex_sum(): assert latex(Sum(x*y**2, (x, -2, 2), (y, -5, 5))) == \ r"\sum_{\substack{-2 \leq x \leq 2\\-5 \leq y \leq 5}} x y^{2}" assert latex(Sum(x**2, (x, -2, 2))) == \ r"\sum_{x=-2}^{2} x^{2}" assert latex(Sum(x**2 + y, (x, -2, 2))) == \ r"\sum_{x=-2}^{2} \left(x^{2} + y\right)"
def _repr_html_(self, include_terms_cache=True, doit=True): ''' Jupyter notebook integration for pretty printing Taken from: http://ipython.readthedocs.io/en/stable/config/integrating.html ''' def subscripts_of(key): with bind_Mul_indexed(key, self.indexed) as (_, subscripts): return tuple(subscripts) substitutions = dict(zip(self.index, itertools.repeat(0))) keys_with_integral_subscripts = {k.subs(substitutions):k for k in self.terms_cache} integral_subscrips = {subscripts_of(k):k for k in keys_with_integral_subscripts} sorted_subscripts = sorted(integral_subscrips.keys()) ordered_terms_cache = [Eq(symbolic_key, self.terms_cache[symbolic_key]) for k in sorted_subscripts for symbolic_key in [keys_with_integral_subscripts[integral_subscrips[k]]]] src = r'$\left(\Theta, \Gamma\right)_{{{index}}}^{{{sym}}}$ where: <br><ul>{Theta}{Gamma}</ul>'.format( sym=latex(self.indexed), index=','.join(map(latex, self.index)), #index=latex(self.index), Theta=r'<li>$\Theta = \left\{{ {rec_eqs} \right\}}$</li>'.format( rec_eqs=latex(self.recurrence_eq.doit() if doit else self.recurrence_eq)), Gamma=r'<li>$\Gamma = \left\{{\begin{{array}}{{c}}{terms_cache}\end{{array}}\right\}}$</li>'.format( terms_cache=r'\\'.join(map(latex, ordered_terms_cache)))) return src
def test_latex_intervals(): a = Symbol('a', real=True) assert latex(Interval(0, a)) == r"\left[0, a\right]" assert latex(Interval(0, a, False, False)) == r"\left[0, a\right]" assert latex(Interval(0, a, True, False)) == r"\left(0, a\right]" assert latex(Interval(0, a, False, True)) == r"\left[0, a\right)" assert latex(Interval(0, a, True, True)) == r"\left(0, a\right)"
def test_commutator(): A = Operator("A") B = Operator("B") c = Commutator(A, B) c_tall = Commutator(A ** 2, B) assert str(c) == "[A,B]" assert pretty(c) == "[A,B]" assert upretty(c) == u"[A,B]" assert latex(c) == r"\left[A,B\right]" sT(c, "Commutator(Operator(Symbol('A')),Operator(Symbol('B')))") assert str(c_tall) == "[A**2,B]" ascii_str = """\ [ 2 ]\n\ [A ,B]\ """ ucode_str = u( """\ ⎡ 2 ⎤\n\ ⎣A ,B⎦\ """ ) assert pretty(c_tall) == ascii_str assert upretty(c_tall) == ucode_str assert latex(c_tall) == r"\left[A^{2},B\right]" sT(c_tall, "Commutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))")
def test_anticommutator(): A = Operator("A") B = Operator("B") ac = AntiCommutator(A, B) ac_tall = AntiCommutator(A ** 2, B) assert str(ac) == "{A,B}" assert pretty(ac) == "{A,B}" assert upretty(ac) == u"{A,B}" assert latex(ac) == r"\left\{A,B\right\}" sT(ac, "AntiCommutator(Operator(Symbol('A')),Operator(Symbol('B')))") assert str(ac_tall) == "{A**2,B}" ascii_str = """\ / 2 \\\n\ <A ,B>\n\ \\ /\ """ ucode_str = u( """\ ⎧ 2 ⎫\n\ ⎨A ,B⎬\n\ ⎩ ⎭\ """ ) assert pretty(ac_tall) == ascii_str assert upretty(ac_tall) == ucode_str assert latex(ac_tall) == r"\left\{A^{2},B\right\}" sT(ac_tall, "AntiCommutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))")
def test_mode(): expr = x+y assert latex(expr) == 'x + y' assert latex(expr, mode='plain') == 'x + y' assert latex(expr, mode='inline') == '$x + y$' assert latex(expr, mode='equation*')== '\\begin{equation*}x + y\\end{equation*}' assert latex(expr, mode='equation')== '\\begin{equation}x + y\\end{equation}'
def test_latex(): assert latex((2*tau)**Rational(7,2)) == "8 \\sqrt{2} \\tau^{\\frac{7}{2}}" assert latex((2*mu)**Rational(7,2), mode='equation*') == \ "\\begin{equation*}8 \\sqrt{2} \\mu^{\\frac{7}{2}}\\end{equation*}" assert latex((2*mu)**Rational(7,2), mode='equation', itex=True) == \ "$$8 \\sqrt{2} \\mu^{\\frac{7}{2}}$$" assert latex([2/x, y]) =="\\begin{bmatrix}\\frac{2}{x}, & y\\end{bmatrix}"
def test_latex_Matrix(): M = Matrix([[1 + x, y], [y, x - 1]]) assert latex(M) == "$\\left(\\begin{smallmatrix}1 + x & y\\\\y & -1 + " "x\\end{smallmatrix}\\right)$" profile = {"mat_str": "bmatrix"} assert latex(M, profile) == "$\\left(\\begin{bmatrix}1 + x & y\\\\y & -1 + " + "x\\end{bmatrix}\\right)$" profile["mat_delim"] = None assert latex(M, profile) == "$\\begin{bmatrix}1 + x & y\\\\y & -1 + " "x\\end{bmatrix}$"
def test_latex(): assert latex((2 * tau) ** Rational(7, 2)) == "$8 \\sqrt{2} \\sqrt[7]{\\tau}$" assert ( latex((2 * mu) ** Rational(7, 2), inline=False) == "\\begin{equation*}8 \\sqrt{2} \\sqrt[7]{\\mu}\\end{equation*}" ) assert latex([2 / x, y]) == "$\\begin{bmatrix}\\frac{2}{x}, & y\\end{bmatrix}$"
def test_latex(): assert latex((2*tau)**Rational(7,2)) == "$8 \\sqrt{2} \\tau^{\\frac{7}{2}}$" assert latex((2*mu)**Rational(7,2), inline=False) == \ "\\begin{equation*}8 \\sqrt{2} \\mu^{\\frac{7}{2}}\\end{equation*}" assert latex((2*mu)**Rational(7,2), inline=False, itex=True) == \ "$$8 \\sqrt{2} \\mu^{\\frac{7}{2}}$$" assert latex([2/x, y]) =="$\\begin{bmatrix}\\frac{2}{x}, & y\\end{bmatrix}$"
def test_latex_fresnel(): from sympy.functions.special.error_functions import (fresnels, fresnelc) from sympy.abc import z assert latex(fresnels(z)) == r'S\left(z\right)' assert latex(fresnelc(z)) == r'C\left(z\right)' assert latex(fresnels(z)**2) == r'S^{2}\left(z\right)' assert latex(fresnelc(z)**2) == r'C^{2}\left(z\right)'
def clean_up_zeros(matrix_spec, label="", colors={}, environment="equation", cancel_zeros=True, diagonal_col_offset=None): matrix, indexed_sym = matrix_spec if diagonal_col_offset is None: diagonal_col_offset = 1 tex_code = r"\begin{" + environment + r"}" + "\n" if environment else "" tex_code += r"\left[\begin{array}{" + ('c' * matrix.cols) + r'}' + "\n" for r in range(matrix.rows): for c in range(matrix.cols): space = "" if c == 0 else " " if r*diagonal_col_offset < c: coeff_str = "" elif cancel_zeros: coeff_str = latex(matrix[r,c]) if matrix[r,c] != 0 else "" else: coeff_str = latex(matrix[r,c]) if (r,c) in colors: coeff_str = r'\textcolor{' + colors[(r,c)] + r'}{' + coeff_str + "}" tex_code += "{}{} {}".format(space, coeff_str, r'\\' if c == matrix.cols-1 else r'&') tex_code += "" if r == matrix.rows - 1 else "\n" label = "\n{}".format(r'\label{eq:' + label + r'}' + "\n" if label else "") tex_code += "\n" + r'\end{array}\right]' tex_code += label + r'\end{' + environment + '}' if environment else "" return tex_code
def test_latex_Poly(): assert latex(Poly(x**2 + 2 * x, x)) == \ r"\operatorname{Poly}{\left( x^{2} + 2 x, x, domain=\mathbb{Z} \right)}" assert latex(Poly(x/y, x)) == \ r"\operatorname{Poly}{\left( \frac{x}{y}, x, domain=\mathbb{Z}\left(y\right) \right)}" assert latex(Poly(2.0*x + y)) == \ r"\operatorname{Poly}{\left( 2.0 x + 1.0 y, x, y, domain=\mathbb{R} \right)}"
def test_Hadamard(): from sympy.matrices import MatrixSymbol, HadamardProduct X = MatrixSymbol("X", 2, 2) Y = MatrixSymbol("Y", 2, 2) assert latex(HadamardProduct(X, Y * Y)) == r"X \circ \left(Y Y\right)" assert latex(HadamardProduct(X, Y) * Y) == r"\left(X \circ Y\right) Y"
def test_commutator(): A = Operator('A') B = Operator('B') c = Commutator(A, B) c_tall = Commutator(A**2, B) assert str(c) == '[A,B]' assert pretty(c) == '[A,B]' assert upretty(c) == u('[A,B]') assert latex(c) == r'\left[A,B\right]' sT(c, "Commutator(Operator(Symbol('A')),Operator(Symbol('B')))") assert str(c_tall) == '[A**2,B]' ascii_str = \ """\ [ 2 ]\n\ [A ,B]\ """ ucode_str = \ u("""\ ⎡ 2 ⎤\n\ ⎣A ,B⎦\ """) assert pretty(c_tall) == ascii_str assert upretty(c_tall) == ucode_str assert latex(c_tall) == r'\left[\left(A\right)^{2},B\right]' sT(c_tall, "Commutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))")
def test_PrettyPoly(): from sympy.polys.domains import QQ F = QQ.frac_field(x, y) R = QQ[x, y] assert latex(F.convert(x/(x + y))) == latex(x/(x + y)) assert latex(R.convert(x + y)) == latex(x + y)
def test_anticommutator(): A = Operator('A') B = Operator('B') ac = AntiCommutator(A, B) ac_tall = AntiCommutator(A**2, B) assert str(ac) == '{A,B}' assert pretty(ac) == '{A,B}' assert upretty(ac) == u('{A,B}') assert latex(ac) == r'\left\{A,B\right\}' sT(ac, "AntiCommutator(Operator(Symbol('A')),Operator(Symbol('B')))") assert str(ac_tall) == '{A**2,B}' ascii_str = \ """\ / 2 \\\n\ <A ,B>\n\ \\ /\ """ ucode_str = \ u("""\ ⎧ 2 ⎫\n\ ⎨A ,B⎬\n\ ⎩ ⎭\ """) assert pretty(ac_tall) == ascii_str assert upretty(ac_tall) == ucode_str assert latex(ac_tall) == r'\left\{\left(A\right)^{2},B\right\}' sT(ac_tall, "AntiCommutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))")
def test_latex_product(): assert ( latex(Product(x * y ** 2, (x, -2, 2), (y, -5, 5))) == r"\prod_{\substack{-2 \leq x \leq 2\\-5 \leq y \leq 5}} x y^{2}" ) assert latex(Product(x ** 2, (x, -2, 2))) == r"\prod_{x=-2}^{2} x^{2}" assert latex(Product(x ** 2 + y, (x, -2, 2))) == r"\prod_{x=-2}^{2} \left(x^{2} + y\right)"
def test_mode(): expr = x + y assert latex(expr) == "x + y" assert latex(expr, mode="plain") == "x + y" assert latex(expr, mode="inline") == "$x + y$" assert latex(expr, mode="equation*") == "\\begin{equation*}x + y\\end{equation*}" assert latex(expr, mode="equation") == "\\begin{equation}x + y\\end{equation}"
def test_QuotientRing(): from sympy.polys.domains import QQ R = QQ[x] / [x ** 2 + 1] assert latex(R) == r"\frac{\mathbb{Q}\left[x\right]}{\left< {x^{2} + 1} \right>}" assert latex(R.one) == r"{1} + {\left< {x^{2} + 1} \right>}"
def test_latex_functions(): assert latex(exp(x)) == "${e}^{x}$" f = Function('f') beta = Function('beta') assert latex(f(x)) == r"$f\left(x\right)$" assert latex(beta(x)) == r"$\beta\left(x\right)$" assert latex(sin(x)) == r"$\mathrm{sin}\left(x\right)$"
def test_latex_Matrix(): M = Matrix([[1 + x, y], [y, x - 1]]) assert latex(M) == "\\left(\\begin{smallmatrix}1 + x & y\\\\y & -1 + " "x\\end{smallmatrix}\\right)" settings = {"mat_str": "bmatrix"} assert latex(M, **settings) == "\\left(\\begin{bmatrix}1 + x & y\\\\y &" " -1 + x\\end{bmatrix}\\right)" settings["mat_delim"] = None assert latex(M, **settings) == "\\begin{bmatrix}1 + x & y\\\\y & -1 + " "x\\end{bmatrix}" assert latex(M) == "\\left(\\begin{smallmatrix}1 + x & y\\\\y & -1 + " "x\\end{smallmatrix}\\right)"
def test_latex_symbols(): Gamma, lmbda, rho = map(Symbol, ('Gamma', 'lambda', 'rho')) mass, volume = map(Symbol, ('mass', 'volume')) assert latex(Gamma + lmbda) == "$\Gamma+\lambda$" assert latex(Gamma * lmbda) == "$\Gamma \lambda$" assert latex(volume * rho == mass) == r"$\rho \cdot \mathrm{volume} = \mathrm{mass}$" assert latex(volume / mass * rho == 1) == r"$\rho \cdot \mathrm{volume} \cdot {\mathrm{mass}}^{(-1)} = 1$" assert latex(mass**3 * volume**3) == r"${\mathrm{mass}}^{3} \cdot {\mathrm{volume}}^{3}$"
def test_custom_symbol_names(): x = Symbol('x') y = Symbol('y') assert latex(x) == "x" assert latex(x, symbol_names={x:"x_i"}) == "x_i" assert latex(x + y, symbol_names={x:"x_i"}) == "x_i + y" assert latex(x**2, symbol_names={x:"x_i"}) == "x_i^{2}" assert latex(x + y, symbol_names={x:"x_i", y:"y_j"}) == "x_i + y_j"
def test_latex_Piecewise(): p = Piecewise((x, x < 1), (x ** 2, True)) assert latex(p) == "\\begin{cases} x & \\text{for}\: x < 1 \\\\x^{2} &" " \\text{otherwise} \\end{cases}" assert ( latex(p, itex=True) == "\\begin{cases} x & \\text{for}\: x \\lt 1 \\\\x^{2} &" " \\text{otherwise} \\end{cases}" ) p = Piecewise((x, x < 0), (0, x >= 0)) assert latex(p) == "\\begin{cases} x & \\text{for}\\: x < 0 \\\\0 &" " \\text{for}\\: x \\geq 0 \\end{cases}"
def test_boolean_args_order(): syms = symbols("a:f") expr = And(*syms) assert latex(expr) == "a \\wedge b \\wedge c \\wedge d \\wedge e \\wedge f" expr = Or(*syms) assert latex(expr) == "a \\vee b \\vee c \\vee d \\vee e \\vee f"
def test_issue469(): beta = Symbol(r'\beta') y = beta+x assert latex(y) in [r'\beta + x', r'x + \beta'] beta = Symbol(r'beta') y = beta+x assert latex(y) in [r'\beta + x', r'x + \beta']
def test_latex_DiracDelta(): assert latex(DiracDelta(x)) == r"\delta\left(x\right)" assert latex(DiracDelta(x)**2) == r"\left(\delta\left(x\right)\right)^{2}" assert latex(DiracDelta(x, 0)) == r"\delta\left(x\right)" assert latex(DiracDelta(x, 5)) == \ r"\delta^{\left( 5 \right)}\left( x \right)" assert latex(DiracDelta(x, 5)**2) == \ r"\left(\delta^{\left( 5 \right)}\left( x \right)\right)^{2}"
def test_latex_Matrix(): M = Matrix([[1 + x, y], [y, x - 1]]) assert latex(M) == "\\left[\\begin{smallmatrix}x + 1 & y\\\\y & x - 1" "\\end{smallmatrix}\\right]" settings = {"mat_str": "bmatrix"} assert latex(M, **settings) == "\\left[\\begin{bmatrix}x + 1 & y\\\\y &" " x - 1\\end{bmatrix}\\right]" settings["mat_delim"] = None assert latex(M, **settings) == "\\begin{bmatrix}x + 1 & y\\\\y & x - 1" "\\end{bmatrix}" assert latex(M) == "\\left[\\begin{smallmatrix}x + 1 & y\\\\y & x - 1" "\\end{smallmatrix}\\right]"
def test_issue469(): beta = Symbol(r"\beta") y = beta + x assert latex(y) in [r"\beta + x", r"x + \beta"] beta = Symbol(r"beta") y = beta + x assert latex(y) in [r"\beta + x", r"x + \beta"]
def test_latex_KroneckerDelta(): assert latex(KroneckerDelta(x, y)) == r"\delta_{x y}" assert latex(KroneckerDelta(x, y)**2) == r"\left(\delta_{x y}\right)^{2}" assert latex(KroneckerDelta(x, y + 1)) == r"\delta_{x, y + 1}" assert latex(KroneckerDelta(x + 1, y)) == r"\delta_{x + 1, y}"
def test_latex_Lambda(): assert latex(Lambda(x, x + 1)) == \ r"\Lambda {\left (x, x + 1 \right )}" assert latex(Lambda((x, y), x + 1)) == \ r"\Lambda {\left (\begin{pmatrix}x, & y\end{pmatrix}, x + 1 \right )}"
def test_latex_numbers(): assert latex(catalan(n)) == r"C_{n}"
def test_latex_union(): assert latex(Union(Interval(0, 1), Interval(2, 3))) == \ r"\left[0, 1\right] \cup \left[2, 3\right]" assert latex(Union(Interval(1, 1), Interval(2, 2), Interval(3, 4))) == \ r"\left\{1, 2\right\} \cup \left[3, 4\right]"
def test_latex_limits(): assert latex(Limit(x, x, oo)) == r"\lim_{x \to \infty} x"
def test_latex_Float(): assert latex(Float(1.0e100)) == r"1.0 \times 10^{100}" assert latex(Float(1.0e-100)) == r"1.0 \times 10^{-100}" assert latex(Float(1.0e-100), mul_symbol="dot") == r"1.0 \cdot 10^{-100}" assert latex(1.0 * oo) == r"\infty" assert latex(-1.0 * oo) == r"- \infty"
def test_constants(): assert str(hbar) == 'hbar' assert pretty(hbar) == 'hbar' assert upretty(hbar) == 'ℏ' assert latex(hbar) == r'\hbar' sT(hbar, "HBar()")
def test_Adjoint(): from sympy.matrices import MatrixSymbol, Adjoint X = MatrixSymbol('X', 2, 2) Y = MatrixSymbol('Y', 2, 2) # Either of these would be fine assert latex(Adjoint(X + Y)) == r'X^\dag + Y^\dag'
def test_Hadamard(): from sympy.matrices import MatrixSymbol, HadamardProduct X = MatrixSymbol('X', 2, 2) Y = MatrixSymbol('Y', 2, 2) assert latex(HadamardProduct(X, Y * Y)) == r'X \circ \left(Y Y\right)' assert latex(HadamardProduct(X, Y) * Y) == r'\left(X \circ Y\right) Y'
def test_Tr(): #TODO: Handle indices A, B = symbols('A B', commutative=False) t = Tr(A * B) assert latex(t) == r'\mbox{Tr}\left(A B\right)'
def test_PolynomialRing(): from sympy.polys.domains import QQ assert latex(QQ[x, y]) == r"\mathbb{Q}\left[x, y\right]" assert latex(QQ.poly_ring(x, y, order="ilex")) == \ r"S_<^{-1}\mathbb{Q}\left[x, y\right]"
def test_noncommutative(): A, B, C = symbols('A,B,C', commutative=False) assert latex(A * B * C**-1) == "A B C^{-1}" assert latex(C**-1 * A * B) == "C^{-1} A B" assert latex(A * C**-1 * B) == "A C^{-1} B"
def test_latex_Heaviside(): assert latex(Heaviside(x)) == r"\theta\left(x\right)" assert latex(Heaviside(x)**2) == r"\left(\theta\left(x\right)\right)^{2}"
def test_gate(): a, b, c, d = symbols('a,b,c,d') uMat = Matrix([[a, b], [c, d]]) q = Qubit(1, 0, 1, 0, 1) g1 = IdentityGate(2) g2 = CGate((3, 0), XGate(1)) g3 = CNotGate(1, 0) g4 = UGate((0, ), uMat) assert str(g1) == '1(2)' assert pretty(g1) == '1 \n 2' assert upretty(g1) == '1 \n 2' assert latex(g1) == r'1_{2}' sT(g1, "IdentityGate(Integer(2))") assert str(g1 * q) == '1(2)*|10101>' ascii_str = \ """\ 1 *|10101>\n\ 2 \ """ ucode_str = \ """\ 1 ⋅❘10101⟩\n\ 2 \ """ assert pretty(g1 * q) == ascii_str assert upretty(g1 * q) == ucode_str assert latex(g1 * q) == r'1_{2} {\left|10101\right\rangle }' sT( g1 * q, "Mul(IdentityGate(Integer(2)), Qubit(Integer(1),Integer(0),Integer(1),Integer(0),Integer(1)))" ) assert str(g2) == 'C((3,0),X(1))' ascii_str = \ """\ C /X \\\n\ 3,0\\ 1/\ """ ucode_str = \ """\ C ⎛X ⎞\n\ 3,0⎝ 1⎠\ """ assert pretty(g2) == ascii_str assert upretty(g2) == ucode_str assert latex(g2) == r'C_{3,0}{\left(X_{1}\right)}' sT(g2, "CGate(Tuple(Integer(3), Integer(0)),XGate(Integer(1)))") assert str(g3) == 'CNOT(1,0)' ascii_str = \ """\ CNOT \n\ 1,0\ """ ucode_str = \ """\ CNOT \n\ 1,0\ """ assert pretty(g3) == ascii_str assert upretty(g3) == ucode_str assert latex(g3) == r'CNOT_{1,0}' sT(g3, "CNotGate(Integer(1),Integer(0))") ascii_str = \ """\ U \n\ 0\ """ ucode_str = \ """\ U \n\ 0\ """ assert str(g4) == \ """\ U((0,),Matrix([\n\ [a, b],\n\ [c, d]]))\ """ assert pretty(g4) == ascii_str assert upretty(g4) == ucode_str assert latex(g4) == r'U_{0}' sT( g4, "UGate(Tuple(Integer(0)),MutableDenseMatrix([[Symbol('a'), Symbol('b')], [Symbol('c'), Symbol('d')]]))" )
def test_latex_inverse(): #tests issue 1030 assert latex(1 / x) == "\\frac{1}{x}" assert latex(1 / (x + y)) in ["\\frac{1}{x + y}", "\\frac{1}{y + x}"]
def test_hilbert(): h1 = HilbertSpace() h2 = ComplexSpace(2) h3 = FockSpace() h4 = L2(Interval(0, oo)) assert str(h1) == 'H' assert pretty(h1) == 'H' assert upretty(h1) == 'H' assert latex(h1) == r'\mathcal{H}' sT(h1, "HilbertSpace()") assert str(h2) == 'C(2)' ascii_str = \ """\ 2\n\ C \ """ ucode_str = \ """\ 2\n\ C \ """ assert pretty(h2) == ascii_str assert upretty(h2) == ucode_str assert latex(h2) == r'\mathcal{C}^{2}' sT(h2, "ComplexSpace(Integer(2))") assert str(h3) == 'F' assert pretty(h3) == 'F' assert upretty(h3) == 'F' assert latex(h3) == r'\mathcal{F}' sT(h3, "FockSpace()") assert str(h4) == 'L2(Interval(0, oo))' ascii_str = \ """\ 2\n\ L \ """ ucode_str = \ """\ 2\n\ L \ """ assert pretty(h4) == ascii_str assert upretty(h4) == ucode_str assert latex(h4) == r'{\mathcal{L}^2}\left( \left[0, \infty\right) \right)' sT(h4, "L2(Interval(Integer(0), oo, false, true))") assert str(h1 + h2) == 'H+C(2)' ascii_str = \ """\ 2\n\ H + C \ """ ucode_str = \ """\ 2\n\ H ⊕ C \ """ assert pretty(h1 + h2) == ascii_str assert upretty(h1 + h2) == ucode_str assert latex(h1 + h2) sT(h1 + h2, "DirectSumHilbertSpace(HilbertSpace(),ComplexSpace(Integer(2)))") assert str(h1 * h2) == "H*C(2)" ascii_str = \ """\ 2\n\ H x C \ """ ucode_str = \ """\ 2\n\ H ⨂ C \ """ assert pretty(h1 * h2) == ascii_str assert upretty(h1 * h2) == ucode_str assert latex(h1 * h2) sT(h1 * h2, "TensorProductHilbertSpace(HilbertSpace(),ComplexSpace(Integer(2)))") assert str(h1**2) == 'H**2' ascii_str = \ """\ x2\n\ H \ """ ucode_str = \ """\ ⨂2\n\ H \ """ assert pretty(h1**2) == ascii_str assert upretty(h1**2) == ucode_str assert latex(h1**2) == r'{\mathcal{H}}^{\otimes 2}' sT(h1**2, "TensorPowerHilbertSpace(HilbertSpace(),Integer(2))")
def test_latex_basic(): assert latex(1 + x) == "x + 1" assert latex(x**2) == "x^{2}" assert latex(x**(1 + x)) == "x^{x + 1}" assert latex(x**3 + x + 1 + x**2) == "x^{3} + x^{2} + x + 1" assert latex(2 * x * y) == "2 x y" assert latex(2 * x * y, mul_symbol='dot') == r"2 \cdot x \cdot y" assert latex(sqrt(x)) == r"\sqrt{x}" assert latex(x**Rational(1, 3)) == r"\sqrt[3]{x}" assert latex(sqrt(x)**3) == r"x^{\frac{3}{2}}" assert latex(sqrt(x), itex=True) == r"\sqrt{x}" assert latex(x**Rational(1, 3), itex=True) == r"\root{3}{x}" assert latex(sqrt(x)**3, itex=True) == r"x^{\frac{3}{2}}" assert latex(x**Rational(3, 4)) == r"x^{\frac{3}{4}}" assert latex(x**Rational(3, 4), fold_frac_powers=True) == "x^{3/4}" assert latex(1.5e20 * x) == r"1.5 \times 10^{20} x" assert latex(1.5e20 * x, mul_symbol='dot') == r"1.5 \cdot 10^{20} \cdot x" assert latex(1 / sin(x)) == r"\frac{1}{\sin{\left (x \right )}}" assert latex(sin(x)**-1) == r"\frac{1}{\sin{\left (x \right )}}" assert latex(~x) == r"\neg x" assert latex(x & y) == r"x \wedge y" assert latex(x & y & z) == r"x \wedge y \wedge z" assert latex(x | y) == r"x \vee y" assert latex(x | y | z) == r"x \vee y \vee z" assert latex((x & y) | z) == r"z \vee \left(x \wedge y\right)" assert latex(Implies(x, y)) == r"x \Rightarrow y" assert latex(~(x >> ~y)) == r"\neg (x \Rightarrow \neg y)" assert latex(~x, symbol_names={x: "x_i"}) == r"\neg x_i" assert latex(x & y, symbol_names={x: "x_i", y: "y_i"}) == \ r"x_i \wedge y_i" assert latex(x & y & z, symbol_names={x: "x_i", y: "y_i", z: "z_i"}) == \ r"x_i \wedge y_i \wedge z_i" assert latex(x | y, symbol_names={x: "x_i", y: "y_i"}) == r"x_i \vee y_i" assert latex(x | y | z, symbol_names={x: "x_i", y: "y_i", z: "z_i"}) == \ r"x_i \vee y_i \vee z_i" assert latex((x & y) | z, symbol_names={x: "x_i", y: "y_i", z: "z_i"}) == \ r"z_i \vee \left(x_i \wedge y_i\right)" assert latex(Implies(x, y), symbol_names={x: "x_i", y: "y_i"}) == \ r"x_i \Rightarrow y_i"
def test_latex_emptyset(): assert latex(S.EmptySet) == r"\emptyset"
def test_latex_TransformationSet(): x = Symbol('x') assert latex(TransformationSet(Lambda(x, x**2), S.Naturals)) == \ r"\left\{x^{2}\; |\; x \in \mathbb{N}\right\}"
def test_latex_RootSum(): assert latex(RootSum(x**5 + x + 3, sin)) == \ r"\operatorname{RootSum} {\left(x^{5} + x + 3, \Lambda {\left (x, \sin{\left (x \right )} \right )}\right)}"
def test_latex_Naturals(): assert latex(S.Naturals) == r"\mathbb{N}" assert latex(S.Integers) == r"\mathbb{Z}"
def test_latex_integrals(): assert latex(Integral(log(x), x)) == r"\int \log{\left (x \right )}\, dx" assert latex(Integral(x**2, (x, 0, 1))) == r"\int_{0}^{1} x^{2}\, dx" assert latex(Integral(x**2, (x, 10, 20))) == r"\int_{10}^{20} x^{2}\, dx" assert latex(Integral(y * x**2, (x, 0, 1), y)) == r"\int\int_{0}^{1} x^{2} y\, dx\, dy" assert latex(Integral(y*x**2, (x, 0, 1), y), mode='equation*') \ == r"\begin{equation*}\int\int\limits_{0}^{1} x^{2} y\, dx\, dy\end{equation*}" assert latex(Integral(y*x**2, (x, 0, 1), y), mode='equation*', itex=True) \ == r"$$\int\int_{0}^{1} x^{2} y\, dx\, dy$$" assert latex(Integral(x, (x, 0))) == r"\int^{0} x\, dx" assert latex(Integral(x * y, x, y)) == r"\iint x y\, dx\, dy" assert latex(Integral(x * y * z, x, y, z)) == r"\iiint x y z\, dx\, dy\, dz" assert latex(Integral(x*y*z*t, x, y, z, t)) == \ r"\iiiint t x y z\, dx\, dy\, dz\, dt" assert latex(Integral(x, x, x, x, x, x, x)) == \ r"\int\int\int\int\int\int x\, dx\, dx\, dx\, dx\, dx\, dx" assert latex(Integral(x, x, y, (z, 0, 1))) == \ r"\int_{0}^{1}\int\int x\, dx\, dy\, dz"
def test_latex_RootOf(): assert latex(RootOf(x**5 + x + 3, 0)) == \ r"\operatorname{RootOf} {\left(x^{5} + x + 3, 0\right)}"
def test_settings(): raises(TypeError, lambda: latex(x * y, method="garbage"))
def test_latex_Range(): assert latex(Range(1, 51)) == \ r'\left\{1, 2, \ldots, 50\right\}' assert latex(Range(1, 4)) == r'\left\{1, 2, 3\right\}'
def test_latex_issue1477(): assert latex(Symbol("beta_13_2")) == r"\beta_{13 2}" assert latex(Symbol("beta_132_20")) == r"\beta_{132 20}" assert latex(Symbol("beta_13")) == r"\beta_{13}" assert latex(Symbol("x_a_b")) == r"x_{a b}" assert latex(Symbol("x_1_2_3")) == r"x_{1 2 3}" assert latex(Symbol("x_a_b1")) == r"x_{a b1}" assert latex(Symbol("x_a_1")) == r"x_{a 1}" assert latex(Symbol("x_1_a")) == r"x_{1 a}" assert latex(Symbol("x_1^aa")) == r"x^{aa}_{1}" assert latex(Symbol("x_1__aa")) == r"x^{aa}_{1}" assert latex(Symbol("x_11^a")) == r"x^{a}_{11}" assert latex(Symbol("x_11__a")) == r"x^{a}_{11}" assert latex(Symbol("x_a_a_a_a")) == r"x_{a a a a}" assert latex(Symbol("x_a_a^a^a")) == r"x^{a a}_{a a}" assert latex(Symbol("x_a_a__a__a")) == r"x^{a a}_{a a}" assert latex(Symbol("alpha_11")) == r"\alpha_{11}" assert latex(Symbol("alpha_11_11")) == r"\alpha_{11 11}" assert latex(Symbol("alpha_alpha")) == r"\alpha_{\alpha}" assert latex(Symbol("alpha^aleph")) == r"\alpha^{\aleph}" assert latex(Symbol("alpha__aleph")) == r"\alpha^{\aleph}"
def test_latex_sets(): for s in (FiniteSet, frozenset, set): assert latex(s([x * y, x**2])) == r"\left\{x^{2}, x y\right\}" assert latex(s(range(1, 6))) == r"\left\{1, 2, 3, 4, 5\right\}" assert latex(s(range(1, 13))) == \ r"\left\{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\right\}"
def test_latex_issue1282(): y = 4 * 4**log(2) assert latex(y) == '4 \\times 4^{\\log{\\left (2 \\right )}}' assert latex(1 / y) == '\\frac{1}{4 \\times 4^{\\log{\\left (2 \\right )}}}'
def test_lamda(): assert latex(Symbol('lamda')) == r"\lambda" assert latex(Symbol('Lamda')) == r"\Lambda"
def test_latex_order(): expr = x**3 + x**2 * y + 3 * x * y**3 + y**4 assert latex(expr, order='lex') == "x^{3} + x^{2} y + 3 x y^{3} + y^{4}" assert latex(expr, order='rev-lex') == "y^{4} + 3 x y^{3} + x^{2} y + x^{3}"