Chris@87: """ Chris@87: Standard container-class for easy multiple-inheritance. Chris@87: Try to inherit from the ndarray instead of using this class as this is not Chris@87: complete. Chris@87: Chris@87: """ Chris@87: from __future__ import division, absolute_import, print_function Chris@87: Chris@87: from numpy.core import ( Chris@87: array, asarray, absolute, add, subtract, multiply, divide, Chris@87: remainder, power, left_shift, right_shift, bitwise_and, bitwise_or, Chris@87: bitwise_xor, invert, less, less_equal, not_equal, equal, greater, Chris@87: greater_equal, shape, reshape, arange, sin, sqrt, transpose Chris@87: ) Chris@87: from numpy.compat import long Chris@87: Chris@87: Chris@87: class container(object): Chris@87: Chris@87: def __init__(self, data, dtype=None, copy=True): Chris@87: self.array = array(data, dtype, copy=copy) Chris@87: Chris@87: def __repr__(self): Chris@87: if len(self.shape) > 0: Chris@87: return self.__class__.__name__ + repr(self.array)[len("array"):] Chris@87: else: Chris@87: return self.__class__.__name__ + "(" + repr(self.array) + ")" Chris@87: Chris@87: def __array__(self, t=None): Chris@87: if t: Chris@87: return self.array.astype(t) Chris@87: return self.array Chris@87: Chris@87: # Array as sequence Chris@87: def __len__(self): Chris@87: return len(self.array) Chris@87: Chris@87: def __getitem__(self, index): Chris@87: return self._rc(self.array[index]) Chris@87: Chris@87: def __getslice__(self, i, j): Chris@87: return self._rc(self.array[i:j]) Chris@87: Chris@87: def __setitem__(self, index, value): Chris@87: self.array[index] = asarray(value, self.dtype) Chris@87: Chris@87: def __setslice__(self, i, j, value): Chris@87: self.array[i:j] = asarray(value, self.dtype) Chris@87: Chris@87: def __abs__(self): Chris@87: return self._rc(absolute(self.array)) Chris@87: Chris@87: def __neg__(self): Chris@87: return self._rc(-self.array) Chris@87: Chris@87: def __add__(self, other): Chris@87: return self._rc(self.array + asarray(other)) Chris@87: Chris@87: __radd__ = __add__ Chris@87: Chris@87: def __iadd__(self, other): Chris@87: add(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __sub__(self, other): Chris@87: return self._rc(self.array - asarray(other)) Chris@87: Chris@87: def __rsub__(self, other): Chris@87: return self._rc(asarray(other) - self.array) Chris@87: Chris@87: def __isub__(self, other): Chris@87: subtract(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __mul__(self, other): Chris@87: return self._rc(multiply(self.array, asarray(other))) Chris@87: Chris@87: __rmul__ = __mul__ Chris@87: Chris@87: def __imul__(self, other): Chris@87: multiply(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __div__(self, other): Chris@87: return self._rc(divide(self.array, asarray(other))) Chris@87: Chris@87: def __rdiv__(self, other): Chris@87: return self._rc(divide(asarray(other), self.array)) Chris@87: Chris@87: def __idiv__(self, other): Chris@87: divide(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __mod__(self, other): Chris@87: return self._rc(remainder(self.array, other)) Chris@87: Chris@87: def __rmod__(self, other): Chris@87: return self._rc(remainder(other, self.array)) Chris@87: Chris@87: def __imod__(self, other): Chris@87: remainder(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __divmod__(self, other): Chris@87: return (self._rc(divide(self.array, other)), Chris@87: self._rc(remainder(self.array, other))) Chris@87: Chris@87: def __rdivmod__(self, other): Chris@87: return (self._rc(divide(other, self.array)), Chris@87: self._rc(remainder(other, self.array))) Chris@87: Chris@87: def __pow__(self, other): Chris@87: return self._rc(power(self.array, asarray(other))) Chris@87: Chris@87: def __rpow__(self, other): Chris@87: return self._rc(power(asarray(other), self.array)) Chris@87: Chris@87: def __ipow__(self, other): Chris@87: power(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __lshift__(self, other): Chris@87: return self._rc(left_shift(self.array, other)) Chris@87: Chris@87: def __rshift__(self, other): Chris@87: return self._rc(right_shift(self.array, other)) Chris@87: Chris@87: def __rlshift__(self, other): Chris@87: return self._rc(left_shift(other, self.array)) Chris@87: Chris@87: def __rrshift__(self, other): Chris@87: return self._rc(right_shift(other, self.array)) Chris@87: Chris@87: def __ilshift__(self, other): Chris@87: left_shift(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __irshift__(self, other): Chris@87: right_shift(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __and__(self, other): Chris@87: return self._rc(bitwise_and(self.array, other)) Chris@87: Chris@87: def __rand__(self, other): Chris@87: return self._rc(bitwise_and(other, self.array)) Chris@87: Chris@87: def __iand__(self, other): Chris@87: bitwise_and(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __xor__(self, other): Chris@87: return self._rc(bitwise_xor(self.array, other)) Chris@87: Chris@87: def __rxor__(self, other): Chris@87: return self._rc(bitwise_xor(other, self.array)) Chris@87: Chris@87: def __ixor__(self, other): Chris@87: bitwise_xor(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __or__(self, other): Chris@87: return self._rc(bitwise_or(self.array, other)) Chris@87: Chris@87: def __ror__(self, other): Chris@87: return self._rc(bitwise_or(other, self.array)) Chris@87: Chris@87: def __ior__(self, other): Chris@87: bitwise_or(self.array, other, self.array) Chris@87: return self Chris@87: Chris@87: def __pos__(self): Chris@87: return self._rc(self.array) Chris@87: Chris@87: def __invert__(self): Chris@87: return self._rc(invert(self.array)) Chris@87: Chris@87: def _scalarfunc(self, func): Chris@87: if len(self.shape) == 0: Chris@87: return func(self[0]) Chris@87: else: Chris@87: raise TypeError( Chris@87: "only rank-0 arrays can be converted to Python scalars.") Chris@87: Chris@87: def __complex__(self): Chris@87: return self._scalarfunc(complex) Chris@87: Chris@87: def __float__(self): Chris@87: return self._scalarfunc(float) Chris@87: Chris@87: def __int__(self): Chris@87: return self._scalarfunc(int) Chris@87: Chris@87: def __long__(self): Chris@87: return self._scalarfunc(long) Chris@87: Chris@87: def __hex__(self): Chris@87: return self._scalarfunc(hex) Chris@87: Chris@87: def __oct__(self): Chris@87: return self._scalarfunc(oct) Chris@87: Chris@87: def __lt__(self, other): Chris@87: return self._rc(less(self.array, other)) Chris@87: Chris@87: def __le__(self, other): Chris@87: return self._rc(less_equal(self.array, other)) Chris@87: Chris@87: def __eq__(self, other): Chris@87: return self._rc(equal(self.array, other)) Chris@87: Chris@87: def __ne__(self, other): Chris@87: return self._rc(not_equal(self.array, other)) Chris@87: Chris@87: def __gt__(self, other): Chris@87: return self._rc(greater(self.array, other)) Chris@87: Chris@87: def __ge__(self, other): Chris@87: return self._rc(greater_equal(self.array, other)) Chris@87: Chris@87: def copy(self): Chris@87: return self._rc(self.array.copy()) Chris@87: Chris@87: def tostring(self): Chris@87: return self.array.tostring() Chris@87: Chris@87: def byteswap(self): Chris@87: return self._rc(self.array.byteswap()) Chris@87: Chris@87: def astype(self, typecode): Chris@87: return self._rc(self.array.astype(typecode)) Chris@87: Chris@87: def _rc(self, a): Chris@87: if len(shape(a)) == 0: Chris@87: return a Chris@87: else: Chris@87: return self.__class__(a) Chris@87: Chris@87: def __array_wrap__(self, *args): Chris@87: return self.__class__(args[0]) Chris@87: Chris@87: def __setattr__(self, attr, value): Chris@87: if attr == 'array': Chris@87: object.__setattr__(self, attr, value) Chris@87: return Chris@87: try: Chris@87: self.array.__setattr__(attr, value) Chris@87: except AttributeError: Chris@87: object.__setattr__(self, attr, value) Chris@87: Chris@87: # Only called after other approaches fail. Chris@87: def __getattr__(self, attr): Chris@87: if (attr == 'array'): Chris@87: return object.__getattribute__(self, attr) Chris@87: return self.array.__getattribute__(attr) Chris@87: Chris@87: ############################################################# Chris@87: # Test of class container Chris@87: ############################################################# Chris@87: if __name__ == '__main__': Chris@87: temp = reshape(arange(10000), (100, 100)) Chris@87: Chris@87: ua = container(temp) Chris@87: # new object created begin test Chris@87: print(dir(ua)) Chris@87: print(shape(ua), ua.shape) # I have changed Numeric.py Chris@87: Chris@87: ua_small = ua[:3, :5] Chris@87: print(ua_small) Chris@87: # this did not change ua[0,0], which is not normal behavior Chris@87: ua_small[0, 0] = 10 Chris@87: print(ua_small[0, 0], ua[0, 0]) Chris@87: print(sin(ua_small) / 3. * 6. + sqrt(ua_small ** 2)) Chris@87: print(less(ua_small, 103), type(less(ua_small, 103))) Chris@87: print(type(ua_small * reshape(arange(15), shape(ua_small)))) Chris@87: print(reshape(ua_small, (5, 3))) Chris@87: print(transpose(ua_small))