1414
1515
1616class Formula (object ):
17+ """
18+ Examples
19+ --------
20+ >>> f = Formula("x")
21+ >>> f
22+ <Formula: x>
23+ >>> f.ast
24+ [{'val': {'id': 0, 'type': 'mathord', 'text': 'x', 'role': None}, \
25+ 'structure': {'bro': [None, None], 'child': None, 'father': None, 'forest': None}}]
26+ >>> f.elements
27+ [{'id': 0, 'type': 'mathord', 'text': 'x', 'role': None}]
28+ >>> f.variable_standardization(inplace=True)
29+ <Formula: x>
30+ >>> f.elements
31+ [{'id': 0, 'type': 'mathord', 'text': 'x', 'role': None, 'var': 0}]
32+ """
33+
1734 def __init__ (self , formula : (str , List [Dict ]), variable_standardization = False , const_mathord = None ,
1835 * args , ** kwargs ):
36+ """
37+
38+ Parameters
39+ ----------
40+ formula: str or List[Dict]
41+ latex formula string or the parsed abstracted syntax tree
42+ variable_standardization
43+ const_mathord
44+ args
45+ kwargs
46+ """
1947 self ._formula = formula
2048 self ._ast = None
2149 self .reset_ast (
@@ -43,11 +71,15 @@ def variable_standardization(self, inplace=False, const_mathord=None, variable_c
4371 return Formula (ast_tree , is_str = False )
4472
4573 @property
46- def element (self ):
74+ def ast (self ):
4775 return self ._ast
4876
4977 @property
50- def ast (self ) -> (nx .Graph , nx .DiGraph ):
78+ def elements (self ):
79+ return [self .ast_graph .nodes [node ] for node in self .ast_graph .nodes ]
80+
81+ @property
82+ def ast_graph (self ) -> (nx .Graph , nx .DiGraph ):
5183 edges = [(edge [0 ], edge [1 ]) for edge in get_edges (self ._ast ) if edge [2 ] == 3 ]
5284 tree = nx .DiGraph ()
5385 for node in self ._ast :
@@ -67,8 +99,9 @@ def __repr__(self):
6799 else :
68100 return super (Formula , self ).__repr__ ()
69101
70- def reset_ast (self , formula_ensure_str = True , variable_standardization = False , const_mathord = None , * args , ** kwargs ):
71- if formula_ensure_str is True and self .resetable is True :
102+ def reset_ast (self , formula_ensure_str : bool = True , variable_standardization = False , const_mathord = None , * args ,
103+ ** kwargs ):
104+ if formula_ensure_str is True and self .resetable is False :
72105 raise TypeError ("formula must be str, now is %s" % type (self ._formula ))
73106 self ._ast = str2ast (self ._formula , * args , ** kwargs ) if isinstance (self ._formula , str ) else self ._formula
74107 if variable_standardization :
@@ -82,34 +115,49 @@ def resetable(self):
82115
83116
84117class FormulaGroup (object ):
118+ """
119+ Examples
120+ ---------
121+ >>> fg = FormulaGroup(["x + y", "y + x", "z + x"])
122+ >>> fg
123+ <FormulaGroup: <Formula: x + y>;<Formula: y + x>;<Formula: z + x>>
124+ >>> fg = FormulaGroup(["x + y", Formula("y + x"), "z + x"])
125+ >>> fg
126+ <FormulaGroup: <Formula: x + y>;<Formula: y + x>;<Formula: z + x>>
127+ >>> fg = FormulaGroup(["x", Formula("y"), "x"])
128+ >>> fg.elements
129+ [{'id': 0, 'type': 'mathord', 'text': 'x', 'role': None}, {'id': 1, 'type': 'mathord', 'text': 'y', 'role': None},\
130+ {'id': 2, 'type': 'mathord', 'text': 'x', 'role': None}]
131+ >>> fg = FormulaGroup(["x", Formula("y"), "x"], variable_standardization=True)
132+ >>> fg.elements
133+ [{'id': 0, 'type': 'mathord', 'text': 'x', 'role': None, 'var': 0}, \
134+ {'id': 1, 'type': 'mathord', 'text': 'y', 'role': None, 'var': 1}, \
135+ {'id': 2, 'type': 'mathord', 'text': 'x', 'role': None, 'var': 0}]
136+ """
137+
85138 def __init__ (self ,
86- formula_list : (List [( str , Formula , dict ) ]),
139+ formula_list : (list , List [str ], List [ Formula ]),
87140 variable_standardization = False ,
88141 const_mathord = None ,
89142 detach = True
90143 ):
91- """
92-
93- Parameters
94- ----------
95- formula_list: List[str]
96- """
97144 forest = []
98145 self ._formulas = []
99- for index in range (0 , len (formula_list )):
100- formula = formula_list [index ]
146+ for formula in formula_list :
101147 if isinstance (formula , str ):
102- tree = str2ast (
148+ formula = Formula (
103149 formula ,
104150 forest_begin = len (forest ),
105151 )
106- self ._formulas .append (Formula (tree ))
152+ self ._formulas .append (formula )
153+ tree = formula .ast
107154 elif isinstance (formula , Formula ):
108155 if detach :
109156 formula = deepcopy (formula )
110157 tree = formula .reset_ast (
111158 formula_ensure_str = True ,
112159 variable_standardization = False ,
160+ forest_begin = len (forest ),
113161 )
114162 self ._formulas .append (formula )
115163 else :
@@ -149,11 +197,15 @@ def __repr__(self):
149197 return "<FormulaGroup: %s>" % ";" .join ([repr (_formula ) for _formula in self ._formulas ])
150198
151199 @property
152- def element (self ):
200+ def ast (self ):
153201 return self ._forest
154202
155203 @property
156- def ast (self ) -> (nx .Graph , nx .DiGraph ):
204+ def elements (self ):
205+ return [self .ast_graph .nodes [node ] for node in self .ast_graph .nodes ]
206+
207+ @property
208+ def ast_graph (self ) -> (nx .Graph , nx .DiGraph ):
157209 edges = [(edge [0 ], edge [1 ]) for edge in get_edges (self ._forest ) if edge [2 ] == 3 ]
158210 tree = nx .DiGraph ()
159211 for node in self ._forest :
0 commit comments