You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"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
+
]
32
+
},
33
+
{
34
+
"cell_type": "code",
35
+
"collapsed": false,
36
+
"input": [
37
+
"%load_ext cythonmagic"
38
+
],
39
+
"language": "python",
40
+
"metadata": {},
41
+
"outputs": [],
42
+
"prompt_number": 1
43
+
},
44
+
{
45
+
"cell_type": "heading",
46
+
"level": 2,
47
+
"metadata": {},
48
+
"source": [
49
+
"The %cython_inline magic"
50
+
]
51
+
},
52
+
{
53
+
"cell_type": "markdown",
54
+
"metadata": {},
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. "
57
+
]
58
+
},
59
+
{
60
+
"cell_type": "code",
61
+
"collapsed": false,
62
+
"input": [
63
+
"a = 10\n",
64
+
"b = 20"
65
+
],
66
+
"language": "python",
67
+
"metadata": {},
68
+
"outputs": [],
69
+
"prompt_number": 2
70
+
},
71
+
{
72
+
"cell_type": "code",
73
+
"collapsed": false,
74
+
"input": [
75
+
"%%cython_inline\n",
76
+
"return a+b"
77
+
],
78
+
"language": "python",
79
+
"metadata": {},
80
+
"outputs": [
81
+
{
82
+
"output_type": "pyout",
83
+
"prompt_number": 3,
84
+
"text": [
85
+
"30"
86
+
]
87
+
}
88
+
],
89
+
"prompt_number": 3
90
+
},
91
+
{
92
+
"cell_type": "heading",
93
+
"level": 2,
94
+
"metadata": {},
95
+
"source": [
96
+
"The %cython_pyximport magic"
97
+
]
98
+
},
99
+
{
100
+
"cell_type": "markdown",
101
+
"metadata": {},
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."
104
+
]
105
+
},
106
+
{
107
+
"cell_type": "code",
108
+
"collapsed": false,
109
+
"input": [
110
+
"%%cython_pyximport foo\n",
111
+
"def f(x):\n",
112
+
" return 4.0*x"
113
+
],
114
+
"language": "python",
115
+
"metadata": {},
116
+
"outputs": [],
117
+
"prompt_number": 4
118
+
},
119
+
{
120
+
"cell_type": "code",
121
+
"collapsed": false,
122
+
"input": [
123
+
"f(10)"
124
+
],
125
+
"language": "python",
126
+
"metadata": {},
127
+
"outputs": [
128
+
{
129
+
"output_type": "pyout",
130
+
"prompt_number": 5,
131
+
"text": [
132
+
"40.0"
133
+
]
134
+
}
135
+
],
136
+
"prompt_number": 5
137
+
},
138
+
{
139
+
"cell_type": "heading",
140
+
"level": 2,
141
+
"metadata": {},
142
+
"source": [
143
+
"The %cython magic"
144
+
]
145
+
},
146
+
{
147
+
"cell_type": "markdown",
148
+
"metadata": {},
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",
151
+
"\n",
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."
"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:"
244
+
]
245
+
},
246
+
{
247
+
"cell_type": "code",
248
+
"collapsed": false,
249
+
"input": [
250
+
"%%cython -lm\n",
251
+
"from libc.math cimport sin\n",
252
+
"print 'sin(1)=', sin(1)"
253
+
],
254
+
"language": "python",
255
+
"metadata": {},
256
+
"outputs": [
257
+
{
258
+
"output_type": "stream",
259
+
"stream": "stdout",
260
+
"text": [
261
+
"sin(1)= 0.841470984808\n"
262
+
]
263
+
}
264
+
],
265
+
"prompt_number": 9
266
+
},
267
+
{
268
+
"cell_type": "markdown",
269
+
"metadata": {},
270
+
"source": [
271
+
"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."
0 commit comments