Beispiel #1
0
def test_bra():
    b = Bra('0')

    assert isinstance(b, Bra)
    assert isinstance(b, BraBase)
    assert isinstance(b, StateBase)
    assert isinstance(b, QExpr)

    assert b.label == (Symbol('0'),)
    assert b.hilbert_space == HilbertSpace()
    assert b.is_commutative is False

    # Make sure this doesn't get converted to the number pi.
    b = Bra('pi')
    assert b.label == (Symbol('pi'),)

    b = Bra(x, y)
    assert b.label == (x, y)
    assert b.hilbert_space == HilbertSpace()
    assert b.is_commutative is False

    assert b.dual_class() == Ket
    assert b.dual == Ket(x, y)
    assert b.subs(x, y) == Bra(y, y)

    assert Bra() == Bra('psi')
Beispiel #2
0
def test_bra_ket_dagger():
    x = symbols("x", complex=True)
    k = Ket("k")
    b = Bra("b")
    assert Dagger(k) == Bra("k")
    assert Dagger(b) == Ket("b")
    assert Dagger(k).is_commutative is False

    k2 = Ket("k2")
    e = 2 * I * k + x * k2
    assert Dagger(e) == conjugate(x) * Dagger(k2) - 2 * I * Dagger(k)
Beispiel #3
0
def test_bra_ket_dagger():
    x = symbols('x', complex=True)
    k = Ket('k')
    b = Bra('b')
    assert Dagger(k) == Bra('k')
    assert Dagger(b) == Ket('b')
    assert Dagger(k).is_commutative == False

    k2 = Ket('k2')
    e = 2 * I * k + x * k2
    assert Dagger(e) == conjugate(x) * Dagger(k2) - 2 * I * Dagger(k)
Beispiel #4
0
def test_outer_product():
    k = Ket('k')
    b = Bra('b')
    op = OuterProduct(k, b)

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative is False

    op = k*b

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative is False

    op = 2*k*b

    assert op == Mul(Integer(2), k, b)

    op = 2*(k*b)

    assert op == Mul(Integer(2), OuterProduct(k, b))

    assert Dagger(k*b) == OuterProduct(Dagger(b), Dagger(k))
    assert Dagger(k*b).is_commutative is False

    #test the _eval_trace
    assert Tr(OuterProduct(JzKet(1, 1), JzBra(1, 1))).doit() == 1

    # test scaled kets and bras
    assert OuterProduct(2 * k, b) == 2 * OuterProduct(k, b)
    assert OuterProduct(k, 2 * b) == 2 * OuterProduct(k, b)

    # test sums of kets and bras
    k1, k2 = Ket('k1'), Ket('k2')
    b1, b2 = Bra('b1'), Bra('b2')
    assert (OuterProduct(k1 + k2, b1) ==
            OuterProduct(k1, b1) + OuterProduct(k2, b1))
    assert (OuterProduct(k1, b1 + b2) ==
            OuterProduct(k1, b1) + OuterProduct(k1, b2))
    assert (OuterProduct(1 * k1 + 2 * k2, 3 * b1 + 4 * b2) ==
            3 * OuterProduct(k1, b1) +
            4 * OuterProduct(k1, b2) +
            6 * OuterProduct(k2, b1) +
            8 * OuterProduct(k2, b2))
Beispiel #5
0
def test_outer_product():
    k = Ket('k')
    b = Bra('b')
    op = OuterProduct(k, b)

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative == False

    op = k * b

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative == False

    op = 2 * k * b

    assert op == Mul(Integer(2), k, b)

    op = 2 * (k * b)

    assert op == Mul(Integer(2), OuterProduct(k, b))

    assert Dagger(k * b) == OuterProduct(Dagger(b), Dagger(k))
    assert Dagger(k * b).is_commutative == False
