Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
Jason Rhinelander | 2a75784 | 2017-01-24 11:26:51 -0500 | [diff] [blame] | 3 | pytestmark = pytest.requires_eigen_and_numpy |
| 4 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 5 | with pytest.suppress(ImportError): |
| 6 | import numpy as np |
| 7 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 8 | ref = np.array([[ 0., 3, 0, 0, 0, 11], |
Dean Moldovan | 2391917 | 2016-08-25 17:08:09 +0200 | [diff] [blame] | 9 | [22, 0, 0, 0, 17, 11], |
| 10 | [ 7, 5, 0, 1, 0, 11], |
| 11 | [ 0, 0, 0, 0, 0, 11], |
| 12 | [ 0, 0, 14, 0, 8, 11]]) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 13 | |
| 14 | |
| 15 | def assert_equal_ref(mat): |
| 16 | np.testing.assert_array_equal(mat, ref) |
| 17 | |
| 18 | |
| 19 | def assert_sparse_equal_ref(sparse_mat): |
| 20 | assert_equal_ref(sparse_mat.todense()) |
| 21 | |
| 22 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 23 | def test_fixed(): |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 24 | from pybind11_tests import fixed_r, fixed_c, fixed_copy_r, fixed_copy_c |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 25 | |
| 26 | assert_equal_ref(fixed_c()) |
| 27 | assert_equal_ref(fixed_r()) |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 28 | assert_equal_ref(fixed_copy_r(fixed_r())) |
| 29 | assert_equal_ref(fixed_copy_c(fixed_c())) |
| 30 | assert_equal_ref(fixed_copy_r(fixed_c())) |
| 31 | assert_equal_ref(fixed_copy_c(fixed_r())) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 32 | |
| 33 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 34 | def test_dense(): |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 35 | from pybind11_tests import dense_r, dense_c, dense_copy_r, dense_copy_c |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 36 | |
| 37 | assert_equal_ref(dense_r()) |
| 38 | assert_equal_ref(dense_c()) |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 39 | assert_equal_ref(dense_copy_r(dense_r())) |
| 40 | assert_equal_ref(dense_copy_c(dense_c())) |
| 41 | assert_equal_ref(dense_copy_r(dense_c())) |
| 42 | assert_equal_ref(dense_copy_c(dense_r())) |
| 43 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 44 | |
Jason Rhinelander | d9d224f | 2017-01-12 19:50:33 -0500 | [diff] [blame] | 45 | def test_partially_fixed(): |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 46 | from pybind11_tests import (partial_copy_four_rm_r, partial_copy_four_rm_c, |
| 47 | partial_copy_four_cm_r, partial_copy_four_cm_c) |
Jason Rhinelander | d9d224f | 2017-01-12 19:50:33 -0500 | [diff] [blame] | 48 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 49 | ref2 = np.array([[0., 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]) |
| 50 | np.testing.assert_array_equal(partial_copy_four_rm_r(ref2), ref2) |
| 51 | np.testing.assert_array_equal(partial_copy_four_rm_c(ref2), ref2) |
| 52 | np.testing.assert_array_equal(partial_copy_four_rm_r(ref2[:, 1]), ref2[:, [1]]) |
| 53 | np.testing.assert_array_equal(partial_copy_four_rm_c(ref2[0, :]), ref2[[0], :]) |
| 54 | np.testing.assert_array_equal(partial_copy_four_rm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)]) |
| 55 | np.testing.assert_array_equal( |
| 56 | partial_copy_four_rm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :]) |
Jason Rhinelander | d9d224f | 2017-01-12 19:50:33 -0500 | [diff] [blame] | 57 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 58 | np.testing.assert_array_equal(partial_copy_four_cm_r(ref2), ref2) |
| 59 | np.testing.assert_array_equal(partial_copy_four_cm_c(ref2), ref2) |
| 60 | np.testing.assert_array_equal(partial_copy_four_cm_r(ref2[:, 1]), ref2[:, [1]]) |
| 61 | np.testing.assert_array_equal(partial_copy_four_cm_c(ref2[0, :]), ref2[[0], :]) |
| 62 | np.testing.assert_array_equal(partial_copy_four_cm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)]) |
| 63 | np.testing.assert_array_equal( |
| 64 | partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :]) |
| 65 | |
Dean Moldovan | 4567f1f | 2017-05-11 15:38:39 +0200 | [diff] [blame] | 66 | # TypeError should be raise for a shape mismatch |
| 67 | functions = [partial_copy_four_rm_r, partial_copy_four_rm_c, |
| 68 | partial_copy_four_cm_r, partial_copy_four_cm_c] |
| 69 | matrix_with_wrong_shape = [[1, 2], |
| 70 | [3, 4]] |
| 71 | for f in functions: |
| 72 | with pytest.raises(TypeError) as excinfo: |
| 73 | f(matrix_with_wrong_shape) |
| 74 | assert "incompatible function arguments" in str(excinfo.value) |
| 75 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 76 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 77 | def test_mutator_descriptors(): |
| 78 | from pybind11_tests import fixed_mutator_r, fixed_mutator_c, fixed_mutator_a |
| 79 | zr = np.arange(30, dtype='float32').reshape(5, 6) # row-major |
| 80 | zc = zr.reshape(6, 5).transpose() # column-major |
| 81 | |
| 82 | fixed_mutator_r(zr) |
| 83 | fixed_mutator_c(zc) |
| 84 | fixed_mutator_a(zr) |
| 85 | fixed_mutator_a(zc) |
| 86 | with pytest.raises(TypeError) as excinfo: |
| 87 | fixed_mutator_r(zc) |
Jason Rhinelander | e9e1774 | 2017-04-08 19:26:42 -0400 | [diff] [blame] | 88 | assert ('(arg0: numpy.ndarray[float32[5, 6], flags.writeable, flags.c_contiguous]) -> None' |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 89 | in str(excinfo.value)) |
| 90 | with pytest.raises(TypeError) as excinfo: |
| 91 | fixed_mutator_c(zr) |
Jason Rhinelander | e9e1774 | 2017-04-08 19:26:42 -0400 | [diff] [blame] | 92 | assert ('(arg0: numpy.ndarray[float32[5, 6], flags.writeable, flags.f_contiguous]) -> None' |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 93 | in str(excinfo.value)) |
| 94 | with pytest.raises(TypeError) as excinfo: |
| 95 | fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype='float32')) |
Jason Rhinelander | e9e1774 | 2017-04-08 19:26:42 -0400 | [diff] [blame] | 96 | assert ('(arg0: numpy.ndarray[float32[5, 6], flags.writeable]) -> None' |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 97 | in str(excinfo.value)) |
| 98 | zr.flags.writeable = False |
| 99 | with pytest.raises(TypeError): |
| 100 | fixed_mutator_r(zr) |
| 101 | with pytest.raises(TypeError): |
| 102 | fixed_mutator_a(zr) |
| 103 | |
| 104 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 105 | def test_cpp_casting(): |
| 106 | from pybind11_tests import (cpp_copy, cpp_ref_c, cpp_ref_r, cpp_ref_any, |
| 107 | fixed_r, fixed_c, get_cm_ref, get_rm_ref, ReturnTester) |
| 108 | assert cpp_copy(fixed_r()) == 22. |
| 109 | assert cpp_copy(fixed_c()) == 22. |
| 110 | z = np.array([[5., 6], [7, 8]]) |
| 111 | assert cpp_copy(z) == 7. |
| 112 | assert cpp_copy(get_cm_ref()) == 21. |
| 113 | assert cpp_copy(get_rm_ref()) == 21. |
| 114 | assert cpp_ref_c(get_cm_ref()) == 21. |
| 115 | assert cpp_ref_r(get_rm_ref()) == 21. |
| 116 | with pytest.raises(RuntimeError) as excinfo: |
| 117 | # Can't reference fixed_c: it contains floats, cpp_ref_any wants doubles |
| 118 | cpp_ref_any(fixed_c()) |
| 119 | assert 'Unable to cast Python instance' in str(excinfo.value) |
| 120 | with pytest.raises(RuntimeError) as excinfo: |
| 121 | # Can't reference fixed_r: it contains floats, cpp_ref_any wants doubles |
| 122 | cpp_ref_any(fixed_r()) |
| 123 | assert 'Unable to cast Python instance' in str(excinfo.value) |
| 124 | assert cpp_ref_any(ReturnTester.create()) == 1. |
| 125 | |
| 126 | assert cpp_ref_any(get_cm_ref()) == 21. |
| 127 | assert cpp_ref_any(get_cm_ref()) == 21. |
| 128 | |
| 129 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 130 | def test_pass_readonly_array(): |
| 131 | from pybind11_tests import fixed_copy_r, fixed_r, fixed_r_const |
| 132 | z = np.full((5, 6), 42.0) |
| 133 | z.flags.writeable = False |
| 134 | np.testing.assert_array_equal(z, fixed_copy_r(z)) |
| 135 | np.testing.assert_array_equal(fixed_r_const(), fixed_r()) |
| 136 | assert not fixed_r_const().flags.writeable |
| 137 | np.testing.assert_array_equal(fixed_copy_r(fixed_r_const()), fixed_r_const()) |
| 138 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 139 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 140 | def test_nonunit_stride_from_python(): |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 141 | from pybind11_tests import ( |
Dean Moldovan | 5143989 | 2017-02-28 18:07:51 +0100 | [diff] [blame] | 142 | double_row, double_col, double_complex, double_mat_cm, double_mat_rm, |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 143 | double_threec, double_threer) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 144 | |
| 145 | counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3)) |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 146 | second_row = counting_mat[1, :] |
| 147 | second_col = counting_mat[:, 1] |
| 148 | np.testing.assert_array_equal(double_row(second_row), 2.0 * second_row) |
| 149 | np.testing.assert_array_equal(double_col(second_row), 2.0 * second_row) |
Dean Moldovan | 5143989 | 2017-02-28 18:07:51 +0100 | [diff] [blame] | 150 | np.testing.assert_array_equal(double_complex(second_row), 2.0 * second_row) |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 151 | np.testing.assert_array_equal(double_row(second_col), 2.0 * second_col) |
| 152 | np.testing.assert_array_equal(double_col(second_col), 2.0 * second_col) |
Dean Moldovan | 5143989 | 2017-02-28 18:07:51 +0100 | [diff] [blame] | 153 | np.testing.assert_array_equal(double_complex(second_col), 2.0 * second_col) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 154 | |
| 155 | counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3)) |
| 156 | slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]] |
| 157 | for slice_idx, ref_mat in enumerate(slices): |
Jason Rhinelander | d9d224f | 2017-01-12 19:50:33 -0500 | [diff] [blame] | 158 | np.testing.assert_array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat) |
| 159 | np.testing.assert_array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 160 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 161 | # Mutator: |
| 162 | double_threer(second_row) |
| 163 | double_threec(second_col) |
| 164 | np.testing.assert_array_equal(counting_mat, [[0., 2, 2], [6, 16, 10], [6, 14, 8]]) |
| 165 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 166 | |
Cris Luengo | 627da3f | 2017-04-06 11:34:39 -0600 | [diff] [blame] | 167 | def test_negative_stride_from_python(msg): |
| 168 | from pybind11_tests import ( |
| 169 | double_row, double_col, double_complex, double_mat_cm, double_mat_rm, |
| 170 | double_threec, double_threer) |
| 171 | |
| 172 | # Eigen doesn't support (as of yet) negative strides. When a function takes an Eigen |
| 173 | # matrix by copy or const reference, we can pass a numpy array that has negative strides. |
| 174 | # Otherwise, an exception will be thrown as Eigen will not be able to map the numpy array. |
| 175 | |
| 176 | counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3)) |
| 177 | counting_mat = counting_mat[::-1, ::-1] |
| 178 | second_row = counting_mat[1, :] |
| 179 | second_col = counting_mat[:, 1] |
| 180 | np.testing.assert_array_equal(double_row(second_row), 2.0 * second_row) |
| 181 | np.testing.assert_array_equal(double_col(second_row), 2.0 * second_row) |
| 182 | np.testing.assert_array_equal(double_complex(second_row), 2.0 * second_row) |
| 183 | np.testing.assert_array_equal(double_row(second_col), 2.0 * second_col) |
| 184 | np.testing.assert_array_equal(double_col(second_col), 2.0 * second_col) |
| 185 | np.testing.assert_array_equal(double_complex(second_col), 2.0 * second_col) |
| 186 | |
| 187 | counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3)) |
| 188 | counting_3d = counting_3d[::-1, ::-1, ::-1] |
| 189 | slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]] |
| 190 | for slice_idx, ref_mat in enumerate(slices): |
| 191 | np.testing.assert_array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat) |
| 192 | np.testing.assert_array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat) |
| 193 | |
| 194 | # Mutator: |
| 195 | with pytest.raises(TypeError) as excinfo: |
| 196 | double_threer(second_row) |
| 197 | assert msg(excinfo.value) == """ |
| 198 | double_threer(): incompatible function arguments. The following argument types are supported: |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 199 | 1. (arg0: numpy.ndarray[float32[1, 3], flags.writeable]) -> None |
Cris Luengo | 627da3f | 2017-04-06 11:34:39 -0600 | [diff] [blame] | 200 | |
| 201 | Invoked with: array([ 5., 4., 3.], dtype=float32) |
| 202 | """ |
| 203 | |
| 204 | with pytest.raises(TypeError) as excinfo: |
| 205 | double_threec(second_col) |
| 206 | assert msg(excinfo.value) == """ |
| 207 | double_threec(): incompatible function arguments. The following argument types are supported: |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 208 | 1. (arg0: numpy.ndarray[float32[3, 1], flags.writeable]) -> None |
Cris Luengo | 627da3f | 2017-04-06 11:34:39 -0600 | [diff] [blame] | 209 | |
| 210 | Invoked with: array([ 7., 4., 1.], dtype=float32) |
| 211 | """ |
| 212 | |
| 213 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 214 | def test_nonunit_stride_to_python(): |
| 215 | from pybind11_tests import diagonal, diagonal_1, diagonal_n, block |
| 216 | |
| 217 | assert np.all(diagonal(ref) == ref.diagonal()) |
| 218 | assert np.all(diagonal_1(ref) == ref.diagonal(1)) |
| 219 | for i in range(-5, 7): |
| 220 | assert np.all(diagonal_n(ref, i) == ref.diagonal(i)), "diagonal_n({})".format(i) |
| 221 | |
| 222 | assert np.all(block(ref, 2, 1, 3, 3) == ref[2:5, 1:4]) |
| 223 | assert np.all(block(ref, 1, 4, 4, 2) == ref[1:, 4:]) |
| 224 | assert np.all(block(ref, 1, 4, 3, 2) == ref[1:4, 4:]) |
| 225 | |
| 226 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 227 | def test_eigen_ref_to_python(): |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 228 | from pybind11_tests import cholesky1, cholesky2, cholesky3, cholesky4 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 229 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 230 | chols = [cholesky1, cholesky2, cholesky3, cholesky4] |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 231 | for i, chol in enumerate(chols, start=1): |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 232 | mymat = chol(np.array([[1., 2, 4], [2, 13, 23], [4, 23, 77]])) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 233 | assert np.all(mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])), "cholesky{}".format(i) |
| 234 | |
| 235 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 236 | def assign_both(a1, a2, r, c, v): |
| 237 | a1[r, c] = v |
| 238 | a2[r, c] = v |
| 239 | |
| 240 | |
| 241 | def array_copy_but_one(a, r, c, v): |
| 242 | z = np.array(a, copy=True) |
| 243 | z[r, c] = v |
| 244 | return z |
| 245 | |
| 246 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 247 | def test_eigen_return_references(): |
| 248 | """Tests various ways of returning references and non-referencing copies""" |
| 249 | from pybind11_tests import ReturnTester |
| 250 | master = np.ones((10, 10)) |
| 251 | a = ReturnTester() |
| 252 | a_get1 = a.get() |
| 253 | assert not a_get1.flags.owndata and a_get1.flags.writeable |
| 254 | assign_both(a_get1, master, 3, 3, 5) |
| 255 | a_get2 = a.get_ptr() |
| 256 | assert not a_get2.flags.owndata and a_get2.flags.writeable |
| 257 | assign_both(a_get1, master, 2, 3, 6) |
| 258 | |
| 259 | a_view1 = a.view() |
| 260 | assert not a_view1.flags.owndata and not a_view1.flags.writeable |
| 261 | with pytest.raises(ValueError): |
| 262 | a_view1[2, 3] = 4 |
| 263 | a_view2 = a.view_ptr() |
| 264 | assert not a_view2.flags.owndata and not a_view2.flags.writeable |
| 265 | with pytest.raises(ValueError): |
| 266 | a_view2[2, 3] = 4 |
| 267 | |
| 268 | a_copy1 = a.copy_get() |
| 269 | assert a_copy1.flags.owndata and a_copy1.flags.writeable |
| 270 | np.testing.assert_array_equal(a_copy1, master) |
| 271 | a_copy1[7, 7] = -44 # Shouldn't affect anything else |
| 272 | c1want = array_copy_but_one(master, 7, 7, -44) |
| 273 | a_copy2 = a.copy_view() |
| 274 | assert a_copy2.flags.owndata and a_copy2.flags.writeable |
| 275 | np.testing.assert_array_equal(a_copy2, master) |
| 276 | a_copy2[4, 4] = -22 # Shouldn't affect anything else |
| 277 | c2want = array_copy_but_one(master, 4, 4, -22) |
| 278 | |
| 279 | a_ref1 = a.ref() |
| 280 | assert not a_ref1.flags.owndata and a_ref1.flags.writeable |
| 281 | assign_both(a_ref1, master, 1, 1, 15) |
| 282 | a_ref2 = a.ref_const() |
| 283 | assert not a_ref2.flags.owndata and not a_ref2.flags.writeable |
| 284 | with pytest.raises(ValueError): |
| 285 | a_ref2[5, 5] = 33 |
| 286 | a_ref3 = a.ref_safe() |
| 287 | assert not a_ref3.flags.owndata and a_ref3.flags.writeable |
| 288 | assign_both(a_ref3, master, 0, 7, 99) |
| 289 | a_ref4 = a.ref_const_safe() |
| 290 | assert not a_ref4.flags.owndata and not a_ref4.flags.writeable |
| 291 | with pytest.raises(ValueError): |
| 292 | a_ref4[7, 0] = 987654321 |
| 293 | |
| 294 | a_copy3 = a.copy_ref() |
| 295 | assert a_copy3.flags.owndata and a_copy3.flags.writeable |
| 296 | np.testing.assert_array_equal(a_copy3, master) |
| 297 | a_copy3[8, 1] = 11 |
| 298 | c3want = array_copy_but_one(master, 8, 1, 11) |
| 299 | a_copy4 = a.copy_ref_const() |
| 300 | assert a_copy4.flags.owndata and a_copy4.flags.writeable |
| 301 | np.testing.assert_array_equal(a_copy4, master) |
| 302 | a_copy4[8, 4] = 88 |
| 303 | c4want = array_copy_but_one(master, 8, 4, 88) |
| 304 | |
| 305 | a_block1 = a.block(3, 3, 2, 2) |
| 306 | assert not a_block1.flags.owndata and a_block1.flags.writeable |
| 307 | a_block1[0, 0] = 55 |
| 308 | master[3, 3] = 55 |
| 309 | a_block2 = a.block_safe(2, 2, 3, 2) |
| 310 | assert not a_block2.flags.owndata and a_block2.flags.writeable |
| 311 | a_block2[2, 1] = -123 |
| 312 | master[4, 3] = -123 |
| 313 | a_block3 = a.block_const(6, 7, 4, 3) |
| 314 | assert not a_block3.flags.owndata and not a_block3.flags.writeable |
| 315 | with pytest.raises(ValueError): |
| 316 | a_block3[2, 2] = -44444 |
| 317 | |
| 318 | a_copy5 = a.copy_block(2, 2, 2, 3) |
| 319 | assert a_copy5.flags.owndata and a_copy5.flags.writeable |
| 320 | np.testing.assert_array_equal(a_copy5, master[2:4, 2:5]) |
| 321 | a_copy5[1, 1] = 777 |
| 322 | c5want = array_copy_but_one(master[2:4, 2:5], 1, 1, 777) |
| 323 | |
| 324 | a_corn1 = a.corners() |
| 325 | assert not a_corn1.flags.owndata and a_corn1.flags.writeable |
| 326 | a_corn1 *= 50 |
| 327 | a_corn1[1, 1] = 999 |
| 328 | master[0, 0] = 50 |
| 329 | master[0, 9] = 50 |
| 330 | master[9, 0] = 50 |
| 331 | master[9, 9] = 999 |
| 332 | a_corn2 = a.corners_const() |
| 333 | assert not a_corn2.flags.owndata and not a_corn2.flags.writeable |
| 334 | with pytest.raises(ValueError): |
| 335 | a_corn2[1, 0] = 51 |
| 336 | |
| 337 | # All of the changes made all the way along should be visible everywhere |
| 338 | # now (except for the copies, of course) |
| 339 | np.testing.assert_array_equal(a_get1, master) |
| 340 | np.testing.assert_array_equal(a_get2, master) |
| 341 | np.testing.assert_array_equal(a_view1, master) |
| 342 | np.testing.assert_array_equal(a_view2, master) |
| 343 | np.testing.assert_array_equal(a_ref1, master) |
| 344 | np.testing.assert_array_equal(a_ref2, master) |
| 345 | np.testing.assert_array_equal(a_ref3, master) |
| 346 | np.testing.assert_array_equal(a_ref4, master) |
| 347 | np.testing.assert_array_equal(a_block1, master[3:5, 3:5]) |
| 348 | np.testing.assert_array_equal(a_block2, master[2:5, 2:4]) |
| 349 | np.testing.assert_array_equal(a_block3, master[6:10, 7:10]) |
| 350 | np.testing.assert_array_equal(a_corn1, master[0::master.shape[0] - 1, 0::master.shape[1] - 1]) |
| 351 | np.testing.assert_array_equal(a_corn2, master[0::master.shape[0] - 1, 0::master.shape[1] - 1]) |
| 352 | |
| 353 | np.testing.assert_array_equal(a_copy1, c1want) |
| 354 | np.testing.assert_array_equal(a_copy2, c2want) |
| 355 | np.testing.assert_array_equal(a_copy3, c3want) |
| 356 | np.testing.assert_array_equal(a_copy4, c4want) |
| 357 | np.testing.assert_array_equal(a_copy5, c5want) |
| 358 | |
| 359 | |
| 360 | def assert_keeps_alive(cl, method, *args): |
| 361 | from pybind11_tests import ConstructorStats |
| 362 | cstats = ConstructorStats.get(cl) |
| 363 | start_with = cstats.alive() |
| 364 | a = cl() |
| 365 | assert cstats.alive() == start_with + 1 |
| 366 | z = method(a, *args) |
| 367 | assert cstats.alive() == start_with + 1 |
| 368 | del a |
| 369 | # Here's the keep alive in action: |
| 370 | assert cstats.alive() == start_with + 1 |
| 371 | del z |
| 372 | # Keep alive should have expired: |
| 373 | assert cstats.alive() == start_with |
| 374 | |
| 375 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 376 | def test_eigen_keepalive(): |
| 377 | from pybind11_tests import ReturnTester, ConstructorStats |
| 378 | a = ReturnTester() |
| 379 | |
| 380 | cstats = ConstructorStats.get(ReturnTester) |
| 381 | assert cstats.alive() == 1 |
| 382 | unsafe = [a.ref(), a.ref_const(), a.block(1, 2, 3, 4)] |
| 383 | copies = [a.copy_get(), a.copy_view(), a.copy_ref(), a.copy_ref_const(), |
| 384 | a.copy_block(4, 3, 2, 1)] |
| 385 | del a |
| 386 | assert cstats.alive() == 0 |
| 387 | del unsafe |
| 388 | del copies |
| 389 | |
| 390 | for meth in [ReturnTester.get, ReturnTester.get_ptr, ReturnTester.view, |
| 391 | ReturnTester.view_ptr, ReturnTester.ref_safe, ReturnTester.ref_const_safe, |
| 392 | ReturnTester.corners, ReturnTester.corners_const]: |
| 393 | assert_keeps_alive(ReturnTester, meth) |
| 394 | |
| 395 | for meth in [ReturnTester.block_safe, ReturnTester.block_const]: |
| 396 | assert_keeps_alive(ReturnTester, meth, 4, 3, 2, 1) |
| 397 | |
| 398 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 399 | def test_eigen_ref_mutators(): |
| 400 | """Tests whether Eigen can mutate numpy values""" |
| 401 | from pybind11_tests import add_rm, add_cm, add_any, add1, add2 |
| 402 | orig = np.array([[1., 2, 3], [4, 5, 6], [7, 8, 9]]) |
| 403 | zr = np.array(orig) |
| 404 | zc = np.array(orig, order='F') |
| 405 | add_rm(zr, 1, 0, 100) |
| 406 | assert np.all(zr == np.array([[1., 2, 3], [104, 5, 6], [7, 8, 9]])) |
| 407 | add_cm(zc, 1, 0, 200) |
| 408 | assert np.all(zc == np.array([[1., 2, 3], [204, 5, 6], [7, 8, 9]])) |
| 409 | |
| 410 | add_any(zr, 1, 0, 20) |
| 411 | assert np.all(zr == np.array([[1., 2, 3], [124, 5, 6], [7, 8, 9]])) |
| 412 | add_any(zc, 1, 0, 10) |
| 413 | assert np.all(zc == np.array([[1., 2, 3], [214, 5, 6], [7, 8, 9]])) |
| 414 | |
| 415 | # Can't reference a col-major array with a row-major Ref, and vice versa: |
| 416 | with pytest.raises(TypeError): |
| 417 | add_rm(zc, 1, 0, 1) |
| 418 | with pytest.raises(TypeError): |
| 419 | add_cm(zr, 1, 0, 1) |
| 420 | |
| 421 | # Overloads: |
| 422 | add1(zr, 1, 0, -100) |
| 423 | add2(zr, 1, 0, -20) |
| 424 | assert np.all(zr == orig) |
| 425 | add1(zc, 1, 0, -200) |
| 426 | add2(zc, 1, 0, -10) |
| 427 | assert np.all(zc == orig) |
| 428 | |
| 429 | # a non-contiguous slice (this won't work on either the row- or |
| 430 | # column-contiguous refs, but should work for the any) |
| 431 | cornersr = zr[0::2, 0::2] |
| 432 | cornersc = zc[0::2, 0::2] |
| 433 | |
| 434 | assert np.all(cornersr == np.array([[1., 3], [7, 9]])) |
| 435 | assert np.all(cornersc == np.array([[1., 3], [7, 9]])) |
| 436 | |
| 437 | with pytest.raises(TypeError): |
| 438 | add_rm(cornersr, 0, 1, 25) |
| 439 | with pytest.raises(TypeError): |
| 440 | add_cm(cornersr, 0, 1, 25) |
| 441 | with pytest.raises(TypeError): |
| 442 | add_rm(cornersc, 0, 1, 25) |
| 443 | with pytest.raises(TypeError): |
| 444 | add_cm(cornersc, 0, 1, 25) |
| 445 | add_any(cornersr, 0, 1, 25) |
| 446 | add_any(cornersc, 0, 1, 44) |
| 447 | assert np.all(zr == np.array([[1., 2, 28], [4, 5, 6], [7, 8, 9]])) |
| 448 | assert np.all(zc == np.array([[1., 2, 47], [4, 5, 6], [7, 8, 9]])) |
| 449 | |
| 450 | # You shouldn't be allowed to pass a non-writeable array to a mutating Eigen method: |
| 451 | zro = zr[0:4, 0:4] |
| 452 | zro.flags.writeable = False |
| 453 | with pytest.raises(TypeError): |
| 454 | add_rm(zro, 0, 0, 0) |
| 455 | with pytest.raises(TypeError): |
| 456 | add_any(zro, 0, 0, 0) |
| 457 | with pytest.raises(TypeError): |
| 458 | add1(zro, 0, 0, 0) |
| 459 | with pytest.raises(TypeError): |
| 460 | add2(zro, 0, 0, 0) |
| 461 | |
| 462 | # integer array shouldn't be passable to a double-matrix-accepting mutating func: |
| 463 | zi = np.array([[1, 2], [3, 4]]) |
| 464 | with pytest.raises(TypeError): |
| 465 | add_rm(zi) |
| 466 | |
| 467 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 468 | def test_numpy_ref_mutators(): |
| 469 | """Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)""" |
| 470 | from pybind11_tests import ( |
| 471 | get_cm_ref, get_cm_const_ref, get_rm_ref, get_rm_const_ref, reset_refs) |
| 472 | reset_refs() # In case another test already changed it |
| 473 | |
| 474 | zc = get_cm_ref() |
| 475 | zcro = get_cm_const_ref() |
| 476 | zr = get_rm_ref() |
| 477 | zrro = get_rm_const_ref() |
| 478 | |
| 479 | assert [zc[1, 2], zcro[1, 2], zr[1, 2], zrro[1, 2]] == [23] * 4 |
| 480 | |
| 481 | assert not zc.flags.owndata and zc.flags.writeable |
| 482 | assert not zr.flags.owndata and zr.flags.writeable |
| 483 | assert not zcro.flags.owndata and not zcro.flags.writeable |
| 484 | assert not zrro.flags.owndata and not zrro.flags.writeable |
| 485 | |
| 486 | zc[1, 2] = 99 |
| 487 | expect = np.array([[11., 12, 13], [21, 22, 99], [31, 32, 33]]) |
| 488 | # We should have just changed zc, of course, but also zcro and the original eigen matrix |
| 489 | assert np.all(zc == expect) |
| 490 | assert np.all(zcro == expect) |
| 491 | assert np.all(get_cm_ref() == expect) |
| 492 | |
| 493 | zr[1, 2] = 99 |
| 494 | assert np.all(zr == expect) |
| 495 | assert np.all(zrro == expect) |
| 496 | assert np.all(get_rm_ref() == expect) |
| 497 | |
| 498 | # Make sure the readonly ones are numpy-readonly: |
| 499 | with pytest.raises(ValueError): |
| 500 | zcro[1, 2] = 6 |
| 501 | with pytest.raises(ValueError): |
| 502 | zrro[1, 2] = 6 |
| 503 | |
| 504 | # We should be able to explicitly copy like this (and since we're copying, |
| 505 | # the const should drop away) |
| 506 | y1 = np.array(get_cm_const_ref()) |
| 507 | |
| 508 | assert y1.flags.owndata and y1.flags.writeable |
| 509 | # We should get copies of the eigen data, which was modified above: |
| 510 | assert y1[1, 2] == 99 |
| 511 | y1[1, 2] += 12 |
| 512 | assert y1[1, 2] == 111 |
| 513 | assert zc[1, 2] == 99 # Make sure we aren't referencing the original |
| 514 | |
| 515 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 516 | def test_both_ref_mutators(): |
| 517 | """Tests a complex chain of nested eigen/numpy references""" |
| 518 | from pybind11_tests import ( |
| 519 | incr_matrix, get_cm_ref, incr_matrix_any, even_cols, even_rows, reset_refs) |
| 520 | reset_refs() # In case another test already changed it |
| 521 | |
| 522 | z = get_cm_ref() # numpy -> eigen |
| 523 | z[0, 2] -= 3 |
| 524 | z2 = incr_matrix(z, 1) # numpy -> eigen -> numpy -> eigen |
| 525 | z2[1, 1] += 6 |
| 526 | z3 = incr_matrix(z, 2) # (numpy -> eigen)^3 |
| 527 | z3[2, 2] += -5 |
| 528 | z4 = incr_matrix(z, 3) # (numpy -> eigen)^4 |
| 529 | z4[1, 1] -= 1 |
| 530 | z5 = incr_matrix(z, 4) # (numpy -> eigen)^5 |
| 531 | z5[0, 0] = 0 |
| 532 | assert np.all(z == z2) |
| 533 | assert np.all(z == z3) |
| 534 | assert np.all(z == z4) |
| 535 | assert np.all(z == z5) |
| 536 | expect = np.array([[0., 22, 20], [31, 37, 33], [41, 42, 38]]) |
| 537 | assert np.all(z == expect) |
| 538 | |
| 539 | y = np.array(range(100), dtype='float64').reshape(10, 10) |
| 540 | y2 = incr_matrix_any(y, 10) # np -> eigen -> np |
| 541 | y3 = incr_matrix_any(y2[0::2, 0::2], -33) # np -> eigen -> np slice -> np -> eigen -> np |
| 542 | y4 = even_rows(y3) # numpy -> eigen slice -> (... y3) |
| 543 | y5 = even_cols(y4) # numpy -> eigen slice -> (... y4) |
| 544 | y6 = incr_matrix_any(y5, 1000) # numpy -> eigen -> (... y5) |
| 545 | |
| 546 | # Apply same mutations using just numpy: |
| 547 | yexpect = np.array(range(100), dtype='float64').reshape(10, 10) |
| 548 | yexpect += 10 |
| 549 | yexpect[0::2, 0::2] -= 33 |
| 550 | yexpect[0::4, 0::4] += 1000 |
| 551 | assert np.all(y6 == yexpect[0::4, 0::4]) |
| 552 | assert np.all(y5 == yexpect[0::4, 0::4]) |
| 553 | assert np.all(y4 == yexpect[0::4, 0::2]) |
| 554 | assert np.all(y3 == yexpect[0::2, 0::2]) |
| 555 | assert np.all(y2 == yexpect) |
| 556 | assert np.all(y == yexpect) |
| 557 | |
| 558 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 559 | def test_nocopy_wrapper(): |
| 560 | from pybind11_tests import get_elem, get_elem_nocopy, get_elem_rm_nocopy |
| 561 | # get_elem requires a column-contiguous matrix reference, but should be |
| 562 | # callable with other types of matrix (via copying): |
| 563 | int_matrix_colmajor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], order='F') |
| 564 | dbl_matrix_colmajor = np.array(int_matrix_colmajor, dtype='double', order='F', copy=True) |
| 565 | int_matrix_rowmajor = np.array(int_matrix_colmajor, order='C', copy=True) |
| 566 | dbl_matrix_rowmajor = np.array(int_matrix_rowmajor, dtype='double', order='C', copy=True) |
| 567 | |
| 568 | # All should be callable via get_elem: |
| 569 | assert get_elem(int_matrix_colmajor) == 8 |
| 570 | assert get_elem(dbl_matrix_colmajor) == 8 |
| 571 | assert get_elem(int_matrix_rowmajor) == 8 |
| 572 | assert get_elem(dbl_matrix_rowmajor) == 8 |
| 573 | |
| 574 | # All but the second should fail with get_elem_nocopy: |
| 575 | with pytest.raises(TypeError) as excinfo: |
| 576 | get_elem_nocopy(int_matrix_colmajor) |
| 577 | assert ('get_elem_nocopy(): incompatible function arguments.' in str(excinfo.value) and |
| 578 | ', flags.f_contiguous' in str(excinfo.value)) |
| 579 | assert get_elem_nocopy(dbl_matrix_colmajor) == 8 |
| 580 | with pytest.raises(TypeError) as excinfo: |
| 581 | get_elem_nocopy(int_matrix_rowmajor) |
| 582 | assert ('get_elem_nocopy(): incompatible function arguments.' in str(excinfo.value) and |
| 583 | ', flags.f_contiguous' in str(excinfo.value)) |
| 584 | with pytest.raises(TypeError) as excinfo: |
| 585 | get_elem_nocopy(dbl_matrix_rowmajor) |
| 586 | assert ('get_elem_nocopy(): incompatible function arguments.' in str(excinfo.value) and |
| 587 | ', flags.f_contiguous' in str(excinfo.value)) |
| 588 | |
| 589 | # For the row-major test, we take a long matrix in row-major, so only the third is allowed: |
| 590 | with pytest.raises(TypeError) as excinfo: |
| 591 | get_elem_rm_nocopy(int_matrix_colmajor) |
| 592 | assert ('get_elem_rm_nocopy(): incompatible function arguments.' in str(excinfo.value) and |
| 593 | ', flags.c_contiguous' in str(excinfo.value)) |
| 594 | with pytest.raises(TypeError) as excinfo: |
| 595 | get_elem_rm_nocopy(dbl_matrix_colmajor) |
| 596 | assert ('get_elem_rm_nocopy(): incompatible function arguments.' in str(excinfo.value) and |
| 597 | ', flags.c_contiguous' in str(excinfo.value)) |
| 598 | assert get_elem_rm_nocopy(int_matrix_rowmajor) == 8 |
| 599 | with pytest.raises(TypeError) as excinfo: |
| 600 | get_elem_rm_nocopy(dbl_matrix_rowmajor) |
| 601 | assert ('get_elem_rm_nocopy(): incompatible function arguments.' in str(excinfo.value) and |
| 602 | ', flags.c_contiguous' in str(excinfo.value)) |
| 603 | |
| 604 | |
Dean Moldovan | 30f6c3b | 2017-06-26 23:20:39 +0200 | [diff] [blame] | 605 | def test_eigen_ref_life_support(): |
| 606 | """Ensure the lifetime of temporary arrays created by the `Ref` caster |
| 607 | |
| 608 | The `Ref` caster sometimes creates a copy which needs to stay alive. This needs to |
| 609 | happen both for directs casts (just the array) or indirectly (e.g. list of arrays). |
| 610 | """ |
| 611 | from pybind11_tests import get_elem_direct, get_elem_indirect |
| 612 | |
| 613 | a = np.full(shape=10, fill_value=8, dtype=np.int8) |
| 614 | assert get_elem_direct(a) == 8 |
| 615 | |
| 616 | list_of_a = [a] |
| 617 | assert get_elem_indirect(list_of_a) == 8 |
| 618 | |
| 619 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 620 | def test_special_matrix_objects(): |
| 621 | from pybind11_tests import incr_diag, symmetric_upper, symmetric_lower |
| 622 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 623 | assert np.all(incr_diag(7) == np.diag([1., 2, 3, 4, 5, 6, 7])) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 624 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 625 | asymm = np.array([[ 1., 2, 3, 4], |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 626 | [ 5, 6, 7, 8], |
| 627 | [ 9, 10, 11, 12], |
| 628 | [13, 14, 15, 16]]) |
| 629 | symm_lower = np.array(asymm) |
| 630 | symm_upper = np.array(asymm) |
| 631 | for i in range(4): |
| 632 | for j in range(i + 1, 4): |
| 633 | symm_lower[i, j] = symm_lower[j, i] |
| 634 | symm_upper[j, i] = symm_upper[i, j] |
| 635 | |
| 636 | assert np.all(symmetric_lower(asymm) == symm_lower) |
| 637 | assert np.all(symmetric_upper(asymm) == symm_upper) |
| 638 | |
| 639 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 640 | def test_dense_signature(doc): |
Dean Moldovan | 5143989 | 2017-02-28 18:07:51 +0100 | [diff] [blame] | 641 | from pybind11_tests import double_col, double_row, double_complex, double_mat_rm |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 642 | |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 643 | assert doc(double_col) == """ |
| 644 | double_col(arg0: numpy.ndarray[float32[m, 1]]) -> numpy.ndarray[float32[m, 1]] |
| 645 | """ |
| 646 | assert doc(double_row) == """ |
| 647 | double_row(arg0: numpy.ndarray[float32[1, n]]) -> numpy.ndarray[float32[1, n]] |
| 648 | """ |
Dean Moldovan | 5143989 | 2017-02-28 18:07:51 +0100 | [diff] [blame] | 649 | assert doc(double_complex) == """ |
| 650 | double_complex(arg0: numpy.ndarray[complex64[m, 1]]) -> numpy.ndarray[complex64[m, 1]] |
| 651 | """ |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 652 | assert doc(double_mat_rm) == """ |
| 653 | double_mat_rm(arg0: numpy.ndarray[float32[m, n]]) -> numpy.ndarray[float32[m, n]] |
| 654 | """ |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 655 | |
| 656 | |
Jason Rhinelander | e9e1774 | 2017-04-08 19:26:42 -0400 | [diff] [blame] | 657 | def test_named_arguments(): |
| 658 | from pybind11_tests import matrix_multiply |
| 659 | |
| 660 | a = np.array([[1.0, 2], [3, 4], [5, 6]]) |
| 661 | b = np.ones((2, 1)) |
| 662 | |
| 663 | assert np.all(matrix_multiply(a, b) == np.array([[3.], [7], [11]])) |
| 664 | assert np.all(matrix_multiply(A=a, B=b) == np.array([[3.], [7], [11]])) |
| 665 | assert np.all(matrix_multiply(B=b, A=a) == np.array([[3.], [7], [11]])) |
| 666 | |
| 667 | with pytest.raises(ValueError) as excinfo: |
| 668 | matrix_multiply(b, a) |
| 669 | assert str(excinfo.value) == 'Nonconformable matrices!' |
| 670 | |
| 671 | with pytest.raises(ValueError) as excinfo: |
| 672 | matrix_multiply(A=b, B=a) |
| 673 | assert str(excinfo.value) == 'Nonconformable matrices!' |
| 674 | |
| 675 | with pytest.raises(ValueError) as excinfo: |
| 676 | matrix_multiply(B=a, A=b) |
| 677 | assert str(excinfo.value) == 'Nonconformable matrices!' |
| 678 | |
| 679 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 680 | @pytest.requires_eigen_and_scipy |
| 681 | def test_sparse(): |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 682 | from pybind11_tests import sparse_r, sparse_c, sparse_copy_r, sparse_copy_c |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 683 | |
| 684 | assert_sparse_equal_ref(sparse_r()) |
| 685 | assert_sparse_equal_ref(sparse_c()) |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 686 | assert_sparse_equal_ref(sparse_copy_r(sparse_r())) |
| 687 | assert_sparse_equal_ref(sparse_copy_c(sparse_c())) |
| 688 | assert_sparse_equal_ref(sparse_copy_r(sparse_c())) |
| 689 | assert_sparse_equal_ref(sparse_copy_c(sparse_r())) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 690 | |
| 691 | |
| 692 | @pytest.requires_eigen_and_scipy |
| 693 | def test_sparse_signature(doc): |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 694 | from pybind11_tests import sparse_copy_r, sparse_copy_c |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 695 | |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 696 | assert doc(sparse_copy_r) == """ |
| 697 | sparse_copy_r(arg0: scipy.sparse.csr_matrix[float32]) -> scipy.sparse.csr_matrix[float32] |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 698 | """ # noqa: E501 line too long |
Jason Rhinelander | 17d0283 | 2017-01-16 20:35:14 -0500 | [diff] [blame] | 699 | assert doc(sparse_copy_c) == """ |
| 700 | sparse_copy_c(arg0: scipy.sparse.csc_matrix[float32]) -> scipy.sparse.csc_matrix[float32] |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 701 | """ # noqa: E501 line too long |
Jason Rhinelander | efa8726 | 2017-03-17 14:51:52 -0300 | [diff] [blame] | 702 | |
| 703 | |
| 704 | def test_issue738(): |
| 705 | from pybind11_tests import iss738_f1, iss738_f2 |
| 706 | |
| 707 | assert np.all(iss738_f1(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]])) |
| 708 | assert np.all(iss738_f1(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]])) |
| 709 | |
| 710 | assert np.all(iss738_f2(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]])) |
| 711 | assert np.all(iss738_f2(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]])) |
Dean Moldovan | 0d765f4 | 2017-03-21 01:15:20 +0100 | [diff] [blame] | 712 | |
| 713 | |
| 714 | def test_custom_operator_new(): |
| 715 | """Using Eigen types as member variables requires a class-specific |
| 716 | operator new with proper alignment""" |
| 717 | from pybind11_tests import CustomOperatorNew |
| 718 | |
| 719 | o = CustomOperatorNew() |
| 720 | np.testing.assert_allclose(o.a, 0.0) |
| 721 | np.testing.assert_allclose(o.b.diagonal(), 1.0) |