diff --git a/9_Cython.ipynb b/9_Cython.ipynb index 12a4d05..cd610b2 100644 --- a/9_Cython.ipynb +++ b/9_Cython.ipynb @@ -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,