Beispiel #6
0
def test_ket():
    k = Ket('0')

    assert isinstance(k, Ket)
    assert isinstance(k, KetBase)
    assert isinstance(k, StateBase)
    assert isinstance(k, QExpr)

    assert k.label == (Symbol('0'), )
    assert k.hilbert_space == HilbertSpace()
    assert k.is_commutative is False

    # Make sure this doesn't get converted to the number pi.
    k = Ket('pi')
    assert k.label == (Symbol('pi'), )

    k = Ket(x, y)
    assert k.label == (x, y)
    assert k.hilbert_space == HilbertSpace()
    assert k.is_commutative is False

    assert k.dual_class() == Bra
    assert k.dual == Bra(x, y)
    assert k.subs(x, y) == Ket(y, y)

    k = CustomKet()
    assert k == CustomKet("test")

    k = CustomKetMultipleLabels()
    assert k == CustomKetMultipleLabels("r", "theta", "phi")

    assert Ket() == Ket('psi')
Beispiel #7
0
def test_operator():
    a = Operator('A')
    b = Operator('B', Symbol('t'), S(1) / 2)
    inv = a.inv()
    f = Function('f')
    x = symbols('x')
    d = DifferentialOperator(Derivative(f(x), x), f(x))
    op = OuterProduct(Ket(), Bra())
    assert str(a) == 'A'
    assert pretty(a) == 'A'
    assert upretty(a) == u'A'
    assert latex(a) == 'A'
    sT(a, "Operator(Symbol('A'))")
    assert str(inv) == 'A**(-1)'
    ascii_str = \
"""\
 -1\n\
A  \
"""
    ucode_str = \
u"""\
 -1\n\
A  \
"""
    assert pretty(inv) == ascii_str
    assert upretty(inv) == ucode_str
    assert latex(inv) == r'\left(A\right)^{-1}'
    sT(inv, "Pow(Operator(Symbol('A')), Integer(-1))")
    assert str(d) == 'DifferentialOperator(Derivative(f(x), x),f(x))'
    ascii_str = \
"""\
                    /d            \\\n\
DifferentialOperator|--(f(x)),f(x)|\n\
                    \dx           /\
"""
    ucode_str = \
u"""\
                    ⎛d            ⎞\n\
DifferentialOperator⎜──(f(x)),f(x)⎟\n\
                    ⎝dx           ⎠\
"""
    assert pretty(d) == ascii_str
    assert upretty(d) == ucode_str
    assert latex(d) == \
        r'DifferentialOperator\left(\frac{\partial}{\partial x} \operatorname{f}{\left (x \right )},\operatorname{f}{\left (x \right )}\right)'
    sT(
        d,
        "DifferentialOperator(Derivative(Function('f')(Symbol('x')), Symbol('x')),Function('f')(Symbol('x')))"
    )
    assert str(b) == 'Operator(B,t,1/2)'
    assert pretty(b) == 'Operator(B,t,1/2)'
    assert upretty(b) == u'Operator(B,t,1/2)'
    assert latex(b) == r'Operator\left(B,t,\frac{1}{2}\right)'
    sT(b, "Operator(Symbol('B'),Symbol('t'),Rational(1, 2))")
    assert str(op) == '|psi><psi|'
    assert pretty(op) == '|psi><psi|'
    assert upretty(op) == u'❘ψ⟩⟨ψ❘'
    assert latex(op) == r'{\left|\psi\right\rangle }{\left\langle \psi\right|}'
    sT(op, "OuterProduct(Ket(Symbol('psi')),Bra(Symbol('psi')))")
