py_vector.cpp 25 KB

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