コード例 #1
0
    def __getitem__(localization, proxy_obj, arguments):
        r = TypeWrapper.get_wrapper_of(proxy_obj.__self__)
        index_selector = arguments[0]
        if isinstance(index_selector, tuple):
            index_selector = index_selector[0]

        dims = call_utilities.get_dimensions(localization, r)
        if dims > 1:
            if call_utilities.is_iterable(arguments[0]):
                if call_utilities.is_iterable(arguments[0].get_contained_type()):
                    return call_utilities.create_numpy_array_n_dimensions(
                        call_utilities.cast_to_numpy_type(call_utilities.get_inner_type(localization, r)), dims - 1)

            contained = call_utilities.cast_to_numpy_type(call_utilities.get_inner_type(localization, r))
            for i in range(dims - 1):
                contained = UnionType.add(contained,
                                          call_utilities.create_numpy_array_n_dimensions(r.get_contained_type(), i + 1))
        else:
            contained = r.get_contained_type()

        if isinstance(index_selector, TypeWrapper):
            if isinstance(index_selector.wrapped_type, slice) or (
                        call_utilities.is_iterable(index_selector) and not isinstance(index_selector.wrapped_type,
                                                                                      tuple)):
                l = call_utilities.create_numpy_array(contained)
                return l
        return contained  # proxy_obj.__self__.dtype.type()
コード例 #2
0
    def dot(localization, proxy_obj, arguments):
        dvar = call_utilities.parse_varargs_and_kwargs(
            localization, arguments, ['out'], {
                'out': numpy.ndarray,
            }, 'dot')

        if isinstance(dvar, StypyTypeError):
            return dvar

        if Number == type(arguments[0]) and Number == type(arguments[1]):
            return call_utilities.cast_to_greater_numpy_type(
                arguments[0], arguments[1])

        if Number == type(arguments[0]) and call_utilities.is_iterable(
                arguments[1]):
            c_t = get_contained_elements_type(localization, arguments[1])
            if not 'out' in dvar.keys():
                return call_utilities.create_numpy_array(c_t)
            else:
                set_contained_elements_type(localization, dvar['out'], c_t)
                return dvar['out']

        if Number == type(arguments[1]) and call_utilities.is_iterable(
                arguments[0]):
            c_t = get_contained_elements_type(localization, arguments[0])
            if not 'out' in dvar.keys():
                return call_utilities.create_numpy_array(c_t)
            else:
                set_contained_elements_type(localization, dvar['out'], c_t)
                return dvar['out']

        if call_utilities.is_iterable(
                arguments[0]) and call_utilities.is_iterable(arguments[1]):
            if call_utilities.get_dimensions(
                    localization,
                    arguments[0]) == 1 and call_utilities.get_dimensions(
                        localization, arguments[1]) == 1:
                return call_utilities.cast_to_greater_numpy_type(
                    call_utilities.get_inner_type(localization, arguments[0]),
                    call_utilities.get_inner_type(localization, arguments[1]))

            typ = call_utilities.cast_to_greater_numpy_type(
                call_utilities.get_inner_type(localization, arguments[0]),
                call_utilities.get_inner_type(localization, arguments[1]))
            for i in range(
                    call_utilities.get_dimensions(localization, arguments[0])):
                typ = call_utilities.create_numpy_array(typ)

            if not 'out' in dvar.keys():
                return typ
            else:
                set_contained_elements_type(
                    localization, dvar['out'],
                    get_contained_elements_type(localization, typ))
                return dvar['out']

        return arguments[0]
コード例 #3
0
    def iadd(localization, proxy_obj, arguments):
        if call_utilities.is_iterable(
                arguments[0]) and call_utilities.is_iterable(arguments[1]):
            if isinstance(
                    arguments[0].get_wrapped_type(), list) and isinstance(
                        arguments[1].get_wrapped_type(), tuple):
                t1 = get_contained_elements_type(localization, arguments[0])
                t2 = get_contained_elements_type(localization, arguments[1])

                tEnd = UnionType.add(t1, t2)

                set_contained_elements_type(localization, arguments[0], tEnd)
                return arguments[0]

        return None
