Пример #1
0
 def descr_transpose(self, space, args_w):
     if len(args_w) == 1 and space.isinstance_w(args_w[0], space.w_tuple):
         args_w = space.fixedview(args_w[0])
     if len(args_w) >= 1:
         for w_arg in args_w:
             try:
                 support.index_w(space, w_arg)
             except OperationError:
                 raise oefmt(space.w_TypeError, "an integer is required")
         raise oefmt(space.w_ValueError, "axes don't match array")
     return self
Пример #2
0
 def descr_transpose(self, space, args_w):
     if len(args_w) == 1 and space.isinstance_w(args_w[0], space.w_tuple):
         args_w = space.fixedview(args_w[0])
     if len(args_w) >= 1:
         for w_arg in args_w:
             try:
                 support.index_w(space, w_arg)
             except OperationError:
                 raise oefmt(space.w_TypeError, "an integer is required")
         raise oefmt(space.w_ValueError, "axes don't match array")
     return self
Пример #3
0
def choose(space, arr, choices, shape, dtype, out, mode):
    shapelen = len(shape)
    pairs = [a.create_iter(shape) for a in choices]
    iterators = [i[0] for i in pairs]
    states = [i[1] for i in pairs]
    arr_iter, arr_state = arr.create_iter(shape)
    out_iter, out_state = out.create_iter(shape)
    while not arr_iter.done(arr_state):
        choose_driver.jit_merge_point(shapelen=shapelen, dtype=dtype,
                                      mode=mode)
        index = support.index_w(space, arr_iter.getitem(arr_state))
        if index < 0 or index >= len(iterators):
            if mode == NPY.RAISE:
                raise OperationError(space.w_ValueError, space.wrap(
                    "invalid entry in choice array"))
            elif mode == NPY.WRAP:
                index = index % (len(iterators))
            else:
                assert mode == NPY.CLIP
                if index < 0:
                    index = 0
                else:
                    index = len(iterators) - 1
        val = iterators[index].getitem(states[index]).convert_to(space, dtype)
        out_iter.setitem(out_state, val)
        for i in range(len(iterators)):
            states[i] = iterators[i].next(states[i])
        out_state = out_iter.next(out_state)
        arr_state = arr_iter.next(arr_state)
Пример #4
0
def choose(space, arr, choices, shape, dtype, out, mode):
    shapelen = len(shape)
    pairs = [a.create_iter(shape) for a in choices]
    iterators = [i[0] for i in pairs]
    states = [i[1] for i in pairs]
    arr_iter, arr_state = arr.create_iter(shape)
    out_iter, out_state = out.create_iter(shape)
    while not arr_iter.done(arr_state):
        choose_driver.jit_merge_point(shapelen=shapelen,
                                      dtype=dtype,
                                      mode=mode)
        index = support.index_w(space, arr_iter.getitem(arr_state))
        if index < 0 or index >= len(iterators):
            if mode == NPY.RAISE:
                raise OperationError(
                    space.w_ValueError,
                    space.wrap("invalid entry in choice array"))
            elif mode == NPY.WRAP:
                index = index % (len(iterators))
            else:
                assert mode == NPY.CLIP
                if index < 0:
                    index = 0
                else:
                    index = len(iterators) - 1
        val = iterators[index].getitem(states[index]).convert_to(space, dtype)
        out_iter.setitem(out_state, val)
        for i in range(len(iterators)):
            states[i] = iterators[i].next(states[i])
        out_state = out_iter.next(out_state)
        arr_state = arr_iter.next(arr_state)
Пример #5
0
 def _single_item_index(self, space, w_idx):
     """ Return an index of single item if possible, otherwise raises
     IndexError
     """
     if (space.isinstance_w(w_idx, space.w_text) or
         space.isinstance_w(w_idx, space.w_slice) or
         space.is_w(w_idx, space.w_None)):
         raise IndexError
     if isinstance(w_idx, W_NDimArray) and not w_idx.is_scalar():
         raise ArrayArgumentException
     shape = self.get_shape()
     shape_len = len(shape)
     view_w = None
     if space.isinstance_w(w_idx, space.w_list):
         raise ArrayArgumentException
     if space.isinstance_w(w_idx, space.w_tuple):
         view_w = space.fixedview(w_idx)
         if len(view_w) != shape_len:
             raise IndexError
         # check for arrays
         for w_item in view_w:
             if (isinstance(w_item, W_NDimArray) or
                 space.isinstance_w(w_item, space.w_list)):
                 raise ArrayArgumentException
             elif space.is_w(w_item, space.w_Ellipsis):
                 raise IndexError
         return self._lookup_by_index(space, view_w)
     if shape_len == 0:
         raise oefmt(space.w_IndexError, "too many indices for array")
     elif shape_len > 1:
         raise IndexError
     idx = support.index_w(space, w_idx)
     return self._lookup_by_index(space, [space.newint(idx)])
