1 // Copyright Jim Bosch 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)
7 #ifndef boost_python_numpy_ufunc_hpp_
8 #define boost_python_numpy_ufunc_hpp_
11 * @brief Utilities to create ufunc-like broadcasting functions out of C++ functors.
14 #include <boost/python.hpp>
15 #include <boost/python/numpy/numpy_object_mgr_traits.hpp>
16 #include <boost/python/numpy/dtype.hpp>
17 #include <boost/python/numpy/ndarray.hpp>
19 namespace boost { namespace python { namespace numpy {
22 * @brief A boost.python "object manager" (subclass of object) for PyArray_MultiIter.
24 * multi_iter is a Python object, but a very low-level one. It should generally only be used
25 * in loops of the form:
27 * while (iter.not_done()) {
33 * @todo I can't tell if this type is exposed in Python anywhere; if it is, we should use that name.
34 * It's more dangerous than most object managers, however - maybe it actually belongs in
37 class multi_iter : public object
41 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(multi_iter, object);
43 /// @brief Increment the iterator.
46 /// @brief Check if the iterator is at its end.
47 bool not_done() const;
49 /// @brief Return a pointer to the element of the nth broadcasted array.
50 char * get_data(int n) const;
52 /// @brief Return the number of dimensions of the broadcasted array expression.
55 /// @brief Return the shape of the broadcasted array expression as an array of integers.
56 Py_intptr_t const * get_shape() const;
58 /// @brief Return the shape of the broadcasted array expression in the nth dimension.
59 Py_intptr_t shape(int n) const;
63 /// @brief Construct a multi_iter over a single sequence or scalar object.
64 multi_iter make_multi_iter(object const & a1);
66 /// @brief Construct a multi_iter by broadcasting two objects.
67 multi_iter make_multi_iter(object const & a1, object const & a2);
69 /// @brief Construct a multi_iter by broadcasting three objects.
70 multi_iter make_multi_iter(object const & a1, object const & a2, object const & a3);
73 * @brief Helps wrap a C++ functor taking a single scalar argument as a broadcasting ufunc-like
76 * Typical usage looks like this:
80 * typedef double argument_type;
81 * typedef double result_type;
82 * double operator()(double input) const { return input * M_PI; }
85 * BOOST_PYTHON_MODULE(example)
87 * class_< TimesPI >("TimesPI")
88 * .def("__call__", unary_ufunc<TimesPI>::make());
93 template <typename TUnaryFunctor,
94 typename TArgument=typename TUnaryFunctor::argument_type,
95 typename TResult=typename TUnaryFunctor::result_type>
100 * @brief A C++ function with object arguments that broadcasts its arguments before
101 * passing them to the underlying C++ functor.
103 static object call(TUnaryFunctor & self, object const & input, object const & output)
105 dtype in_dtype = dtype::get_builtin<TArgument>();
106 dtype out_dtype = dtype::get_builtin<TResult>();
107 ndarray in_array = from_object(input, in_dtype, ndarray::ALIGNED);
108 ndarray out_array = (output != object()) ?
109 from_object(output, out_dtype, ndarray::ALIGNED | ndarray::WRITEABLE)
110 : zeros(in_array.get_nd(), in_array.get_shape(), out_dtype);
111 multi_iter iter = make_multi_iter(in_array, out_array);
112 while (iter.not_done())
114 TArgument * argument = reinterpret_cast<TArgument*>(iter.get_data(0));
115 TResult * result = reinterpret_cast<TResult*>(iter.get_data(1));
116 *result = self(*argument);
119 return out_array.scalarize();
123 * @brief Construct a boost.python function object from call() with reasonable keyword names.
125 * Users will often want to specify their own keyword names with the same signature, but this
126 * is a convenient shortcut.
130 return make_function(call, default_call_policies(), (arg("input"), arg("output")=object()));
135 * @brief Helps wrap a C++ functor taking a pair of scalar arguments as a broadcasting ufunc-like
138 * Typical usage looks like this:
142 * typedef double first_argument_type;
143 * typedef double second_argument_type;
144 * typedef double result_type;
145 * double operator()(double input1, double input2) const { return std::cos(input1 + input2); }
148 * BOOST_PYTHON_MODULE(example)
150 * class_< CosSum >("CosSum")
151 * .def("__call__", binary_ufunc<CosSum>::make());
156 template <typename TBinaryFunctor,
157 typename TArgument1=typename TBinaryFunctor::first_argument_type,
158 typename TArgument2=typename TBinaryFunctor::second_argument_type,
159 typename TResult=typename TBinaryFunctor::result_type>
164 call(TBinaryFunctor & self, object const & input1, object const & input2,
165 object const & output)
167 dtype in1_dtype = dtype::get_builtin<TArgument1>();
168 dtype in2_dtype = dtype::get_builtin<TArgument2>();
169 dtype out_dtype = dtype::get_builtin<TResult>();
170 ndarray in1_array = from_object(input1, in1_dtype, ndarray::ALIGNED);
171 ndarray in2_array = from_object(input2, in2_dtype, ndarray::ALIGNED);
172 multi_iter iter = make_multi_iter(in1_array, in2_array);
173 ndarray out_array = (output != object())
174 ? from_object(output, out_dtype, ndarray::ALIGNED | ndarray::WRITEABLE)
175 : zeros(iter.get_nd(), iter.get_shape(), out_dtype);
176 iter = make_multi_iter(in1_array, in2_array, out_array);
177 while (iter.not_done())
179 TArgument1 * argument1 = reinterpret_cast<TArgument1*>(iter.get_data(0));
180 TArgument2 * argument2 = reinterpret_cast<TArgument2*>(iter.get_data(1));
181 TResult * result = reinterpret_cast<TResult*>(iter.get_data(2));
182 *result = self(*argument1, *argument2);
185 return out_array.scalarize();
190 return make_function(call, default_call_policies(),
191 (arg("input1"), arg("input2"), arg("output")=object()));
196 } // namespace boost::python::numpy
201 NUMPY_OBJECT_MANAGER_TRAITS(numpy::multi_iter);
203 }}} // namespace boost::python::converter