-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_to_math_latex.py
78 lines (57 loc) · 1.81 KB
/
python_to_math_latex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class T:
def latex(self):
return ''
def __repr__(self):
return self.latex()
class BinOp(T):
def __init__(self, a, b, op):
self.a = a
self.b = b
self.op = op
def latex(self):
return self.a.latex() + ' ' + self.op + ' ' + self.b.latex()
def __truediv__(self, other):
return Frac(self, other)
def __mul__(self, other):
return Product(self, other)
def __add__(self, other):
return BinOp(self, other, '+')
def __sub__(self, other):
return BinOp(self, other, '-')
class Frac(BinOp):
def __init__(self, a, b):
super().__init__(a, b, '')
def latex(self):
return '\\frac{' + self.a.latex() + '}{' + self.b.latex() + '}'
class Product(BinOp):
NOTHING = ''
CDOT = r'\cdot'
TIMES = r'\times'
SPACE = r'\,'
DEFAULT = NOTHING # can be changed
@classmethod
def setdefault(cls, x):
cls.DEFAULT = x
def __init__(self, a, b, type=None):
if type is None:
type = self.DEFAULT
super().__init__(a, b, type)
def latex(self):
return self.a.latex() + ' ' + self.op + ' ' * bool(self.op) + self.b.latex()
class Term(BinOp):
def __init__(self, x):
self.x = x
def latex(self):
return self.x
def populate_terms(*items, dictionary=None):
if dictionary is None:
dictionary = globals()
for item in items:
dictionary[item] = Term(item)
def test():
assert Term('V_out').latex() == 'V_out'
assert Term(r'V_{\text{out}}').latex() == r'V_{\text{out}}'
V_out = Term(r'V_{\text{out}}')
assert V_out.latex() == r'V_{\text{out}}'
assert Frac(Term('x'), Term('y')).latex() == '\\frac{x}{y}'
assert (Term('x') / Term('y')).latex() == '\\frac{x}{y}'