Esempio n. 1
0
    def compute(self, env: Environment):
        e1 = env.find_object('e1')
        e2 = env.find_object('e2')

        if isinstance(e2, EmptyParamObj):
            if e1.get_type().name not in ['double', 'logical', 'integer']:
                raise errors.InvalidArgTypeInArgs('x', '- x')

            res = []
            for item in e1.items:
                res.append(
                    atomic_subtract(Atomic(False, types.LogicalType()),
                                    item.value))

            res = [VectorItem(None, el) for el in res]

            ret = VectorObj(res)
            return ret

        if e1.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('x', 'x - y')
        if e2.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('y', 'x - y')

        as_integer = False

        if e1.get_type().name == 'integer' and e2.get_type().name == 'integer':
            as_integer = True
        elif e1.get_type().name == 'integer' and e2.get_type(
        ).name == 'logical':
            as_integer = True
        elif e1.get_type().name == 'logical' and e2.get_type(
        ).name == 'integer':
            as_integer = True

        res = perform_op_on_vector_or_atomic(
            e1, e2,
            lambda l, r: cast_atomic(atomic_subtract(l, r), 'integer'
                                     if as_integer else 'double'))

        res = [VectorItem(None, el) for el in res]

        if len(res) == 0:
            t = get_more_important_vector_type(e1.get_type(), e2.get_type())
            return VectorObj([], t)

        ret = VectorObj.create(res)
        return ret
Esempio n. 2
0
    def compute(self, params: List[Param], env: Environment):
        args = self.arrange_args(params)
        e1: Param = args['e1']
        e2: Param = args['e2']

        if e1.value.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('x', 'x ** y')
        if e2.value.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('y', 'x ** y')
        res = []

        as_integer = False

        if e1.value.get_type().name == 'integer' and e2.value.get_type().name == 'integer':
            as_integer = True
        elif e1.value.get_type().name == 'integer' and e2.value.get_type().name == 'logical':
            as_integer = True
        elif e1.value.get_type().name == 'logical' and e2.value.get_type().name == 'integer':
            as_integer = True

        for l, r in concat_vectors(e1.value.items, e2.value.items):
            res.append(cast_atomic(atomic_mod(l.value, r.value), 'integer' if as_integer else 'double'))

        res = [VectorItem(None, el) for el in res]

        ret = VectorObj(res)
        return ret
Esempio n. 3
0
    def compute(self, env: Environment):
        e1 = env.find_object('e1')
        e2 = env.find_object('e2')

        if e1.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('x', 'x ** y')
        if e2.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('y', 'x ** y')

        as_integer = False

        if e1.get_type().name == 'integer' and e2.get_type().name == 'integer':
            as_integer = True
        elif e1.get_type().name == 'integer' and e2.get_type(
        ).name == 'logical':
            as_integer = True
        elif e1.get_type().name == 'logical' and e2.get_type(
        ).name == 'integer':
            as_integer = True

        res = perform_op_on_vector_or_atomic(
            e1, e2,
            lambda l, r: cast_atomic(atomic_mod(l, r), 'integer'
                                     if as_integer else 'double'))

        res = [VectorItem(None, el) for el in res]

        if len(res) == 0:
            t = get_more_important_vector_type(e1.get_type(), e2.get_type())
            return VectorObj([], t)

        ret = VectorObj.create(res)
        return ret
Esempio n. 4
0
    def compute(self, env: Environment):
        ln: VectorObj = env.find_object('length')
        if ln.get_type().name not in ['double', 'integer']:
            raise errors.InvalidArg('length')

        if len(ln.items) != 1:
            raise errors.InvalidArg('length')

        val = ln.items[0][1]

        if val.is_inf:
            raise errors.R_RuntimeError('vector size cannot be infinite')
        elif val.is_na:
            raise errors.InvalidArg('length')
        elif val.is_nan:
            raise errors.R_RuntimeError('vector size cannot be NA/NaN')

        count = val.value

        items = [
            VectorItem(None, Atomic(False, types.LogicalType()))
            for _ in range(int(count) * (-1 if val.is_neg else 1))
        ]

        ret = VectorObj(items, types.LogicalType())
        return ret
