Skip to content

Commit

Permalink
adding cython text
Browse files Browse the repository at this point in the history
  • Loading branch information
derekmolloy committed May 17, 2018
1 parent 25f33b5 commit efb5987
Show file tree
Hide file tree
Showing 9 changed files with 4,000 additions and 0 deletions.
24 changes: 24 additions & 0 deletions chp05/boost/ebb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<string>
#include<boost/python.hpp> // .hpp convention for c++ headers
using namespace std; // just like cpp for source files

namespace exploringbb{ // keep the global namespace clean

string hello(string name) { // e.g., returns "Hello Derek!"
return ("Hello " + name + "!");
}

double integrate(double a, double b, int n) { // same as before
double sum=0, dx = (b-a)/n;
for(int i=0; i<n; i++){ sum += sin((a+i)*dx); }
return sum*dx;
}
}

BOOST_PYTHON_MODULE(ebb){ // the module is called ebb
using namespace boost::python; // require the boost.python namespace
using namespace exploringbb; // bring in custom namespace
def("hello", hello); // make hello() visible to Python
def("integrate", integrate); // make integrate() also visible
}

Binary file added chp05/boost/ebb.so
Binary file not shown.
10 changes: 10 additions & 0 deletions chp05/boost/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/python
# A Python program that calls C program code
import ebb

print "Start of the Python program"
print ebb.hello("Derek")
val = ebb.integrate(0, 3.14159, 1000000)
print "The integral result is: ", val
print "End of the Python program"

11 changes: 11 additions & 0 deletions chp05/cython/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("test", ["test.pyx"])]
setup(
name = 'random number sum application',
cmdclass = {'build_ext' : build_ext },
ext_modules = ext_modules
)

Loading

0 comments on commit efb5987

Please sign in to comment.