ufunc.cpp 965 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright Jim Bosch & Ankit Daftery 2010-2012.
  2. // Copyright Stefan Seefeld 2016.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/python/numpy.hpp>
  7. namespace p = boost::python;
  8. namespace np = boost::python::numpy;
  9. struct UnaryCallable
  10. {
  11. typedef double argument_type;
  12. typedef double result_type;
  13. double operator()(double r) const { return r * 2;}
  14. };
  15. struct BinaryCallable
  16. {
  17. typedef double first_argument_type;
  18. typedef double second_argument_type;
  19. typedef double result_type;
  20. double operator()(double a, double b) const { return a * 2 + b * 3;}
  21. };
  22. BOOST_PYTHON_MODULE(ufunc_ext)
  23. {
  24. np::initialize();
  25. p::class_<UnaryCallable>("UnaryCallable")
  26. .def("__call__", np::unary_ufunc<UnaryCallable>::make());
  27. p::class_< BinaryCallable>("BinaryCallable")
  28. .def("__call__", np::binary_ufunc<BinaryCallable>::make());
  29. }