Beispiel #8
0
def test_operator():
    a = Operator("A")
    b = Operator("B", Symbol("t"), S.Half)
    inv = a.inv()
    f = Function("f")
    x = symbols("x")
    d = DifferentialOperator(Derivative(f(x), x), f(x))
    op = OuterProduct(Ket(), Bra())
    assert str(a) == "A"
    assert pretty(a) == "A"
    assert upretty(a) == u"A"
    assert latex(a) == "A"
    sT(a, "Operator(Symbol('A'))")
    assert str(inv) == "A**(-1)"
    ascii_str = """\
 -1\n\
A  \
"""
    ucode_str = u("""\
 -1\n\
A  \
""")
    assert pretty(inv) == ascii_str
    assert upretty(inv) == ucode_str
    assert latex(inv) == r"A^{-1}"
    sT(inv, "Pow(Operator(Symbol('A')), Integer(-1))")
    assert str(d) == "DifferentialOperator(Derivative(f(x), x),f(x))"
    ascii_str = """\
                    /d            \\\n\
DifferentialOperator|--(f(x)),f(x)|\n\
                    \\dx           /\
"""
    ucode_str = u("""\
                    ⎛d            ⎞\n\
DifferentialOperator⎜──(f(x)),f(x)⎟\n\
                    ⎝dx           ⎠\
""")
    assert pretty(d) == ascii_str
    assert upretty(d) == ucode_str
    assert (
        latex(d) ==
        r"DifferentialOperator\left(\frac{d}{d x} f{\left(x \right)},f{\left(x \right)}\right)"
    )
    sT(
        d,
        "DifferentialOperator(Derivative(Function('f')(Symbol('x')), Tuple(Symbol('x'), Integer(1))),Function('f')(Symbol('x')))",
    )
    assert str(b) == "Operator(B,t,1/2)"
    assert pretty(b) == "Operator(B,t,1/2)"
    assert upretty(b) == u"Operator(B,t,1/2)"
    assert latex(b) == r"Operator\left(B,t,\frac{1}{2}\right)"
    sT(b, "Operator(Symbol('B'),Symbol('t'),Rational(1, 2))")
    assert str(op) == "|psi><psi|"
    assert pretty(op) == "|psi><psi|"
    assert upretty(op) == u"❘ψ⟩⟨ψ❘"
    assert latex(op) == r"{\left|\psi\right\rangle }{\left\langle \psi\right|}"
    sT(op, "OuterProduct(Ket(Symbol('psi')),Bra(Symbol('psi')))")
Beispiel #9
0
def test_innerproduct():
    k = Ket('k')
    b = Bra('b')
    ip = InnerProduct(b, k)
    assert isinstance(ip, InnerProduct)
    assert ip.bra == b
    assert ip.ket == k
    assert b*k == InnerProduct(b, k)
    assert k*(b*k)*b == k*InnerProduct(b, k)*b
    assert InnerProduct(b, k).subs(b, Dagger(k)) == Dagger(k)*k
Beispiel #10
0
def convert_atom(atom):
    if atom.LETTER():
        subscriptName = ''
        if atom.subexpr():
            subscript = None
            if atom.subexpr().expr():  # subscript is expr
                subscript = convert_expr(atom.subexpr().expr())
            else:  # subscript is atom
                subscript = convert_atom(atom.subexpr().atom())
            subscriptName = '_{' + StrPrinter().doprint(subscript) + '}'
        return sympy.Symbol(atom.LETTER().getText() + subscriptName)
    elif atom.SYMBOL():
        s = atom.SYMBOL().getText()[1:]
        if s == "infty":
            return sympy.oo
        else:
            if atom.subexpr():
                subscript = None
                if atom.subexpr().expr():  # subscript is expr
                    subscript = convert_expr(atom.subexpr().expr())
                else:  # subscript is atom
                    subscript = convert_atom(atom.subexpr().atom())
                subscriptName = StrPrinter().doprint(subscript)
                s += '_{' + subscriptName + '}'
            return sympy.Symbol(s)
    elif atom.NUMBER():
        s = atom.NUMBER().getText().replace(",", "")
        return sympy.Number(s)
    elif atom.DIFFERENTIAL():
        var = get_differential_var(atom.DIFFERENTIAL())
        return sympy.Symbol('d' + var.name)
    elif atom.mathit():
        text = rule2text(atom.mathit().mathit_text())
        return sympy.Symbol(text)
    elif atom.frac():
        return convert_frac(atom.frac())
    elif atom.binom():
        return convert_binom(atom.binom())
    elif atom.bra():
        val = convert_expr(atom.bra().expr())
        return Bra(val)
    elif atom.ket():
        val = convert_expr(atom.ket().expr())
        return Ket(val)
Beispiel #11
0
def test_bra():
    b = Bra('0')

    assert isinstance(b, Bra)
    assert isinstance(b, BraBase)
    assert isinstance(b, StateBase)
    assert isinstance(b, QExpr)

    assert b.label == (Symbol('0'), )
    assert b.hilbert_space == HilbertSpace()
    assert b.is_commutative == False

    # Make sure this doesn't get converted to the number pi.
    b = Bra('pi')
    assert b.label == (Symbol('pi'), )

    b = Bra(x, y)
    assert b.label == (x, y)
    assert b.hilbert_space == HilbertSpace()
    assert b.is_commutative == False

    assert b.dual_class == Ket
    assert b.dual == Ket(x, y)
    assert b.subs(x, y) == Bra(y, y)
Beispiel #12
0
def test_outer_product():
    k = Ket('k')
    b = Bra('b')
    op = OuterProduct(k, b)

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative is False

    op = k * b

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative is False

    op = 2 * k * b

    assert op == Mul(Integer(2), k, b)

    op = 2 * (k * b)

    assert op == Mul(Integer(2), OuterProduct(k, b))

    assert Dagger(k * b) == OuterProduct(Dagger(b), Dagger(k))
    assert Dagger(k * b).is_commutative is False

    #test the _eval_trace
    assert Tr(OuterProduct(JzKet(1, 1), JzBra(1, 1))).doit() == 1
Beispiel #13
0
def test_ket():
    k = Ket('0')

    assert isinstance(k, Ket)
    assert isinstance(k, KetBase)
    assert isinstance(k, StateBase)
    assert isinstance(k, QExpr)

    assert k.label == (Symbol('0'), )
    assert k.hilbert_space == HilbertSpace()
    assert k.is_commutative == False

    # Make sure this doesn't get converted to the number pi.
    k = Ket('pi')
    assert k.label == (Symbol('pi'), )

    k = Ket(x, y)
    assert k.label == (x, y)
    assert k.hilbert_space == HilbertSpace()
    assert k.is_commutative == False

    assert k.dual_class == Bra
    assert k.dual == Bra(x, y)
    assert k.subs(x, y) == Ket(y, y)
Beispiel #14
0
def test_innerproduct_dagger():
    k = Ket("k")
    b = Bra("b")
    ip = b * k
    assert Dagger(ip) == Dagger(k) * Dagger(b)
Beispiel #15
0
def test_state():
    x = symbols("x")
    bra = Bra()
    ket = Ket()
    bra_tall = Bra(x / 2)
    ket_tall = Ket(x / 2)
    tbra = TimeDepBra()
    tket = TimeDepKet()
    assert str(bra) == "<psi|"
    assert pretty(bra) == "<psi|"
    assert upretty(bra) == u"⟨ψ❘"
    assert latex(bra) == r"{\left\langle \psi\right|}"
    sT(bra, "Bra(Symbol('psi'))")
    assert str(ket) == "|psi>"
    assert pretty(ket) == "|psi>"
    assert upretty(ket) == u"❘ψ⟩"
    assert latex(ket) == r"{\left|\psi\right\rangle }"
    sT(ket, "Ket(Symbol('psi'))")
    assert str(bra_tall) == "<x/2|"
    ascii_str = """\
 / |\n\
/ x|\n\
\\ -|\n\
 \\2|\
"""
    ucode_str = u("""\
 ╱ │\n\
╱ x│\n\
╲ ─│\n\
 ╲2│\
""")
    assert pretty(bra_tall) == ascii_str
    assert upretty(bra_tall) == ucode_str
    assert latex(bra_tall) == r"{\left\langle \frac{x}{2}\right|}"
    sT(bra_tall, "Bra(Mul(Rational(1, 2), Symbol('x')))")
    assert str(ket_tall) == "|x/2>"
    ascii_str = """\
| \\ \n\
|x \\\n\
|- /\n\
|2/ \
"""
    ucode_str = u("""\
│ ╲ \n\
│x ╲\n\
│─ ╱\n\
│2╱ \
""")
    assert pretty(ket_tall) == ascii_str
    assert upretty(ket_tall) == ucode_str
    assert latex(ket_tall) == r"{\left|\frac{x}{2}\right\rangle }"
    sT(ket_tall, "Ket(Mul(Rational(1, 2), Symbol('x')))")
    assert str(tbra) == "<psi;t|"
    assert pretty(tbra) == u"<psi;t|"
    assert upretty(tbra) == u"⟨ψ;t❘"
    assert latex(tbra) == r"{\left\langle \psi;t\right|}"
    sT(tbra, "TimeDepBra(Symbol('psi'),Symbol('t'))")
    assert str(tket) == "|psi;t>"
    assert pretty(tket) == "|psi;t>"
    assert upretty(tket) == u"❘ψ;t⟩"
    assert latex(tket) == r"{\left|\psi;t\right\rangle }"
    sT(tket, "TimeDepKet(Symbol('psi'),Symbol('t'))")