コード例 #4
0
    def divide(localization, proxy_obj, arguments, func_name='divide'):
        dvar = call_utilities.parse_varargs_and_kwargs(
            localization, arguments, ['out', 'where'], {
                'out': numpy.ndarray,
                'where': numpy.ndarray,
            }, func_name, 2)

        if isinstance(dvar, StypyTypeError):
            return dvar

        if Number == type(arguments[0]) and Number == type(arguments[1]):
            return call_utilities.cast_to_greater_numpy_type(
                arguments[0], arguments[1])

        try:
            dims = 0
            if call_utilities.is_iterable(arguments[0]):
                param0 = call_utilities.get_inner_type(localization,
                                                       arguments[0])
                dims = call_utilities.get_dimensions(localization,
                                                     arguments[0])
            else:
                param0 = arguments[0]

            if call_utilities.is_iterable(arguments[1]):
                param1 = call_utilities.get_inner_type(localization,
                                                       arguments[1])
                temp = call_utilities.get_dimensions(localization,
                                                     arguments[1])
                if temp > dims:
                    dims = temp
            else:
                param1 = arguments[1]
            if dims > 0:
                ret = call_utilities.create_numpy_array_n_dimensions(
                    call_utilities.cast_to_greater_numpy_type(param0, param1),
                    dims)
            else:
                ret = call_utilities.cast_to_greater_numpy_type(param0, param1)

        except Exception as ex:
            return StypyTypeError(localization, str(ex))

        if 'out' in dvar.keys():
            set_contained_elements_type(localization, dvar['out'], ret)
            return dvar['out']

        return ret
コード例 #5
0
def is_suitable_for_loop_condition(localization, condition_type):
    """
    A loop must iterate an iterable object or data structure or an string. This function checks this
    :param localization: Caller information
    :param condition_type: Type of the condition
    :return:
    """
    if is_error_type(condition_type):
        return False

    if type(condition_type) is RecursionType:
        return False

    if type(condition_type) is file:
        return True

    if condition_type is undefined_type.UndefinedType:
        return False

    if not (can_store_elements(condition_type) or can_represent_type(Str, condition_type) or (
            can_represent_type(IterableObject, condition_type)) or call_utilities.is_iterable(condition_type)):
        StypyTypeError(localization, "The type of this for loop condition is erroneous")
        return False

    return True
コード例 #6
0
    def random(localization, proxy_obj, arguments):
        # if len(arguments) == 0:
        #     return float()
        #
        # dvar = call_utilities.parse_varargs_and_kwargs(localization, arguments, ['size'], {
        #     'size': [Integer, IterableDataStructureWithTypedElements(Integer)],
        # }, 'random', 0)
        #
        # if isinstance(dvar, StypyTypeError):
        #     return dvar
        #
        # if call_utilities.is_iterable(arguments[0]):
        #     inner_array = call_utilities.create_numpy_array(numpy.float64())
        #     return call_utilities.create_numpy_array(inner_array)
        #
        # return float()
        if len(arguments) == 0:
            return float()

        dvar = call_utilities.parse_varargs_and_kwargs(localization, arguments, ['size'], {
            'size': [Integer, IterableDataStructureWithTypedElements(Integer)],
        }, 'random', 0)

        if isinstance(dvar, StypyTypeError):
            return dvar

        if call_utilities.is_iterable(arguments[0]):
            inner_array = call_utilities.create_numpy_array_n_dimensions(numpy.float64(), call_utilities.get_dimensions(localization, arguments[0]))
            return call_utilities.create_numpy_array(inner_array)

        return call_utilities.create_numpy_array(numpy.float64())
コード例 #7
0
    def __div__(localization, proxy_obj, arguments, func_name='__div__'):
        # dvar = call_utilities.parse_varargs_and_kwargs(localization, arguments, ['out', 'where'], {
        #     'out': numpy.ndarray,
        #     'where': numpy.ndarray,
        # }, func_name, 2)
        #
        # if isinstance(dvar, StypyTypeError):
        #     return dvar
        try:
            dims = 0
            if call_utilities.is_iterable(arguments[0]):
                param0 = call_utilities.get_inner_type(localization, arguments[0])
                dims = call_utilities.get_dimensions(localization, arguments[0])
            else:
                param0 = arguments[0]

            if dims > 0:
                ret = call_utilities.create_numpy_array_n_dimensions(
                    call_utilities.cast_to_numpy_type(param0),
                    dims)
            else:
                ret = call_utilities.create_numpy_array(call_utilities.cast_to_numpy_type(param0))

        except Exception as ex:
            return StypyTypeError(localization, str(ex))

        # if 'out' in dvar.keys():
        #     set_contained_elements_type(localization, dvar['out'],
        #                                 ret)
        #     return dvar['out']

        return ret
