comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/lib/user_array.py @ 87:2a2c65a20a8b

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