Skip to content

Commit

Permalink
fixing issue #52
Browse files Browse the repository at this point in the history
  • Loading branch information
BFuks committed Apr 5, 2022
1 parent e215b6f commit ca85a0c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
20 changes: 16 additions & 4 deletions madanalysis/fastsim/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, id_, vars_):
self.boolean = ['true','false']
self.unary_ops = ['acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh',
'cos', 'cosh', 'erf', 'erfc', 'exp', 'fabs', 'gamma', 'lgamma', 'log',
'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'minus']
self.binary2_ops = [ 'atan2', 'fmod', 'hypot', 'pow']
self.binary1_ops = { '^':1, '+':3, '-':3, '*':2, '/':2, '<=':4 ,'>=':4,
'<':4, '>':4, '==':4, 'and':5, 'or':5}
Expand All @@ -66,6 +66,7 @@ def info(self):

# Main method: creating an ast from a formula
def feed(self, formula_string):
self.logger.debug('Handling the formula: ' + formula_string);
frml = formula_string.replace('**', ' ^ ')
for op in self.binary1_ops.keys():
frml = frml.replace(op, ' ' + op + ' ')
Expand All @@ -81,7 +82,9 @@ def feed(self, formula_string):
frml = frml.replace(op, op.replace(' ',''))
frml = frml.replace('( - ', '( -')
frml = frml.split()
self.logger.debug(' ** Formula ready to be procesed: ' + str(frml));
frml = self.ToBasicLeaves(frml)
self.logger.debug(' ** Formula after basic leaves: ' + str(frml));
while ')' in frml:
id_end = frml.index(')')
id_start = [i for i,x in enumerate(frml[:id_end]) if x=='('][-1]
Expand All @@ -108,6 +111,7 @@ def feed(self, formula_string):
return
del frml[id_start:id_end+1]
frml[id_start:id_start] = sub_tree
self.logger.debug(' ** Formula after parentheses treated: ' + str(frml));
frml = self.MakeConnections(frml)
if frml==False:
self.reset()
Expand Down Expand Up @@ -183,41 +187,49 @@ def MakeConnections(self, sub_formula):

# replacing all constants from the formulas by leaves
def ToBasicLeaves(self, formula):
new_formula = []
new_formula = []; last = ""
self.logger.debug(" ** ToBasicLeaves formula: " + str(formula));
for elem in formula:
# constants
if re.match("""(?x) ^ [+-]?\ * ( \d+ ( \.\d* )? |\.\d+ ) ([eE][+-]?\d+)? $""", elem):
new_leaf = Leaf(self.size(), 'cst', elem, [], [])
new_formula.append(new_leaf)
self.leaves.append(new_leaf)
last='';
# variables
elif elem.upper() in self.variables and elem!='gamma':
new_leaf = Leaf(self.size(), 'var', elem.upper(), [], [])
new_formula.append(new_leaf)
self.leaves.append(new_leaf)
last='';
# unary operators
elif elem.lower() in self.unary_ops:
new_leaf = Leaf(self.size(), 'un_op', elem.lower(), [], [])
elif (elem.lower() in self.unary_ops) or (elem=='-' and last=='bin'):
new_leaf = Leaf(self.size(), 'un_op', elem.replace('-','minus').lower(), [], [])
new_formula.append(new_leaf)
self.leaves.append(new_leaf)
last='';
# unary operators
elif elem.lower() in self.binary2_ops:
new_leaf = Leaf(self.size(), 'bin2_op', elem.lower(), [], [])
new_formula.append(new_leaf)
self.leaves.append(new_leaf)
last = "bin";
# booleans
elif elem.lower() in self.boolean:
new_leaf = Leaf(self.size(), 'bool', elem.lower(), [], [])
new_formula.append(new_leaf)
self.leaves.append(new_leaf)
last = "bin";
# unary operators
elif elem.lower() in list(self.binary1_ops.keys()):
new_leaf = Leaf(self.size(), 'bin1_op', elem.lower(), [], [])
new_formula.append(new_leaf)
self.leaves.append(new_leaf)
last = "bin";
# only parentheses
else:
new_formula.append(elem)
last='';
return new_formula


Expand Down
10 changes: 5 additions & 5 deletions madanalysis/fastsim/ast_leaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def write(self, tree):
if self.type in ['cst', 'var', 'bool']:
return self.name
elif self.type == 'un_op' and len(self.daughters)==1:
return self.name + '(' + tree.get(self.daughters[0]).write(tree) + ')'
if self.name =='minus': return '-' + tree.get(self.daughters[0]).write(tree)
else: return self.name + '(' + tree.get(self.daughters[0]).write(tree) + ')'
elif self.type == 'bin2_op' and len(self.daughters)==2:
return self.name + '(' + tree.get(self.daughters[0]).write(tree) + ', ' + \
tree.get(self.daughters[1]).write(tree) + ')'
Expand All @@ -88,11 +89,10 @@ def write_cpp(self, tree):
elif self.type in ['var', 'bool']:
return self.name
elif self.type == 'un_op' and len(self.daughters)==1 and self.name!='-':
return 'std::'+self.name.replace('gamma','tgamma') + '(' +\
tree.get(self.daughters[0]).write_cpp(tree) + ')'
if self.name=='minus': return '-' + tree.get(self.daughters[0]).write_cpp(tree)
else : return 'std::'+self.name.replace('gamma','tgamma') + '(' + tree.get(self.daughters[0]).write_cpp(tree) + ')'
elif self.type == 'un_op' and len(self.daughters)==1 and self.name=='-':
return self.name + '(' +\
tree.get(self.daughters[0]).write_cpp(tree) + ')'
return self.name + '(' + tree.get(self.daughters[0]).write_cpp(tree) + ')'
elif self.type == 'bin2_op' and len(self.daughters)==2:
return 'std::'+self.name + '(' +\
tree.get(self.daughters[0]).write_cpp(tree) + ', ' + \
Expand Down

0 comments on commit ca85a0c

Please sign in to comment.