py_vector.cpp 28 KB

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