# -*- coding: utf-8 -*- import pytest from pybind11_tests import ConstructorStats np = pytest.importorskip("numpy") m = pytest.importorskip("pybind11_tests.eigen") ref = np.array( [ [0.0, 3, 0, 0, 0, 11], [22, 0, 0, 0, 17, 11], [7, 5, 0, 1, 0, 11], [0, 0, 0, 0, 0, 11], [0, 0, 14, 0, 8, 11], ] ) def assert_equal_ref(mat): np.testing.assert_array_equal(mat, ref) def assert_sparse_equal_ref(sparse_mat): assert_equal_ref(sparse_mat.toarray()) def test_fixed(): assert_equal_ref(m.fixed_c()) assert_equal_ref(m.fixed_r()) assert_equal_ref(m.fixed_copy_r(m.fixed_r())) assert_equal_ref(m.fixed_copy_c(m.fixed_c())) assert_equal_ref(m.fixed_copy_r(m.fixed_c())) assert_equal_ref(m.fixed_copy_c(m.fixed_r())) def test_dense(): assert_equal_ref(m.dense_r()) assert_equal_ref(m.dense_c()) assert_equal_ref(m.dense_copy_r(m.dense_r())) assert_equal_ref(m.dense_copy_c(m.dense_c())) assert_equal_ref(m.dense_copy_r(m.dense_c())) assert_equal_ref(m.dense_copy_c(m.dense_r())) def test_partially_fixed(): ref2 = np.array([[0.0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]) np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2), ref2) np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2), ref2) np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2[:, 1]), ref2[:, [1]]) np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2[0, :]), ref2[[0], :]) np.testing.assert_array_equal( m.partial_copy_four_rm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)] ) np.testing.assert_array_equal( m.partial_copy_four_rm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :] ) np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2), ref2) np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2), ref2) np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2[:, 1]), ref2[:, [1]]) np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2[0, :]), ref2[[0], :]) np.testing.assert_array_equal( m.partial_copy_four_cm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)] ) np.testing.assert_array_equal( m.partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :] ) # TypeError should be raise for a shape mismatch functions = [ m.partial_copy_four_rm_r, m.partial_copy_four_rm_c, m.partial_copy_four_cm_r, m.partial_copy_four_cm_c, ] matrix_with_wrong_shape = [[1, 2], [3, 4]] for f in functions: with pytest.raises(TypeError) as excinfo: f(matrix_with_wrong_shape) assert "incompatible function arguments" in str(excinfo.value) def test_mutator_descriptors(): zr = np.arange(30, dtype="float32").reshape(5, 6) # row-major zc = zr.reshape(6, 5).transpose() # column-major m.fixed_mutator_r(zr) m.fixed_mutator_c(zc) m.fixed_mutator_a(zr) m.fixed_mutator_a(zc) with pytest.raises(TypeError) as excinfo: m.fixed_mutator_r(zc) assert ( "(arg0: numpy.ndarray[numpy.float32[5, 6]," " flags.writeable, flags.c_contiguous]) -> None" in str(excinfo.value) ) with pytest.raises(TypeError) as excinfo: m.fixed_mutator_c(zr) assert ( "(arg0: numpy.ndarray[numpy.float32[5, 6]," " flags.writeable, flags.f_contiguous]) -> None" in str(excinfo.value) ) with pytest.raises(TypeError) as excinfo: m.fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype="float32")) assert "(arg0: numpy.ndarray[numpy.float32[5, 6], flags.writeable]) -> None" in str( excinfo.value ) zr.flags.writeable = False with pytest.raises(TypeError): m.fixed_mutator_r(zr) with pytest.raises(TypeError): m.fixed_mutator_a(zr) def test_cpp_casting(): assert m.cpp_copy(m.fixed_r()) == 22.0 assert m.cpp_copy(m.fixed_c()) == 22.0 z = np.array([[5.0, 6], [7, 8]]) assert m.cpp_copy(z) == 7.0 assert m.cpp_copy(m.get_cm_ref()) == 21.0 assert m.cpp_copy(m.get_rm_ref()) == 21.0 assert m.cpp_ref_c(m.get_cm_ref()) == 21.0 assert m.cpp_ref_r(m.get_rm_ref()) == 21.0 with pytest.raises(RuntimeError) as excinfo: # Can't reference m.fixed_c: it contains floats, m.cpp_ref_any wants doubles m.cpp_ref_any(m.fixed_c()) assert "Unable to cast Python instance" in str(excinfo.value) with pytest.raises(RuntimeError) as excinfo: # Can't reference m.fixed_r: it contains floats, m.cpp_ref_any wants doubles m.cpp_ref_any(m.fixed_r()) assert "Unable to cast Python instance" in str(excinfo.value) assert m.cpp_ref_any(m.ReturnTester.create()) == 1.0 assert m.cpp_ref_a
# -*- coding: utf-8 -*-
from pybind11_tests import docstring_options as m


