Пример #1
0
def repeat(space, w_arr, repeats, w_axis):
    arr = convert_to_array(space, w_arr)
    if space.is_none(w_axis):
        arr = arr.descr_flatten(space)
        orig_size = arr.get_shape()[0]
        shape = [arr.get_shape()[0] * repeats]
        w_res = W_NDimArray.from_shape(space,
                                       shape,
                                       arr.get_dtype(),
                                       w_instance=arr)
        for i in range(repeats):
            chunks = [Chunk(i, shape[0] - repeats + i, repeats, orig_size)]
            view = new_view(space, w_res, chunks)
            view.implementation.setslice(space, arr)
    else:
        axis = space.int_w(w_axis)
        shape = arr.get_shape()[:]
        chunks = [Chunk(0, i, 1, i) for i in shape]
        orig_size = shape[axis]
        shape[axis] *= repeats
        w_res = W_NDimArray.from_shape(space,
                                       shape,
                                       arr.get_dtype(),
                                       w_instance=arr)
        for i in range(repeats):
            chunks[axis] = Chunk(i, shape[axis] - repeats + i, repeats,
                                 orig_size)
            view = new_view(space, w_res, chunks)
            view.implementation.setslice(space, arr)
    return w_res
Пример #2
0
def concatenate(space, w_args, w_axis=None):
    args_w = space.listview(w_args)
    if len(args_w) == 0:
        raise oefmt(space.w_ValueError,
                    "need at least one array to concatenate")
    args_w = [convert_to_array(space, w_arg) for w_arg in args_w]
    if w_axis is None:
        w_axis = space.wrap(0)
    if space.is_none(w_axis):
        args_w = [
            w_arg.reshape(space, space.newlist([w_arg.descr_get_size(space)]))
            for w_arg in args_w
        ]
        w_axis = space.wrap(0)
    dtype = args_w[0].get_dtype()
    shape = args_w[0].get_shape()[:]
    ndim = len(shape)
    if ndim == 0:
        raise oefmt(space.w_ValueError,
                    "zero-dimensional arrays cannot be concatenated")
    axis = space.int_w(w_axis)
    orig_axis = axis
    if axis < 0:
        axis = ndim + axis
    if ndim == 1 and axis != 0:
        axis = 0
    if axis < 0 or axis >= ndim:
        raise oefmt(space.w_IndexError, "axis %d out of bounds [0, %d)",
                    orig_axis, ndim)
    for arr in args_w[1:]:
        if len(arr.get_shape()) != ndim:
            raise OperationError(
                space.w_ValueError,
                space.wrap(
                    "all the input arrays must have same number of dimensions")
            )
        for i, axis_size in enumerate(arr.get_shape()):
            if i == axis:
                shape[i] += axis_size
            elif axis_size != shape[i]:
                raise OperationError(
                    space.w_ValueError,
                    space.wrap("all the input array dimensions except for the "
                               "concatenation axis must match exactly"))

    dtype = find_result_type(space, args_w, [])
    # concatenate does not handle ndarray subtypes, it always returns a ndarray
    res = W_NDimArray.from_shape(space, shape, dtype, 'C')
    chunks = [Chunk(0, i, 1, i) for i in shape]
    axis_start = 0
    for arr in args_w:
        if arr.get_shape()[axis] == 0:
            continue
        chunks[axis] = Chunk(axis_start, axis_start + arr.get_shape()[axis], 1,
                             arr.get_shape()[axis])
        view = new_view(space, res, chunks)
        view.implementation.setslice(space, arr)
        axis_start += arr.get_shape()[axis]
    return res
Пример #3
0
 def descr_getitem(self, space, orig_arr, w_index):
     try:
         item = self._single_item_index(space, w_index)
         return self.getitem(item)
     except IndexError:
         # not a single result
         chunks = self._prepare_slice_args(space, w_index)
         return new_view(space, orig_arr, chunks)
Пример #4
0
 def descr_getitem(self, space, orig_arr, w_index):
     try:
         item = self._single_item_index(space, w_index)
         return self.getitem(item)
     except IndexError:
         # not a single result
         chunks = self._prepare_slice_args(space, w_index)
         return new_view(space, orig_arr, chunks)
Пример #5
0
 def descr_setitem(self, space, orig_arr, w_index, w_value):
     try:
         item = self._single_item_index(space, w_index)
         self.setitem(item, self.dtype.coerce(space, w_value))
     except IndexError:
         w_value = convert_to_array(space, w_value)
         chunks = self._prepare_slice_args(space, w_index)
         view = new_view(space, orig_arr, chunks)
         view.implementation.setslice(space, w_value)
