Show More
@@ -1,226 +1,239 b'' | |||
|
1 | 1 | { |
|
2 | 2 | "metadata": { |
|
3 | 3 | "name": "cython_extension" |
|
4 | 4 | }, |
|
5 | 5 | "nbformat": 3, |
|
6 | 6 | "worksheets": [ |
|
7 | 7 | { |
|
8 | 8 | "cells": [ |
|
9 | 9 | { |
|
10 | 10 | "cell_type": "heading", |
|
11 | 11 | "level": 1, |
|
12 | 12 | "source": [ |
|
13 | 13 | "Cython Magic Functions Extension" |
|
14 | 14 | ] |
|
15 | 15 | }, |
|
16 | 16 | { |
|
17 | 17 | "cell_type": "heading", |
|
18 | 18 | "level": 2, |
|
19 | 19 | "source": [ |
|
20 | 20 | "Loading the extension" |
|
21 | 21 | ] |
|
22 | 22 | }, |
|
23 | 23 | { |
|
24 | 24 | "cell_type": "markdown", |
|
25 | 25 | "source": [ |
|
26 | 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 | 30 | "cell_type": "code", |
|
31 | "collapsed": false, | |
|
32 | 31 | "input": [ |
|
33 | 32 | "%load_ext cythonmagic" |
|
34 | 33 | ], |
|
35 | 34 | "language": "python", |
|
36 | 35 | "outputs": [], |
|
37 | 36 | "prompt_number": 1 |
|
38 | 37 | }, |
|
39 | 38 | { |
|
40 | 39 | "cell_type": "heading", |
|
41 | 40 | "level": 2, |
|
42 | 41 | "source": [ |
|
43 | 42 | "The %cython_inline magic" |
|
44 | 43 | ] |
|
45 | 44 | }, |
|
46 | 45 | { |
|
47 | 46 | "cell_type": "markdown", |
|
48 | 47 | "source": [ |
|
49 | 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 | 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", |
|
60 | 58 | "outputs": [], |
|
61 | 59 | "prompt_number": 8 |
|
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", |
|
71 | 68 | "outputs": [ |
|
72 | 69 | { |
|
73 | 70 | "output_type": "pyout", |
|
74 | 71 | "prompt_number": 9, |
|
75 | 72 | "text": [ |
|
76 | 73 | "30" |
|
77 | 74 | ] |
|
78 | 75 | } |
|
79 | 76 | ], |
|
80 | 77 | "prompt_number": 9 |
|
81 | 78 | }, |
|
82 | 79 | { |
|
83 | 80 | "cell_type": "heading", |
|
84 | 81 | "level": 2, |
|
85 | 82 | "source": [ |
|
86 | 83 | "The %cython_pyximport magic" |
|
87 | 84 | ] |
|
88 | 85 | }, |
|
89 | 86 | { |
|
90 | 87 | "cell_type": "markdown", |
|
91 | 88 | "source": [ |
|
92 | 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 | 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", |
|
104 | 100 | "outputs": [], |
|
105 | 101 | "prompt_number": 18 |
|
106 | 102 | }, |
|
107 | 103 | { |
|
108 | 104 | "cell_type": "code", |
|
109 | "collapsed": false, | |
|
110 | 105 | "input": [ |
|
111 | 106 | "f(10)" |
|
112 | 107 | ], |
|
113 | 108 | "language": "python", |
|
114 | 109 | "outputs": [ |
|
115 | 110 | { |
|
116 | 111 | "output_type": "pyout", |
|
117 | 112 | "prompt_number": 19, |
|
118 | 113 | "text": [ |
|
119 | 114 | "40.0" |
|
120 | 115 | ] |
|
121 | 116 | } |
|
122 | 117 | ], |
|
123 | 118 | "prompt_number": 19 |
|
124 | 119 | }, |
|
125 | 120 | { |
|
126 | 121 | "cell_type": "heading", |
|
127 | 122 | "level": 2, |
|
128 | 123 | "source": [ |
|
129 | 124 | "The %cython magic" |
|
130 | 125 | ] |
|
131 | 126 | }, |
|
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", |
|
174 | 168 | "outputs": [], |
|
175 | 169 | "prompt_number": 6 |
|
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 | ], |
|
183 | 176 | "language": "python", |
|
184 | 177 | "outputs": [ |
|
185 | 178 | { |
|
186 | 179 | "output_type": "pyout", |
|
187 | 180 | "prompt_number": 7, |
|
188 | 181 | "text": [ |
|
189 | 182 | "10.327861752731728" |
|
190 | 183 | ] |
|
191 | 184 | } |
|
192 | 185 | ], |
|
193 | 186 | "prompt_number": 7 |
|
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 | ], |
|
201 | 193 | "language": "python", |
|
202 | 194 | "outputs": [ |
|
203 | 195 | { |
|
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 | } |
|
225 | 238 | ] |
|
226 | 239 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now