diff --git a/docs/examples/notebooks/cython_extension.ipynb b/docs/examples/notebooks/cython_extension.ipynb index 6aaf636..a6689fa 100644 --- a/docs/examples/notebooks/cython_extension.ipynb +++ b/docs/examples/notebooks/cython_extension.ipynb @@ -28,7 +28,6 @@ }, { "cell_type": "code", - "collapsed": false, "input": [ "%load_ext cythonmagic" ], @@ -51,9 +50,8 @@ }, { "cell_type": "code", - "collapsed": true, "input": [ - "a = 10", + "a = 10\n", "b = 20" ], "language": "python", @@ -62,9 +60,8 @@ }, { "cell_type": "code", - "collapsed": false, "input": [ - "%%cython_inline", + "%%cython_inline\n", "return a+b" ], "language": "python", @@ -94,10 +91,9 @@ }, { "cell_type": "code", - "collapsed": false, "input": [ - "%%cython_pyximport foo", - "def f(x):", + "%%cython_pyximport foo\n", + "def f(x):\n", " return 4.0*x" ], "language": "python", @@ -106,7 +102,6 @@ }, { "cell_type": "code", - "collapsed": false, "input": [ "f(10)" ], @@ -132,42 +127,41 @@ { "cell_type": "markdown", "source": [ - "Probably the most important magic is the `%cython` magic. This is similar to the `%%cython_pyximport` magic, but doesn't require you to specify a module name. Instead, the `%%cython` magic uses manages everything using temporary files in the `~/.cython/magic` directory. All of the symbols in the Cython module are imported automatically by the magic.", - "", + "Probably the most important magic is the `%cython` magic. This is similar to the `%%cython_pyximport` magic, but doesn't require you to specify a module name. Instead, the `%%cython` magic uses manages everything using temporary files in the `~/.cython/magic` directory. All of the symbols in the Cython module are imported automatically by the magic.\n", + "\n", "Here is a simple example of a Black-Scholes options pricing algorithm written in Cython:" ] }, { "cell_type": "code", - "collapsed": false, "input": [ - "%%cython", - "cimport cython", - "from libc.math cimport exp, sqrt, pow, log, erf", - "", - "@cython.cdivision(True)", - "cdef double std_norm_cdf(double x) nogil:", - " return 0.5*(1+erf(x/sqrt(2.0)))", - "", - "@cython.cdivision(True)", - "def black_scholes(double s, double k, double t, double v,", - " double rf, double div, double cp):", - " \"\"\"Price an option using the Black-Scholes model.", - " ", - " s : initial stock price", - " k : strike price", - " t : expiration time", - " v : volatility", - " rf : risk-free rate", - " div : dividend", - " cp : +1/-1 for call/put", - " \"\"\"", - " cdef double d1, d2, optprice", - " with nogil:", - " d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))", - " d2 = d1 - v*sqrt(t)", - " optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\", - " cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)", + "%%cython\n", + "cimport cython\n", + "from libc.math cimport exp, sqrt, pow, log, erf\n", + "\n", + "@cython.cdivision(True)\n", + "cdef double std_norm_cdf(double x) nogil:\n", + " return 0.5*(1+erf(x/sqrt(2.0)))\n", + "\n", + "@cython.cdivision(True)\n", + "def black_scholes(double s, double k, double t, double v,\n", + " double rf, double div, double cp):\n", + " \"\"\"Price an option using the Black-Scholes model.\n", + " \n", + " s : initial stock price\n", + " k : strike price\n", + " t : expiration time\n", + " v : volatility\n", + " rf : risk-free rate\n", + " div : dividend\n", + " cp : +1/-1 for call/put\n", + " \"\"\"\n", + " cdef double d1, d2, optprice\n", + " with nogil:\n", + " d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))\n", + " d2 = d1 - v*sqrt(t)\n", + " optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\\n", + " cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)\n", " return optprice" ], "language": "python", @@ -176,7 +170,6 @@ }, { "cell_type": "code", - "collapsed": false, "input": [ "black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)" ], @@ -194,7 +187,6 @@ }, { "cell_type": "code", - "collapsed": false, "input": [ "%timeit black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)" ], @@ -204,21 +196,42 @@ "output_type": "stream", "stream": "stdout", "text": [ - "1000000 loops, best of 3: 621 ns per loop", - "" + "1000000 loops, best of 3: 621 ns per loop\n" ] } ], "prompt_number": 14 }, { + "cell_type": "markdown", + "source": [ + "Cython allows you to specify additional libraries to be linked with your extension, you can do so with the `-l` flag (also spelled `--lib`). Note that this flag can be passed more than once to specify multiple libraries, such as `-l lib1 -l lib2`. Here's a simple example of how to access the system math library:" + ] + }, + { "cell_type": "code", - "collapsed": true, "input": [ - "" + "%%cython -l m\n", + "from libc.math cimport sin\n", + "print 'sin(1)=', sin(1)" ], "language": "python", - "outputs": [] + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sin(1)= 0.841470984808\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "markdown", + "source": [ + "You can similarly use the `-i/--include` flag to add include directories to the search path, and `-c/--compile-args` to add extra flags that are passed to Cython via the `extra_compile_args` of the distutils `Extension` class. Please see [the Cython docs on C library usage](http://docs.cython.org/src/tutorial/clibraries.html) for more details on the use of these flags." + ] } ] }