From: Jochen <jo...@un...> - 2002-03-20 22:13:42
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 This just came on the numpy list. Might be interesting for pygsl as an alternativ to extract_tool. On Wed, 20 Mar 2002 13:11:12 -0800 Paul Dubois wrote: Paul> A beta version of Pyfort 7.0 is available at pyfortran.sf.net. The Paul> documentation is not yet upgraded to this version. Paul> Pyfort 7.0 adds the ability to wrap Fortran-like C coding to extend Numpy. Paul> Dramatically illustrating the virtue of open source software, credit for Paul> this improvement goes to: Paul> Michiel de Hoon Paul> Human Genome Center Paul> University of Tokyo Paul> For example, if you have this C code: Paul> double mydot(int n, double* x, double *y) { Paul> int i; Paul> double d; Paul> d = 0.0; Paul> for (i=0; i < n; ++i) d += x[i] * y[i]; Paul> return d; Paul> } Paul> Then you can create a Pyfort input file mymod.pyf: Paul> function mydot (n, x, y) Paul> integer:: n=size(x) Paul> doubleprecision x(n), y(n) Paul> doubleprecision mydot Paul> end Paul> Compile mydot.c into a library libmydot.a. Paul> Then: Paul> pyfort -c cc -i -L. -l mydot mymod.pyf Paul> builds and installs the module mymod containing function mydot, which you Paul> can use from Python: Paul> import Numeric, mymod Paul> x=Numeric.array([1.,2.3.]) Paul> y=Numeric.array([5., -1., -1.]) Paul> print mymod.mydot(x,y) Paul> Note that by wrapping mydot in this way, Pyfort takes care of problems like Paul> converting input arrays of the wrong type, such as integer; making sure Paul> that x and y have the same length; and making sure x and y are contiguous. Paul> I added directory testc that contains an example like this and one where an Paul> array is output. Paul> Mr. de Hoon explained his patch as follows. Paul> "I have modified fortran_compiler.py to add gcc as a Paul> compiler. This enables pyfort to be used for C code Paul> instead of Fortran code only. To use this option, call Paul> pyfort with the -cgcc option to specify gcc as the Paul> compiler. In order to switch off the default TRANSPOSE Paul> and MIRROR options, some small modifications were Paul> needed in generator.py also. [Editor's note: both -c gcc and -c cc will Paul> work] Paul> Before writing this addition to pyfort, I tried to use Paul> swig to generate the wrapper for my C code. However, Paul> pyfort was easier to use in my case because it is Paul> integrated with numpy. I wasn't able to get swig use Paul> numpy arrays. In addition, I am writing extension code Paul> both in fortran and C, so it is easier having to use Paul> only one tool (pyfort) for both. Paul> In a sense, it is easier to extend python with C than Paul> with fortran because you don't have to worry about Paul> transposing the array. I tried to be minimally Paul> instrusive on the existing pyfort code to switch off Paul> transposing arrays; there may be prettier ways to do Paul> this than what I have done. With this modification, I Paul> was able to pass one- and two-dimensional numpy arrays Paul> from Python to C and back without problems, as well as Paul> scalar variables with intent(in) and intent(out). I Paul> have also used the modified Pyfort on some Fortran Paul> routines to make sure I didn't break something in the Paul> fortran part of Pyfort. Paul> I haven't done an extensive test of this addition, but Paul> I haven't found any problems with it so far. I hope Paul> this patch will be useful to other people trying to Paul> extend Python/numpy with C routines." Paul> Michiel de Hoon Paul> Human Genome Center Paul> University of Tokyo Paul> md...@im... Greetings, Jochen - -- University of North Carolina phone: +1-919-962-4403 Department of Chemistry phone: +1-919-962-1579 Venable Hall CB#3290 (Kenan C148) fax: +1-919-843-6041 Chapel Hill, NC 27599, USA GnuPG key: 44BCCD8E -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6-cygwin-fcn-1 (Cygwin) Comment: Processed by Mailcrypt and GnuPG <http://www.gnupg.org/> iEYEARECAAYFAjyZCOgACgkQiJ/aUUS8zY6h+ACcCSjAWx1zUf5Pi/dzY3cST2tr ZYYAoKit0wdxSJaNmSoMujIxJ5LfYztx =+zeR -----END PGP SIGNATURE----- |