[SciPy-user] Calling C code (Newbie Question)

Tom Kornack tom at kornack.com
Mon Sep 27 22:58:33 CDT 2004


I am attempting to perform a Lomb Periodogram (using the Scargle 
method) on a rather large data set. It's a way of getting a power 
spectrum for non-uniformly sampled data. I have the program written in 
C and it works well. I have converted the program to pure Python and 
it's too slow. It's still slow when I convert the for loops into 
various Numeric constructs.

So I'm looking for a way to call a C program from python and pass big 
Numeric arrays or a similar format to and from it. (without, hopefully, 
copying them in memory). I tried weave, but I noticed that you can't 
weave in whole programs. I suppose you can't weave in anything with a 
def statement, right? I would deeply appreciate any guidance. I feel as 
though this must be a very common program for scientists and I did try 
to find explicit examples of embedded C code, but to no avail.

If you want to read on, see below.

Thank you very much.
Tom Kornack


If you would like to see just what kind of computation is being done, 
the C and python codes can be found here:


The main loops look like this, where om and time are arrays:

     for i in range(numf):
        s2[i] = sum( sin(2.*om[i]*time) )
        c2[i] = sum( cos(2.*om[i]*time) )

and I tried to speed things up by doing:

     s2 = sum( sin(2. * outerproduct(om,time) ), 1)
     c2 = sum( cos(2. * outerproduct(om,time) ), 1)

but found that was too memory intensive and still slow. So that's why I 
wanted to do something purely in C.

Fundamental Symmetries Lab, Princeton University
609-716-7259 (h), 609-933-2186 (m), 609-258-0702 (w), 609-258-1625 (f)
Thomas Kornack, 157 North Post Road, Princeton Junction, NJ 08550-5009

More information about the SciPy-user mailing list