Diff of /PDSim/misc/datatypes.pyx [80bcaf] .. [83bb88]  Maximize  Restore

Switch to unified view

a/PDSim/misc/datatypes.pyx b/PDSim/misc/datatypes.pyx
...
...
44
            #If a numpy array use the buffering interface
44
            #If a numpy array use the buffering interface
45
            elif isinstance(data, np.ndarray): 
45
            elif isinstance(data, np.ndarray): 
46
                npdata = data
46
                npdata = data
47
                for i in range(self.N):
47
                for i in range(self.N):
48
                    self.data[i] = npdata[i]
48
                    self.data[i] = npdata[i]
49
            #If it is an array.array, use the buffer interface
49
            else:
50
            #elif isinstance(data, array.array):
50
                #Now it must either be an iterable or a failure
51
            #    vdata = data
51
                try:
52
            #    self.data[:] = vdata
53
            elif isinstance(data, list):
54
                for i,el in enumerate(data):
52
                    for i,el in enumerate(data):
55
                    self.data[i] = el
53
                        self.data[i] = el
54
                except TypeError:
55
                    raise TypeError("Sorry but you provided a type to arraym that doesn't work.  Good types are arraym, numpy arrays, or any iterable.")
56
        else:
56
        else:
57
            self.data = NULL
57
            self.data = NULL
58
            
58
            
59
    cpdef set_size(self, int N):
59
    cpdef set_size(self, int N):
60
        """
60
        """
61
        Set the size of the internal array, initialized to zeros
61
        Set the size of the internal array, initialized to zeros
62
        """
62
        """
63
        #If a zero or negative length passed in, don't allocate memory, but set length flag 
64
        if N <= 0:
65
            self.N = 0
66
            return
67
        
63
        if self.data is NULL:
68
        if self.data is NULL:
64
            #Allocate the memory for the array that will be used internally
69
            #Allocate the memory for the array that will be used internally
65
            self.data = <double *> calloc(N, sizeof(double))
70
            self.data = <double *> calloc(N, sizeof(double))
66
            self.N = N
71
            self.N = N
67
        else:
72
        else:
...
...
104
            if isarray_y:
109
            if isarray_y:
105
                x,y = y,x
110
                x,y = y,x
106
            N = (<arraym>x).N
111
            N = (<arraym>x).N
107
            z = (<arraym>x).copy()
112
            z = (<arraym>x).copy()
108
            zdata = (<arraym>z).data
113
            zdata = (<arraym>z).data
114
            
115
            try:
116
                #Try to make an iterator out of y
117
                iterator = iter(y)
118
            except TypeError:
119
                # not iterable - int, float, etc.
120
                
109
            # Cast to a double
121
                # Cast y to a double
110
            yd = (<double>y)
122
                yd = (<double>y)
111
            # Add on the other array values
123
                # Add on the other array values
112
            for i in range(N):
124
                for i in range(N):
113
                zdata[i] += yd
125
                    zdata[i] += yd
126
            else:
127
                # iterable - list, tuple, numpy array, etc.
128
                for i in range(N):
129
                    zdata[i] += y[i]
114
        
130
        
115
        return z
131
        return z
116
    
132
    
117
    def __mul__(x, y):
133
    def __mul__(x, y):
118
        cdef int i, N
134
        cdef int i, N
...
...
136
            if isarray_y:
152
            if isarray_y:
137
                x,y = y,x
153
                x,y = y,x
138
            N = (<arraym>x).N
154
            N = (<arraym>x).N
139
            z = (<arraym>x).copy()
155
            z = (<arraym>x).copy()
140
            zdata = (<arraym>z).data
156
            zdata = (<arraym>z).data
157
            
158
            try:
159
                #Try to make an iterator out of y
160
                iterator = iter(y)
161
            except TypeError:
162
                # not iterable - int, float, etc.
163
                
141
            # Cast to a double
164
                # Cast to a double
142
            yd = (<double>y)
165
                yd = (<double>y)
143
            # Add on the other array values
166
                # Multiply by the other value
144
            for i in range(N):
167
                for i in range(N):
145
                zdata[i] *= yd
168
                    zdata[i] *= yd
169
            else:
170
                # iterable - list, tuple, numpy array, etc.
171
                #No type introspection possible
172
                for i in range(N):
173
                    zdata[i] *= y[i]
146
            
174
            
147
        return z
175
        return z
148
    
176
    
149
    def __truediv__(x, y):
177
    def __truediv__(x, y):
150
        cdef int i, N
178
        cdef int i, N
...
...
154
        cdef arraym z
182
        cdef arraym z
155
        
183
        
156
        isarray_x = isinstance(x, arraym)
184
        isarray_x = isinstance(x, arraym)
157
        isarray_y = isinstance(y, arraym)
185
        isarray_y = isinstance(y, arraym)
158
186
187
        #Both x and y are arraym instances
159
        if isarray_x & isarray_y:
188
        if isarray_x & isarray_y:
160
            check_dims(x, y)
189
            check_dims(x, y)
161
            N = (<arraym>x).N
190
            N = (<arraym>x).N
162
            z = (<arraym>x).copy()
191
            z = (<arraym>x).copy()
163
            zdata = (<arraym>z).data
192
            zdata = (<arraym>z).data
164
            ydata = (<arraym>y).data
193
            ydata = (<arraym>y).data
165
            # Add on the other array values
194
            # Add on the other array values
166
            for i in range(N):
195
            for i in range(N):
167
                zdata[i] /= ydata[i]
196
                zdata[i] /= ydata[i]
197
                
198
        #One of x and y is an arraym
168
        elif isarray_x != isarray_y:
199
        elif isarray_x != isarray_y:
169
            if isarray_y:
200
            if isarray_y:
170
                N = (<arraym>y).N
201
                N = (<arraym>y).N
171
                z = (<arraym>y).copy()
202
                z = (<arraym>y).copy()
172
                zdata = (<arraym>z).data
203
                zdata = (<arraym>z).data
204
                if isinstance(x,(int,float)):
173
                 # Cast lhs to a double and rhs to a double*
205
                    # Cast lhs to a double and rhs to a double*
174
                xd = (<double>x)
206
                    xd = (<double>x)
175
                ydata = (<arraym>y).data
207
                    ydata = (<arraym>y).data
176
                # Add on the other array values
208
                    # Add on the other array values
177
                for i in range(N):
209
                    for i in range(N):
178
                    zdata[i] = xd/ydata[i]
210
                        zdata[i] = xd/ydata[i]
211
                else:
212
                    #Hopefully it is an iterable
213
                    for i in range(len(x)):
214
                        z[i] = x[i]/y[i]                    
179
            else:
215
            else:
180
                N = (<arraym>x).N
216
                N = (<arraym>x).N
181
                z = (<arraym>x).copy()
217
                z = (<arraym>x).copy()
182
                zdata = (<arraym>z).data
218
                zdata = (<arraym>z).data
219
                if isinstance(y,(int,float)):
183
                 # Cast rhs to a double
220
                    # Cast rhs to a double
184
                yd = <double> y
221
                    yd = <double> y
185
                # Add on the other array values
222
                    # Add on the other array values
186
                for i in range(N):
223
                    for i in range(N):
187
                    zdata[i] /= yd
224
                        zdata[i] /= yd
225
                else:
226
                    #Hopefully it is an iterable
227
                    for i in range(len(x)):
228
                        z[i] = x[i]/y[i]
188
            
229
                    
189
        return z
230
        return z
190
    
231
    
191
    def __sub__(x, y):
232
    def __sub__(x, y):
192
        cdef int i, N
233
        cdef int i, N
193
        cdef bint isarray_x, isarray_y
234
        cdef bint isarray_x, isarray_y
...
...
205
            zdata = (<arraym>z).data
246
            zdata = (<arraym>z).data
206
            ydata = (<arraym>y).data
247
            ydata = (<arraym>y).data
207
            # Add on the other array values
248
            # Add on the other array values
208
            for i in range(N):
249
            for i in range(N):
209
                zdata[i] -= ydata[i]
250
                zdata[i] -= ydata[i]
251
                
252
        #One of x and y is an arraym
210
        elif isarray_x != isarray_y:
253
        elif isarray_x != isarray_y:
211
            if isarray_y:
254
            if isarray_y:
212
                N = (<arraym>y).N
255
                N = (<arraym>y).N
213
                z = (<arraym>y).copy()
256
                z = (<arraym>y).copy()
214
                zdata = (<arraym>z).data
257
                zdata = (<arraym>z).data
258
                if isinstance(x,(int,float)):
215
                 # Cast lhs to a double and rhs to a double*
259
                    # Cast lhs to a double and rhs to a double*
216
                xd = (<double>x)
260
                    xd = (<double>x)
217
                ydata = (<arraym>y).data
261
                    ydata = (<arraym>y).data
218
                # Add on the other array values
262
                    # Add on the other array values
219
                for i in range(N):
263
                    for i in range(N):
220
                    zdata[i] = xd - ydata[i]
264
                        zdata[i] = xd - ydata[i]
265
                else:
266
                    #Hopefully it is an iterable
267
                    for i in range(len(x)):
268
                        z[i] = x[i] - y[i]                    
221
            else:
269
            else:
222
                N = (<arraym>x).N
270
                N = (<arraym>x).N
223
                z = (<arraym>x).copy()
271
                z = (<arraym>x).copy()
224
                zdata = (<arraym>z).data
272
                zdata = (<arraym>z).data
273
                if isinstance(y,(int,float)):
225
                 # Cast rhs to a double
274
                    # Cast rhs to a double
226
                yd = <double> y
275
                    yd = <double> y
227
                # Add on the other array values
276
                    # Add on the other array values
228
                for i in range(N):
277
                    for i in range(N):
229
                    zdata[i] -= yd
278
                        zdata[i] -= yd
279
                else:
280
                    #Hopefully it is an iterable
281
                    for i in range(len(x)):
282
                        z[i] = x[i] - y[i]
230
            
283
            
231
        return z
284
        return z
232
        
285
        
233
    cpdef arraym copy(self):
286
    cpdef arraym copy(self):
234
        cdef arraym arr = arraym()
287
        cdef arraym arr = arraym()
...
...
248
        if j < i:
301
        if j < i:
249
            raise IndexError('Indices must be increasing')
302
            raise IndexError('Indices must be increasing')
250
        if j == i:
303
        if j == i:
251
            raise IndexError('Length of slice must be greater than 1')
304
            raise IndexError('Length of slice must be greater than 1')
252
        if j > self.N:
305
        if j > self.N:
253
            raise IndexError('End of slice out of bounds. Length of arraym is '+str(self.N))
306
            raise IndexError('End of slice out of bounds. Length of arraym is '+str(self.N)+' requested end is '+str(j))
254
        
307
        
255
        arr.set_size(j-i)
308
        arr.set_size(j-i)
256
        memcpy(arr.data,self.data+i,(j-i)*sizeof(double))
309
        memcpy(arr.data,self.data+i,(j-i)*sizeof(double))
257
        return arr
310
        return arr
258
    
311
    
259
    cpdef extend(self, arraym array2):
312
    cpdef extend(self, arraym array2):
260
        cdef double* new_data
313
        cdef double* new_data
261
        cdef int N = array2.N + self.N
314
        cdef int N = array2.N + self.N
315
        
316
        #Only extend if there is something in the extension array
317
        if N > self.N:
262
        #Reallocate the array to extend its length
318
            #Reallocate the array to extend its length
263
        new_data = <double*>realloc(self.data, N*sizeof(double))
319
            new_data = <double*>realloc(self.data, N*sizeof(double))
264
        #Copy into the new array
320
            #Copy into the new array
265
        memcpy(new_data+self.N, array2.data, array2.N*sizeof(double))
321
            memcpy(new_data+self.N, array2.data, array2.N*sizeof(double))
266
        #Free the old array
322
            #Free the old array
267
        free(self.data)
323
            free(self.data)
268
        #Make self.data point to the newly allocated array
324
            #Make self.data point to the newly allocated array
269
        self.data = new_data
325
            self.data = new_data
270
        #Set the length
326
            #Set the length
271
        self.N = N
327
            self.N = N
272
        
328
        
273
    def __getslice__(self, Py_ssize_t i, Py_ssize_t j):
329
    def __getslice__(self, Py_ssize_t i, Py_ssize_t j):
274
        return self.slice(i,j)
330
        return self.slice(i,j)
275
    
331
    
276
    def __iter__(self):
332
    def __iter__(self):
...
...
280
    def __repr__(self):
336
    def __repr__(self):
281
        return str(list(self))
337
        return str(list(self))
282
        
338
        
283
    def __len__(self):
339
    def __len__(self):
284
        return self.N
340
        return self.N
341
342
cpdef arraym empty_arraym(int N):
343
    """
344
    A convenience function to return an arraym with the given size initialized to zero
345
    
346
    Parameters
347
    ----------
348
    N: int
349
        Size of the arraym to return initialized to zero
350
    """
351
    cdef arraym arr = arraym()
352
    arr.set_size(N)
353
    return arr
354
355
356
357
358
359
###############################################################################
360
###############################################################################
361
##          LISTM - Enhanced list with element-wise operators                ##
362
###############################################################################
363
###############################################################################
364
365
366
cdef class listm(list):
367
    """
368
    See http://docs.cython.org/src/userguide/special_methods.html
369
    """
370
    def __add__(self,y):
371
        cdef int i,N
372
        cdef bool isarray_x,isarray_y
373
        
374
        isarray_x = isinstance(self,listm)
375
        isarray_y = isinstance(y,listm)
376
        
377
        if isinstance(self,listm):
378
            N=len(self)
379
            if isinstance(y,int) or isinstance(y,float):
380
                return listm([self[i]*y for i in range(N)])
381
            else:
382
                return listm([self[i]*y[i] for i in range(N)])
383
        else:
384
            ### it is backwards, self is something else, y is a listm
385
            N=len(y)
386
            if isinstance(self,(int,float)) and not isinstance(y,(int,float)):
387
                self,y = y,self
388
                return listm([y[i]+self for i in range(N)])
389
            else: 
390
                return listm([self[i]+y[i] for i in range(N)])
391
                
392
    def __mul__(self,y):
393
        cdef int i,N
394
        
395
        if isinstance(self,listm):
396
            N=len(self)
397
            if isinstance(y,int) or isinstance(y,float):
398
                return listm([self[i]*y for i in range(N)])
399
            else:
400
                return listm([self[i]*y[i] for i in range(N)])
401
        else:
402
            ### it is backwards, self is something else, y is a listm
403
            N=len(y)
404
            if isinstance(self,int) or isinstance(self,float):
405
                return listm([y[i]*self for i in range(N)])
406
            else:
407
                return listm([self[i]*y[i] for i in range(N)])
408
                
409
    def __truediv__(self,y):
410
        cdef int i,N
411
        
412
        if isinstance(self,listm):
413
            N=len(self)
414
            if isinstance(y,int) or isinstance(y,float):
415
                return listm([self[i]/y for i in range(N)])
416
            else:
417
                return listm([self[i]/y[i] for i in range(N)])
418
        else:
419
            ### it is backwards, self is something else, y is a listm
420
            N=len(y)
421
            if isinstance(self,int) or isinstance(self,float):
422
                return listm([self/y[i] for i in range(N)])
423
            else:
424
                return listm([self[i]/y[i] for i in range(N)])
425
    
426
    def __sub__(self,y):
427
        cdef int i,N
428
        
429
        if isinstance(self,listm):
430
            N=len(self)
431
            if isinstance(y,int) or isinstance(y,float):
432
                return listm([self[i]-y for i in range(N)])
433
            else:
434
                return listm([self[i]-y[i] for i in range(N)])
435
        else:
436
            ### it is backwards, self is something else, y is a listm
437
            N=len(y)
438
            if isinstance(self,int) or isinstance(self,float):
439
                return listm([self-y[i] for i in range(N)])
440
            else:
441
                return listm([self[i]-y[i] for i in range(N)])
442
    
443
    def __reduce__(self):
444
        d={}
445
        d['data']=list(self)
446
        return rebuildListm,(d,)
447
    
448
    def copy(self):
449
        """
450
        Return a copy of the listm instance
451
        """
452
        return listm(self[:])
453
          
454
def rebuildListm(d):
455
    return listm(d['data'])

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks