py_vector.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. #include <Eigen/Dense>
  2. #include <Eigen/Sparse>
  3. #include "python.h"
  4. template <typename Type> void init_fixed_from_buffer_3(Type &v, py::buffer &b) {
  5. typedef typename Type::Scalar Scalar;
  6. py::buffer_info info = b.request();
  7. if (info.format != py::format_descriptor<Scalar>::value())
  8. throw std::runtime_error("Incompatible buffer format!");
  9. if (!((info.ndim == 1 && info.strides[0] == sizeof(Scalar)) ||
  10. (info.ndim == 2 &&
  11. ((info.shape[0] == 1 && info.strides[0] == sizeof(Scalar) &&
  12. info.shape[1] == 3) ||
  13. (info.shape[1] == 1 && info.strides[1] == sizeof(Scalar) &&
  14. info.shape[0] == 3)))))
  15. throw std::runtime_error("Incompatible buffer dimension!");
  16. memcpy(v.data(), info.ptr, sizeof(Scalar) * 3);
  17. }
  18. /// Creates Python bindings for an Eigen order-1 tensor of size 3 (i.e. a vector/normal/point)
  19. template <typename Type>
  20. py::class_<Type> bind_eigen_1_3(py::module &m, const char *name,
  21. py::object parent = py::object()) {
  22. typedef typename Type::Scalar Scalar;
  23. py::class_<Type> vector(m, name, parent);
  24. vector
  25. /* Constructors */
  26. .def(py::init<>())
  27. .def(py::init<Scalar>())
  28. .def(py::init<Scalar, Scalar, Scalar>())
  29. .def("__init__", [](Type &v, const std::vector<Scalar> &v2) {
  30. if (v2.size() != 3)
  31. throw std::runtime_error("Incompatible size!");
  32. memcpy(v.data(), &v2[0], sizeof(Scalar) * 3);
  33. })
  34. .def("__init__", [](Type &v, py::buffer b) {
  35. init_fixed_from_buffer_3(v, b);
  36. })
  37. /* Initialization */
  38. .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
  39. .def("setZero", [](Type &m) { m.setZero(); })
  40. /* Arithmetic operators (def_cast forcefully casts the result back to a
  41. Matrix to avoid type issues with Eigen's crazy expression templates) */
  42. .def_cast(-py::self)
  43. .def_cast(py::self + py::self)
  44. .def_cast(py::self - py::self)
  45. .def_cast(py::self * Scalar())
  46. .def_cast(py::self / Scalar())
  47. .def_cast(py::self += py::self)
  48. .def_cast(py::self -= py::self)
  49. .def_cast(py::self *= Scalar())
  50. .def_cast(py::self /= Scalar())
  51. /* Comparison operators */
  52. .def(py::self == py::self)
  53. .def(py::self != py::self)
  54. /* Python protocol implementations */
  55. .def("__len__", [](const Type &) { return (int) 3; })
  56. .def("__repr__", [](const Type &v) {
  57. std::ostringstream oss;
  58. oss << v;
  59. return oss.str();
  60. })
  61. .def("__getitem__", [](const Type &c, int i) {
  62. if (i < 0 || i >= 3)
  63. throw py::index_error();
  64. return c[i];
  65. })
  66. .def("__setitem__", [](Type &c, int i, Scalar v) {
  67. if (i < 0 || i >= 3)
  68. throw py::index_error();
  69. c[i] = v;
  70. })
  71. /* Buffer access for interacting with NumPy */
  72. .def_buffer([](Type &m) -> py::buffer_info {
  73. return py::buffer_info(
  74. m.data(), /* Pointer to buffer */
  75. sizeof(Scalar), /* Size of one scalar */
  76. /* Python struct-style format descriptor */
  77. py::format_descriptor<Scalar>::value(),
  78. 1, { (size_t) 3 },
  79. { sizeof(Scalar) }
  80. );
  81. });
  82. return vector;
  83. }
  84. /// Creates Python bindings for a dynamic Eigen order-1 tensor (i.e. a vector)
  85. template <typename Type>
  86. py::class_<Type> bind_eigen_1(py::module &m, const char *name,
  87. py::object parent = py::object()) {
  88. typedef typename Type::Scalar Scalar;
  89. /* Many Eigen functions are templated and can't easily be referenced using
  90. a function pointer, thus a big portion of the binding code below
  91. instantiates Eigen code using small anonymous wrapper functions */
  92. py::class_<Type> vector(m, name, parent);
  93. vector
  94. /* Constructors */
  95. .def(py::init<>())
  96. .def(py::init<size_t>())
  97. .def("__init__", [](Type &v, const std::vector<Scalar> &v2) {
  98. new (&v) Type(v2.size());
  99. memcpy(v.data(), &v2[0], sizeof(Scalar) * v2.size());
  100. })
  101. .def("__init__", [](Type &v, py::buffer b) {
  102. py::buffer_info info = b.request();
  103. if (info.format != py::format_descriptor<Scalar>::value()) {
  104. throw std::runtime_error("Incompatible buffer format!");
  105. } else if (info.ndim == 1 && info.strides[0] == sizeof(Scalar)) {
  106. new (&v) Type(info.shape[0]);
  107. memcpy(v.data(), info.ptr, sizeof(Scalar) * info.shape[0]);
  108. } else if (info.ndim == 2 && ((info.shape[0] == 1 && info.strides[0] == sizeof(Scalar))
  109. || (info.shape[1] == 1 && info.strides[1] == sizeof(Scalar)))) {
  110. new (&v) Type(info.shape[0] * info.shape[1]);
  111. memcpy(v.data(), info.ptr, sizeof(Scalar) * info.shape[0] * info.shape[1]);
  112. } else {
  113. throw std::runtime_error("Incompatible buffer dimension!");
  114. }
  115. })
  116. /* Size query functions */
  117. .def("size", [](const Type &m) { return m.size(); })
  118. .def("cols", [](const Type &m) { return m.cols(); })
  119. .def("rows", [](const Type &m) { return m.rows(); })
  120. /* Initialization */
  121. .def("setZero", [](Type &m) { m.setZero(); })
  122. .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
  123. /* Resizing */
  124. .def("resize", [](Type &m, size_t s0) { m.resize(s0); })
  125. .def("resizeLike", [](Type &m, const Type &m2) { m.resizeLike(m2); })
  126. .def("conservativeResize", [](Type &m, size_t s0) { m.conservativeResize(s0); })
  127. /* Component-wise operations */
  128. .def("cwiseAbs", &Type::cwiseAbs)
  129. .def("cwiseAbs2", &Type::cwiseAbs2)
  130. .def("cwiseSqrt", &Type::cwiseSqrt)
  131. .def("cwiseInverse", &Type::cwiseInverse)
  132. .def("cwiseMin", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMin(m2); })
  133. .def("cwiseMax", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMax(m2); })
  134. .def("cwiseMin", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMin(s); })
  135. .def("cwiseMax", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMax(s); })
  136. .def("cwiseProduct", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseProduct(m2); })
  137. .def("cwiseQuotient", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseQuotient(m2); })
  138. /* Arithmetic operators (def_cast forcefully casts the result back to a
  139. Type to avoid type issues with Eigen's crazy expression templates) */
  140. .def_cast(-py::self)
  141. .def_cast(py::self + py::self)
  142. .def_cast(py::self - py::self)
  143. .def_cast(py::self * Scalar())
  144. .def_cast(py::self / Scalar())
  145. .def("__rmul__", [](const Type& a, const Scalar& b)
  146. {
  147. return Type(b * a);
  148. })
  149. /* Arithmetic in-place operators */
  150. .def_cast(py::self += py::self)
  151. .def_cast(py::self -= py::self)
  152. .def_cast(py::self *= py::self)
  153. .def_cast(py::self *= Scalar())
  154. .def_cast(py::self /= Scalar())
  155. /* Comparison operators */
  156. .def(py::self == py::self)
  157. .def(py::self != py::self)
  158. /* Python protocol implementations */
  159. .def("__repr__", [](const Type &v) {
  160. std::ostringstream oss;
  161. oss << v.transpose();
  162. return oss.str();
  163. })
  164. .def("__getitem__", [](const Type &m, size_t i) {
  165. if (i >= (size_t) m.size())
  166. throw py::index_error();
  167. return m[i];
  168. })
  169. .def("__setitem__", [](Type &m, size_t i, Scalar v) {
  170. if (i >= (size_t) m.size())
  171. throw py::index_error();
  172. m[i] = v;
  173. })
  174. /* Buffer access for interacting with NumPy */
  175. .def_buffer([](Type &m) -> py::buffer_info {
  176. return py::buffer_info(
  177. m.data(), /* Pointer to buffer */
  178. sizeof(Scalar), /* Size of one scalar */
  179. /* Python struct-style format descriptor */
  180. py::format_descriptor<Scalar>::value(),
  181. 1, /* Number of dimensions */
  182. { (size_t) m.size() }, /* Buffer dimensions */
  183. { sizeof(Scalar) } /* Strides (in bytes) for each index */
  184. );
  185. })
  186. /* Static initializers */
  187. .def_static("Zero", [](size_t n) { return Type(Type::Zero(n)); })
  188. .def_static("Ones", [](size_t n) { return Type(Type::Ones(n)); })
  189. .def_static("Constant", [](size_t n, Scalar value) { return Type(Type::Constant(n, value)); })
  190. .def("MapMatrix", [](const Type& m, size_t r, size_t c)
  191. {
  192. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(Eigen::Map<const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>>(m.data(),r,c));
  193. })
  194. ;
  195. return vector;
  196. }
  197. /// Creates Python bindings for a dynamic Eigen order-2 tensor (i.e. a matrix)
  198. template <typename Type>
  199. py::class_<Type> bind_eigen_2(py::module &m, const char *name,
  200. py::object parent = py::object()) {
  201. typedef typename Type::Scalar Scalar;
  202. /* Many Eigen functions are templated and can't easily be referenced using
  203. a function pointer, thus a big portion of the binding code below
  204. instantiates Eigen code using small anonymous wrapper functions */
  205. py::class_<Type> matrix(m, name, parent);
  206. matrix
  207. /* Constructors */
  208. .def(py::init<>())
  209. .def(py::init<size_t, size_t>())
  210. .def("__init__", [](Type &m, Scalar f) {
  211. new (&m) Type(1, 1);
  212. m(0, 0) = f;
  213. })
  214. .def("__init__", [](Type &m, py::buffer b) {
  215. py::buffer_info info = b.request();
  216. if (info.format != py::format_descriptor<Scalar>::value())
  217. throw std::runtime_error("Incompatible buffer format!");
  218. if (info.ndim == 1) {
  219. new (&m) Type(info.shape[0], 1);
  220. memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  221. } else if (info.ndim == 2) {
  222. if (info.strides[0] == sizeof(Scalar)) {
  223. new (&m) Type(info.shape[0], info.shape[1]);
  224. memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  225. } else {
  226. new (&m) Type(info.shape[1], info.shape[0]);
  227. memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  228. m.transposeInPlace();
  229. }
  230. } else {
  231. throw std::runtime_error("Incompatible buffer dimension!");
  232. }
  233. })
  234. /* Size query functions */
  235. .def("size", [](const Type &m) { return m.size(); })
  236. .def("cols", [](const Type &m) { return m.cols(); })
  237. .def("rows", [](const Type &m) { return m.rows(); })
  238. /* Initialization */
  239. .def("setZero", [](Type &m) { m.setZero(); })
  240. .def("setIdentity", [](Type &m) { m.setIdentity(); })
  241. .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
  242. /* Resizing */
  243. .def("resize", [](Type &m, size_t s0, size_t s1) { m.resize(s0, s1); })
  244. .def("resizeLike", [](Type &m, const Type &m2) { m.resizeLike(m2); })
  245. .def("conservativeResize", [](Type &m, size_t s0, size_t s1) { m.conservativeResize(s0, s1); })
  246. /* Component-wise operations */
  247. .def("cwiseAbs", &Type::cwiseAbs)
  248. .def("cwiseAbs2", &Type::cwiseAbs2)
  249. .def("cwiseSqrt", &Type::cwiseSqrt)
  250. .def("cwiseInverse", &Type::cwiseInverse)
  251. .def("cwiseMin", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMin(m2); })
  252. .def("cwiseMax", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMax(m2); })
  253. .def("cwiseMin", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMin(s); })
  254. .def("cwiseMax", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMax(s); })
  255. .def("cwiseProduct", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseProduct(m2); })
  256. .def("cwiseQuotient", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseQuotient(m2); })
  257. /* Row and column-wise operations */
  258. .def("rowwiseSum", [](const Type &m) {return Type(m.rowwise().sum());} )
  259. .def("rowwiseProd", [](const Type &m) {return Type(m.rowwise().prod());} )
  260. .def("rowwiseMean", [](const Type &m) {return Type(m.rowwise().mean());} )
  261. .def("rowwiseNorm", [](const Type &m) {return Type(m.rowwise().norm());} )
  262. .def("rowwiseMinCoeff", [](const Type &m) {return Type(m.rowwise().minCoeff());} )
  263. .def("rowwiseMaxCoeff", [](const Type &m) {return Type(m.rowwise().maxCoeff());} )
  264. .def("colwiseSum", [](const Type &m) {return Type(m.colwise().sum());} )
  265. .def("colwiseProd", [](const Type &m) {return Type(m.colwise().prod());} )
  266. .def("colwiseMean", [](const Type &m) {return Type(m.colwise().mean());} )
  267. .def("colwiseNorm", [](const Type &m) {return Type(m.colwise().norm());} )
  268. .def("colwiseMinCoeff", [](const Type &m) {return Type(m.colwise().minCoeff());} )
  269. .def("colwiseMaxCoeff", [](const Type &m) {return Type(m.colwise().maxCoeff());} )
  270. /* Arithmetic operators (def_cast forcefully casts the result back to a
  271. Type to avoid type issues with Eigen's crazy expression templates) */
  272. .def_cast(-py::self)
  273. .def_cast(py::self + py::self)
  274. .def_cast(py::self - py::self)
  275. .def_cast(py::self * py::self)
  276. .def_cast(py::self * Scalar())
  277. .def_cast(py::self / Scalar())
  278. .def("__rmul__", [](const Type& a, const Scalar& b)
  279. {
  280. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(b * a);
  281. })
  282. /* Arithmetic in-place operators */
  283. .def_cast(py::self += py::self)
  284. .def_cast(py::self -= py::self)
  285. .def_cast(py::self *= py::self)
  286. .def_cast(py::self *= Scalar())
  287. .def_cast(py::self /= Scalar())
  288. /* Comparison operators */
  289. .def(py::self == py::self)
  290. .def(py::self != py::self)
  291. .def("transposeInPlace", [](Type &m) { m.transposeInPlace(); })
  292. /* Other transformations */
  293. .def("transpose", [](Type &m) -> Type { return m.transpose(); })
  294. /* Python protocol implementations */
  295. .def("__repr__", [](const Type &v) {
  296. std::ostringstream oss;
  297. oss << v;
  298. return oss.str();
  299. })
  300. .def("__getitem__", [](const Type &m, std::pair<size_t, size_t> i) {
  301. if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  302. throw py::index_error();
  303. return m(i.first, i.second);
  304. })
  305. .def("__setitem__", [](Type &m, std::pair<size_t, size_t> i, Scalar v) {
  306. if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  307. throw py::index_error();
  308. m(i.first, i.second) = v;
  309. })
  310. /* Buffer access for interacting with NumPy */
  311. .def_buffer([](Type &m) -> py::buffer_info {
  312. return py::buffer_info(
  313. m.data(), /* Pointer to buffer */
  314. sizeof(Scalar), /* Size of one scalar */
  315. /* Python struct-style format descriptor */
  316. py::format_descriptor<Scalar>::value(),
  317. 2, /* Number of dimensions */
  318. { (size_t) m.rows(), /* Buffer dimensions */
  319. (size_t) m.cols() },
  320. { sizeof(Scalar), /* Strides (in bytes) for each index */
  321. sizeof(Scalar) * m.rows() }
  322. );
  323. })
  324. /* Static initializers */
  325. .def_static("Zero", [](size_t n, size_t m) { return Type(Type::Zero(n, m)); })
  326. .def_static("Ones", [](size_t n, size_t m) { return Type(Type::Ones(n, m)); })
  327. .def_static("Constant", [](size_t n, size_t m, Scalar value) { return Type(Type::Constant(n, m, value)); })
  328. .def_static("Identity", [](size_t n, size_t m) { return Type(Type::Identity(n, m)); })
  329. .def("MapMatrix", [](const Type& m, size_t r, size_t c)
  330. {
  331. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(Eigen::Map<const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>>(m.data(),r,c));
  332. })
  333. ;
  334. return matrix;
  335. }
  336. /// Creates Python bindings for a dynamic Eigen sparse order-2 tensor (i.e. a matrix)
  337. template <typename Type>
  338. py::class_<Type> bind_eigen_sparse_2(py::module &m, const char *name,
  339. py::object parent = py::object()) {
  340. typedef typename Type::Scalar Scalar;
  341. /* Many Eigen functions are templated and can't easily be referenced using
  342. a function pointer, thus a big portion of the binding code below
  343. instantiates Eigen code using small anonymous wrapper functions */
  344. py::class_<Type> matrix(m, name, parent);
  345. matrix
  346. /* Constructors */
  347. .def(py::init<>())
  348. .def(py::init<size_t, size_t>())
  349. // .def("__init__", [](Type &m, Scalar f) {
  350. // new (&m) Type(1, 1);
  351. // m(0, 0) = f;
  352. // })
  353. // .def("__init__", [](Type &m, py::buffer b) {
  354. // py::buffer_info info = b.request();
  355. // if (info.format != py::format_descriptor<Scalar>::value())
  356. // throw std::runtime_error("Incompatible buffer format!");
  357. // if (info.ndim == 1) {
  358. // new (&m) Type(info.shape[0], 1);
  359. // memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  360. // } else if (info.ndim == 2) {
  361. // if (info.strides[0] == sizeof(Scalar)) {
  362. // new (&m) Type(info.shape[0], info.shape[1]);
  363. // memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  364. // } else {
  365. // new (&m) Type(info.shape[1], info.shape[0]);
  366. // memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  367. // m.transposeInPlace();
  368. // }
  369. // } else {
  370. // throw std::runtime_error("Incompatible buffer dimension!");
  371. // }
  372. // })
  373. /* Size query functions */
  374. .def("size", [](const Type &m) { return m.size(); })
  375. .def("cols", [](const Type &m) { return m.cols(); })
  376. .def("rows", [](const Type &m) { return m.rows(); })
  377. /* Initialization */
  378. // .def("setZero", [](Type &m) { m.setZero(); })
  379. // .def("setIdentity", [](Type &m) { m.setIdentity(); })
  380. // .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
  381. /* Resizing */
  382. // .def("resize", [](Type &m, size_t s0, size_t s1) { m.resize(s0, s1); })
  383. // .def("resizeLike", [](Type &m, const Type &m2) { m.resizeLike(m2); })
  384. // .def("conservativeResize", [](Type &m, size_t s0, size_t s1) { m.conservativeResize(s0, s1); })
  385. /* Component-wise operations */
  386. // .def("cwiseAbs", &Type::cwiseAbs)
  387. // .def("cwiseAbs2", &Type::cwiseAbs2)
  388. // .def("cwiseSqrt", &Type::cwiseSqrt)
  389. // .def("cwiseInverse", &Type::cwiseInverse)
  390. // .def("cwiseMin", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMin(m2); })
  391. // .def("cwiseMax", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMax(m2); })
  392. // .def("cwiseMin", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMin(s); })
  393. // .def("cwiseMax", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMax(s); })
  394. // .def("cwiseProduct", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseProduct(m2); })
  395. // .def("cwiseQuotient", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseQuotient(m2); })
  396. /* Arithmetic operators (def_cast forcefully casts the result back to a
  397. Type to avoid type issues with Eigen's crazy expression templates) */
  398. .def_cast(-py::self)
  399. .def_cast(py::self + py::self)
  400. .def_cast(py::self - py::self)
  401. .def_cast(py::self * py::self)
  402. .def_cast(py::self * Scalar())
  403. // Special case, sparse * dense produces a dense matrix
  404. .def("__mul__", []
  405. (const Type &a, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  406. {
  407. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a * b);
  408. })
  409. .def("__rmul__", [](const Type& a, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  410. {
  411. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(b * a);
  412. })
  413. // Special case, sparse * dense vector produces a dense vector
  414. .def("__mul__", []
  415. (const Type &a, const Eigen::Matrix<Scalar,Eigen::Dynamic,1>& b)
  416. {
  417. return Eigen::Matrix<Scalar,Eigen::Dynamic,1>(a * b);
  418. })
  419. .def("__rmul__", [](const Type& a, const Eigen::Matrix<Scalar,1,Eigen::Dynamic>& b)
  420. {
  421. return Eigen::Matrix<Scalar,1,Eigen::Dynamic>(b * a);
  422. })
  423. //.def(py::self * Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>())
  424. .def_cast(py::self / Scalar())
  425. /* Arithmetic in-place operators */
  426. // .def_cast(py::self += py::self)
  427. // .def_cast(py::self -= py::self)
  428. // .def_cast(py::self *= py::self)
  429. // .def_cast(py::self *= Scalar())
  430. // .def_cast(py::self /= Scalar())
  431. /* Comparison operators */
  432. // .def(py::self == py::self)
  433. // .def(py::self != py::self)
  434. // .def("transposeInPlace", [](Type &m) { m.transposeInPlace(); })
  435. // /* Other transformations */
  436. // .def("transpose", [](Type &m) -> Type { return m.transpose(); })
  437. /* Python protocol implementations */
  438. .def("__repr__", [](const Type &v) {
  439. std::ostringstream oss;
  440. oss << v;
  441. return oss.str();
  442. })
  443. // .def("__getitem__", [](const Type &m, std::pair<size_t, size_t> i) {
  444. // if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  445. // throw py::index_error();
  446. // return m(i.first, i.second);
  447. // })
  448. // .def("__setitem__", [](Type &m, std::pair<size_t, size_t> i, Scalar v) {
  449. // if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  450. // throw py::index_error();
  451. // m(i.first, i.second) = v;
  452. // })
  453. // /* Buffer access for interacting with NumPy */
  454. // .def_buffer([](Type &m) -> py::buffer_info {
  455. // return py::buffer_info(
  456. // m.data(), /* Pointer to buffer */
  457. // sizeof(Scalar), /* Size of one scalar */
  458. // /* Python struct-style format descriptor */
  459. // py::format_descriptor<Scalar>::value(),
  460. // 2, /* Number of dimensions */
  461. // { (size_t) m.rows(), /* Buffer dimensions */
  462. // (size_t) m.cols() },
  463. // { sizeof(Scalar), /* Strides (in bytes) for each index */
  464. // sizeof(Scalar) * m.rows() }
  465. // );
  466. // })
  467. /* Static initializers */
  468. // .def_static("Zero", [](size_t n, size_t m) { return Type(Type::Zero(n, m)); })
  469. // .def_static("Ones", [](size_t n, size_t m) { return Type(Type::Ones(n, m)); })
  470. // .def_static("Constant", [](size_t n, size_t m, Scalar value) { return Type(Type::Constant(n, m, value)); })
  471. // .def_static("Identity", [](size_t n, size_t m) { return Type(Type::Identity(n, m)); })
  472. .def("toCOO",[](const Type& m)
  473. {
  474. Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> t(m.nonZeros(),3);
  475. int count = 0;
  476. for (int k=0; k<m.outerSize(); ++k)
  477. for (typename Type::InnerIterator it(m,k); it; ++it)
  478. t.row(count++) << it.row(), it.col(), it.value();
  479. return t;
  480. })
  481. .def("fromCOO",[](Type& m, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& t, int rows, int cols)
  482. {
  483. typedef Eigen::Triplet<Scalar> T;
  484. std::vector<T> tripletList;
  485. tripletList.reserve(t.rows());
  486. for(unsigned i=0;i<t.rows();++i)
  487. tripletList.push_back(T(round(t(i,0)),round(t(i,1)),t(i,2)));
  488. if (rows == -1)
  489. rows = t.col(0).maxCoeff()+1;
  490. if (cols == -1)
  491. cols = t.col(1).maxCoeff()+1;
  492. m.resize(rows,cols);
  493. m.setFromTriplets(tripletList.begin(), tripletList.end());
  494. }, py::arg("t"), py::arg("rows") = -1, py::arg("cols") = -1)
  495. ;
  496. return matrix;
  497. }
  498. void python_export_vector(py::module &m) {
  499. py::module me = m.def_submodule(
  500. "eigen", "Wrappers for Eigen types");
  501. /* Bindings for VectorXd */
  502. bind_eigen_1<Eigen::VectorXd> (me, "VectorXd");
  503. py::implicitly_convertible<py::buffer, Eigen::VectorXd>();
  504. py::implicitly_convertible<double, Eigen::VectorXd>();
  505. /* Bindings for VectorXi */
  506. bind_eigen_1<Eigen::VectorXi> (me, "VectorXi");
  507. py::implicitly_convertible<py::buffer, Eigen::VectorXi>();
  508. py::implicitly_convertible<double, Eigen::VectorXi>();
  509. /* Bindings for MatrixXd */
  510. bind_eigen_2<Eigen::MatrixXd> (me, "MatrixXd");
  511. py::implicitly_convertible<py::buffer, Eigen::MatrixXd>();
  512. py::implicitly_convertible<double, Eigen::MatrixXd>();
  513. /* Bindings for MatrixXi */
  514. bind_eigen_2<Eigen::MatrixXi> (me, "MatrixXi");
  515. py::implicitly_convertible<py::buffer, Eigen::MatrixXi>();
  516. py::implicitly_convertible<double, Eigen::MatrixXi>();
  517. /* Bindings for Vector3d */
  518. auto vector3 = bind_eigen_1_3<Eigen::Vector3d>(me, "Vector3d");
  519. vector3
  520. .def("norm", [](const Eigen::Vector3d &v) { return v.norm(); })
  521. .def("squaredNorm", [](const Eigen::Vector3d &v) { return v.squaredNorm(); })
  522. .def("normalize", [](Eigen::Vector3d &v) { v.normalize(); })
  523. .def("normalized", [](const Eigen::Vector3d &v) -> Eigen::Vector3d { return v.normalized(); })
  524. .def("dot", [](const Eigen::Vector3d &v1, const Eigen::Vector3d &v2) { return v1.dot(v2); })
  525. .def("cross", [](const Eigen::Vector3d &v1, const Eigen::Vector3d &v2) -> Eigen::Vector3d { return v1.cross(v2); })
  526. .def_property("x", [](const Eigen::Vector3d &v) -> double { return v.x(); },
  527. [](Eigen::Vector3d &v, double x) { v.x() = x; }, "X coordinate")
  528. .def_property("y", [](const Eigen::Vector3d &v) -> double { return v.y(); },
  529. [](Eigen::Vector3d &v, double y) { v.y() = y; }, "Y coordinate")
  530. .def_property("z", [](const Eigen::Vector3d &v) -> double { return v.z(); },
  531. [](Eigen::Vector3d &v, double z) { v.z() = z; }, "Z coordinate");
  532. py::implicitly_convertible<py::buffer, Eigen::Vector3d>();
  533. py::implicitly_convertible<double, Eigen::Vector3d>();
  534. /* Bindings for SparseMatrix<double> */
  535. bind_eigen_sparse_2< Eigen::SparseMatrix<double> > (me, "SparseMatrixd");
  536. /* Bindings for SparseMatrix<int> */
  537. bind_eigen_sparse_2< Eigen::SparseMatrix<int> > (me, "SparseMatrixi");
  538. }