Skip to content

Commit 3c14937

Browse files
Merge pull request #74 from lambda-feedback/tr105-ln-instead-of-log
Fixed so that preview shows 'ln' for natural logarithm and prints bas…
2 parents 7a9c098 + 4cc561b commit 3c14937

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

app/evaluation_tests.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
("exp", "Bexp(x)exp(x)", "B*exp(2*x)", r"B e^{x} e^{x}"),
3333
("exp2", "a+b*E^2", "a+b*exp(2)", r"a + b e^{2}"),
3434
("exp3", "a+b*e^2", "a+b*exp(2)", r"a + b e^{2}"),
35-
("log", "Bexp(log(10))", "10B", r"B e^{\log{\left(10 \right)}}"),
35+
("ln", "Bexp(ln(10))", "10B", r"B e^{\ln{\left(10 \right)}}"),
36+
("log10", "B*10^(log(10,10))", "10B", r"10^{\log_{10}{\left(10 \right)}} B"),
37+
("logb", "B*b^(log(a,b))", "aB", r"B b^{\log_{b}{\left(a \right)}}"),
3638
("sqrt", "Bsqrt(4)", "2B", r"\sqrt{4} B"),
3739
("sign", "Bsign(1)", "B", r"B \operatorname{sign}{\left(1 \right)}"),
3840
("abs", "BAbs(-2)", "2B", r"B \left|{-2}\right|"),
@@ -126,7 +128,7 @@ def test_simple_polynomial_with_input_symbols_correct(self, response, answer):
126128
params = {
127129
"strict_syntax": False,
128130
"symbols": {
129-
"longName": {"aliases": [], "latex": "\\(\\mathrm\{longName\}\\)"}
131+
"longName": {"aliases": [], "latex": r"\(\mathrm{longName}\)"}
130132
}
131133
}
132134
result = evaluation_function(response, answer, params)

app/expression_utilities.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,32 @@
1616
import re
1717
from typing import Dict, List, TypedDict
1818

19+
class ModifiedLatexPrinter(LatexPrinter):
20+
"""Modified LatexPrinter class that prints logarithms other than the natural logarithm correctly.
21+
"""
22+
def _print_log(self, expr, exp=None):
23+
if self._settings["ln_notation"] and len(expr.args) < 2:
24+
log_not = r"\ln"
25+
else:
26+
log_not = r"\log"
27+
if len(expr.args) > 1:
28+
base = self._print(expr.args[1])
29+
log_not = r"\log_{%s}" % base
30+
tex = r"%s{\left(%s \right)}" % (log_not, self._print(expr.args[0]))
31+
32+
if exp is not None:
33+
return r"%s^{%s}" % (tex, exp)
34+
else:
35+
return tex
36+
1937
elementary_functions_names = [
2038
('sin', []), ('sinc', []), ('csc', ['cosec']), ('cos', []), ('sec', []), ('tan', []), ('cot', ['cotan']),
2139
('asin', ['arcsin']), ('acsc', ['arccsc', 'arccosec', 'acosec']), ('acos', ['arccos']), ('asec', ['arcsec']),
2240
('atan', ['arctan']), ('acot', ['arccot', 'arccotan', 'acotan']), ('atan2', ['arctan2']),
2341
('sinh', []), ('cosh', []), ('tanh', []), ('csch', ['cosech']), ('sech', []),
2442
('asinh', ['arcsinh']), ('acosh', ['arccosh']), ('atanh', ['arctanh']),
2543
('acsch', ['arccsch', 'arccosech']), ('asech', ['arcsech']),
26-
('exp', ['Exp']), ('E', ['e']), ('log', []),
44+
('exp', ['Exp']), ('E', ['e']), ('log', ['ln']),
2745
('sqrt', []), ('sign', []), ('Abs', ['abs']), ('Max', ['max']), ('Min', ['min']), ('arg', []), ('ceiling', ['ceil']), ('floor', []),
2846
# Below this line should probably not be collected with elementary functions. Some like 'common operations' would be a better name
2947
('summation', ['sum','Sum']), ('Derivative', ['diff']),
@@ -453,10 +471,18 @@ def latex_symbols(symbols):
453471
return symbol_dict
454472

455473

456-
def sympy_to_latex(equation, symbols):
457-
latex_out = LatexPrinter(
458-
{"symbol_names": latex_symbols(symbols)}
459-
).doprint(equation)
474+
def sympy_to_latex(equation, symbols, settings=None):
475+
default_settings = {
476+
"symbol_names": latex_symbols(symbols),
477+
"ln_notation": True,
478+
}
479+
if settings is None:
480+
settings = default_settings
481+
else:
482+
for key in default_settings.keys():
483+
if key not in settings.keys():
484+
settings[key] = default_settings[key]
485+
latex_out = ModifiedLatexPrinter(settings).doprint(equation)
460486
return latex_out
461487

462488

app/preview.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import sympy
44
from latex2sympy2 import latex2sympy
5-
from sympy.printing.latex import LatexPrinter
65
from typing_extensions import NotRequired
76

87
from sympy.parsing.sympy_parser import T as parser_transformations
@@ -16,6 +15,7 @@
1615
substitute_input_symbols,
1716
SymbolDict,
1817
sympy_symbols,
18+
sympy_to_latex,
1919
)
2020

2121
from .feedback.symbolic_equal import internal as symbolic_equal_internal_messages
@@ -146,9 +146,7 @@ def preview_function(response: str, params: Params) -> Result:
146146
latex_out = []
147147
sympy_out = []
148148
for expression in expression_list:
149-
latex_out.append(
150-
LatexPrinter({"symbol_names": latex_symbols(symbols)}).doprint(expression)
151-
)
149+
latex_out.append(sympy_to_latex(expression, symbols))
152150
sympy_out.append(str(expression))
153151

154152
if len(sympy_out) == 1:

0 commit comments

Comments
 (0)