Efficient storage of floats

Chris Barker chrishbarker at attbi.com
Thu Dec 27 20:10:07 CET 2001

Francois Petitjean wrote:
> I have to read a number of points coordinates (npoints). Different
> structures can be used to store these numbers:

> 2. The coordinates can be stored in an array
> acoords = array.array('f')

This is crying out to be done with Numeric Arrays

import Numeric:

coords = Numeric.zeros((3,npoints),Numeric.Float)

> Using acoords : x,y,z = acoords[3*kpt],acoords[3*kpt+1],acoords[3*kpt+2]

x,y,z = acoords(n)

The possible n-d arrays of Numeric make this kind of thing MUCH easier,
and you will get the efficient storage you want. Numeric arrays use the
same amont of storage a C array would use for the same type, plus the
overhead of the Array object, which would be negligable compared to the
data for a large array.

Asside for that, you now have a data an object type that supports
effiecent computation, wiht nice easy notation:

transformed_coords = acoords * 5

to multiply all the coordanates by 5, or:

transformed_coords = acoords * (x_scale, y_scale, z_scale)

Etc, etc, etc.

If you are working with a lot of numbers, you are crazy not to use


Christopher Barker,
ChrisHBarker at attbi.net                ---           ---           ---
                                     ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------

More information about the Python-list mailing list