Пример #6
0
 def descr_setitem(self, space, orig_arr, w_index, w_value):
     try:
         item = self._single_item_index(space, w_index)
         self.setitem(item, self.dtype.coerce(space, w_value))
     except IndexError:
         w_value = convert_to_array(space, w_value)
         chunks = self._prepare_slice_args(space, w_index)
         view = new_view(space, orig_arr, chunks)
         view.implementation.setslice(space, w_value)
Пример #7
0
def concatenate(space, w_args, w_axis=None):
    args_w = space.listview(w_args)
    if len(args_w) == 0:
        raise oefmt(space.w_ValueError, "need at least one array to concatenate")
    args_w = [convert_to_array(space, w_arg) for w_arg in args_w]
    if w_axis is None:
        w_axis = space.wrap(0)
    if space.is_none(w_axis):
        args_w = [w_arg.reshape(space,
                                space.newlist([w_arg.descr_get_size(space)]),
                                w_arg.get_order())
                  for w_arg in args_w]
        w_axis = space.wrap(0)
    dtype = args_w[0].get_dtype()
    shape = args_w[0].get_shape()[:]
    ndim = len(shape)
    if ndim == 0:
        raise oefmt(space.w_ValueError,
                    "zero-dimensional arrays cannot be concatenated")
    axis = space.int_w(w_axis)
    orig_axis = axis
    if axis < 0:
        axis = ndim + axis
    if ndim == 1 and axis != 0:
        axis = 0
    if axis < 0 or axis >= ndim:
        raise oefmt(space.w_IndexError, "axis %d out of bounds [0, %d)",
                    orig_axis, ndim)
    for arr in args_w[1:]:
        if len(arr.get_shape()) != ndim:
            raise oefmt(space.w_ValueError,
                        "all the input arrays must have same number of "
                        "dimensions")
        for i, axis_size in enumerate(arr.get_shape()):
            if i == axis:
                shape[i] += axis_size
            elif axis_size != shape[i]:
                raise oefmt(space.w_ValueError,
                            "all the input array dimensions except for the "
                            "concatenation axis must match exactly")

    dtype = find_result_type(space, args_w, [])
    # concatenate does not handle ndarray subtypes, it always returns a ndarray
    res = W_NDimArray.from_shape(space, shape, dtype, NPY.CORDER)
    chunks = [Chunk(0, i, 1, i) for i in shape]
    axis_start = 0
    for arr in args_w:
        if arr.get_shape()[axis] == 0:
            continue
        chunks[axis] = Chunk(axis_start, axis_start + arr.get_shape()[axis], 1,
                             arr.get_shape()[axis])
        view = new_view(space, res, chunks)
        view.implementation.setslice(space, arr)
        axis_start += arr.get_shape()[axis]
    return res
Пример #8
0
 def descr_getitem(self, space, orig_arr, w_index):
     try:
         item = self._single_item_index(space, w_index)
         return self.getitem(item)
     except IndexError:
         # not a single result
         chunks = self._prepare_slice_args(space, w_index)
         copy = False
         if isinstance(chunks[0], BooleanChunk):
             # numpy compatibility
             copy = True
         w_ret = new_view(space, orig_arr, chunks)
         if copy:
             w_ret = w_ret.descr_copy(space, space.newint(w_ret.get_order()))
         return w_ret
Пример #9
0
def repeat(space, w_arr, repeats, w_axis):
    arr = convert_to_array(space, w_arr)
    if space.is_none(w_axis):
        arr = arr.descr_flatten(space)
        orig_size = arr.get_shape()[0]
        shape = [arr.get_shape()[0] * repeats]
        w_res = W_NDimArray.from_shape(space, shape, arr.get_dtype(), w_instance=arr)
        for i in range(repeats):
            chunks = [Chunk(i, shape[0] - repeats + i, repeats, orig_size)]
            view = new_view(space, w_res, chunks)
            view.implementation.setslice(space, arr)
    else:
        axis = space.int_w(w_axis)
        shape = arr.get_shape()[:]
        chunks = [Chunk(0, i, 1, i) for i in shape]
        orig_size = shape[axis]
        shape[axis] *= repeats
        w_res = W_NDimArray.from_shape(space, shape, arr.get_dtype(), w_instance=arr)
        for i in range(repeats):
            chunks[axis] = Chunk(i, shape[axis] - repeats + i, repeats,
                                 orig_size)
            view = new_view(space, w_res, chunks)
            view.implementation.setslice(space, arr)
    return w_res
Пример #10
0
 def descr_getitem(self, space, orig_arr, w_index):
     try:
         item = self._single_item_index(space, w_index)
         return self.getitem(item)
     except IndexError:
         # not a single result
         chunks = self._prepare_slice_args(space, w_index)
         copy = False
         if isinstance(chunks[0], BooleanChunk):
             # numpy compatibility
             copy = True
         w_ret = new_view(space, orig_arr, chunks)
         if copy:
             w_ret = w_ret.descr_copy(space, space.wrap(w_ret.get_order()))
         return w_ret