~ubuntu-branches/ubuntu/feisty/python-numpy/feisty

« back to all changes in this revision

Viewing changes to numpy/lib/UserArray.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-07-12 10:00:24 UTC
  • Revision ID: james.westby@ubuntu.com-20060712100024-5lw9q2yczlisqcrt
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
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
 
4
complete.
 
5
"""
 
6
 
 
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
 
11
 
 
12
class UserArray(object):
 
13
    def __init__(self, data, dtype=None, copy=True):
 
14
        self.array = array(data, dtype, copy)
 
15
 
 
16
    def __repr__(self):
 
17
        if len(self.shape) > 0:
 
18
            return self.__class__.__name__+repr(self.array)[len("array"):]
 
19
        else:
 
20
            return self.__class__.__name__+"("+repr(self.array)+")"
 
21
 
 
22
    def __array__(self,t=None):
 
23
        if t: return self.array.astype(t)
 
24
        return self.array
 
25
 
 
26
    # Array as sequence
 
27
    def __len__(self): return len(self.array)
 
28
 
 
29
    def __getitem__(self, index):
 
30
        return self._rc(self.array[index])
 
31
 
 
32
    def __getslice__(self, i, j):
 
33
        return self._rc(self.array[i:j])
 
34
 
 
35
 
 
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)
 
40
 
 
41
    def __abs__(self):
 
42
        return self._rc(absolute(self.array))
 
43
    def __neg__(self):
 
44
        return self._rc(-self.array)
 
45
 
 
46
    def __add__(self, other):
 
47
        return self._rc(self.array+asarray(other))
 
48
    __radd__ = __add__
 
49
 
 
50
    def __iadd__(self, other):
 
51
        add(self.array, other, self.array)
 
52
        return self
 
53
 
 
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)
 
60
        return self
 
61
 
 
62
    def __mul__(self, other):
 
63
        return self._rc(multiply(self.array,asarray(other)))
 
64
    __rmul__ = __mul__
 
65
    def __imul__(self, other):
 
66
        multiply(self.array, other, self.array)
 
67
        return self
 
68
 
 
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)
 
75
        return self
 
76
 
 
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)
 
83
        return self
 
84
 
 
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)))
 
91
 
 
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)
 
98
        return self
 
99
 
 
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)
 
110
        return self
 
111
    def __irshift__(self,other):
 
112
        right_shift(self.array, other, self.array)
 
113
        return self
 
114
 
 
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)
 
121
        return self
 
122
 
 
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)
 
129
        return self
 
130
 
 
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)
 
137
        return self
 
138
 
 
139
    def __neg__(self):
 
140
        return self._rc(-self.array)
 
141
    def __pos__(self):
 
142
        return self._rc(self.array)
 
143
    def __abs__(self):
 
144
        return self._rc(abs(self.array))
 
145
    def __invert__(self):
 
146
        return self._rc(invert(self.array))
 
147
 
 
148
    def _scalarfunc(self, func):
 
149
        if len(self.shape) == 0:
 
150
            return func(self[0])
 
151
        else:
 
152
            raise TypeError, "only rank-0 arrays can be converted to Python scalars."
 
153
 
 
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)
 
160
 
 
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))
 
167
 
 
168
    def copy(self): return self._rc(self.array.copy())
 
169
 
 
170
    def tostring(self): return self.array.tostring()
 
171
 
 
172
    def byteswap(self): return self._rc(self.array.byteswap())
 
173
 
 
174
    def astype(self, typecode): return self._rc(self.array.astype(typecode))
 
175
 
 
176
    def _rc(self, a):
 
177
        if len(shape(a)) == 0: return a
 
178
        else: return self.__class__(a)
 
179
 
 
180
    def __array_wrap__(self, *args):
 
181
        return self.__class__(args[0])
 
182
 
 
183
    def __setattr__(self,attr,value):
 
184
        if attr == 'array':
 
185
            object.__setattr__(self, attr, value)
 
186
            return
 
187
        try:
 
188
            self.array.__setattr__(attr, value)
 
189
        except AttributeError:
 
190
            object.__setattr__(self, attr, value)
 
191
 
 
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)
 
197
 
 
198
#############################################################
 
199
# Test of class UserArray
 
200
#############################################################
 
201
if __name__ == '__main__':
 
202
    temp=reshape(arange(10000),(100,100))
 
203
 
 
204
    ua=UserArray(temp)
 
205
    # new object created begin test
 
206
    print dir(ua)
 
207
    print shape(ua),ua.shape # I have changed Numeric.py
 
208
 
 
209
    ua_small=ua[:3,:5]
 
210
    print ua_small
 
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)