Esempio n. 5
0
 def compute(self, env: Environment):
     e1 = env.find_object('x')
     e2 = env.find_object('y')
     if e1.get_type().name not in ['double', 'logical', 'integer']:
         raise errors.InvalidArgTypeInArgs('x', 'x | y')
     if e2.get_type().name not in ['double', 'logical', 'integer']:
         raise errors.InvalidArgTypeInArgs('y', 'x | y')
     res = VectorItem(None, atomic_or(e1.items[0].value, e2.items[0].value))
     ret = VectorObj.create([res])
     return ret
Esempio n. 6
0
 def compute(self, params: List[Param], env: Environment):
     args = self.arrange_args(params)
     e1: Param = args['x']
     e2: Param = args['y']
     if e1.value.get_type().name not in ['double', 'logical', 'integer']:
         raise errors.InvalidArgTypeInArgs('x', 'x | y')
     if e2.value.get_type().name not in ['double', 'logical', 'integer']:
         raise errors.InvalidArgTypeInArgs('y', 'x | y')
     res = VectorItem(None, atomic_or(e1.value.items[0].value, e2.value.items[0].value))
     ret = VectorObj([res])
     return ret
Esempio n. 7
0
    def compute(self, params: List[Param], env: Environment):
        args = self.arrange_args(params)
        e1: Param = args['x']
        if e1.value.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgType()
        res = []
        for item in e1.value.items:
            res.append(atomic_not(item.value))

        res = [VectorItem(None, el) for el in res]

        ret = VectorObj(res)
        return ret
Esempio n. 8
0
    def compute(self, env: Environment):
        e1 = env.find_object('x')
        e2 = env.find_object('y')
        if e1.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('x', 'x || y')
        if e2.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('y', 'x || y')

        res = perform_op_on_vector_or_atomic(
            e1, e2, lambda l, r: cast_atomic(atomic_power(l, r)))

        res = [VectorItem(None, el) for el in res]

        ret = VectorObj.create(res)
        return ret
Esempio n. 9
0
    def compute(self, params: List[Param], env: Environment):
        args = self.arrange_args(params)
        e1: Param = args['x']
        e2: Param = args['y']
        if e1.value.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('x', 'x || y')
        if e2.value.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('y', 'x || y')
        res = []
        for l, r in concat_vectors(e1.value.items, e2.value.items):
            res.append(atomic_or(l.value, r.value))

        res = [VectorItem(None, el) for el in res]

        ret = VectorObj(res)
        return ret
Esempio n. 10
0
    def compute(self, env: Environment):
        dots: DotsObj = env.find_object('...')

        if dots.get_type().name == 'NULL':
            return NULLObj()

        items = dots.items
        if len(items) == 0:
            return NULLObj()

        items_types = set(map(lambda i: i[1].get_type().name, items))
        casts = ['list', 'character', 'double', 'integer', 'logical']
        if set(casts).issubset(items_types):
            list_fun: FunctionObj = built_in_env.find_function('list')
            ret = list_fun.compute(env)
            return ret

        flat_items = []
        for item in items:
            if isinstance(item[1], Atomic):
                flat_items.append(VectorItem(item[0], item[1]))
            elif isinstance(item[1], VectorObj):
                flat_items.extend(item[1].items)
            else:
                raise Exception('Invalid item in c function - {}'.format(item))

        if 'character' in items_types:
            if len(items_types) > 1:
                flat_items = cast_vector_items(flat_items, str,
                                               types.CharacterType)
        elif 'double' in items_types:
            if len(items_types) > 1:
                flat_items = cast_vector_items(flat_items, float,
                                               types.DoubleType)
        elif 'integer' in items_types:
            if len(items_types) > 1:
                flat_items = cast_vector_items(flat_items, int,
                                               types.IntegerType)
        elif 'logical' in items_types:
            if len(items_types) > 1:
                flat_items = cast_vector_items(flat_items, bool,
                                               types.LogicalType)
        else:
            raise Exception(
                "unknown vector type in types - {}".format(items_types))

        return VectorObj.create(flat_items)
