Esempio n. 1
0
def put(array, indices, values, axis=0):
    """put stores 'values' into 'array' at 'indices...'.

    parameters which must be specified by keyword:

    array           data to be indexed & stuffed (put to).

    indices         An integer sequence, or tuple of integer sequences
                    specifying the array coordinates to hich data
                    is to be put.  Partial indices result in entire
                    inner blocks being overwritten.

    values          A sequence of values to be written to the specified
                    indices of array.

    axis=0          selects the axis along which the put should be performed.

    >>> o = fromlist(range(10))
    >>> a = arange(5)*2
    >>> put(o, a, 0); o
    ObjectArray([0, 1, 0, 3, 0, 5, 0, 7, 0, 9])
    """
    work = asarray(array)
    if axis == 0:
        work._put((indices,), values)
    elif isinstance(axis, (int, long)):
        if isinstance(indices, (int, long, float)):
            raise ValueError("indices must be a sequence")
        work = _gen.swapaxes(work, 0, axis)
        work._put((indices,), values)
        work = _gen.swapaxes(work, 0, axis)
    else:
        def_axes = range(work.rank)
        for x in axis:
            def_axes.remove(x)
        axis = list(axis) + def_axes
        work = _gen.transpose(work, axis)
        work._put(indices, values)
        work = _gen.transpose(work, axis)
    if work is not array:
        if isinstance(array, _gen.NDArray):
            array[:] = work
Esempio n. 2
0
def put(array, indices, values, axis=0):
    """put stores 'values' into 'array' at 'indices...'.

    parameters which must be specified by keyword:

    array           data to be indexed & stuffed (put to).

    indices         An integer sequence, or tuple of integer sequences
                    specifying the array coordinates to hich data
                    is to be put.  Partial indices result in entire
                    inner blocks being overwritten.

    values          A sequence of values to be written to the specified
                    indices of array.

    axis=0          selects the axis along which the put should be performed.

    >>> o = fromlist(range(10))
    >>> a = arange(5)*2
    >>> put(o, a, 0); o
    ObjectArray([0, 1, 0, 3, 0, 5, 0, 7, 0, 9])
    """
    work = asarray(array)
    if axis == 0:
        work._put((indices, ), values)
    elif isinstance(axis, (int, long)):
        if isinstance(indices, (int, long, float)):
            raise ValueError("indices must be a sequence")
        work = _gen.swapaxes(work, 0, axis)
        work._put((indices, ), values)
        work = _gen.swapaxes(work, 0, axis)
    else:
        def_axes = range(work.rank)
        for x in axis:
            def_axes.remove(x)
        axis = list(axis) + def_axes
        work = _gen.transpose(work, axis)
        work._put(indices, values)
        work = _gen.transpose(work, axis)
    if work is not array:
        if isinstance(array, _gen.NDArray):
            array[:] = work
Esempio n. 3
0
def take(array, indices, axis=0):
    """take picks elements of 'array' specified by 'indices'.

    parameters which must be specified by keyword:

    array           data to be indexed & collected (taken from).

    indices         An integer sequence, or tuple of integer sequences
                    specifying the array coordinates from which data
                    is to be taken.  Partial indices result in entire
                    inner blocks being taken.

    axis=0          selects the axis (or axes) along which the take
                    should be performed.

    >>> o = fromlist(range(10))
    >>> a = arange(5)*2
    >>> take(o, a)
    ObjectArray([0, 2, 4, 6, 8])
    """
    if axis == 0:
        array = asarray(array)
        return array._take((indices,))
    elif isinstance(axis, (int, long)):
        if isinstance(indices, (int, long, float)):
            raise ValueError("indices must be a sequence")
        work = _gen.swapaxes(array, 0, axis)
        work = work._take((indices,))
        return _gen.swapaxes(work, 0, axis)
    else:
        def_axes = range(array.rank)
        for x in axis:
            def_axes.remove(x)
        axis = list(axis) + def_axes
        work = _gen.transpose(array, axis)
        return work._take(indices)
Esempio n. 4
0
def take(array, indices, axis=0):
    """take picks elements of 'array' specified by 'indices'.

    parameters which must be specified by keyword:

    array           data to be indexed & collected (taken from).

    indices         An integer sequence, or tuple of integer sequences
                    specifying the array coordinates from which data
                    is to be taken.  Partial indices result in entire
                    inner blocks being taken.

    axis=0          selects the axis (or axes) along which the take
                    should be performed.

    >>> o = fromlist(range(10))
    >>> a = arange(5)*2
    >>> take(o, a)
    ObjectArray([0, 2, 4, 6, 8])
    """
    if axis == 0:
        array = asarray(array)
        return array._take((indices, ))
    elif isinstance(axis, (int, long)):
        if isinstance(indices, (int, long, float)):
            raise ValueError("indices must be a sequence")
        work = _gen.swapaxes(array, 0, axis)
        work = work._take((indices, ))
        return _gen.swapaxes(work, 0, axis)
    else:
        def_axes = range(array.rank)
        for x in axis:
            def_axes.remove(x)
        axis = list(axis) + def_axes
        work = _gen.transpose(array, axis)
        return work._take(indices)