Commit e122a152 authored by scoder's avatar scoder

Merge pull request #297 from aidanhs/fix-freeze-demo

Fix 'freeze' demo
parents 0a3154c3 158092a9
...@@ -18,7 +18,7 @@ LDLIBS = $(PY_LDLIBS) ...@@ -18,7 +18,7 @@ LDLIBS = $(PY_LDLIBS)
TARGETS = nCr python TARGETS = nCr python
# List of Cython source files, with main module first. # List of Cython source files, with main module first.
CYTHON_SOURCE = combinatorics.pyx cmath.pyx CYTHON_SOURCE = combinatorics.pyx lcmath.pyx
CYTHON_SECONDARY = $(CYTHON_SOURCE:.pyx=.c) $(TARGETS:=.c) CYTHON_SECONDARY = $(CYTHON_SOURCE:.pyx=.c) $(TARGETS:=.c)
......
...@@ -42,30 +42,30 @@ EXAMPLE ...@@ -42,30 +42,30 @@ EXAMPLE
In the Demos/freeze directory, there exist two Cython modules: In the Demos/freeze directory, there exist two Cython modules:
cmath.pyx lcmath.pyx
A module that interfaces with the -lm library. A module that interfaces with the -lm library.
combinatorics.pyx combinatorics.pyx
A module that implements n-choose-r using cmath. A module that implements n-choose-r using lcmath.
Both modules have the Python idiom ``if __name__ == "__main__"``, which only Both modules have the Python idiom ``if __name__ == "__main__"``, which only
execute if that module is the "main" module. If run as main, cmath prints the execute if that module is the "main" module. If run as main, lcmath prints the
factorial of the argument, while combinatorics prints n-choose-r. factorial of the argument, while combinatorics prints n-choose-r.
The provided Makefile creates an executable, *nCr*, using combinatorics as the The provided Makefile creates an executable, *nCr*, using combinatorics as the
"main" module. It basically performs the following (ignoring the compiler "main" module. It basically performs the following (ignoring the compiler
flags):: flags)::
$ cython_freeze combinatorics cmath > nCr.c $ cython_freeze combinatorics lcmath > nCr.c
$ cython combinatorics.pyx $ cython combinatorics.pyx
$ cython cmath.pyx $ cython lcmath.pyx
$ gcc -c nCr.c $ gcc -c nCr.c
$ gcc -c combinatorics.c $ gcc -c combinatorics.c
$ gcc -c cmath.c $ gcc -c lcmath.c
$ gcc nCr.o combinatorics.o cmath.o -o nCr $ gcc nCr.o combinatorics.o lcmath.o -o nCr
Because the combinatorics module was listed first, its ``__name__`` is set Because the combinatorics module was listed first, its ``__name__`` is set
to ``"__main__"``, while cmath's is set to ``"cmath"``. The executable now to ``"__main__"``, while lcmath's is set to ``"lcmath"``. The executable now
contains a Python interpreter and both Cython modules. :: contains a Python interpreter and both Cython modules. ::
$ ./nCr $ ./nCr
...@@ -81,19 +81,19 @@ linked so you can profile it with gprof. To do this, add the ``--pymain`` ...@@ -81,19 +81,19 @@ linked so you can profile it with gprof. To do this, add the ``--pymain``
flag to ``cython_freeze``. In the Makefile, the *python* executable is built flag to ``cython_freeze``. In the Makefile, the *python* executable is built
like this. :: like this. ::
$ cython_freeze --pymain combinatorics cmath -o python.c $ cython_freeze --pymain combinatorics lcmath -o python.c
$ gcc -c python.c $ gcc -c python.c
$ gcc python.o combinatorics.o cmath.o -o python $ gcc python.o combinatorics.o lcmath.o -o python
Now ``python`` is a normal Python interpreter, but the cmath and combinatorics Now ``python`` is a normal Python interpreter, but the lcmath and combinatorics
modules will be built into the executable. :: modules will be built into the executable. ::
$ ./python $ ./python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2 [GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information. Type "help", "copyright", "credits" or "license" for more information.
>>> import cmath >>> import lcmath
>>> cmath.factorial(155) >>> lcmath.factorial(155)
4.7891429014634364e+273 4.7891429014634364e+273
......
import cmath import lcmath
def nCr(n, r): def nCr(n, r):
"""Return the number of ways to choose r elements of a set of n.""" """Return the number of ways to choose r elements of a set of n."""
return cmath.exp( cmath.lfactorial(n) - cmath.lfactorial(r) return lcmath.exp( lcmath.lfactorial(n) - lcmath.lfactorial(r)
- cmath.lfactorial(n-r) ) - lcmath.lfactorial(n-r) )
if __name__ == "__main__": if __name__ == "__main__":
import sys import sys
......
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