#include <boost/python.hpp>
#include <boost/python/numeric.hpp>
using namespace boost::python;
char const* say_yes(){return "YES!!";}
// See if we can invoke array() from C++
object new_array()
{
return numeric::array(make_tuple(
make_tuple(1,2.2,3)
, make_tuple(4,5,6)
, make_tuple(7,8,9)
),'d');
}
// test argument conversion
void take_array(numeric::array x){x[make_tuple(0,0)]= 0.5;}
BOOST_PYTHON_MODULE(libboopy)
{
def("yes", say_yes);
def("new_array", new_array);
def("take_array", take_array);
}
This c++ code can compile
$ cmake .
$ make
with file CMakeLists.txt
INCLUDE_DIRECTORIES( /usr/include/python2.4 )
ADD_LIBRARY(boopy SHARED boopy.cpp )
TARGET_LINK_LIBRARIES(boopy boost_python python2.4 )
Then libboopy.so is genereated. libboopy.so can call from python as the fllowing
def boopy_test():
import sys
import numarray
import libboopy
print libboopy.yes()
x = libboopy.new_array()
print x
libboopy.take_array(x)
print x
z=numarray.array([[0,1],[1,3]], numarray.Float)
print z
libboopy.take_array(z) # note here
print z
if __name__ == '__main__':
print "running..."
import sys
boopy_test()
output is
YES!!
[[ 1. 2.2 3. ]
[ 4. 5. 6. ]
[ 7. 8. 9. ]]
[[ 0.5 2.2 3. ]
[ 4. 5. 6. ]
[ 7. 8. 9. ]]
[[ 0. 1.]
[ 1. 3.]]
[[ 0.5 1. ]
[ 1. 3. ]]
0 件のコメント:
コメントを投稿