user_array.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. """
  2. Standard container-class for easy multiple-inheritance.
  3. Try to inherit from the ndarray instead of using this class as this is not
  4. complete.
  5. """
  6. from __future__ import division, absolute_import, print_function
  7. from numpy.core import (
  8. array, asarray, absolute, add, subtract, multiply, divide,
  9. remainder, power, left_shift, right_shift, bitwise_and, bitwise_or,
  10. bitwise_xor, invert, less, less_equal, not_equal, equal, greater,
  11. greater_equal, shape, reshape, arange, sin, sqrt, transpose
  12. )
  13. from numpy.compat import long
  14. class container(object):
  15. """
  16. container(data, dtype=None, copy=True)
  17. Standard container-class for easy multiple-inheritance.
  18. Methods
  19. -------
  20. copy
  21. tostring
  22. byteswap
  23. astype
  24. """
  25. def __init__(self, data, dtype=None, copy=True):
  26. self.array = array(data, dtype, copy=copy)
  27. def __repr__(self):
  28. if self.ndim > 0:
  29. return self.__class__.__name__ + repr(self.array)[len("array"):]
  30. else:
  31. return self.__class__.__name__ + "(" + repr(self.array) + ")"
  32. def __array__(self, t=None):
  33. if t:
  34. return self.array.astype(t)
  35. return self.array
  36. # Array as sequence
  37. def __len__(self):
  38. return len(self.array)
  39. def __getitem__(self, index):
  40. return self._rc(self.array[index])
  41. def __setitem__(self, index, value):
  42. self.array[index] = asarray(value, self.dtype)
  43. def __abs__(self):
  44. return self._rc(absolute(self.array))
  45. def __neg__(self):
  46. return self._rc(-self.array)
  47. def __add__(self, other):
  48. return self._rc(self.array + asarray(other))
  49. __radd__ = __add__
  50. def __iadd__(self, other):
  51. add(self.array, other, self.array)
  52. return self
  53. def __sub__(self, other):
  54. return self._rc(self.array - asarray(other))
  55. def __rsub__(self, other):
  56. return self._rc(asarray(other) - self.array)
  57. def __isub__(self, other):
  58. subtract(self.array, other, self.array)
  59. return self
  60. def __mul__(self, other):
  61. return self._rc(multiply(self.array, asarray(other)))
  62. __rmul__ = __mul__
  63. def __imul__(self, other):
  64. multiply(self.array, other, self.array)
  65. return self
  66. def __div__(self, other):
  67. return self._rc(divide(self.array, asarray(other)))
  68. def __rdiv__(self, other):
  69. return self._rc(divide(asarray(other), self.array))
  70. def __idiv__(self, other):
  71. divide(self.array, other, self.array)
  72. return self
  73. def __mod__(self, other):
  74. return self._rc(remainder(self.array, other))
  75. def __rmod__(self, other):
  76. return self._rc(remainder(other, self.array))
  77. def __imod__(self, other):
  78. remainder(self.array, other, self.array)
  79. return self
  80. def __divmod__(self, other):
  81. return (self._rc(divide(self.array, other)),
  82. self._rc(remainder(self.array, other)))
  83. def __rdivmod__(self, other):
  84. return (self._rc(divide(other, self.array)),
  85. self._rc(remainder(other, self.array)))
  86. def __pow__(self, other):
  87. return self._rc(power(self.array, asarray(other)))
  88. def __rpow__(self, other):
  89. return self._rc(power(asarray(other), self.array))
  90. def __ipow__(self, other):
  91. power(self.array, other, self.array)
  92. return self
  93. def __lshift__(self, other):
  94. return self._rc(left_shift(self.array, other))
  95. def __rshift__(self, other):
  96. return self._rc(right_shift(self.array, other))
  97. def __rlshift__(self, other):
  98. return self._rc(left_shift(other, self.array))
  99. def __rrshift__(self, other):
  100. return self._rc(right_shift(other, self.array))
  101. def __ilshift__(self, other):
  102. left_shift(self.array, other, self.array)
  103. return self
  104. def __irshift__(self, other):
  105. right_shift(self.array, other, self.array)
  106. return self
  107. def __and__(self, other):
  108. return self._rc(bitwise_and(self.array, other))
  109. def __rand__(self, other):
  110. return self._rc(bitwise_and(other, self.array))
  111. def __iand__(self, other):
  112. bitwise_and(self.array, other, self.array)
  113. return self
  114. def __xor__(self, other):
  115. return self._rc(bitwise_xor(self.array, other))
  116. def __rxor__(self, other):
  117. return self._rc(bitwise_xor(other, self.array))
  118. def __ixor__(self, other):
  119. bitwise_xor(self.array, other, self.array)
  120. return self
  121. def __or__(self, other):
  122. return self._rc(bitwise_or(self.array, other))
  123. def __ror__(self, other):
  124. return self._rc(bitwise_or(other, self.array))
  125. def __ior__(self, other):
  126. bitwise_or(self.array, other, self.array)
  127. return self
  128. def __pos__(self):
  129. return self._rc(self.array)
  130. def __invert__(self):
  131. return self._rc(invert(self.array))
  132. def _scalarfunc(self, func):
  133. if self.ndim == 0:
  134. return func(self[0])
  135. else:
  136. raise TypeError(
  137. "only rank-0 arrays can be converted to Python scalars.")
  138. def __complex__(self):
  139. return self._scalarfunc(complex)
  140. def __float__(self):
  141. return self._scalarfunc(float)
  142. def __int__(self):
  143. return self._scalarfunc(int)
  144. def __long__(self):
  145. return self._scalarfunc(long)
  146. def __hex__(self):
  147. return self._scalarfunc(hex)
  148. def __oct__(self):
  149. return self._scalarfunc(oct)
  150. def __lt__(self, other):
  151. return self._rc(less(self.array, other))
  152. def __le__(self, other):
  153. return self._rc(less_equal(self.array, other))
  154. def __eq__(self, other):
  155. return self._rc(equal(self.array, other))
  156. def __ne__(self, other):
  157. return self._rc(not_equal(self.array, other))
  158. def __gt__(self, other):
  159. return self._rc(greater(self.array, other))
  160. def __ge__(self, other):
  161. return self._rc(greater_equal(self.array, other))
  162. def copy(self):
  163. ""
  164. return self._rc(self.array.copy())
  165. def tostring(self):
  166. ""
  167. return self.array.tostring()
  168. def byteswap(self):
  169. ""
  170. return self._rc(self.array.byteswap())
  171. def astype(self, typecode):
  172. ""
  173. return self._rc(self.array.astype(typecode))
  174. def _rc(self, a):
  175. if len(shape(a)) == 0:
  176. return a
  177. else:
  178. return self.__class__(a)
  179. def __array_wrap__(self, *args):
  180. return self.__class__(args[0])
  181. def __setattr__(self, attr, value):
  182. if attr == 'array':
  183. object.__setattr__(self, attr, value)
  184. return
  185. try:
  186. self.array.__setattr__(attr, value)
  187. except AttributeError:
  188. object.__setattr__(self, attr, value)
  189. # Only called after other approaches fail.
  190. def __getattr__(self, attr):
  191. if (attr == 'array'):
  192. return object.__getattribute__(self, attr)
  193. return self.array.__getattribute__(attr)
  194. #############################################################
  195. # Test of class container
  196. #############################################################
  197. if __name__ == '__main__':
  198. temp = reshape(arange(10000), (100, 100))
  199. ua = container(temp)
  200. # new object created begin test
  201. print(dir(ua))
  202. print(shape(ua), ua.shape) # I have changed Numeric.py
  203. ua_small = ua[:3, :5]
  204. print(ua_small)
  205. # this did not change ua[0,0], which is not normal behavior
  206. ua_small[0, 0] = 10
  207. print(ua_small[0, 0], ua[0, 0])
  208. print(sin(ua_small) / 3. * 6. + sqrt(ua_small ** 2))
  209. print(less(ua_small, 103), type(less(ua_small, 103)))
  210. print(type(ua_small * reshape(arange(15), shape(ua_small))))
  211. print(reshape(ua_small, (5, 3)))
  212. print(transpose(ua_small))