numeric.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef NUMARRAY_DWA2002922_HPP
  6. # define NUMARRAY_DWA2002922_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/tuple.hpp>
  9. # include <boost/python/str.hpp>
  10. # include <boost/preprocessor/iteration/local.hpp>
  11. # include <boost/preprocessor/cat.hpp>
  12. # include <boost/preprocessor/repetition/enum.hpp>
  13. # include <boost/preprocessor/repetition/enum_params.hpp>
  14. # include <boost/preprocessor/repetition/enum_binary_params.hpp>
  15. namespace boost { namespace python { namespace numeric {
  16. class array;
  17. namespace aux
  18. {
  19. struct BOOST_PYTHON_DECL array_base : object
  20. {
  21. # define BOOST_PP_LOCAL_MACRO(n) \
  22. array_base(BOOST_PP_ENUM_PARAMS_Z(1, n, object const& x));
  23. # define BOOST_PP_LOCAL_LIMITS (1, 7)
  24. # include BOOST_PP_LOCAL_ITERATE()
  25. object argmax(long axis=-1);
  26. object argmin(long axis=-1);
  27. object argsort(long axis=-1);
  28. object astype(object const& type = object());
  29. void byteswap();
  30. object copy() const;
  31. object diagonal(long offset = 0, long axis1 = 0, long axis2 = 1) const;
  32. void info() const;
  33. bool is_c_array() const;
  34. bool isbyteswapped() const;
  35. array new_(object type) const;
  36. void sort();
  37. object trace(long offset = 0, long axis1 = 0, long axis2 = 1) const;
  38. object type() const;
  39. char typecode() const;
  40. object factory(
  41. object const& sequence = object()
  42. , object const& typecode = object()
  43. , bool copy = true
  44. , bool savespace = false
  45. , object type = object()
  46. , object shape = object());
  47. object getflat() const;
  48. long getrank() const;
  49. object getshape() const;
  50. bool isaligned() const;
  51. bool iscontiguous() const;
  52. long itemsize() const;
  53. long nelements() const;
  54. object nonzero() const;
  55. void put(object const& indices, object const& values);
  56. void ravel();
  57. object repeat(object const& repeats, long axis=0);
  58. void resize(object const& shape);
  59. void setflat(object const& flat);
  60. void setshape(object const& shape);
  61. void swapaxes(long axis1, long axis2);
  62. object take(object const& sequence, long axis = 0) const;
  63. void tofile(object const& file) const;
  64. str tostring() const;
  65. void transpose(object const& axes = object());
  66. object view() const;
  67. public: // implementation detail - do not touch.
  68. BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array_base, object);
  69. };
  70. struct BOOST_PYTHON_DECL array_object_manager_traits
  71. {
  72. static bool check(PyObject* obj);
  73. static detail::new_non_null_reference adopt(PyObject* obj);
  74. static PyTypeObject const* get_pytype() ;
  75. };
  76. } // namespace aux
  77. class array : public aux::array_base
  78. {
  79. typedef aux::array_base base;
  80. public:
  81. object astype() { return base::astype(); }
  82. template <class Type>
  83. object astype(Type const& type_)
  84. {
  85. return base::astype(object(type_));
  86. }
  87. template <class Type>
  88. array new_(Type const& type_) const
  89. {
  90. return base::new_(object(type_));
  91. }
  92. template <class Sequence>
  93. void resize(Sequence const& x)
  94. {
  95. base::resize(object(x));
  96. }
  97. # define BOOST_PP_LOCAL_MACRO(n) \
  98. void resize(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \
  99. { \
  100. resize(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \
  101. }
  102. # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
  103. # include BOOST_PP_LOCAL_ITERATE()
  104. template <class Sequence>
  105. void setshape(Sequence const& x)
  106. {
  107. base::setshape(object(x));
  108. }
  109. # define BOOST_PP_LOCAL_MACRO(n) \
  110. void setshape(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \
  111. { \
  112. setshape(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \
  113. }
  114. # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
  115. # include BOOST_PP_LOCAL_ITERATE()
  116. template <class Indices, class Values>
  117. void put(Indices const& indices, Values const& values)
  118. {
  119. base::put(object(indices), object(values));
  120. }
  121. template <class Sequence>
  122. object take(Sequence const& sequence, long axis = 0)
  123. {
  124. return base::take(object(sequence), axis);
  125. }
  126. template <class File>
  127. void tofile(File const& f) const
  128. {
  129. base::tofile(object(f));
  130. }
  131. object factory()
  132. {
  133. return base::factory();
  134. }
  135. template <class Sequence>
  136. object factory(Sequence const& sequence)
  137. {
  138. return base::factory(object(sequence));
  139. }
  140. template <class Sequence, class Typecode>
  141. object factory(
  142. Sequence const& sequence
  143. , Typecode const& typecode_
  144. , bool copy = true
  145. , bool savespace = false
  146. )
  147. {
  148. return base::factory(object(sequence), object(typecode_), copy, savespace);
  149. }
  150. template <class Sequence, class Typecode, class Type>
  151. object factory(
  152. Sequence const& sequence
  153. , Typecode const& typecode_
  154. , bool copy
  155. , bool savespace
  156. , Type const& type
  157. )
  158. {
  159. return base::factory(object(sequence), object(typecode_), copy, savespace, object(type));
  160. }
  161. template <class Sequence, class Typecode, class Type, class Shape>
  162. object factory(
  163. Sequence const& sequence
  164. , Typecode const& typecode_
  165. , bool copy
  166. , bool savespace
  167. , Type const& type
  168. , Shape const& shape
  169. )
  170. {
  171. return base::factory(object(sequence), object(typecode_), copy, savespace, object(type), object(shape));
  172. }
  173. # define BOOST_PYTHON_ENUM_AS_OBJECT(z, n, x) object(BOOST_PP_CAT(x,n))
  174. # define BOOST_PP_LOCAL_MACRO(n) \
  175. template <BOOST_PP_ENUM_PARAMS_Z(1, n, class T)> \
  176. explicit array(BOOST_PP_ENUM_BINARY_PARAMS_Z(1, n, T, const& x)) \
  177. : base(BOOST_PP_ENUM_1(n, BOOST_PYTHON_ENUM_AS_OBJECT, x)) \
  178. {}
  179. # define BOOST_PP_LOCAL_LIMITS (1, 7)
  180. # include BOOST_PP_LOCAL_ITERATE()
  181. # undef BOOST_PYTHON_AS_OBJECT
  182. static BOOST_PYTHON_DECL void set_module_and_type(char const* package_name = 0, char const* type_attribute_name = 0);
  183. static BOOST_PYTHON_DECL std::string get_module_name();
  184. public: // implementation detail -- for internal use only
  185. BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array, base);
  186. };
  187. } // namespace boost::python::numeric
  188. namespace converter
  189. {
  190. template <>
  191. struct object_manager_traits< numeric::array >
  192. : numeric::aux::array_object_manager_traits
  193. {
  194. BOOST_STATIC_CONSTANT(bool, is_specialized = true);
  195. };
  196. }
  197. }} // namespace boost::python
  198. #endif // NUMARRAY_DWA2002922_HPP