def invert(self, node_id): """ Performs a logical inversion on the diagram :type node_id: int :rtype: int """ node = self.get_node(node_id) if not node.is_terminal(): if node.child_true == self.one_id and node.child_false == self.zero_id: return self.internal(node.test, self.zero_id, self.one_id) elif node.child_true == self.zero_id and node.child_false == self.one_id: return self.internal(node.test, self.one_id, self.zero_id) else: if node_id == self.one_id: return self.zero_id elif node_id == self.zero_id: return self.one_id else: raise RuntimeError( "Cannot invert leaf node that is not one or zero: {}". format(node)) # minus_one = self.terminal("-1") # return self.apply(Multiplication, self.apply(Summation, node_id, minus_one), minus_one) to_invert = self.diagram(node_id) from pyxadd import leaf_transform return leaf_transform.transform_leaves(self._transform_invert, to_invert)
def invert(self, node_id): """ Performs a logical inversion on the diagram :type node_id: int :rtype: int """ node = self.get_node(node_id) if not node.is_terminal(): if node.child_true == self.one_id and node.child_false == self.zero_id: return self.internal(node.test, self.zero_id, self.one_id) elif node.child_true == self.zero_id and node.child_false == self.one_id: return self.internal(node.test, self.one_id, self.zero_id) else: if node_id == self.one_id: return self.zero_id elif node_id == self.zero_id: return self.one_id else: raise RuntimeError("Cannot invert leaf node that is not one or zero: {}".format(node)) # minus_one = self.terminal("-1") # return self.apply(Multiplication, self.apply(Summation, node_id, minus_one), minus_one) to_invert = self.diagram(node_id) from pyxadd import leaf_transform return leaf_transform.transform_leaves(self._transform_invert, to_invert)
def invert(self, node_id): """ Performs a logical inversion on the diagram :type node_id: int :rtype: int """ node = self.get_node(node_id) if node.child_true == self.one_id and node.child_false == self.zero_id: return self.internal(node.test, self.zero_id, self.one_id) elif node.child_true == self.zero_id and node.child_false == self.one_id: return self.internal(node.test, self.one_id, self.zero_id) # minus_one = self.terminal("-1") # return self.apply(Multiplication, self.apply(Summation, node_id, minus_one), minus_one) to_invert = self.diagram(node_id) from pyxadd import leaf_transform return leaf_transform.transform_leaves(self._transform_invert, to_invert)
def inversion2(root_id): to_invert = pool.diagram(root_id) profile = WalkingProfile(diagram) return leaf_transform.transform_leaves(transform, to_invert)
def test_leaf_transform_simple_test(self): pool = Pool() pool.int_var("x") test1 = pool.bool_test(LinearTest("x", ">=")) leaf_transform.transform_leaves(lambda t, d: d.pool.terminal(2), pool.diagram(test1))
def test_leaf_transform_leaf_node(self): pool = Pool() diagram = pool.diagram(pool.one_id) leaf_transform.transform_leaves(lambda t, d: d.pool.terminal(2), diagram)
def transform_leaves(self, f): transformed_id = transform_leaves(f, self.diagram) diagram = self.diagram.pool.diagram(transformed_id) return self._matrix(diagram, self.row_variables, self.column_variables, [self], self.height, self.width)