Beispiel #16
0
def test_innerproduct_dagger():
    k = Ket('k')
    b = Bra('b')
    ip = b*k
    assert Dagger(ip) == Dagger(k)*Dagger(b)
Beispiel #17
0
def test_state():
    x = symbols('x')
    bra = Bra()
    ket = Ket()
    bra_tall = Bra(x / 2)
    ket_tall = Ket(x / 2)
    tbra = TimeDepBra()
    tket = TimeDepKet()
    assert str(bra) == '<psi|'
    assert pretty(bra) == '<psi|'
    assert upretty(bra) == u'⟨ψ❘'
    assert latex(bra) == r'{\left\langle \psi\right|}'
    sT(bra, "Bra(Symbol('psi'))")
    assert str(ket) == '|psi>'
    assert pretty(ket) == '|psi>'
    assert upretty(ket) == u'❘ψ⟩'
    assert latex(ket) == r'{\left|\psi\right\rangle }'
    sT(ket, "Ket(Symbol('psi'))")
    assert str(bra_tall) == '<x/2|'
    ascii_str = \
"""\
 / |\n\
/ x|\n\
\\ -|\n\
 \\2|\
"""
    ucode_str = \
u"""\
 ╱ │\n\
╱ x│\n\
╲ ─│\n\
 ╲2│\
"""
    assert pretty(bra_tall) == ascii_str
    assert upretty(bra_tall) == ucode_str
    assert latex(bra_tall) == r'{\left\langle \frac{1}{2} x\right|}'
    sT(bra_tall, "Bra(Mul(Rational(1, 2), Symbol('x')))")
    assert str(ket_tall) == '|x/2>'
    ascii_str = \
"""\
| \\ \n\
|x \\\n\
|- /\n\
|2/ \
"""
    ucode_str = \
u"""\
│ ╲ \n\
│x ╲\n\
│─ ╱\n\
│2╱ \
"""
    assert pretty(ket_tall) == ascii_str
    assert upretty(ket_tall) == ucode_str
    assert latex(ket_tall) == r'{\left|\frac{1}{2} x\right\rangle }'
    sT(ket_tall, "Ket(Mul(Rational(1, 2), Symbol('x')))")
    assert str(tbra) == '<psi;t|'
    assert pretty(tbra) == u'<psi;t|'
    assert upretty(tbra) == u'⟨ψ;t❘'
    assert latex(tbra) == r'{\left\langle \psi;t\right|}'
    sT(tbra, "TimeDepBra(Symbol('psi'),Symbol('t'))")
    assert str(tket) == '|psi;t>'
    assert pretty(tket) == '|psi;t>'
    assert upretty(tket) == u'❘ψ;t⟩'
    assert latex(tket) == r'{\left|\psi;t\right\rangle }'
    sT(tket, "TimeDepKet(Symbol('psi'),Symbol('t'))")
