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

Timothy Hochberg tim.hochberg@ieee....
Wed Mar 14 12:09:12 CDT 2007

```On 3/14/07, Sturla Molden <sturla@molden.no> wrote:
>
> 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.

This may just be a terminology problem, but just to be clear, appending to a
Python list is amortized (on average) constant time (O(1)), not O(N).

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.tail = None
>          self.len = 0
>
>      def append(self, data):
>          tmp = self.node(data)
>          if self.tail == None:
>              self.tail = 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)
>          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
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>

--

//=][=\\

tim.hochberg@ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070314/d6ecbf88/attachment-0001.html
```