コード例 #8
0
def will_iterate_loop(localization, condition_type):
    """
    A loop must iterate an iterable object or data structure or an string. This function checks if the iterable object
    is empty (its contents are of the type UndefinedType). In that case, it does not iterate
    :param localization: Caller information
    :param condition_type: Type of the condition
    :return:
    """
    if is_error_type(condition_type):
        return False

    if type(condition_type) is file:
        return True

    if condition_type is undefined_type.UndefinedType:
        return False

    if (can_store_elements(condition_type) or (
            can_represent_type(IterableObject, condition_type)) or call_utilities.is_iterable(condition_type)):
        try:
            Localization.set_current(localization)
            t = get_contained_elements_type(condition_type)
            if type(t) is undefined_type.UndefinedType or t is undefined_type.UndefinedType:
                return False
        except:
            pass

        return True

    return True
コード例 #9
0
    def zeros(localization, proxy_obj, arguments):
        dvar = call_utilities.parse_varargs_and_kwargs(
            localization, arguments, ['dtype', 'order'], {
                'dtype': [type, IterableDataStructure],
                'order': Str,
            }, 'zeros')

        if isinstance(dvar, StypyTypeError):
            return dvar

        if 'dtype' in dvar.keys():
            dtype = dvar['dtype']
        else:
            dtype = None

        t = call_utilities.check_possible_values(dvar, 'order', ['C', 'F'])
        if isinstance(t, StypyTypeError):
            return t

        if Number == type(arguments[0]):
            return call_utilities.create_numpy_array(numpy.float64(),
                                                     dtype=dtype)
        else:
            if call_utilities.is_iterable(dtype):
                tup = call_utilities.wrap_contained_type(tuple())
                tup.set_contained_type(numpy.float64())
                contents = call_utilities.create_numpy_array(tup)
                return call_utilities.create_numpy_array(contents)

            dims = call_utilities.get_dimensions(localization, arguments[0])
            typ = call_utilities.create_numpy_array_n_dimensions(
                numpy.float64(), dims, dtype=dtype)

            return typ
コード例 #10
0
    def add(localization, proxy_obj, arguments):
        if call_utilities.is_iterable(
                arguments[0]) and call_utilities.is_iterable(arguments[1]):
            if isinstance(arguments[0].get_wrapped_type(),
                          tuple) and isinstance(
                              arguments[1].get_wrapped_type(), tuple):
                t1 = call_utilities.get_contained_elements_type(
                    localization, arguments[0])
                t2 = call_utilities.get_contained_elements_type(
                    localization, arguments[1])
                if isinstance(t1, UnionType):
                    t1 = t1.duplicate()
                tEnd = UnionType.add(t1, t2)
                wrap = call_utilities.wrap_contained_type((tEnd, ))
                wrap.set_contained_type(tEnd)
                return wrap

        return None  # Type rule results
コード例 #11
0
    def tile(localization, proxy_obj, arguments):
        dims = 1
        if len(arguments) == 2 and call_utilities.is_iterable(arguments[1]):
            dims = call_utilities.get_dimensions(localization, arguments[1])

        if Number == type(arguments[0]):
            return call_utilities.create_numpy_array_n_dimensions(arguments[0], dims)

        return call_utilities.create_numpy_array_n_dimensions(get_contained_elements_type(localization, arguments[0]), dims)
コード例 #12
0
    def block(localization, proxy_obj, arguments):
        union = None
        for arg in arguments:
            if call_utilities.is_iterable(arg):
                union = UnionType.add(
                    union, get_contained_elements_type(localization, arg))
            else:
                union = UnionType.add(union, arg)

        return call_utilities.create_numpy_array(union)
コード例 #13
0
    def einsum(localization, proxy_obj, arguments):
        if isinstance(arguments[-1], dict):
            if Str == type(arguments[0]):
                arg_num = 2
            else:
                arg_num = 1

            dvar = call_utilities.parse_varargs_and_kwargs(localization, arguments,
                                                           ['out', 'dtype', 'order', 'casting', 'optimize'], {
                                                               'out': IterableDataStructure,
                                                               'dtype': type,
                                                               'order': Str,
                                                               'casting': Str,
                                                               'optimize': [bool, Str]
                                                           }, 'einsum', arg_num)

            if isinstance(dvar, StypyTypeError):
                return dvar

            val_temp = call_utilities.check_possible_values(dvar, 'order', ['C', 'F', 'A', 'K'])
            if isinstance(val_temp, StypyTypeError):
                return val_temp

            val_temp = call_utilities.check_possible_values(dvar, 'casting', ['no', 'equiv', 'safe', 'same_kind', 'unsafe'])
            if isinstance(val_temp, StypyTypeError):
                return val_temp

            val_temp = call_utilities.check_possible_values(dvar, 'optimize', ['greedy', 'optimal', False, True])
            if isinstance(val_temp, StypyTypeError):
                return val_temp

            arguments = arguments[:-1]
        else:
            dvar = dict()

        typ = None
        if Str == type(arguments[0]):
            arg_list = arguments[1:]
            if Number == type(arguments[1]) and 'out' in dvar:
                return dvar['out']
        else:
            arg_list = arguments

        for arg in arg_list:
            if call_utilities.is_iterable(arg):
                typ_temp = call_utilities.cast_to_numpy_type(call_utilities.get_inner_type(localization, arg))
                typ = call_utilities.cast_to_greater_numpy_type(typ, typ_temp)

        union = UnionType.add(typ, call_utilities.create_numpy_array(DynamicType))

        if 'out' in dvar:
            set_contained_elements_type(localization, dvar['out'], DynamicType)
            return call_utilities.create_numpy_array(DynamicType)

        return union
