예제 #1
0
def atomic_subtract(e1: Atomic, e2: Atomic):
    type = get_more_important_vector_type(e1.get_type(), e2.get_type())

    if type.name == 'character':
        raise errors.NonNumericToBinary()
    new_e2 = Atomic(e2.value, e2.type, is_na=e2.is_na, is_nan=e2.is_nan, is_inf=e2.is_inf, is_neg=not e2.is_neg)

    ret = atomic_add(e1, new_e2)
    return ret
예제 #2
0
def atomic_not(e: Atomic):
    if e.type.name == 'character':
        raise errors.InvalidArgType()

    if e.is_inf:
        ret = Atomic(False, types.LogicalType())
        return ret
    elif e.is_na or e.is_nan:
        return Atomic(None, e.type, is_na=True)
    ret = Atomic(not bool(e.value), types.LogicalType())
    return ret
예제 #3
0
def atomic_divide(e1: Atomic, e2: Atomic):
    type = get_more_important_vector_type(e1.get_type(), e2.get_type())

    if type.name == 'character':
        raise errors.NonNumericToBinary()

    if e2.is_inf:
        new_e2 = Atomic(False, types.LogicalType(), is_inf=False, is_neg=e2.is_neg)
    else:
        if e2.value == 0 and not e2.is_na and not e2.is_nan:
            new_e2 = Atomic(None, e2.type, is_na=False, is_nan=False, is_inf=True, is_neg=e2.is_neg)
        elif not e2.is_na and not e2.is_nan:
            new_e2 = Atomic(1 / e2.value, e2.type, is_na=e2.is_na, is_nan=e2.is_nan, is_inf=e2.is_inf, is_neg=e2.is_neg)
        else:
            new_e2 = e2
    ret = atomic_multiply(e1, new_e2)
    return ret
예제 #4
0
def atomic_multiply(e1: Atomic, e2: Atomic):
    type = get_more_important_vector_type(e1.get_type(), e2.get_type())

    if type.name == 'character':
        raise errors.NonNumericToBinary()

    is_nan = e1.is_nan or e2.is_nan
    is_na = e1.is_na or e2.is_na or ((e1.is_inf or e2.is_inf) and (e1.value == 0 or e2.value == 0))
    is_inf = (e1.is_inf or e2.is_inf)
    is_neg = e1.is_neg != e2.is_neg

    if is_nan:
        return Atomic(None, type, is_nan=True)
    elif is_na:
        return Atomic(None, type, is_na=True)
    elif is_inf:
        return Atomic(None, type, is_inf=True, is_neg=is_neg)

    if type.name == 'double':
        val = float(e1.value) * float(e2.value) * (-1 if e1.is_neg != e2.is_neg else 1)
    elif type.name == 'integer':
        val = int(int(e1.value) * int(e2.value) * (-1 if e1.is_neg != e2.is_neg else 1))
    elif type.name == 'logical':
        val = int(e1.value) * int(e2.value) * (-1 if e1.is_neg != e2.is_neg else 1)
        type = types.IntegerType()
    else:
        raise Exception('invalid vector type - {}'.format(type.name))

    if val < 0:
        return Atomic(-val, type, is_neg=True)
    else:
        return Atomic(val, type)
예제 #5
0
def cast_atomic(e: Atomic, type_name):
    if e.get_type().name == type_name:
        return e
    elif type_name == 'character':
        ret = Atomic(bool(e.value), types.CharacterType(), is_na=e.is_na, is_nan=e.is_nan, is_inf=e.is_inf,
                     is_neg=e.is_neg)
        return ret
    elif type_name == 'double':
        ret = Atomic(float(e.value), types.DoubleType(), is_na=e.is_na, is_nan=e.is_nan, is_inf=e.is_inf,
                     is_neg=e.is_neg)
        return ret
    elif type_name == 'integer':
        ret = Atomic(int(e.value), types.IntegerType(), is_na=e.is_na, is_nan=e.is_nan, is_inf=e.is_inf,
                     is_neg=e.is_neg)
        return ret
    elif type_name == 'logical':
        ret = Atomic(bool(e.value), types.LogicalType(), is_na=e.is_na, is_nan=e.is_nan, is_inf=e.is_inf,
                     is_neg=e.is_neg)
        return ret
    else:
        raise Exception('unsupported vector cast type - {}'.format(type_name))
