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