py_vector.cpp 26 KB

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