Skip to content

Commit 896bc51

Browse files
authored
Merge pull request #227 from lambda-feedback/bug/fix-inappropriate-symbol-and-!-for-factorial
Bug/fix inappropriate symbol and ! for factorial
2 parents 51708f8 + f956890 commit 896bc51

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

app/evaluation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ def evaluation_function(response, answer, params, include_test_data=False) -> di
288288
if "!" in response:
289289
evaluation_result.add_feedback(("NOTATION_WARNING_FACTORIAL", symbolic_comparison_internal_messages("NOTATION_WARNING_FACTORIAL")(dict())))
290290

291+
if "!!!" in response:
292+
evaluation_result.add_feedback(
293+
("NOTATION_WARNING_TRIPLE_FACTORIAL", symbolic_comparison_internal_messages("NOTATION_WARNING_TRIPLE_FACTORIAL")(dict())))
294+
291295
reserved_expressions_success, reserved_expressions = parse_reserved_expressions(reserved_expressions_strings, parameters, evaluation_result)
292296
if reserved_expressions_success is False:
293297
return evaluation_result.serialise(include_test_data)

app/feedback/symbolic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"PARSE_ERROR": f"`{inputs.get('x','')}` could not be parsed as a valid mathematical expression. Ensure that correct codes for input symbols are used, correct notation is used, that the expression is unambiguous and that all parentheses are closed.",
2222
"NOTATION_WARNING_EXPONENT": "Note that `^` cannot be used to denote exponentiation, use `**` instead.",
2323
"NOTATION_WARNING_FACTORIAL": "Note that `!` cannot be used to denote factorial, use `factorial(...)` instead.",
24+
"NOTATION_WARNING_TRIPLE_FACTORIAL": "Note that `!!!` is not supported.",
2425
"EXPRESSION_NOT_EQUALITY": "The response was an expression but was expected to be an equality.",
2526
"EQUALITY_NOT_EXPRESSION": "The response was an equality but was expected to be an expression.",
2627
"EQUALITIES_EQUIVALENT": None,

app/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pydot
22
typing_extensions
33
mpmath==1.2.1
4-
sympy==1.12
4+
sympy==1.14
55
antlr4-python3-runtime==4.7.2
66
git+https://github.com/lambda-feedback/latex2sympy.git@master#egg=latex2sympy2

app/tests/symbolic_evaluation_tests.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,6 @@ def test_warning_inappropriate_symbol(self):
775775
'(0,002*6800*v)/1,2',
776776
'(0.002*6800*v)/1.2'
777777
),
778-
(
779-
'-∞',
780-
'-inf'
781-
),
782778
(
783779
'x.y',
784780
'x*y'
@@ -1865,15 +1861,52 @@ def test_sum_in_answer(self, response, answer, value):
18651861
result = evaluation_function(response, answer, params)
18661862
assert result["is_correct"] is value
18671863

1868-
def test_exclamation_mark_for_factorial(self):
1869-
response = "3!"
1870-
answer = "factorial(3)"
1864+
@pytest.mark.parametrize(
1865+
"response, answer, value",
1866+
[
1867+
("3!", "factorial(3)", True),
1868+
("(n+1)!", "factorial(n+1)", True),
1869+
("n!", "factorial(n)", True),
1870+
("a!=b", "factorial(3)", False),
1871+
("2*n!", "2*factorial(n)", True),
1872+
("3!", "3!", True),
1873+
("3*sin(n)!", "3*factorial(sin(n))", True)
1874+
]
1875+
)
1876+
def test_exclamation_mark_for_factorial(self, response, answer, value):
18711877
params = {
18721878
"strict_syntax": False,
18731879
"elementary_functions": True,
18741880
}
18751881
result = evaluation_function(response, answer, params)
1876-
assert result["is_correct"] is True
1882+
assert result["is_correct"] is value
1883+
1884+
@pytest.mark.parametrize(
1885+
"response, answer, value",
1886+
[
1887+
("3!!", "factorial2(3)", True),
1888+
("(n+1)!!", "factorial2(n+1)", True),
1889+
("n!!", "factorial2(n)", True),
1890+
("a!=b", "factorial2(3)", False),
1891+
("2*n!!", "2*factorial2(n)", True),
1892+
("3!!", "3!!", True),
1893+
]
1894+
)
1895+
def test_double_exclamation_mark_for_factorial(self, response, answer, value):
1896+
params = {
1897+
"strict_syntax": False,
1898+
"elementary_functions": True,
1899+
}
1900+
result = evaluation_function(response, answer, params)
1901+
assert result["is_correct"] is value
1902+
1903+
def test_warning_for_triple_factorial(self):
1904+
answer = '2^4!'
1905+
response = '2^4!!!'
1906+
params = {'strict_syntax': False}
1907+
result = evaluation_function(response, answer, params, include_test_data=True)
1908+
assert result["is_correct"] is False
1909+
assert "NOTATION_WARNING_TRIPLE_FACTORIAL" in result["tags"]
18771910

18781911
def test_alternatives_to_input_symbols_takes_priority_over_elementary_function_alternatives(self):
18791912
answer = "Ef*exp(x)"

app/utility/expression_utilities.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,17 +691,20 @@ def parse_expression(expr_string, parsing_params):
691691
substitutions.sort(key=substitutions_sort_key)
692692
if parsing_params["elementary_functions"] is True:
693693
substitutions += protect_elementary_functions_substitutions(expr)
694+
694695
substitutions = list(set(substitutions))
695696
substitutions.sort(key=substitutions_sort_key)
696697
expr = substitute(expr, substitutions)
697698
expr = " ".join(expr.split())
699+
698700
can_split = lambda x: False if x in unsplittable_symbols else _token_splittable(x)
699701
if strict_syntax is True:
700702
transformations = parser_transformations[0:4]+extra_transformations
701703
else:
702704
transformations = parser_transformations[0:5, 6]+extra_transformations+(split_symbols_custom(can_split),)+parser_transformations[8, 9]
703705
if parsing_params.get("rationalise", False):
704706
transformations += parser_transformations[11]
707+
705708
if "=" in expr:
706709
expr_parts = expr.split("=")
707710
lhs = parse_expr(expr_parts[0], transformations=transformations, local_dict=symbol_dict)
@@ -713,6 +716,7 @@ def parse_expression(expr_string, parsing_params):
713716
parsed_expr = parsed_expr.simplify()
714717
else:
715718
parsed_expr = parse_expr(expr, transformations=transformations, local_dict=symbol_dict, evaluate=False)
719+
716720
if not isinstance(parsed_expr, Basic):
717721
raise ValueError(f"Failed to parse Sympy expression `{expr}`")
718722
parsed_expr_set.add(parsed_expr)

0 commit comments

Comments
 (0)