2
Standard container-class for easy backward compatibility with Numeric.
3
Try to inherit from the ndarray instead of using this class as this is not
7
from numpy.core import array, asarray, absolute, add, subtract, multiply, \
8
divide, remainder, power, left_shift, right_shift, bitwise_and, \
9
bitwise_or, bitwise_xor, invert, less, less_equal, not_equal, equal, \
10
greater, greater_equal, shape, reshape, arange, sin, sqrt, transpose
12
class UserArray(object):
13
def __init__(self, data, dtype=None, copy=True):
14
self.array = array(data, dtype, copy)
17
if len(self.shape) > 0:
18
return self.__class__.__name__+repr(self.array)[len("array"):]
20
return self.__class__.__name__+"("+repr(self.array)+")"
22
def __array__(self,t=None):
23
if t: return self.array.astype(t)
27
def __len__(self): return len(self.array)
29
def __getitem__(self, index):
30
return self._rc(self.array[index])
32
def __getslice__(self, i, j):
33
return self._rc(self.array[i:j])
36
def __setitem__(self, index, value):
37
self.array[index] = asarray(value,self.dtype)
38
def __setslice__(self, i, j, value):
39
self.array[i:j] = asarray(value,self.dtype)
42
return self._rc(absolute(self.array))
44
return self._rc(-self.array)
46
def __add__(self, other):
47
return self._rc(self.array+asarray(other))
50
def __iadd__(self, other):
51
add(self.array, other, self.array)
54
def __sub__(self, other):
55
return self._rc(self.array-asarray(other))
56
def __rsub__(self, other):
57
return self._rc(asarray(other)-self.array)
58
def __isub__(self, other):
59
subtract(self.array, other, self.array)
62
def __mul__(self, other):
63
return self._rc(multiply(self.array,asarray(other)))
65
def __imul__(self, other):
66
multiply(self.array, other, self.array)
69
def __div__(self, other):
70
return self._rc(divide(self.array,asarray(other)))
71
def __rdiv__(self, other):
72
return self._rc(divide(asarray(other),self.array))
73
def __idiv__(self, other):
74
divide(self.array, other, self.array)
77
def __mod__(self, other):
78
return self._rc(remainder(self.array, other))
79
def __rmod__(self, other):
80
return self._rc(remainder(other, self.array))
81
def __imod__(self, other):
82
remainder(self.array, other, self.array)
85
def __divmod__(self, other):
86
return (self._rc(divide(self.array,other)),
87
self._rc(remainder(self.array, other)))
88
def __rdivmod__(self, other):
89
return (self._rc(divide(other, self.array)),
90
self._rc(remainder(other, self.array)))
92
def __pow__(self,other):
93
return self._rc(power(self.array,asarray(other)))
94
def __rpow__(self,other):
95
return self._rc(power(asarray(other),self.array))
96
def __ipow__(self,other):
97
power(self.array, other, self.array)
100
def __lshift__(self,other):
101
return self._rc(left_shift(self.array, other))
102
def __rshift__(self,other):
103
return self._rc(right_shift(self.array, other))
104
def __rlshift__(self,other):
105
return self._rc(left_shift(other, self.array))
106
def __rrshift__(self,other):
107
return self._rc(right_shift(other, self.array))
108
def __ilshift__(self,other):
109
left_shift(self.array, other, self.array)
111
def __irshift__(self,other):
112
right_shift(self.array, other, self.array)
115
def __and__(self, other):
116
return self._rc(bitwise_and(self.array, other))
117
def __rand__(self, other):
118
return self._rc(bitwise_and(other, self.array))
119
def __iand__(self, other):
120
bitwise_and(self.array, other, self.array)
123
def __xor__(self, other):
124
return self._rc(bitwise_xor(self.array, other))
125
def __rxor__(self, other):
126
return self._rc(bitwise_xor(other, self.array))
127
def __ixor__(self, other):
128
bitwise_xor(self.array, other, self.array)
131
def __or__(self, other):
132
return self._rc(bitwise_or(self.array, other))
133
def __ror__(self, other):
134
return self._rc(bitwise_or(other, self.array))
135
def __ior__(self, other):
136
bitwise_or(self.array, other, self.array)
140
return self._rc(-self.array)
142
return self._rc(self.array)
144
return self._rc(abs(self.array))
145
def __invert__(self):
146
return self._rc(invert(self.array))
148
def _scalarfunc(self, func):
149
if len(self.shape) == 0:
152
raise TypeError, "only rank-0 arrays can be converted to Python scalars."
154
def __complex__(self): return self._scalarfunc(complex)
155
def __float__(self): return self._scalarfunc(float)
156
def __int__(self): return self._scalarfunc(int)
157
def __long__(self): return self._scalarfunc(long)
158
def __hex__(self): return self._scalarfunc(hex)
159
def __oct__(self): return self._scalarfunc(oct)
161
def __lt__(self,other): return self._rc(less(self.array,other))
162
def __le__(self,other): return self._rc(less_equal(self.array,other))
163
def __eq__(self,other): return self._rc(equal(self.array,other))
164
def __ne__(self,other): return self._rc(not_equal(self.array,other))
165
def __gt__(self,other): return self._rc(greater(self.array,other))
166
def __ge__(self,other): return self._rc(greater_equal(self.array,other))
168
def copy(self): return self._rc(self.array.copy())
170
def tostring(self): return self.array.tostring()
172
def byteswap(self): return self._rc(self.array.byteswap())
174
def astype(self, typecode): return self._rc(self.array.astype(typecode))
177
if len(shape(a)) == 0: return a
178
else: return self.__class__(a)
180
def __array_wrap__(self, *args):
181
return self.__class__(args[0])
183
def __setattr__(self,attr,value):
185
object.__setattr__(self, attr, value)
188
self.array.__setattr__(attr, value)
189
except AttributeError:
190
object.__setattr__(self, attr, value)
192
# Only called after other approaches fail.
193
def __getattr__(self,attr):
194
if (attr == 'array'):
195
return object.__getattribute__(self, attr)
196
return self.array.__getattribute__(attr)
198
#############################################################
199
# Test of class UserArray
200
#############################################################
201
if __name__ == '__main__':
202
temp=reshape(arange(10000),(100,100))
205
# new object created begin test
207
print shape(ua),ua.shape # I have changed Numeric.py
211
ua_small[0,0]=10 # this did not change ua[0,0], which is not normal behavior
212
print ua_small[0,0],ua[0,0]
213
print sin(ua_small)/3.*6.+sqrt(ua_small**2)
214
print less(ua_small,103),type(less(ua_small,103))
215
print type(ua_small*reshape(arange(15),shape(ua_small)))
216
print reshape(ua_small,(5,3))
217
print transpose(ua_small)