Пример #6
0
 def _single_item_index(self, space, w_idx):
     """ Return an index of single item if possible, otherwise raises
     IndexError
     """
     if (space.isinstance_w(w_idx, space.w_str) or
         space.isinstance_w(w_idx, space.w_slice) or
         space.is_w(w_idx, space.w_None)):
         raise IndexError
     if isinstance(w_idx, W_NDimArray) and not w_idx.is_scalar():
         raise ArrayArgumentException
     shape = self.get_shape()
     shape_len = len(shape)
     view_w = None
     if space.isinstance_w(w_idx, space.w_list):
         raise ArrayArgumentException
     if space.isinstance_w(w_idx, space.w_tuple):
         view_w = space.fixedview(w_idx)
         if len(view_w) != shape_len:
             raise IndexError
         # check for arrays
         for w_item in view_w:
             if (isinstance(w_item, W_NDimArray) or
                 space.isinstance_w(w_item, space.w_list)):
                 raise ArrayArgumentException
             elif space.is_w(w_item, space.w_Ellipsis):
                 raise IndexError
         return self._lookup_by_index(space, view_w)
     if shape_len == 0:
         raise oefmt(space.w_IndexError, "too many indices for array")
     elif shape_len > 1:
         raise IndexError
     idx = support.index_w(space, w_idx)
     return self._lookup_by_index(space, [space.wrap(idx)])
Пример #7
0
def put(space, w_arr, w_indices, w_values, w_mode):
    from pypy.module.micronumpy.support import index_w

    arr = convert_to_array(space, w_arr)
    mode = clipmode_converter(space, w_mode)

    if not w_indices:
        raise OperationError(space.w_ValueError,
                             space.wrap("indice list cannot be empty"))
    if not w_values:
        raise OperationError(space.w_ValueError,
                             space.wrap("value list cannot be empty"))

    dtype = arr.get_dtype()

    if space.isinstance_w(w_indices, space.w_list):
        indices = space.listview(w_indices)
    else:
        indices = [w_indices]

    if space.isinstance_w(w_values, space.w_list):
        values = space.listview(w_values)
    else:
        values = [w_values]

    v_idx = 0
    for idx in indices:
        index = index_w(space, idx)

        if index < 0 or index >= arr.get_size():
            if mode == NPY_RAISE:
                raise OperationError(
                    space.w_IndexError,
                    space.wrap(
                        "index %d is out of bounds for axis 0 with size %d" %
                        (index, arr.get_size())))
            elif mode == NPY_WRAP:
                index = index % arr.get_size()
            elif mode == NPY_CLIP:
                if index < 0:
                    index = 0
                else:
                    index = arr.get_size() - 1
            else:
                assert False

        value = values[v_idx]

        if v_idx + 1 < len(values):
            v_idx += 1

        arr.setitem(space, [index], dtype.coerce(space, value))
Пример #8
0
def put(space, w_arr, w_indices, w_values, w_mode):
    from pypy.module.micronumpy.support import index_w

    arr = convert_to_array(space, w_arr)
    mode = clipmode_converter(space, w_mode)

    if not w_indices:
        raise OperationError(space.w_ValueError, space.wrap("indice list cannot be empty"))
    if not w_values:
        raise OperationError(space.w_ValueError, space.wrap("value list cannot be empty"))

    dtype = arr.get_dtype()

    if space.isinstance_w(w_indices, space.w_list):
        indices = space.listview(w_indices)
    else:
        indices = [w_indices]

    if space.isinstance_w(w_values, space.w_list):
        values = space.listview(w_values)
    else:
        values = [w_values]

    v_idx = 0
    for idx in indices:
        index = index_w(space, idx)

        if index < 0 or index >= arr.get_size():
            if mode == NPY_RAISE:
                raise OperationError(
                    space.w_IndexError,
                    space.wrap("index %d is out of bounds for axis 0 with size %d" % (index, arr.get_size())),
                )
            elif mode == NPY_WRAP:
                index = index % arr.get_size()
            elif mode == NPY_CLIP:
                if index < 0:
                    index = 0
                else:
                    index = arr.get_size() - 1
            else:
                assert False

        value = values[v_idx]

        if v_idx + 1 < len(values):
            v_idx += 1

        arr.setitem(space, [index], dtype.coerce(space, value))
