py_vector.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include <Eigen/Geometry>
  9. #include <Eigen/Dense>
  10. #include <Eigen/Sparse>
  11. #include "../python_shared.h"
  12. /// Creates Python bindings for a dynamic Eigen matrix
  13. template <typename Type>
  14. py::class_<Type> bind_eigen_2(py::module &m, const char *name) {
  15. typedef typename Type::Scalar Scalar;
  16. /* Many Eigen functions are templated and can't easily be referenced using
  17. a function pointer, thus a big portion of the binding code below
  18. instantiates Eigen code using small anonymous wrapper functions */
  19. py::class_<Type> matrix(m, name, py::buffer_protocol());
  20. matrix
  21. /* Constructors */
  22. .def(py::init<>())
  23. .def(py::init<size_t, size_t>())
  24. .def("__init__", [](Type &m, Scalar f) {
  25. new (&m) Type(1, 1);
  26. m(0, 0) = f;
  27. })
  28. .def("__init__", [](Type &m, py::buffer b) {
  29. py::buffer_info info = b.request();
  30. if (info.format != py::format_descriptor<Scalar>::format())
  31. throw std::runtime_error("Incompatible buffer format!");
  32. if (info.ndim == 1) {
  33. new (&m) Type(info.shape[0], 1);
  34. memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  35. } else if (info.ndim == 2) {
  36. if (info.strides[0] == sizeof(Scalar)) {
  37. new (&m) Type(info.shape[0], info.shape[1]);
  38. memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  39. } else {
  40. new (&m) Type(info.shape[1], info.shape[0]);
  41. memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  42. m.transposeInPlace();
  43. }
  44. } else {
  45. throw std::runtime_error("Incompatible buffer dimension!");
  46. }
  47. })
  48. .def("__init__", [](Type &m, std::vector<std::vector< Scalar> >& b) {
  49. if (b.size() == 0)
  50. {
  51. new (&m) Type(0, 0);
  52. return;
  53. }
  54. // Size checks
  55. unsigned rows = b.size();
  56. unsigned cols = b[0].size();
  57. for (unsigned i=0;i<rows;++i)
  58. if (b[i].size() != cols)
  59. throw std::runtime_error("All rows should have the same size!");
  60. new (&m) Type(rows, cols);
  61. m.resize(rows,cols);
  62. for (unsigned i=0;i<rows;++i)
  63. for (unsigned j=0;j<cols;++j)
  64. m(i,j) = b[i][j];
  65. return;
  66. })
  67. .def("__init__", [](Type &m, std::vector<Scalar>& b) {
  68. if (b.size() == 0)
  69. {
  70. new (&m) Type(0, 0);
  71. return;
  72. }
  73. // Size checks
  74. unsigned rows = b.size();
  75. unsigned cols = 1;
  76. new (&m) Type(rows, cols);
  77. m.resize(rows,cols);
  78. for (unsigned i=0;i<rows;++i)
  79. m(i,0) = b[i];
  80. return;
  81. })
  82. /* Size query functions */
  83. .def("size", [](const Type &m) { return m.size(); })
  84. .def("cols", [](const Type &m) { return m.cols(); })
  85. .def("rows", [](const Type &m) { return m.rows(); })
  86. .def("shape", [](const Type &m) { return std::tuple<int,int>(m.rows(), m.cols()); })
  87. /* Extract rows and columns */
  88. .def("col", [](const Type &m, int i) {
  89. if (i<0 || i>=m.cols())
  90. throw std::runtime_error("Column index out of bound.");
  91. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(m.col(i));
  92. })
  93. .def("row", [](const Type &m, int i) {
  94. if (i<0 || i>=m.rows())
  95. throw std::runtime_error("Row index out of bound.");
  96. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(m.row(i));
  97. })
  98. /* Initialization */
  99. .def("setZero", [](Type &m) { m.setZero(); })
  100. .def("setIdentity", [](Type &m) { m.setIdentity(); })
  101. .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
  102. .def("setRandom", [](Type &m) { m.setRandom(); })
  103. .def("setZero", [](Type &m, const int& r, const int& c) { m.setZero(r,c); })
  104. .def("setIdentity", [](Type &m, const int& r, const int& c) { m.setIdentity(r,c); })
  105. .def("setConstant", [](Type &m, const int& r, const int& c, Scalar value) { m.setConstant(r,c,value); })
  106. .def("setRandom", [](Type &m, const int& r, const int& c) { m.setRandom(r,c); })
  107. .def("setCol", [](Type &m, int i, const Type& v) { m.col(i) = v; })
  108. .def("setRow", [](Type &m, int i, const Type& v) { m.row(i) = v; })
  109. .def("setBlock", [](Type &m, int i, int j, int p, int q, const Type& v) { m.block(i,j,p,q) = v; })
  110. .def("block", [](Type &m, int i, int j, int p, int q) { return Type(m.block(i,j,p,q)); })
  111. .def("rightCols", [](Type &m, const int& k) { return Type(m.rightCols(k)); })
  112. .def("leftCols", [](Type &m, const int& k) { return Type(m.leftCols(k)); })
  113. .def("setLeftCols", [](Type &m, const int& k, const Type& v) { return Type(m.leftCols(k) = v); })
  114. .def("setRightCols", [](Type &m, const int& k, const Type& v) { return Type(m.rightCols(k) = v); })
  115. .def("topRows", [](Type &m, const int& k) { return Type(m.topRows(k)); })
  116. .def("bottomRows", [](Type &m, const int& k) { return Type(m.bottomRows(k)); })
  117. .def("setTopRows", [](Type &m, const int& k, const Type& v) { return Type(m.topRows(k) = v); })
  118. .def("setBottomRows", [](Type &m, const int& k, const Type& v) { return Type(m.bottomRows(k) = v); })
  119. .def("topLeftCorner", [](Type &m, const int& p, const int&q) { return Type(m.topLeftCorner(p,q)); })
  120. .def("bottomLeftCorner", [](Type &m, const int& p, const int&q) { return Type(m.bottomLeftCorner(p,q)); })
  121. .def("topRightCorner", [](Type &m, const int& p, const int&q) { return Type(m.topRightCorner(p,q)); })
  122. .def("bottomRightCorner", [](Type &m, const int& p, const int&q) { return Type(m.bottomRightCorner(p,q)); })
  123. /* Resizing */
  124. .def("resize", [](Type &m, size_t s0, size_t s1) { m.resize(s0, s1); })
  125. .def("resizeLike", [](Type &m, const Type &m2) { m.resizeLike(m2); })
  126. .def("conservativeResize", [](Type &m, size_t s0, size_t s1) { m.conservativeResize(s0, s1); })
  127. .def("mean", [](const Type &m) {return m.mean();})
  128. .def("sum", [](const Type &m) {return m.sum();})
  129. .def("prod", [](const Type &m) {return m.prod();})
  130. .def("trace", [](const Type &m) {return m.trace();})
  131. .def("norm", [](const Type &m) {return m.norm();})
  132. .def("squaredNorm", [](const Type &m) {return m.squaredNorm();})
  133. .def("squaredMean", [](const Type &m) {return m.array().square().mean();})
  134. .def("minCoeff", [](const Type &m) {return m.minCoeff();} )
  135. .def("maxCoeff", [](const Type &m) {return m.maxCoeff();} )
  136. .def("castdouble", [](const Type &m) {return Eigen::MatrixXd(m.template cast<double>());})
  137. .def("castint", [](const Type &m) {return Eigen::MatrixXi(m.template cast<int>());})
  138. /* Component-wise operations */
  139. .def("cwiseAbs", &Type::cwiseAbs)
  140. .def("cwiseAbs2", &Type::cwiseAbs2)
  141. .def("cwiseSqrt", &Type::cwiseSqrt)
  142. .def("cwiseInverse", &Type::cwiseInverse)
  143. .def("cwiseMin", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMin(m2); })
  144. .def("cwiseMax", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMax(m2); })
  145. .def("cwiseMin", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMin(s); })
  146. .def("cwiseMax", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMax(s); })
  147. .def("cwiseProduct", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseProduct(m2); })
  148. .def("cwiseQuotient", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseQuotient(m2); })
  149. /* Row and column-wise operations */
  150. .def("rowwiseSet", [](Type &m, const Type &m2) {return Type(m.rowwise() = Eigen::Matrix<Scalar, 1, Eigen::Dynamic>(m2));} )
  151. .def("rowwiseSum", [](const Type &m) {return Type(m.rowwise().sum());} )
  152. .def("rowwiseProd", [](const Type &m) {return Type(m.rowwise().prod());} )
  153. .def("rowwiseMean", [](const Type &m) {return Type(m.rowwise().mean());} )
  154. .def("rowwiseNorm", [](const Type &m) {return Type(m.rowwise().norm());} )
  155. .def("rowwiseNormalized", [](const Type &m) {return Type(m.rowwise().normalized());} )
  156. .def("rowwiseReverse", [](const Type &m) {return Type(m.rowwise().reverse());} )
  157. .def("rowwiseMinCoeff", [](const Type &m) {return Type(m.rowwise().minCoeff());} )
  158. .def("rowwiseMaxCoeff", [](const Type &m) {return Type(m.rowwise().maxCoeff());} )
  159. .def("colwiseSet", [](Type &m, const Type &m2) {return Type(m.colwise() = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>(m2));} )
  160. .def("colwiseSum", [](const Type &m) {return Type(m.colwise().sum());} )
  161. .def("colwiseProd", [](const Type &m) {return Type(m.colwise().prod());} )
  162. .def("colwiseMean", [](const Type &m) {return Type(m.colwise().mean());} )
  163. .def("colwiseNorm", [](const Type &m) {return Type(m.colwise().norm());} )
  164. .def("colwiseNormalized", [](const Type &m) {return Type(m.colwise().normalized());} )
  165. .def("colwiseReverse", [](const Type &m) {return Type(m.colwise().reverse());} )
  166. .def("colwiseMinCoeff", [](const Type &m) {return Type(m.colwise().minCoeff());} )
  167. .def("colwiseMaxCoeff", [](const Type &m) {return Type(m.colwise().maxCoeff());} )
  168. .def("replicate", [](const Type &m, const int& r, const int& c) {return Type(m.replicate(r,c));} )
  169. .def("asDiagonal", [](const Type &m) {return Eigen::DiagonalMatrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(m.asDiagonal());} )
  170. .def("sparseView", [](Type &m) { return Eigen::SparseMatrix<Scalar>(m.sparseView()); })
  171. /* Arithmetic operators (def_cast forcefully casts the result back to a
  172. Type to avoid type issues with Eigen's crazy expression templates) */
  173. .def_cast(-py::self)
  174. .def_cast(py::self + py::self)
  175. .def_cast(py::self - py::self)
  176. .def_cast(py::self * py::self)
  177. // .def_cast(py::self - Scalar())
  178. // .def_cast(py::self * Scalar())
  179. // .def_cast(py::self / Scalar())
  180. .def("__mul__", []
  181. (const Type &a, const Scalar& b)
  182. {
  183. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a * b);
  184. })
  185. .def("__rmul__", [](const Type& a, const Scalar& b)
  186. {
  187. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(b * a);
  188. })
  189. .def("__add__", []
  190. (const Type &a, const Scalar& b)
  191. {
  192. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a.array() + b);
  193. })
  194. .def("__radd__", [](const Type& a, const Scalar& b)
  195. {
  196. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(b + a.array());
  197. })
  198. .def("__sub__", []
  199. (const Type &a, const Scalar& b)
  200. {
  201. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a.array() - b);
  202. })
  203. .def("__rsub__", [](const Type& a, const Scalar& b)
  204. {
  205. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(b - a.array());
  206. })
  207. .def("__div__", []
  208. (const Type &a, const Scalar& b)
  209. {
  210. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a / b);
  211. })
  212. .def("__truediv__", []
  213. (const Type &a, const Scalar& b)
  214. {
  215. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a / b);
  216. })
  217. /* Arithmetic in-place operators */
  218. .def_cast(py::self += py::self)
  219. .def_cast(py::self -= py::self)
  220. .def_cast(py::self *= py::self)
  221. .def_cast(py::self *= Scalar())
  222. .def_cast(py::self /= Scalar())
  223. /* Comparison operators */
  224. .def(py::self == py::self)
  225. .def(py::self != py::self)
  226. .def("__lt__", []
  227. (const Type &a, const Scalar& b) -> Eigen::Matrix<bool,Eigen::Dynamic,Eigen::Dynamic>
  228. {
  229. return Eigen::Matrix<bool, Eigen::Dynamic, Eigen::Dynamic>(a.array() < b);
  230. })
  231. .def("__gt__", []
  232. (const Type &a, const Scalar& b) -> Eigen::Matrix<bool,Eigen::Dynamic,Eigen::Dynamic>
  233. {
  234. return Eigen::Matrix<bool, Eigen::Dynamic, Eigen::Dynamic>(a.array() > b);
  235. })
  236. .def("__le__", []
  237. (const Type &a, const Scalar& b) -> Eigen::Matrix<bool,Eigen::Dynamic,Eigen::Dynamic>
  238. {
  239. return Eigen::Matrix<bool, Eigen::Dynamic, Eigen::Dynamic>(a.array() <= b);
  240. })
  241. .def("__ge__", []
  242. (const Type &a, const Scalar& b) -> Eigen::Matrix<bool,Eigen::Dynamic,Eigen::Dynamic>
  243. {
  244. return Eigen::Matrix<bool, Eigen::Dynamic, Eigen::Dynamic>(a.array() >= b);
  245. })
  246. .def("transposeInPlace", [](Type &m) { m.transposeInPlace(); })
  247. /* Other transformations */
  248. .def("transpose", [](Type &m) -> Type { return m.transpose(); })
  249. /* Python protocol implementations */
  250. .def("__repr__", [](const Type &v) {
  251. std::ostringstream oss;
  252. oss << v;
  253. return oss.str();
  254. })
  255. .def("__getitem__", [](const Type &m, std::pair<size_t, size_t> i) {
  256. if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  257. throw py::index_error();
  258. return m(i.first, i.second);
  259. })
  260. .def("__setitem__", [](Type &m, std::pair<size_t, size_t> i, Scalar v) {
  261. if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  262. throw py::index_error();
  263. m(i.first, i.second) = v;
  264. })
  265. .def("__getitem__", [](const Type &m, size_t i) {
  266. if (i >= (size_t) m.size())
  267. throw py::index_error();
  268. return m(i);
  269. })
  270. .def("__setitem__", [](Type &m, size_t i, Scalar v) {
  271. if (i >= (size_t) m.size())
  272. throw py::index_error();
  273. m(i) = v;
  274. })
  275. /* Buffer access for interacting with NumPy */
  276. .def_buffer([](Type &m) -> py::buffer_info {
  277. return py::buffer_info(
  278. m.data(), /* Pointer to buffer */
  279. sizeof(Scalar), /* Size of one scalar */
  280. /* Python struct-style format descriptor */
  281. py::format_descriptor<Scalar>::format(),
  282. 2, /* Number of dimensions */
  283. { (size_t) m.rows(), /* Buffer dimensions */
  284. (size_t) m.cols() },
  285. { sizeof(Scalar), /* Strides (in bytes) for each index */
  286. sizeof(Scalar) * m.rows() }
  287. );
  288. })
  289. /* Static initializers */
  290. .def_static("Zero", [](size_t n, size_t m) { return Type(Type::Zero(n, m)); })
  291. .def_static("Random", [](size_t n, size_t m) { return Type(Type::Random(n, m)); })
  292. .def_static("Ones", [](size_t n, size_t m) { return Type(Type::Ones(n, m)); })
  293. .def_static("Constant", [](size_t n, size_t m, Scalar value) { return Type(Type::Constant(n, m, value)); })
  294. .def_static("Identity", [](size_t n, size_t m) { return Type(Type::Identity(n, m)); })
  295. .def("MapMatrix", [](const Type& m, size_t r, size_t c)
  296. {
  297. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(Eigen::Map<const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>>(m.data(),r,c));
  298. })
  299. .def("copy", [](const Type &m) { return Type(m); })
  300. ;
  301. return matrix;
  302. }
  303. /// Creates Python bindings for a dynamic Eigen sparse order-2 tensor (i.e. a matrix)
  304. template <typename Type>
  305. py::class_<Type> bind_eigen_sparse_2(py::module &m, const char *name) {
  306. typedef typename Type::Scalar Scalar;
  307. /* Many Eigen functions are templated and can't easily be referenced using
  308. a function pointer, thus a big portion of the binding code below
  309. instantiates Eigen code using small anonymous wrapper functions */
  310. py::class_<Type> matrix(m, name, py::buffer_protocol());
  311. matrix
  312. /* Constructors */
  313. .def(py::init<>())
  314. .def(py::init<size_t, size_t>())
  315. // .def("__init__", [](Type &m, Scalar f) {
  316. // new (&m) Type(1, 1);
  317. // m(0, 0) = f;
  318. // })
  319. // .def("__init__", [](Type &m, py::buffer b) {
  320. // py::buffer_info info = b.request();
  321. // if (info.format != py::format_descriptor<Scalar>::value())
  322. // throw std::runtime_error("Incompatible buffer format!");
  323. // if (info.ndim == 1) {
  324. // new (&m) Type(info.shape[0], 1);
  325. // memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  326. // } else if (info.ndim == 2) {
  327. // if (info.strides[0] == sizeof(Scalar)) {
  328. // new (&m) Type(info.shape[0], info.shape[1]);
  329. // memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  330. // } else {
  331. // new (&m) Type(info.shape[1], info.shape[0]);
  332. // memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  333. // m.transposeInPlace();
  334. // }
  335. // } else {
  336. // throw std::runtime_error("Incompatible buffer dimension!");
  337. // }
  338. // })
  339. /* Size query functions */
  340. .def("size", [](const Type &m) { return m.size(); })
  341. .def("cols", [](const Type &m) { return m.cols(); })
  342. .def("rows", [](const Type &m) { return m.rows(); })
  343. .def("shape", [](const Type &m) { return std::tuple<int,int>(m.rows(), m.cols()); })
  344. /* Initialization */
  345. .def("setZero", [](Type &m) { m.setZero(); })
  346. .def("setIdentity", [](Type &m) { m.setIdentity(); })
  347. .def("transpose", [](Type &m) { return Type(m.transpose()); })
  348. .def("norm", [](Type &m) { return m.norm(); })
  349. /* Resizing */
  350. // .def("resize", [](Type &m, size_t s0, size_t s1) { m.resize(s0, s1); })
  351. // .def("resizeLike", [](Type &m, const Type &m2) { m.resizeLike(m2); })
  352. // .def("conservativeResize", [](Type &m, size_t s0, size_t s1) { m.conservativeResize(s0, s1); })
  353. /* Component-wise operations */
  354. // .def("cwiseAbs", &Type::cwiseAbs)
  355. // .def("cwiseAbs2", &Type::cwiseAbs2)
  356. // .def("cwiseSqrt", &Type::cwiseSqrt)
  357. // .def("cwiseInverse", &Type::cwiseInverse)
  358. // .def("cwiseMin", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMin(m2); })
  359. // .def("cwiseMax", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMax(m2); })
  360. // .def("cwiseMin", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMin(s); })
  361. // .def("cwiseMax", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMax(s); })
  362. // .def("cwiseProduct", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseProduct(m2); })
  363. // .def("cwiseQuotient", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseQuotient(m2); })
  364. /* Arithmetic operators (def_cast forcefully casts the result back to a
  365. Type to avoid type issues with Eigen's crazy expression templates) */
  366. .def_cast(-py::self)
  367. .def_cast(py::self + py::self)
  368. .def_cast(py::self - py::self)
  369. .def_cast(py::self * py::self)
  370. .def_cast(py::self * Scalar())
  371. .def_cast(Scalar() * py::self)
  372. // Special case, sparse * dense produces a dense matrix
  373. // .def("__mul__", []
  374. // (const Type &a, const Scalar& b)
  375. // {
  376. // return Type(a * b);
  377. // })
  378. // .def("__rmul__", [](const Type& a, const Scalar& b)
  379. // {
  380. // return Type(b * a);
  381. // })
  382. .def("__mul__", []
  383. (const Type &a, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  384. {
  385. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a * b);
  386. })
  387. .def("__rmul__", [](const Type& a, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  388. {
  389. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(b * a);
  390. })
  391. .def("__mul__", []
  392. (const Type &a, const Eigen::DiagonalMatrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  393. {
  394. return Type(a * b);
  395. })
  396. .def("__rmul__", [](const Type& a, const Eigen::DiagonalMatrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  397. {
  398. return Type(b * a);
  399. })
  400. //.def(py::self * Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>())
  401. // .def_cast(py::self / Scalar())
  402. /* Arithmetic in-place operators */
  403. // .def_cast(py::self += py::self)
  404. // .def_cast(py::self -= py::self)
  405. // .def_cast(py::self *= py::self)
  406. // .def_cast(py::self *= Scalar())
  407. // .def_cast(py::self /= Scalar())
  408. /* Comparison operators */
  409. // .def(py::self == py::self)
  410. // .def(py::self != py::self)
  411. // .def("transposeInPlace", [](Type &m) { m.transposeInPlace(); })
  412. // /* Other transformations */
  413. // .def("transpose", [](Type &m) -> Type { return m.transpose(); })
  414. /* Python protocol implementations */
  415. .def("__repr__", [](const Type &v) {
  416. std::ostringstream oss;
  417. oss << v;
  418. return oss.str();
  419. })
  420. /* Static initializers */
  421. // .def_static("Zero", [](size_t n, size_t m) { return Type(Type::Zero(n, m)); })
  422. // .def_static("Ones", [](size_t n, size_t m) { return Type(Type::Ones(n, m)); })
  423. // .def_static("Constant", [](size_t n, size_t m, Scalar value) { return Type(Type::Constant(n, m, value)); })
  424. // .def_static("Identity", [](size_t n, size_t m) { return Type(Type::Identity(n, m)); })
  425. .def("toCOO",[](const Type& m)
  426. {
  427. Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> t(m.nonZeros(),3);
  428. int count = 0;
  429. for (int k=0; k<m.outerSize(); ++k)
  430. for (typename Type::InnerIterator it(m,k); it; ++it)
  431. t.row(count++) << it.row(), it.col(), it.value();
  432. return t;
  433. })
  434. .def("fromCOO",[](Type& m, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& t, int rows, int cols)
  435. {
  436. typedef Eigen::Triplet<Scalar> T;
  437. std::vector<T> tripletList;
  438. tripletList.reserve(t.rows());
  439. for(unsigned i=0;i<t.rows();++i)
  440. tripletList.push_back(T(round(t(i,0)),round(t(i,1)),t(i,2)));
  441. if (rows == -1)
  442. rows = t.col(0).maxCoeff()+1;
  443. if (cols == -1)
  444. cols = t.col(1).maxCoeff()+1;
  445. m.resize(rows,cols);
  446. m.setFromTriplets(tripletList.begin(), tripletList.end());
  447. }, py::arg("t"), py::arg("rows") = -1, py::arg("cols") = -1)
  448. .def("insert",[](Type& m, const int row, const int col, const Scalar value)
  449. {
  450. return m.insert(row,col) = value;
  451. }, py::arg("row"), py::arg("col"), py::arg("value"))
  452. .def("makeCompressed",[](Type& m)
  453. {
  454. return m.makeCompressed();
  455. })
  456. .def("diagonal", [](const Type &m) {return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(m.diagonal());} )
  457. ;
  458. return matrix;
  459. }
  460. /// Creates Python bindings for a diagonal Eigen sparse order-2 tensor (i.e. a matrix)
  461. template <typename Type>
  462. py::class_<Type> bind_eigen_diagonal_2(py::module &m, const char *name) {
  463. typedef typename Type::Scalar Scalar;
  464. /* Many Eigen functions are templated and can't easily be referenced using
  465. a function pointer, thus a big portion of the binding code below
  466. instantiates Eigen code using small anonymous wrapper functions */
  467. py::class_<Type> matrix(m, name, py::buffer_protocol());
  468. matrix
  469. /* Constructors */
  470. .def(py::init<>())
  471. //.def(py::init<size_t, size_t>())
  472. /* Size query functions */
  473. .def("size", [](const Type &m) { return m.size(); })
  474. .def("cols", [](const Type &m) { return m.cols(); })
  475. .def("rows", [](const Type &m) { return m.rows(); })
  476. .def("shape", [](const Type &m) { return std::tuple<int,int>(m.rows(), m.cols()); })
  477. /* Initialization */
  478. .def("setZero", [](Type &m) { m.setZero(); })
  479. .def("setIdentity", [](Type &m) { m.setIdentity(); })
  480. /* Arithmetic operators (def_cast forcefully casts the result back to a
  481. Type to avoid type issues with Eigen's crazy expression templates) */
  482. // .def_cast(-py::self)
  483. // .def_cast(py::self + py::self)
  484. // .def_cast(py::self - py::self)
  485. // .def_cast(py::self * py::self)
  486. .def_cast(py::self * Scalar())
  487. .def_cast(Scalar() * py::self)
  488. // // Special case, sparse * dense produces a dense matrix
  489. // .def("__mul__", []
  490. // (const Type &a, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  491. // {
  492. // return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a * b);
  493. // })
  494. // .def("__rmul__", [](const Type& a, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  495. // {
  496. // return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(b * a);
  497. // })
  498. .def("__mul__", []
  499. (const Type &a, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  500. {
  501. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a * b);
  502. })
  503. .def("__rmul__", [](const Type& a, const Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& b)
  504. {
  505. return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(b * a);
  506. })
  507. .def("__mul__", []
  508. (const Type &a, const Eigen::SparseMatrix<Scalar>& b)
  509. {
  510. return Eigen::SparseMatrix<Scalar>(a * b);
  511. })
  512. .def("__rmul__", [](const Type& a, const Eigen::SparseMatrix<Scalar>& b)
  513. {
  514. return Eigen::SparseMatrix<Scalar>(b * a);
  515. })
  516. /* Python protocol implementations */
  517. .def("__repr__", [](const Type &/*v*/) {
  518. std::ostringstream oss;
  519. oss << "<< operator undefined for diagonal matrices";
  520. return oss.str();
  521. })
  522. /* Other transformations */
  523. ;
  524. return matrix;
  525. }
  526. void python_export_vector(py::module &m) {
  527. py::module me = m.def_submodule(
  528. "eigen", "Wrappers for Eigen types");
  529. /* Bindings for VectorXd */
  530. // bind_eigen_1<Eigen::VectorXd> (me, "VectorXd");
  531. // py::implicitly_convertible<py::buffer, Eigen::VectorXd>();
  532. // py::implicitly_convertible<double, Eigen::VectorXd>();
  533. /* Bindings for VectorXi */
  534. // bind_eigen_1<Eigen::VectorXi> (me, "VectorXi");
  535. // py::implicitly_convertible<py::buffer, Eigen::VectorXi>();
  536. // py::implicitly_convertible<double, Eigen::VectorXi>();
  537. /* Bindings for MatrixXd */
  538. bind_eigen_2<Eigen::MatrixXd> (me, "MatrixXd");
  539. //py::implicitly_convertible<py::buffer, Eigen::MatrixXd>();
  540. //py::implicitly_convertible<double, Eigen::MatrixXd>();
  541. /* Bindings for MatrixXi */
  542. bind_eigen_2<Eigen::MatrixXi> (me, "MatrixXi");
  543. //py::implicitly_convertible<py::buffer, Eigen::MatrixXi>();
  544. //py::implicitly_convertible<double, Eigen::MatrixXi>();
  545. /* Bindings for MatrixXb */
  546. #if EIGEN_VERSION_AT_LEAST(3,3,0)
  547. // Temporarily disabled with Eigen 3.3
  548. #else
  549. bind_eigen_2<Eigen::Matrix<bool,Eigen::Dynamic,Eigen::Dynamic> > (me, "MatrixXb");
  550. #endif
  551. /* Bindings for MatrixXuc */
  552. bind_eigen_2<Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> > (me, "MatrixXuc");
  553. // py::implicitly_convertible<py::buffer, Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> >();
  554. // py::implicitly_convertible<double, Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> >();
  555. // /* Bindings for Vector3d */
  556. // auto vector3 = bind_eigen_1_3<Eigen::Vector3d>(me, "Vector3d");
  557. // vector3
  558. // .def("norm", [](const Eigen::Vector3d &v) { return v.norm(); })
  559. // .def("squaredNorm", [](const Eigen::Vector3d &v) { return v.squaredNorm(); })
  560. // .def("normalize", [](Eigen::Vector3d &v) { v.normalize(); })
  561. // .def("normalized", [](const Eigen::Vector3d &v) -> Eigen::Vector3d { return v.normalized(); })
  562. // .def("dot", [](const Eigen::Vector3d &v1, const Eigen::Vector3d &v2) { return v1.dot(v2); })
  563. // .def("cross", [](const Eigen::Vector3d &v1, const Eigen::Vector3d &v2) -> Eigen::Vector3d { return v1.cross(v2); })
  564. // .def_property("x", [](const Eigen::Vector3d &v) -> double { return v.x(); },
  565. // [](Eigen::Vector3d &v, double x) { v.x() = x; }, "X coordinate")
  566. // .def_property("y", [](const Eigen::Vector3d &v) -> double { return v.y(); },
  567. // [](Eigen::Vector3d &v, double y) { v.y() = y; }, "Y coordinate")
  568. // .def_property("z", [](const Eigen::Vector3d &v) -> double { return v.z(); },
  569. // [](Eigen::Vector3d &v, double z) { v.z() = z; }, "Z coordinate");
  570. //
  571. // py::implicitly_convertible<py::buffer, Eigen::Vector3d>();
  572. // py::implicitly_convertible<double, Eigen::Vector3d>();
  573. /* Bindings for SparseMatrix<double> */
  574. bind_eigen_sparse_2< Eigen::SparseMatrix<double> > (me, "SparseMatrixd");
  575. /* Bindings for SparseMatrix<int> */
  576. bind_eigen_sparse_2< Eigen::SparseMatrix<int> > (me, "SparseMatrixi");
  577. /* Bindings for DiagonalMatrix<double> */
  578. bind_eigen_diagonal_2< Eigen::DiagonalMatrix<double,Eigen::Dynamic,Eigen::Dynamic> > (me, "DiagonalMatrixd");
  579. /* Bindings for DiagonalMatrix<int> */
  580. bind_eigen_diagonal_2< Eigen::DiagonalMatrix<int,Eigen::Dynamic,Eigen::Dynamic> > (me, "DiagonalMatrixi");
  581. /* Bindings for SimplicialLLT*/
  582. py::class_<Eigen::SimplicialLLT<Eigen::SparseMatrix<double > >> simpliciallltsparse(me, "SimplicialLLTsparse");
  583. simpliciallltsparse
  584. .def(py::init<>())
  585. .def(py::init<Eigen::SparseMatrix<double>>())
  586. .def("info",[](const Eigen::SimplicialLLT<Eigen::SparseMatrix<double > >& s)
  587. {
  588. if (s.info() == Eigen::Success)
  589. return "Success";
  590. else
  591. return "Numerical Issue";
  592. })
  593. .def("analyzePattern",[](Eigen::SimplicialLLT<Eigen::SparseMatrix<double > >& s, const Eigen::SparseMatrix<double>& a) { return s.analyzePattern(a); })
  594. .def("factorize",[](Eigen::SimplicialLLT<Eigen::SparseMatrix<double > >& s, const Eigen::SparseMatrix<double>& a) { return s.factorize(a); })
  595. .def("solve",[](const Eigen::SimplicialLLT<Eigen::SparseMatrix<double > >& s, const Eigen::MatrixXd& rhs) { return Eigen::MatrixXd(s.solve(rhs)); })
  596. ;
  597. // Bindings for Affine3d
  598. py::class_<Eigen::Affine3d > affine3d(me, "Affine3d");
  599. affine3d
  600. .def(py::init<>())
  601. .def_static("Identity", []() { return Eigen::Affine3d::Identity(); })
  602. .def("setIdentity",[](Eigen::Affine3d& a){
  603. return a.setIdentity();
  604. })
  605. .def("rotate",[](Eigen::Affine3d& a, double angle, Eigen::MatrixXd axis) {
  606. assert_is_Vector3("axis", axis);
  607. return a.rotate(Eigen::AngleAxisd(angle, Eigen::Vector3d(axis)));
  608. })
  609. .def("rotate",[](Eigen::Affine3d& a, Eigen::Quaterniond quat) {
  610. return a.rotate(quat);
  611. })
  612. .def("translate",[](Eigen::Affine3d& a, Eigen::MatrixXd offset) {
  613. assert_is_Vector3("offset", offset);
  614. return a.translate(Eigen::Vector3d(offset));
  615. })
  616. .def("matrix", [](Eigen::Affine3d& a) -> Eigen::MatrixXd {
  617. return Eigen::MatrixXd(a.matrix());
  618. })
  619. ;
  620. // Bindings for Quaterniond
  621. py::class_<Eigen::Quaterniond > quaterniond(me, "Quaterniond");
  622. quaterniond
  623. .def(py::init<>())
  624. .def(py::init<double, double, double, double>())
  625. .def("__init__", [](Eigen::Quaterniond &q, double angle, Eigen::MatrixXd axis) {
  626. assert_is_Vector3("axis", axis);
  627. new (&q) Eigen::Quaterniond(Eigen::AngleAxisd(angle, Eigen::Vector3d(axis)));
  628. })
  629. .def_static("Identity", []() { return Eigen::Quaterniond::Identity(); })
  630. .def("__repr__", [](const Eigen::Quaterniond &v) {
  631. std::ostringstream oss;
  632. oss << "(" << v.w() << ", " << v.x() << ", " << v.y() << ", " << v.z() << ")";
  633. return oss.str();
  634. })
  635. .def("conjugate",[](Eigen::Quaterniond& q) {
  636. return q.conjugate();
  637. })
  638. .def("normalize",[](Eigen::Quaterniond& q) {
  639. return q.normalize();
  640. })
  641. .def("slerp",[](Eigen::Quaterniond& q, double & t, Eigen::Quaterniond other) {
  642. return q.slerp(t, other);
  643. })
  644. // .def_cast(-py::self)
  645. // .def_cast(py::self + py::self)
  646. // .def_cast(py::self - py::self)
  647. .def_cast(py::self * py::self)
  648. // .def_cast(py::self - Scalar())
  649. // .def_cast(py::self * Scalar())
  650. // .def_cast(py::self / Scalar())
  651. // .def("__mul__", []
  652. // (const Type &a, const Scalar& b)
  653. // {
  654. // return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(a * b);
  655. // })
  656. // .def("__rmul__", [](const Type& a, const Scalar& b)
  657. // {
  658. // return Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>(b * a);
  659. // })
  660. ;
  661. }