1- import math
21import importlib
32
43import MLC .Log .log as lg
@@ -98,6 +97,7 @@ def op_compute(self, arg_list):
9897
9998class Division_Node (Internal_Node ):
10099 PROTECTION = 0.001
100+ SIMPLIFY_PROTECTION = 0.01
101101
102102 def __init__ (self ):
103103 Internal_Node .__init__ (self , "/" , 1 )
@@ -107,12 +107,11 @@ def formal(self):
107107
108108 def _process_division (self , dividend , divisor ):
109109 if type (divisor ) == np .ndarray :
110- # Check if at least one element is below the protection value
111- if [x for x in divisor if abs (x ) < Division_Node .PROTECTION ] != []:
112- return np .sign (divisor ) * dividend / np .repeat (Division_Node .PROTECTION , len (divisor ))
110+ new_divisor = [Division_Node .PROTECTION if np .abs (x ) < Division_Node .PROTECTION else np .abs (x ) for x in divisor ]
111+ return np .sign (divisor ) * dividend / np .asarray (new_divisor )
113112 else :
114113 if abs (divisor ) < Division_Node .PROTECTION :
115- return dividend / Division_Node .PROTECTION
114+ return np . sign ( divisor ) * dividend / Division_Node .PROTECTION
116115
117116 return dividend / divisor
118117
@@ -127,7 +126,7 @@ def op_simplify(self):
127126
128127 if not self ._nodes [0 ].is_sensor () and not self ._nodes [1 ].is_sensor ():
129128 # FIXME: Harcoded number. Change it
130- if abs (float (self ._nodes [1 ].to_string ())) < 0.01 :
129+ if abs (float (self ._nodes [1 ].to_string ())) < Division_Node . SIMPLIFY_PROTECTION :
131130 return Leaf_Node (process_float (0 ))
132131 else :
133132 arg = float (self ._nodes [0 ].to_string ()) / float (self ._nodes [1 ].to_string ())
@@ -149,7 +148,7 @@ def formal(self):
149148
150149 def op_simplify (self ):
151150 if not self ._nodes [0 ].is_sensor ():
152- arg = math .sin (float (self ._nodes [0 ].to_string ()))
151+ arg = np .sin (float (self ._nodes [0 ].to_string ()))
153152 return Leaf_Node (process_float (arg ))
154153 else :
155154 return self
@@ -168,7 +167,7 @@ def formal(self):
168167
169168 def op_simplify (self ):
170169 if not self ._nodes [0 ].is_sensor ():
171- arg = math .cos (float (self ._nodes [0 ].to_string ()))
170+ arg = np .cos (float (self ._nodes [0 ].to_string ()))
172171 return Leaf_Node (process_float (arg ))
173172 else :
174173 return self
@@ -179,6 +178,7 @@ def op_compute(self, arg_list):
179178
180179class Logarithm_Node (Internal_Node ):
181180 PROTECTION = 0.00001
181+ SIMPLIFY_PROTECTION = 0.01
182182
183183 def __init__ (self ):
184184 Internal_Node .__init__ (self , "log" , 5 )
@@ -188,9 +188,7 @@ def formal(self):
188188
189189 def _process_arg (self , arg ):
190190 if type (arg ) == np .ndarray :
191- # Check if at least one element is below the protection value
192- if [x for x in arg if abs (x ) < Logarithm_Node .PROTECTION ] != []:
193- return np .repeat (Logarithm_Node .PROTECTION , len (arg ))
191+ return [Logarithm_Node .PROTECTION if np .abs (x ) < Logarithm_Node .PROTECTION else np .abs (x ) for x in arg ]
194192 else :
195193 if abs (arg ) < Logarithm_Node .PROTECTION :
196194 return Logarithm_Node .PROTECTION
@@ -199,10 +197,10 @@ def _process_arg(self, arg):
199197
200198 def op_simplify (self ):
201199 if not self ._nodes [0 ].is_sensor ():
202- if float (self ._nodes [0 ].to_string ()) < 0.01 :
203- arg = math .log (0.01 )
200+ if float (self ._nodes [0 ].to_string ()) < Logarithm_Node . SIMPLIFY_PROTECTION :
201+ arg = np .log (Logarithm_Node . SIMPLIFY_PROTECTION )
204202 else :
205- arg = math .log (float (self ._nodes [0 ].to_string ()))
203+ arg = np .log (float (self ._nodes [0 ].to_string ()))
206204
207205 return Leaf_Node (process_float (arg ))
208206 else :
@@ -224,9 +222,9 @@ def op_simplify(self):
224222 if not self ._nodes [0 ].is_sensor ():
225223 lg .logger_ .debug ("[EXP NODE] Value: " + self ._nodes [0 ].to_string ())
226224 try :
227- arg = math .exp (float (self ._nodes [0 ].to_string ()))
225+ arg = np .exp (float (self ._nodes [0 ].to_string ()))
228226 except OverflowError :
229- # FIXME: See what to do with this expression, because there are problems with
227+ # FIXME: See what to do with this expression, because there are problems when
230228 # an infinite value is the argumento of a sinusoidal function
231229 return Leaf_Node (process_float (float ("inf" )))
232230
@@ -248,7 +246,7 @@ def formal(self):
248246
249247 def op_simplify (self ):
250248 if not self ._nodes [0 ].is_sensor ():
251- arg = math .tanh (float (self ._nodes [0 ].to_string ()))
249+ arg = np .tanh (float (self ._nodes [0 ].to_string ()))
252250 return Leaf_Node (process_float (arg ))
253251 else :
254252 return self
0 commit comments