##// END OF EJS Templates
Update example notebook with new flags.
Fernando Perez -
Show More
@@ -1,226 +1,239 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "name": "cython_extension"
3 "name": "cython_extension"
4 },
4 },
5 "nbformat": 3,
5 "nbformat": 3,
6 "worksheets": [
6 "worksheets": [
7 {
7 {
8 "cells": [
8 "cells": [
9 {
9 {
10 "cell_type": "heading",
10 "cell_type": "heading",
11 "level": 1,
11 "level": 1,
12 "source": [
12 "source": [
13 "Cython Magic Functions Extension"
13 "Cython Magic Functions Extension"
14 ]
14 ]
15 },
15 },
16 {
16 {
17 "cell_type": "heading",
17 "cell_type": "heading",
18 "level": 2,
18 "level": 2,
19 "source": [
19 "source": [
20 "Loading the extension"
20 "Loading the extension"
21 ]
21 ]
22 },
22 },
23 {
23 {
24 "cell_type": "markdown",
24 "cell_type": "markdown",
25 "source": [
25 "source": [
26 "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:"
26 "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:"
27 ]
27 ]
28 },
28 },
29 {
29 {
30 "cell_type": "code",
30 "cell_type": "code",
31 "collapsed": false,
32 "input": [
31 "input": [
33 "%load_ext cythonmagic"
32 "%load_ext cythonmagic"
34 ],
33 ],
35 "language": "python",
34 "language": "python",
36 "outputs": [],
35 "outputs": [],
37 "prompt_number": 1
36 "prompt_number": 1
38 },
37 },
39 {
38 {
40 "cell_type": "heading",
39 "cell_type": "heading",
41 "level": 2,
40 "level": 2,
42 "source": [
41 "source": [
43 "The %cython_inline magic"
42 "The %cython_inline magic"
44 ]
43 ]
45 },
44 },
46 {
45 {
47 "cell_type": "markdown",
46 "cell_type": "markdown",
48 "source": [
47 "source": [
49 "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. "
48 "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. "
50 ]
49 ]
51 },
50 },
52 {
51 {
53 "cell_type": "code",
52 "cell_type": "code",
54 "collapsed": true,
55 "input": [
53 "input": [
56 "a = 10",
54 "a = 10\n",
57 "b = 20"
55 "b = 20"
58 ],
56 ],
59 "language": "python",
57 "language": "python",
60 "outputs": [],
58 "outputs": [],
61 "prompt_number": 8
59 "prompt_number": 8
62 },
60 },
63 {
61 {
64 "cell_type": "code",
62 "cell_type": "code",
65 "collapsed": false,
66 "input": [
63 "input": [
67 "%%cython_inline",
64 "%%cython_inline\n",
68 "return a+b"
65 "return a+b"
69 ],
66 ],
70 "language": "python",
67 "language": "python",
71 "outputs": [
68 "outputs": [
72 {
69 {
73 "output_type": "pyout",
70 "output_type": "pyout",
74 "prompt_number": 9,
71 "prompt_number": 9,
75 "text": [
72 "text": [
76 "30"
73 "30"
77 ]
74 ]
78 }
75 }
79 ],
76 ],
80 "prompt_number": 9
77 "prompt_number": 9
81 },
78 },
82 {
79 {
83 "cell_type": "heading",
80 "cell_type": "heading",
84 "level": 2,
81 "level": 2,
85 "source": [
82 "source": [
86 "The %cython_pyximport magic"
83 "The %cython_pyximport magic"
87 ]
84 ]
88 },
85 },
89 {
86 {
90 "cell_type": "markdown",
87 "cell_type": "markdown",
91 "source": [
88 "source": [
92 "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."
89 "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."
93 ]
90 ]
94 },
91 },
95 {
92 {
96 "cell_type": "code",
93 "cell_type": "code",
97 "collapsed": false,
98 "input": [
94 "input": [
99 "%%cython_pyximport foo",
95 "%%cython_pyximport foo\n",
100 "def f(x):",
96 "def f(x):\n",
101 " return 4.0*x"
97 " return 4.0*x"
102 ],
98 ],
103 "language": "python",
99 "language": "python",
104 "outputs": [],
100 "outputs": [],
105 "prompt_number": 18
101 "prompt_number": 18
106 },
102 },
107 {
103 {
108 "cell_type": "code",
104 "cell_type": "code",
109 "collapsed": false,
110 "input": [
105 "input": [
111 "f(10)"
106 "f(10)"
112 ],
107 ],
113 "language": "python",
108 "language": "python",
114 "outputs": [
109 "outputs": [
115 {
110 {
116 "output_type": "pyout",
111 "output_type": "pyout",
117 "prompt_number": 19,
112 "prompt_number": 19,
118 "text": [
113 "text": [
119 "40.0"
114 "40.0"
120 ]
115 ]
121 }
116 }
122 ],
117 ],
123 "prompt_number": 19
118 "prompt_number": 19
124 },
119 },
125 {
120 {
126 "cell_type": "heading",
121 "cell_type": "heading",
127 "level": 2,
122 "level": 2,
128 "source": [
123 "source": [
129 "The %cython magic"
124 "The %cython magic"
130 ]
125 ]
131 },
126 },
132 {
127 {
133 "cell_type": "markdown",
128 "cell_type": "markdown",
134 "source": [
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.",
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",
136 "",
131 "\n",
137 "Here is a simple example of a Black-Scholes options pricing algorithm written in Cython:"
132 "Here is a simple example of a Black-Scholes options pricing algorithm written in Cython:"
138 ]
133 ]
139 },
134 },
140 {
135 {
141 "cell_type": "code",
136 "cell_type": "code",
142 "collapsed": false,
143 "input": [
137 "input": [
144 "%%cython",
138 "%%cython\n",
145 "cimport cython",
139 "cimport cython\n",
146 "from libc.math cimport exp, sqrt, pow, log, erf",
140 "from libc.math cimport exp, sqrt, pow, log, erf\n",
147 "",
141 "\n",
148 "@cython.cdivision(True)",
142 "@cython.cdivision(True)\n",
149 "cdef double std_norm_cdf(double x) nogil:",
143 "cdef double std_norm_cdf(double x) nogil:\n",
150 " return 0.5*(1+erf(x/sqrt(2.0)))",
144 " return 0.5*(1+erf(x/sqrt(2.0)))\n",
151 "",
145 "\n",
152 "@cython.cdivision(True)",
146 "@cython.cdivision(True)\n",
153 "def black_scholes(double s, double k, double t, double v,",
147 "def black_scholes(double s, double k, double t, double v,\n",
154 " double rf, double div, double cp):",
148 " double rf, double div, double cp):\n",
155 " \"\"\"Price an option using the Black-Scholes model.",
149 " \"\"\"Price an option using the Black-Scholes model.\n",
156 " ",
150 " \n",
157 " s : initial stock price",
151 " s : initial stock price\n",
158 " k : strike price",
152 " k : strike price\n",
159 " t : expiration time",
153 " t : expiration time\n",
160 " v : volatility",
154 " v : volatility\n",
161 " rf : risk-free rate",
155 " rf : risk-free rate\n",
162 " div : dividend",
156 " div : dividend\n",
163 " cp : +1/-1 for call/put",
157 " cp : +1/-1 for call/put\n",
164 " \"\"\"",
158 " \"\"\"\n",
165 " cdef double d1, d2, optprice",
159 " cdef double d1, d2, optprice\n",
166 " with nogil:",
160 " with nogil:\n",
167 " d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))",
161 " d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))\n",
168 " d2 = d1 - v*sqrt(t)",
162 " d2 = d1 - v*sqrt(t)\n",
169 " optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\",
163 " optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\\n",
170 " cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)",
164 " cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)\n",
171 " return optprice"
165 " return optprice"
172 ],
166 ],
173 "language": "python",
167 "language": "python",
174 "outputs": [],
168 "outputs": [],
175 "prompt_number": 6
169 "prompt_number": 6
176 },
170 },
177 {
171 {
178 "cell_type": "code",
172 "cell_type": "code",
179 "collapsed": false,
180 "input": [
173 "input": [
181 "black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
174 "black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
182 ],
175 ],
183 "language": "python",
176 "language": "python",
184 "outputs": [
177 "outputs": [
185 {
178 {
186 "output_type": "pyout",
179 "output_type": "pyout",
187 "prompt_number": 7,
180 "prompt_number": 7,
188 "text": [
181 "text": [
189 "10.327861752731728"
182 "10.327861752731728"
190 ]
183 ]
191 }
184 }
192 ],
185 ],
193 "prompt_number": 7
186 "prompt_number": 7
194 },
187 },
195 {
188 {
196 "cell_type": "code",
189 "cell_type": "code",
197 "collapsed": false,
198 "input": [
190 "input": [
199 "%timeit black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
191 "%timeit black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
200 ],
192 ],
201 "language": "python",
193 "language": "python",
202 "outputs": [
194 "outputs": [
203 {
195 {
204 "output_type": "stream",
196 "output_type": "stream",
205 "stream": "stdout",
197 "stream": "stdout",
206 "text": [
198 "text": [
207 "1000000 loops, best of 3: 621 ns per loop",
199 "1000000 loops, best of 3: 621 ns per loop\n"
208 ""
209 ]
200 ]
210 }
201 }
211 ],
202 ],
212 "prompt_number": 14
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 "cell_type": "code",
212 "cell_type": "code",
216 "collapsed": true,
217 "input": [
213 "input": [
218 ""
214 "%%cython -l m\n",
215 "from libc.math cimport sin\n",
216 "print 'sin(1)=', sin(1)"
219 ],
217 ],
220 "language": "python",
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 }
225 ]
238 ]
226 } No newline at end of file
239 }
General Comments 0
You need to be logged in to leave comments. Login now