Skip to content

Commit

Permalink
transformers refactoring latex i substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
idanpa committed Mar 8, 2024
1 parent 55df0bd commit 7055c02
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion calcpy/tests/test_transformers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sympy import symbols
from sympy import symbols, I
from datetime import datetime

def test_autodate(ip):
Expand Down Expand Up @@ -47,8 +47,10 @@ def test_auto_latex(ip):
assert ip.run_cell('$x+y$ == x+y').result == True
ip.run_cell('a,b = 3,4')
assert ip.run_cell('$a+b$').result == 7
assert ip.run_cell('$i$').result == I
ip.calcpy.auto_latex_sub = False
assert ip.run_cell('$a+b$').result == symbols('a') + symbols('b')
assert ip.run_cell('$i$').result == I

def test_auto_symbols(ip):
assert ip.run_cell('x+y_1+z2').result == symbols('x')+symbols('y_1')+symbols('z2')
Expand Down
3 changes: 2 additions & 1 deletion calcpy/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def dateparse(datetime_string):
def parse_latex(s):
ip = IPython.get_ipython()
expr = sympy.parsing.latex.parse_latex(s)
expr = expr.subs({'i': sympy.I})
if not ip.calcpy.auto_latex_sub:
return expr
subs = {sym.name : ip.user_ns.get(sym.name,sympy.symbols(sym.name)) for sym in expr.free_symbols}
Expand Down Expand Up @@ -74,7 +75,7 @@ def raw_code_transformer(code):
for m in re.finditer(latex_pattern, code):
key = hash(m.group())
code = code.replace(m.group(), f'({key})')
latex_matches[key] = f'parse_latex(r"""{m[1]}""").subs({{symbols("i"):sympy.I}})'
latex_matches[key] = f'parse_latex(r"""{m[1]}""")'

code = code.replace('⋅','*')
code = code.replace('ⅈ','i') # for auto product to detect it
Expand Down

0 comments on commit 7055c02

Please sign in to comment.