Example #1
0
 def __getitem__(self, key):
     try:
         size = []
         typ = int
         for k in range(len(key)):
             step = key[k].step
             start = key[k].start
             if start is None:
                 start = 0
             if step is None:
                 step = 1
             if isinstance(step, complex):
                 size.append(int(abs(step)))
                 typ = float
             else:
                 size.append(
                     int(math.ceil((key[k].stop - start)/(step*1.0))))
             if (isinstance(step, float) or
                     isinstance(start, float) or
                     isinstance(key[k].stop, float)):
                 typ = float
         if self.sparse:
             nn = [_nx.arange(_x, dtype=_t)
                     for _x, _t in zip(size, (typ,)*len(size))]
         else:
             nn = _nx.indices(size, typ)
         for k in range(len(size)):
             step = key[k].step
             start = key[k].start
             if start is None:
                 start = 0
             if step is None:
                 step = 1
             if isinstance(step, complex):
                 step = int(abs(step))
                 if step != 1:
                     step = (key[k].stop - start)/float(step-1)
             nn[k] = (nn[k]*step+start)
         if self.sparse:
             slobj = [_nx.newaxis]*len(size)
             for k in range(len(size)):
                 slobj[k] = slice(None, None)
                 nn[k] = nn[k][slobj]
                 slobj[k] = _nx.newaxis
         return nn
     except (IndexError, TypeError):
         step = key.step
         stop = key.stop
         start = key.start
         if start is None:
             start = 0
         if isinstance(step, complex):
             step = abs(step)
             length = int(step)
             if step != 1:
                 step = (key.stop-start)/float(step-1)
             stop = key.stop + step
             return _nx.arange(0, length, 1, float)*step + start
         else:
             return _nx.arange(start, stop, step)
Example #2
0
def triu_indices(n, k=0, m=None):
    """
    Return the indices for the upper-triangle of an (n, m) array.

    Parameters
    ----------
    n : int
        The size of the arrays for which the returned indices will
        be valid.
    k : int, optional
        Diagonal offset (see `triu` for details).
    m : int, optional
        .. versionadded:: 1.9.0

        The column dimension of the arrays for which the returned
        arrays will be valid.
        By default `m` is taken equal to `n`.


    Returns
    -------
    inds : tuple, shape(2) of ndarrays, shape(`n`)
        The indices for the triangle. The returned tuple contains two arrays,
        each with the indices along one dimension of the array.  Can be used
        to slice a ndarray of shape(`n`, `n`).

    See also
    --------
    tril_indices : similar function, for lower-triangular.
    mask_indices : generic function accepting an arbitrary mask function.
    triu, tril

    Notes
    -----
    .. versionadded:: 1.4.0

    Examples
    --------
    Compute two different sets of indices to access 4x4 arrays, one for the
    upper triangular part starting at the main diagonal, and one starting two
    diagonals further right:

    >>> iu1 = np.triu_indices(4)
    >>> iu2 = np.triu_indices(4, 2)

    Here is how they can be used with a sample array:

    >>> a = np.arange(16).reshape(4, 4)
    >>> a
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])

    Both for indexing:

    >>> a[iu1]
    array([ 0,  1,  2, ..., 10, 11, 15])

    And for assigning values:

    >>> a[iu1] = -1
    >>> a
    array([[-1, -1, -1, -1],
           [ 4, -1, -1, -1],
           [ 8,  9, -1, -1],
           [12, 13, 14, -1]])

    These cover only a small part of the whole array (two diagonals right
    of the main one):

    >>> a[iu2] = -10
    >>> a
    array([[ -1,  -1, -10, -10],
           [  4,  -1,  -1, -10],
           [  8,   9,  -1,  -1],
           [ 12,  13,  14,  -1]])

    """
    tri_ = ~tri(n, m, k=k - 1, dtype=bool)

    return tuple(
        broadcast_to(inds, tri_.shape)[tri_]
        for inds in indices(tri_.shape, sparse=True))
Example #3
0
 def __getitem__(self, key):
     try:
         size = []
         typ = int
         for k in range(len(key)):
             step = key[k].step
             start = key[k].start
             if start is None:
                 start = 0
             if step is None:
                 step = 1
             if isinstance(step, complex):
                 size.append(int(abs(step)))
                 typ = float
             else:
                 size.append(
                     int(math.ceil((key[k].stop - start) / (step * 1.0))))
             if (isinstance(step, float) or isinstance(start, float)
                     or isinstance(key[k].stop, float)):
                 typ = float
         if self.sparse:
             nn = [
                 _nx.arange(_x, dtype=_t)
                 for _x, _t in zip(size, (typ, ) * len(size))
             ]
         else:
             nn = _nx.indices(size, typ)
         for k in range(len(size)):
             step = key[k].step
             start = key[k].start
             if start is None:
                 start = 0
             if step is None:
                 step = 1
             if isinstance(step, complex):
                 step = int(abs(step))
                 if step != 1:
                     step = (key[k].stop - start) / float(step - 1)
             nn[k] = (nn[k] * step + start)
         if self.sparse:
             slobj = [_nx.newaxis] * len(size)
             for k in range(len(size)):
                 slobj[k] = slice(None, None)
                 nn[k] = nn[k][slobj]
                 slobj[k] = _nx.newaxis
         return nn
     except (IndexError, TypeError):
         step = key.step
         stop = key.stop
         start = key.start
         if start is None:
             start = 0
         if isinstance(step, complex):
             step = abs(step)
             length = int(step)
             if step != 1:
                 step = (key.stop - start) / float(step - 1)
             stop = key.stop + step
             return _nx.arange(0, length, 1, float) * step + start
         else:
             return _nx.arange(start, stop, step)