| 30 | |
| 31 | template< typename TVectorType > |
| 32 | py::class_< TVectorType > CreateVectorInterface(pybind11::module& m, std::string Name ) |
| 33 | { |
| 34 | py::class_< TVectorType, std::shared_ptr<TVectorType> > binder(m,Name.c_str(), py::buffer_protocol()); |
| 35 | binder.def(py::init<>()); |
| 36 | binder.def(py::init<const TVectorType&>()); |
| 37 | |
| 38 | //binder.def(py::init<std::TVectorType& >()) |
| 39 | binder.def("Size", [](const TVectorType& self) |
| 40 | { |
| 41 | return self.size(); |
| 42 | } ); |
| 43 | binder.def("Resize", [](TVectorType& self, const typename TVectorType::size_type new_size) |
| 44 | { |
| 45 | if(self.size() != new_size) self.resize(new_size, false); |
| 46 | } ); |
| 47 | binder.def("__len__", [](const TVectorType& self) |
| 48 | { |
| 49 | return self.size(); |
| 50 | } ); |
| 51 | |
| 52 | //operating on the object itself, +=, -=, *=, etc |
| 53 | binder.def("__iadd__", [](TVectorType& self, const typename TVectorType::value_type scalar) |
| 54 | { |
| 55 | for(unsigned int i=0; i<self.size(); ++i) self[i]+=scalar; |
| 56 | return self; |
| 57 | }, py::is_operator()); |
| 58 | binder.def("__isub__", [](TVectorType& self, const typename TVectorType::value_type scalar) |
| 59 | { |
| 60 | for(unsigned int i=0; i<self.size(); ++i) self[i]-=scalar; |
| 61 | return self; |
| 62 | }, py::is_operator()); |
| 63 | binder.def("__imul__", [](TVectorType& self, const typename TVectorType::value_type scalar) |
| 64 | { |
| 65 | for(unsigned int i=0; i<self.size(); ++i) self[i]*=scalar; |
| 66 | return self; |
| 67 | }, py::is_operator()); |
| 68 | binder.def("__itruediv__", [](TVectorType& self, const typename TVectorType::value_type scalar) |
| 69 | { |
| 70 | for(unsigned int i=0; i<self.size(); ++i) self[i]/=scalar; |
| 71 | return self; |
| 72 | }, py::is_operator()); |
| 73 | |
| 74 | binder.def("__iadd__", [](TVectorType& self, const TVectorType& other_vec) |
| 75 | { |
| 76 | noalias(self) += other_vec; |
| 77 | return self; |
| 78 | }, py::is_operator()); |
| 79 | binder.def("__isub__", [](TVectorType& self, const TVectorType& other_vec) |
| 80 | { |
| 81 | noalias(self) -= other_vec; |
| 82 | return self; |
| 83 | }, py::is_operator()); |
| 84 | |
| 85 | //returning a different object |
| 86 | // binder.def("__add__", [](TVectorType vec1, const double scalar){for(unsigned int i=0; i<vec1.size(); ++i) vec1[i]+=scalar; return vec1;}, py::is_operator()); |
| 87 | // binder.def("__sub__", [](TVectorType vec1, const double scalar){for(unsigned int i=0; i<vec1.size(); ++i) vec1[i]-=scalar; return vec1;}, py::is_operator()); |
| 88 | binder.def("__mul__", [](TVectorType vec1, const typename TVectorType::value_type scalar) |
| 89 | { |
nothing calls this directly
no test coverage detected