Exemplo n.º 1
0
    def resolve_transpose(self, ary, args, kws):
        def sentry_shape_scalar(ty):
            if ty in types.number_domain:
                # Guard against non integer type
                if not isinstance(ty, types.Integer):
                    raise TypeError("transpose() arg cannot be {0}".format(ty))
                return True
            else:
                return False

        assert not kws
        if len(args) == 0:
            return signature(self.resolve_T(ary))

        if len(args) == 1:
            shape, = args

            if sentry_shape_scalar(shape):
                assert ary.ndim == 1
                return signature(ary, *args)

            shape = normalize_shape(shape)
            if shape is None:
                return

            assert ary.ndim == shape.count
            return signature(self.resolve_T(ary), shape)

        else:
            if any(not sentry_shape_scalar(a) for a in args):
                raise TypeError("transpose({0}) is not supported".format(
                    ', '.join(args)))
            assert ary.ndim == len(args)
            return signature(self.resolve_T(ary), *args)
Exemplo n.º 2
0
def sum_expand(self, args, kws):
    """
    sum can be called with or without an axis parameter.
    """
    pysig = None
    if kws:
        def sum_stub(axis):
            pass
        pysig = utils.pysignature(sum_stub)
        # rewrite args
        args = list(args) + [kws['axis']]
        kws = None
    args_len = len(args)
    assert args_len <= 1
    if args_len == 0:
        # No axis parameter so the return type of the summation is a scalar
        # of the type of the array.
        out = signature(_expand_integer(self.this.dtype), *args,
                        recvr=self.this)
    else:
        # There is an axis paramter so the return type of this summation is
        # an array of dimension one less than the input array.
        return_type = types.Array(dtype=_expand_integer(self.this.dtype),
                                  ndim=self.this.ndim-1, layout='C')
        out = signature(return_type, *args, recvr=self.this)
    return out.replace(pysig=pysig)
Exemplo n.º 3
0
 def generic(self, args, kws):
     assert not kws
     (val,) = args
     if isinstance(val, (types.Buffer, types.BaseTuple)):
         return signature(types.intp, val)
     elif isinstance(val, (types.RangeType)):
         return signature(val.dtype, val)
Exemplo n.º 4
0
def generic_hetero_always_real(self, args, kws):
    assert not args
    assert not kws
    if isinstance(self.this.dtype, (types.Integer, types.Boolean)):
        return signature(types.float64, recvr=self.this)
    if isinstance(self.this.dtype, types.Complex):
        return signature(self.this.dtype.underlying_float, recvr=self.this)
    return signature(self.this.dtype, recvr=self.this)
Exemplo n.º 5
0
    def generic(self, args, kws):
        assert not kws
        ary, idx, val = args

        if ary.ndim == 1:
            return signature(ary.dtype, ary, types.intp, ary.dtype)
        elif ary.ndim > 1:
            return signature(ary.dtype, ary, idx, ary.dtype)
Exemplo n.º 6
0
def generic_expand(self, args, kws):
    if isinstance(self.this.dtype, types.Integer):
        # Expand to a machine int, not larger (like Numpy)
        if self.this.dtype.signed:
            return signature(max(types.intp, self.this.dtype), recvr=self.this)
        else:
            return signature(max(types.uintp, self.this.dtype), recvr=self.this)
    return signature(self.this.dtype, recvr=self.this)
Exemplo n.º 7
0
 def generic(self, args, kws):
     assert not kws
     # empty tuple case
     if len(args) == 0:
         return signature(types.Tuple(()))
     (val,) = args
     # tuple as input
     if isinstance(val, types.BaseTuple):
         return signature(val, val)
Exemplo n.º 8
0
    def generic(self, args, kws):
        assert not kws

        [arg] = args

        if isinstance(arg, types.Integer):
            return signature(arg, arg)
        if isinstance(arg, (types.Float, types.Boolean)):
            return signature(types.intp, arg)
