def parse_element(cls, element): """Parse a single element and returns it as a Sympy expression Called on every intervals borders""" element = element.replace("∞", str(sys.maxsize)) element = element.replace("\\infty", str(sys.maxsize)) element = element.replace("\\infinity", str(sys.maxsize)) return MathProblem.parse_answer(element)
def test_math_all_together(self): self.assertEqual((float( MathProblem.parse_answer( "\\frac{\\sqrt{x_{11}x_{12}+x_{13}x_{14}}}{\\sqrt[3]{x_{15}^{x_{12}}}x_{11}}" ).subs([("x_{11}", 1), ("x_{12}", 2), ("x_{13}", 3), ("x_{14}", 4), ("x_{15}", 5)]))), math.sqrt(14) / (25**(1 / 3))) self.assertEqual((float( MathProblem.parse_answer( "\\frac{\\sqrt{x_{11}x_{12}+x_{13}x_{14}}}{\\log\\left(\\sqrt[3]{x_{15}^{x_{12}}}\\right)}" ).subs([("x_{11}", 1), ("x_{12}", 2), ("x_{13}", 3), ("x_{14}", 4), ("x_{15}", 5)]))), math.sqrt(14) / math.log10(25**(1 / 3))) self.assertEqual((float( MathProblem.parse_answer( "\\left(\\frac{\\sqrt{x_{11}x_{12}+x_{13}x_{14}}}{\\log\\left(\\sqrt[3]{x_{15}^{x_{12}}}\\right)}\\right)^{\\log\\left(x_{12}x_{13}\\right)}" ).subs([("x_{11}", 1), ("x_{12}", 2), ("x_{13}", 3), ("x_{14}", 4), ("x_{15}", 5)]))), (math.sqrt(14) / math.log10(25**(1 / 3)))**math.log10(6)) self.assertEqual((float( MathProblem.parse_answer( "\\sqrt{\\left(\\frac{x_{12}^{x_{14}}}{x_{13}^{x_{12}}}\\right)}" ).subs([("x_{11}", 1), ("x_{12}", 2), ("x_{13}", 3), ("x_{14}", 4), ("x_{15}", 5)]))), 4 / 3) self.assertEqual((float( MathProblem.parse_answer( "\\int_0^1\\sqrt{\\left(\\frac{x_{12}^{x_{14}}}{x_{13}^{x_{12}}}\\right)}" ).subs([("x_{11}", 1), ("x_{12}", 2), ("x_{13}", 3), ("x_{14}", 4), ("x_{15}", 5)]))), 4 / 3) self.assertEqual( float(MathProblem.parse_answer("2\\pi* r_1").subs("r_{1}", 10)), 2 * math.pi * 10)
def parse_conditions(cls, conditions): conditions_tab = conditions.split("\\&") final_conditions = None for condition in conditions_tab: if condition.startswith("(") and condition.endswith( ")" ): #MathProblem.parse_answer does not accept equation/inequation surrounded by parenthesis condition = condition[1:-1] condition = MathProblem.parse_answer(condition) if final_conditions is None: final_conditions = sympify(condition) else: final_conditions = sympify(condition & final_conditions) return simplify(final_conditions)
def test_simple_expression(self): self.assertEqual((int(MathProblem.parse_answer("2x+1").subs("x", 17))), 35) self.assertEqual((int(MathProblem.parse_answer("1+x2").subs("x", 17))), 35) self.assertEqual( (int(MathProblem.parse_answer("2x+2-1").subs("x", 17))), 35) self.assertEqual( int((MathProblem.parse_answer("x+x+1").subs("x", 17))), 35) self.assertEqual( (int(MathProblem.parse_answer("2x+7-6").subs("x", 17))), 35) self.assertEqual( (int(MathProblem.parse_answer("x3+2-x-1").subs("x", 17))), 35) self.assertEqual((int( MathProblem.parse_answer("\\frac{3xxx+2xx-xxx-1xx}{xx}").subs( "x", 17))), 35) self.assertEqual((int( MathProblem.parse_answer("\\frac{15xxx+10xx-5xxx-5xx}{5xx}").subs( "x", 17))), 35) self.assertEqual((int(MathProblem.parse_answer("|x|").subs("x", -5))), 5) self.assertEqual( (int(MathProblem.parse_answer("\\left|x\\right|").subs("x", -5))), 5)
def parse_implicit_set(cls, eq): """Parses an implicit set, formatted such as {variable, condition, domain} For example, eq could be {x|(x²>25)&(x³<150)}""" eq = eq[1:-1] eq = eq.replace("\\left", "") eq = eq.replace("\\right", "") eq_tab = eq.split("|") if len(eq_tab) != 3: raise ValueError( "For implicit set, please follow the format {variable|condition|domain}" ) target = MathProblem.parse_answer(eq_tab[0]) condition = cls.parse_conditions(eq_tab[1]) domain = cls.parse_domain(eq_tab[2]) return simplify(ConditionSet(target, condition, domain))
def test_unique_expression(self): self.assertEqual((int(MathProblem.parse_answer("x").subs("x", 10))), 10) self.assertEqual((int(MathProblem.parse_answer("2x-x").subs("x", 10))), 10) self.assertEqual((int(MathProblem.parse_answer("x2-x").subs("x", 10))), 10) self.assertEqual( (int(MathProblem.parse_answer("\\frac{5x}{5}").subs("x", 10))), 10) self.assertEqual( (int(MathProblem.parse_answer("\\frac{25x}{5}-4x").subs("x", 10))), 10) self.assertEqual( (int(MathProblem.parse_answer("\\frac{x}{1}").subs("x", 10))), 10) self.assertEqual((int(MathProblem.parse_answer("1x").subs("x", 10))), 10) self.assertEqual( (int(MathProblem.parse_answer("1x^1-5+5").subs("x", 10))), 10) self.assertEqual( (int(MathProblem.parse_answer("\\frac{x^2}{x}").subs("x", 10))), 10)
def test_multivariable_polynomial(self): self.assertEqual((int( MathProblem.parse_answer("3x^2+x+4y^2+2y+4").subs([("x", 3), ("y", 4)]))), 106) self.assertEqual((int( MathProblem.parse_answer("4+3x^2+x+4y^2+2y+1-1").subs([("x", 3), ("y", 4) ]))), 106) self.assertEqual((int( MathProblem.parse_answer("3x^2+x+(2y+1)2y+4").subs([("x", 3), ("y", 4)]))), 106) self.assertEqual((int( MathProblem.parse_answer("x*\\left(3x+1\\right)+4y^2+2y+4").subs( [("x", 3), ("y", 4)]))), 106) self.assertEqual((int( MathProblem.parse_answer("3x^2+x+2(2y^2)+2y+4").subs([("x", 3), ("y", 4)]))), 106) self.assertEqual((int( MathProblem.parse_answer("3x^2+2x-x+4y^2+2y+4").subs([("x", 3), ("y", 4)]))), 106) self.assertEqual((int( MathProblem.parse_answer("3x^2+x2-x+y^2*4+2y+4").subs([("x", 3), ("y", 4) ]))), 106) self.assertEqual((int( MathProblem.parse_answer( "\\frac{3x^3+xx2-xx+y^2*4x+2yx+4x}{x}").subs([("x", 3), ("y", 4)]))), 106) self.assertEqual((int( MathProblem.parse_answer( "\\frac{3x^2y+xy2-xy+y^3*4+2yy+4y}{y}").subs([("x", 3), ("y", 4)]))), 106)
def test_simple_polynomial(self): self.assertEqual( (int(MathProblem.parse_answer("3x^2+2x+5").subs("x", 18))), 1013) self.assertEqual( (int(MathProblem.parse_answer("3x^2+2x+5+x^4-x^4").subs("x", 18))), 1013) self.assertEqual( (int(MathProblem.parse_answer("3x^2+5x+10-5-3x").subs("x", 18))), 1013) self.assertEqual( (int(MathProblem.parse_answer("3xx+2x+5").subs("x", 18))), 1013) self.assertEqual( (int(MathProblem.parse_answer("xx+2xx+2x+5").subs("x", 18))), 1013) self.assertEqual((int( MathProblem.parse_answer("2+3x^2*2+x+x+3-3xx").subs("x", 18))), 1013) self.assertEqual((int( MathProblem.parse_answer( "\\frac{2x+3x^3*2+xx+xx+3x-3xxx}{x}").subs("x", 18))), 1013)
def parse_element(cls, latex_str): """Parse a single element""" return MathProblem.parse_answer(latex_str)
def test_unique_exponent(self): self.assertEqual((int(MathProblem.parse_answer("x^1").subs("x", 10))), 10) self.assertEqual((int(MathProblem.parse_answer("x^2").subs("x", 10))), 100) self.assertEqual((int(MathProblem.parse_answer("x^2x").subs("x", 30))), 27000) self.assertEqual((int(MathProblem.parse_answer("x^3").subs("x", 30))), 27000) self.assertEqual((int(MathProblem.parse_answer("x^x").subs("x", 3))), 27) self.assertEqual((int(MathProblem.parse_answer("x^x2").subs("x", 3))), 54) self.assertEqual((int(MathProblem.parse_answer("x^{x}").subs("x", 3))), 27) self.assertEqual( (int(MathProblem.parse_answer("x^{1x}").subs("x", 3))), 27) self.assertEqual( (int(MathProblem.parse_answer("x^{2x}").subs("x", 3))), 729) self.assertEqual( (int(MathProblem.parse_answer("x^{2x}2").subs("x", 3))), 1458) self.assertEqual( (int(MathProblem.parse_answer("x^{x2}2").subs("x", 3))), 1458) self.assertEqual( (int(MathProblem.parse_answer("2x^{x2}").subs("x", 3))), 1458) self.assertEqual( (int(MathProblem.parse_answer("xx^{2x}").subs("x", 2))), 32) self.assertEqual( (int(MathProblem.parse_answer("(3x)^{2x}").subs("x", 2))), 1296) self.assertEqual( (int(MathProblem.parse_answer("x^{2xx}").subs("x", 2))), 256) self.assertEqual( (int(MathProblem.parse_answer("x^{x2x}").subs("x", 2))), 256) self.assertEqual( (int(MathProblem.parse_answer("x^{2x^2}").subs("x", 2))), 256) self.assertEqual((int( MathProblem.parse_answer("\\frac{x^{2x^2}}{1}").subs("x", 2))), 256) self.assertEqual((int( MathProblem.parse_answer("\\frac{\\frac{x^{2x^2}}{1}}{1}").subs( "x", 2))), 256) self.assertEqual((int( MathProblem.parse_answer("\\frac{\\frac{2xx^{2x^2}}{1}}{2x}").subs( "x", 2))), 256) self.assertEqual((int( MathProblem.parse_answer("\\frac{2x^{2x^2}}{2}").subs("x", 2))), 256) self.assertEqual((int( MathProblem.parse_answer("\\frac{2xx^{2x^2}}{2x}").subs("x", 2))), 256) self.assertEqual((int( MathProblem.parse_answer("\\frac{4x^{2x^2}}{2x}").subs("x", 2))), 256) self.assertEqual((int( MathProblem.parse_answer("\\frac{xxx^{2x^2}}{2x}").subs("x", 2))), 256) self.assertEqual((float( MathProblem.parse_answer("\\frac{2x}{xxx^{2x^2}}").subs("x", 2))), 1 / 256) self.assertEqual((float( MathProblem.parse_answer("\\sqrt{\\frac{2x}{xxx^{2x^2}}}").subs( "x", 2))), math.sqrt(1 / 256))
def parse_element(cls, latex_str): """Parse a single element""" # Needs simplify because the parser of MathProblem doesn't do any return simplify(MathProblem.parse_answer(latex_str))
def test_is_equal_math_tolerance(self): test_instance = MathProblem("fake_id", {"fake_content": 5}, "french", "fake_taskf") test_instance._tolerance = 0.0001 self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3"))) self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1"))) self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.14"))) self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.141"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1415"))) test_instance._tolerance = 0.001 self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3"))) self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1"))) self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.14"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.141"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1415"))) test_instance._tolerance = 0.01 self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3"))) self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.14"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.141"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1415"))) test_instance._tolerance = 0.1 self.assertFalse( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.14"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.141"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1415"))) test_instance._tolerance = 1 self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.14"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.141"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\pi"), MathProblem.parse_answer("3.1415")))
def test_is_equal_math(self): test_instance = MathProblem("fake_id", {"fake_content": 5}, "french", "fake_taskf") self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("2x"), MathProblem.parse_answer("x+x"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("2x"), MathProblem.parse_answer("3x-x"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("2x"), MathProblem.parse_answer("4x-2x"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("2x"), MathProblem.parse_answer("2*x"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("2x"), MathProblem.parse_answer("2x+0"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("2x"), MathProblem.parse_answer("x+x+x-x"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("2x"), MathProblem.parse_answer("x-i^2x"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("2x"), MathProblem.parse_answer("\\frac{4x}{2}"))) self.assertTrue( test_instance.is_equal( MathProblem.parse_answer("2x"), MathProblem.parse_answer("\\frac{4x^2}{2x}"))) self.assertTrue( test_instance.is_equal( MathProblem.parse_answer("2x"), MathProblem.parse_answer("\\frac{6x^2}{2x}-x"))) self.assertTrue( test_instance.is_equal( MathProblem.parse_answer("2x"), MathProblem.parse_answer("\\frac{2x^x}{x^{x-1}}"))) self.assertTrue( test_instance.is_equal( MathProblem.parse_answer("2x"), MathProblem.parse_answer("x*2*\\frac{5x}{2+3x+4x-2x+1-3}"))) self.assertTrue( test_instance.is_equal( MathProblem.parse_answer("2x"), MathProblem.parse_answer("x+x*\\pi-x*(\\pi-1)"))) self.assertTrue( test_instance.is_equal( MathProblem.parse_answer("2x"), MathProblem.parse_answer("x+x*x_1*x_{12}-x*(x_1*x_{12}-1)"))) self.assertTrue( test_instance.is_equal( MathProblem.parse_answer("\\frac{1}{\\sqrt{3}}"), MathProblem.parse_answer("\\frac{\\sqrt{3}}{3}"))) self.assertTrue( test_instance.is_equal( MathProblem.parse_answer("\\frac{\\sqrt{3}}{3}"), MathProblem.parse_answer("\\frac{1}{\\sqrt{3}}"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("0.5"), MathProblem.parse_answer("\\frac{1}{2}"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("\\frac{1}{2}"), MathProblem.parse_answer("0.5"))) self.assertTrue( test_instance.is_equal(MathProblem.parse_answer("0.5"), MathProblem.parse_answer("\\frac{5}{10}"))) self.assertTrue( test_instance.is_equal( MathProblem.parse_answer("0.5"), MathProblem.parse_answer("\\frac{2x_1x_{12}}{4x_1x_{12}}")))
def test_multiple_exponent(self): self.assertEqual((int( MathProblem.parse_answer("x^2+y^2").subs([("x", 2), ("y", 3)]))), 13) self.assertEqual((int( MathProblem.parse_answer("x^y+y^x").subs([("x", 2), ("y", 3)]))), 17) self.assertEqual((int( MathProblem.parse_answer("2x^y+y^x").subs([("x", 2), ("y", 3)]))), 25) self.assertEqual((int( MathProblem.parse_answer("x^y2+y^x").subs([("x", 2), ("y", 3)]))), 25) self.assertEqual((int( MathProblem.parse_answer("2x^y+2y^x").subs([("x", 2), ("y", 3)]))), 34) self.assertEqual((int( MathProblem.parse_answer("2x^2y+2y^x").subs([("x", 2), ("y", 3)]))), 42) self.assertEqual((int( MathProblem.parse_answer("2x^{2y}+2y^x").subs([("x", 2), ("y", 3)]))), 146) self.assertEqual((int( MathProblem.parse_answer("2x^{2y}+(2y)^x").subs([("x", 2), ("y", 3)]))), 164) self.assertEqual((int( MathProblem.parse_answer("\\frac{6x^{2y+1}+(2y)^x3x}{3x}").subs( [("x", 2), ("y", 3)]))), 164) self.assertEqual((int( MathProblem.parse_answer("2x^{y^2}").subs([("x", 2), ("y", 3)]))), 1024) self.assertEqual((float( MathProblem.parse_answer("\\frac{x+x}{4xx^y+y^x2x}").subs( [("x", 2), ("y", 3)]))), 1 / 25) self.assertEqual((float( MathProblem.parse_answer("\\sqrt{2x^{2y}}").subs([("x", 2), ("y", 3)]))), math.sqrt(128))
def test_multi_char_subscripts(self): self.assertEqual( (int(MathProblem.parse_answer("x_{12}").subs("x_{12}", 10))), 10) self.assertEqual((int( MathProblem.parse_answer("x_{12}x_{13}").subs([("x_{12}", 3), ("x_{13}", 4)]))), 12) self.assertEqual( (int(MathProblem.parse_answer("x_{12}2").subs("x_{12}", 10))), 20) self.assertEqual( (int(MathProblem.parse_answer("2x_{12}").subs("x_{12}", 10))), 20) self.assertEqual( (int(MathProblem.parse_answer("2x_{12}2").subs("x_{12}", 10))), 40) self.assertEqual((int( MathProblem.parse_answer("x_{12}+x_{13}").subs([("x_{12}", 3), ("x_{13}", 4)]))), 7) self.assertEqual((int( MathProblem.parse_answer("\\log x_{12}").subs("x_{12}", 100))), 2) self.assertEqual((int( MathProblem.parse_answer("2\\log x_{12}").subs("x_{12}", 100))), 4) self.assertEqual((float( MathProblem.parse_answer("\\log2x_{12}").subs("x_{12}", 100))), math.log10(200)) self.assertEqual( (int(MathProblem.parse_answer("a_{xy}").subs("a_{x*y}", 100))), 100 ) # a multicharacter subscript containing letter(s) such as x_{ab} is translated into x_{a*b} self.assertEqual((int( MathProblem.parse_answer("\\sqrt{x_{12}}").subs("x_{12}", 100))), 10) self.assertEqual((int( MathProblem.parse_answer("5x_{11}\\sqrt{x_{12}}").subs( [("x_{11}", 3), ("x_{12}", 4)]))), 30) self.assertEqual( (int(MathProblem.parse_answer("\\ln e+x").subs("x", 5))), 6) self.assertEqual( (int(MathProblem.parse_answer("\\ln e^x").subs("x", 5))), 5) self.assertEqual((int( MathProblem.parse_answer("\\ln e^{xy}").subs([("x", 2), ("y", 3)]))), 6) self.assertEqual((int( MathProblem.parse_answer("\\ln e^{x_1x_2}").subs([("x_{1}", 2), ("x_{2}", 3)]))), 6)
def test_single_char_subscripts(self): self.assertEqual( (int(MathProblem.parse_answer("x_1").subs("x_{1}", 10))), 10) self.assertEqual((int( MathProblem.parse_answer("x_1+x_2").subs([("x_{1}", 1), ("x_{2}", 4)]))), 5) self.assertEqual((int( MathProblem.parse_answer("x_1x_2").subs([("x_{1}", 2), ("x_{2}", 5)]))), 10) self.assertEqual((int( MathProblem.parse_answer("x_1^{x_2}").subs([("x_{1}", 2), ("x_{2}", 3)]))), 8) self.assertEqual( (int(MathProblem.parse_answer("x_y").subs([("x_{y}", 3)]))), 3) self.assertEqual( (int(MathProblem.parse_answer("2x_1").subs([("x_{1}", 3)]))), 6) self.assertEqual( (int(MathProblem.parse_answer("x_12").subs([("x_{1}", 3)]))), 6) self.assertEqual( (int(MathProblem.parse_answer("x_1^3").subs([("x_{1}", 3)]))), 27) self.assertEqual((int( MathProblem.parse_answer("x_1^{x_2}").subs([("x_{1}", 3), ("x_{2}", 2)]))), 9) self.assertEqual((int( MathProblem.parse_answer("\\frac{x_1}{x_2}").subs([("x_{1}", 10), ("x_{2}", 2) ]))), 5) self.assertEqual((int( MathProblem.parse_answer("\\log x_1").subs([("x_{1}", 100)]))), 2) self.assertEqual( (int(MathProblem.parse_answer("\\log2x_1").subs([("x_{1}", 50)]))), 2) self.assertEqual((int( MathProblem.parse_answer("\\log10^{x_1}").subs([("x_{1}", 3)]))), 3) self.assertEqual((int( MathProblem.parse_answer("\\sqrt{\\log2x_1}").subs([("x_{1}", 5000) ]))), 2) self.assertEqual((float( MathProblem.parse_answer("\\log\\left(x_1x_2\\right)").subs( [("x_{1}", 10), ("x_{2}", 2)]))), math.log10(20))
def test_log(self): self.assertEqual( (int(MathProblem.parse_answer("\\log x").subs("x", 10))), 1) self.assertEqual( (int(MathProblem.parse_answer("\\log10x").subs("x", 10))), 2) self.assertEqual( (int(MathProblem.parse_answer("\\log10x+10").subs("x", 10))), 12) self.assertEqual((int( MathProblem.parse_answer("\\left(\\log x\\right)^2").subs( "x", 100))), 4) self.assertEqual((int( MathProblem.parse_answer("\\log\\left(x+90\\right)").subs("x", 10))), 2) self.assertEqual((int( MathProblem.parse_answer( "\\left(\\log x\\right)^{\\log1000}").subs("x", 100))), 8) self.assertEqual( (int(MathProblem.parse_answer("\\log x^2").subs("x", 10))), 2) self.assertEqual( (int(MathProblem.parse_answer("\\sqrt{\\log x^4}").subs("x", 10))), 2) self.assertEqual((float( MathProblem.parse_answer("\\log x^y").subs([("x", 10), ("y", 3)]))), 3) self.assertEqual( (float(MathProblem.parse_answer("\\log 2x").subs("x", 10))), float(math.log10(20))) self.assertEqual( (float(MathProblem.parse_answer("2\\log 2x").subs("x", 10))), float(2 * math.log10(20))) self.assertEqual( (float(MathProblem.parse_answer("\\log x^10").subs("x", 2))), float(math.log10(1024))) self.assertEqual((float( MathProblem.parse_answer( "\\left(\\left(\\log x\\right)^{\\log100}\\right)^{2}").subs( "x", 100))), 16) self.assertEqual((float( MathProblem.parse_answer("\\frac{\\log1000x}{\\log10x}").subs( "x", 10))), 2) self.assertEqual((float( MathProblem.parse_answer("\\left(\\log x\\right)^y").subs( [("x", 100), ("y", 4)]))), 16) self.assertEqual((float(MathProblem.parse_answer("\\ln 5"))), math.log(5)) self.assertEqual( (float(MathProblem.parse_answer("\\ln 5+10").subs("x", 10))), math.log(5) + 10) self.assertEqual( (float(MathProblem.parse_answer("\\ln5x").subs("x", 10))), math.log(50)) self.assertEqual((float( MathProblem.parse_answer("\\log xy").subs([("x", 10), ("y", 100)]))), 3) self.assertEqual((float( MathProblem.parse_answer("\\left(\\log x\\right)y").subs( [("x", 100), ("y", 10)]))), 20)