[Numpy-discussion] [ANN] Pyfort-7.0b2 -- extending Numeric with C routines

Paul F Dubois paul at pfdubois.com
Sun Mar 24 08:49:05 CST 2002


Pyfort 7.0b2 is now available at sf.net/projects/pyfortran.

Nummies who do not use Fortran may be interested in using Pyfort; with
Michiel de Hoon's "C" option, it is now extremely easy to wrap a simple
kind of C code for processing Numeric's arrays, like this:

double ctry(int n, double* x, double* y)
{
    int i;
    double d;
    d = 0.0;
    for (i=0; i < n; ++i) {
        d += x[i] * y[i];
    }
    return d;
}

void cout(int n, double* x, double* y)
{
    int i;
    for (i = 0; i < n; ++i) {
        y[i] = 1.414159 * x[i];
    }
}

double c2(int n, int m, double x[n][m]) {
    double sum = 0.0;
    int i, j;
    for (i=0; i < n; i++) {
        for (j=0; j < m; j++) {
            sum += x[i][j];
        }
    }
    return sum;
}

and then call it from Python like this:

import testc, Numeric
x = Numeric.array([1.,2.,3.])
y = Numeric.array([6.,-1.,-1.])
z = Numeric.arange(6) *1.0
z.shape=(3,2)
print "Should be 1.0:", testc.ctry(x,y)
print "Should be sqrt(2) * [1,2,3]:", testc.cout(x)
print "Should be 15.0:", testc.c2(z)
z.shape=(2,3)
print "Should be 15.0:", testc.c2(z)

----------- notes
Somehow 7.0b1 was missing the C examples. I apparently lost the changes
I had made to the MANIFEST and did not realize my CVS commits had
failed. Bad day at the office, I guess. 

I added testc back in, and added a 2-dimensional example. I also
eliminated some warning errors in the generated code, and fixed an error
in that case. My thanks to Michiel de Hoon and J.S. Whitaker.





More information about the Numpy-discussion mailing list