Exemplo n.º 9
0
 def generic(self, args, kws):
     if len(args) == 1:
         # Guard against unary + and -
         return
     left, right = args
     if not all(isinstance(tp, types.NPTimedelta) for tp in args):
         return
     if npdatetime.can_cast_timedelta_units(left.unit, right.unit):
         return signature(right, left, right)
     elif npdatetime.can_cast_timedelta_units(right.unit, left.unit):
         return signature(left, left, right)
Exemplo n.º 10
0
 def generic(self, args, kws):
     assert not kws
     nargs = len(args)
     if nargs == 2:
         [inp, out] = args
         if isinstance(inp, types.Array) and isinstance(out, types.Array):
             return signature(out, inp, out)
     elif nargs == 1:
         [inp] = args
         if inp in types.number_domain:
             return signature(types.float64, types.float64)
Exemplo n.º 11
0
    def generic(self, args, kws):
        assert not kws
        ary, idx, val = args

        # Implementation presently supports float64 only,
        # so fail typing otherwise
        if ary.dtype != types.float64:
            return

        if ary.ndim == 1:
            return signature(ary.dtype, ary, types.intp, ary.dtype)
        elif ary.ndim > 1:
            return signature(ary.dtype, ary, idx, ary.dtype)
Exemplo n.º 12
0
    def generic(self, args, kws):
        assert not kws
        ary, idx, val = args
        if not isinstance(ary, types.Buffer):
            return
        if not ary.mutable:
            raise TypeError("Cannot modify value of type %s" %(ary,))
        out = get_array_index_type(ary, idx)
        if out is None:
            return

        idx = out.index
        res = out.result
        if isinstance(res, types.Array):
            # Indexing produces an array
            if isinstance(val, types.Array):
                if not self.context.can_convert(val.dtype, res.dtype):
                    # DType conversion not possible
                    return
                else:
                    res = val
            elif isinstance(val, types.Sequence):
                if (res.ndim == 1 and
                    self.context.can_convert(val.dtype, res.dtype)):
                    # Allow assignement of sequence to 1d array
                    res = val
                else:
                    # NOTE: sequence-to-array broadcasting is unsupported
                    return
            else:
                # Allow scalar broadcasting
                if self.context.can_convert(val, res.dtype):
                    res = res.dtype
                else:
                    # Incompatible scalar type
                    return
        elif not isinstance(val, types.Array):
            # Single item assignment
            if not self.context.can_convert(val, res):
                # if the array dtype is not yet defined
                if not res.is_precise():
                    # set the array type to use the dtype of value (RHS)
                    newary = ary.copy(dtype=val)
                    return signature(types.none, newary, idx, res)
                else:
                    return
            res = val
        else:
            return
        return signature(types.none, ary, idx, res)
Exemplo n.º 13
0
 def resolve_take(self, ary, args, kws):
     assert not kws
     argty, = args
     if isinstance(argty, types.Integer):
         sig = signature(ary.dtype, *args)
     elif isinstance(argty, types.Array):
         sig = signature(argty.copy(layout='C', dtype=ary.dtype), *args)
     elif isinstance(argty, types.List): # 1d lists only
         sig = signature(types.Array(ary.dtype, 1, 'C'), *args)
     elif isinstance(argty, types.BaseTuple):
         sig = signature(types.Array(ary.dtype, np.ndim(argty), 'C'), *args)
     else:
         raise TypeError("take(%s) not supported for %s" % argty)
     return sig
Exemplo n.º 14
0
 def generic(self, args, kws):
     # Resolution of members for record and structured arrays
     record, idx, value = args
     if isinstance(record, types.Record) and isinstance(idx, str):
         expectedty = record.typeof(idx)
         if self.context.can_convert(value, expectedty) is not None:
             return signature(types.void, record, types.Const(idx), value)
Exemplo n.º 15
0
    def generic(self, args, kws):
        assert not kws

        [arg] = args

        if arg not in types.number_domain:
            raise TypeError("float() only support for numbers")

        if arg in types.complex_domain:
            raise TypeError("float() does not support complex")

        if arg in types.integer_domain:
            return signature(types.float64, arg)

        elif arg in types.real_domain:
            return signature(arg, arg)
