blob: 64fb2e5adee0f90fe40cd68702cfcc5a9406c6a9 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
Jason Rhinelander391c7542017-07-25 16:47:36 -04002from pybind11_tests import ConstructorStats
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02003
Jason Rhinelander2a757842017-01-24 11:26:51 -05004pytestmark = pytest.requires_eigen_and_numpy
5
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02006with pytest.suppress(ImportError):
Jason Rhinelander391c7542017-07-25 16:47:36 -04007 from pybind11_tests import eigen as m
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02008 import numpy as np
9
Jason Rhinelander17d02832017-01-16 20:35:14 -050010 ref = np.array([[ 0., 3, 0, 0, 0, 11],
Dean Moldovan23919172016-08-25 17:08:09 +020011 [22, 0, 0, 0, 17, 11],
12 [ 7, 5, 0, 1, 0, 11],
13 [ 0, 0, 0, 0, 0, 11],
14 [ 0, 0, 14, 0, 8, 11]])
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020015
16
17def assert_equal_ref(mat):
18 np.testing.assert_array_equal(mat, ref)
19
20
21def assert_sparse_equal_ref(sparse_mat):
22 assert_equal_ref(sparse_mat.todense())
23
24
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020025def test_fixed():
Jason Rhinelander391c7542017-07-25 16:47:36 -040026 assert_equal_ref(m.fixed_c())
27 assert_equal_ref(m.fixed_r())
28 assert_equal_ref(m.fixed_copy_r(m.fixed_r()))
29 assert_equal_ref(m.fixed_copy_c(m.fixed_c()))
30 assert_equal_ref(m.fixed_copy_r(m.fixed_c()))
31 assert_equal_ref(m.fixed_copy_c(m.fixed_r()))
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020032
33
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020034def test_dense():
Jason Rhinelander391c7542017-07-25 16:47:36 -040035 assert_equal_ref(m.dense_r())
36 assert_equal_ref(m.dense_c())
37 assert_equal_ref(m.dense_copy_r(m.dense_r()))
38 assert_equal_ref(m.dense_copy_c(m.dense_c()))
39 assert_equal_ref(m.dense_copy_r(m.dense_c()))
40 assert_equal_ref(m.dense_copy_c(m.dense_r()))
Jason Rhinelander17d02832017-01-16 20:35:14 -050041
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020042
Jason Rhinelanderd9d224f2017-01-12 19:50:33 -050043def test_partially_fixed():
Jason Rhinelander17d02832017-01-16 20:35:14 -050044 ref2 = np.array([[0., 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]])
Jason Rhinelander391c7542017-07-25 16:47:36 -040045 np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2), ref2)
46 np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2), ref2)
47 np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2[:, 1]), ref2[:, [1]])
48 np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2[0, :]), ref2[[0], :])
49 np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)])
Jason Rhinelander17d02832017-01-16 20:35:14 -050050 np.testing.assert_array_equal(
Jason Rhinelander391c7542017-07-25 16:47:36 -040051 m.partial_copy_four_rm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :])
Jason Rhinelanderd9d224f2017-01-12 19:50:33 -050052
Jason Rhinelander391c7542017-07-25 16:47:36 -040053 np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2), ref2)
54 np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2), ref2)
55 np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2[:, 1]), ref2[:, [1]])
56 np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2[0, :]), ref2[[0], :])
57 np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)])
Jason Rhinelander17d02832017-01-16 20:35:14 -050058 np.testing.assert_array_equal(
Jason Rhinelander391c7542017-07-25 16:47:36 -040059 m.partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :])
Jason Rhinelander17d02832017-01-16 20:35:14 -050060
Dean Moldovan4567f1f2017-05-11 15:38:39 +020061 # TypeError should be raise for a shape mismatch
Jason Rhinelander391c7542017-07-25 16:47:36 -040062 functions = [m.partial_copy_four_rm_r, m.partial_copy_four_rm_c,
63 m.partial_copy_four_cm_r, m.partial_copy_four_cm_c]
Dean Moldovan4567f1f2017-05-11 15:38:39 +020064 matrix_with_wrong_shape = [[1, 2],
65 [3, 4]]
66 for f in functions:
67 with pytest.raises(TypeError) as excinfo:
68 f(matrix_with_wrong_shape)
69 assert "incompatible function arguments" in str(excinfo.value)
70
Jason Rhinelander17d02832017-01-16 20:35:14 -050071
Jason Rhinelander17d02832017-01-16 20:35:14 -050072def test_mutator_descriptors():
Jason Rhinelander17d02832017-01-16 20:35:14 -050073 zr = np.arange(30, dtype='float32').reshape(5, 6) # row-major
74 zc = zr.reshape(6, 5).transpose() # column-major
75
Jason Rhinelander391c7542017-07-25 16:47:36 -040076 m.fixed_mutator_r(zr)
77 m.fixed_mutator_c(zc)
78 m.fixed_mutator_a(zr)
79 m.fixed_mutator_a(zc)
Jason Rhinelander17d02832017-01-16 20:35:14 -050080 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -040081 m.fixed_mutator_r(zc)
Jason Rhinelandere9e17742017-04-08 19:26:42 -040082 assert ('(arg0: numpy.ndarray[float32[5, 6], flags.writeable, flags.c_contiguous]) -> None'
Jason Rhinelander17d02832017-01-16 20:35:14 -050083 in str(excinfo.value))
84 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -040085 m.fixed_mutator_c(zr)
Jason Rhinelandere9e17742017-04-08 19:26:42 -040086 assert ('(arg0: numpy.ndarray[float32[5, 6], flags.writeable, flags.f_contiguous]) -> None'
Jason Rhinelander17d02832017-01-16 20:35:14 -050087 in str(excinfo.value))
88 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -040089 m.fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype='float32'))
Jason Rhinelandere9e17742017-04-08 19:26:42 -040090 assert ('(arg0: numpy.ndarray[float32[5, 6], flags.writeable]) -> None'
Jason Rhinelander17d02832017-01-16 20:35:14 -050091 in str(excinfo.value))
92 zr.flags.writeable = False
93 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -040094 m.fixed_mutator_r(zr)
Jason Rhinelander17d02832017-01-16 20:35:14 -050095 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -040096 m.fixed_mutator_a(zr)
Jason Rhinelander17d02832017-01-16 20:35:14 -050097
98
Jason Rhinelander17d02832017-01-16 20:35:14 -050099def test_cpp_casting():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400100 assert m.cpp_copy(m.fixed_r()) == 22.
101 assert m.cpp_copy(m.fixed_c()) == 22.
Jason Rhinelander17d02832017-01-16 20:35:14 -0500102 z = np.array([[5., 6], [7, 8]])
Jason Rhinelander391c7542017-07-25 16:47:36 -0400103 assert m.cpp_copy(z) == 7.
104 assert m.cpp_copy(m.get_cm_ref()) == 21.
105 assert m.cpp_copy(m.get_rm_ref()) == 21.
106 assert m.cpp_ref_c(m.get_cm_ref()) == 21.
107 assert m.cpp_ref_r(m.get_rm_ref()) == 21.
Jason Rhinelander17d02832017-01-16 20:35:14 -0500108 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400109 # Can't reference m.fixed_c: it contains floats, m.cpp_ref_any wants doubles
110 m.cpp_ref_any(m.fixed_c())
Jason Rhinelander17d02832017-01-16 20:35:14 -0500111 assert 'Unable to cast Python instance' in str(excinfo.value)
112 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400113 # Can't reference m.fixed_r: it contains floats, m.cpp_ref_any wants doubles
114 m.cpp_ref_any(m.fixed_r())
Jason Rhinelander17d02832017-01-16 20:35:14 -0500115 assert 'Unable to cast Python instance' in str(excinfo.value)
Jason Rhinelander391c7542017-07-25 16:47:36 -0400116 assert m.cpp_ref_any(m.ReturnTester.create()) == 1.
Jason Rhinelander17d02832017-01-16 20:35:14 -0500117
Jason Rhinelander391c7542017-07-25 16:47:36 -0400118 assert m.cpp_ref_any(m.get_cm_ref()) == 21.
119 assert m.cpp_ref_any(m.get_cm_ref()) == 21.
Jason Rhinelander17d02832017-01-16 20:35:14 -0500120
121
Jason Rhinelander17d02832017-01-16 20:35:14 -0500122def test_pass_readonly_array():
Jason Rhinelander17d02832017-01-16 20:35:14 -0500123 z = np.full((5, 6), 42.0)
124 z.flags.writeable = False
Jason Rhinelander391c7542017-07-25 16:47:36 -0400125 np.testing.assert_array_equal(z, m.fixed_copy_r(z))
126 np.testing.assert_array_equal(m.fixed_r_const(), m.fixed_r())
127 assert not m.fixed_r_const().flags.writeable
128 np.testing.assert_array_equal(m.fixed_copy_r(m.fixed_r_const()), m.fixed_r_const())
Jason Rhinelander17d02832017-01-16 20:35:14 -0500129
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200130
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200131def test_nonunit_stride_from_python():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200132 counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3))
Jason Rhinelander17d02832017-01-16 20:35:14 -0500133 second_row = counting_mat[1, :]
134 second_col = counting_mat[:, 1]
Jason Rhinelander391c7542017-07-25 16:47:36 -0400135 np.testing.assert_array_equal(m.double_row(second_row), 2.0 * second_row)
136 np.testing.assert_array_equal(m.double_col(second_row), 2.0 * second_row)
137 np.testing.assert_array_equal(m.double_complex(second_row), 2.0 * second_row)
138 np.testing.assert_array_equal(m.double_row(second_col), 2.0 * second_col)
139 np.testing.assert_array_equal(m.double_col(second_col), 2.0 * second_col)
140 np.testing.assert_array_equal(m.double_complex(second_col), 2.0 * second_col)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200141
142 counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3))
143 slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
144 for slice_idx, ref_mat in enumerate(slices):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400145 np.testing.assert_array_equal(m.double_mat_cm(ref_mat), 2.0 * ref_mat)
146 np.testing.assert_array_equal(m.double_mat_rm(ref_mat), 2.0 * ref_mat)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200147
Jason Rhinelander17d02832017-01-16 20:35:14 -0500148 # Mutator:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400149 m.double_threer(second_row)
150 m.double_threec(second_col)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500151 np.testing.assert_array_equal(counting_mat, [[0., 2, 2], [6, 16, 10], [6, 14, 8]])
152
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200153
Cris Luengo627da3f2017-04-06 11:34:39 -0600154def test_negative_stride_from_python(msg):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400155 """Eigen doesn't support (as of yet) negative strides. When a function takes an Eigen matrix by
156 copy or const reference, we can pass a numpy array that has negative strides. Otherwise, an
157 exception will be thrown as Eigen will not be able to map the numpy array."""
Cris Luengo627da3f2017-04-06 11:34:39 -0600158
159 counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3))
160 counting_mat = counting_mat[::-1, ::-1]
161 second_row = counting_mat[1, :]
162 second_col = counting_mat[:, 1]
Jason Rhinelander391c7542017-07-25 16:47:36 -0400163 np.testing.assert_array_equal(m.double_row(second_row), 2.0 * second_row)
164 np.testing.assert_array_equal(m.double_col(second_row), 2.0 * second_row)
165 np.testing.assert_array_equal(m.double_complex(second_row), 2.0 * second_row)
166 np.testing.assert_array_equal(m.double_row(second_col), 2.0 * second_col)
167 np.testing.assert_array_equal(m.double_col(second_col), 2.0 * second_col)
168 np.testing.assert_array_equal(m.double_complex(second_col), 2.0 * second_col)
Cris Luengo627da3f2017-04-06 11:34:39 -0600169
170 counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3))
171 counting_3d = counting_3d[::-1, ::-1, ::-1]
172 slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
173 for slice_idx, ref_mat in enumerate(slices):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400174 np.testing.assert_array_equal(m.double_mat_cm(ref_mat), 2.0 * ref_mat)
175 np.testing.assert_array_equal(m.double_mat_rm(ref_mat), 2.0 * ref_mat)
Cris Luengo627da3f2017-04-06 11:34:39 -0600176
177 # Mutator:
178 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400179 m.double_threer(second_row)
Cris Luengo627da3f2017-04-06 11:34:39 -0600180 assert msg(excinfo.value) == """
Jason Rhinelander391c7542017-07-25 16:47:36 -0400181 double_threer(): incompatible function arguments. The following argument types are supported:
182 1. (arg0: numpy.ndarray[float32[1, 3], flags.writeable]) -> None
Cris Luengo627da3f2017-04-06 11:34:39 -0600183
Jason Rhinelander88efb252018-01-11 10:37:52 -0400184 Invoked with: """ + repr(np.array([ 5., 4., 3.], dtype='float32')) # noqa: E501 line too long
Cris Luengo627da3f2017-04-06 11:34:39 -0600185
186 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400187 m.double_threec(second_col)
Cris Luengo627da3f2017-04-06 11:34:39 -0600188 assert msg(excinfo.value) == """
Jason Rhinelander391c7542017-07-25 16:47:36 -0400189 double_threec(): incompatible function arguments. The following argument types are supported:
190 1. (arg0: numpy.ndarray[float32[3, 1], flags.writeable]) -> None
Cris Luengo627da3f2017-04-06 11:34:39 -0600191
Jason Rhinelander88efb252018-01-11 10:37:52 -0400192 Invoked with: """ + repr(np.array([ 7., 4., 1.], dtype='float32')) # noqa: E501 line too long
Cris Luengo627da3f2017-04-06 11:34:39 -0600193
194
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200195def test_nonunit_stride_to_python():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400196 assert np.all(m.diagonal(ref) == ref.diagonal())
197 assert np.all(m.diagonal_1(ref) == ref.diagonal(1))
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200198 for i in range(-5, 7):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400199 assert np.all(m.diagonal_n(ref, i) == ref.diagonal(i)), "m.diagonal_n({})".format(i)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200200
Jason Rhinelander391c7542017-07-25 16:47:36 -0400201 assert np.all(m.block(ref, 2, 1, 3, 3) == ref[2:5, 1:4])
202 assert np.all(m.block(ref, 1, 4, 4, 2) == ref[1:, 4:])
203 assert np.all(m.block(ref, 1, 4, 3, 2) == ref[1:4, 4:])
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200204
205
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200206def test_eigen_ref_to_python():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400207 chols = [m.cholesky1, m.cholesky2, m.cholesky3, m.cholesky4]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200208 for i, chol in enumerate(chols, start=1):
Jason Rhinelander17d02832017-01-16 20:35:14 -0500209 mymat = chol(np.array([[1., 2, 4], [2, 13, 23], [4, 23, 77]]))
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200210 assert np.all(mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])), "cholesky{}".format(i)
211
212
Jason Rhinelander17d02832017-01-16 20:35:14 -0500213def assign_both(a1, a2, r, c, v):
214 a1[r, c] = v
215 a2[r, c] = v
216
217
218def array_copy_but_one(a, r, c, v):
219 z = np.array(a, copy=True)
220 z[r, c] = v
221 return z
222
223
Jason Rhinelander17d02832017-01-16 20:35:14 -0500224def test_eigen_return_references():
225 """Tests various ways of returning references and non-referencing copies"""
Jason Rhinelander391c7542017-07-25 16:47:36 -0400226
Jason Rhinelander17d02832017-01-16 20:35:14 -0500227 master = np.ones((10, 10))
Jason Rhinelander391c7542017-07-25 16:47:36 -0400228 a = m.ReturnTester()
Jason Rhinelander17d02832017-01-16 20:35:14 -0500229 a_get1 = a.get()
230 assert not a_get1.flags.owndata and a_get1.flags.writeable
231 assign_both(a_get1, master, 3, 3, 5)
232 a_get2 = a.get_ptr()
233 assert not a_get2.flags.owndata and a_get2.flags.writeable
234 assign_both(a_get1, master, 2, 3, 6)
235
236 a_view1 = a.view()
237 assert not a_view1.flags.owndata and not a_view1.flags.writeable
238 with pytest.raises(ValueError):
239 a_view1[2, 3] = 4
240 a_view2 = a.view_ptr()
241 assert not a_view2.flags.owndata and not a_view2.flags.writeable
242 with pytest.raises(ValueError):
243 a_view2[2, 3] = 4
244
245 a_copy1 = a.copy_get()
246 assert a_copy1.flags.owndata and a_copy1.flags.writeable
247 np.testing.assert_array_equal(a_copy1, master)
248 a_copy1[7, 7] = -44 # Shouldn't affect anything else
249 c1want = array_copy_but_one(master, 7, 7, -44)
250 a_copy2 = a.copy_view()
251 assert a_copy2.flags.owndata and a_copy2.flags.writeable
252 np.testing.assert_array_equal(a_copy2, master)
253 a_copy2[4, 4] = -22 # Shouldn't affect anything else
254 c2want = array_copy_but_one(master, 4, 4, -22)
255
256 a_ref1 = a.ref()
257 assert not a_ref1.flags.owndata and a_ref1.flags.writeable
258 assign_both(a_ref1, master, 1, 1, 15)
259 a_ref2 = a.ref_const()
260 assert not a_ref2.flags.owndata and not a_ref2.flags.writeable
261 with pytest.raises(ValueError):
262 a_ref2[5, 5] = 33
263 a_ref3 = a.ref_safe()
264 assert not a_ref3.flags.owndata and a_ref3.flags.writeable
265 assign_both(a_ref3, master, 0, 7, 99)
266 a_ref4 = a.ref_const_safe()
267 assert not a_ref4.flags.owndata and not a_ref4.flags.writeable
268 with pytest.raises(ValueError):
269 a_ref4[7, 0] = 987654321
270
271 a_copy3 = a.copy_ref()
272 assert a_copy3.flags.owndata and a_copy3.flags.writeable
273 np.testing.assert_array_equal(a_copy3, master)
274 a_copy3[8, 1] = 11
275 c3want = array_copy_but_one(master, 8, 1, 11)
276 a_copy4 = a.copy_ref_const()
277 assert a_copy4.flags.owndata and a_copy4.flags.writeable
278 np.testing.assert_array_equal(a_copy4, master)
279 a_copy4[8, 4] = 88
280 c4want = array_copy_but_one(master, 8, 4, 88)
281
282 a_block1 = a.block(3, 3, 2, 2)
283 assert not a_block1.flags.owndata and a_block1.flags.writeable
284 a_block1[0, 0] = 55
285 master[3, 3] = 55
286 a_block2 = a.block_safe(2, 2, 3, 2)
287 assert not a_block2.flags.owndata and a_block2.flags.writeable
288 a_block2[2, 1] = -123
289 master[4, 3] = -123
290 a_block3 = a.block_const(6, 7, 4, 3)
291 assert not a_block3.flags.owndata and not a_block3.flags.writeable
292 with pytest.raises(ValueError):
293 a_block3[2, 2] = -44444
294
295 a_copy5 = a.copy_block(2, 2, 2, 3)
296 assert a_copy5.flags.owndata and a_copy5.flags.writeable
297 np.testing.assert_array_equal(a_copy5, master[2:4, 2:5])
298 a_copy5[1, 1] = 777
299 c5want = array_copy_but_one(master[2:4, 2:5], 1, 1, 777)
300
301 a_corn1 = a.corners()
302 assert not a_corn1.flags.owndata and a_corn1.flags.writeable
303 a_corn1 *= 50
304 a_corn1[1, 1] = 999
305 master[0, 0] = 50
306 master[0, 9] = 50
307 master[9, 0] = 50
308 master[9, 9] = 999
309 a_corn2 = a.corners_const()
310 assert not a_corn2.flags.owndata and not a_corn2.flags.writeable
311 with pytest.raises(ValueError):
312 a_corn2[1, 0] = 51
313
314 # All of the changes made all the way along should be visible everywhere
315 # now (except for the copies, of course)
316 np.testing.assert_array_equal(a_get1, master)
317 np.testing.assert_array_equal(a_get2, master)
318 np.testing.assert_array_equal(a_view1, master)
319 np.testing.assert_array_equal(a_view2, master)
320 np.testing.assert_array_equal(a_ref1, master)
321 np.testing.assert_array_equal(a_ref2, master)
322 np.testing.assert_array_equal(a_ref3, master)
323 np.testing.assert_array_equal(a_ref4, master)
324 np.testing.assert_array_equal(a_block1, master[3:5, 3:5])
325 np.testing.assert_array_equal(a_block2, master[2:5, 2:4])
326 np.testing.assert_array_equal(a_block3, master[6:10, 7:10])
327 np.testing.assert_array_equal(a_corn1, master[0::master.shape[0] - 1, 0::master.shape[1] - 1])
328 np.testing.assert_array_equal(a_corn2, master[0::master.shape[0] - 1, 0::master.shape[1] - 1])
329
330 np.testing.assert_array_equal(a_copy1, c1want)
331 np.testing.assert_array_equal(a_copy2, c2want)
332 np.testing.assert_array_equal(a_copy3, c3want)
333 np.testing.assert_array_equal(a_copy4, c4want)
334 np.testing.assert_array_equal(a_copy5, c5want)
335
336
337def assert_keeps_alive(cl, method, *args):
Jason Rhinelander17d02832017-01-16 20:35:14 -0500338 cstats = ConstructorStats.get(cl)
339 start_with = cstats.alive()
340 a = cl()
341 assert cstats.alive() == start_with + 1
342 z = method(a, *args)
343 assert cstats.alive() == start_with + 1
344 del a
345 # Here's the keep alive in action:
346 assert cstats.alive() == start_with + 1
347 del z
348 # Keep alive should have expired:
349 assert cstats.alive() == start_with
350
351
Jason Rhinelander17d02832017-01-16 20:35:14 -0500352def test_eigen_keepalive():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400353 a = m.ReturnTester()
354 cstats = ConstructorStats.get(m.ReturnTester)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500355 assert cstats.alive() == 1
356 unsafe = [a.ref(), a.ref_const(), a.block(1, 2, 3, 4)]
357 copies = [a.copy_get(), a.copy_view(), a.copy_ref(), a.copy_ref_const(),
358 a.copy_block(4, 3, 2, 1)]
359 del a
360 assert cstats.alive() == 0
361 del unsafe
362 del copies
363
Jason Rhinelander391c7542017-07-25 16:47:36 -0400364 for meth in [m.ReturnTester.get, m.ReturnTester.get_ptr, m.ReturnTester.view,
365 m.ReturnTester.view_ptr, m.ReturnTester.ref_safe, m.ReturnTester.ref_const_safe,
366 m.ReturnTester.corners, m.ReturnTester.corners_const]:
367 assert_keeps_alive(m.ReturnTester, meth)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500368
Jason Rhinelander391c7542017-07-25 16:47:36 -0400369 for meth in [m.ReturnTester.block_safe, m.ReturnTester.block_const]:
370 assert_keeps_alive(m.ReturnTester, meth, 4, 3, 2, 1)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500371
372
Jason Rhinelander17d02832017-01-16 20:35:14 -0500373def test_eigen_ref_mutators():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400374 """Tests Eigen's ability to mutate numpy values"""
375
Jason Rhinelander17d02832017-01-16 20:35:14 -0500376 orig = np.array([[1., 2, 3], [4, 5, 6], [7, 8, 9]])
377 zr = np.array(orig)
378 zc = np.array(orig, order='F')
Jason Rhinelander391c7542017-07-25 16:47:36 -0400379 m.add_rm(zr, 1, 0, 100)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500380 assert np.all(zr == np.array([[1., 2, 3], [104, 5, 6], [7, 8, 9]]))
Jason Rhinelander391c7542017-07-25 16:47:36 -0400381 m.add_cm(zc, 1, 0, 200)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500382 assert np.all(zc == np.array([[1., 2, 3], [204, 5, 6], [7, 8, 9]]))
383
Jason Rhinelander391c7542017-07-25 16:47:36 -0400384 m.add_any(zr, 1, 0, 20)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500385 assert np.all(zr == np.array([[1., 2, 3], [124, 5, 6], [7, 8, 9]]))
Jason Rhinelander391c7542017-07-25 16:47:36 -0400386 m.add_any(zc, 1, 0, 10)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500387 assert np.all(zc == np.array([[1., 2, 3], [214, 5, 6], [7, 8, 9]]))
388
389 # Can't reference a col-major array with a row-major Ref, and vice versa:
390 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400391 m.add_rm(zc, 1, 0, 1)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500392 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400393 m.add_cm(zr, 1, 0, 1)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500394
395 # Overloads:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400396 m.add1(zr, 1, 0, -100)
397 m.add2(zr, 1, 0, -20)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500398 assert np.all(zr == orig)
Jason Rhinelander391c7542017-07-25 16:47:36 -0400399 m.add1(zc, 1, 0, -200)
400 m.add2(zc, 1, 0, -10)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500401 assert np.all(zc == orig)
402
403 # a non-contiguous slice (this won't work on either the row- or
404 # column-contiguous refs, but should work for the any)
405 cornersr = zr[0::2, 0::2]
406 cornersc = zc[0::2, 0::2]
407
408 assert np.all(cornersr == np.array([[1., 3], [7, 9]]))
409 assert np.all(cornersc == np.array([[1., 3], [7, 9]]))
410
411 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400412 m.add_rm(cornersr, 0, 1, 25)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500413 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400414 m.add_cm(cornersr, 0, 1, 25)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500415 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400416 m.add_rm(cornersc, 0, 1, 25)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500417 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400418 m.add_cm(cornersc, 0, 1, 25)
419 m.add_any(cornersr, 0, 1, 25)
420 m.add_any(cornersc, 0, 1, 44)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500421 assert np.all(zr == np.array([[1., 2, 28], [4, 5, 6], [7, 8, 9]]))
422 assert np.all(zc == np.array([[1., 2, 47], [4, 5, 6], [7, 8, 9]]))
423
424 # You shouldn't be allowed to pass a non-writeable array to a mutating Eigen method:
425 zro = zr[0:4, 0:4]
426 zro.flags.writeable = False
427 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400428 m.add_rm(zro, 0, 0, 0)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500429 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400430 m.add_any(zro, 0, 0, 0)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500431 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400432 m.add1(zro, 0, 0, 0)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500433 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400434 m.add2(zro, 0, 0, 0)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500435
436 # integer array shouldn't be passable to a double-matrix-accepting mutating func:
437 zi = np.array([[1, 2], [3, 4]])
438 with pytest.raises(TypeError):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400439 m.add_rm(zi)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500440
441
Jason Rhinelander17d02832017-01-16 20:35:14 -0500442def test_numpy_ref_mutators():
443 """Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)"""
Jason Rhinelander17d02832017-01-16 20:35:14 -0500444
Jason Rhinelander391c7542017-07-25 16:47:36 -0400445 m.reset_refs() # In case another test already changed it
446
447 zc = m.get_cm_ref()
448 zcro = m.get_cm_const_ref()
449 zr = m.get_rm_ref()
450 zrro = m.get_rm_const_ref()
Jason Rhinelander17d02832017-01-16 20:35:14 -0500451
452 assert [zc[1, 2], zcro[1, 2], zr[1, 2], zrro[1, 2]] == [23] * 4
453
454 assert not zc.flags.owndata and zc.flags.writeable
455 assert not zr.flags.owndata and zr.flags.writeable
456 assert not zcro.flags.owndata and not zcro.flags.writeable
457 assert not zrro.flags.owndata and not zrro.flags.writeable
458
459 zc[1, 2] = 99
460 expect = np.array([[11., 12, 13], [21, 22, 99], [31, 32, 33]])
461 # We should have just changed zc, of course, but also zcro and the original eigen matrix
462 assert np.all(zc == expect)
463 assert np.all(zcro == expect)
Jason Rhinelander391c7542017-07-25 16:47:36 -0400464 assert np.all(m.get_cm_ref() == expect)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500465
466 zr[1, 2] = 99
467 assert np.all(zr == expect)
468 assert np.all(zrro == expect)
Jason Rhinelander391c7542017-07-25 16:47:36 -0400469 assert np.all(m.get_rm_ref() == expect)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500470
471 # Make sure the readonly ones are numpy-readonly:
472 with pytest.raises(ValueError):
473 zcro[1, 2] = 6
474 with pytest.raises(ValueError):
475 zrro[1, 2] = 6
476
477 # We should be able to explicitly copy like this (and since we're copying,
478 # the const should drop away)
Jason Rhinelander391c7542017-07-25 16:47:36 -0400479 y1 = np.array(m.get_cm_const_ref())
Jason Rhinelander17d02832017-01-16 20:35:14 -0500480
481 assert y1.flags.owndata and y1.flags.writeable
482 # We should get copies of the eigen data, which was modified above:
483 assert y1[1, 2] == 99
484 y1[1, 2] += 12
485 assert y1[1, 2] == 111
486 assert zc[1, 2] == 99 # Make sure we aren't referencing the original
487
488
Jason Rhinelander17d02832017-01-16 20:35:14 -0500489def test_both_ref_mutators():
490 """Tests a complex chain of nested eigen/numpy references"""
Jason Rhinelander17d02832017-01-16 20:35:14 -0500491
Jason Rhinelander391c7542017-07-25 16:47:36 -0400492 m.reset_refs() # In case another test already changed it
493
494 z = m.get_cm_ref() # numpy -> eigen
Jason Rhinelander17d02832017-01-16 20:35:14 -0500495 z[0, 2] -= 3
Jason Rhinelander391c7542017-07-25 16:47:36 -0400496 z2 = m.incr_matrix(z, 1) # numpy -> eigen -> numpy -> eigen
Jason Rhinelander17d02832017-01-16 20:35:14 -0500497 z2[1, 1] += 6
Jason Rhinelander391c7542017-07-25 16:47:36 -0400498 z3 = m.incr_matrix(z, 2) # (numpy -> eigen)^3
Jason Rhinelander17d02832017-01-16 20:35:14 -0500499 z3[2, 2] += -5
Jason Rhinelander391c7542017-07-25 16:47:36 -0400500 z4 = m.incr_matrix(z, 3) # (numpy -> eigen)^4
Jason Rhinelander17d02832017-01-16 20:35:14 -0500501 z4[1, 1] -= 1
Jason Rhinelander391c7542017-07-25 16:47:36 -0400502 z5 = m.incr_matrix(z, 4) # (numpy -> eigen)^5
Jason Rhinelander17d02832017-01-16 20:35:14 -0500503 z5[0, 0] = 0
504 assert np.all(z == z2)
505 assert np.all(z == z3)
506 assert np.all(z == z4)
507 assert np.all(z == z5)
508 expect = np.array([[0., 22, 20], [31, 37, 33], [41, 42, 38]])
509 assert np.all(z == expect)
510
511 y = np.array(range(100), dtype='float64').reshape(10, 10)
Jason Rhinelander391c7542017-07-25 16:47:36 -0400512 y2 = m.incr_matrix_any(y, 10) # np -> eigen -> np
513 y3 = m.incr_matrix_any(y2[0::2, 0::2], -33) # np -> eigen -> np slice -> np -> eigen -> np
514 y4 = m.even_rows(y3) # numpy -> eigen slice -> (... y3)
515 y5 = m.even_cols(y4) # numpy -> eigen slice -> (... y4)
516 y6 = m.incr_matrix_any(y5, 1000) # numpy -> eigen -> (... y5)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500517
518 # Apply same mutations using just numpy:
519 yexpect = np.array(range(100), dtype='float64').reshape(10, 10)
520 yexpect += 10
521 yexpect[0::2, 0::2] -= 33
522 yexpect[0::4, 0::4] += 1000
523 assert np.all(y6 == yexpect[0::4, 0::4])
524 assert np.all(y5 == yexpect[0::4, 0::4])
525 assert np.all(y4 == yexpect[0::4, 0::2])
526 assert np.all(y3 == yexpect[0::2, 0::2])
527 assert np.all(y2 == yexpect)
528 assert np.all(y == yexpect)
529
530
Jason Rhinelander17d02832017-01-16 20:35:14 -0500531def test_nocopy_wrapper():
Jason Rhinelander17d02832017-01-16 20:35:14 -0500532 # get_elem requires a column-contiguous matrix reference, but should be
533 # callable with other types of matrix (via copying):
534 int_matrix_colmajor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], order='F')
535 dbl_matrix_colmajor = np.array(int_matrix_colmajor, dtype='double', order='F', copy=True)
536 int_matrix_rowmajor = np.array(int_matrix_colmajor, order='C', copy=True)
537 dbl_matrix_rowmajor = np.array(int_matrix_rowmajor, dtype='double', order='C', copy=True)
538
539 # All should be callable via get_elem:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400540 assert m.get_elem(int_matrix_colmajor) == 8
541 assert m.get_elem(dbl_matrix_colmajor) == 8
542 assert m.get_elem(int_matrix_rowmajor) == 8
543 assert m.get_elem(dbl_matrix_rowmajor) == 8
Jason Rhinelander17d02832017-01-16 20:35:14 -0500544
Jason Rhinelander391c7542017-07-25 16:47:36 -0400545 # All but the second should fail with m.get_elem_nocopy:
Jason Rhinelander17d02832017-01-16 20:35:14 -0500546 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400547 m.get_elem_nocopy(int_matrix_colmajor)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500548 assert ('get_elem_nocopy(): incompatible function arguments.' in str(excinfo.value) and
549 ', flags.f_contiguous' in str(excinfo.value))
Jason Rhinelander391c7542017-07-25 16:47:36 -0400550 assert m.get_elem_nocopy(dbl_matrix_colmajor) == 8
Jason Rhinelander17d02832017-01-16 20:35:14 -0500551 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400552 m.get_elem_nocopy(int_matrix_rowmajor)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500553 assert ('get_elem_nocopy(): incompatible function arguments.' in str(excinfo.value) and
554 ', flags.f_contiguous' in str(excinfo.value))
555 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400556 m.get_elem_nocopy(dbl_matrix_rowmajor)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500557 assert ('get_elem_nocopy(): incompatible function arguments.' in str(excinfo.value) and
558 ', flags.f_contiguous' in str(excinfo.value))
559
560 # For the row-major test, we take a long matrix in row-major, so only the third is allowed:
561 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400562 m.get_elem_rm_nocopy(int_matrix_colmajor)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500563 assert ('get_elem_rm_nocopy(): incompatible function arguments.' in str(excinfo.value) and
564 ', flags.c_contiguous' in str(excinfo.value))
565 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400566 m.get_elem_rm_nocopy(dbl_matrix_colmajor)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500567 assert ('get_elem_rm_nocopy(): incompatible function arguments.' in str(excinfo.value) and
568 ', flags.c_contiguous' in str(excinfo.value))
Jason Rhinelander391c7542017-07-25 16:47:36 -0400569 assert m.get_elem_rm_nocopy(int_matrix_rowmajor) == 8
Jason Rhinelander17d02832017-01-16 20:35:14 -0500570 with pytest.raises(TypeError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400571 m.get_elem_rm_nocopy(dbl_matrix_rowmajor)
Jason Rhinelander17d02832017-01-16 20:35:14 -0500572 assert ('get_elem_rm_nocopy(): incompatible function arguments.' in str(excinfo.value) and
573 ', flags.c_contiguous' in str(excinfo.value))
574
575
Dean Moldovan30f6c3b2017-06-26 23:20:39 +0200576def test_eigen_ref_life_support():
577 """Ensure the lifetime of temporary arrays created by the `Ref` caster
578
579 The `Ref` caster sometimes creates a copy which needs to stay alive. This needs to
580 happen both for directs casts (just the array) or indirectly (e.g. list of arrays).
581 """
Dean Moldovan30f6c3b2017-06-26 23:20:39 +0200582
583 a = np.full(shape=10, fill_value=8, dtype=np.int8)
Jason Rhinelander391c7542017-07-25 16:47:36 -0400584 assert m.get_elem_direct(a) == 8
Dean Moldovan30f6c3b2017-06-26 23:20:39 +0200585
586 list_of_a = [a]
Jason Rhinelander391c7542017-07-25 16:47:36 -0400587 assert m.get_elem_indirect(list_of_a) == 8
Dean Moldovan30f6c3b2017-06-26 23:20:39 +0200588
589
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200590def test_special_matrix_objects():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400591 assert np.all(m.incr_diag(7) == np.diag([1., 2, 3, 4, 5, 6, 7]))
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200592
Jason Rhinelander17d02832017-01-16 20:35:14 -0500593 asymm = np.array([[ 1., 2, 3, 4],
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200594 [ 5, 6, 7, 8],
595 [ 9, 10, 11, 12],
596 [13, 14, 15, 16]])
597 symm_lower = np.array(asymm)
598 symm_upper = np.array(asymm)
599 for i in range(4):
600 for j in range(i + 1, 4):
601 symm_lower[i, j] = symm_lower[j, i]
602 symm_upper[j, i] = symm_upper[i, j]
603
Jason Rhinelander391c7542017-07-25 16:47:36 -0400604 assert np.all(m.symmetric_lower(asymm) == symm_lower)
605 assert np.all(m.symmetric_upper(asymm) == symm_upper)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200606
607
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200608def test_dense_signature(doc):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400609 assert doc(m.double_col) == """
Dean Moldovan76e993a2016-12-13 00:59:28 +0100610 double_col(arg0: numpy.ndarray[float32[m, 1]]) -> numpy.ndarray[float32[m, 1]]
611 """
Jason Rhinelander391c7542017-07-25 16:47:36 -0400612 assert doc(m.double_row) == """
Dean Moldovan76e993a2016-12-13 00:59:28 +0100613 double_row(arg0: numpy.ndarray[float32[1, n]]) -> numpy.ndarray[float32[1, n]]
614 """
Jason Rhinelander391c7542017-07-25 16:47:36 -0400615 assert doc(m.double_complex) == """
Dean Moldovan51439892017-02-28 18:07:51 +0100616 double_complex(arg0: numpy.ndarray[complex64[m, 1]]) -> numpy.ndarray[complex64[m, 1]]
617 """
Jason Rhinelander391c7542017-07-25 16:47:36 -0400618 assert doc(m.double_mat_rm) == """
Dean Moldovan76e993a2016-12-13 00:59:28 +0100619 double_mat_rm(arg0: numpy.ndarray[float32[m, n]]) -> numpy.ndarray[float32[m, n]]
620 """
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200621
622
Jason Rhinelandere9e17742017-04-08 19:26:42 -0400623def test_named_arguments():
Jason Rhinelandere9e17742017-04-08 19:26:42 -0400624 a = np.array([[1.0, 2], [3, 4], [5, 6]])
625 b = np.ones((2, 1))
626
Jason Rhinelander391c7542017-07-25 16:47:36 -0400627 assert np.all(m.matrix_multiply(a, b) == np.array([[3.], [7], [11]]))
628 assert np.all(m.matrix_multiply(A=a, B=b) == np.array([[3.], [7], [11]]))
629 assert np.all(m.matrix_multiply(B=b, A=a) == np.array([[3.], [7], [11]]))
Jason Rhinelandere9e17742017-04-08 19:26:42 -0400630
631 with pytest.raises(ValueError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400632 m.matrix_multiply(b, a)
Jason Rhinelandere9e17742017-04-08 19:26:42 -0400633 assert str(excinfo.value) == 'Nonconformable matrices!'
634
635 with pytest.raises(ValueError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400636 m.matrix_multiply(A=b, B=a)
Jason Rhinelandere9e17742017-04-08 19:26:42 -0400637 assert str(excinfo.value) == 'Nonconformable matrices!'
638
639 with pytest.raises(ValueError) as excinfo:
Jason Rhinelander391c7542017-07-25 16:47:36 -0400640 m.matrix_multiply(B=a, A=b)
Jason Rhinelandere9e17742017-04-08 19:26:42 -0400641 assert str(excinfo.value) == 'Nonconformable matrices!'
642
643
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200644@pytest.requires_eigen_and_scipy
645def test_sparse():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400646 assert_sparse_equal_ref(m.sparse_r())
647 assert_sparse_equal_ref(m.sparse_c())
648 assert_sparse_equal_ref(m.sparse_copy_r(m.sparse_r()))
649 assert_sparse_equal_ref(m.sparse_copy_c(m.sparse_c()))
650 assert_sparse_equal_ref(m.sparse_copy_r(m.sparse_c()))
651 assert_sparse_equal_ref(m.sparse_copy_c(m.sparse_r()))
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200652
653
654@pytest.requires_eigen_and_scipy
655def test_sparse_signature(doc):
Jason Rhinelander391c7542017-07-25 16:47:36 -0400656 assert doc(m.sparse_copy_r) == """
Jason Rhinelander17d02832017-01-16 20:35:14 -0500657 sparse_copy_r(arg0: scipy.sparse.csr_matrix[float32]) -> scipy.sparse.csr_matrix[float32]
Dean Moldovan76e993a2016-12-13 00:59:28 +0100658 """ # noqa: E501 line too long
Jason Rhinelander391c7542017-07-25 16:47:36 -0400659 assert doc(m.sparse_copy_c) == """
Jason Rhinelander17d02832017-01-16 20:35:14 -0500660 sparse_copy_c(arg0: scipy.sparse.csc_matrix[float32]) -> scipy.sparse.csc_matrix[float32]
Dean Moldovan76e993a2016-12-13 00:59:28 +0100661 """ # noqa: E501 line too long
Jason Rhinelanderefa87262017-03-17 14:51:52 -0300662
663
664def test_issue738():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400665 """Ignore strides on a length-1 dimension (even if they would be incompatible length > 1)"""
666 assert np.all(m.iss738_f1(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]]))
667 assert np.all(m.iss738_f1(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]]))
Jason Rhinelanderefa87262017-03-17 14:51:52 -0300668
Jason Rhinelander391c7542017-07-25 16:47:36 -0400669 assert np.all(m.iss738_f2(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]]))
670 assert np.all(m.iss738_f2(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]]))
Dean Moldovan0d765f42017-03-21 01:15:20 +0100671
672
Jason Rhinelander6a81dbb2017-09-21 19:09:39 -0300673def test_issue1105():
674 """Issue 1105: 1xN or Nx1 input arrays weren't accepted for eigen
675 compile-time row vectors or column vector"""
676 assert m.iss1105_row(np.ones((1, 7)))
677 assert m.iss1105_col(np.ones((7, 1)))
678
679 # These should still fail (incompatible dimensions):
680 with pytest.raises(TypeError) as excinfo:
681 m.iss1105_row(np.ones((7, 1)))
682 assert "incompatible function arguments" in str(excinfo)
683 with pytest.raises(TypeError) as excinfo:
684 m.iss1105_col(np.ones((1, 7)))
685 assert "incompatible function arguments" in str(excinfo)
686
687
Dean Moldovan0d765f42017-03-21 01:15:20 +0100688def test_custom_operator_new():
689 """Using Eigen types as member variables requires a class-specific
690 operator new with proper alignment"""
Dean Moldovan0d765f42017-03-21 01:15:20 +0100691
Jason Rhinelander391c7542017-07-25 16:47:36 -0400692 o = m.CustomOperatorNew()
Dean Moldovan0d765f42017-03-21 01:15:20 +0100693 np.testing.assert_allclose(o.a, 0.0)
694 np.testing.assert_allclose(o.b.diagonal(), 1.0)