{
"metadata": {
"signature": "sha256:627a3e6ccb2a6a4c222372198ad3e4d7a2417c1e1963f9606a53230a6bc3c907"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"F2py\n",
"======================================================================\n",
"\n",
"This page provides examples on how to use the [F2py](http://docs.scipy.org/doc/numpy-dev/f2py/) fortran wrapping\n",
"program.\n",
"\n",
"It is possible to start with simple routines or even to wrap full\n",
"Fortran modules. [F2py](http://docs.scipy.org/doc/numpy-dev/f2py/) is used in SciPy itself and you can find some\n",
"examples in the source code of SciPy.\n",
"\n",
"Short examples\n",
"--------------\n",
"\n",
"### Wrapping a function from lapack\n",
"\n",
"*Taken from a message on 2006-06-22 to scipy-user by ArndBaecker*\n",
"\n",
"Thanks to f2py, wrapping Fortran code is (with a bit of effort) trivial\n",
"in many cases. For complicated functions requiring many arguments the\n",
"wrapper can become longish. Fortunately, many things can be learnt from\n",
"looking at \\`\\`scipy/Lib/linalg/generic\\_flapack.pyf\\`\\` In particular,\n",
"the documentation at is excellent.\n",
"I also found the f2py notes by FernandoPerez very helpful,\n",
"\n",
"\n",
"Let me try to give some general remarks on how to start (the real\n",
"authority on all this is of course Pearu, so please correct me if I got\n",
"things wrong here): \\* first find a routine which will do the job you\n",
"want: \\* If the lapack documentation is installed properly on Linux you\n",
"could do \\* provides a nice decision tree \\*\n",
"make sure that that it does not exist in scipy:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" from scipy.lib import lapack\n",
" lapack.clapack. (assuming Ipython)\n",
" lapack.clapack."
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`\u00a0Remark:\u00a0routines\u00a0starting\u00a0with\u00a0c/z\u00a0are\u00a0for\u00a0double/single\u00a0complex`\\\n",
"`\u00a0and\u00a0routines\u00a0for\u00a0d/s\u00a0for\u00a0double/single\u00a0real\u00a0numbers.`\\\n",
"`\u00a0The\u00a0calling\u00a0sequence\u00a0for\u00a0c/z\u00a0and\u00a0d/s\u00a0are\u00a0(I\u00a0think\u00a0always)\u00a0the\u00a0same\u00a0and`\\\n",
"`\u00a0sometimes\u00a0they\u00a0are\u00a0also\u00a0the\u00a0same\u00a0for\u00a0the\u00a0real\u00a0and\u00a0complex\u00a0case.`\\\n",
"`*\u00a0Then\u00a0one\u00a0has\u00a0to\u00a0download\u00a0the\u00a0fortran\u00a0file\u00a0for\u00a0the\u00a0lapack\u00a0routine\u00a0of\u00a0interest.`\\\n",
"`*\u00a0Generate\u00a0wrapper\u00a0by\u00a0calling`\\\n",
"`\u00a0\u00a0\u00a0`\n",
"\n",
"`*\u00a0Generate\u00a0library`\\\n",
"`\u00a0\u00a0\u00a0`\n",
"\n",
"`*\u00a0You\u00a0can\u00a0use\u00a0this\u00a0by`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" import wrap_lap"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`\u00a0Note,\u00a0that\u00a0this\u00a0is\u00a0not\u00a0yet\u00a0polished\u00a0(this\u00a0is\u00a0the\u00a0part\u00a0on`\\\n",
"`\u00a0which\u00a0has\u00a0to\u00a0spent\u00a0some\u00a0effort\u00a0;-)\u00a0),\u00a0i.e.\u00a0one`\\\n",
"`\u00a0has\u00a0to\u00a0tell\u00a0which\u00a0variables\u00a0are\u00a0input,\u00a0which\u00a0are\u00a0output`\\\n",
"`\u00a0and\u00a0which\u00a0are\u00a0optional.\u00a0In\u00a0addition\u00a0temporary`\\\n",
"`\u00a0storage\u00a0has\u00a0to\u00a0be\u00a0provided\u00a0with\u00a0the\u00a0right\u00a0dimensions`\\\n",
"`\u00a0as\u00a0described\u00a0in\u00a0the\u00a0documentation\u00a0part\u00a0of\u00a0the\u00a0lapack\u00a0routine.`\n",
"\n",
"Concrete (and very simple) example (non-lapack):\n",
"\n",
"### Wrapping Hermite polynomials\n",
"\n",
"Download code (found after hours of googling ;-) , from\n",
"\n",
"\n",
"and extract\n",
"\n",
"Generate wrapper framework:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"# only run the following line _once_\n",
"# (and never again, otherwise the hand-modified hermite.pyf\n",
"# goes down the drains)\n",
"f2py -m hermite -h hermite.pyf hermite.f\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then modify\n",
"\n",
"Create the module:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"f2py -c hermite.pyf hermite.f\n",
"\n",
"# add this if you want:\n",
"-DF2PY_REPORT_ON_ARRAY_COPY=1 -DF2PY_REPORT_ATEXIT\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simple test:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import hermite\n",
"hermite.vahepo(2,2.0)\n",
"import scipy\n",
"scipy.special.hermite(2)(2.0)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A more complicated example about\n",
"how to wrap routines for band matrices can be found at\n",
"http://www.physik.tu-dresden.de/~baecker/comp_talks.html\n",
"under \"Python and Co - some recent developments\".\n",
"\n",
"## Wrapping a simple C code\n",
"\n",
"f2py is also capable of handling C code. An example is found on the wiki: [\"Cookbook/f2py_and_NumPy\"].\n",
"\n",
"## Step by step wrapping of a simple numerical code: Interactive System for Ice sheet Simulation\n",
"http://websrv.cs.umt.edu/isis/index.php/F2py_example"
]
}
],
"metadata": {}
}
]
}