Beispiel #18
0
def test_innerproduct():
    x = symbols('x')
    ip1 = InnerProduct(Bra(), Ket())
    ip2 = InnerProduct(TimeDepBra(), TimeDepKet())
    ip3 = InnerProduct(JzBra(1, 1), JzKet(1, 1))
    ip4 = InnerProduct(JzBraCoupled(1, 1, (1, 1)), JzKetCoupled(1, 1, (1, 1)))
    ip_tall1 = InnerProduct(Bra(x / 2), Ket(x / 2))
    ip_tall2 = InnerProduct(Bra(x), Ket(x / 2))
    ip_tall3 = InnerProduct(Bra(x / 2), Ket(x))
    assert str(ip1) == '<psi|psi>'
    assert pretty(ip1) == '<psi|psi>'
    assert upretty(ip1) == u'⟨ψ❘ψ⟩'
    assert latex(
        ip1) == r'\left\langle \psi \right. {\left|\psi\right\rangle }'
    sT(ip1, "InnerProduct(Bra(Symbol('psi')),Ket(Symbol('psi')))")
    assert str(ip2) == '<psi;t|psi;t>'
    assert pretty(ip2) == '<psi;t|psi;t>'
    assert upretty(ip2) == u'⟨ψ;t❘ψ;t⟩'
    assert latex(ip2) == \
        r'\left\langle \psi;t \right. {\left|\psi;t\right\rangle }'
    sT(
        ip2,
        "InnerProduct(TimeDepBra(Symbol('psi'),Symbol('t')),TimeDepKet(Symbol('psi'),Symbol('t')))"
    )
    assert str(ip3) == "<1,1|1,1>"
    assert pretty(ip3) == '<1,1|1,1>'
    assert upretty(ip3) == u'⟨1,1❘1,1⟩'
    assert latex(ip3) == r'\left\langle 1,1 \right. {\left|1,1\right\rangle }'
    sT(
        ip3,
        "InnerProduct(JzBra(Integer(1),Integer(1)),JzKet(Integer(1),Integer(1)))"
    )
    assert str(ip4) == "<1,1,j1=1,j2=1|1,1,j1=1,j2=1>"
    assert pretty(ip4) == '<1,1,j1=1,j2=1|1,1,j1=1,j2=1>'
    assert upretty(ip4) == u'⟨1,1,j₁=1,j₂=1❘1,1,j₁=1,j₂=1⟩'
    assert latex(ip4) == \
        r'\left\langle 1,1,j_{1}=1,j_{2}=1 \right. {\left|1,1,j_{1}=1,j_{2}=1\right\rangle }'
    sT(
        ip4,
        "InnerProduct(JzBraCoupled(Integer(1),Integer(1),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1)))),JzKetCoupled(Integer(1),Integer(1),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1)))))"
    )
    assert str(ip_tall1) == '<x/2|x/2>'
    ascii_str = \
"""\
 / | \\ \n\
/ x|x \\\n\
\\ -|- /\n\
 \\2|2/ \
"""
    ucode_str = \
u"""\
 ╱ │ ╲ \n\
╱ x│x ╲\n\
╲ ─│─ ╱\n\
 ╲2│2╱ \
"""
    assert pretty(ip_tall1) == ascii_str
    assert upretty(ip_tall1) == ucode_str
    assert latex(ip_tall1) == \
        r'\left\langle \frac{1}{2} x \right. {\left|\frac{1}{2} x\right\rangle }'
    sT(
        ip_tall1,
        "InnerProduct(Bra(Mul(Rational(1, 2), Symbol('x'))),Ket(Mul(Rational(1, 2), Symbol('x'))))"
    )
    assert str(ip_tall2) == '<x|x/2>'
    ascii_str = \
"""\
 / | \\ \n\
/  |x \\\n\
\\ x|- /\n\
 \\ |2/ \
"""
    ucode_str = \
