Commit bd2a3d43 authored by ggellner@encolpuis's avatar ggellner@encolpuis

Made cython highlighting the default in all files.

parent e5d24fd8
.. highlight:: cython
.. _early-binding-speed-label:
Early Binding for Speed
......@@ -17,7 +19,9 @@ slowness compared to 'early binding' languages such as C++.
However with Cython it is possible to gain significant speed-ups through the
use of 'early binding' programming techniques.
For example, consider the following (silly) code example::
For example, consider the following (silly) code example:
.. sourcecode:: cython
cdef class Rectangle:
cdef int x0, y0
......@@ -38,7 +42,9 @@ In the :func:`rectArea` method, the call to :meth:`rect.area` and the
:meth:`.area` method contain a lot of Python overhead.
However, in Cython, it is possible to eliminate a lot of this overhead in cases
where calls occur within Cython code. For example::
where calls occur within Cython code. For example:
.. sourcecode:: cython
cdef class Rectangle:
cdef int x0, y0
......@@ -71,7 +77,9 @@ Rectangle. By using this declaration, instead of just dynamically assigning to
But Cython offers us more simplicity again, by allowing us to declare
dual-access methods - methods that can be efficiently called at C level, but
can also be accessed from pure Python code at the cost of the Python access
overheads. Consider this code::
overheads. Consider this code:
.. sourcecode:: cython
cdef class Rectangle:
cdef int x0, y0
......@@ -90,7 +98,7 @@ overheads. Consider this code::
rect = Rectangle(x0, y0, x1, y1)
return rect.area()
.. note::
.. Note::
in earlier versions of Cython, the :keyword:`cpdef` keyword is
:keyword:`rdef` - but has the same effect).
......
.. highlight:: cython
Extension Types
===============
Introduction
------------
-------------
As well as creating normal user-defined classes with the Python class
statement, Cython also lets you create new built-in Python types, known as
......@@ -34,7 +35,7 @@ extension types to wrap arbitrary C data structures and provide a Python-like
interface to them.
Attributes
----------
-----------
Attributes of an extension type are stored directly in the object's C struct.
The set of attributes is fixed at compile time; you can't add attributes to an
......
.. highlight:: cython
Interfacing with External C Code
================================
......
.. highlight:: cython
.. _language-basics-label:
Language Basics
......
.. highlight:: cython
.. _cython-limitations-label:
*************
......
.. highlight:: cython
.. _numpy_tute-label:
**************************
......@@ -220,9 +222,7 @@ Adding types
=============
To add types we use custom Cython syntax, so we are now breaking Python source
compatibility. Here's :file:`convolve2.pyx`. *Read the comments!*
.. code-block:: cython
compatibility. Here's :file:`convolve2.pyx`. *Read the comments!* ::
from __future__ import division
import numpy as np
......@@ -339,9 +339,7 @@ not provided then one-dimensional is assumed).
More information on this syntax [:enhancements/buffer:can be found here].
Showing the changes needed to produce :file:`convolve3.pyx` only:
.. sourcecode:: cython
Showing the changes needed to produce :file:`convolve3.pyx` only::
...
def naive_convolve(np.ndarray[DTYPE_t, ndim=2] f, np.ndarray[DTYPE_t, ndim=2] g):
......@@ -374,9 +372,7 @@ The array lookups are still slowed down by two factors:
2. Negative indices are checked for and handled correctly. The code above is
explicitly coded so that it doesn't use negative indices, and it
(hopefully) always access within bounds. We can add a decorator to disable
bounds checking:
.. sourcecode:: cython
bounds checking::
...
cimport cython
......@@ -395,9 +391,7 @@ positive, by casting the variables to unsigned integer types (if you do have
negative values, then this casting will create a very large positive value
instead and you will attempt to access out-of-bounds values). Casting is done
with a special ``<>``-syntax. The code below is changed to use either
unsigned ints or casting as appropriate:
.. sourcecode:: cython
unsigned ints or casting as appropriate::
...
cdef int s, t # changed
......@@ -456,9 +450,7 @@ function call.)
More generic code
==================
It would be possible to do:
.. sourcecode:: cython
It would be possible to do::
def naive_convolve(object[DTYPE_t, ndim=2] f, ...):
......
.. highlight:: cython
.. _overview-label:
********
......
.. highlight:: cython
Differences between Cython and Pyrex
====================================
......
.. highlight:: cython
.. _sharing-declarations-label:
Sharing Declarations Between Cython Modules
......
.. highlight:: cython
.. _compilation_label:
****************************
......
.. highlight:: cython
.. _tutorial_label:
*********
......@@ -107,7 +109,10 @@ Here's a small example showing some of what can be done. It's a routine for
finding prime numbers. You tell it how many primes you want, and it returns
them as a Python list.
:file:`primes.pyx`: ::
:file:`primes.pyx`:
.. sourcecode:: cython
:linenos:
def primes(int kmax):
cdef int n, k, i
......
.. highlight:: cython
.. _wrapping-cplusplus-label:
Wrapping C++ Classes in Cython
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment