123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- #ifndef boost_python_numpy_ufunc_hpp_
- #define boost_python_numpy_ufunc_hpp_
- #include <boost/python.hpp>
- #include <boost/python/numpy/numpy_object_mgr_traits.hpp>
- #include <boost/python/numpy/dtype.hpp>
- #include <boost/python/numpy/ndarray.hpp>
- #include <boost/python/numpy/config.hpp>
- namespace boost { namespace python { namespace numpy {
- class BOOST_NUMPY_DECL multi_iter : public object
- {
- public:
- BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(multi_iter, object);
-
- void next();
-
- bool not_done() const;
-
- char * get_data(int n) const;
-
- int get_nd() const;
-
-
- Py_intptr_t const * get_shape() const;
-
- Py_intptr_t shape(int n) const;
-
- };
- BOOST_NUMPY_DECL multi_iter make_multi_iter(object const & a1);
- BOOST_NUMPY_DECL multi_iter make_multi_iter(object const & a1, object const & a2);
- BOOST_NUMPY_DECL multi_iter make_multi_iter(object const & a1, object const & a2, object const & a3);
- template <typename TUnaryFunctor,
- typename TArgument=typename TUnaryFunctor::argument_type,
- typename TResult=typename TUnaryFunctor::result_type>
- struct unary_ufunc
- {
-
- static object call(TUnaryFunctor & self, object const & input, object const & output)
- {
- dtype in_dtype = dtype::get_builtin<TArgument>();
- dtype out_dtype = dtype::get_builtin<TResult>();
- ndarray in_array = from_object(input, in_dtype, ndarray::ALIGNED);
- ndarray out_array = ! output.is_none() ?
- from_object(output, out_dtype, ndarray::ALIGNED | ndarray::WRITEABLE)
- : zeros(in_array.get_nd(), in_array.get_shape(), out_dtype);
- multi_iter iter = make_multi_iter(in_array, out_array);
- while (iter.not_done())
- {
- TArgument * argument = reinterpret_cast<TArgument*>(iter.get_data(0));
- TResult * result = reinterpret_cast<TResult*>(iter.get_data(1));
- *result = self(*argument);
- iter.next();
- }
- return out_array.scalarize();
- }
-
- static object make()
- {
- return make_function(call, default_call_policies(), (arg("input"), arg("output")=object()));
- }
- };
- template <typename TBinaryFunctor,
- typename TArgument1=typename TBinaryFunctor::first_argument_type,
- typename TArgument2=typename TBinaryFunctor::second_argument_type,
- typename TResult=typename TBinaryFunctor::result_type>
- struct binary_ufunc
- {
- static object
- call(TBinaryFunctor & self, object const & input1, object const & input2,
- object const & output)
- {
- dtype in1_dtype = dtype::get_builtin<TArgument1>();
- dtype in2_dtype = dtype::get_builtin<TArgument2>();
- dtype out_dtype = dtype::get_builtin<TResult>();
- ndarray in1_array = from_object(input1, in1_dtype, ndarray::ALIGNED);
- ndarray in2_array = from_object(input2, in2_dtype, ndarray::ALIGNED);
- multi_iter iter = make_multi_iter(in1_array, in2_array);
- ndarray out_array = !output.is_none()
- ? from_object(output, out_dtype, ndarray::ALIGNED | ndarray::WRITEABLE)
- : zeros(iter.get_nd(), iter.get_shape(), out_dtype);
- iter = make_multi_iter(in1_array, in2_array, out_array);
- while (iter.not_done())
- {
- TArgument1 * argument1 = reinterpret_cast<TArgument1*>(iter.get_data(0));
- TArgument2 * argument2 = reinterpret_cast<TArgument2*>(iter.get_data(1));
- TResult * result = reinterpret_cast<TResult*>(iter.get_data(2));
- *result = self(*argument1, *argument2);
- iter.next();
- }
- return out_array.scalarize();
- }
- static object make()
- {
- return make_function(call, default_call_policies(),
- (arg("input1"), arg("input2"), arg("output")=object()));
- }
- };
- }
- namespace converter
- {
- NUMPY_OBJECT_MANAGER_TRAITS(numpy::multi_iter);
- }}}
- #endif
|