Exemplo n.º 16
0
 def resolve_itemset(self, ary, args, kws):
     assert not kws
     # We don't support explicit arguments as that's exactly equivalent
     # to regular indexing.  The no-argument form is interesting to
     # allow some degree of genericity when writing functions.
     if len(args) == 1:
         return signature(types.none, ary.dtype)
Exemplo n.º 17
0
    def generic(self, args, kws):
        assert not kws

        if len(args) == 1:
            [arg] = args
            if arg not in types.number_domain:
                raise TypeError("complex() only support for numbers")
            return signature(types.complex128, arg)

        elif len(args) == 2:
            [real, imag] = args

            if (real not in types.number_domain or
                        imag not in types.number_domain):
                raise TypeError("complex() only support for numbers")
            return signature(types.complex128, real, imag)
Exemplo n.º 18
0
    def generic(self, args, kws):
        assert not kws
        [ary, idx] = args
        if not isinstance(ary, types.Array):
            return

        idx = normalize_index(idx)
        if not idx:
            return

        if idx == types.slice3_type: #(types.slice2_type, types.slice3_type):
            res = ary.copy(layout='A')
        elif isinstance(idx, (types.UniTuple, types.Tuple)):
            if ary.ndim > len(idx):
                return
            elif ary.ndim < len(idx):
                return
            elif any(i == types.slice3_type for i in idx):
                ndim = ary.ndim
                for i in idx:
                    if i != types.slice3_type:
                        ndim -= 1
                res = ary.copy(ndim=ndim, layout='A')
            else:
                res = ary.dtype
        elif idx == types.intp:
            if ary.ndim != 1:
                return
            res = ary.dtype

        else:
            raise Exception("unreachable: index type of %s" % idx)

        return signature(res, ary, idx)
Exemplo n.º 19
0
 def generic(self, args, kws):
     assert not args
     assert not kws
     this = self.this
     if this.layout == 'C':
         resty = this.copy(ndim=1)
         return signature(resty, recvr=this)
Exemplo n.º 20
0
    def generic(self, args, kws):
        assert not kws
        ary, idx, val = args
        if not isinstance(ary, types.Buffer):
            return
        if not ary.mutable:
            raise TypeError("Cannot modify value of type %s" %(ary,))
        out = get_array_index_type(ary, idx)
        if out is None:
            return

        idx = out.index
        res = out.result
        if isinstance(res, types.Array):
            # Indexing produces an array
            if not isinstance(val, types.Array):
                # Allow scalar broadcasting
                res = res.dtype
            elif (val.ndim == res.ndim and
                  self.context.can_convert(val.dtype, res.dtype)):
                # Allow assignement of same-dimensionality compatible-dtype array
                res = val
            else:
                # Unexpected dimensionality of assignment source
                # (array broadcasting is unsupported)
                return
        elif not isinstance(val, types.Array):
            # Single item assignment
            res = val
        else:
            return
        return signature(types.none, ary, idx, res)
Exemplo n.º 21
0
 def resolve_view(self, ary, args, kws):
     from .npydecl import _parse_dtype
     assert not kws
     dtype, = args
     dtype = _parse_dtype(dtype)
     retty = ary.copy(dtype=dtype)
     return signature(retty, *args)
Exemplo n.º 22
0
 def generic(self, args, kws):
     assert not kws
     if len(args) == 1:
         # One-argument type() -> return the __class__
         classty = self.context.resolve_getattr(args[0], "__class__")
         if classty is not None:
             return signature(classty, *args)
Exemplo n.º 23
0
 def generic(self, args, kws):
     assert not kws
     ary, idx, val = args
     if isinstance(ary, types.Buffer):
         if not ary.mutable:
             raise TypeError("Immutable array")
         return signature(types.none, ary, normalize_index(idx), ary.dtype)
Exemplo n.º 24
0
 def generic(self, args, kws):
     for a in args:
         sig = self.context.resolve_function_type("print_item", (a,), {})
         if sig is None:
             raise TypeError("Type %s is not printable." % a)
         assert sig.return_type is types.none
     return signature(types.none, *args)
