Skip to content

Commit

Permalink
Added f2py
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesFergusson committed Nov 26, 2019
1 parent 4e87599 commit faff16b
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions 9_Cython.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,77 @@
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fortran\n",
"\n",
"As an aside we note that something similar exists for Fortran code, the f2py package (which can also handle C). It's only for wrapping code where as Cython alows you to mix it together and in fortran strings and arrays need a little more work to pass (see: https://docs.scipy.org/doc/numpy/f2py/). F2py can be used on the command line a bit like a compiler, first you create some Fortran code like this: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"FUNCTION fibonacci(n,b)\n",
"\tINTEGER, INTENT(in) :: n\n",
"\tINTEGER, INTENT(out) :: fibonacci\n",
"\tINTEGER :: i,a,b,tmp\n",
"\ta = 0\n",
"\tb = 1\n",
"\tDO i=1,n\n",
"\t\ttmp = a+b\n",
"\t\ta = b\n",
"\t\tb = tmp\n",
"\tEND DO\n",
"\tfibonacci = b\n",
"END FUNCTION fibonacci "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then to create a loadable module (`.so` file) you would then compile with f2py:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f2py -c fibonacci.f90 -m forfuncs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now you should be able to use the function after importing the module in python"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import forfuncs\n",
"\n",
"forfuncs.fibonacci(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Otherwise you can use it via python by creating signiture files. I'll leave you to explore the documentation..."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit faff16b

Please sign in to comment.