コード例 #1
0
ファイル: types.py プロジェクト: tpn/numba
def from_numpy_dtype(np_dtype):
    """
    :param np_dtype: the NumPy dtype (e.g. np.dtype(np.double))
    :return: a dtype type representation
    """
    from numba.typesystem import numpy_support
    return numpy_dtype(numpy_support.map_dtype(np_dtype))
コード例 #2
0
ファイル: constants.py プロジェクト: FrancescAlted/numba
def get_default_match_table(u):
    """
    Get a matcher table: { (type -> bool) : (value -> type) }
    """
    table = {
        is_NULL:
            lambda value: numba.typesystem.null,
        is_dtype_constructor:
            lambda value: numba.typesystem.from_numpy_dtype(np.dtype(value)),
        is_numpy_scalar:
            lambda value: numpy_support.map_dtype(value.dtype),
        is_ctypes:
            lambda value: from_ctypes(value, u),
        cffi_support.is_cffi_func:
            lambda value: from_cffi(value, u),
        is_numba_exttype:
            lambda value: getattr(type(value), '__numba_ext_type'),
        numbawrapper.is_numba_wrapper:
            lambda value: u.jit_function(value),
        is_autojit_func:
            lambda value: u.autojit_function(value),
        is_registered:
            lambda value: from_typefunc(value, u),
    }

    return table
コード例 #3
0
ファイル: types.py プロジェクト: ASPP/numba
def from_numpy_dtype(np_dtype):
    """
    :param np_dtype: the NumPy dtype (e.g. np.dtype(np.double))
    :return: a dtype type representation
    """
    from numba.typesystem import numpy_support
    return numpy_dtype(numpy_support.map_dtype(np_dtype))
コード例 #4
0
def get_default_match_table(u):
    """
    Get a matcher table: { (type -> bool) : (value -> type) }
    """
    table = {
        is_NULL:
        lambda value: numba.typesystem.null,
        is_dtype_constructor:
        lambda value: numba.typesystem.from_numpy_dtype(np.dtype(value)),
        is_numpy_scalar:
        lambda value: numpy_support.map_dtype(value.dtype),
        is_ctypes:
        lambda value: from_ctypes(value, u),
        cffi_support.is_cffi_func:
        lambda value: from_cffi(value, u),
        is_numba_exttype:
        lambda value: getattr(type(value), '__numba_ext_type'),
        numbawrapper.is_numba_wrapper:
        lambda value: u.jit_function(value),
        is_autojit_func:
        lambda value: u.autojit_function(value),
        is_registered:
        lambda value: from_typefunc(value, u),
    }

    return table
コード例 #5
0
def promote_int(u, promote, type1, type2):
    """
    Promote two integer types. We have to remain portable at this point, e.g.
    promote_int(Py_ssize_t, int) should be Py_ssize_t, and not int64 or int32.
    """
    t1, t2 = type1.get_dtype(), type2.get_dtype()
    return numpy_support.map_dtype(np.result_type(t1, t2))
コード例 #6
0
ファイル: numpyufuncs.py プロジェクト: ASPP/numba
def numba_type_from_sig(ufunc_signature):
    """
    Convert ufunc type signature string (e.g. 'dd->d') to a function
    """
    args, ret = ufunc_signature.split('->')
    to_numba = lambda c: numpy_support.map_dtype(np.dtype(c))

    signature = to_numba(ret)(*map(to_numba, args))
    return signature
コード例 #7
0
 def type_ndarray(value):
     if isinstance(value, np.ndarray):
         dtype = numpy_support.map_dtype(value.dtype)
         return u.array(dtype, value.ndim)