예제 #6
0
def atomic_mod(e1: Atomic, e2: Atomic):
    type = get_more_important_vector_type(e1.get_type(), e2.get_type())

    if type.name == 'character':
        raise errors.NonNumericToBinary()

    is_na = e1.is_na and not (e2.value == 0)
    is_nan = (e1.is_nan and e2.is_na) or (e1.is_nan and (e2.value == 0)) or ((e1.value == 0) and e2.is_nan)
    is_inf = (e1.is_inf and not e2.is_inf and e2.is_neg) or (e1.is_inf and e2.value != 0)

    if is_na:
        return Atomic(None, type, is_na=True)
    elif is_nan:
        return Atomic(None, type, is_nan=True)
    elif is_inf:
        return Atomic(None, type, is_inf=True)
    elif e1.value == 1 and e1.is_neg and e2.value == 0:
        return Atomic(1, type)
    elif e1.is_neg and e2.value == int(e2.value):
        ret = Atomic(e1.value ** e2.value, type)
        return ret

    if type.name == 'double':
        val = (float(e1.value) * (-1 if e1.is_neg else 1)) ** (float(e2.value) * (-1 if e1.is_neg else 1))
    elif type.name == 'integer':
        val = int((int(e1.value) * (-1 if e1.is_neg else 1)) ** (int(e2.value) * (-1 if e1.is_neg else 1)))
    elif type.name == 'logical':
        val = (int(e1.value) * (-1 if e1.is_neg else 1)) ** (int(e2.value) * (-1 if e1.is_neg else 1))
        type = types.IntegerType()
    else:
        raise Exception('invalid vector type - {}'.format(type.name))

    if isinstance(val, complex):
        return Atomic(None, type, is_nan=True)

    if val < 0:
        return Atomic(-val, type, is_neg=True)
    else:
        return Atomic(val, type)
예제 #7
0
def atomic_is_equal(e1: Atomic, e2: Atomic):
    type = get_more_important_vector_type(e1.get_type(), e2.get_type())
    if e1.is_na or e2.is_na:
        return Atomic(None, types.LogicalType(), is_na=True)
    if e1.is_nan or e2.is_nan:
        return Atomic(None, types.LogicalType(), is_na=True)
    if e1.is_inf and e2.is_inf:
        return Atomic(e1.value == e2.value, types.LogicalType())

    if type.name == 'character':
        return Atomic(str(e1.value) == str(e2.value), types.LogicalType())
    elif type.name == 'double':
        return Atomic(float(e1.value) == float(e2.value), types.LogicalType())
    elif type.name == 'integer':
        return Atomic(int(e1.value) == int(e2.value), types.LogicalType())
    elif type.name == 'logical':
        return Atomic(bool(e1.value) == bool(e2.value), types.LogicalType())
    else:
        raise Exception('invalid vector type - {}'.format(type.name))
예제 #8
0
def atomic_is_greater(e1: Atomic, e2: Atomic):
    type = get_more_important_vector_type(e1.get_type(), e2.get_type())
    if e1.is_na or e2.is_na:
        return Atomic(None, types.LogicalType(), is_na=True)
    if e1.is_nan or e2.is_nan:
        return Atomic(None, types.LogicalType(), is_na=True)
    if e1.is_inf and e2.is_inf:
        return Atomic(e1.is_neg < e2.is_neg, types.LogicalType())
    if e1.is_inf != e2.is_inf:
        return Atomic(e1.is_inf and not e1.is_neg or e2.is_inf and e2.is_neg, types.LogicalType())

    if type.name == 'character':
        return Atomic(str(e1.value) > str(e2.value), types.LogicalType())
    elif type.name == 'double':
        return Atomic(float(e1.value) * (-1 if e1.is_neg else 1) > float(e2.value) * (-1 if e2.is_neg else 1),
                      types.LogicalType())
    elif type.name == 'integer':
        return Atomic(int(e1.value) * (-1 if e1.is_neg else 1) > int(e2.value) * (-1 if e2.is_neg else 1),
                      types.LogicalType())
    elif type.name == 'logical':
        return Atomic(bool(e1.value) > bool(e2.value), types.LogicalType())
    else:
        raise Exception('invalid vector type - {}'.format(type.name))
예제 #9
0
    def compute(self, params: List[Param], env: Environment):
        args = self.arrange_args(params)
        e1: Param = args['e1']
        e2: Param = args['e2']

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

            res = []
            for item in e1.value.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.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_subtract(l.value, r.value), 'integer' if as_integer else 'double'))

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

        ret = VectorObj(res)
        return ret
예제 #10
0
def atomic_or(e1: Atomic, e2: Atomic):
    type = get_more_important_vector_type(e1.get_type(), e2.get_type())

    if e1.is_na or e2.is_na:
        return Atomic(None, types.LogicalType(), is_na=True)
    if e1.is_nan or e2.is_nan:
        return Atomic(None, types.LogicalType(), is_na=True)
    if e1.is_inf or e2.is_inf:
        return Atomic(True, types.LogicalType())
    if (e1.is_inf and e2.value == 0) or (e1.value == 0 and e2.is_inf):
        return Atomic(False, types.LogicalType())

    if type.name == 'character':
        val = str(e1.value) or str(e2.value)
    elif type.name == 'double':
        val = float(e1.value) * (-1 if e1.is_neg else 1) or float(e2.value) * (-1 if e2.is_neg else 1)
    elif type.name == 'integer':
        val = int(e1.value) * (-1 if e1.is_neg else 1) or int(e2.value) * (-1 if e2.is_neg else 1)
    elif type.name == 'logical':
        val = bool(e1.value) or bool(e2.value)
    else:
        raise Exception('invalid vector type - {}'.format(type.name))

    return Atomic(bool(val), types.LogicalType())
예제 #11
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