def __setitem__(self, idx, value): try: if idx.dtype == numpy.dtype('bool') or (idx[0].dtype == 'bool' and len(idx) == 1): # Special case for boolean setitem self_flat = self.flat idx = afnumpy.where(idx.flat) self_flat[idx] = value return except AttributeError: pass except RuntimeError: # idx is all False return idx, idx_shape = indexing.__convert_dim__(self.shape, idx) if any(x is None for x in idx): # one of the indices is empty return idx = tuple(idx) if len(idx) == 0: idx = tuple([0]) if(isinstance(value, ndarray)): if(value.dtype != self.dtype): raise TypeError('left hand side must have same dtype as right hand side') value = indexing.__expand_dim__(self.shape, value, idx).d_array elif(isinstance(value, numbers.Number)): pass else: raise NotImplementedError('values must be a afnumpy.ndarray') self.d_array[idx] = value # This is a hack to be able to make it look like arrays with stride[0] > 1 can still be views # In practise right now it only applies to ndarray.real and ndarray.imag try: self._base[self._base_index] = self except AttributeError: pass
def __getitem__(self, args): if not isinstance(args, tuple): args = (args,) if len(args) == 1 and isinstance(args[0], afnumpy.ndarray) and args[0].dtype == numpy.dtype('bool'): # Special case for boolean getitem return self.flat[afnumpy.where(args[0].flat)] idx, new_shape = indexing.__convert_dim__(self.shape, args) if any(x is None for x in idx): # one of the indices is empty return ndarray(indexing.__index_shape__(self.shape, idx), dtype=self.dtype) idx = tuple(idx) if len(idx) == 0: idx = tuple([0]) s = self.d_array[idx] shape = pu.af_shape(s) array = ndarray(shape, dtype=self.dtype, af_array=s) if(shape != new_shape): array = array.reshape(new_shape) if new_shape == () and Ellipsis not in args: # Return the actual scalar return numpy.array(array)[()] return array