dtype.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #include <boost/cstdint.hpp>
  8. namespace p = boost::python;
  9. namespace np = boost::python::numpy;
  10. template <typename T>
  11. np::dtype accept(T) {
  12. return np::dtype::get_builtin<T>();
  13. }
  14. BOOST_PYTHON_MODULE(dtype_ext)
  15. {
  16. np::initialize();
  17. // wrap dtype equivalence test, since it isn't available in Python API.
  18. p::def("equivalent", np::equivalent);
  19. // integers, by number of bits
  20. p::def("accept_int8", accept<boost::int8_t>);
  21. p::def("accept_uint8", accept<boost::uint8_t>);
  22. p::def("accept_int16", accept<boost::int16_t>);
  23. p::def("accept_uint16", accept<boost::uint16_t>);
  24. p::def("accept_int32", accept<boost::int32_t>);
  25. p::def("accept_uint32", accept<boost::uint32_t>);
  26. p::def("accept_int64", accept<boost::int64_t>);
  27. p::def("accept_uint64", accept<boost::uint64_t>);
  28. // integers, by C name according to NumPy
  29. p::def("accept_bool_", accept<bool>);
  30. p::def("accept_byte", accept<signed char>);
  31. p::def("accept_ubyte", accept<unsigned char>);
  32. p::def("accept_short", accept<short>);
  33. p::def("accept_ushort", accept<unsigned short>);
  34. p::def("accept_intc", accept<int>);
  35. p::def("accept_uintc", accept<unsigned int>);
  36. // floats and complex
  37. p::def("accept_float32", accept<float>);
  38. p::def("accept_complex64", accept< std::complex<float> >);
  39. p::def("accept_float64", accept<double>);
  40. p::def("accept_complex128", accept< std::complex<double> >);
  41. if (sizeof(long double) > sizeof(double)) {
  42. p::def("accept_longdouble", accept<long double>);
  43. p::def("accept_clongdouble", accept< std::complex<long double> >);
  44. }
  45. }