IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
13.2 Object array functions


13.2 Object array functions

array( sequence=None, shape=None, typecode='O')
The function array is, for most practical purposes, all a user needs to know to construct an object array.

The first argument, sequence, can be an arbitrary sequence of Python objects, such as a list, tuple, or another object array.

  >>> import numarray.objects as obj
  >>> class C:
  ...     pass
  >>> c = C()
  >>> a = obj.array([c, c, c])
  >>> a
  ObjectArray([c, c, c])

Like objects in Python lists, objects in object arrays are referred to, not copied, so changes to the objects are reflected in the originals because they are one and the same.

     >>> a[0].attribute  = 'this'
     >>> c.attribute
     'this'

The second argument, shape, optionally specifies the shape of the array. If no shape is specified, the shape is implied by the sequence.

  >>> import numarray.objects as obj
  >>> class C:
  ...     pass
  >>> c = C()
  >>> a = obj.fromlist([c, c, c])
  >>> a
  ObjectArray([c, c, c])

The last argument, typecode, is there for backward compatibility with Numeric; it must be specified as 'O'.

asarray( obj)
asarray converts sequences which are not object arrays into object arrays. If obj is already an ObjectArray, it is returned unaltered.
  >>> import numarray.objects as obj
  >>> a = obj.asarray([1,''this'',''that''])
  >>> a
  ObjectArray([1 'this' 'that'])
  >>> b = obj.asarray(a)
  >>> b is a
  True

choose( selector, population, output=None)
choose selects elements from population based on the values in selector, either returning the selected array or storing it in the optional ObjectArray specified by output. selector should be an integer sequence where each element is within the range 0 to lenpopulation. population should be a sequence of ObjectArrays. The shapes of selector and each element of population must be mutually broadcastable.
  >>> import numarray.objects as obj
  >>> s = num.arange(25, shape=(5,5)) % 3
  >>> p = obj.fromlist(["foo", 1, {"this":"that"}])
  >>> obj.choose(s, p)
  ObjectArray([['foo', 1, {'this': 'that'}, 'foo', 1],
    [{'this': 'that'}, 'foo', 1, {'this': 'that'}, 'foo'],
    [1, {'this': 'that'}, 'foo', 1, {'this': 'that'}],
    ['foo', 1, {'this': 'that'}, 'foo', 1],
    [{'this': 'that'}, 'foo', 1, {'this': 'that'}, 'foo']])

sort( objects, axis=-1, output=None)
sort sorts the elements from objects along the specified axis. If an output array is specified, the result is stored there and the return value is None, otherwise the sort is returned.
    >>> import numarray.objects as obj
    >>> a = obj.ObjectArray(shape=(5,5))
    >>> a[:] = range(5,0,-1)
    >>> obj.sort(a)
    ObjectArray([[1, 2, 3, 4, 5],
                 [1, 2, 3, 4, 5],
                 [1, 2, 3, 4, 5],
                 [1, 2, 3, 4, 5],
                 [1, 2, 3, 4, 5]])
    >>> a[:] = range(5,0,-1)
    >>> a.transpose()
    >>> obj.sort(a, axis=0)
    ObjectArray([[1, 1, 1, 1, 1],
                 [2, 2, 2, 2, 2],
                 [3, 3, 3, 3, 3],
                 [4, 4, 4, 4, 4],
                 [5, 5, 5, 5, 5]])

argsort( objects, axis=-1, output=None)
argsort returns the sort order for the elements from objects along the specified axis. If an output array is specified, the result is stored there and the return value is None, otherwise the sort order is returned.
  >>> import numarray.objects as obj
  >>> a = obj.ObjectArray(shape=(5,5))
  >>> a[:] = ['e','d','c','b','a']
  >>> obj.argsort(a)
  array([[4, 3, 2, 1, 0],
         [4, 3, 2, 1, 0],
         [4, 3, 2, 1, 0],
         [4, 3, 2, 1, 0],
         [4, 3, 2, 1, 0]])

take( objects, indices, axis=0)
take returns elements of objects specified by tuple of index arrays indices along the specified axis.
  >>> import numarray.objects as obj
  >>> o = obj.fromlist(range(10))
  >>> a = obj.arange(5)*2
  >>> obj.take(o, a)
  ObjectArray([0, 2, 4, 6, 8])

put( objects, indices, values, axis=-1)
put stores values at the locations of objects specified by tuple of index arrays indices.
  >>> import numarray.objects as obj
  >>> o = obj.fromlist(range(10))
  >>> a = obj.arange(5)*2
  >>> obj.put(o, a, 0); o
  ObjectArray([0, 1, 0, 3, 0, 5, 0, 7, 0, 9])

add( objects1, objects2, out=None)
numarray.objects defines universal functions which are named after and use the operators defined in the standard library module operator.py. In addition, the operator hooks of the ObjectArray class are defined to call the operators. add applies the add operator to corresponding elements of objects1 and objects2. Like the ufuncs in the numerical side of numarray, the object ufuncs support reduction and accumulation. In addition to add, there are ufuncs defined for every unary and binary operator function in the standard library module operator.py. Some of these are given additional synonyms so that they use numarray naming conventions, e.g. sub has an alias named subtract.
  >>> import numarray.objects as obj
  >>> a = obj.fromlist(["t","u","w"])
  >>> a
  ObjectArray(['t', 'u', 'w'])
  >>> a+a
  ObjectArray(['tt', 'uu', 'ww'])
  >>> obj.add(a,a)
  ObjectArray(['tt', 'uu', 'ww'])
  >>> obj.add.reduce(a)
  'tuw' # not, as in the docs, an ObjectArray
  >>> obj.add.accumulate(a)
  ObjectArray(['t', 'tu', 'tuw']) # w, not v

  >>> a = obj.fromlist(["t","u","w"])
  >>> a
  ObjectArray(['t', 'u', 'w'])
  >>> a+a
  ObjectArray(['tt', 'uu', 'ww'])
  >>> obj.add(a,a)
  ObjectArray(['tt', 'uu', 'ww'])
  >>> obj.add.reduce(a)
  ObjectArray('tuv')
  >>> obj.add.accumulate(a)
  ObjectArray(['t', 'tu', 'tuv'])

Send comments to the NumArray community.