Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/evaluation_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_multi_character_implicit_multi_variable(self):
("e**ea", False, True),
("e**Ea", False, True),
("e^{ea}", True, True),
# ("e^{Ea}", True, True), # TODO: Support aliases for latex input
("e^{Ea}", True, True),
]
)
def test_e_latex(self, response, is_latex, is_correct):
Expand Down
34 changes: 30 additions & 4 deletions app/preview_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ def test_eulers_number_notation(self, response, is_latex, elementary_functions,
("e**ea", False, "e^{ea}", "E**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
("e**Ea", False, "e^{ea}", "E**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
("e^{ea}", True, "e^{ea}", "exp(ea)", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
# ("e^{Ea}", True, "e^{Ea}", "e**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}), # TODO: Clarify if we want to be able to use aliases for LaTeX?
("e^{Ea}", True, "e^{Ea}", "e**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
("e**aea", False, "e^{aea}", "E**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
("e**aEa", False, "e^{aea}", "E**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
("e^{aea}", True, "e^{aea}", "exp(aea)", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
# ("e^{aEa}", True, "e^{aEa}", "e**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}), # TODO: Clarify if we want to be able to use aliases for LaTeX?
("e^{aEa}", True, "e^{aEa}", "e**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
]
)
def test_e_latex(self, response, is_latex, response_latex, response_sympy, symbols):
Expand All @@ -140,9 +140,35 @@ def test_e_latex(self, response, is_latex, response_latex, response_sympy, symbo
assert "preview" in result.keys()
preview = result["preview"]

assert preview["latex"] == response_latex
assert preview["sympy"] == response_sympy
assert preview["latex"] == response_latex, "latex_error"
assert preview["sympy"] == response_sympy, "sympy_error"

@pytest.mark.parametrize(
"response, is_latex, response_latex, response_sympy, symbols", [
("ab", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("Ab", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("aB", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("AB", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("ab", True, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("Ab", True, "Ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("aB", True, "aB", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
("AB", True, "AB", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
]
)
def test_alias(self, response, is_latex, response_latex, response_sympy, symbols):
params = {
"is_latex": is_latex,
"strict_syntax": False,
"elementary_functions": True,
"symbols": symbols,
}

result = preview_function(response, params)
assert "preview" in result.keys()
preview = result["preview"]

assert preview["latex"] == response_latex, "latex error"
assert preview["sympy"] == response_sympy, "sympy error"

@pytest.mark.parametrize(
"response, is_latex, response_latex, response_sympy",
Expand Down
24 changes: 22 additions & 2 deletions app/utility/preview_utilities.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import re
from typing import TypedDict

from sympy.parsing.sympy_parser import standard_transformations, implicit_multiplication_application
from typing_extensions import NotRequired

from sympy import Symbol
from sympy import Symbol, parse_expr, srepr
from latex2sympy2 import latex2sympy

from copy import deepcopy
Expand Down Expand Up @@ -177,6 +179,24 @@ def parse_latex(response: str, symbols: SymbolDict, simplify: bool, parameters=N
substitutions[latex_symbol_str_postprocess] = Symbol(sympy_symbol_str)


aliases = symbols[sympy_symbol_str]['aliases']
transformations = (standard_transformations + (implicit_multiplication_application,))
for alias in aliases:
if not alias.strip():
continue
try:
parsed_alias = parse_expr(
alias,
transformations=transformations,
global_dict={},
local_dict={'Symbol': Symbol,'E': Symbol("E")}
)
substitutions[parsed_alias] = Symbol(sympy_symbol_str)
except Exception as e:
print(e)
substitutions[Symbol(alias)] = Symbol(sympy_symbol_str)


parsed_responses = set()
for expression in response_set:
try:
Expand All @@ -195,7 +215,7 @@ def parse_latex(response: str, symbols: SymbolDict, simplify: bool, parameters=N
if simplify is True:
expression_postprocess = expression_postprocess.simplify()

parsed_responses.add(str(expression_postprocess.xreplace(substitutions)))
parsed_responses.add(str(expression_postprocess.subs(substitutions)))

if len(parsed_responses) < 2:
return parsed_responses.pop()
Expand Down