py_vector.cpp 26 KB

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