u"""\
 ╱ │ ╲ \n\
╱  │x ╲\n\
╲ x│─ ╱\n\
 ╲ │2╱ \
"""
    assert pretty(ip_tall2) == ascii_str
    assert upretty(ip_tall2) == ucode_str
    assert latex(ip_tall2) == \
        r'\left\langle x \right. {\left|\frac{1}{2} x\right\rangle }'
    sT(ip_tall2,
       "InnerProduct(Bra(Symbol('x')),Ket(Mul(Rational(1, 2), Symbol('x'))))")
    assert str(ip_tall3) == '<x/2|x>'
    ascii_str = \
"""\
 / | \\ \n\
/ x|  \\\n\
\\ -|x /\n\
 \\2| / \
"""
    ucode_str = \
u"""\
 ╱ │ ╲ \n\
╱ x│  ╲\n\
╲ ─│x ╱\n\
 ╲2│ ╱ \
"""
    assert pretty(ip_tall3) == ascii_str
    assert upretty(ip_tall3) == ucode_str
    assert latex(ip_tall3) == \
        r'\left\langle \frac{1}{2} x \right. {\left|x\right\rangle }'
    sT(ip_tall3,
       "InnerProduct(Bra(Mul(Rational(1, 2), Symbol('x'))),Ket(Symbol('x')))")
Beispiel #19
0
 (r"\left\{x + y\right\} z", _Mul(_Add(x, y), z)),
 (r"1+1", _Add(1, 1)),
 (r"0+1", _Add(0, 1)),
 (r"1*2", _Mul(1, 2)),
 (r"0*1", _Mul(0, 1)),
 (r"x = y", Eq(x, y)),
 (r"x \neq y", Ne(x, y)),
 (r"x < y", Lt(x, y)),
 (r"x > y", Gt(x, y)),
 (r"x \leq y", Le(x, y)),
 (r"x \geq y", Ge(x, y)),
 (r"x \le y", Le(x, y)),
 (r"x \ge y", Ge(x, y)),
 (r"\lfloor x \rfloor", floor(x)),
 (r"\lceil x \rceil", ceiling(x)),
 (r"\langle x |", Bra('x')),
 (r"| x \rangle", Ket('x')),
 (r"\sin \theta", sin(theta)),
 (r"\sin(\theta)", sin(theta)),
 (r"\sin^{-1} a", asin(a)),
 (r"\sin a \cos b", _Mul(sin(a), cos(b))),
 (r"\sin \cos \theta", sin(cos(theta))),
 (r"\sin(\cos \theta)", sin(cos(theta))),
 (r"\frac{a}{b}", a / b),
 (r"\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))),
 (r"\frac{7}{3}", _Mul(7, _Pow(3, -1))),
 (r"(\csc x)(\sec y)", csc(x) * sec(y)),
 (r"\lim_{x \to 3} a", Limit(a, x, 3)),
 (r"\lim_{x \rightarrow 3} a", Limit(a, x, 3)),
 (r"\lim_{x \Rightarrow 3} a", Limit(a, x, 3)),
 (r"\lim_{x \longrightarrow 3} a", Limit(a, x, 3)),
 ("\\left\\{x + y\\right\\} z", _Mul(_Add(x, y), z)),
 ("1+1", Add(1, 1, evaluate=False)),
 ("0+1", Add(0, 1, evaluate=False)),
 ("1*2", Mul(1, 2, evaluate=False)),
 ("0*1", Mul(0, 1, evaluate=False)),
 ("x = y", Eq(x, y)),
 ("x \\neq y", Ne(x, y)),
 ("x < y", Lt(x, y)),
 ("x > y", Gt(x, y)),
 ("x \\leq y", Le(x, y)),
 ("x \\geq y", Ge(x, y)),
 ("x \\le y", Le(x, y)),
 ("x \\ge y", Ge(x, y)),
 ("\\lfloor x \\rfloor", floor(x)),
 ("\\lceil x \\rceil", ceiling(x)),
 ("\\langle x |", Bra('x')),
 ("| x \\rangle", Ket('x')),
 ("\\sin \\theta", sin(theta)),
 ("\\sin(\\theta)", sin(theta)),
 ("\\sin^{-1} a", asin(a)),
 ("\\sin a \\cos b", _Mul(sin(a), cos(b))),
 ("\\sin \\cos \\theta", sin(cos(theta))),
 ("\\sin(\\cos \\theta)", sin(cos(theta))),
 ("\\frac{a}{b}", a / b),
 ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))),
 ("\\frac{7}{3}", _Mul(7, _Pow(3, -1))),
 ("(\\csc x)(\\sec y)", csc(x) * sec(y)),
 ("\\lim_{x \\to 3} a", Limit(a, x, 3)),
 ("\\lim_{x \\rightarrow 3} a", Limit(a, x, 3)),
 ("\\lim_{x \\Rightarrow 3} a", Limit(a, x, 3)),
 ("\\lim_{x \\longrightarrow 3} a", Limit(a, x, 3)),
