diff options
author | gabrieldemarmiesse <gabriel.demarmiesse@teraki.com> | 2018-05-26 14:41:16 +0200 |
---|---|---|
committer | gabrieldemarmiesse <gabriel.demarmiesse@teraki.com> | 2018-05-27 14:14:29 +0200 |
commit | f35173fba8614b83d26ce11c9a34af5fc3b3bc71 (patch) | |
tree | 7de1a76a7bcd7eed5aed8ccfc00c8bf39d2e6c4d /docs/examples/tutorial/cython_tutorial | |
parent | 45df48301b0ab5dd35e9d68581b57f15b69dd74f (diff) | |
download | cython-f35173fba8614b83d26ce11c9a34af5fc3b3bc71.tar.gz |
Now all the files in the example directory are tested.
Diffstat (limited to 'docs/examples/tutorial/cython_tutorial')
-rw-r--r-- | docs/examples/tutorial/cython_tutorial/fib.pyx | 10 | ||||
-rw-r--r-- | docs/examples/tutorial/cython_tutorial/primes.py | 19 | ||||
-rw-r--r-- | docs/examples/tutorial/cython_tutorial/primes.pyx | 23 | ||||
-rw-r--r-- | docs/examples/tutorial/cython_tutorial/primes_cpp.pyx | 21 | ||||
-rw-r--r-- | docs/examples/tutorial/cython_tutorial/setup.py | 6 |
5 files changed, 79 insertions, 0 deletions
diff --git a/docs/examples/tutorial/cython_tutorial/fib.pyx b/docs/examples/tutorial/cython_tutorial/fib.pyx new file mode 100644 index 000000000..473719cb3 --- /dev/null +++ b/docs/examples/tutorial/cython_tutorial/fib.pyx @@ -0,0 +1,10 @@ +from __future__ import print_function + +def fib(n): + """Print the Fibonacci series up to n.""" + a, b = 0, 1 + while b < n: + print(b, end=' ') + a, b = b, a + b + + print() diff --git a/docs/examples/tutorial/cython_tutorial/primes.py b/docs/examples/tutorial/cython_tutorial/primes.py new file mode 100644 index 000000000..5e0d32e69 --- /dev/null +++ b/docs/examples/tutorial/cython_tutorial/primes.py @@ -0,0 +1,19 @@ + +def primes(kmax): + result = [] + if kmax > 1000: + kmax = 1000 + + p = [0] * 1000 + k = 0 + n = 2 + while k < kmax: + i = 0 + while i < k and n % p[i] != 0: + i += 1 + if i == k: + p[k] = n + k += 1 + result.append(n) + n += 1 + return result diff --git a/docs/examples/tutorial/cython_tutorial/primes.pyx b/docs/examples/tutorial/cython_tutorial/primes.pyx new file mode 100644 index 000000000..96ecdb59a --- /dev/null +++ b/docs/examples/tutorial/cython_tutorial/primes.pyx @@ -0,0 +1,23 @@ +def primes(int nb_primes): + cdef int n, i, len_p + cdef int p[1000] + if nb_primes > 1000: + nb_primes = 1000 + + len_p = 0 # The current number of elements in p. + n = 2 + while len_p < nb_primes: + # Is n prime? + for i in p[:len_p]: + if n % i == 0: + break + + # If no break occurred in the loop, we have a prime. + else: + p[len_p] = n + len_p += 1 + n += 1 + + # Let's return the result in a python list: + result_as_list = [prime for prime in p[:len_p]] + return result_as_list diff --git a/docs/examples/tutorial/cython_tutorial/primes_cpp.pyx b/docs/examples/tutorial/cython_tutorial/primes_cpp.pyx new file mode 100644 index 000000000..57bfe9cc2 --- /dev/null +++ b/docs/examples/tutorial/cython_tutorial/primes_cpp.pyx @@ -0,0 +1,21 @@ +# distutils: language=c++
+
+from libcpp.vector cimport vector
+
+def primes(unsigned int nb_primes):
+ cdef int n, i
+ cdef vector[int] p
+ p.reserve(nb_primes) # allocate memory for 'nb_primes' elements.
+
+ n = 2
+ while p.size() < nb_primes: # size() for vectors is similar to len()
+ for i in p:
+ if n % i == 0:
+ break
+ else:
+ p.push_back(n) # push_back is similar to append()
+ n += 1
+
+ # Vectors are automatically converted to Python
+ # lists when converted to Python objects.
+ return p
diff --git a/docs/examples/tutorial/cython_tutorial/setup.py b/docs/examples/tutorial/cython_tutorial/setup.py new file mode 100644 index 000000000..302a08e5f --- /dev/null +++ b/docs/examples/tutorial/cython_tutorial/setup.py @@ -0,0 +1,6 @@ +from distutils.core import setup +from Cython.Build import cythonize + +setup( + ext_modules=cythonize("fib.pyx"), +) |