Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_wrong_value(self):
wrong = Node(Operations.MINUS,
Node(Operations.MULTIPLICATION,
left=Node(Operations.IDENTITY, value='x'),
right=Node(Operations.NUMBER, value=4)),
Node(Operations.MULTIPLICATION,
left=Node(Operations.IDENTITY, value='y'),
right=Node(Operations.NUMBER, value=2)))
e = Expression(root=wrong, variables=['x', 'y'])
self.assertGreater(self.f.expression_value(e), 0.0)
def test_pickle_number(self):
operation = Operations.NUMBER
returned_operation = pickle.loads(pickle.dumps(operation))
self._test_for_equality(operation, returned_operation)
def __setstate__(self, state):
"""
This method is being called while unpickling.
"""
self.value = state[self._value_dict_key]
self.operation = Operation(None, None)
self.operation.__setstate__(state[self._operation_dict_key])
if self._left_node_dict_key in state:
self.left = Node(Operations.NUMBER)
self.left.__setstate__(state[self._left_node_dict_key])
if self._right_node_dict_key in state:
self.right = Node(Operations.NUMBER)
self.right.__setstate__(state[self._right_node_dict_key])
def __setstate__(self, state):
"""
Initializes operation where unpickling
"""
if state[self._dict_key] == 'number':
operator = Operations.NUMBER
elif state[self._dict_key] == 'variable':
operator = Operations.IDENTITY
elif state[self._dict_key] == '+':
operator = Operations.PLUS
elif state[self._dict_key] == '-':
operator = Operations.MINUS
elif state[self._dict_key] == '*':
operator = Operations.MULTIPLICATION
elif state[self._dict_key] == '/':
operator = Operations.DIVISION
elif state[self._dict_key] == 'sin':
operator = Operations.SIN
elif state[self._dict_key] == 'cos':
operator = Operations.COS
self._init_from_operation(operator)
"""
Simplifies the current node and all its subtrees according
to the simple arithmetic rules.
Return True only if the current node or at least one
of its subtrees was modified during the simplification.
"""
#TODO: add more rules
#leave only 3 digits after decimal point
if self.is_number():
self.value = round(self.value * 1000) / 1000
#calculate unary function for number
if self.is_unary() and self.left.is_number():
self.value = self.value_in_point({})
self.operation = Operations.NUMBER
self.left = None
return True
#calculate binary function for two numbers
if (self.is_binary() and self.left.is_number() and
self.right.is_number()):
self.value = self.value_in_point({})
self.operation = Operations.NUMBER
self.left = self.right = None
return True
#calculate x / x
if (self.is_binary() and
self.left.is_variable() and self.right.is_variable() and
self.left.value == self.right.value and self.operation == Operations.DIVISION):
self.value = 1