def test_sympy_rounding(): expr = 12.3456789 * a + 1.23456789e-55 * b assert round_and_render_line_objects_to_latex( CalcLine([expr], '', ''), cell_precision=3, cell_notation=True, **config_options ).latex == '12.35 a + 1.235 \\cdot 10^{-55} b' assert round_and_render_line_objects_to_latex( CalcLine([expr], '', ''), cell_precision=4, cell_notation=False, **config_options ).latex == '12.3457 a'
def test_categorize_line(): assert handcalcs.handcalcs.categorize_line("a = 2 # Comment", {"a": 2}, "") == ParameterLine( line=deque(["a", "=", 2]), comment=" Comment", latex="") assert handcalcs.handcalcs.categorize_line("y = (a+4) # Comment", { "a": 2, "y": 6 }, "") == ParameterLine(line=deque(["y", "=", 6]), comment=" Comment", latex="") assert handcalcs.handcalcs.categorize_line( "alpha_eta_psi = 4 / (y**(a + 1)) # Comment", { "a": 2, "y": 6, "alpha_eta_psi": 0.018518518518518517 }, "", ) == CalcLine( line=deque([ "alpha_eta_psi", "=", "4", "/", deque(["y", "**", deque(["a", "+", "1"])]) ]), comment=" Comment", latex="", ) assert handcalcs.handcalcs.categorize_line( "alpha_eta_psi", { "a": 2, "y": 6, "alpha_eta_psi": 0.018518518518518517 }, "") == ParameterLine(line=deque( ["alpha_eta_psi", "=", 0.018518518518518517]), comment="", latex="") assert handcalcs.handcalcs.categorize_line("if x < 1: b = x # Comment", { "x": 2, "b": 2 }, "") == ConditionalLine( condition=deque(["x", "<", "1"]), condition_type="if", expressions=deque( [ParameterLine(line=deque(["b", "=", 2]), comment="", latex="")]), raw_condition="x < 1", raw_expression="b = x", true_condition=deque([]), true_expressions=deque([]), comment=" Comment", latex="", )
def test_pint_rounding(): L = (1.23456789 * kip) d = (2 * ft) M = L * d assert round_and_render_line_objects_to_latex( CalcLine([L], '', ''), cell_precision=2, cell_notation=False, **config_options).latex == '1.23\\ \\mathrm{kip}' assert round_and_render_line_objects_to_latex( CalcLine([d], '', ''), cell_precision=2, cell_notation=True, **config_options).latex == '2.00\\times 10^{0}\\ \\mathrm{foot}' assert round_and_render_line_objects_to_latex( CalcLine([M], '', ''), cell_precision=2, cell_notation=False, **config_options).latex == '2.47\\ \\mathrm{foot} \\cdot \\mathrm{kip}'
def test_pint_with_sympy(): import sympy pint.quantity._Quantity._sympy_ = lambda s: sympy.sympify(f'({s.m})*{s.u}') pint.quantity._Quantity._repr_latex_ = lambda s: ( s.m._repr_latex_() + r'\ ' + s.u._repr_latex_() if hasattr(s.m, '_repr_latex_') else '${:~L}$'.format(s)) L = 1.23456789 * sympy.symbols('a') * kip # NOTE: Pint in sympy objects do not accept float format strings. While pint has its own # format strings which kinda work like float format strings, they DO NOT work # when pint objects are in sympy objects. Therefore, pint objects in sympy # objects will typically be rounded to one extra decimal place. This is just # the way it is. assert round_and_render_line_objects_to_latex( # cell_precision=3 -> four decimal places CalcLine([L], '', ''), cell_precision=3, cell_notation=True, **config_options).latex == '\\mathtt{\\text{1.2346*a kip}}'
def format_lines(): assert handcalcs.handcalcs.format_lines( ParameterLine(line=deque(["y", "=", "6"]), comment=" Comment", latex="y = 6")) == ParameterLine( line=deque(["y", "=", "6"]), comment=" Comment", latex="y &= 6\\;\\;\\textrm{(Comment)}\n", ) assert handcalcs.handcalcs.format_lines( CalcLine( line=deque([ "\\alpha_{\\eta_{\\psi}}", "=", "\\frac{", "4", "}{", "\\left(", "y", "\\right)", "^{", "\\left(", "a", "+", "1", "\\right)", "}", "}", "=", "\\frac{", "4", "}{", "\\left(", "6", "\\right)", "^{", "\\left(", "2", "+", "1", "\\right)", "}", "}", "=", "0.019", ]), comment=" Comment", latex= "\\alpha_{\\eta_{\\psi}} = \\frac{ 4 }{ \\left( y \\right) ^{ \\left( a + 1 \\right) } } = \\frac{ 4 }{ \\left( 6 \\right) ^{ \\left( 2 + 1 \\right) } } = 0.019", ) ) == CalcLine( line=deque([ "\\alpha_{\\eta_{\\psi}}", "=", "\\frac{", "4", "}{", "\\left(", "y", "\\right)", "^{", "\\left(", "a", "+", "1", "\\right)", "}", "}", "=", "\\frac{", "4", "}{", "\\left(", "6", "\\right)", "^{", "\\left(", "2", "+", "1", "\\right)", "}", "}", "=", "0.019", ]), comment=" Comment", latex= "\\alpha_{\\eta_{\\psi}} &= \\frac{ 4 }{ \\left( y \\right) ^{ \\left( a + 1 \\right) } } = \\frac{ 4 }{ \\left( 6 \\right) ^{ \\left( 2 + 1 \\right) } } &= 0.019\\;\\;\\textrm{(Comment)}\n", ) assert handcalcs.handcalcs.format_lines( ConditionalLine( condition=deque(["x", ">", "1"]), condition_type="elif", expressions=deque([ CalcLine( line=deque([ "b", "=", "x", "\\cdot", "1", "=", "2", "\\cdot", "1", "=", "2", ]), comment="", latex="b = x \\cdot 1 = 2 \\cdot 1 = 2", ), ParameterLine(line=deque(["c", "=", "2"]), comment="", latex="c = 2"), ]), raw_condition="x > 1", raw_expression="b = x*1; c = b", true_condition=deque([ "x", ">", "1", "\\rightarrow", "\\left(", "2", ">", "1", "\\right)" ]), true_expressions=deque([ CalcLine( line=deque([ "b", "=", "x", "\\cdot", "1", "=", "2", "\\cdot", "1", "=", "2", ]), comment="", latex="b = x \\cdot 1 = 2 \\cdot 1 = 2", ), ParameterLine(line=deque(["c", "=", "2"]), comment="", latex="c = 2"), ]), comment=" Comment", latex="b = x \\cdot 1 = 2 \\cdot 1 = 2\\\\\nc = 2", ) ) == ConditionalLine( condition=deque(["x", ">", "1"]), condition_type="elif", expressions=deque([ CalcLine( line=deque([ "b", "=", "x", "\\cdot", "1", "=", "2", "\\cdot", "1", "=", "2", ]), comment="", latex="b &= x \\cdot 1 = 2 \\cdot 1 &= 2\n", ), ParameterLine(line=deque(["c", "=", "2"]), comment="", latex="c &= 2\\;\n"), ]), raw_condition="x > 1", raw_expression="b = x*1; c = b", true_condition=deque([ "x", ">", "1", "\\rightarrow", "\\left(", "2", ">", "1", "\\right)" ]), true_expressions=deque([ CalcLine( line=deque([ "b", "=", "x", "\\cdot", "1", "=", "2", "\\cdot", "1", "=", "2", ]), comment="", latex="b &= x \\cdot 1 = 2 \\cdot 1 &= 2\n", ), ParameterLine(line=deque(["c", "=", "2"]), comment="", latex="c &= 2\\;\n"), ]), comment=" Comment", latex= "&\\text{Since, }x > 1 \\rightarrow \\left( 2 > 1 \\right):\\;\\;\\textrm{(Comment)}\n\\end{aligned}\n\\]\n\\[\n\\begin{aligned}\nb &= x \\cdot 1 = 2 \\cdot 1 &= 2\n\\\\\nc &= 2\\;\n", ) assert handcalcs.handcalcs.format_lines( ConditionalLine( condition=deque([""]), condition_type="else", expressions=deque([ CalcLine( line=deque([ "b", "=", "x", "\\cdot", "1", "=", "10", "\\cdot", "1", "=", "10", ]), comment="", latex="b = x \\cdot 1 = 10 \\cdot 1 = 10", ), ParameterLine(line=deque(["c", "=", "10"]), comment="", latex="c = 10"), ]), raw_condition="", raw_expression="b = x*1; c = b", true_condition=deque([""]), true_expressions=deque([ CalcLine( line=deque([ "b", "=", "x", "\\cdot", "1", "=", "10", "\\cdot", "1", "=", "10", ]), comment="", latex="b = x \\cdot 1 = 10 \\cdot 1 = 10", ), ParameterLine(line=deque(["c", "=", "10"]), comment="", latex="c = 10"), ]), comment="Comment", latex="b = x \\cdot 1 = 10 \\cdot 1 = 10\\\\\nc = 10", )) == ConditionalLine( condition=deque([""]), condition_type="else", expressions=deque([ CalcLine( line=deque([ "b", "=", "x", "\\cdot", "1", "=", "10", "\\cdot", "1", "=", "10", ]), comment="", latex="b &= x \\cdot 1 = 10 \\cdot 1 &= 10\n", ), ParameterLine(line=deque(["c", "=", "10"]), comment="", latex="c &= 10\\;\n"), ]), raw_condition="", raw_expression="b = x*1; c = b", true_condition=deque([""]), true_expressions=deque([ CalcLine( line=deque([ "b", "=", "x", "\\cdot", "1", "=", "10", "\\cdot", "1", "=", "10", ]), comment="", latex="b &= x \\cdot 1 = 10 \\cdot 1 &= 10\n", ), ParameterLine(line=deque(["c", "=", "10"]), comment="", latex="c &= 10\\;\n"), ]), comment="Comment", latex="b &= x \\cdot 1 = 10 \\cdot 1 &= 10\n\\\\\nc &= 10\\;\n", ) assert handcalcs.handcalcs.format_lines( LongCalcLine( line=deque([ "b", "=", "3", "\\cdot", "a", "=", "3", "\\cdot", "2", "=", "6" ]), comment="", latex="b = 3 \\cdot a = 3 \\cdot 2 = 6", )) == LongCalcLine( line=deque([ "b", "=", "3", "\\cdot", "a", "=", "3", "\\cdot", "2", "=", "6" ]), comment="", latex="b &= 3 \\cdot a \\\\&= 3 \\cdot 2 \\\\&= 6\\\\\n", )
def test_round_and_render_line_objects_to_latex(): assert handcalcs.handcalcs.round_and_render_line_objects_to_latex( CalcLine( line=deque([ "\\alpha_{\\eta_{\\psi}}", "=", "\\frac{", "4", "}{", "\\left(", "y", "\\right)", "^{", "\\left(", "a", "+", "1", "\\right)", "}", "}", "=", "\\frac{", "4", "}{", "\\left(", 6, "\\right)", "^{", "\\left(", 2, "+", "1", "\\right)", "}", "}", "=", 0.018518518518518517, ]), comment=" Comment", latex="", ), 3, ) == CalcLine( line=deque([ "\\alpha_{\\eta_{\\psi}}", "=", "\\frac{", "4", "}{", "\\left(", "y", "\\right)", "^{", "\\left(", "a", "+", "1", "\\right)", "}", "}", "=", "\\frac{", "4", "}{", "\\left(", "6", "\\right)", "^{", "\\left(", "2", "+", "1", "\\right)", "}", "}", "=", "0.019", ]), comment=" Comment", latex= "\\alpha_{\\eta_{\\psi}} = \\frac{ 4 }{ \\left( y \\right) ^{ \\left( a + 1 \\right) } } = \\frac{ 4 }{ \\left( 6 \\right) ^{ \\left( 2 + 1 \\right) } } = 0.019", ) assert handcalcs.handcalcs.round_and_render_line_objects_to_latex( ParameterLine( line=deque(["\\alpha_{\\eta_{\\psi}}", "=", 0.018518518518518517]), comment="", latex="", ), 3, ) == ParameterLine( line=deque(["\\alpha_{\\eta_{\\psi}}", "=", "0.019"]), comment="", latex="\\alpha_{\\eta_{\\psi}} = 0.019", ) assert handcalcs.handcalcs.round_and_render_line_objects_to_latex( ParameterLine(line=deque(["y", "=", -2]), comment="", latex=""), 3) == ParameterLine(line=deque(["y", "=", "-2"]), comment="", latex="y = -2") assert handcalcs.handcalcs.round_and_render_line_objects_to_latex( CalcLine( line=deque([ "y", "=", "\\sqrt{", "\\left(", "\\frac{", "a", "}{", "b", "}", "\\right)", "}", "+", "\\arcsin{", "\\left(", "\\sin{", "\\left(", "\\frac{", "b", "}{", "c", "}", "\\right)", "}", "\\right)", "}", "+", "\\left(", "\\frac{", "a", "}{", "b", "}", "\\right)", "^{", "\\left(", "0.5", "\\right)", "}", "+", "\\sqrt{", "\\left(", "\\frac{", "a", "\\cdot", "b", "+", "b", "\\cdot", "c", "}{", "\\left(", "b", "\\right)", "^{", "2", "}", "}", "\\right)", "}", "+", "\\sin{", "\\left(", "\\frac{", "a", "}{", "b", "}", "\\right)", "}", "=", "\\sqrt{", "\\left(", "\\frac{", 10000001, "}{", 20000002, "}", "\\right)", "}", "+", "\\arcsin{", "\\left(", "\\sin{", "\\left(", "\\frac{", 20000002, "}{", 30000003, "}", "\\right)", "}", "\\right)", "}", "+", "\\left(", "\\frac{", 10000001, "}{", 20000002, "}", "\\right)", "^{", "\\left(", "0.5", "\\right)", "}", "+", "\\sqrt{", "\\left(", "\\frac{", 10000001, "\\cdot", 20000002, "+", 20000002, "\\cdot", 30000003, "}{", "\\left(", 20000002, "\\right)", "^{", "2", "}", "}", "\\right)", "}", "+", "\\sin{", "\\left(", "\\frac{", 10000001, "}{", 20000002, "}", "\\right)", "}", "=", 3.97451933001706, ]), comment=" Comment", latex="", ), 3, ) == CalcLine( line=deque([ "y", "=", "\\sqrt{", "\\left(", "\\frac{", "a", "}{", "b", "}", "\\right)", "}", "+", "\\arcsin{", "\\left(", "\\sin{", "\\left(", "\\frac{", "b", "}{", "c", "}", "\\right)", "}", "\\right)", "}", "+", "\\left(", "\\frac{", "a", "}{", "b", "}", "\\right)", "^{", "\\left(", "0.5", "\\right)", "}", "+", "\\sqrt{", "\\left(", "\\frac{", "a", "\\cdot", "b", "+", "b", "\\cdot", "c", "}{", "\\left(", "b", "\\right)", "^{", "2", "}", "}", "\\right)", "}", "+", "\\sin{", "\\left(", "\\frac{", "a", "}{", "b", "}", "\\right)", "}", "=", "\\sqrt{", "\\left(", "\\frac{", "10000001", "}{", "20000002", "}", "\\right)", "}", "+", "\\arcsin{", "\\left(", "\\sin{", "\\left(", "\\frac{", "20000002", "}{", "30000003", "}", "\\right)", "}", "\\right)", "}", "+", "\\left(", "\\frac{", "10000001", "}{", "20000002", "}", "\\right)", "^{", "\\left(", "0.5", "\\right)", "}", "+", "\\sqrt{", "\\left(", "\\frac{", "10000001", "\\cdot", "20000002", "+", "20000002", "\\cdot", "30000003", "}{", "\\left(", "20000002", "\\right)", "^{", "2", "}", "}", "\\right)", "}", "+", "\\sin{", "\\left(", "\\frac{", "10000001", "}{", "20000002", "}", "\\right)", "}", "=", "3.975", ]), comment=" Comment", latex= "y = \\sqrt{ \\left( \\frac{ a }{ b } \\right) } + \\arcsin{ \\left( \\sin{ \\left( \\frac{ b }{ c } \\right) } \\right) } + \\left( \\frac{ a }{ b } \\right) ^{ \\left( 0.5 \\right) } + \\sqrt{ \\left( \\frac{ a \\cdot b + b \\cdot c }{ \\left( b \\right) ^{ 2 } } \\right) } + \\sin{ \\left( \\frac{ a }{ b } \\right) } = \\sqrt{ \\left( \\frac{ 10000001 }{ 20000002 } \\right) } + \\arcsin{ \\left( \\sin{ \\left( \\frac{ 20000002 }{ 30000003 } \\right) } \\right) } + \\left( \\frac{ 10000001 }{ 20000002 } \\right) ^{ \\left( 0.5 \\right) } + \\sqrt{ \\left( \\frac{ 10000001 \\cdot 20000002 + 20000002 \\cdot 30000003 }{ \\left( 20000002 \\right) ^{ 2 } } \\right) } + \\sin{ \\left( \\frac{ 10000001 }{ 20000002 } \\right) } = 3.975", )
def test_add_result_values_to_lines(): assert handcalcs.handcalcs.add_result_values_to_line( CalcLine( line=deque([ "alpha_eta_psi", "=", "4", "/", deque(["y", "**", deque(["a", "+", "1"])]), ]), comment=" Comment", latex="", ), { "a": 2, "y": 6, "alpha_eta_psi": 0.018518518518518517 }, ) == CalcLine( line=deque([ "alpha_eta_psi", "=", "4", "/", deque(["y", "**", deque(["a", "+", "1"])]), deque(["=", 0.018518518518518517]), ]), comment=" Comment", latex="", ) assert handcalcs.handcalcs.add_result_values_to_line( ConditionalLine( condition=deque(["x", "<", "1"]), condition_type="if", expressions=deque([ ParameterLine(line=deque(["b", "=", 2]), comment="", latex="") ]), raw_condition="x < 1", raw_expression="b = x", true_condition=deque([]), true_expressions=deque([]), comment=" Comment", latex="", ), { "x": 2, "b": 2, "c": 2 }, ) == ConditionalLine( condition=deque(["x", "<", "1"]), condition_type="if", expressions=deque( [ParameterLine(line=deque(["b", "=", 2]), comment="", latex="")]), raw_condition="x < 1", raw_expression="b = x", true_condition=deque([]), true_expressions=deque([]), comment=" Comment", latex="", )