def test_docstring_options():
    # options.disable_function_signatures()
    assert not m.test_function1.__doc__

    assert m.test_function2.__doc__ == "A custom docstring"

    # docstring specified on just the first overload definition:
    assert m.test_overloaded1.__doc__ == "Overload docstring"

    # docstring on both overloads:
    assert m.test_overloaded2.__doc__ == "overload docstring 1\noverload docstring 2"

    # docstring on only second overload:
    assert m.test_overloaded3.__doc__ == "Overload docstr"

    # options.enable_function_signatures()
    assert m.test_function3.__doc__.startswith("test_function3(a: int, b: int) -> None")

    assert m.test_function4.__doc__.startswith("test_function4(a: int, b: int) -> None")
    assert m.test_function4.__doc__.endswith("A custom docstring\n")

    # options.disable_function_signatures()
    # options.disable_user_defined_docstrings()
    assert not m.test_function5.__doc__

    # nested options.enable_user_defined_docstrings()
    assert m.test_function6.__doc__ == "A custom docstring"

    # RAII destructor
    assert m.test_function7.__doc__.startswith("test_function7(a: int, b: int) -> None")
    assert m.test_function7.__doc__.endswith("A custom docstring\n")

    # Suppression of user-defined docstrings for non-function objects
    assert not m.DocstringTestFoo.__doc__
    assert not m.DocstringTestFoo.value_prop.__doc__
