473,480 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.coordinates
[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.

from numarray import array
p1 = [0,0,1]
p2 = [0,0,2]
p3 = [0,0,3]
a1 = array((p1,p2))
a1array([[0, 0, 1], [0, 0, 2]]) a2 = array((a1,p3))Traceback (most recent call last): File "<stdin>", line 1, in ?
File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/
site-packages/numarray/numarraycore.py", line 417, in array return
fromlist(sequence,type,shape)
File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/
site-packages/numarray/numarraycore.py", line 267, in fromlist
arr.fromlist(seq)
ValueError: Nested sequences with different lengths. temp = list(a1)
temp.append(p3)
a2 = array(temp)
a2array([[0, 0, 1],

[0, 0, 2],
[0, 0, 3]])
Mar 22 '06 #1
1 2037
Ivan Vinogradov wrote:
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.coordinates
[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.


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

class ArrayGrower(object):
def __init__(self,initsize=100):
self.memarray = zeros((initsize,3),Float)
self.npoints = 0
def add_point(self,point):
if self.npoints >= len(self.memarray):
oldlen = len(self.memarray)
newmemarray = zeros((oldlen*2,3),Float)
newmemarray[:oldlen] = self.memarray
self.memarray = newmemarray
self.memarray[self.npoints] = point
self.npoints += 1
def get_current_array(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_array() to get an array slice
of the proper size.
x = ArrayGrower(2)
x.add_point([0,1,2])
x.get_current_array() array([ [ 0., 1., 2.]]) x.add_point([0,0,0])
x.add_point([1,1,1])
x.get_current_array()

array([[ 0., 1., 2.],
[ 0., 0., 0.],
[ 1., 1., 1.]])
Carl Banks

Mar 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1960
by: Marco Bubke | last post by:
Hi Ok, I get a reproduceable seqmentation fault. Ok, fist the code: cdef NumArray array_to_double(NumArray array): # maybe here is memoty leak! cdef NumArray flat_array_obj flat_array_obj...
2
2931
by: Marc Schellens | last post by:
Following the NumPy documentation, I took over some C code, but run into an error. Does anybody have a suggestion? Thanks, marc gdlpython.cpp:225: `PyArray_Type' undeclared (first use this...
3
1583
by: SunX | last post by:
I tried to initialize a float point array by: import numarray xur = numarray.fromfunction(lambda x,y,z:x*y*z, (2, 2, 2)) but I ended up with an integer array even though x, y, and z are all...
6
2695
by: Gaubitzer Erwin | last post by:
Hi there I wrote a short program which reads scientific data from a file and stores its values in a Numeric array. At the same time it reads the names of its dimensions which are then in the...
6
2729
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
4
3117
by: Ben Champion | last post by:
I have installed python 2.3.3 on my windows machine have have ran several programs succesfully. I have also installed numarray 1.1 for my version of python and am now trying to create arrays using...
1
3199
by: Chris P. | last post by:
Hi. I have a very simple task to perform and I'm having a hard time doing it. Given an array called 'x' (created using the numarray library), is there a single command that rounds each of its...
7
3092
by: Fabian Wauthier | last post by:
Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: ...
0
1225
by: andrewfelch | last post by:
Below is the code to/from Boolean arrays and Unsigned integers. On my Pentium 4, functions such as "bitwise_and" are 32 times faster when run on 32-bit integers instead of the...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.