コード例 #14
0
    def angle(localization, proxy_obj, arguments):
        if not call_utilities.is_iterable(arguments):
            return numpy.float64()

        r = TypeWrapper.get_wrapper_of(proxy_obj.__self__)
        dims = call_utilities.get_dimensions(localization, r)
        if dims > 1:
            return call_utilities.create_numpy_array_n_dimensions(
                numpy.float64(), dims - 1)
        else:
            return call_utilities.create_numpy_array(numpy.float64())
コード例 #15
0
    def dstack(localization, proxy_obj, arguments):
        elem_list = wrap_contained_type(list())
        for arg in arguments:
            if call_utilities.is_iterable(arg):
                cont = get_contained_elements_type(localization, arg)
                if call_utilities.is_iterable(cont):
                    union2 = UnionType.add(elem_list.get_contained_type(),
                                           cont.get_contained_type())
                    elem_list.set_contained_type(union2)
                else:
                    union2 = UnionType.add(elem_list.get_contained_type(),
                                           cont)
                    elem_list.set_contained_type(union2)
            else:
                return StypyTypeError(
                    localization,
                    "A non-iterable parameter {0} was passed to the dstack function"
                    .format(str(arg)))

        return call_utilities.create_numpy_array(elem_list)
コード例 #16
0
    def concatenate(localization, proxy_obj, arguments):
        union = None
        for arg in arguments:
            if call_utilities.is_iterable(arg):
                union = UnionType.add(
                    union, get_contained_elements_type(localization, arg))
            else:
                return StypyTypeError(
                    localization,
                    "A non-iterable parameter {0} was passed to the concatenate function"
                    .format(str(arg)))

        return call_utilities.create_numpy_array(union)
コード例 #17
0
    def hstack(localization, proxy_obj, arguments):
        union = None
        for arg in arguments:
            if call_utilities.is_iterable(arg):
                union = UnionType.add(
                    union, call_utilities.get_inner_type(localization, arg))
            else:
                return StypyTypeError(
                    localization,
                    "A non-iterable parameter {0} was passed to the hstack function"
                    .format(str(arg)))

        return call_utilities.create_numpy_array(union)
コード例 #18
0
    def ascontiguousarray(localization, proxy_obj, arguments):
        dvar = call_utilities.parse_varargs_and_kwargs(localization, arguments, ['dtype'],{
            'dtype': type,
        }, 'ascontiguousarray')

        if 'dtype' in dvar.keys():
            dtype = dvar['dtype']
        else:
            dtype = None

        if call_utilities.is_iterable(arguments[0]):
            typ = get_contained_elements_type(localization, arguments[0])
        else:
            typ = arguments[0]

        return call_utilities.create_numpy_array(typ, dtype=dtype)
コード例 #19
0
    def atleast_2d(localization, proxy_obj, arguments):
        rets = list()
        for arg in arguments:
            if call_utilities.is_iterable(arg):
                if call_utilities.get_dimensions(localization, arg) >= 2:
                    rets.append(arg)
                else:
                    rets.append(
                        call_utilities.create_numpy_array_n_dimensions(
                            call_utilities.get_inner_type(localization, arg),
                            2))
            else:
                return StypyTypeError(
                    localization,
                    "A non-iterable parameter {0} was passed to the atleast_2d function"
                    .format(str(arg)))

        if len(rets) == 1:
            return rets[0]

        return tuple(rets)
コード例 #20
0
    def reshape(localization, proxy_obj, arguments):
        shape_levels = len(arguments)
        if Str == type(arguments[-1]):
            shape_levels -= 1

        if shape_levels == 0:
            return StypyTypeError(localization,
                                  "Invalid 'shape' parameter for reshape call: There must be at least one shape element")
        if call_utilities.is_iterable(arguments[0]):
            shape_levels = 2

        if len(arguments) > 1 and not Str == type(arguments[-1]) and not Integer == type(arguments[-1]):
            return StypyTypeError(localization,
                                  "Invalid 'order' parameter for reshape call: {0}".format(str(arguments[-1])))

        r = TypeWrapper.get_wrapper_of(proxy_obj.__self__)
        contained = r.get_contained_type()

        for i in range(shape_levels):
            contained = call_utilities.create_numpy_array(contained)
        return contained
