1 from __future__
import annotations
5 from pybind11_tests
import ConstructorStats
7 np = pytest.importorskip(
"numpy")
8 m = pytest.importorskip(
"pybind11_tests.eigen_matrix")
13 [0.0, 3, 0, 0, 0, 11],
14 [22, 0, 0, 0, 17, 11],
23 np.testing.assert_array_equal(mat, ref)
49 ref2 = np.array([[0.0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]])
50 np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2), ref2)
51 np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2), ref2)
52 np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2[:, 1]), ref2[:, [1]])
53 np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2[0, :]), ref2[[0], :])
54 np.testing.assert_array_equal(
55 m.partial_copy_four_rm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)]
57 np.testing.assert_array_equal(
58 m.partial_copy_four_rm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :]
61 np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2), ref2)
62 np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2), ref2)
63 np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2[:, 1]), ref2[:, [1]])
64 np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2[0, :]), ref2[[0], :])
65 np.testing.assert_array_equal(
66 m.partial_copy_four_cm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)]
68 np.testing.assert_array_equal(
69 m.partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :]
74 m.partial_copy_four_rm_r,
75 m.partial_copy_four_rm_c,
76 m.partial_copy_four_cm_r,
77 m.partial_copy_four_cm_c,
79 matrix_with_wrong_shape = [[1, 2], [3, 4]]
81 with pytest.raises(TypeError)
as excinfo:
82 f(matrix_with_wrong_shape)
83 assert "incompatible function arguments" in str(excinfo.value)
87 zr = np.arange(30, dtype=
"float32").
reshape(5, 6)
88 zc = zr.reshape(6, 5).transpose()
94 with pytest.raises(TypeError)
as excinfo:
97 "(arg0: numpy.ndarray[numpy.float32[5, 6],"
98 " flags.writeable, flags.c_contiguous]) -> None" in str(excinfo.value)
100 with pytest.raises(TypeError)
as excinfo:
101 m.fixed_mutator_c(zr)
103 "(arg0: numpy.ndarray[numpy.float32[5, 6],"
104 " flags.writeable, flags.f_contiguous]) -> None" in str(excinfo.value)
106 with pytest.raises(TypeError)
as excinfo:
107 m.fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype=
"float32"))
108 assert "(arg0: numpy.ndarray[numpy.float32[5, 6], flags.writeable]) -> None" in str(
111 zr.flags.writeable =
False
112 with pytest.raises(TypeError):
113 m.fixed_mutator_r(zr)
114 with pytest.raises(TypeError):
115 m.fixed_mutator_a(zr)
119 assert m.cpp_copy(m.fixed_r()) == 22.0
120 assert m.cpp_copy(m.fixed_c()) == 22.0
121 z = np.array([[5.0, 6], [7, 8]])
122 assert m.cpp_copy(z) == 7.0
123 assert m.cpp_copy(m.get_cm_ref()) == 21.0
124 assert m.cpp_copy(m.get_rm_ref()) == 21.0
125 assert m.cpp_ref_c(m.get_cm_ref()) == 21.0
126 assert m.cpp_ref_r(m.get_rm_ref()) == 21.0
127 with pytest.raises(RuntimeError)
as excinfo:
129 m.cpp_ref_any(m.fixed_c())
130 assert "Unable to cast Python instance" in str(excinfo.value)
131 with pytest.raises(RuntimeError)
as excinfo:
133 m.cpp_ref_any(m.fixed_r())
134 assert "Unable to cast Python instance" in str(excinfo.value)
135 assert m.cpp_ref_any(m.ReturnTester.create()) == 1.0
137 assert m.cpp_ref_any(m.get_cm_ref()) == 21.0
138 assert m.cpp_ref_any(m.get_cm_ref()) == 21.0
142 z = np.full((5, 6), 42.0)
143 z.flags.writeable =
False
144 np.testing.assert_array_equal(z, m.fixed_copy_r(z))
145 np.testing.assert_array_equal(m.fixed_r_const(), m.fixed_r())
146 assert not m.fixed_r_const().flags.writeable
147 np.testing.assert_array_equal(m.fixed_copy_r(m.fixed_r_const()), m.fixed_r_const())
151 counting_mat = np.arange(9.0, dtype=np.float32).
reshape((3, 3))
152 second_row = counting_mat[1, :]
153 second_col = counting_mat[:, 1]
154 np.testing.assert_array_equal(m.double_row(second_row), 2.0 * second_row)
155 np.testing.assert_array_equal(m.double_col(second_row), 2.0 * second_row)
156 np.testing.assert_array_equal(m.double_complex(second_row), 2.0 * second_row)
157 np.testing.assert_array_equal(m.double_row(second_col), 2.0 * second_col)
158 np.testing.assert_array_equal(m.double_col(second_col), 2.0 * second_col)
159 np.testing.assert_array_equal(m.double_complex(second_col), 2.0 * second_col)
161 counting_3d = np.arange(27.0, dtype=np.float32).
reshape((3, 3, 3))
162 slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
163 for ref_mat
in slices:
164 np.testing.assert_array_equal(m.double_mat_cm(ref_mat), 2.0 * ref_mat)
165 np.testing.assert_array_equal(m.double_mat_rm(ref_mat), 2.0 * ref_mat)
168 m.double_threer(second_row)
169 m.double_threec(second_col)
170 np.testing.assert_array_equal(counting_mat, [[0.0, 2, 2], [6, 16, 10], [6, 14, 8]])
174 """Eigen doesn't support (as of yet) negative strides. When a function takes an Eigen matrix by
175 copy or const reference, we can pass a numpy array that has negative strides. Otherwise, an
176 exception will be thrown as Eigen will not be able to map the numpy array."""
178 counting_mat = np.arange(9.0, dtype=np.float32).
reshape((3, 3))
179 counting_mat = counting_mat[::-1, ::-1]
180 second_row = counting_mat[1, :]
181 second_col = counting_mat[:, 1]
182 np.testing.assert_array_equal(m.double_row(second_row), 2.0 * second_row)
183 np.testing.assert_array_equal(m.double_col(second_row), 2.0 * second_row)
184 np.testing.assert_array_equal(m.double_complex(second_row), 2.0 * second_row)
185 np.testing.assert_array_equal(m.double_row(second_col), 2.0 * second_col)
186 np.testing.assert_array_equal(m.double_col(second_col), 2.0 * second_col)
187 np.testing.assert_array_equal(m.double_complex(second_col), 2.0 * second_col)
189 counting_3d = np.arange(27.0, dtype=np.float32).
reshape((3, 3, 3))
190 counting_3d = counting_3d[::-1, ::-1, ::-1]
191 slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
192 for ref_mat
in slices:
193 np.testing.assert_array_equal(m.double_mat_cm(ref_mat), 2.0 * ref_mat)
194 np.testing.assert_array_equal(m.double_mat_rm(ref_mat), 2.0 * ref_mat)
197 with pytest.raises(TypeError)
as excinfo:
198 m.double_threer(second_row)
202 double_threer(): incompatible function arguments. The following argument types are supported:
203 1. (arg0: numpy.ndarray[numpy.float32[1, 3], flags.writeable]) -> None
206 +
repr(np.array([5.0, 4.0, 3.0], dtype=
"float32"))
209 with pytest.raises(TypeError)
as excinfo:
210 m.double_threec(second_col)
214 double_threec(): incompatible function arguments. The following argument types are supported:
215 1. (arg0: numpy.ndarray[numpy.float32[3, 1], flags.writeable]) -> None
218 +
repr(np.array([7.0, 4.0, 1.0], dtype=
"float32"))
223 with pytest.raises(RuntimeError)
as excinfo:
224 m.block(ref, 0, 0, 0, 0)
225 assert str(excinfo.value) ==
"type_caster for Eigen::Ref made a copy."
229 assert np.all(m.diagonal(ref) == ref.diagonal())
230 assert np.all(m.diagonal_1(ref) == ref.diagonal(1))
231 for i
in range(-5, 7):
232 assert np.all(m.diagonal_n(ref, i) == ref.diagonal(i)), f
"m.diagonal_n({i})"
236 rof = np.asarray(ref, order=
"F")
237 assert np.all(m.block(rof, 2, 1, 3, 3) == rof[2:5, 1:4])
238 assert np.all(m.block(rof, 1, 4, 4, 2) == rof[1:, 4:])
239 assert np.all(m.block(rof, 1, 4, 3, 2) == rof[1:4, 4:])
243 chols = [m.cholesky1, m.cholesky2, m.cholesky3, m.cholesky4]
244 for i, chol
in enumerate(chols, start=1):
245 mymat = chol(np.array([[1.0, 2, 4], [2, 13, 23], [4, 23, 77]]))
247 mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])
257 z = np.array(a, copy=
True)
263 """Tests various ways of returning references and non-referencing copies"""
265 primary = np.ones((10, 10))
268 assert not a_get1.flags.owndata
269 assert a_get1.flags.writeable
272 assert not a_get2.flags.owndata
273 assert a_get2.flags.writeable
277 assert not a_view1.flags.owndata
278 assert not a_view1.flags.writeable
279 with pytest.raises(ValueError):
281 a_view2 = a.view_ptr()
282 assert not a_view2.flags.owndata
283 assert not a_view2.flags.writeable
284 with pytest.raises(ValueError):
287 a_copy1 = a.copy_get()
288 assert a_copy1.flags.owndata
289 assert a_copy1.flags.writeable
290 np.testing.assert_array_equal(a_copy1, primary)
293 a_copy2 = a.copy_view()
294 assert a_copy2.flags.owndata
295 assert a_copy2.flags.writeable
296 np.testing.assert_array_equal(a_copy2, primary)
301 assert not a_ref1.flags.owndata
302 assert a_ref1.flags.writeable
304 a_ref2 = a.ref_const()
305 assert not a_ref2.flags.owndata
306 assert not a_ref2.flags.writeable
307 with pytest.raises(ValueError):
309 a_ref3 = a.ref_safe()
310 assert not a_ref3.flags.owndata
311 assert a_ref3.flags.writeable
313 a_ref4 = a.ref_const_safe()
314 assert not a_ref4.flags.owndata
315 assert not a_ref4.flags.writeable
316 with pytest.raises(ValueError):
317 a_ref4[7, 0] = 987654321
319 a_copy3 = a.copy_ref()
320 assert a_copy3.flags.owndata
321 assert a_copy3.flags.writeable
322 np.testing.assert_array_equal(a_copy3, primary)
325 a_copy4 = a.copy_ref_const()
326 assert a_copy4.flags.owndata
327 assert a_copy4.flags.writeable
328 np.testing.assert_array_equal(a_copy4, primary)
332 a_block1 = a.block(3, 3, 2, 2)
333 assert not a_block1.flags.owndata
334 assert a_block1.flags.writeable
337 a_block2 = a.block_safe(2, 2, 3, 2)
338 assert not a_block2.flags.owndata
339 assert a_block2.flags.writeable
340 a_block2[2, 1] = -123
342 a_block3 = a.block_const(6, 7, 4, 3)
343 assert not a_block3.flags.owndata
344 assert not a_block3.flags.writeable
345 with pytest.raises(ValueError):
346 a_block3[2, 2] = -44444
348 a_copy5 = a.copy_block(2, 2, 2, 3)
349 assert a_copy5.flags.owndata
350 assert a_copy5.flags.writeable
351 np.testing.assert_array_equal(a_copy5, primary[2:4, 2:5])
355 a_corn1 = a.corners()
356 assert not a_corn1.flags.owndata
357 assert a_corn1.flags.writeable
364 a_corn2 = a.corners_const()
365 assert not a_corn2.flags.owndata
366 assert not a_corn2.flags.writeable
367 with pytest.raises(ValueError):
372 np.testing.assert_array_equal(a_get1, primary)
373 np.testing.assert_array_equal(a_get2, primary)
374 np.testing.assert_array_equal(a_view1, primary)
375 np.testing.assert_array_equal(a_view2, primary)
376 np.testing.assert_array_equal(a_ref1, primary)
377 np.testing.assert_array_equal(a_ref2, primary)
378 np.testing.assert_array_equal(a_ref3, primary)
379 np.testing.assert_array_equal(a_ref4, primary)
380 np.testing.assert_array_equal(a_block1, primary[3:5, 3:5])
381 np.testing.assert_array_equal(a_block2, primary[2:5, 2:4])
382 np.testing.assert_array_equal(a_block3, primary[6:10, 7:10])
383 np.testing.assert_array_equal(
384 a_corn1, primary[0 :: primary.shape[0] - 1, 0 :: primary.shape[1] - 1]
386 np.testing.assert_array_equal(
387 a_corn2, primary[0 :: primary.shape[0] - 1, 0 :: primary.shape[1] - 1]
390 np.testing.assert_array_equal(a_copy1, c1want)
391 np.testing.assert_array_equal(a_copy2, c2want)
392 np.testing.assert_array_equal(a_copy3, c3want)
393 np.testing.assert_array_equal(a_copy4, c4want)
394 np.testing.assert_array_equal(a_copy5, c5want)
399 start_with = cstats.alive()
401 assert cstats.alive() == start_with + 1
403 assert cstats.alive() == start_with + 1
406 assert cstats.alive() == start_with + 1
409 assert cstats.alive() == start_with
415 assert cstats.alive() == 1
416 unsafe = [a.ref(), a.ref_const(), a.block(1, 2, 3, 4)]
422 a.copy_block(4, 3, 2, 1),
425 assert cstats.alive() == 0
431 m.ReturnTester.get_ptr,
433 m.ReturnTester.view_ptr,
434 m.ReturnTester.ref_safe,
435 m.ReturnTester.ref_const_safe,
436 m.ReturnTester.corners,
437 m.ReturnTester.corners_const,
441 for meth
in [m.ReturnTester.block_safe, m.ReturnTester.block_const]:
446 """Tests Eigen's ability to mutate numpy values"""
448 orig = np.array([[1.0, 2, 3], [4, 5, 6], [7, 8, 9]])
450 zc = np.array(orig, order=
"F")
451 m.add_rm(zr, 1, 0, 100)
452 assert np.all(zr == np.array([[1.0, 2, 3], [104, 5, 6], [7, 8, 9]]))
453 m.add_cm(zc, 1, 0, 200)
454 assert np.all(zc == np.array([[1.0, 2, 3], [204, 5, 6], [7, 8, 9]]))
456 m.add_any(zr, 1, 0, 20)
457 assert np.all(zr == np.array([[1.0, 2, 3], [124, 5, 6], [7, 8, 9]]))
458 m.add_any(zc, 1, 0, 10)
459 assert np.all(zc == np.array([[1.0, 2, 3], [214, 5, 6], [7, 8, 9]]))
462 with pytest.raises(TypeError):
463 m.add_rm(zc, 1, 0, 1)
464 with pytest.raises(TypeError):
465 m.add_cm(zr, 1, 0, 1)
468 m.add1(zr, 1, 0, -100)
469 m.add2(zr, 1, 0, -20)
470 assert np.all(zr == orig)
471 m.add1(zc, 1, 0, -200)
472 m.add2(zc, 1, 0, -10)
473 assert np.all(zc == orig)
477 cornersr = zr[0::2, 0::2]
478 cornersc = zc[0::2, 0::2]
480 assert np.all(cornersr == np.array([[1.0, 3], [7, 9]]))
481 assert np.all(cornersc == np.array([[1.0, 3], [7, 9]]))
483 with pytest.raises(TypeError):
484 m.add_rm(cornersr, 0, 1, 25)
485 with pytest.raises(TypeError):
486 m.add_cm(cornersr, 0, 1, 25)
487 with pytest.raises(TypeError):
488 m.add_rm(cornersc, 0, 1, 25)
489 with pytest.raises(TypeError):
490 m.add_cm(cornersc, 0, 1, 25)
491 m.add_any(cornersr, 0, 1, 25)
492 m.add_any(cornersc, 0, 1, 44)
493 assert np.all(zr == np.array([[1.0, 2, 28], [4, 5, 6], [7, 8, 9]]))
494 assert np.all(zc == np.array([[1.0, 2, 47], [4, 5, 6], [7, 8, 9]]))
498 zro.flags.writeable =
False
499 with pytest.raises(TypeError):
500 m.add_rm(zro, 0, 0, 0)
501 with pytest.raises(TypeError):
502 m.add_any(zro, 0, 0, 0)
503 with pytest.raises(TypeError):
505 with pytest.raises(TypeError):
509 zi = np.array([[1, 2], [3, 4]])
510 with pytest.raises(TypeError):
515 """Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)"""
520 zcro = m.get_cm_const_ref()
522 zrro = m.get_rm_const_ref()
524 assert [zc[1, 2], zcro[1, 2], zr[1, 2], zrro[1, 2]] == [23] * 4
526 assert not zc.flags.owndata
527 assert zc.flags.writeable
528 assert not zr.flags.owndata
529 assert zr.flags.writeable
530 assert not zcro.flags.owndata
531 assert not zcro.flags.writeable
532 assert not zrro.flags.owndata
533 assert not zrro.flags.writeable
536 expect = np.array([[11.0, 12, 13], [21, 22, 99], [31, 32, 33]])
538 assert np.all(zc == expect)
539 assert np.all(zcro == expect)
540 assert np.all(m.get_cm_ref() == expect)
543 assert np.all(zr == expect)
544 assert np.all(zrro == expect)
545 assert np.all(m.get_rm_ref() == expect)
548 with pytest.raises(ValueError):
550 with pytest.raises(ValueError):
555 y1 = np.array(m.get_cm_const_ref())
557 assert y1.flags.owndata
558 assert y1.flags.writeable
560 assert y1[1, 2] == 99
562 assert y1[1, 2] == 111
563 assert zc[1, 2] == 99
567 """Tests a complex chain of nested eigen/numpy references"""
573 z2 = m.incr_matrix(z, 1)
575 z3 = m.incr_matrix(z, 2)
577 z4 = m.incr_matrix(z, 3)
579 z5 = m.incr_matrix(z, 4)
581 assert np.all(z == z2)
582 assert np.all(z == z3)
583 assert np.all(z == z4)
584 assert np.all(z == z5)
585 expect = np.array([[0.0, 22, 20], [31, 37, 33], [41, 42, 38]])
586 assert np.all(z == expect)
588 y = np.array(
range(100), dtype=
"float64").
reshape(10, 10)
589 y2 = m.incr_matrix_any(y, 10)
590 y3 = m.incr_matrix_any(
595 y6 = m.incr_matrix_any(y5, 1000)
598 yexpect = np.array(
range(100), dtype=
"float64").
reshape(10, 10)
600 yexpect[0::2, 0::2] -= 33
601 yexpect[0::4, 0::4] += 1000
602 assert np.all(y6 == yexpect[0::4, 0::4])
603 assert np.all(y5 == yexpect[0::4, 0::4])
604 assert np.all(y4 == yexpect[0::4, 0::2])
605 assert np.all(y3 == yexpect[0::2, 0::2])
606 assert np.all(y2 == yexpect)
607 assert np.all(y == yexpect)
613 int_matrix_colmajor = np.array(
614 [[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=
"l", order=
"F"
616 dbl_matrix_colmajor = np.array(
617 int_matrix_colmajor, dtype=
"double", order=
"F", copy=
True
619 int_matrix_rowmajor = np.array(int_matrix_colmajor, order=
"C", copy=
True)
620 dbl_matrix_rowmajor = np.array(
621 int_matrix_rowmajor, dtype=
"double", order=
"C", copy=
True
625 assert m.get_elem(int_matrix_colmajor) == 8
626 assert m.get_elem(dbl_matrix_colmajor) == 8
627 assert m.get_elem(int_matrix_rowmajor) == 8
628 assert m.get_elem(dbl_matrix_rowmajor) == 8
631 with pytest.raises(TypeError)
as excinfo:
632 m.get_elem_nocopy(int_matrix_colmajor)
633 assert "get_elem_nocopy(): incompatible function arguments." in str(excinfo.value)
634 assert ", flags.f_contiguous" in str(excinfo.value)
635 assert m.get_elem_nocopy(dbl_matrix_colmajor) == 8
636 with pytest.raises(TypeError)
as excinfo:
637 m.get_elem_nocopy(int_matrix_rowmajor)
638 assert "get_elem_nocopy(): incompatible function arguments." in str(excinfo.value)
639 assert ", flags.f_contiguous" in str(excinfo.value)
640 with pytest.raises(TypeError)
as excinfo:
641 m.get_elem_nocopy(dbl_matrix_rowmajor)
642 assert "get_elem_nocopy(): incompatible function arguments." in str(excinfo.value)
643 assert ", flags.f_contiguous" in str(excinfo.value)
646 with pytest.raises(TypeError)
as excinfo:
647 m.get_elem_rm_nocopy(int_matrix_colmajor)
648 assert "get_elem_rm_nocopy(): incompatible function arguments." in str(
651 assert ", flags.c_contiguous" in str(excinfo.value)
652 with pytest.raises(TypeError)
as excinfo:
653 m.get_elem_rm_nocopy(dbl_matrix_colmajor)
654 assert "get_elem_rm_nocopy(): incompatible function arguments." in str(
657 assert ", flags.c_contiguous" in str(excinfo.value)
658 assert m.get_elem_rm_nocopy(int_matrix_rowmajor) == 8
659 with pytest.raises(TypeError)
as excinfo:
660 m.get_elem_rm_nocopy(dbl_matrix_rowmajor)
661 assert "get_elem_rm_nocopy(): incompatible function arguments." in str(
664 assert ", flags.c_contiguous" in str(excinfo.value)
668 """Ensure the lifetime of temporary arrays created by the `Ref` caster
670 The `Ref` caster sometimes creates a copy which needs to stay alive. This needs to
671 happen both for directs casts (just the array) or indirectly (e.g. list of arrays).
674 a = np.full(shape=10, fill_value=8, dtype=np.int8)
675 assert m.get_elem_direct(a) == 8
678 assert m.get_elem_indirect(list_of_a) == 8
682 assert np.all(m.incr_diag(7) == np.diag([1.0, 2, 3, 4, 5, 6, 7]))
684 asymm = np.array([[1.0, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
685 symm_lower = np.array(asymm)
686 symm_upper = np.array(asymm)
688 for j
in range(i + 1, 4):
689 symm_lower[i, j] = symm_lower[j, i]
690 symm_upper[j, i] = symm_upper[i, j]
692 assert np.all(m.symmetric_lower(asymm) == symm_lower)
693 assert np.all(m.symmetric_upper(asymm) == symm_upper)
700 double_col(arg0: numpy.ndarray[numpy.float32[m, 1]]) -> numpy.ndarray[numpy.float32[m, 1]]
706 double_row(arg0: numpy.ndarray[numpy.float32[1, n]]) -> numpy.ndarray[numpy.float32[1, n]]
709 assert doc(m.double_complex) == (
711 double_complex(arg0: numpy.ndarray[numpy.complex64[m, 1]])"""
712 """ -> numpy.ndarray[numpy.complex64[m, 1]]
715 assert doc(m.double_mat_rm) == (
717 double_mat_rm(arg0: numpy.ndarray[numpy.float32[m, n]])"""
718 """ -> numpy.ndarray[numpy.float32[m, n]]
724 assert "\n" not in str(
doc(m.defaults_mat))
725 assert "\n" not in str(
doc(m.defaults_vec))
729 a = np.array([[1.0, 2], [3, 4], [5, 6]])
732 assert np.all(m.matrix_multiply(a, b) == np.array([[3.0], [7], [11]]))
733 assert np.all(m.matrix_multiply(A=a, B=b) == np.array([[3.0], [7], [11]]))
734 assert np.all(m.matrix_multiply(B=b, A=a) == np.array([[3.0], [7], [11]]))
736 with pytest.raises(ValueError)
as excinfo:
737 m.matrix_multiply(b, a)
738 assert str(excinfo.value) ==
"Nonconformable matrices!"
740 with pytest.raises(ValueError)
as excinfo:
741 m.matrix_multiply(A=b, B=a)
742 assert str(excinfo.value) ==
"Nonconformable matrices!"
744 with pytest.raises(ValueError)
as excinfo:
745 m.matrix_multiply(B=a, A=b)
746 assert str(excinfo.value) ==
"Nonconformable matrices!"
750 pytest.importorskip(
"scipy")
760 pytest.importorskip(
"scipy")
764 sparse_copy_r(arg0: scipy.sparse.csr_matrix[numpy.float32]) -> scipy.sparse.csr_matrix[numpy.float32]
770 sparse_copy_c(arg0: scipy.sparse.csc_matrix[numpy.float32]) -> scipy.sparse.csc_matrix[numpy.float32]
776 """Ignore strides on a length-1 dimension (even if they would be incompatible length > 1)"""
777 assert np.all(m.iss738_f1(np.array([[1.0, 2, 3]])) == np.array([[1.0, 102, 203]]))
779 m.iss738_f1(np.array([[1.0], [2], [3]])) == np.array([[1.0], [12], [23]])
782 assert np.all(m.iss738_f2(np.array([[1.0, 2, 3]])) == np.array([[1.0, 102, 203]]))
784 m.iss738_f2(np.array([[1.0], [2], [3]])) == np.array([[1.0], [12], [23]])
788 @pytest.mark.parametrize(
"func", [m.iss738_f1, m.iss738_f2])
789 @pytest.mark.parametrize(
"sizes", [(0, 2), (2, 0)])
791 """Ignore strides on a length-0 dimension (even if they would be incompatible length > 1)"""
792 assert np.all(
func(np.zeros(sizes)) == np.zeros(sizes))
796 """Issue 1105: 1xN or Nx1 input arrays weren't accepted for eigen
797 compile-time row vectors or column vector"""
798 assert m.iss1105_row(np.ones((1, 7)))
799 assert m.iss1105_col(np.ones((7, 1)))
802 with pytest.raises(TypeError)
as excinfo:
803 m.iss1105_row(np.ones((7, 1)))
804 assert "incompatible function arguments" in str(excinfo.value)
805 with pytest.raises(TypeError)
as excinfo:
806 m.iss1105_col(np.ones((1, 7)))
807 assert "incompatible function arguments" in str(excinfo.value)
811 """Using Eigen types as member variables requires a class-specific
812 operator new with proper alignment"""
814 o = m.CustomOperatorNew()
815 np.testing.assert_allclose(o.a, 0.0)
816 np.testing.assert_allclose(o.b.diagonal(), 1.0)