Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | with pytest.suppress(ImportError): |
| 4 | import numpy as np |
| 5 | |
Dean Moldovan | 2391917 | 2016-08-25 17:08:09 +0200 | [diff] [blame] | 6 | ref = np.array([[ 0, 3, 0, 0, 0, 11], |
| 7 | [22, 0, 0, 0, 17, 11], |
| 8 | [ 7, 5, 0, 1, 0, 11], |
| 9 | [ 0, 0, 0, 0, 0, 11], |
| 10 | [ 0, 0, 14, 0, 8, 11]]) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 11 | |
| 12 | |
| 13 | def assert_equal_ref(mat): |
| 14 | np.testing.assert_array_equal(mat, ref) |
| 15 | |
| 16 | |
| 17 | def assert_sparse_equal_ref(sparse_mat): |
| 18 | assert_equal_ref(sparse_mat.todense()) |
| 19 | |
| 20 | |
| 21 | @pytest.requires_eigen_and_numpy |
| 22 | def test_fixed(): |
| 23 | from pybind11_tests import fixed_r, fixed_c, fixed_passthrough_r, fixed_passthrough_c |
| 24 | |
| 25 | assert_equal_ref(fixed_c()) |
| 26 | assert_equal_ref(fixed_r()) |
| 27 | assert_equal_ref(fixed_passthrough_r(fixed_r())) |
| 28 | assert_equal_ref(fixed_passthrough_c(fixed_c())) |
| 29 | assert_equal_ref(fixed_passthrough_r(fixed_c())) |
| 30 | assert_equal_ref(fixed_passthrough_c(fixed_r())) |
| 31 | |
| 32 | |
| 33 | @pytest.requires_eigen_and_numpy |
| 34 | def test_dense(): |
| 35 | from pybind11_tests import dense_r, dense_c, dense_passthrough_r, dense_passthrough_c |
| 36 | |
| 37 | assert_equal_ref(dense_r()) |
| 38 | assert_equal_ref(dense_c()) |
| 39 | assert_equal_ref(dense_passthrough_r(dense_r())) |
| 40 | assert_equal_ref(dense_passthrough_c(dense_c())) |
| 41 | assert_equal_ref(dense_passthrough_r(dense_c())) |
| 42 | assert_equal_ref(dense_passthrough_c(dense_r())) |
| 43 | |
| 44 | |
| 45 | @pytest.requires_eigen_and_numpy |
| 46 | def test_nonunit_stride_from_python(): |
| 47 | from pybind11_tests import double_row, double_col, double_mat_cm, double_mat_rm |
| 48 | |
| 49 | counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3)) |
| 50 | first_row = counting_mat[0, :] |
| 51 | first_col = counting_mat[:, 0] |
| 52 | assert np.array_equal(double_row(first_row), 2.0 * first_row) |
| 53 | assert np.array_equal(double_col(first_row), 2.0 * first_row) |
| 54 | assert np.array_equal(double_row(first_col), 2.0 * first_col) |
| 55 | assert np.array_equal(double_col(first_col), 2.0 * first_col) |
| 56 | |
| 57 | counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3)) |
| 58 | slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]] |
| 59 | for slice_idx, ref_mat in enumerate(slices): |
| 60 | assert np.array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat) |
| 61 | assert np.array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat) |
| 62 | |
| 63 | |
| 64 | @pytest.requires_eigen_and_numpy |
| 65 | def test_nonunit_stride_to_python(): |
| 66 | from pybind11_tests import diagonal, diagonal_1, diagonal_n, block |
| 67 | |
| 68 | assert np.all(diagonal(ref) == ref.diagonal()) |
| 69 | assert np.all(diagonal_1(ref) == ref.diagonal(1)) |
| 70 | for i in range(-5, 7): |
| 71 | assert np.all(diagonal_n(ref, i) == ref.diagonal(i)), "diagonal_n({})".format(i) |
| 72 | |
| 73 | assert np.all(block(ref, 2, 1, 3, 3) == ref[2:5, 1:4]) |
| 74 | assert np.all(block(ref, 1, 4, 4, 2) == ref[1:, 4:]) |
| 75 | assert np.all(block(ref, 1, 4, 3, 2) == ref[1:4, 4:]) |
| 76 | |
| 77 | |
| 78 | @pytest.requires_eigen_and_numpy |
| 79 | def test_eigen_ref_to_python(): |
| 80 | from pybind11_tests import cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6 |
| 81 | |
| 82 | chols = [cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6] |
| 83 | for i, chol in enumerate(chols, start=1): |
| 84 | mymat = chol(np.array([[1, 2, 4], [2, 13, 23], [4, 23, 77]])) |
| 85 | assert np.all(mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])), "cholesky{}".format(i) |
| 86 | |
| 87 | |
| 88 | @pytest.requires_eigen_and_numpy |
| 89 | def test_special_matrix_objects(): |
| 90 | from pybind11_tests import incr_diag, symmetric_upper, symmetric_lower |
| 91 | |
| 92 | assert np.all(incr_diag(7) == np.diag([1, 2, 3, 4, 5, 6, 7])) |
| 93 | |
| 94 | asymm = np.array([[ 1, 2, 3, 4], |
| 95 | [ 5, 6, 7, 8], |
| 96 | [ 9, 10, 11, 12], |
| 97 | [13, 14, 15, 16]]) |
| 98 | symm_lower = np.array(asymm) |
| 99 | symm_upper = np.array(asymm) |
| 100 | for i in range(4): |
| 101 | for j in range(i + 1, 4): |
| 102 | symm_lower[i, j] = symm_lower[j, i] |
| 103 | symm_upper[j, i] = symm_upper[i, j] |
| 104 | |
| 105 | assert np.all(symmetric_lower(asymm) == symm_lower) |
| 106 | assert np.all(symmetric_upper(asymm) == symm_upper) |
| 107 | |
| 108 | |
| 109 | @pytest.requires_eigen_and_numpy |
| 110 | def test_dense_signature(doc): |
| 111 | from pybind11_tests import double_col, double_row, double_mat_rm |
| 112 | |
| 113 | assert doc(double_col) == "double_col(arg0: numpy.ndarray[float32[m, 1]]) -> numpy.ndarray[float32[m, 1]]" |
| 114 | assert doc(double_row) == "double_row(arg0: numpy.ndarray[float32[1, n]]) -> numpy.ndarray[float32[1, n]]" |
| 115 | assert doc(double_mat_rm) == "double_mat_rm(arg0: numpy.ndarray[float32[m, n]]) -> numpy.ndarray[float32[m, n]]" |
| 116 | |
| 117 | |
| 118 | @pytest.requires_eigen_and_scipy |
| 119 | def test_sparse(): |
| 120 | from pybind11_tests import sparse_r, sparse_c, sparse_passthrough_r, sparse_passthrough_c |
| 121 | |
| 122 | assert_sparse_equal_ref(sparse_r()) |
| 123 | assert_sparse_equal_ref(sparse_c()) |
| 124 | assert_sparse_equal_ref(sparse_passthrough_r(sparse_r())) |
| 125 | assert_sparse_equal_ref(sparse_passthrough_c(sparse_c())) |
| 126 | assert_sparse_equal_ref(sparse_passthrough_r(sparse_c())) |
| 127 | assert_sparse_equal_ref(sparse_passthrough_c(sparse_r())) |
| 128 | |
| 129 | |
| 130 | @pytest.requires_eigen_and_scipy |
| 131 | def test_sparse_signature(doc): |
| 132 | from pybind11_tests import sparse_passthrough_r, sparse_passthrough_c |
| 133 | |
| 134 | assert doc(sparse_passthrough_r) == "sparse_passthrough_r(arg0: scipy.sparse.csr_matrix[float32]) -> scipy.sparse.csr_matrix[float32]" |
| 135 | assert doc(sparse_passthrough_c) == "sparse_passthrough_c(arg0: scipy.sparse.csc_matrix[float32]) -> scipy.sparse.csc_matrix[float32]" |