コード例 #21
0
    def stack(localization, proxy_obj, arguments):
        dvar = call_utilities.parse_varargs_and_kwargs(localization, arguments,
                                                       ['axis'], {
                                                           'axis': Integer,
                                                       }, 'stack')

        if isinstance(dvar, StypyTypeError):
            return dvar

        union = None
        for arg in arguments:
            if call_utilities.is_iterable(arg):
                union = UnionType.add(
                    union, get_contained_elements_type(localization, arg))
            else:
                return StypyTypeError(
                    localization,
                    "A non-iterable parameter {0} was passed to the stack function"
                    .format(str(arg)))

        return call_utilities.create_numpy_array(union)
コード例 #22
0
    def randint(localization, proxy_obj, arguments):
        if len(arguments) == 1:
            return int()

        dvar = call_utilities.parse_varargs_and_kwargs(localization, arguments, ['high', 'size', 'dtype'], {
            'high': Integer,
            'size': [Integer, IterableDataStructureWithTypedElements(Integer)],
            'dtype': type,
        }, 'randint', 1)

        if isinstance(dvar, StypyTypeError):
            return dvar

        if 'dtype' in dvar:
            if not Integer == dvar['dtype']:
                return StypyTypeError(localization, "Unsupported type {0} for randint".format(str(dvar['dtype'])))
        if 'size' in dvar:
            if call_utilities.is_iterable(dvar['size']):
                return call_utilities.create_numpy_array_n_dimensions(numpy.int32(), 2)
            return call_utilities.create_numpy_array(numpy.int32())

        return int()
コード例 #23
0
    def bitwise_and(localization,
                    proxy_obj,
                    arguments,
                    func_name='bitwise_and'):
        dvar = call_utilities.parse_varargs_and_kwargs(
            localization, arguments, ['where'], {
                'where': [Str,
                          IterableDataStructureWithTypedElements(bool)],
            }, func_name, 2)

        if isinstance(dvar, StypyTypeError):
            return dvar

        if call_utilities.is_iterable(
                arguments[0]) and call_utilities.is_iterable(arguments[1]):
            if not call_utilities.check_possible_types(
                    call_utilities.get_inner_type(localization, arguments[0]),
                [bool, Integer]) or not call_utilities.check_possible_types(
                    call_utilities.get_inner_type(localization, arguments[1]),
                    [bool, Integer]):
                return StypyTypeError(
                    localization, " ufunc '" + func_name +
                    "' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''"
                )

            if call_utilities.is_numpy_array(arguments[0]):
                return call_utilities.create_numpy_array(
                    call_utilities.get_inner_type(localization, arguments[0]))
            if call_utilities.is_numpy_array(arguments[1]):
                return call_utilities.create_numpy_array(
                    call_utilities.get_inner_type(localization, arguments[1]))
            return arguments[0]
        else:
            if call_utilities.is_iterable(
                    arguments[0]) and not call_utilities.is_iterable(
                        arguments[1]):
                if not call_utilities.check_possible_types(
                        call_utilities.get_inner_type(localization,
                                                      arguments[0]),
                    [bool, Integer
                     ]) or not call_utilities.check_possible_types(
                         arguments[1], [bool, Integer]):
                    return StypyTypeError(
                        localization, " ufunc '" + func_name +
                        "' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''"
                    )

                if call_utilities.is_numpy_array(arguments[0]):
                    return call_utilities.create_numpy_array(
                        call_utilities.get_inner_type(localization,
                                                      arguments[0]))
                return arguments[0]
            else:
                if not call_utilities.is_iterable(
                        arguments[0]) and call_utilities.is_iterable(
                            arguments[1]):
                    if not call_utilities.check_possible_types(
                            call_utilities.get_inner_type(
                                localization, arguments[1]),
                        [bool, Integer
                         ]) or not call_utilities.check_possible_types(
                             arguments[0], [bool, Integer]):
                        return StypyTypeError(
                            localization, " ufunc '" + func_name +
                            "' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''"
                        )

                    if call_utilities.is_numpy_array(arguments[1]):
                        return call_utilities.create_numpy_array(
                            call_utilities.get_inner_type(
                                localization, arguments[1]))
                    return arguments[1]
                else:
                    return arguments[0]