Exemplo n.º 25
0
 def generic(self, args, kws):
     assert not kws
     args = args[::-1] if self.reverse_args else args
     sig = self.context.resolve_function_type(self.op, args, kws)
     if self.reverse_args and sig is not None:
         sig = signature(sig.return_type, *sig.args[::-1])
     return sig
Exemplo n.º 26
0
 def resolve_nonzero(self, ary, args, kws):
     assert not args
     assert not kws
     # 0-dim arrays return one result array
     ndim = max(ary.ndim, 1)
     retty = types.UniTuple(types.Array(types.intp, 1, 'C'), ndim)
     return signature(retty)
Exemplo n.º 27
0
    def generic(self, args, kws):
        assert not kws
        ary, idx, val = args
        # Implementation presently supports:
        # float64, float32, int32, int64, uint32, uint64 only,
        # so fail typing otherwise
        supported_types = (types.float64, types.float32,
                           types.int32, types.uint32,
                           types.int64, types.uint64)
        if ary.dtype not in supported_types:
            return

        if ary.ndim == 1:
            return signature(ary.dtype, ary, types.intp, ary.dtype)
        elif ary.ndim > 1:
            return signature(ary.dtype, ary, idx, ary.dtype)
Exemplo n.º 28
0
 def generic(self, args, kws):
     assert not kws
     [ary, idx] = args
     out = get_array_index_type(ary, idx)
     if out is not None:
         ary, idx, res = out
         return signature(res, ary, idx)
Exemplo n.º 29
0
 def generic(self, args, kws):
     assert not kws
     ary, old, val = args
     dty = ary.dtype
     # only support int32
     if dty == types.int32 and ary.ndim == 1:
         return signature(dty, ary, dty, dty)
Exemplo n.º 30
0
 def generic(self, args, kws):
     assert not kws
     ary, idx, val = args
     if isinstance(ary, types.Array):
         if ary.const:
             raise TypeError("Constant array")
         return signature(types.none, ary, normalize_index(idx), ary.dtype)
Exemplo n.º 31
0
 def generic(self, args, kws):
     arg, = args
     if self.is_accepted_type(arg):
         return signature(types.none, *args)
Exemplo n.º 32
0
 def generic(self, args, kws):
     sig = signature(mnb_type, types.intp)
     pysig = utils.pysignature(MultinomialNB_dummy)
     sig.pysig = pysig
     return sig
Exemplo n.º 33
0
 def resolve_train(self, dict, args, kws):
     assert not kws
     assert len(args) == 2
     return signature(types.none, *args)
Exemplo n.º 34
0
class UnorderedCmpOp(ConcreteTemplate):
    cases = OrderedCmpOp.cases + [
        signature(types.boolean, op, op) for op in sorted(types.complex_domain)
    ]
Exemplo n.º 35
0
 def generic(self, args, kws):
     assert not kws
     if len(args) == 1:
         it = args[0]
         if isinstance(it, types.IteratorType):
             return signature(it.yield_type, *args)
Exemplo n.º 36
0
 def generic(self, args, kws):
     assert not kws
     assert len(args) == 1
     assert isinstance(args[0], (types.DType, types.NumberClass))
     return signature(args[0].dtype, *args)
Exemplo n.º 37
0
 def generic(self, args, kws):
     assert not kws
     [pair] = args
     if isinstance(pair, types.Pair):
         return signature(pair.second_type, pair)
Exemplo n.º 38
0
class BinOpFloorDiv(ConcreteTemplate):
    cases = list(integer_binop_cases)
    cases += [signature(op, op, op) for op in sorted(types.real_domain)]
Exemplo n.º 39
0
class DivMod(ConcreteTemplate):
    _tys = machine_ints + sorted(types.real_domain)
    cases = [signature(types.UniTuple(ty, 2), ty, ty) for ty in _tys]
Exemplo n.º 40
0
 def generic(self, args, kws):
     assert not kws
     ary, idx, val = args
     if ary == string_array_type:
         return signature(types.none, *args)
Exemplo n.º 41
0
 def generic(self, args, kws):
     arg, = args
     return signature(types.none, *args)
