"Cython has an IPython 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:"
"Cython has an IPython 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:"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"collapsed": false,
"collapsed": false,
"input": [
"input": [
"%load_ext cython"
"%load_ext cython"
],
],
"language": "python",
"language": "python",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"prompt_number": 1
"prompt_number": 1
},
},
{
{
"cell_type": "heading",
"cell_type": "heading",
"level": 2,
"level": 2,
"metadata": {},
"metadata": {},
"source": [
"source": [
"The %cython_inline magic"
"The %cython_inline magic"
]
]
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"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. "
"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. "
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"collapsed": false,
"collapsed": false,
"input": [
"input": [
"a = 10\n",
"a = 10\n",
"b = 20"
"b = 20"
],
],
"language": "python",
"language": "python",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"prompt_number": 2
"prompt_number": 2
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"collapsed": false,
"collapsed": false,
"input": [
"input": [
"%%cython_inline\n",
"%%cython_inline\n",
"return a+b"
"return a+b"
],
],
"language": "python",
"language": "python",
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"metadata": {},
"metadata": {},
"output_type": "pyout",
"output_type": "pyout",
"prompt_number": 3,
"prompt_number": 3,
"text": [
"text": [
"30"
"30"
]
]
}
}
],
],
"prompt_number": 3
"prompt_number": 3
},
},
{
{
"cell_type": "heading",
"cell_type": "heading",
"level": 2,
"level": 2,
"metadata": {},
"metadata": {},
"source": [
"source": [
"The %cython_pyximport magic"
"The %cython_pyximport magic"
]
]
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"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."
"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."
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"collapsed": false,
"collapsed": false,
"input": [
"input": [
"%%cython_pyximport foo\n",
"%%cython_pyximport foo\n",
"def f(x):\n",
"def f(x):\n",
" return 4.0*x"
" return 4.0*x"
],
],
"language": "python",
"language": "python",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"prompt_number": 4
"prompt_number": 4
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"collapsed": false,
"collapsed": false,
"input": [
"input": [
"f(10)"
"f(10)"
],
],
"language": "python",
"language": "python",
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"metadata": {},
"metadata": {},
"output_type": "pyout",
"output_type": "pyout",
"prompt_number": 5,
"prompt_number": 5,
"text": [
"text": [
"40.0"
"40.0"
]
]
}
}
],
],
"prompt_number": 5
"prompt_number": 5
},
},
{
{
"cell_type": "heading",
"cell_type": "heading",
"level": 2,
"level": 2,
"metadata": {},
"metadata": {},
"source": [
"source": [
"The %cython magic"
"The %cython magic"
]
]
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"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",
"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",
"\n",
"\n",
"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."
"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."
"100000 loops, best of 3: 2.28 \u00b5s per loop\n"
"100000 loops, best of 3: 2.28 \u00b5s per loop\n"
]
]
}
}
],
],
"prompt_number": 11
"prompt_number": 11
},
},
{
{
"cell_type": "heading",
"cell_type": "heading",
"level": 2,
"level": 2,
"metadata": {},
"metadata": {},
"source": [
"source": [
"External libraries"
"External libraries"
]
]
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"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:"
"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:"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"collapsed": false,
"collapsed": false,
"input": [
"input": [
"%%cython -lm\n",
"%%cython -lm\n",
"from libc.math cimport sin\n",
"from libc.math cimport sin\n",
"print 'sin(1)=', sin(1)"
"print 'sin(1)=', sin(1)"
],
],
"language": "python",
"language": "python",
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"output_type": "stream",
"output_type": "stream",
"stream": "stdout",
"stream": "stdout",
"text": [
"text": [
"sin(1)= 0.841470984808\n"
"sin(1)= 0.841470984808\n"
]
]
}
}
],
],
"prompt_number": 12
"prompt_number": 12
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"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."
"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."