|
@@ -1,4 +1,5 @@
|
|
|
#include <Eigen/Dense>
|
|
|
+#include <Eigen/Sparse>
|
|
|
|
|
|
#include "python.h"
|
|
|
|
|
@@ -339,18 +340,166 @@ py::class_<Type> bind_eigen_2(py::module &m, const char *name,
|
|
|
return matrix;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+template <typename Type>
|
|
|
+py::class_<Type> bind_eigen_sparse_2(py::module &m, const char *name,
|
|
|
+ py::object parent = py::object()) {
|
|
|
+ typedef typename Type::Scalar Scalar;
|
|
|
+
|
|
|
+
|
|
|
+ a function pointer, thus a big portion of the binding code below
|
|
|
+ instantiates Eigen code using small anonymous wrapper functions */
|
|
|
+ py::class_<Type> matrix(m, name, parent);
|
|
|
+
|
|
|
+ matrix
|
|
|
+
|
|
|
+ .def(py::init<>())
|
|
|
+ .def(py::init<size_t, size_t>())
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ .def("size", [](const Type &m) { return m.size(); })
|
|
|
+ .def("cols", [](const Type &m) { return m.cols(); })
|
|
|
+ .def("rows", [](const Type &m) { return m.rows(); })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Type to avoid type issues with Eigen's crazy expression templates) */
|
|
|
+ .def_cast(-py::self)
|
|
|
+ .def_cast(py::self + py::self)
|
|
|
+ .def_cast(py::self - py::self)
|
|
|
+ .def_cast(py::self * py::self)
|
|
|
+ .def_cast(py::self * Scalar())
|
|
|
+ .def(py::self * Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>())
|
|
|
+ .def_cast(py::self / Scalar())
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ .def("__repr__", [](const Type &v) {
|
|
|
+ std::ostringstream oss;
|
|
|
+ oss << v;
|
|
|
+ return oss.str();
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ;
|
|
|
+ return matrix;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
void python_export_vector(py::module &m) {
|
|
|
|
|
|
py::module me = m.def_submodule(
|
|
|
"eigen", "Wrappers for Eigen types");
|
|
|
|
|
|
+
|
|
|
bind_eigen_1<Eigen::VectorXd> (me, "VectorXd");
|
|
|
+ py::implicitly_convertible<py::buffer, Eigen::VectorXd>();
|
|
|
+ py::implicitly_convertible<double, Eigen::VectorXd>();
|
|
|
+
|
|
|
+
|
|
|
bind_eigen_1<Eigen::VectorXi> (me, "VectorXi");
|
|
|
+ py::implicitly_convertible<py::buffer, Eigen::VectorXi>();
|
|
|
+ py::implicitly_convertible<double, Eigen::VectorXi>();
|
|
|
+
|
|
|
+
|
|
|
bind_eigen_2<Eigen::MatrixXd> (me, "MatrixXd");
|
|
|
- bind_eigen_2<Eigen::MatrixXi> (me, "MatrixXi");
|
|
|
+ py::implicitly_convertible<py::buffer, Eigen::MatrixXd>();
|
|
|
+ py::implicitly_convertible<double, Eigen::MatrixXd>();
|
|
|
|
|
|
+
|
|
|
+ bind_eigen_2<Eigen::MatrixXi> (me, "MatrixXi");
|
|
|
+ py::implicitly_convertible<py::buffer, Eigen::MatrixXi>();
|
|
|
+ py::implicitly_convertible<double, Eigen::MatrixXi>();
|
|
|
|
|
|
-
|
|
|
+
|
|
|
auto vector3 = bind_eigen_1_3<Eigen::Vector3d>(me, "Vector3d");
|
|
|
vector3
|
|
|
.def("norm", [](const Eigen::Vector3d &v) { return v.norm(); })
|
|
@@ -366,15 +515,19 @@ void python_export_vector(py::module &m) {
|
|
|
.def_property("z", [](const Eigen::Vector3d &v) -> double { return v.z(); },
|
|
|
[](Eigen::Vector3d &v, double z) { v.z() = z; }, "Z coordinate");
|
|
|
|
|
|
- py::implicitly_convertible<py::buffer, Eigen::VectorXd>();
|
|
|
- py::implicitly_convertible<py::buffer, Eigen::MatrixXd>();
|
|
|
- py::implicitly_convertible<py::buffer, Eigen::VectorXi>();
|
|
|
- py::implicitly_convertible<py::buffer, Eigen::MatrixXi>();
|
|
|
py::implicitly_convertible<py::buffer, Eigen::Vector3d>();
|
|
|
-
|
|
|
- py::implicitly_convertible<double, Eigen::VectorXd>();
|
|
|
- py::implicitly_convertible<double, Eigen::MatrixXd>();
|
|
|
- py::implicitly_convertible<double, Eigen::VectorXi>();
|
|
|
- py::implicitly_convertible<double, Eigen::MatrixXi>();
|
|
|
py::implicitly_convertible<double, Eigen::Vector3d>();
|
|
|
+
|
|
|
+
|
|
|
+ bind_eigen_sparse_2< Eigen::SparseMatrix<double> > (me, "SparseMatrixd");
|
|
|
+
|
|
|
+
|
|
|
+ bind_eigen_sparse_2< Eigen::SparseMatrix<int> > (me, "SparseMatrixi");
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|