py_vector.cpp 26 KB

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