Source code for mlprodict.grammar.gactions_num

"""
Action definition.


:githublink:`%|py|5`
"""
from .gtypes import MLNumTypeFloat32, MLNumTypeBool
from .gactions import MLActionBinary, MLActionFunctionCall


[docs]class MLActionAdd(MLActionBinary): """ Addition :githublink:`%|py|12` """
[docs] def __init__(self, act1, act2): """ :param act1: first element :param act2: second element :githublink:`%|py|18` """ MLActionBinary.__init__(self, act1, act2, "+") if type(act1.output) != type(act2.output): raise TypeError("Not the same input type {0} != {1}".format( type(act1.output), type(act2.output)))
[docs] def execute(self, **kwargs): MLActionBinary.execute(self, **kwargs) res = self.ChildrenResults return self.output.validate(res[0] + res[1])
[docs]class MLActionSign(MLActionFunctionCall): """ Sign of an expression: 1=positive, 0=negative. :githublink:`%|py|33` """
[docs] def __init__(self, act1): """ :param act1: first element :githublink:`%|py|38` """ MLActionFunctionCall.__init__(self, "sign", act1.output, act1) if not isinstance(act1.output, MLNumTypeFloat32): raise TypeError( "The input action must produce float32 not '{0}'".format(type(act1.output)))
[docs] def execute(self, **kwargs): MLActionFunctionCall.execute(self, **kwargs) res = self.ChildrenResults return self.output.validate(self.output.softcast(1 if res[0] >= 0 else 0))
[docs]class MLActionTestInf(MLActionBinary): """ Operator ``<``. :githublink:`%|py|53` """
[docs] def __init__(self, act1, act2): """ :param act1: first element :param act2: second element :githublink:`%|py|59` """ MLActionBinary.__init__(self, act1, act2, "<=") if type(act1.output) != type(act2.output): raise TypeError("Not the same input type {0} != {1}".format( type(act1.output), type(act2.output))) self.output = MLNumTypeBool()
[docs] def execute(self, **kwargs): MLActionBinary.execute(self, **kwargs) res = self.ChildrenResults return self.output.validate(self.output.softcast(res[0] <= res[1]))
[docs]class MLActionTestEqual(MLActionBinary): """ Operator ``==``. :githublink:`%|py|75` """
[docs] def __init__(self, act1, act2): """ :param act1: first element :param act2: second element :githublink:`%|py|81` """ MLActionBinary.__init__(self, act1, act2, "==") if type(act1.output) != type(act2.output): raise TypeError("Not the same input type {0} != {1}".format( type(act1.output), type(act2.output))) self.output = MLNumTypeBool()
[docs] def execute(self, **kwargs): MLActionBinary.execute(self, **kwargs) res = self.ChildrenResults return self.output.validate(self.output.softcast(res[0] == res[1]))