user_array.py 7.5 KB

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