2014-01-19 20:39:35 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
2014-02-21 05:27:33 +00:00
|
|
|
from .render import Render
|
|
|
|
from .fraction import Fraction
|
2014-02-22 09:38:57 +00:00
|
|
|
from .polynom import Polynom
|
2014-02-26 11:48:41 +00:00
|
|
|
from .generic import first_elem, last_elem
|
2014-01-19 20:39:35 +00:00
|
|
|
|
|
|
|
# ------------------------
|
|
|
|
# A infix to postfix list convertor
|
|
|
|
|
2014-02-26 11:48:41 +00:00
|
|
|
p2i_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":": ":", "^":"^"}
|
2014-01-19 20:39:35 +00:00
|
|
|
p2i_postfix = {}
|
|
|
|
p2i_other = {"(": "(", ")": ")"}
|
|
|
|
|
|
|
|
post2in_fix = Render(p2i_infix, p2i_postfix, p2i_other, join = False)
|
|
|
|
|
2014-02-26 11:48:41 +00:00
|
|
|
# ------------------------
|
|
|
|
# A console render
|
|
|
|
|
|
|
|
def txtMult(op1,op2):
|
|
|
|
""" Tex render for *
|
|
|
|
Cases where \\times won't be displayed
|
|
|
|
* nbr letter
|
|
|
|
* nbr (
|
|
|
|
* )(
|
|
|
|
"""
|
|
|
|
first_nbr = type(op1) in [int, Fraction]
|
|
|
|
seg_letter = type(op2) == str and op2.isalpha()
|
|
|
|
first_par = (first_elem(op2) == "(")
|
|
|
|
seg_par = (last_elem(op1) == ")")
|
|
|
|
|
|
|
|
if (first_nbr and (seg_letter or seg_par)) \
|
|
|
|
or (first_par and seg_par):
|
|
|
|
return [op1, op2]
|
|
|
|
else:
|
|
|
|
return [op1, "*", op2]
|
|
|
|
|
|
|
|
txt_infix = {"+": "+", "-": "-", "*": txtMult, "/" : "/", ":":":", "^":"^"}
|
|
|
|
txt_postfix = {}
|
|
|
|
txt_other = {"(": "(", ")": ")"}
|
|
|
|
|
|
|
|
txt_render = Render(txt_infix, txt_postfix, txt_other)
|
|
|
|
|
2014-01-19 20:39:35 +00:00
|
|
|
# ------------------------
|
|
|
|
# A latex render
|
|
|
|
|
|
|
|
def texSlash(op1, op2):
|
2014-02-25 14:59:22 +00:00
|
|
|
""" Tex render for / """
|
2014-01-19 20:39:35 +00:00
|
|
|
if not Render.isNumerande(op1) and op1[0] == "(" and op1[-1] == ")":
|
|
|
|
op1 = op1[1:-1]
|
|
|
|
if not Render.isNumerande(op2) and op2[0] == "(" and op2[-1] == ")":
|
|
|
|
op2 = op2[1:-1]
|
|
|
|
return ["\\frac{" , op1 , "}{" , op2 , "}"]
|
|
|
|
|
|
|
|
def texFrac(frac):
|
2014-02-25 14:59:22 +00:00
|
|
|
""" Tex render for Fractions"""
|
2014-01-19 20:39:35 +00:00
|
|
|
return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"]
|
|
|
|
|
2014-01-28 22:13:16 +00:00
|
|
|
def texMult(op1,op2):
|
2014-02-26 11:48:41 +00:00
|
|
|
""" Tex render for *
|
|
|
|
Cases where \\times won't be displayed
|
|
|
|
* nbr letter
|
|
|
|
* nbr (
|
|
|
|
* )(
|
|
|
|
"""
|
|
|
|
first_nbr = type(op1) in [int, Fraction]
|
|
|
|
seg_letter = type(op2) == str and op2.isalpha()
|
|
|
|
first_par = (first_elem(op2) == "(")
|
|
|
|
seg_par = (last_elem(op1) == ")")
|
|
|
|
|
|
|
|
if (first_nbr and (seg_letter or seg_par)) \
|
|
|
|
or (first_par and seg_par):
|
|
|
|
return [op1, op2]
|
2014-01-28 22:13:16 +00:00
|
|
|
else:
|
|
|
|
return [op1, "\\times", op2]
|
|
|
|
|
|
|
|
tex_infix = {"+": " + ", "-": " - ", "*": texMult , ":": ":", "^":"^"}
|
2014-01-19 20:39:35 +00:00
|
|
|
tex_postfix = {"/": texSlash}
|
|
|
|
tex_other = {"(": "(", ")": ")"}
|
2014-02-25 15:52:18 +00:00
|
|
|
tex_type_render = {str:str, int: str, Fraction: texFrac, Polynom: str}
|
2014-01-19 20:39:35 +00:00
|
|
|
|
|
|
|
tex_render = Render(tex_infix, tex_postfix, tex_other, type_render = tex_type_render)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-02-26 11:48:41 +00:00
|
|
|
#exp = [2, 5, '^', 1, '-', 3, 4, '*', ':']
|
|
|
|
#print(txt_render(exp))
|
|
|
|
#exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':']
|
|
|
|
exp = [2, -3, "*"]
|
2014-01-19 20:39:35 +00:00
|
|
|
print(tex_render(exp))
|
2014-02-26 11:48:41 +00:00
|
|
|
#exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, '+']
|
|
|
|
#print(post2in_fix(exp))
|
2014-01-19 20:39:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|