Exemplo n.º 42
0
 def generic(self, args, kws):
     assert not kws
     [obj] = args
     if isinstance(obj, types.IterableType):
         return signature(obj.iterator_type, obj)
Exemplo n.º 43
0
 def resolve_copy(self, ary, args, kws):
     return signature(string_array_type, *args)
Exemplo n.º 44
0
 def generic(self, args, kws):
     assert not kws
     if all(isinstance(it, types.IterableType) for it in args):
         zip_type = types.ZipType(args)
         return signature(zip_type, *args)
Exemplo n.º 45
0
 def resolve_item(self, ty, args, kws):
     assert not kws
     if not args:
         return signature(ty)
Exemplo n.º 46
0
 def generic(self, args, kws):
     assert not kws
     arg, = args
     if isinstance(arg, types.Hashable):
         return signature(types.intp, *args)
Exemplo n.º 47
0
 def generic(self, args, kws):
     args = args[::-1]
     sig = self.context.resolve_function_type(operator.contains, args, kws)
     return signature(sig.return_type, *sig.args[::-1])
Exemplo n.º 48
0
 def resolve_conjugate(self, ty, args, kws):
     assert not args
     assert not kws
     return signature(ty)
Exemplo n.º 49
0
    def generic(self, args, kws):
        assert not kws
        (seq, val) = args

        if isinstance(seq, (types.Sequence)):
            return signature(types.boolean, seq, val)
Exemplo n.º 50
0
 def generic(self, args, kws):
     assert not kws
     (val, ) = args
     if isinstance(val, (types.BaseTuple)):
         return signature(types.boolean, val)
Exemplo n.º 51
0
 def generic(self, args, kws):
     [lhs, rhs] = args
     return signature(types.boolean, lhs, rhs)
Exemplo n.º 52
0
 def generic(self, args, kws):
     assert not kws
     ptr, idx, val = args
     if isinstance(ptr, types.CPointer) and isinstance(idx, types.Integer):
         return signature(types.none, ptr, normalize_1d_index(idx),
                          ptr.dtype)
Exemplo n.º 53
0
 def generic(self, args, kws):
     assert not kws
     [it] = args
     if isinstance(it, types.IteratorType):
         return signature(types.Pair(it.yield_type, types.boolean), it)
Exemplo n.º 54
0
 def generic(self, args, kws):
     assert not kws
     (arg1, arg2) = args
     if isinstance(arg1, types.Literal) and isinstance(arg2, types.Literal):
         return signature(types.boolean, arg1, arg2)
Exemplo n.º 55
0
    according to the integer typing NBEP.
    """
    bitwidth = choose_result_bitwidth(*inputs)
    signed = any(tp.signed for tp in inputs)
    return types.Integer.from_bitwidth(bitwidth, signed)


# The "machine" integer types to take into consideration for operator typing
# (according to the integer typing NBEP)
machine_ints = (sorted(set(
    (types.intp, types.int64))) + sorted(set((types.uintp, types.uint64))))

# Explicit integer rules for binary operators; smaller ints will be
# automatically upcast.
integer_binop_cases = tuple(
    signature(choose_result_int(op1, op2), op1, op2)
    for op1, op2 in itertools.product(machine_ints, machine_ints))


class BinOp(ConcreteTemplate):
    cases = list(integer_binop_cases)
    cases += [signature(op, op, op) for op in sorted(types.real_domain)]
    cases += [signature(op, op, op) for op in sorted(types.complex_domain)]


@infer_global(operator.add)
class BinOpAdd(BinOp):
    pass


@infer_global(operator.iadd)
Exemplo n.º 56
0
 def resolve_predict(self, dict, args, kws):
     assert not kws
     assert len(args) == 1
     return signature(types.Array(types.int32, 1, 'C'), *args)
Exemplo n.º 57
0
 def generic(self, args, kws):
     if not kws and len(args) == 1 and args[0] == string_type:
         return signature(string_array_type, *args)
Exemplo n.º 58
0
class BitwiseLogicOperation(BinOp):
    cases = [signature(types.boolean, types.boolean, types.boolean)]
    cases += list(integer_binop_cases)
    unsafe_casting = False