st]: assert_keeps_alive(m.ReturnTester, meth, 4, 3, 2, 1) def test_eigen_ref_mutators(): """Tests Eigen's ability to mutate numpy values""" orig = np.array([[1.0, 2, 3], [4, 5, 6], [7, 8, 9]]) zr = np.array(orig) zc = np.array(orig, order="F") m.add_rm(zr, 1, 0, 100) assert np.all(zr == np.array([[1.0, 2, 3], [104, 5, 6], [7, 8, 9]])) m.add_cm(zc, 1, 0, 200) assert np.all(zc == np.array([[1.0, 2, 3], [204, 5, 6], [7, 8, 9]])) m.add_any(zr, 1, 0, 20) assert np.all(zr == np.array([[1.0, 2, 3], [124, 5, 6], [7, 8, 9]])) m.add_any(zc, 1, 0, 10) assert np.all(zc == np.array([[1.0, 2, 3], [214, 5, 6], [7, 8, 9]])) # Can't reference a col-major array with a row-major Ref, and vice versa: with pytest.raises(TypeError): m.add_rm(zc, 1, 0, 1) with pytest.raises(TypeError): m.add_cm(zr, 1, 0, 1) # Overloads: m.add1(zr, 1, 0, -100) m.add2(zr, 1, 0, -20) assert np.all(zr == orig) m.add1(zc, 1, 0, -200) m.add2(zc, 1, 0, -10) assert np.all(zc == orig) # a non-contiguous slice (this won't work on either the row- or # column-contiguous refs, but should work for the any) cornersr = zr[0::2, 0::2] cornersc = zc[0::2, 0::2] assert np.all(cornersr == np.array([[1.0, 3], [7, 9]])) assert np.all(cornersc == np.array([[1.0, 3], [7, 9]])) with pytest.raises(TypeError): m.add_rm(cornersr, 0, 1, 25) with pytest.raises(TypeError): m.add_cm(cornersr, 0, 1, 25) with pytest.raises(TypeError): m.add_rm(cornersc, 0, 1, 25) with pytest.raises(TypeError): m.add_cm(cornersc, 0, 1, 25) m.add_any(cornersr, 0, 1, 25) m.add_any(cornersc, 0, 1, 44) assert np.all(zr == np.array([[1.0, 2, 28], [4, 5, 6], [7, 8, 9]])) assert np.all(zc == np.array([[1.0, 2, 47], [4, 5, 6], [7, 8, 9]])) # You shouldn't be allowed to pass a non-writeable array to a mutating Eigen method: zro = zr[0:4, 0:4] zro.flags.writeable = False with pytest.raises(TypeError): m.add_rm(zro, 0, 0, 0) with pytest.raises(TypeError): m.add_any(zro, 0, 0, 0) with pytest.raises(TypeError): m.add1(zro, 0, 0, 0) with pytest.raises(TypeError): m.add2(zro, 0, 0, 0) # integer array shouldn't be passable to a double-matrix-accepting mutating func: zi = np.array([[1, 2], [3, 4]]) with pytest.raises(TypeError): m.add_rm(zi) def test_numpy_ref_mutators(): """Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)""" m.reset_refs() # In case another test already changed it zc = m.get_cm_ref() zcro = m.get_cm_const_ref() zr = m.get_rm_ref() zrro = m.get_rm_const_ref() assert [zc[1, 2], zcro[1, 2], zr[1, 2], zrro[1, 2]] == [23] * 4 assert not zc.flags.owndata and zc.flags.writeable assert not zr.flags.owndata and zr.flags.writeable assert not zcro.flags.owndata and not zcro.flags.writeable assert not zrro.flags.owndata and not zrro.flags.writeable zc[1, 2] = 99 expect = np.array([[11.0, 12, 13], [21, 22, 99], [31, 32, 33]]) # We should have just changed zc, of course, but also zcro and the original eigen matrix assert np.all(zc == expect) assert np.all(zcro == expect) assert np.all(m.get_cm_ref() == expect) zr[1, 2] = 99 assert np.all(zr == expect) assert np.all(zrro == expect) assert np.all(m.get_rm_ref() == expect) # Make sure the readonly ones are numpy-readonly: with pytest.raises(ValueError): zcro[1, 2] = 6 with pytest.raises(ValueError): zrro[1, 2] = 6 # We should be able to explicitly copy like this (and since we're copying, # the const should drop away) y1 = np.array(m.get_cm_const_ref()) assert y1.flags.owndata and y1.flags.writeable # We should get copies of the eigen data, which was modified above: assert y1[1, 2] == 99 y1[1, 2] += 12 assert y1[1, 2] == 111 assert zc[1, 2] == 99 # Make sure we aren't referencing the original def test_both_ref_mutators(): """Tests a complex chain of nested eigen/numpy references""" m.reset_refs() # In case another test already changed it z = m.get_cm_ref() # numpy -> eigen z[0, 2] -= 3 z2 = m.incr_matrix(z, 1) # numpy -> eigen -> numpy -> eigen z2[1, 1] += 6 z3 = m.incr_matrix(z, 2) # (numpy -> eigen)^3 z3[2, 2] += -5 z4 = m.incr_matrix(z, 3) # (numpy -> eigen)^4 z4[1, 1] -= 1 z5 = m.incr_matrix(z, 4) # (numpy -> eigen)^5 z5[0, 0] = 0 assert np.all(z == z2) assert np.all(z == z3) assert np.all(z == z4) assert np.all(z == z5) expect = np.array([[0.0, 22, 20], [31, 37, 33], [41, 42, 38]]) assert np.all(z == expect) y = np.array(range(100), dtype="float64").reshape(10, 10) y2 = m.incr_matrix_any(y, 10) # np -> eigen -> np y3 = m.incr_matrix_any( y2[0::2, 0::2], -33 ) # np -> eigen -> np slice -> np -> eigen -> np y4 = m.even_rows(y3) # numpy -> eigen slice -> (... y3) y5 = m.even_cols(y4) # numpy -> eigen slice -> (... y4) y6 = m.incr_matrix_any(y5, 1000) # numpy -> eigen -> (... y5) # Apply same mutations using just numpy: yexpect = np.array(range(100), dtype="float64").reshape(10, 10) yexpect += 10 yexpect[0::2, 0::2] -= 33 yexpect[0::4, 0::4] += 1000 assert np.all(y6 == yexpect[0::4, 0::4]) assert np.all(y5 == yexpect[0::4, 0::4]) assert np.all(y4 == yexpect[0::4, 0::2]) assert np.all(y3 == yexpect[0::2, 0::2]) assert np.all(y2 == yexpect) assert np.all(y == yexpect) def test_nocopy_wrapper(): # get_elem requires a column-contiguous matrix reference, but should be # callable with other types of matrix (via copying): int_matrix_colmajor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], order="F") dbl_matrix_colmajor = np.array( int_matrix_colmajor, dtype="double", order="F", copy=True ) int_matrix_rowmajor = np.array(int_matrix_colmajor, order="C", copy=True) dbl_matrix_rowmajor = np.array( int_matrix_rowmajor, dtype="double", order="C", copy=True ) # All should be callable via get_elem: assert m.get_elem(int_matrix_colmajor) == 8 assert m.get_elem(dbl_matrix_colmajor) == 8 assert m.get_elem(int_matrix_rowmajor) == 8 assert m.get_elem(dbl_matrix_rowmajor) == 8 # All but the second should fail with m.get_elem_nocopy: with pytest.raises(TypeError) as excinfo: m.get_elem_nocopy(int_matrix_colmajor) assert "get_elem_nocopy(): incompatible function arguments." in str( excinfo.value ) and ", flags.f_contiguous" in str(excinfo.value) assert m.get_elem_nocopy(dbl_matrix_colmajor) == 8 with pytest.raises(TypeError) as excinfo: m.get_elem_nocopy(int_matrix_rowmajor) assert "get_elem_nocopy(): incompatible function arguments." in str( excinfo.value ) and ", flags.f_contiguous" in str(excinfo.value) with pytest.raises(TypeError) as excinfo: m.get_elem_nocopy(dbl_matrix_rowmajor) assert "get_elem_nocopy(): incompatible function arguments." in str( excinfo.value ) and ", flags.f_contiguous" in str(excinfo.value) # For the row-major test, we take a long matrix in row-major, so only the third is allowed: with pytest.raises(TypeError) as excinfo: m.get_elem_rm_nocopy(int_matrix_colmajor) assert "get_elem_rm_nocopy(): incompatible function arguments." in str( excinfo.value ) and ", flags.c_contiguous" in str(excinfo.value) with pytest.raises(TypeError) as excinfo: m.get_elem_rm_nocopy(dbl_matrix_colmajor) assert "get_elem_rm_nocopy(): incompatible function arguments." in str( excinfo.value ) and ", flags.c_contiguous" in str(excinfo.value) assert m.get_elem_rm_nocopy(int_matrix_rowmajor) == 8 with pytest.raises(TypeError) as excinfo: m.get_elem_rm_nocopy(dbl_matrix_rowmajor) assert "get_elem_rm_nocopy(): incompatible function arguments." in str( excinfo.value ) and ", flags.c_contiguous" in str(excinfo.value) def test_eigen_ref_life_support(): """Ensure the lifetime of temporary arrays created by the `Ref` caster The `Ref` caster sometimes creates a copy which needs to stay alive. This needs to happen both for directs casts (just the array) or indirectly (e.g. list of arrays). """ a = np.full(shape=10, fill_value=8, dtype=np.int8) assert m.get_elem_direct(a) == 8 list_of_a = [a] assert m.get_elem_indirect(list_of_a) == 8 def test_special_matrix_objects(): assert np.all(m.incr_diag(7) == np.diag([1.0, 2, 3, 4, 5, 6, 7])) asymm = np.array([[1.0, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) symm_lower = np.array(asymm) symm_upper = np.array(asymm) for i in range(4): for j in range(i + 1, 4): symm_lower[i, j] = symm_lower[j, i] symm_upper[j, i] = symm_upper[i, j] assert np.all(m.symmetric_lower(asymm) == symm_lower) assert np.all(m.symmetric_upper(asymm) == symm_upper) def test_dense_signature(doc): assert ( doc(m.double_col) == """ double_col(arg0: numpy.ndarray[numpy.float32[m, 1]]) -> numpy.ndarray[numpy.float32[m, 1]] """ ) assert ( doc(m.double_row) == """ double_row(arg0: numpy.ndarray[numpy.float32[1, n]]) -> numpy.ndarray[numpy.float32[1, n]] """ ) assert doc(m.double_complex) == ( """ double_complex(arg0: numpy.ndarray[numpy.complex64[m, 1]])""" """ -> numpy.ndarray[numpy.complex64[m, 1]] """ ) assert doc(m.double_mat_rm) == ( """ double_mat_rm(arg0: numpy.ndarray[numpy.float32[m, n]])""" """ -> numpy.ndarray[numpy.float32[m, n]] """ ) def test_named_arguments(): a = np.array([[1.0, 2], [3, 4], [5, 6]]) b = np.ones((2, 1)) assert np.all(m.matrix_multiply(a, b) == np.array([[3.0], [7], [11]])) assert np.all(m.matrix_multiply(A=a, B=b) == np.array([[3.0], [7], [11]])) assert np.all(m.matrix_multiply(B=b, A=a) == np.array([[3.0], [7], [11]])) with pytest.raises(ValueError) as excinfo: m.matrix_multiply(b, a) assert str(excinfo.value) == "Nonconformable matrices!" with pytest.raises(ValueError) as excinfo: m.matrix_multiply(A=b, B=a) assert str(excinfo.value) == "Nonconformable matrices!" with pytest.raises(ValueError) as excinfo: m.matrix_multiply(B=a, A=b) assert str(excinfo.value) == "Nonconformable matrices!" def test_sparse(): pytest.importorskip("scipy") assert_sparse_equal_ref(m.sparse_r()) assert_sparse_equal_ref(m.sparse_c()) assert_sparse_equal_ref(m.sparse_copy_r(m.sparse_r())) assert_sparse_equal_ref(m.sparse_copy_c(m.sparse_c())) assert_sparse_equal_ref(m.sparse_copy_r(m.sparse_c())) assert_sparse_equal_ref(m.sparse_copy_c(m.sparse_r())) def test_sparse_signature(doc): pytest.importorskip("scipy") assert ( doc(m.sparse_copy_r) == """ sparse_copy_r(arg0: scipy.sparse.csr_matrix[numpy.float32]) -> scipy.sparse.csr_matrix[numpy.float32] """ # noqa: E501 line too long ) assert ( doc(m.sparse_copy_c) == """ sparse_copy_c(arg0: scipy.sparse.csc_matrix[numpy.float32]) -> scipy.sparse.csc_matrix[numpy.float32] """ # noqa: E501 line too long ) def test_issue738(): """Ignore strides on a length-1 dimension (even if they would be incompatible length > 1)""" assert np.all(m.iss738_f1(np.array([[1.0, 2, 3]])) == np.array([[1.0, 102, 203]])) assert np.all( m.iss738_f1(np.array([[1.0], [2], [3]])) == np.array([[1.0], [12], [23]]) ) assert np.all(m.iss738_f2(np.array([[1.0, 2, 3]])) == np.array([[1.0, 102, 203]])) assert np.all( m.iss738_f2(np.array([[1.0], [2], [3]])) == np.array([[1.0], [12], [23]]) ) def test_issue1105(): """Issue 1105: 1xN or Nx1 input arrays weren't accepted for eigen compile-time row vectors or column vector""" assert m.iss1105_row(np.ones((1, 7))) assert m.iss1105_col(np.ones((7, 1))) # These should still fail (incompatible dimensions): with pytest.raises(TypeError) as excinfo: m.iss1105_row(np.ones((7, 1))) assert "incompatible function arguments" in str(excinfo.value) with pytest.raises(TypeError) as excinfo: m.iss1105_col(np.ones((1, 7))) assert "incompatible function arguments" in str(excinfo.value) def test_custom_operator_new(): """Using Eigen types as member variables requires a class-specific operator new with proper alignment""" o = m.CustomOperatorNew() np.testing.assert_allclose(o.a, 0.0) np.testing.assert_allclose(o.b.diagonal(), 1.0)