Go to the documentation of this file.00001
00002
00003
00004
00005
00014 #include <boost/numpy.hpp>
00015 #include <iostream>
00016
00017 namespace p = boost::python;
00018 namespace np = boost::numpy;
00019
00020
00021
00022
00023
00024 struct UnarySquare
00025 {
00026 typedef double argument_type;
00027 typedef double result_type;
00028
00029 double operator()(double r) const { return r * r;}
00030 };
00031
00032 struct BinarySquare
00033 {
00034 typedef double first_argument_type;
00035 typedef double second_argument_type;
00036 typedef double result_type;
00037
00038 double operator()(double a,double b) const { return (a*a + b*b) ; }
00039 };
00040
00041 int main(int argc, char **argv)
00042 {
00043
00044 Py_Initialize();
00045
00046 np::initialize();
00047
00048 p::object ud = p::class_<UnarySquare, boost::shared_ptr<UnarySquare> >("UnarySquare")
00049 .def("__call__", np::unary_ufunc<UnarySquare>::make());
00050
00051 p::object inst = ud();
00052
00053 std::cout << "Square of unary scalar 1.0 is " << p::extract <char const * > (p::str(inst.attr("__call__")(1.0))) << std::endl ;
00054
00055 int arr[] = {1,2,3,4} ;
00056
00057 np::ndarray demo_array = np::from_data(arr, np::dtype::get_builtin<int>() , p::make_tuple(4), p::make_tuple(4), p::object());
00058
00059 std::cout << "Demo array is " << p::extract <char const * > (p::str(demo_array)) << std::endl ;
00060
00061 p::object result_array = inst.attr("__call__")(demo_array) ;
00062
00063 std::cout << "Square of demo array is " << p::extract <char const * > (p::str(result_array)) << std::endl ;
00064
00065 p::list li ;
00066 li.append(3);
00067 li.append(7);
00068
00069 std::cout << "Demo list is " << p::extract <char const * > (p::str(li)) << std::endl ;
00070
00071 result_array = inst.attr("__call__")(li) ;
00072
00073 std::cout << "Square of demo list is " << p::extract <char const * > (p::str(result_array)) << std::endl ;
00074
00075
00076 ud = p::class_<BinarySquare, boost::shared_ptr<BinarySquare> >("BinarySquare")
00077 .def("__call__", np::binary_ufunc<BinarySquare>::make());
00078
00079 inst = ud();
00080
00081 std::cout << "The two input list for binary ufunc are " << std::endl << p::extract <char const * > (p::str(demo_array)) << std::endl << p::extract <char const * > (p::str(demo_array)) << std::endl ;
00082
00083 result_array = inst.attr("__call__")(demo_array,demo_array) ;
00084 std::cout << "Square of list with binary ufunc is " << p::extract <char const * > (p::str(result_array)) << std::endl ;
00085 }
00086