Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sympy simplify #24

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
46 changes: 34 additions & 12 deletions pytexit/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from six.moves import range
from six.moves import map


alphabet = 'TUVWXYZ'

unicode_tbl = {
'α': 'alpha',
'β': 'beta',
Expand Down Expand Up @@ -427,18 +430,37 @@ def visit_BinOp(self, n):
# Special binary operators
if isinstance(n.op, ast.Div):
if self.simplify_fractions:
left_is_int = self.looks_like_int(left)
right_is_int = self.looks_like_int(right)
if left_is_int or right_is_int:
if left_is_int and right_is_int:
return self.division('%d' % int(float(left)),
'%d' % int(float(right)))
elif left_is_int:
return self.division('%d' % int(float(left)),
self.visit(n.right))
else:
return self.division(self.visit(n.left),
'%d' % int(float(right)))
from sympy.core.sympify import sympify
from sympy.parsing.sympy_parser import parse_expr, standard_transformations, implicit_multiplication_application, convert_xor, split_symbols_custom, _token_splittable
from sympy.printing.latex import latex
from sympy.simplify.simplify import simplify
from sympy import cancel
transformations = (standard_transformations
+ (implicit_multiplication_application,) + (convert_xor,))
# replace E with an unused letter - sympy thinks E=exp(1)
unused_letter = 'A'
for letter in alphabet:
if letter not in left and letter not in right:
unused_letter = letter
break
left = left.replace('E', unused_letter).strip()
right = right.replace('E', unused_letter).strip()
# parse the expression into a sympy expression and simplify
parsed_left = parse_expr(left,
transformations=transformations)
parsed_right = parse_expr(right,
transformations=transformations)
expression = cancel(parsed_left / parsed_right)
if isinstance(n.right, ast.Name):
# if the right is not a division op, then we need to convert
# to latex
expression = latex(expression)
else:
# otherwise we need to use sympy-ish expressions
expression = str(expression)
# turn everything back to E
expression = expression.replace(unused_letter, 'E')
return expression
return self.division(self.visit(n.left), self.visit(n.right))
elif isinstance(n.op, ast.FloorDiv):
return r'\left\lfloor\frac{%s}{%s}\right\rfloor' % \
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"Operating System :: OS Independent"],
install_requires=[
'six', # python 2-3 compatibility],
'sympy'
],
scripts=[
'scripts/py2tex'],
Expand Down