Beispiel #21
0
def test_operator():
    a = Operator('A')
    b = Operator('B', Symbol('t'), S(1) / 2)
    inv = a.inv()
    f = Function('f')
    x = symbols('x')
    d = DifferentialOperator(Derivative(f(x), x), f(x))
    op = OuterProduct(Ket(), Bra())
    assert str(a) == 'A'
    assert pretty(a) == 'A'
    assert upretty(a) == 'A'
    assert latex(a) == 'A'
    sT(a, "Operator(Symbol('A'))")
    assert str(inv) == 'A**(-1)'
    ascii_str = \
"""\
 -1\n\
A  \
"""
    ucode_str = \
"""\
 -1\n\
A  \
"""
    assert pretty(inv) == ascii_str
    assert upretty(inv) == ucode_str
    #FIXME ajgpitch 2019-09-22
    # It's not clear to me why these extra brackets would be wanted / needed
    #assert latex(inv) == r'\left(A\right)^{-1}'
    # This renders okay
    assert latex(inv) == r'A^{-1}'
    sT(inv, "Pow(Operator(Symbol('A')), Integer(-1))")
    assert str(d) == 'DifferentialOperator(Derivative(f(x), x),f(x))'
    ascii_str = \
"""\
                    /d            \\\n\
DifferentialOperator|--(f(x)),f(x)|\n\
                    \dx           /\
"""
    ucode_str = \
"""\
                    ⎛d            ⎞\n\
DifferentialOperator⎜──(f(x)),f(x)⎟\n\
                    ⎝dx           ⎠\
"""
    assert pretty(d) == ascii_str
    assert upretty(d) == ucode_str
    assert latex(d) == \
        r'DifferentialOperator\left(\frac{d}{d x} f{\left(x \right)},f{\left(x \right)}\right)'
    #FIXME: ajgpitch 2019-09-22
    # Not clear why this is failing
    # `Tuple(Symbol('x'), Integer(1))` seems to enter into srepr(expr)
    # for some reason.
    sT(
        d,
        "DifferentialOperator(Derivative(Function('f')(Symbol('x')), Symbol('x')),Function('f')(Symbol('x')))"
    )
    assert str(b) == 'Operator(B,t,1/2)'
    assert pretty(b) == 'Operator(B,t,1/2)'
    assert upretty(b) == 'Operator(B,t,1/2)'
    assert latex(b) == r'Operator\left(B,t,\frac{1}{2}\right)'
    sT(b, "Operator(Symbol('B'),Symbol('t'),Rational(1, 2))")
    assert str(op) == '|psi><psi|'
    assert pretty(op) == '|psi><psi|'
    assert upretty(op) == '❘ψ⟩⟨ψ❘'
    assert latex(op) == r'{\left|\psi\right\rangle }{\left\langle \psi\right|}'
    sT(op, "OuterProduct(Ket(Symbol('psi')),Bra(Symbol('psi')))")