You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
7.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. import pytest
  2. pytestmark = pytest.requires_numpy
  3. with pytest.suppress(ImportError):
  4. import numpy as np
  5. def test_vectorize(capture):
  6. from pybind11_tests import vectorized_func, vectorized_func2, vectorized_func3
  7. assert np.isclose(vectorized_func3(np.array(3 + 7j)), [6 + 14j])
  8. for f in [vectorized_func, vectorized_func2]:
  9. with capture:
  10. assert np.isclose(f(1, 2, 3), 6)
  11. assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
  12. with capture:
  13. assert np.isclose(f(np.array(1), np.array(2), 3), 6)
  14. assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
  15. with capture:
  16. assert np.allclose(f(np.array([1, 3]), np.array([2, 4]), 3), [6, 36])
  17. assert capture == """
  18. my_func(x:int=1, y:float=2, z:float=3)
  19. my_func(x:int=3, y:float=4, z:float=3)
  20. """
  21. with capture:
  22. a = np.array([[1, 2], [3, 4]], order='F')
  23. b = np.array([[10, 20], [30, 40]], order='F')
  24. c = 3
  25. result = f(a, b, c)
  26. assert np.allclose(result, a * b * c)
  27. assert result.flags.f_contiguous
  28. # All inputs are F order and full or singletons, so we the result is in col-major order:
  29. assert capture == """
  30. my_func(x:int=1, y:float=10, z:float=3)
  31. my_func(x:int=3, y:float=30, z:float=3)
  32. my_func(x:int=2, y:float=20, z:float=3)
  33. my_func(x:int=4, y:float=40, z:float=3)
  34. """
  35. with capture:
  36. a, b, c = np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3
  37. assert np.allclose(f(a, b, c), a * b * c)
  38. assert capture == """
  39. my_func(x:int=1, y:float=2, z:float=3)
  40. my_func(x:int=3, y:float=4, z:float=3)
  41. my_func(x:int=5, y:float=6, z:float=3)
  42. my_func(x:int=7, y:float=8, z:float=3)
  43. my_func(x:int=9, y:float=10, z:float=3)
  44. my_func(x:int=11, y:float=12, z:float=3)
  45. """
  46. with capture:
  47. a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2
  48. assert np.allclose(f(a, b, c), a * b * c)
  49. assert capture == """
  50. my_func(x:int=1, y:float=2, z:float=2)
  51. my_func(x:int=2, y:float=3, z:float=2)
  52. my_func(x:int=3, y:float=4, z:float=2)
  53. my_func(x:int=4, y:float=2, z:float=2)
  54. my_func(x:int=5, y:float=3, z:float=2)
  55. my_func(x:int=6, y:float=4, z:float=2)
  56. """
  57. with capture:
  58. a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2
  59. assert np.allclose(f(a, b, c), a * b * c)
  60. assert capture == """
  61. my_func(x:int=1, y:float=2, z:float=2)
  62. my_func(x:int=2, y:float=2, z:float=2)
  63. my_func(x:int=3, y:float=2, z:float=2)
  64. my_func(x:int=4, y:float=3, z:float=2)
  65. my_func(x:int=5, y:float=3, z:float=2)
  66. my_func(x:int=6, y:float=3, z:float=2)
  67. """
  68. with capture:
  69. a, b, c = np.array([[1, 2, 3], [4, 5, 6]], order='F'), np.array([[2], [3]]), 2
  70. assert np.allclose(f(a, b, c), a * b * c)
  71. assert capture == """
  72. my_func(x:int=1, y:float=2, z:float=2)
  73. my_func(x:int=2, y:float=2, z:float=2)
  74. my_func(x:int=3, y:float=2, z:float=2)
  75. my_func(x:int=4, y:float=3, z:float=2)
  76. my_func(x:int=5, y:float=3, z:float=2)
  77. my_func(x:int=6, y:float=3, z:float=2)
  78. """
  79. with capture:
  80. a, b, c = np.array([[1, 2, 3], [4, 5, 6]])[::, ::2], np.array([[2], [3]]), 2
  81. assert np.allclose(f(a, b, c), a * b * c)
  82. assert capture == """
  83. my_func(x:int=1, y:float=2, z:float=2)
  84. my_func(x:int=3, y:float=2, z:float=2)
  85. my_func(x:int=4, y:float=3, z:float=2)
  86. my_func(x:int=6, y:float=3, z:float=2)
  87. """
  88. with capture:
  89. a, b, c = np.array([[1, 2, 3], [4, 5, 6]], order='F')[::, ::2], np.array([[2], [3]]), 2
  90. assert np.allclose(f(a, b, c), a * b * c)
  91. assert capture == """
  92. my_func(x:int=1, y:float=2, z:float=2)
  93. my_func(x:int=3, y:float=2, z:float=2)
  94. my_func(x:int=4, y:float=3, z:float=2)
  95. my_func(x:int=6, y:float=3, z:float=2)
  96. """
  97. def test_type_selection():
  98. from pybind11_tests import selective_func
  99. assert selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
  100. assert selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
  101. assert selective_func(np.array([1.0j], dtype=np.complex64)) == "Complex float branch taken."
  102. def test_docs(doc):
  103. from pybind11_tests import vectorized_func
  104. assert doc(vectorized_func) == """
  105. vectorized_func(arg0: numpy.ndarray[int32], arg1: numpy.ndarray[float32], arg2: numpy.ndarray[float64]) -> object
  106. """ # noqa: E501 line too long
  107. def test_trivial_broadcasting():
  108. from pybind11_tests import vectorized_is_trivial, trivial, vectorized_func
  109. assert vectorized_is_trivial(1, 2, 3) == trivial.c_trivial
  110. assert vectorized_is_trivial(np.array(1), np.array(2), 3) == trivial.c_trivial
  111. assert vectorized_is_trivial(np.array([1, 3]), np.array([2, 4]), 3) == trivial.c_trivial
  112. assert trivial.c_trivial == vectorized_is_trivial(
  113. np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3)
  114. assert vectorized_is_trivial(
  115. np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2) == trivial.non_trivial
  116. assert vectorized_is_trivial(
  117. np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2) == trivial.non_trivial
  118. z1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype='int32')
  119. z2 = np.array(z1, dtype='float32')
  120. z3 = np.array(z1, dtype='float64')
  121. assert vectorized_is_trivial(z1, z2, z3) == trivial.c_trivial
  122. assert vectorized_is_trivial(1, z2, z3) == trivial.c_trivial
  123. assert vectorized_is_trivial(z1, 1, z3) == trivial.c_trivial
  124. assert vectorized_is_trivial(z1, z2, 1) == trivial.c_trivial
  125. assert vectorized_is_trivial(z1[::2, ::2], 1, 1) == trivial.non_trivial
  126. assert vectorized_is_trivial(1, 1, z1[::2, ::2]) == trivial.c_trivial
  127. assert vectorized_is_trivial(1, 1, z3[::2, ::2]) == trivial.non_trivial
  128. assert vectorized_is_trivial(z1, 1, z3[1::4, 1::4]) == trivial.c_trivial
  129. y1 = np.array(z1, order='F')
  130. y2 = np.array(y1)
  131. y3 = np.array(y1)
  132. assert vectorized_is_trivial(y1, y2, y3) == trivial.f_trivial
  133. assert vectorized_is_trivial(y1, 1, 1) == trivial.f_trivial
  134. assert vectorized_is_trivial(1, y2, 1) == trivial.f_trivial
  135. assert vectorized_is_trivial(1, 1, y3) == trivial.f_trivial
  136. assert vectorized_is_trivial(y1, z2, 1) == trivial.non_trivial
  137. assert vectorized_is_trivial(z1[1::4, 1::4], y2, 1) == trivial.f_trivial
  138. assert vectorized_is_trivial(y1[1::4, 1::4], z2, 1) == trivial.c_trivial
  139. assert vectorized_func(z1, z2, z3).flags.c_contiguous
  140. assert vectorized_func(y1, y2, y3).flags.f_contiguous
  141. assert vectorized_func(z1, 1, 1).flags.c_contiguous
  142. assert vectorized_func(1, y2, 1).flags.f_contiguous
  143. assert vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous
  144. assert vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous