[Numpy-discussion] Matlab -> NumPy translation and indexing

Sturla Molden sturla@molden...
Wed Mar 14 10:11:43 CDT 2007


On 3/14/2007 2:46 PM, Robert Cimrman wrote:

> a = []
> while ...
>  a.append( scalar )
> a = array( a )


While it may help, growing Python lists is also an O(N) process.

One can reduce the amount of allocations by preallocating an ndarray of 
a certain size (e.g. 1024 scalars), filling it up, and storing it in a 
linked list. Finally, the stored arrays are retrieved as a single 
contiguous array. Example code below (cf. class scalar_list).


Sturla Molden




import numpy

class ndarray_list:

     """ a single linked list of numpy ndarrays."""

     class node:
         def __init__(self, data):
             self.next = None
             self.data = data

     def __init__(self):
         self.head = None
         self.tail = None
         self.len = 0

     def append(self, data):
         tmp = self.node(data)
         if self.tail == None:
             self.tail = tmp
             self.head = tmp
             self.len = len(data)
         else:
             self.tail.next = tmp
             self.tail = tmp
             self.len += len(data)

     def length(self): return self.len

     def flatten(self, dtype=float):
         tmp = numpy.empty(self.len, dtype=dtype)
         cur = self.head
         idx0 = 0
         while cur:
             tmp[idx0:idx0+len(cur.data)] = cur.data
             idx0 += len(cur.data)
             cur = cur.next
         return tmp


class scalar_list:

     """ a single linked list of numpy scalars."""

     def __init__(self, size=1024, dtype=float):
         self.cur = 0
         self.size = size
         self.dtype = dtype
         self.arr = numpy.empty(size,dtype)
         self.arrlist = ndarray_list()

     def append(self, scalar):
         cur = self.cur
         self.arr[cur] = scalar
         self.cur += 1
         if self.cur == self.size:
             self.arrlist.append(self.arr)
             self.arr = numpy.empty(self.size,self.dtype)
             self.cur = 0

     def array(self):
         if self.cur: self.arrlist.append(self.arr[:self.cur])
         retval = self.arrlist.flatten(self.dtype)
         self.cur = 0
         self.arr = numpy.empty(self.size,self.dtype)
        	self.arrlist = ndarray_list()
         self.arrlist.append(retval.copy())
         return retval




More information about the Numpy-discussion mailing list