def get_matrix_product_form(self): self.ket_row = Matrix([Ket(s) for s in self.state_components]).transpose() self.bra_col = Matrix([Bra(s) for s in self.state_components]) self.form = UnevaluatedExpr(self.ket_row) * UnevaluatedExpr( self.symmetrized_mat) * UnevaluatedExpr(self.bra_col) return self.form
def format_TeX_mat(self): tex_mat = deepcopy(self.original_mat) for c, i in enumerate(tex_mat): tex_mat[c] = format_matrix_element(i) tex_form = UnevaluatedExpr( self.ket_row) * UnevaluatedExpr(tex_mat) * UnevaluatedExpr( self.bra_col) tex_formatted_form = R'$\hat{H}=' + printing.latex( tex_form, mat_delim='(', mat_str='matrix') + '$' return tex_formatted_form
def test_lcm_usual(): assert_equal("\\lcm(6, 4)", lcm(6, 4)) assert_equal("\\lcm(4, 6)", lcm(4, 6)) assert_equal("\\lcm(2, 2)", lcm(2, 2)) assert_equal("\\lcm(0, 21)", UnevaluatedExpr(lcm(0, 21))) assert_equal("\\lcm(21, 0)", UnevaluatedExpr(lcm(21, 0))) assert_equal("\\lcm(0, 0)", UnevaluatedExpr(lcm(0, 0))) assert_equal("\\lcm(9, 21)", lcm(9, 21)) assert_equal("\\lcm(6128, 24)", lcm(6128, 24)) assert_equal("\\lcm(24, 6128)", lcm(24, 6128)) assert_equal("\\lcm(1E20, 1000000)", lcm(Rational('1E20'), 1000000)) assert_equal("\\lcm(128*10^32, 1)", lcm(Rational('128E32'), 1)) assert_equal("\\operatorname{lcm}(6, 4)", lcm(6, 4)) assert_equal("\\operatorname{lcm}(4, 6)", lcm(4, 6)) assert_equal("\\operatorname{lcm}(2, 2)", lcm(2, 2)) assert_equal("\\operatorname{lcm}(0, 21)", UnevaluatedExpr(lcm(0, 21))) assert_equal("\\operatorname{lcm}(21, 0)", UnevaluatedExpr(lcm(21, 0))) assert_equal("\\operatorname{lcm}(0, 0)", UnevaluatedExpr(lcm(0, 0))) assert_equal("\\operatorname{lcm}(9, 21)", lcm(9, 21)) assert_equal("\\operatorname{lcm}(6128, 24)", lcm(6128, 24)) assert_equal("\\operatorname{lcm}(24, 6128)", lcm(24, 6128)) assert_equal("\\operatorname{lcm}(1E20, 1000000)", lcm(Rational('1E20'), 1000000)) assert_equal("\\operatorname{lcm}(128*10^32, 1)", lcm(Rational('128E32'), 1))
def test_variable_multi_arg_func(): assert_equal( "\\gcd(\\variable{x}, \\variable{y})", UnevaluatedExpr( gcd(Symbol('x' + hashlib.md5('x'.encode()).hexdigest(), real=True), Symbol('y' + hashlib.md5('y'.encode()).hexdigest(), real=True)))) assert_equal( "\\lcm(\\variable{x}, \\variable{y})", UnevaluatedExpr( lcm(Symbol('x' + hashlib.md5('x'.encode()).hexdigest(), real=True), Symbol('y' + hashlib.md5('y'.encode()).hexdigest(), real=True)))) assert_equal( "\\max(\\variable{x}, \\variable{y})", Max(Symbol('x' + hashlib.md5('x'.encode()).hexdigest(), real=True), Symbol('y' + hashlib.md5('y'.encode()).hexdigest(), real=True), evaluate=False)) assert_equal( "\\min(\\variable{x}, \\variable{y})", Min(Symbol('x' + hashlib.md5('x'.encode()).hexdigest(), real=True), Symbol('y' + hashlib.md5('y'.encode()).hexdigest(), real=True), evaluate=False))
def generate_log(var, scaling): if scaling: if var.is_not_scaled: return ExprState(math.log(var.expr), var.ibits) # adjust to Q15 fraction shift = 15 - var.length if shift == 0: subexpr = log(var.expr) else: subexpr = log(ATLShift(var.expr, shift)) # The number is now a Q15 fraction so the scale is multiplied by Q15 minus the correction cor_scale = int( math.floor( math.log(var.scale * 2**(15 - shift)) * pow(2, 15) + 0.5)) # Scale is now 1 * Q15 out_scale = pow(2, -15) return ExprState(UnevaluatedExpr(Add(subexpr, cor_scale)), int(math.ceil((var.ibits + shift) / 2)), q=0, scale=out_scale) return ExprState(log(var.expr), int(math.ceil(var.ibits / 2)))
def test_gcd_usual(): assert_equal("\\gcd(18, 3)", gcd(18, 3)) assert_equal("\\gcd(3, 18)", gcd(3, 18)) assert_equal("\\gcd(2, 2)", gcd(2, 2)) assert_equal("\\gcd(0, 21)", UnevaluatedExpr(gcd(0, 21))) assert_equal("\\gcd(21, 0)", UnevaluatedExpr(gcd(21, 0))) assert_equal("\\gcd(0, 0)", UnevaluatedExpr(gcd(0, 0))) assert_equal("\\gcd(6128, 24)", gcd(6128, 24)) assert_equal("\\gcd(24, 6128)", gcd(24, 6128)) assert_equal("\\gcd(1E20, 1000000)", gcd(Rational('1E20'), 1000000)) assert_equal("\\gcd(128*10^32, 1)", gcd(Rational('128E32'), 1)) assert_equal("\\operatorname{gcd}(18, 3)", gcd(18, 3)) assert_equal("\\operatorname{gcd}(3, 18)", gcd(3, 18)) assert_equal("\\operatorname{gcd}(2, 2)", gcd(2, 2)) assert_equal("\\operatorname{gcd}(0, 21)", UnevaluatedExpr(gcd(0, 21))) assert_equal("\\operatorname{gcd}(21, 0)", UnevaluatedExpr(gcd(21, 0))) assert_equal("\\operatorname{gcd}(0, 0)", UnevaluatedExpr(gcd(0, 0))) assert_equal("\\operatorname{gcd}(6128, 24)", gcd(6128, 24)) assert_equal("\\operatorname{gcd}(24, 6128)", gcd(24, 6128)) assert_equal("\\operatorname{gcd}(1E20, 1000000)", gcd(Rational('1E20'), 1000000)) assert_equal("\\operatorname{gcd}(128*10^32, 1)", gcd(Rational('128E32'), 1))
def test_UnevaluatedExpr(): a, b = symbols("a b") expr1 = 2*UnevaluatedExpr(a+b) assert str(expr1) == "2*(a + b)"
class TestAllGood(object): # These latex strings should parse to the corresponding SymPy expression GOOD_PAIRS = [ ("0", Rational(0)), ("1", Rational(1)), ("-3.14", Rational(-314, 100)), ("5-3", _Add(5, _Mul(-1, 3))), ("(-7.13)(1.5)", _Mul(Rational('-7.13'), Rational('1.5'))), ("\\left(-7.13\\right)\\left(1.5\\right)", _Mul(Rational('-7.13'), Rational('1.5'))), ("x", x), ("2x", 2 * x), ("x^2", x**2), ("x^{3 + 1}", x**_Add(3, 1)), ("x^{\\left\\{3 + 1\\right\\}}", x**_Add(3, 1)), ("-3y + 2x", _Add(_Mul(2, x), Mul(-1, 3, y, evaluate=False))), ("-c", -c), ("a \\cdot b", a * b), ("a / b", a / b), ("a \\div b", a / b), ("a + b", a + b), ("a + b - a", Add(a, b, _Mul(-1, a), evaluate=False)), ("a^2 + b^2 = c^2", Eq(a**2 + b**2, c**2)), ("a^2 + b^2 != 2c^2", Ne(a**2 + b**2, 2 * c**2)), ("a\\mod b", Mod(a, b)), ("\\sin \\theta", sin(theta)), ("\\sin(\\theta)", sin(theta)), ("\\sin\\left(\\theta\\right)", 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))), ("\\arcsin(a)", asin(a)), ("\\arccos(a)", acos(a)), ("\\arctan(a)", atan(a)), ("\\sinh(a)", sinh(a)), ("\\cosh(a)", cosh(a)), ("\\tanh(a)", tanh(a)), ("\\sinh^{-1}(a)", asinh(a)), ("\\cosh^{-1}(a)", acosh(a)), ("\\tanh^{-1}(a)", atanh(a)), ("\\arcsinh(a)", asinh(a)), ("\\arccosh(a)", acosh(a)), ("\\arctanh(a)", atanh(a)), ("\\arsinh(a)", asinh(a)), ("\\arcosh(a)", acosh(a)), ("\\artanh(a)", atanh(a)), ("\\operatorname{arcsinh}(a)", asinh(a)), ("\\operatorname{arccosh}(a)", acosh(a)), ("\\operatorname{arctanh}(a)", atanh(a)), ("\\operatorname{arsinh}(a)", asinh(a)), ("\\operatorname{arcosh}(a)", acosh(a)), ("\\operatorname{artanh}(a)", atanh(a)), ("\\operatorname{gcd}(a, b)", UnevaluatedExpr(gcd(a, b))), ("\\operatorname{lcm}(a, b)", UnevaluatedExpr(lcm(a, b))), ("\\operatorname{gcd}(a,b)", UnevaluatedExpr(gcd(a, b))), ("\\operatorname{lcm}(a,b)", UnevaluatedExpr(lcm(a, b))), ("\\operatorname{floor}(a)", floor(a)), ("\\operatorname{ceil}(b)", ceiling(b)), ("\\cos^2(x)", cos(x)**2), ("\\cos(x)^2", cos(x)**2), ("\\gcd(a, b)", UnevaluatedExpr(gcd(a, b))), ("\\lcm(a, b)", UnevaluatedExpr(lcm(a, b))), ("\\gcd(a,b)", UnevaluatedExpr(gcd(a, b))), ("\\lcm(a,b)", UnevaluatedExpr(lcm(a, b))), ("\\floor(a)", floor(a)), ("\\ceil(b)", ceiling(b)), ("\\max(a, b)", Max(a, b)), ("\\min(a, b)", Min(a, b)), ("\\frac{a}{b}", a / b), ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), ("\\frac{7}{3}", Rational(7, 3)), ("(\\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)), ("\\lim_{x \\Longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\to 3^{+}} a", Limit(a, x, 3, dir='+')), ("\\lim_{x \\to 3^{-}} a", Limit(a, x, 3, dir='-')), ("\\infty", oo), ("\\infty\\%", oo), ("\\$\\infty", oo), ("-\\infty", -oo), ("-\\infty\\%", -oo), ("-\\$\\infty", -oo), ("\\lim_{x \\to \\infty} \\frac{1}{x}", Limit(_Mul(1, _Pow(x, -1)), x, oo)), ("\\frac{d}{dx} x", Derivative(x, x)), ("\\frac{d}{dt} x", Derivative(x, t)), # ("f(x)", f(x)), # ("f(x, y)", f(x, y)), # ("f(x, y, z)", f(x, y, z)), # ("\\frac{d f(x)}{dx}", Derivative(f(x), x)), # ("\\frac{d\\theta(x)}{dx}", Derivative(theta(x), x)), ("|x|", _Abs(x)), ("\\left|x\\right|", _Abs(x)), ("||x||", _Abs(_Abs(x))), ("|x||y|", _Abs(x) * _Abs(y)), ("||x||y||", _Abs(_Abs(x) * _Abs(y))), ("\\lfloor x\\rfloor", floor(x)), ("\\lceil y\\rceil", ceiling(y)), ("\\pi^{|xy|}", pi**_Abs(x * y)), ("\\frac{\\pi}{3}", _Mul(pi, _Pow(3, -1))), ("\\sin{\\frac{\\pi}{2}}", sin(_Mul(pi, _Pow(2, -1)), evaluate=False)), ("a+bI", a + I * b), ("e^{I\\pi}", Integer(-1)), ("\\int x dx", Integral(x, x)), ("\\int x d\\theta", Integral(x, theta)), ("\\int (x^2 - y)dx", Integral(x**2 - y, x)), ("\\int x + a dx", Integral(_Add(x, a), x)), ("\\int da", Integral(1, a)), ("\\int_0^7 dx", Integral(1, (x, 0, 7))), ("\\int_a^b x dx", Integral(x, (x, a, b))), ("\\int^b_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^b x dx", Integral(x, (x, a, b))), ("\\int^{b}_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^{b} x dx", Integral(x, (x, a, b))), ("\\int_{ }^{}x dx", Integral(x, x)), ("\\int^{ }_{ }x dx", Integral(x, x)), ("\\int^{b}_{a} x dx", Integral(x, (x, a, b))), # ("\\int_{f(a)}^{f(b)} f(z) dz", Integral(f(z), (z, f(a), f(b)))), ("\\int (x+a)", Integral(_Add(x, a), x)), ("\\int a + b + c dx", Integral(Add(a, b, c, evaluate=False), x)), ("\\int \\frac{dz}{z}", Integral(Pow(z, -1), z)), ("\\int \\frac{3 dz}{z}", Integral(3 * Pow(z, -1), z)), ("\\int \\frac{1}{x} dx", Integral(Pow(x, -1), x)), ("\\int \\frac{1}{a} + \\frac{1}{b} dx", Integral(_Add(_Pow(a, -1), Pow(b, -1)), x)), ("\\int \\frac{3 \\cdot d\\theta}{\\theta}", Integral(3 * _Pow(theta, -1), theta)), ("\\int \\frac{1}{x} + 1 dx", Integral(_Add(_Pow(x, -1), 1), x)), ("x_0", Symbol('x_0', real=True, positive=True)), ("x_{1}", Symbol('x_1', real=True, positive=True)), ("x_a", Symbol('x_a', real=True, positive=True)), ("x_{b}", Symbol('x_b', real=True, positive=True)), ("h_\\theta", Symbol('h_{\\theta}', real=True, positive=True)), ("h_\\theta ", Symbol('h_{\\theta}', real=True, positive=True)), ("h_{\\theta}", Symbol('h_{\\theta}', real=True, positive=True)), # ("h_{\\theta}(x_0, x_1)", Symbol('h_{theta}', real=True)(Symbol('x_{0}', real=True), Symbol('x_{1}', real=True))), ("x!", _factorial(x)), ("100!", _factorial(100)), ("\\theta!", _factorial(theta)), ("(x + 1)!", _factorial(_Add(x, 1))), ("\\left(x + 1\\right)!", _factorial(_Add(x, 1))), ("(x!)!", _factorial(_factorial(x))), ("x!!!", _factorial(_factorial(_factorial(x)))), ("5!7!", _Mul(_factorial(5), _factorial(7))), ("\\sqrt{x}", sqrt(x)), ("\\sqrt{x + b}", sqrt(_Add(x, b))), ("\\sqrt[3]{\\sin x}", root(sin(x), 3)), ("\\sqrt[y]{\\sin x}", root(sin(x), y)), ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)), ("x < y", StrictLessThan(x, y)), ("x \\leq y", LessThan(x, y)), ("x > y", StrictGreaterThan(x, y)), ("x \\geq y", GreaterThan(x, y)), ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))), ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))), ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))), ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))), ("\\prod_{a = b}^c x", Product(x, (a, b, c))), ("\\prod^{c}_{a = b} x", Product(x, (a, b, c))), ("\\prod^c_{a = b} x", Product(x, (a, b, c))), ("\\ln x", _log(x, E)), ("\\ln xy", _log(x * y, E)), ("\\log x", _log(x, 10)), ("\\log xy", _log(x * y, 10)), # ("\\log_2 x", _log(x, 2)), ("\\log_{2} x", _log(x, 2)), # ("\\log_a x", _log(x, a)), ("\\log_{a} x", _log(x, a)), ("\\log_{11} x", _log(x, 11)), ("\\log_{a^2} x", _log(x, _Pow(a, 2))), ("[x]", x), ("[a + b]", _Add(a, b)), ("\\frac{d}{dx} [ \\tan x ]", Derivative(tan(x), x)), ("2\\overline{x}", 2 * Symbol('xbar', real=True, positive=True)), ("2\\overline{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)), ("\\frac{x}{\\overline{x}_n}", x / Symbol('xbar_n', real=True, positive=True)), ("\\frac{\\sin(x)}{\\overline{x}_n}", sin(x) / Symbol('xbar_n', real=True, positive=True)), ("2\\bar{x}", 2 * Symbol('xbar', real=True, positive=True)), ("2\\bar{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)), ("\\sin\\left(\\theta\\right) \\cdot4", sin(theta) * 4), ("\\ln\\left(\\theta\\right)", _log(theta, E)), ("\\ln\\left(x-\\theta\\right)", _log(x - theta, E)), ("\\ln\\left(\\left(x-\\theta\\right)\\right)", _log(x - theta, E)), ("\\ln\\left(\\left[x-\\theta\\right]\\right)", _log(x - theta, E)), ("\\ln\\left(\\left\\{x-\\theta\\right\\}\\right)", _log(x - theta, E)), ("\\ln\\left(\\left|x-\\theta\\right|\\right)", _log(_Abs(x - theta), E)), ("\\frac{1}{2}xy(x+y)", Mul(Rational(1, 2), x, y, (x + y), evaluate=False)), ("\\frac{1}{2}\\theta(x+y)", Mul(Rational(1, 2), theta, (x + y), evaluate=False)), ("1-f(x)", 1 - f * x), ("\\begin{matrix}1&2\\\\3&4\\end{matrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{matrix}x&x^2\\\\\\sqrt{x}&x\\end{matrix}", Matrix([[x, x**2], [_Pow(x, S.Half), x]])), ("\\begin{matrix}\\sqrt{x}\\\\\\sin(\\theta)\\end{matrix}", Matrix([_Pow(x, S.Half), sin(theta)])), ("\\begin{pmatrix}1&2\\\\3&4\\end{pmatrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{bmatrix}1&2\\\\3&4\\end{bmatrix}", Matrix([[1, 2], [3, 4]])), # scientific notation ("2.5\\times 10^2", Rational(250)), ("1,500\\times 10^{-1}", Rational(150)), # e notation ("2.5E2", Rational(250)), ("1,500E-1", Rational(150)), # multiplication without cmd ("2x2y", Mul(2, x, 2, y, evaluate=False)), ("2x2", Mul(2, x, 2, evaluate=False)), ("x2", x * 2), # lin alg processing ("\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\theta\\begin{matrix}1\\\\3\\end{matrix} - \\begin{matrix}-1\\\\2\\end{matrix}", MatAdd(MatMul(theta, Matrix([[1], [3]]), evaluate=False), MatMul(-1, Matrix([[-1], [2]]), evaluate=False), evaluate=False)), ("\\theta\\begin{matrix}1&0\\\\0&1\\end{matrix}*\\begin{matrix}3\\\\-2\\end{matrix}", MatMul(theta, Matrix([[1, 0], [0, 1]]), Matrix([3, -2]), evaluate=False)), ("\\frac{1}{9}\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(Rational(1, 9), theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix};\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix},\\begin{pmatrix}1\\\\1\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1]), Matrix([1, 1, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right\\}", Matrix([1, 2, 3])), ("\\left{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right}", Matrix([1, 2, 3])), ("{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}}", Matrix([1, 2, 3])), # us dollars ("\\$1,000.00", Rational(1000)), ("\\$543.21", Rational(54321, 100)), ("\\$0.009", Rational(9, 1000)), # percentages ("100\\%", Rational(1)), ("1.5\\%", Rational(15, 1000)), ("0.05\\%", Rational(5, 10000)), # empty set ("\\emptyset", S.EmptySet), # divide by zero ("\\frac{1}{0}", _Pow(0, -1)), ("1+\\frac{5}{0}", _Add(1, _Mul(5, _Pow(0, -1)))), # adjacent single char sub sup ("4^26^2", _Mul(_Pow(4, 2), _Pow(6, 2))), ("x_22^2", _Mul(Symbol('x_2', real=True, positive=True), _Pow(2, 2))) ] def test_good_pair(self, s, eq): assert_equal(s, eq)
def _igcd(*args): return UnevaluatedExpr(igcd(*args))
def _gcd(a, b): return UnevaluatedExpr(gcd(a, b))