##// END OF EJS Templates
Merge pull request #2741 from bfroehle/cython_example_erf_missing...
Bussonnier Matthias -
r8970:d22004d6 merge
parent child Browse files
Show More
@@ -1,270 +1,270
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "name": "cython_extension"
3 "name": "cython_extension"
4 },
4 },
5 "nbformat": 3,
5 "nbformat": 3,
6 "nbformat_minor": 0,
6 "nbformat_minor": 0,
7 "worksheets": [
7 "worksheets": [
8 {
8 {
9 "cells": [
9 "cells": [
10 {
10 {
11 "cell_type": "heading",
11 "cell_type": "heading",
12 "level": 1,
12 "level": 1,
13 "metadata": {},
13 "metadata": {},
14 "source": [
14 "source": [
15 "Cython Magic Functions Extension"
15 "Cython Magic Functions Extension"
16 ]
16 ]
17 },
17 },
18 {
18 {
19 "cell_type": "heading",
19 "cell_type": "heading",
20 "level": 2,
20 "level": 2,
21 "metadata": {},
21 "metadata": {},
22 "source": [
22 "source": [
23 "Loading the extension"
23 "Loading the extension"
24 ]
24 ]
25 },
25 },
26 {
26 {
27 "cell_type": "markdown",
27 "cell_type": "markdown",
28 "metadata": {},
28 "metadata": {},
29 "source": [
29 "source": [
30 "IPtyhon has a `cythonmagic` extension that contains a number of magic functions for working with Cython code. This extension can be loaded using the `%load_ext` magic as follows:"
30 "IPtyhon has a `cythonmagic` extension that contains a number of magic functions for working with Cython code. This extension can be loaded using the `%load_ext` magic as follows:"
31 ]
31 ]
32 },
32 },
33 {
33 {
34 "cell_type": "code",
34 "cell_type": "code",
35 "collapsed": false,
35 "collapsed": false,
36 "input": [
36 "input": [
37 "%load_ext cythonmagic"
37 "%load_ext cythonmagic"
38 ],
38 ],
39 "language": "python",
39 "language": "python",
40 "metadata": {},
40 "metadata": {},
41 "outputs": [],
41 "outputs": [],
42 "prompt_number": 1
42 "prompt_number": 1
43 },
43 },
44 {
44 {
45 "cell_type": "heading",
45 "cell_type": "heading",
46 "level": 2,
46 "level": 2,
47 "metadata": {},
47 "metadata": {},
48 "source": [
48 "source": [
49 "The %cython_inline magic"
49 "The %cython_inline magic"
50 ]
50 ]
51 },
51 },
52 {
52 {
53 "cell_type": "markdown",
53 "cell_type": "markdown",
54 "metadata": {},
54 "metadata": {},
55 "source": [
55 "source": [
56 "The `%%cython_inline` magic uses `Cython.inline` to compile a Cython expression. This allows you to enter and run a function body with Cython code. Use a bare `return` statement to return values. "
56 "The `%%cython_inline` magic uses `Cython.inline` to compile a Cython expression. This allows you to enter and run a function body with Cython code. Use a bare `return` statement to return values. "
57 ]
57 ]
58 },
58 },
59 {
59 {
60 "cell_type": "code",
60 "cell_type": "code",
61 "collapsed": false,
61 "collapsed": false,
62 "input": [
62 "input": [
63 "a = 10\n",
63 "a = 10\n",
64 "b = 20"
64 "b = 20"
65 ],
65 ],
66 "language": "python",
66 "language": "python",
67 "metadata": {},
67 "metadata": {},
68 "outputs": [],
68 "outputs": [],
69 "prompt_number": 2
69 "prompt_number": 2
70 },
70 },
71 {
71 {
72 "cell_type": "code",
72 "cell_type": "code",
73 "collapsed": false,
73 "collapsed": false,
74 "input": [
74 "input": [
75 "%%cython_inline\n",
75 "%%cython_inline\n",
76 "return a+b"
76 "return a+b"
77 ],
77 ],
78 "language": "python",
78 "language": "python",
79 "metadata": {},
79 "metadata": {},
80 "outputs": [
80 "outputs": [
81 {
81 {
82 "output_type": "pyout",
82 "output_type": "pyout",
83 "prompt_number": 3,
83 "prompt_number": 3,
84 "text": [
84 "text": [
85 "30"
85 "30"
86 ]
86 ]
87 }
87 }
88 ],
88 ],
89 "prompt_number": 3
89 "prompt_number": 3
90 },
90 },
91 {
91 {
92 "cell_type": "heading",
92 "cell_type": "heading",
93 "level": 2,
93 "level": 2,
94 "metadata": {},
94 "metadata": {},
95 "source": [
95 "source": [
96 "The %cython_pyximport magic"
96 "The %cython_pyximport magic"
97 ]
97 ]
98 },
98 },
99 {
99 {
100 "cell_type": "markdown",
100 "cell_type": "markdown",
101 "metadata": {},
101 "metadata": {},
102 "source": [
102 "source": [
103 "The `%%cython_pyximport` magic allows you to enter arbitrary Cython code into a cell. That Cython code is written as a `.pyx` file in the current working directory and then imported using `pyximport`. You have the specify the name of the module that the Code will appear in. All symbols from the module are imported automatically by the magic function."
103 "The `%%cython_pyximport` magic allows you to enter arbitrary Cython code into a cell. That Cython code is written as a `.pyx` file in the current working directory and then imported using `pyximport`. You have the specify the name of the module that the Code will appear in. All symbols from the module are imported automatically by the magic function."
104 ]
104 ]
105 },
105 },
106 {
106 {
107 "cell_type": "code",
107 "cell_type": "code",
108 "collapsed": false,
108 "collapsed": false,
109 "input": [
109 "input": [
110 "%%cython_pyximport foo\n",
110 "%%cython_pyximport foo\n",
111 "def f(x):\n",
111 "def f(x):\n",
112 " return 4.0*x"
112 " return 4.0*x"
113 ],
113 ],
114 "language": "python",
114 "language": "python",
115 "metadata": {},
115 "metadata": {},
116 "outputs": [],
116 "outputs": [],
117 "prompt_number": 4
117 "prompt_number": 4
118 },
118 },
119 {
119 {
120 "cell_type": "code",
120 "cell_type": "code",
121 "collapsed": false,
121 "collapsed": false,
122 "input": [
122 "input": [
123 "f(10)"
123 "f(10)"
124 ],
124 ],
125 "language": "python",
125 "language": "python",
126 "metadata": {},
126 "metadata": {},
127 "outputs": [
127 "outputs": [
128 {
128 {
129 "output_type": "pyout",
129 "output_type": "pyout",
130 "prompt_number": 5,
130 "prompt_number": 5,
131 "text": [
131 "text": [
132 "40.0"
132 "40.0"
133 ]
133 ]
134 }
134 }
135 ],
135 ],
136 "prompt_number": 5
136 "prompt_number": 5
137 },
137 },
138 {
138 {
139 "cell_type": "heading",
139 "cell_type": "heading",
140 "level": 2,
140 "level": 2,
141 "metadata": {},
141 "metadata": {},
142 "source": [
142 "source": [
143 "The %cython magic"
143 "The %cython magic"
144 ]
144 ]
145 },
145 },
146 {
146 {
147 "cell_type": "markdown",
147 "cell_type": "markdown",
148 "metadata": {},
148 "metadata": {},
149 "source": [
149 "source": [
150 "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",
150 "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",
151 "\n",
151 "\n",
152 "Here is a simple example of a Black-Scholes options pricing algorithm written in Cython:"
152 "Here is a simple example of a Black-Scholes options pricing algorithm written in Cython. Please note that this example might not compile on non-POSIX systems (e.g., Windows) because of a missing `erf` symbol."
153 ]
153 ]
154 },
154 },
155 {
155 {
156 "cell_type": "code",
156 "cell_type": "code",
157 "collapsed": false,
157 "collapsed": false,
158 "input": [
158 "input": [
159 "%%cython\n",
159 "%%cython\n",
160 "cimport cython\n",
160 "cimport cython\n",
161 "from libc.math cimport exp, sqrt, pow, log, erf\n",
161 "from libc.math cimport exp, sqrt, pow, log, erf\n",
162 "\n",
162 "\n",
163 "@cython.cdivision(True)\n",
163 "@cython.cdivision(True)\n",
164 "cdef double std_norm_cdf(double x) nogil:\n",
164 "cdef double std_norm_cdf(double x) nogil:\n",
165 " return 0.5*(1+erf(x/sqrt(2.0)))\n",
165 " return 0.5*(1+erf(x/sqrt(2.0)))\n",
166 "\n",
166 "\n",
167 "@cython.cdivision(True)\n",
167 "@cython.cdivision(True)\n",
168 "def black_scholes(double s, double k, double t, double v,\n",
168 "def black_scholes(double s, double k, double t, double v,\n",
169 " double rf, double div, double cp):\n",
169 " double rf, double div, double cp):\n",
170 " \"\"\"Price an option using the Black-Scholes model.\n",
170 " \"\"\"Price an option using the Black-Scholes model.\n",
171 " \n",
171 " \n",
172 " s : initial stock price\n",
172 " s : initial stock price\n",
173 " k : strike price\n",
173 " k : strike price\n",
174 " t : expiration time\n",
174 " t : expiration time\n",
175 " v : volatility\n",
175 " v : volatility\n",
176 " rf : risk-free rate\n",
176 " rf : risk-free rate\n",
177 " div : dividend\n",
177 " div : dividend\n",
178 " cp : +1/-1 for call/put\n",
178 " cp : +1/-1 for call/put\n",
179 " \"\"\"\n",
179 " \"\"\"\n",
180 " cdef double d1, d2, optprice\n",
180 " cdef double d1, d2, optprice\n",
181 " with nogil:\n",
181 " with nogil:\n",
182 " d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))\n",
182 " d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))\n",
183 " d2 = d1 - v*sqrt(t)\n",
183 " d2 = d1 - v*sqrt(t)\n",
184 " optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\\n",
184 " optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\\n",
185 " cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)\n",
185 " cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)\n",
186 " return optprice"
186 " return optprice"
187 ],
187 ],
188 "language": "python",
188 "language": "python",
189 "metadata": {},
189 "metadata": {},
190 "outputs": [],
190 "outputs": [],
191 "prompt_number": 6
191 "prompt_number": 6
192 },
192 },
193 {
193 {
194 "cell_type": "code",
194 "cell_type": "code",
195 "collapsed": false,
195 "collapsed": false,
196 "input": [
196 "input": [
197 "black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
197 "black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
198 ],
198 ],
199 "language": "python",
199 "language": "python",
200 "metadata": {},
200 "metadata": {},
201 "outputs": [
201 "outputs": [
202 {
202 {
203 "output_type": "pyout",
203 "output_type": "pyout",
204 "prompt_number": 7,
204 "prompt_number": 7,
205 "text": [
205 "text": [
206 "10.327861752731728"
206 "10.327861752731728"
207 ]
207 ]
208 }
208 }
209 ],
209 ],
210 "prompt_number": 7
210 "prompt_number": 7
211 },
211 },
212 {
212 {
213 "cell_type": "code",
213 "cell_type": "code",
214 "collapsed": false,
214 "collapsed": false,
215 "input": [
215 "input": [
216 "%timeit black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
216 "%timeit black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
217 ],
217 ],
218 "language": "python",
218 "language": "python",
219 "metadata": {},
219 "metadata": {},
220 "outputs": [
220 "outputs": [
221 {
221 {
222 "output_type": "stream",
222 "output_type": "stream",
223 "stream": "stdout",
223 "stream": "stdout",
224 "text": [
224 "text": [
225 "1000000 loops, best of 3: 821 ns per loop\n"
225 "1000000 loops, best of 3: 821 ns per loop\n"
226 ]
226 ]
227 }
227 }
228 ],
228 ],
229 "prompt_number": 8
229 "prompt_number": 8
230 },
230 },
231 {
231 {
232 "cell_type": "markdown",
232 "cell_type": "markdown",
233 "metadata": {},
233 "metadata": {},
234 "source": [
234 "source": [
235 "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 `-lm -llib2 --lib lib3`. Here's a simple example of how to access the system math library:"
235 "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 `-lm -llib2 --lib lib3`. Here's a simple example of how to access the system math library:"
236 ]
236 ]
237 },
237 },
238 {
238 {
239 "cell_type": "code",
239 "cell_type": "code",
240 "collapsed": false,
240 "collapsed": false,
241 "input": [
241 "input": [
242 "%%cython -lm\n",
242 "%%cython -lm\n",
243 "from libc.math cimport sin\n",
243 "from libc.math cimport sin\n",
244 "print 'sin(1)=', sin(1)"
244 "print 'sin(1)=', sin(1)"
245 ],
245 ],
246 "language": "python",
246 "language": "python",
247 "metadata": {},
247 "metadata": {},
248 "outputs": [
248 "outputs": [
249 {
249 {
250 "output_type": "stream",
250 "output_type": "stream",
251 "stream": "stdout",
251 "stream": "stdout",
252 "text": [
252 "text": [
253 "sin(1)= 0.841470984808\n"
253 "sin(1)= 0.841470984808\n"
254 ]
254 ]
255 }
255 }
256 ],
256 ],
257 "prompt_number": 9
257 "prompt_number": 9
258 },
258 },
259 {
259 {
260 "cell_type": "markdown",
260 "cell_type": "markdown",
261 "metadata": {},
261 "metadata": {},
262 "source": [
262 "source": [
263 "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."
263 "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."
264 ]
264 ]
265 }
265 }
266 ],
266 ],
267 "metadata": {}
267 "metadata": {}
268 }
268 }
269 ]
269 ]
270 } No newline at end of file
270 }
General Comments 0
You need to be logged in to leave comments. Login now