Пример #9
0
 def _lookup_by_index(self, space, view_w):
     item = self.start
     strides = self.get_strides()
     for i, w_index in enumerate(view_w):
         if space.isinstance_w(w_index, space.w_slice):
             raise IndexError
         idx = support.index_w(space, w_index)
         if idx < 0:
             idx = self.get_shape()[i] + idx
         if idx < 0 or idx >= self.get_shape()[i]:
             raise oefmt(space.w_IndexError,
                         "index %d is out of bounds for axis %d with size "
                         "%d", idx, i, self.get_shape()[i])
         item += idx * strides[i]
     return item
Пример #10
0
 def _lookup_by_index(self, space, view_w):
     item = self.start
     strides = self.get_strides()
     for i, w_index in enumerate(view_w):
         if space.isinstance_w(w_index, space.w_slice):
             raise IndexError
         idx = support.index_w(space, w_index)
         if idx < 0:
             idx = self.get_shape()[i] + idx
         if idx < 0 or idx >= self.get_shape()[i]:
             raise oefmt(space.w_IndexError,
                         "index %d is out of bounds for axis %d with size "
                         "%d", idx, i, self.get_shape()[i])
         item += idx * strides[i]
     return item
Пример #11
0
 def descr_item(self, space, args_w):
     if len(args_w) == 1 and space.isinstance_w(args_w[0], space.w_tuple):
         args_w = space.fixedview(args_w[0])
     if len(args_w) > 1:
         raise oefmt(space.w_ValueError,
                     "incorrect number of indices for array")
     elif len(args_w) == 1:
         try:
             idx = support.index_w(space, args_w[0])
         except OperationError:
             raise oefmt(space.w_TypeError, "an integer is required")
         if idx != 0:
             raise oefmt(space.w_IndexError,
                         "index %d is out of bounds for size 1", idx)
     return self.item(space)
Пример #12
0
 def descr_item(self, space, args_w):
     if len(args_w) == 1 and space.isinstance_w(args_w[0], space.w_tuple):
         args_w = space.fixedview(args_w[0])
     if len(args_w) > 1:
         raise oefmt(space.w_ValueError,
                     "incorrect number of indices for array")
     elif len(args_w) == 1:
         try:
             idx = support.index_w(space, args_w[0])
         except OperationError:
             raise oefmt(space.w_TypeError, "an integer is required")
         if idx != 0:
             raise oefmt(space.w_IndexError,
                         "index %d is out of bounds for size 1", idx)
     return self.item(space)
Пример #13
0
def put(space, w_arr, w_indices, w_values, w_mode):
    arr = convert_to_array(space, w_arr)
    mode = clipmode_converter(space, w_mode)

    if not w_indices:
        raise oefmt(space.w_ValueError, "indices list cannot be empty")
    if not w_values:
        raise oefmt(space.w_ValueError, "value list cannot be empty")

    dtype = arr.get_dtype()

    if space.isinstance_w(w_indices, space.w_list):
        indices = space.listview(w_indices)
    else:
        indices = [w_indices]

    if space.isinstance_w(w_values, space.w_list):
        values = space.listview(w_values)
    else:
        values = [w_values]

    v_idx = 0
    for idx in indices:
        index = support.index_w(space, idx)

        if index < 0 or index >= arr.get_size():
            if mode == NPY.RAISE:
                raise oefmt(
                    space.w_IndexError,
                    "index %d is out of bounds for axis 0 with size %d", index,
                    arr.get_size())
            elif mode == NPY.WRAP:
                index = index % arr.get_size()
            elif mode == NPY.CLIP:
                if index < 0:
                    index = 0
                else:
                    index = arr.get_size() - 1
            else:
                assert False

        value = values[v_idx]

        if v_idx + 1 < len(values):
            v_idx += 1

        arr.setitem(space, [index], dtype.coerce(space, value))