Esempio n. 11
0
    def compute(self, env: Environment):
        e1 = env.find_object('x')
        e2 = env.find_object('y')
        if e1.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('x', 'x && y')
        if e2.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('y', 'x && y')
        try:
            res = perform_op_on_vector_or_atomic(e1, e2, atomic_and)
        except UnsupportedVectorType as e:
            raise errors.InvalidArgTypeInArgs(
                'x' if e.operand_index == 0 else 'y', 'x && y')

        res = [VectorItem(None, el) for el in res]

        ret = VectorObj.create(res)
        return ret
Esempio n. 12
0
    def compute(self, env: Environment):
        e1 = env.find_object('x')
        if e1.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgType()
        res = []
        if isinstance(e1, VectorObj):
            if len(e1.items) == 0:
                return VectorObj([], type(e1.get_type())())

            for item in e1.items:
                res.append(atomic_not(item[1]))
        else:
            res.append(atomic_not(e1))

        res = [VectorItem(None, el) for el in res]

        ret = VectorObj.create(res)
        return ret
Esempio n. 13
0
    def compute(self, params: List[Param], env: Environment):
        args = self.arrange_args(params)
        e1: Param = args['x']
        e2: Param = args['y']
        if e1.value.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('x', 'x && y')
        if e2.value.get_type().name not in ['double', 'logical', 'integer']:
            raise errors.InvalidArgTypeInArgs('y', 'x && y')
        res = []

        try:
            for l, r in concat_vectors(e1.value.items, e2.value.items):
                res.append(atomic_and(l.value, r.value))
        except UnsupportedVectorType as e:
            raise errors.InvalidArgTypeInArgs('x' if e.operand_index == 0 else 'y', 'x && y')

        res = [VectorItem(None, el) for el in res]

        ret = VectorObj(res)
        return ret
Esempio n. 14
0
    def compute(self, params: List[Param], env: Environment):
        args = self.arrange_args(params)
        dots: DotsObj = args['...']
        items = dots.items
        if len(items) == 0:
            return RObj.get_r_obj('NULLObj').create()

        items_types = set(map(lambda i: i.value.get_type().name, items))
        casts = ['list', 'character', 'double', 'integer', 'logical']
        if set(casts).issubset(items_types):
            list_fun: FunctionObj = built_in_env.find_function('list')
            ret = list_fun.compute(params, env)
            return ret

        flat_items = []
        for item in items:
            if isinstance(item.value, Atomic):
                flat_items.append(VectorItem(item.name, item.value))
            elif isinstance(item.value, VectorObj):
                flat_items.extend(item.value.items)
            else:
                raise Exception('Invalid item in c function - {}'.format(item))

        if 'character' in items_types:
            if len(items_types) > 1:
                flat_items = cast_vector_items(flat_items, str, types.CharacterType)
        elif 'double' in items_types:
            if len(items_types) > 1:
                flat_items = cast_vector_items(flat_items, float, types.DoubleType)
        elif 'integer' in items_types:
            if len(items_types) > 1:
                flat_items = cast_vector_items(flat_items, int, types.IntegerType)
        elif 'logical' in items_types:
            if len(items_types) > 1:
                flat_items = cast_vector_items(flat_items, bool, types.LogicalType)
        else:
            raise Exception("unknown vector type in types - {}".format(items_types))

        return VectorObj.create(flat_items)
Esempio n. 15
0
def cast_vector_items(items, python_type, r_type):
    ret = [VectorItem(item.name, Atomic(python_type(item.value.value), r_type())) for item in items]
    return ret