summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/test_eigen.py
diff options
context:
space:
mode:
authorAndreas Sandberg <andreas.sandberg@arm.com>2017-02-27 13:17:51 +0000
committerAndreas Sandberg <andreas.sandberg@arm.com>2017-05-02 12:37:32 +0000
commitc79706ff4ce591df2151db5504d3c224f3c9965f (patch)
treeb56cd2bfe704a40575a71075e78194a4c516c98d /ext/pybind11/tests/test_eigen.py
parent359cb08623324b62d7c34973ae54d5bc7f23f9fd (diff)
downloadgem5-c79706ff4ce591df2151db5504d3c224f3c9965f.tar.xz
ext: Add pybind rev f4b81b3
Change-Id: I52e4fc9ebf2f59da57d8cf8f3e37cc79598c2f5f Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-by: Andreas Hansson <andreas.hansson@arm.com> Reviewed-by: Curtis Dunham <curtis.dunham@arm.com> Reviewed-on: https://gem5-review.googlesource.com/2229 Reviewed-by: Tony Gutierrez <anthony.gutierrez@amd.com> Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Reviewed-by: Pierre-Yves PĂ©neau <pierre-yves.peneau@lirmm.fr>
Diffstat (limited to 'ext/pybind11/tests/test_eigen.py')
-rw-r--r--ext/pybind11/tests/test_eigen.py145
1 files changed, 145 insertions, 0 deletions
diff --git a/ext/pybind11/tests/test_eigen.py b/ext/pybind11/tests/test_eigen.py
new file mode 100644
index 000000000..b0092fc8b
--- /dev/null
+++ b/ext/pybind11/tests/test_eigen.py
@@ -0,0 +1,145 @@
+import pytest
+
+with pytest.suppress(ImportError):
+ 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 assert_equal_ref(mat):
+ np.testing.assert_array_equal(mat, ref)
+
+
+def assert_sparse_equal_ref(sparse_mat):
+ assert_equal_ref(sparse_mat.todense())
+
+
+@pytest.requires_eigen_and_numpy
+def test_fixed():
+ from pybind11_tests import fixed_r, fixed_c, fixed_passthrough_r, fixed_passthrough_c
+
+ assert_equal_ref(fixed_c())
+ assert_equal_ref(fixed_r())
+ assert_equal_ref(fixed_passthrough_r(fixed_r()))
+ assert_equal_ref(fixed_passthrough_c(fixed_c()))
+ assert_equal_ref(fixed_passthrough_r(fixed_c()))
+ assert_equal_ref(fixed_passthrough_c(fixed_r()))
+
+
+@pytest.requires_eigen_and_numpy
+def test_dense():
+ from pybind11_tests import dense_r, dense_c, dense_passthrough_r, dense_passthrough_c
+
+ assert_equal_ref(dense_r())
+ assert_equal_ref(dense_c())
+ assert_equal_ref(dense_passthrough_r(dense_r()))
+ assert_equal_ref(dense_passthrough_c(dense_c()))
+ assert_equal_ref(dense_passthrough_r(dense_c()))
+ assert_equal_ref(dense_passthrough_c(dense_r()))
+
+
+@pytest.requires_eigen_and_numpy
+def test_nonunit_stride_from_python():
+ from pybind11_tests import double_row, double_col, double_mat_cm, double_mat_rm
+
+ counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3))
+ first_row = counting_mat[0, :]
+ first_col = counting_mat[:, 0]
+ assert np.array_equal(double_row(first_row), 2.0 * first_row)
+ assert np.array_equal(double_col(first_row), 2.0 * first_row)
+ assert np.array_equal(double_row(first_col), 2.0 * first_col)
+ assert np.array_equal(double_col(first_col), 2.0 * first_col)
+
+ counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3))
+ slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
+ for slice_idx, ref_mat in enumerate(slices):
+ assert np.array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat)
+ assert np.array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat)
+
+
+@pytest.requires_eigen_and_numpy
+def test_nonunit_stride_to_python():
+ from pybind11_tests import diagonal, diagonal_1, diagonal_n, block
+
+ assert np.all(diagonal(ref) == ref.diagonal())
+ assert np.all(diagonal_1(ref) == ref.diagonal(1))
+ for i in range(-5, 7):
+ assert np.all(diagonal_n(ref, i) == ref.diagonal(i)), "diagonal_n({})".format(i)
+
+ assert np.all(block(ref, 2, 1, 3, 3) == ref[2:5, 1:4])
+ assert np.all(block(ref, 1, 4, 4, 2) == ref[1:, 4:])
+ assert np.all(block(ref, 1, 4, 3, 2) == ref[1:4, 4:])
+
+
+@pytest.requires_eigen_and_numpy
+def test_eigen_ref_to_python():
+ from pybind11_tests import cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6
+
+ chols = [cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6]
+ for i, chol in enumerate(chols, start=1):
+ mymat = chol(np.array([[1, 2, 4], [2, 13, 23], [4, 23, 77]]))
+ assert np.all(mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])), "cholesky{}".format(i)
+
+
+@pytest.requires_eigen_and_numpy
+def test_special_matrix_objects():
+ from pybind11_tests import incr_diag, symmetric_upper, symmetric_lower
+
+ assert np.all(incr_diag(7) == np.diag([1, 2, 3, 4, 5, 6, 7]))
+
+ asymm = np.array([[ 1, 2, 3, 4],
+ [ 5, 6, 7, 8],
+ [ 9, 10, 11, 12],
+ [13, 14, 15, 16]])
+ symm_lower = np.array(asymm)
+ symm_upper = np.array(asymm)
+ for i in range(4):
+ for j in range(i + 1, 4):
+ symm_lower[i, j] = symm_lower[j, i]
+ symm_upper[j, i] = symm_upper[i, j]
+
+ assert np.all(symmetric_lower(asymm) == symm_lower)
+ assert np.all(symmetric_upper(asymm) == symm_upper)
+
+
+@pytest.requires_eigen_and_numpy
+def test_dense_signature(doc):
+ from pybind11_tests import double_col, double_row, double_mat_rm
+
+ assert doc(double_col) == """
+ double_col(arg0: numpy.ndarray[float32[m, 1]]) -> numpy.ndarray[float32[m, 1]]
+ """
+ assert doc(double_row) == """
+ double_row(arg0: numpy.ndarray[float32[1, n]]) -> numpy.ndarray[float32[1, n]]
+ """
+ assert doc(double_mat_rm) == """
+ double_mat_rm(arg0: numpy.ndarray[float32[m, n]]) -> numpy.ndarray[float32[m, n]]
+ """
+
+
+@pytest.requires_eigen_and_scipy
+def test_sparse():
+ from pybind11_tests import sparse_r, sparse_c, sparse_passthrough_r, sparse_passthrough_c
+
+ assert_sparse_equal_ref(sparse_r())
+ assert_sparse_equal_ref(sparse_c())
+ assert_sparse_equal_ref(sparse_passthrough_r(sparse_r()))
+ assert_sparse_equal_ref(sparse_passthrough_c(sparse_c()))
+ assert_sparse_equal_ref(sparse_passthrough_r(sparse_c()))
+ assert_sparse_equal_ref(sparse_passthrough_c(sparse_r()))
+
+
+@pytest.requires_eigen_and_scipy
+def test_sparse_signature(doc):
+ from pybind11_tests import sparse_passthrough_r, sparse_passthrough_c
+
+ assert doc(sparse_passthrough_r) == """
+ sparse_passthrough_r(arg0: scipy.sparse.csr_matrix[float32]) -> scipy.sparse.csr_matrix[float32]
+ """ # noqa: E501 line too long
+ assert doc(sparse_passthrough_c) == """
+ sparse_passthrough_c(arg0: scipy.sparse.csc_matrix[float32]) -> scipy.sparse.csc_matrix[float32]
+ """ # noqa: E501 line too long