##// END OF EJS Templates
Update example notebook with new flags.
Fernando Perez -
Show More
@@ -28,7 +28,6 b''
28 28 },
29 29 {
30 30 "cell_type": "code",
31 "collapsed": false,
32 31 "input": [
33 32 "%load_ext cythonmagic"
34 33 ],
@@ -51,9 +50,8 b''
51 50 },
52 51 {
53 52 "cell_type": "code",
54 "collapsed": true,
55 53 "input": [
56 "a = 10",
54 "a = 10\n",
57 55 "b = 20"
58 56 ],
59 57 "language": "python",
@@ -62,9 +60,8 b''
62 60 },
63 61 {
64 62 "cell_type": "code",
65 "collapsed": false,
66 63 "input": [
67 "%%cython_inline",
64 "%%cython_inline\n",
68 65 "return a+b"
69 66 ],
70 67 "language": "python",
@@ -94,10 +91,9 b''
94 91 },
95 92 {
96 93 "cell_type": "code",
97 "collapsed": false,
98 94 "input": [
99 "%%cython_pyximport foo",
100 "def f(x):",
95 "%%cython_pyximport foo\n",
96 "def f(x):\n",
101 97 " return 4.0*x"
102 98 ],
103 99 "language": "python",
@@ -106,7 +102,6 b''
106 102 },
107 103 {
108 104 "cell_type": "code",
109 "collapsed": false,
110 105 "input": [
111 106 "f(10)"
112 107 ],
@@ -132,42 +127,41 b''
132 127 {
133 128 "cell_type": "markdown",
134 129 "source": [
135 "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.",
136 "",
130 "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",
131 "\n",
137 132 "Here is a simple example of a Black-Scholes options pricing algorithm written in Cython:"
138 133 ]
139 134 },
140 135 {
141 136 "cell_type": "code",
142 "collapsed": false,
143 137 "input": [
144 "%%cython",
145 "cimport cython",
146 "from libc.math cimport exp, sqrt, pow, log, erf",
147 "",
148 "@cython.cdivision(True)",
149 "cdef double std_norm_cdf(double x) nogil:",
150 " return 0.5*(1+erf(x/sqrt(2.0)))",
151 "",
152 "@cython.cdivision(True)",
153 "def black_scholes(double s, double k, double t, double v,",
154 " double rf, double div, double cp):",
155 " \"\"\"Price an option using the Black-Scholes model.",
156 " ",
157 " s : initial stock price",
158 " k : strike price",
159 " t : expiration time",
160 " v : volatility",
161 " rf : risk-free rate",
162 " div : dividend",
163 " cp : +1/-1 for call/put",
164 " \"\"\"",
165 " cdef double d1, d2, optprice",
166 " with nogil:",
167 " d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))",
168 " d2 = d1 - v*sqrt(t)",
169 " optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\",
170 " cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)",
138 "%%cython\n",
139 "cimport cython\n",
140 "from libc.math cimport exp, sqrt, pow, log, erf\n",
141 "\n",
142 "@cython.cdivision(True)\n",
143 "cdef double std_norm_cdf(double x) nogil:\n",
144 " return 0.5*(1+erf(x/sqrt(2.0)))\n",
145 "\n",
146 "@cython.cdivision(True)\n",
147 "def black_scholes(double s, double k, double t, double v,\n",
148 " double rf, double div, double cp):\n",
149 " \"\"\"Price an option using the Black-Scholes model.\n",
150 " \n",
151 " s : initial stock price\n",
152 " k : strike price\n",
153 " t : expiration time\n",
154 " v : volatility\n",
155 " rf : risk-free rate\n",
156 " div : dividend\n",
157 " cp : +1/-1 for call/put\n",
158 " \"\"\"\n",
159 " cdef double d1, d2, optprice\n",
160 " with nogil:\n",
161 " d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))\n",
162 " d2 = d1 - v*sqrt(t)\n",
163 " optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\\n",
164 " cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)\n",
171 165 " return optprice"
172 166 ],
173 167 "language": "python",
@@ -176,7 +170,6 b''
176 170 },
177 171 {
178 172 "cell_type": "code",
179 "collapsed": false,
180 173 "input": [
181 174 "black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
182 175 ],
@@ -194,7 +187,6 b''
194 187 },
195 188 {
196 189 "cell_type": "code",
197 "collapsed": false,
198 190 "input": [
199 191 "%timeit black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
200 192 ],
@@ -204,21 +196,42 b''
204 196 "output_type": "stream",
205 197 "stream": "stdout",
206 198 "text": [
207 "1000000 loops, best of 3: 621 ns per loop",
208 ""
199 "1000000 loops, best of 3: 621 ns per loop\n"
209 200 ]
210 201 }
211 202 ],
212 203 "prompt_number": 14
213 204 },
214 205 {
206 "cell_type": "markdown",
207 "source": [
208 "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:"
209 ]
210 },
211 {
215 212 "cell_type": "code",
216 "collapsed": true,
217 213 "input": [
218 ""
214 "%%cython -l m\n",
215 "from libc.math cimport sin\n",
216 "print 'sin(1)=', sin(1)"
219 217 ],
220 218 "language": "python",
221 "outputs": []
219 "outputs": [
220 {
221 "output_type": "stream",
222 "stream": "stdout",
223 "text": [
224 "sin(1)= 0.841470984808\n"
225 ]
226 }
227 ],
228 "prompt_number": 2
229 },
230 {
231 "cell_type": "markdown",
232 "source": [
233 "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."
234 ]
222 235 }
223 236 ]
224 237 }
General Comments 0
You need to be logged in to leave comments. Login now