How to use the expression.Operation function in Expression

To help you get started, we’ve selected a few Expression examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
action=(lambda x: x))
    IDENTITY = Operation(operation_type=_variable,
                         action=(lambda x: x))
    PLUS =  Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x + y),
                      string_representation='+')
    MINUS = Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x - y),
                      string_representation='-')
    MULTIPLICATION = Operation(operation_type=_binary_operation,
                               action=(lambda x, y: x * y),
                               string_representation='*')
    DIVISION = Operation(operation_type=_binary_operation,
                         action=(lambda x, y: x / y if y != 0 else x / 0.000001),
                         string_representation='/')
    SIN = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.sin(x)),
                    string_representation='sin')
    COS = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.cos(x)),
                    string_representation='cos')

    @classmethod
    def get_unary_operations(cls):
        """
        Returns list of unary operations.
        Number and variable are not unary operations
        """
        return [Operations.SIN, Operations.COS]

    @classmethod
    def get_binary_operations(cls):
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
def __init__(self, operation, left=None, right=None, value=None):
        """
        Initializes node of the expression tree.
        operation - Operation object.
        If not - NotSupportedOperationError is thrown.
        Also left and right subtrees may be passed.
        value - only for NUMBER and IDENTITY.
        """
        if not isinstance(operation, Operation):
            raise NotSupportedOperationError(operation)
        self.operation = operation
        self.left = left
        self.right = right
        self.value = value
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
_binary_operation = 3

    NUMBER = Operation(operation_type=_number,
                       action=(lambda x: x))
    IDENTITY = Operation(operation_type=_variable,
                         action=(lambda x: x))
    PLUS =  Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x + y),
                      string_representation='+')
    MINUS = Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x - y),
                      string_representation='-')
    MULTIPLICATION = Operation(operation_type=_binary_operation,
                               action=(lambda x, y: x * y),
                               string_representation='*')
    DIVISION = Operation(operation_type=_binary_operation,
                         action=(lambda x, y: x / y if y != 0 else x / 0.000001),
                         string_representation='/')
    SIN = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.sin(x)),
                    string_representation='sin')
    COS = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.cos(x)),
                    string_representation='cos')

    @classmethod
    def get_unary_operations(cls):
        """
        Returns list of unary operations.
        Number and variable are not unary operations
        """
        return [Operations.SIN, Operations.COS]
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
PLUS =  Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x + y),
                      string_representation='+')
    MINUS = Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x - y),
                      string_representation='-')
    MULTIPLICATION = Operation(operation_type=_binary_operation,
                               action=(lambda x, y: x * y),
                               string_representation='*')
    DIVISION = Operation(operation_type=_binary_operation,
                         action=(lambda x, y: x / y if y != 0 else x / 0.000001),
                         string_representation='/')
    SIN = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.sin(x)),
                    string_representation='sin')
    COS = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.cos(x)),
                    string_representation='cos')

    @classmethod
    def get_unary_operations(cls):
        """
        Returns list of unary operations.
        Number and variable are not unary operations
        """
        return [Operations.SIN, Operations.COS]

    @classmethod
    def get_binary_operations(cls):
        """
        Returns list of all possible binary operations
        """
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
self.action = operation.action
        if hasattr(operation, 'string_representation'):
            self.string_representation = operation.string_representation

