##// END OF EJS Templates
Update example notebook with new flags.
Fernando Perez -
Show More
@@ -28,7 +28,6 b''
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 ],
@@ -51,9 +50,8 b''
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",
@@ -62,9 +60,8 b''
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",
@@ -94,10 +91,9 b''
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",
@@ -106,7 +102,6 b''
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 ],
@@ -132,42 +127,41 b''
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",
@@ -176,7 +170,6 b''
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 ],
@@ -194,7 +187,6 b''
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 ],
@@ -204,21 +196,42 b''
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 }
General Comments 0
You need to be logged in to leave comments. Login now