Пример #14
0
def put(space, w_arr, w_indices, w_values, w_mode):
    arr = convert_to_array(space, w_arr)
    mode = clipmode_converter(space, w_mode)

    if not w_indices:
        raise oefmt(space.w_ValueError, "indices list cannot be empty")
    if not w_values:
        raise oefmt(space.w_ValueError, "value list cannot be empty")

    dtype = arr.get_dtype()

    if space.isinstance_w(w_indices, space.w_list):
        indices = space.listview(w_indices)
    else:
        indices = [w_indices]

    if space.isinstance_w(w_values, space.w_list):
        values = space.listview(w_values)
    else:
        values = [w_values]

    v_idx = 0
    for idx in indices:
        index = support.index_w(space, idx)

        if index < 0 or index >= arr.get_size():
            if mode == NPY.RAISE:
                raise oefmt(space.w_IndexError,
                    "index %d is out of bounds for axis 0 with size %d",
                    index, arr.get_size())
            elif mode == NPY.WRAP:
                index = index % arr.get_size()
            elif mode == NPY.CLIP:
                if index < 0:
                    index = 0
                else:
                    index = arr.get_size() - 1
            else:
                assert False

        value = values[v_idx]

        if v_idx + 1 < len(values):
            v_idx += 1

        arr.setitem(space, [index], dtype.coerce(space, value))
Пример #15
0
 def _single_item_index(self, space, w_idx):
     """ Return an index of single item if possible, otherwise raises
     IndexError
     """
     if (space.isinstance_w(w_idx, space.w_str)
             or space.isinstance_w(w_idx, space.w_slice)
             or space.is_w(w_idx, space.w_None)):
         raise IndexError
     if isinstance(w_idx, W_NDimArray) and not w_idx.is_scalar():
         raise ArrayArgumentException
     shape = self.get_shape()
     shape_len = len(shape)
     view_w = None
     if space.isinstance_w(w_idx, space.w_list):
         raise ArrayArgumentException
     if space.isinstance_w(w_idx, space.w_tuple):
         view_w = space.fixedview(w_idx)
         if len(view_w) < shape_len:
             raise IndexError
         if len(view_w) > shape_len:
             # we can allow for one extra None
             count = len(view_w)
             for w_item in view_w:
                 if space.is_w(w_item, space.w_None):
                     count -= 1
             if count == shape_len:
                 raise IndexError  # but it's still not a single item
             raise oefmt(space.w_IndexError, "invalid index")
         # check for arrays
         for w_item in view_w:
             if (isinstance(w_item, W_NDimArray)
                     or space.isinstance_w(w_item, space.w_list)):
                 raise ArrayArgumentException
         return self._lookup_by_index(space, view_w)
     if shape_len == 0:
         raise oefmt(space.w_IndexError, "0-d arrays can't be indexed")
     elif shape_len > 1:
         raise IndexError
     idx = support.index_w(space, w_idx)
     return self._lookup_by_index(space, [space.wrap(idx)])
Пример #16
0
 def _single_item_index(self, space, w_idx):
     """ Return an index of single item if possible, otherwise raises
     IndexError
     """
     if (space.isinstance_w(w_idx, space.w_str) or
         space.isinstance_w(w_idx, space.w_slice) or
         space.is_w(w_idx, space.w_None)):
         raise IndexError
     if isinstance(w_idx, W_NDimArray) and not w_idx.is_scalar():
         raise ArrayArgumentException
     shape = self.get_shape()
     shape_len = len(shape)
     view_w = None
     if space.isinstance_w(w_idx, space.w_list):
         raise ArrayArgumentException
     if space.isinstance_w(w_idx, space.w_tuple):
         view_w = space.fixedview(w_idx)
         if len(view_w) < shape_len:
             raise IndexError
         if len(view_w) > shape_len:
             # we can allow for one extra None
             count = len(view_w)
             for w_item in view_w:
                 if space.is_w(w_item, space.w_None):
                     count -= 1
             if count == shape_len:
                 raise IndexError # but it's still not a single item
             raise oefmt(space.w_IndexError, "invalid index")
         # check for arrays
         for w_item in view_w:
             if (isinstance(w_item, W_NDimArray) or
                 space.isinstance_w(w_item, space.w_list)):
                 raise ArrayArgumentException
         return self._lookup_by_index(space, view_w)
     if shape_len == 0:
         raise oefmt(space.w_IndexError, "0-d arrays can't be indexed")
     elif shape_len > 1:
         raise IndexError
     idx = support.index_w(space, w_idx)
     return self._lookup_by_index(space, [space.wrap(idx)])