py_vector.cpp 25 KB

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