コード例 #8
0
def get_default_typing_rules(u, typeof, promote):
    """
    Get a table mapping Python classes to handlers (value -> type)

    :param u: The type universe
    """
    from numba.typesystem import numpy_support

    table = {}

    def register(*classes):
        def dec(func):
            for cls in classes:
                table[cls] = lambda u, value: func(value)
            return func

        return dec

    @register(int, long)
    def type_int(value):
        if abs(value) < 1:
            bits = 0
        else:
            bits = math.ceil(math.log(abs(value), 2))

        if bits < 32:
            return u.int_
        elif bits < 64:
            return u.int64
        else:
            raise ValueError("Cannot represent %s as int32 or int64", value)

    @register(np.ndarray)
    def type_ndarray(value):
        if isinstance(value, np.ndarray):
            dtype = numpy_support.map_dtype(value.dtype)
            return u.array(dtype, value.ndim)
            #is_c_contig=value.flags['C_CONTIGUOUS'],
            #is_f_contig=value.flags['F_CONTIGUOUS'])

    @register(tuple, list, dict)
    def type_container(value):
        assert isinstance(value, (tuple, list, dict))

        if isinstance(value, dict):
            key_type = type_container(value.keys(), promote, typeof)
            value_type = type_container(value.values(), promote, typeof)
            return u.dict_(key_type, value_type, size=len(value))

        if isinstance(value, tuple):
            container_type = u.tuple_
        else:
            container_type = u.list_

        if 0 < len(value) < 30:
            # Figure out base type if the container is not too large
            # base_type = reduce(promote, (typeof(child) for child in value))
            ty = typeof(value[0])
            if all(typeof(child) == ty for child in value):
                base_type = ty
            else:
                base_type = u.object_
        else:
            base_type = u.object_

        return container_type(base_type, size=len(value))

    register(
        np.dtype)(lambda value: u.numpy_dtype(numpy_support.map_dtype(value)))
    register(types.ModuleType)(lambda value: u.module(value))
    register(itypesystem.Type)(lambda value: u.meta(value))

    return table
コード例 #9
0
ファイル: constants.py プロジェクト: FrancescAlted/numba
 def type_ndarray(value):
     if isinstance(value, np.ndarray):
         dtype = numpy_support.map_dtype(value.dtype)
         return u.array(dtype, value.ndim)
コード例 #10
0
ファイル: constants.py プロジェクト: FrancescAlted/numba
def get_default_typing_rules(u, typeof, promote):
    """
    Get a table mapping Python classes to handlers (value -> type)

    :param u: The type universe
    """

    table = {}
    def register(*classes):
        def dec(func):
            for cls in classes:
                table[cls] = lambda u, value: func(value)
            return func
        return dec

    @register(int, long)
    def type_int(value):
        if abs(value) < 1:
            bits = 0
        else:
            bits = math.ceil(math.log(abs(value), 2))

        if bits < 32:
            return u.int_
        elif bits < 64:
            return u.int64
        else:
            raise ValueError("Cannot represent %s as int32 or int64", value)

    @register(np.ndarray)
    def type_ndarray(value):
        if isinstance(value, np.ndarray):
            dtype = numpy_support.map_dtype(value.dtype)
            return u.array(dtype, value.ndim)
                           #is_c_contig=value.flags['C_CONTIGUOUS'],
                           #is_f_contig=value.flags['F_CONTIGUOUS'])

    @register(tuple, list, dict)
    def type_container(value):
        assert isinstance(value, (tuple, list, dict))

        if isinstance(value, dict):
            key_type = type_container(value.keys(), promote, typeof)
            value_type = type_container(value.values(), promote, typeof)
            return u.dict_(key_type, value_type, size=len(value))

        if isinstance(value, tuple):
            container_type = u.tuple_
        else:
            container_type = u.list_

        if 0 < len(value) < 30:
            # Figure out base type if the container is not too large
            # base_type = reduce(promote, (typeof(child) for child in value))
            ty = typeof(value[0])
            if all(typeof(child) == ty for child in value):
                base_type = ty
            else:
                base_type = u.object_
        else:
            base_type = u.object_

        return container_type(base_type, size=len(value))

    register(np.dtype)(lambda value: u.numpy_dtype(numpy_support.map_dtype(value)))
    register(types.ModuleType)(lambda value: u.module(value))
    register(itypesystem.Type)(lambda value: u.meta(value))

    return table