(self, index)
return self.__class__(
            backend=self.backend,
            shape=tuple(new_shape),
            dtype=self.dtype,
            allocator=self.allocator,
            base=self,
            gpudata=int(self.gpudata) + new_offset,
            strides=new_strides,
            take_array=take_array,
            name=self.name,
            rounding=self.rounding)
Return a sliced view of an array.
Return a sliced view of an array.
Return a sliced view of an array .
def __getitem__(self, index):
        """
        Return a sliced view of an array.
        """
        if not isinstance(index, tuple):
            # speed up common case of [:]
            if index == _none_slice:
                return self
            index = (index,)

        new_shape = []
        new_offset = 0
        new_strides = []

        seen_ellipsis = False
        take_array = None

        index_axis = 0
        array_axis = 0

        while index_axis < len(index):

            index_entry = index[index_axis]

            if array_axis > len(self.shape):
                raise IndexError("too many axes in index")

            # Standard slicing (start:stop:step)
            if isinstance(index_entry, slice):
                start, stop, idx_strides = index_entry.indices(
                    self.shape[array_axis])

                array_strides = self.strides[array_axis]

                # def ceil_div(x, y): return -(-x // y)
                new_shape.append(-((start - stop) // idx_strides))
                new_strides.append(idx_strides * array_strides)
                new_offset += array_strides * start * self.dtype.itemsize

                index_axis += 1
                array_axis += 1

            # Fancy indexing
            elif isinstance(index_entry, (GPUTensor, np.ndarray, list, tuple)):

                if isinstance(index_entry, (list, tuple)):
                    index_entry = np.array(index_entry, dtype=np.int32)

                if isinstance(index_entry, np.ndarray):
                    index_entry = self.__class__(
                        self.backend, index_entry.shape, dtype=np.int32).set(index_entry)

                size = max(index_entry.shape)
                if size != index_entry.size:
                    raise IndexError(
                        "Fancy indexing only currently supported dim > 1 in a single dimension.")

                if take_array is not None:
                    raise IndexError(
                        "Fancy indexing only currently supported one axis at a time.")

                if index_entry.dtype.type is not np.int32:
                    # TODO: this should now work for all int types, but need to
                    # test
                    raise IndexError(
                        "Fancy indexing only currently supported with int32 types.")

                take_array = (index_entry, array_axis)

                new_shape.append(size)
                new_strides.append(self.strides[array_axis])

                index_axis += 1
                array_axis += 1

            elif isinstance(index_entry, (int, np.integer)):
                array_shape = self.shape[array_axis]
                if index_entry < 0:
                    index_entry += array_shape

                if not (0 <= index_entry < array_shape):
                    raise IndexError(
                        "subindex in axis %d out of range" % index_axis)

                new_offset += self.strides[array_axis] * \
                    index_entry * self.dtype.itemsize

                if len(self.shape) < 3:
                    new_shape.append(1)
                    new_strides.append(self.strides[array_axis])

                index_axis += 1
                array_axis += 1

            elif index_entry is Ellipsis:
                index_axis += 1

                remaining_index_count = len(index) - index_axis
                new_array_axis = len(self.shape) - remaining_index_count
                if new_array_axis < array_axis:
                    raise IndexError("invalid use of ellipsis in index")
                while array_axis < new_array_axis:
                    new_shape.append(self.shape[array_axis])
                    new_strides.append(self.strides[array_axis])
                    array_axis += 1

                if seen_ellipsis:
                    raise IndexError(
                        "more than one ellipsis not allowed in index")
                seen_ellipsis = True

            else:
                raise IndexError("invalid subindex in axis %d" % index_axis)

        while array_axis < len(self.shape):
            new_shape.append(self.shape[array_axis])
            new_strides.append(self.strides[array_axis])

            array_axis += 1

        return self.__class__(
            backend=self.backend,
            shape=tuple(new_shape),
            dtype=self.dtype,
            allocator=self.allocator,
            base=self,
            gpudata=int(self.gpudata) + new_offset,
            strides=new_strides,
            take_array=take_array,
            name=self.name,
            rounding=self.rounding)
def __getitem__ ( self , index ) : if not isinstance ( index , tuple ) : # speed up common case of [:] if index == _none_slice : return self index = ( index , ) new_shape = [ ] new_offset = 0 new_strides = [ ] seen_ellipsis = False take_array = None index_axis = 0 array_axis = 0 while index_axis < len ( index ) : index_entry = index [ index_axis ] if array_axis > len ( self . shape ) : raise IndexError ( "too many axes in index" ) # Standard slicing (start:stop:step) if isinstance ( index_entry , slice ) : start , stop , idx_strides = index_entry . indices ( self . shape [ array_axis ] ) array_strides = self . strides [ array_axis ] # def ceil_div(x, y): return -(-x // y) new_shape . append ( - ( ( start - stop ) // idx_strides ) ) new_strides . append ( idx_strides * array_strides ) new_offset += array_strides * start * self . dtype . itemsize index_axis += 1 array_axis += 1 # Fancy indexing elif isinstance ( index_entry , ( GPUTensor , np . ndarray , list , tuple ) ) : if isinstance ( index_entry , ( list , tuple ) ) : index_entry = np . array ( index_entry , dtype = np . int32 ) if isinstance ( index_entry , np . ndarray ) : index_entry = self . __class__ ( self . backend , index_entry . shape , dtype = np . int32 ) . set ( index_entry ) size = max ( index_entry . shape ) if size != index_entry . size : raise IndexError ( "Fancy indexing only currently supported dim > 1 in a single dimension." ) if take_array is not None : raise IndexError ( "Fancy indexing only currently supported one axis at a time." ) if index_entry . dtype . type is not np . int32 : # TODO: this should now work for all int types, but need to # test raise IndexError ( "Fancy indexing only currently supported with int32 types." ) take_array = ( index_entry , array_axis ) new_shape . append ( size ) new_strides . append ( self . strides [ array_axis ] ) index_axis += 1 array_axis += 1 elif isinstance ( index_entry , ( int , np . integer ) ) : array_shape = self . shape [ array_axis ] if index_entry < 0 : index_entry += array_shape if not ( 0 <= index_entry < array_shape ) : raise IndexError ( "subindex in axis %d out of range" % index_axis ) new_offset += self . strides [ array_axis ] * index_entry * self . dtype . itemsize if len ( self . shape ) < 3 : new_shape . append ( 1 ) new_strides . append ( self . strides [ array_axis ] ) index_axis += 1 array_axis += 1 elif index_entry is Ellipsis : index_axis += 1 remaining_index_count = len ( index ) - index_axis new_array_axis = len ( self . shape ) - remaining_index_count if new_array_axis < array_axis : raise IndexError ( "invalid use of ellipsis in index" ) while array_axis < new_array_axis : new_shape . append ( self . shape [ array_axis ] ) new_strides . append ( self . strides [ array_axis ] ) array_axis += 1 if seen_ellipsis : raise IndexError ( "more than one ellipsis not allowed in index" ) seen_ellipsis = True else : raise IndexError ( "invalid subindex in axis %d" % index_axis ) while array_axis < len ( self . shape ) : new_shape . append ( self . shape [ array_axis ] ) new_strides . append ( self . strides [ array_axis ] ) array_axis += 1 return self . __class__ ( backend = self . backend , shape = tuple ( new_shape ) , dtype = self . dtype , allocator = self . allocator , base = self , gpudata = int ( self . gpudata ) + new_offset , strides = new_strides , take_array = take_array , name = self . name , rounding = self . rounding )