aboutsummaryrefslogtreecommitdiffstats
path: root/example
diff options
context:
space:
mode:
authorWenzel Jakob <wenzel.jakob@epfl.ch>2016-05-05 20:33:54 +0200
committerWenzel Jakob <wenzel.jakob@epfl.ch>2016-05-05 21:44:29 +0200
commit9e0a0568fed8c05ba7bb8d5101af7b6c407fb4eb (patch)
tree74164202b9353c27ef6cc9eba7914b7e95fcef72 /example
parent9ac5bc55314457e69dcffd352e79c1332f454e25 (diff)
downloadplatform_external_python_pybind11-9e0a0568fed8c05ba7bb8d5101af7b6c407fb4eb.tar.gz
platform_external_python_pybind11-9e0a0568fed8c05ba7bb8d5101af7b6c407fb4eb.tar.bz2
platform_external_python_pybind11-9e0a0568fed8c05ba7bb8d5101af7b6c407fb4eb.zip
transparent conversion of dense and sparse Eigen types
Diffstat (limited to 'example')
-rw-r--r--example/eigen.cpp73
-rw-r--r--example/eigen.py44
-rw-r--r--example/eigen.ref18
-rw-r--r--example/example.cpp8
4 files changed, 143 insertions, 0 deletions
diff --git a/example/eigen.cpp b/example/eigen.cpp
new file mode 100644
index 0000000..b6fa24a
--- /dev/null
+++ b/example/eigen.cpp
@@ -0,0 +1,73 @@
+/*
+ example/eigen.cpp -- automatic conversion of Eigen types
+
+ Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "example.h"
+#include <pybind11/eigen.h>
+
+void init_eigen(py::module &m) {
+ typedef Eigen::Matrix<float, 5, 6, Eigen::RowMajor> FixedMatrixR;
+ typedef Eigen::Matrix<float, 5, 6> FixedMatrixC;
+ typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> DenseMatrixR;
+ typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> DenseMatrixC;
+ typedef Eigen::SparseMatrix<float, Eigen::RowMajor> SparseMatrixR;
+ typedef Eigen::SparseMatrix<float> SparseMatrixC;
+
+ // Non-symmetric matrix with zero elements
+ Eigen::MatrixXf mat(5, 6);
+ mat << 0, 3, 0, 0, 0, 11, 22, 0, 0, 0, 17, 11, 7, 5, 0, 1, 0, 11, 0,
+ 0, 0, 0, 0, 11, 0, 0, 14, 0, 8, 11;
+
+ m.def("fixed_r", [mat]() -> FixedMatrixR {
+ return FixedMatrixR(mat);
+ });
+
+ m.def("fixed_c", [mat]() -> FixedMatrixC {
+ return FixedMatrixC(mat);
+ });
+
+ m.def("fixed_passthrough_r", [](const FixedMatrixR &m) -> FixedMatrixR {
+ return m;
+ });
+
+ m.def("fixed_passthrough_c", [](const FixedMatrixC &m) -> FixedMatrixC {
+ return m;
+ });
+
+ m.def("dense_r", [mat]() -> DenseMatrixR {
+ return DenseMatrixR(mat);
+ });
+
+ m.def("dense_c", [mat]() -> DenseMatrixC {
+ return DenseMatrixC(mat);
+ });
+
+ m.def("dense_passthrough_r", [](const DenseMatrixR &m) -> DenseMatrixR {
+ return m;
+ });
+
+ m.def("dense_passthrough_c", [](const DenseMatrixC &m) -> DenseMatrixC {
+ return m;
+ });
+
+ m.def("sparse_r", [mat]() -> SparseMatrixR {
+ return Eigen::SparseView<Eigen::MatrixXf>(mat);
+ });
+
+ m.def("sparse_c", [mat]() -> SparseMatrixC {
+ return Eigen::SparseView<Eigen::MatrixXf>(mat);
+ });
+
+ m.def("sparse_passthrough_r", [](const SparseMatrixR &m) -> SparseMatrixR {
+ return m;
+ });
+
+ m.def("sparse_passthrough_c", [](const SparseMatrixC &m) -> SparseMatrixC {
+ return m;
+ });
+}
diff --git a/example/eigen.py b/example/eigen.py
new file mode 100644
index 0000000..accaf23
--- /dev/null
+++ b/example/eigen.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+from __future__ import print_function
+import sys
+sys.path.append('.')
+
+from example import fixed_r, fixed_c
+from example import fixed_passthrough_r, fixed_passthrough_c
+from example import dense_r, dense_c
+from example import dense_passthrough_r, dense_passthrough_c
+from example import sparse_r, sparse_c
+from example import sparse_passthrough_r, sparse_passthrough_c
+import numpy as np
+
+ref = np.array(
+ [[0, 3, 0, 0, 0, 11],
+ [22, 0, 0, 0, 17, 11],
+ [7, 5, 0, 1, 0, 11],
+ [0, 0, 0, 0, 0, 11],
+ [0, 0, 14, 0, 8, 11]])
+
+
+def check(mat):
+ return 'OK' if np.sum(mat - ref) == 0 else 'NOT OK'
+
+print("fixed_r = %s" % check(fixed_r()))
+print("fixed_c = %s" % check(fixed_c()))
+print("pt_r(fixed_r) = %s" % check(fixed_passthrough_r(fixed_r())))
+print("pt_c(fixed_c) = %s" % check(fixed_passthrough_c(fixed_c())))
+print("pt_r(fixed_c) = %s" % check(fixed_passthrough_r(fixed_c())))
+print("pt_c(fixed_r) = %s" % check(fixed_passthrough_c(fixed_r())))
+
+print("dense_r = %s" % check(dense_r()))
+print("dense_c = %s" % check(dense_c()))
+print("pt_r(dense_r) = %s" % check(dense_passthrough_r(dense_r())))
+print("pt_c(dense_c) = %s" % check(dense_passthrough_c(dense_c())))
+print("pt_r(dense_c) = %s" % check(dense_passthrough_r(dense_c())))
+print("pt_c(dense_r) = %s" % check(dense_passthrough_c(dense_r())))
+
+print("sparse_r = %s" % check(sparse_r()))
+print("sparse_c = %s" % check(sparse_c()))
+print("pt_r(sparse_r) = %s" % check(sparse_passthrough_r(sparse_r())))
+print("pt_c(sparse_c) = %s" % check(sparse_passthrough_c(sparse_c())))
+print("pt_r(sparse_c) = %s" % check(sparse_passthrough_r(sparse_c())))
+print("pt_c(sparse_r) = %s" % check(sparse_passthrough_c(sparse_r())))
diff --git a/example/eigen.ref b/example/eigen.ref
new file mode 100644
index 0000000..b87f8ed
--- /dev/null
+++ b/example/eigen.ref
@@ -0,0 +1,18 @@
+fixed_r = OK
+fixed_c = OK
+pt_r(fixed_r) = OK
+pt_c(fixed_c) = OK
+pt_r(fixed_c) = OK
+pt_c(fixed_r) = OK
+dense_r = OK
+dense_c = OK
+pt_r(dense_r) = OK
+pt_c(dense_c) = OK
+pt_r(dense_c) = OK
+pt_c(dense_r) = OK
+sparse_r = OK
+sparse_c = OK
+pt_r(sparse_r) = OK
+pt_c(sparse_c) = OK
+pt_r(sparse_c) = OK
+pt_c(sparse_r) = OK
diff --git a/example/example.cpp b/example/example.cpp
index 34b2df7..b4199e8 100644
--- a/example/example.cpp
+++ b/example/example.cpp
@@ -27,6 +27,10 @@ void init_ex15(py::module &);
void init_ex16(py::module &);
void init_issues(py::module &);
+#if defined(PYBIND11_TEST_EIGEN)
+ void init_eigen(py::module &);
+#endif
+
PYBIND11_PLUGIN(example) {
py::module m("example", "pybind example plugin");
@@ -48,5 +52,9 @@ PYBIND11_PLUGIN(example) {
init_ex16(m);
init_issues(m);
+ #if defined(PYBIND11_TEST_EIGEN)
+ init_eigen(m);
+ #endif
+
return m.ptr();
}