class Operations:
    """
    Class represents all possible operations.
    """
    _number = 0
    _variable = 1
    _unary_operation = 2
    _binary_operation = 3

    NUMBER = Operation(operation_type=_number,
                       action=(lambda x: x))
    IDENTITY = Operation(operation_type=_variable,
                         action=(lambda x: x))
    PLUS =  Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x + y),
                      string_representation='+')
    MINUS = Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x - y),
                      string_representation='-')
    MULTIPLICATION = Operation(operation_type=_binary_operation,
                               action=(lambda x, y: x * y),
                               string_representation='*')
    DIVISION = Operation(operation_type=_binary_operation,
                         action=(lambda x, y: x / y if y != 0 else x / 0.000001),
                         string_representation='/')
    SIN = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.sin(x)),
                    string_representation='sin')
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
_number = 0
    _variable = 1
    _unary_operation = 2
    _binary_operation = 3

    NUMBER = Operation(operation_type=_number,
                       action=(lambda x: x))
    IDENTITY = Operation(operation_type=_variable,
                         action=(lambda x: x))
    PLUS =  Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x + y),
                      string_representation='+')
    MINUS = Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x - y),
                      string_representation='-')
    MULTIPLICATION = Operation(operation_type=_binary_operation,
                               action=(lambda x, y: x * y),
                               string_representation='*')
    DIVISION = Operation(operation_type=_binary_operation,
                         action=(lambda x, y: x / y if y != 0 else x / 0.000001),
                         string_representation='/')
    SIN = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.sin(x)),
                    string_representation='sin')
    COS = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.cos(x)),
                    string_representation='cos')

    @classmethod
    def get_unary_operations(cls):
        """
        Returns list of unary operations.
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
"""
    Class represents all possible operations.
    """
    _number = 0
    _variable = 1
    _unary_operation = 2
    _binary_operation = 3

    NUMBER = Operation(operation_type=_number,
                       action=(lambda x: x))
    IDENTITY = Operation(operation_type=_variable,
                         action=(lambda x: x))
    PLUS =  Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x + y),
                      string_representation='+')
    MINUS = Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x - y),
                      string_representation='-')
    MULTIPLICATION = Operation(operation_type=_binary_operation,
                               action=(lambda x, y: x * y),
                               string_representation='*')
    DIVISION = Operation(operation_type=_binary_operation,
                         action=(lambda x, y: x / y if y != 0 else x / 0.000001),
                         string_representation='/')
    SIN = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.sin(x)),
                    string_representation='sin')
    COS = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.cos(x)),
                    string_representation='cos')

    @classmethod
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
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])
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
self.string_representation = operation.string_representation

class Operations:
    """
    Class represents all possible operations.
    """
    _number = 0
    _variable = 1
    _unary_operation = 2
    _binary_operation = 3

    NUMBER = Operation(operation_type=_number,
                       action=(lambda x: x))
    IDENTITY = Operation(operation_type=_variable,
                         action=(lambda x: x))
    PLUS =  Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x + y),
                      string_representation='+')
    MINUS = Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x - y),
                      string_representation='-')
    MULTIPLICATION = Operation(operation_type=_binary_operation,
                               action=(lambda x, y: x * y),
                               string_representation='*')
    DIVISION = Operation(operation_type=_binary_operation,
                         action=(lambda x, y: x / y if y != 0 else x / 0.000001),
                         string_representation='/')
    SIN = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.sin(x)),
                    string_representation='sin')
    COS = Operation(operation_type=_unary_operation,
                    action=(lambda x: math.cos(x)),
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
def _init_from_operation(self, operation):
        self._operation_type = operation._operation_type
        self.action = operation.action
        if hasattr(operation, 'string_representation'):
            self.string_representation = operation.string_representation

class Operations:
    """
    Class represents all possible operations.
    """
    _number = 0
    _variable = 1
    _unary_operation = 2
    _binary_operation = 3

    NUMBER = Operation(operation_type=_number,
                       action=(lambda x: x))
    IDENTITY = Operation(operation_type=_variable,
                         action=(lambda x: x))
    PLUS =  Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x + y),
                      string_representation='+')
    MINUS = Operation(operation_type=_binary_operation,
                      action=(lambda x, y: x - y),
                      string_representation='-')
    MULTIPLICATION = Operation(operation_type=_binary_operation,
                               action=(lambda x, y: x * y),
                               string_representation='*')
    DIVISION = Operation(operation_type=_binary_operation,
                         action=(lambda x, y: x / y if y != 0 else x / 0.000001),
                         string_representation='/')
    SIN = Operation(operation_type=_unary_operation,