Exemplo n.º 1
0
def choose(selector, population, output=None):
    """choose() picks the elements of the tuple of arrays 'population'
    which are specified by the 'selector' array.  The shape of the
    output array is determined by the broadcasting the selector with
    all of the population arrays.

    >>> choose(_nc.arange(5)%2, ("t", 1))
    ObjectArray(['t', 1, 't', 1, 't'])
    >>> choose(_nc.arange(3, shape=(3,1))%2, (fromlist(["t","u"]), "v"))
    ObjectArray([['t', 'u'],
                 ['v', 'v'],
                 ['t', 'u']])
    >>> a = ObjectArray(shape=(3,2))
    >>> choose(_nc.arange(3, shape=(3,1))%2, (fromlist(["t","u"]), "v"), a); a
    ObjectArray([['t', 'u'],
                 ['v', 'v'],
                 ['t', 'u']])
    """
    if not isinstance(selector, _nc.NumArray):
        selector = _nc.fromlist(selector)
    population = list(population)
    for i in xrange(len(population)):
        p = population[i]
        if not isinstance(p, _gen.NDArray):
            population[i] = fromlist([population[i]])
    inputs = _gen._nWayBroadcast([selector] + population)
    selector, population = inputs[0], inputs[1:]
    if output is None:
        output0 = ObjectArray(shape=selector.shape)
    else:
        output0 = output
    _choose(selector, tuple(population), output0)
    if output is None:
        return output0
Exemplo n.º 2
0
def choose(selector, population, output=None):
    """choose() picks the elements of the tuple of arrays 'population'
    which are specified by the 'selector' array.  The shape of the
    output array is determined by the broadcasting the selector with
    all of the population arrays.

    >>> choose(_nc.arange(5)%2, ("t", 1))
    ObjectArray(['t', 1, 't', 1, 't'])
    >>> choose(_nc.arange(3, shape=(3,1))%2, (fromlist(["t","u"]), "v"))
    ObjectArray([['t', 'u'],
                 ['v', 'v'],
                 ['t', 'u']])
    >>> a = ObjectArray(shape=(3,2))
    >>> choose(_nc.arange(3, shape=(3,1))%2, (fromlist(["t","u"]), "v"), a); a
    ObjectArray([['t', 'u'],
                 ['v', 'v'],
                 ['t', 'u']])
    """
    if not isinstance(selector, _nc.NumArray):
        selector = _nc.fromlist(selector)
    population = list(population)
    for i in xrange(len(population)):
        p = population[i]
        if not isinstance(p, _gen.NDArray):
            population[i] = fromlist([population[i]])
    inputs = _gen._nWayBroadcast([selector] + population)
    selector, population = inputs[0], inputs[1:]
    if output is None:
        output0 = ObjectArray(shape=selector.shape)
    else:
        output0 = output
    _choose(selector, tuple(population), output0)
    if output is None:
        return output0
Exemplo n.º 3
0
def _argsort(objects, witness):
    if len(objects._shape) == 1:
        lo = objects.tolist()
        lw = range(len(lo))
        z = zip(lo, lw)
        z.sort()
        sw = [w for o, w in z]
        w = _nc.fromlist(sw)
        witness._copyFrom(w)
    else:
        for i in xrange(objects._shape[0]):
            _argsort(objects[i], witness[i])
Exemplo n.º 4
0
def _argsort(objects, witness):
    if len(objects._shape) == 1:
        lo = objects.tolist()
        lw = range(len(lo))
        z = zip(lo, lw)
        z.sort()
        sw = [w for o, w in z]
        w = _nc.fromlist(sw)
        witness._copyFrom(w)
    else:
        for i in xrange(objects._shape[0]):
            _argsort(objects[i], witness[i])