Dynamically growing numarray array.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Vinogradov

    #1

    Dynamically growing numarray array.

    Hello All,

    this seems like a trivial problem, but I just can't find an elegant
    solution neither by myself, nor with google's help.

    I'd like to be able to keep an array representing coordinates for a
    system of points.
    Since I'd like to operate on each point's coordinates individually,
    for speed and ufuncs
    numarray fits the bill perfectly, especially since system.coordina tes
    [4] would return proper vector for a 5th point.
    To start, read the coordinates from a text file and add them to our
    array one by one.
    Here it gets un-elegant and probably wasteful for a large number of
    points, by converting the whole array to a list only to use append
    method and then convert it back to array(sample code below). Also,
    there is potential need to add more points later on.

    Any pointers would be greatly appreciated.

    [color=blue][color=green][color=darkred]
    >>> from numarray import array
    >>> p1 = [0,0,1]
    >>> p2 = [0,0,2]
    >>> p3 = [0,0,3]
    >>> a1 = array((p1,p2))
    >>> a1array([[0, 0, 1],[/color][/color][/color]
    [0, 0, 2]])[color=blue][color=green][color=darkred]
    >>> a2 = array((a1,p3))T raceback (most recent call last):[/color][/color][/color]
    File "<stdin>", line 1, in ?
    File "/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/
    site-packages/numarray/numarraycore.py ", line 417, in array return
    fromlist(sequen ce,type,shape)
    File "/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/
    site-packages/numarray/numarraycore.py ", line 267, in fromlist
    arr.fromlist(se q)
    ValueError: Nested sequences with different lengths.[color=blue][color=green][color=darkred]
    >>> temp = list(a1)
    >>> temp.append(p3)
    >>> a2 = array(temp)
    >>> a2array([[0, 0, 1],[/color][/color][/color]
    [0, 0, 2],
    [0, 0, 3]])
  • Carl Banks

    #2
    Re: Dynamically growing numarray array.

    Ivan Vinogradov wrote:[color=blue]
    > Hello All,
    >
    > this seems like a trivial problem, but I just can't find an elegant
    > solution neither by myself, nor with google's help.
    >
    > I'd like to be able to keep an array representing coordinates for a
    > system of points.
    > Since I'd like to operate on each point's coordinates individually,
    > for speed and ufuncs
    > numarray fits the bill perfectly, especially since system.coordina tes
    > [4] would return proper vector for a 5th point.
    > To start, read the coordinates from a text file and add them to our
    > array one by one.
    > Here it gets un-elegant and probably wasteful for a large number of
    > points, by converting the whole array to a list only to use append
    > method and then convert it back to array(sample code below). Also,
    > there is potential need to add more points later on.
    >
    > Any pointers would be greatly appreciated.[/color]

    Very simple thing to do, actually. The following class implements a
    very basic array grower for an array of 3-D points:

    class ArrayGrower(obj ect):
    def __init__(self,i nitsize=100):
    self.memarray = zeros((initsize ,3),Float)
    self.npoints = 0
    def add_point(self, point):
    if self.npoints >= len(self.memarr ay):
    oldlen = len(self.memarr ay)
    newmemarray = zeros((oldlen*2 ,3),Float)
    newmemarray[:oldlen] = self.memarray
    self.memarray = newmemarray
    self.memarray[self.npoints] = point
    self.npoints += 1
    def get_current_arr ay(self):
    return self.memarray[:self.npoints]

    It simply uses a slice of a larger array to hold all your points. It
    keeps track of the number of points and automatically grows the array
    when there's no room to add another point. (You can use a factor less
    than 2 if you have to conserve memory.) Whenever you need the current
    array for a calculation, use get_current_arr ay() to get an array slice
    of the proper size.
    [color=blue][color=green][color=darkred]
    >>> x = ArrayGrower(2)
    >>> x.add_point([0,1,2])
    >>> x.get_current_a rray()[/color][/color][/color]
    array([ [ 0., 1., 2.]])[color=blue][color=green][color=darkred]
    >>> x.add_point([0,0,0])
    >>> x.add_point([1,1,1])
    >>> x.get_current_a rray()[/color][/color][/color]
    array([[ 0., 1., 2.],
    [ 0., 0., 0.],
    [ 1., 1., 1.]])


    Carl Banks

    Comment

    Working...