xcoll 0.5.12__py3-none-any.whl → 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of xcoll might be problematic. Click here for more details.

Files changed (333) hide show
  1. xcoll/__init__.py +5 -18
  2. xcoll/beam_elements/__init__.py +1 -0
  3. xcoll/beam_elements/absorber.py +12 -2
  4. xcoll/beam_elements/base.py +162 -62
  5. xcoll/beam_elements/blowup.py +1 -0
  6. xcoll/beam_elements/elements_src/black_absorber.h +57 -58
  7. xcoll/beam_elements/elements_src/black_crystal.h +49 -50
  8. xcoll/beam_elements/elements_src/everest_block.h +21 -11
  9. xcoll/beam_elements/elements_src/everest_collimator.h +100 -91
  10. xcoll/beam_elements/elements_src/everest_crystal.h +145 -140
  11. xcoll/beam_elements/elements_src/transparent_collimator.h +126 -0
  12. xcoll/beam_elements/elements_src/transparent_crystal.h +118 -0
  13. xcoll/beam_elements/everest.py +16 -5
  14. xcoll/beam_elements/monitor.py +1 -0
  15. xcoll/beam_elements/transparent.py +83 -0
  16. xcoll/colldb.py +15 -6
  17. xcoll/general.py +1 -1
  18. xcoll/headers/particle_states.py +51 -0
  19. xcoll/initial_distribution.py +121 -91
  20. xcoll/interaction_record/interaction_record.py +2 -1
  21. xcoll/interaction_record/interaction_types.py +2 -2
  22. xcoll/line_tools.py +163 -73
  23. xcoll/lossmap.py +519 -127
  24. xcoll/plot.py +109 -0
  25. xcoll/rf_sweep.py +6 -0
  26. xcoll/scattering_routines/engine.py +414 -217
  27. xcoll/scattering_routines/environment.py +297 -0
  28. xcoll/scattering_routines/everest/amorphous.h +95 -71
  29. xcoll/scattering_routines/everest/{channeling.h → channelling.h} +121 -112
  30. xcoll/scattering_routines/everest/constants.h +1 -1
  31. xcoll/scattering_routines/everest/crystal_parameters.h +9 -9
  32. xcoll/scattering_routines/everest/everest.h +8 -3
  33. xcoll/scattering_routines/everest/everest.py +2 -1
  34. xcoll/scattering_routines/everest/ionisation_loss.h +141 -0
  35. xcoll/scattering_routines/everest/jaw.h +19 -24
  36. xcoll/scattering_routines/everest/materials.py +2 -0
  37. xcoll/scattering_routines/everest/multiple_coulomb_scattering.h +2 -2
  38. xcoll/scattering_routines/everest/nuclear_interaction.h +35 -19
  39. xcoll/scattering_routines/everest/properties.h +3 -72
  40. xcoll/xaux.py +65 -109
  41. {xcoll-0.5.12.dist-info → xcoll-0.6.0.dist-info}/METADATA +5 -5
  42. xcoll-0.6.0.dist-info/RECORD +135 -0
  43. xcoll/_manager.py +0 -22
  44. xcoll/headers/particle_states.h +0 -25
  45. xcoll/install.py +0 -35
  46. xcoll/scattering_routines/geant4/collimasim/.git +0 -1
  47. xcoll/scattering_routines/geant4/collimasim/.gitignore +0 -12
  48. xcoll/scattering_routines/geant4/collimasim/.gitmodules +0 -3
  49. xcoll/scattering_routines/geant4/collimasim/CMakeLists.txt +0 -26
  50. xcoll/scattering_routines/geant4/collimasim/README.md +0 -21
  51. xcoll/scattering_routines/geant4/collimasim/docs/Makefile +0 -20
  52. xcoll/scattering_routines/geant4/collimasim/docs/make.bat +0 -35
  53. xcoll/scattering_routines/geant4/collimasim/docs/source/collimasim.rst +0 -10
  54. xcoll/scattering_routines/geant4/collimasim/docs/source/conf.py +0 -59
  55. xcoll/scattering_routines/geant4/collimasim/docs/source/index.rst +0 -26
  56. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.appveyor.yml +0 -37
  57. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.clang-format +0 -19
  58. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.clang-tidy +0 -65
  59. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.cmake-format.yaml +0 -73
  60. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.git +0 -1
  61. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/CODEOWNERS +0 -9
  62. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/CONTRIBUTING.md +0 -386
  63. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/ISSUE_TEMPLATE/bug-report.yml +0 -45
  64. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/ISSUE_TEMPLATE/config.yml +0 -8
  65. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/dependabot.yml +0 -16
  66. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/labeler.yml +0 -8
  67. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/labeler_merged.yml +0 -3
  68. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/pull_request_template.md +0 -19
  69. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/ci.yml +0 -969
  70. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/configure.yml +0 -84
  71. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/format.yml +0 -48
  72. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/labeler.yml +0 -16
  73. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/pip.yml +0 -103
  74. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.gitignore +0 -45
  75. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.pre-commit-config.yaml +0 -151
  76. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.readthedocs.yml +0 -3
  77. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/CMakeLists.txt +0 -297
  78. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/LICENSE +0 -29
  79. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/MANIFEST.in +0 -6
  80. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/README.rst +0 -180
  81. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/Doxyfile +0 -23
  82. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/Makefile +0 -192
  83. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/_static/theme_overrides.css +0 -11
  84. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/chrono.rst +0 -81
  85. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/custom.rst +0 -93
  86. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/eigen.rst +0 -310
  87. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/functional.rst +0 -109
  88. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/index.rst +0 -43
  89. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/overview.rst +0 -171
  90. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/stl.rst +0 -251
  91. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/strings.rst +0 -305
  92. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/classes.rst +0 -1297
  93. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/embedding.rst +0 -262
  94. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/exceptions.rst +0 -396
  95. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/functions.rst +0 -568
  96. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/misc.rst +0 -337
  97. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/index.rst +0 -13
  98. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/numpy.rst +0 -463
  99. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/object.rst +0 -286
  100. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/utilities.rst +0 -155
  101. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/smart_ptrs.rst +0 -174
  102. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/basics.rst +0 -308
  103. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/benchmark.py +0 -91
  104. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/benchmark.rst +0 -95
  105. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/changelog.rst +0 -2050
  106. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/classes.rst +0 -542
  107. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/cmake/index.rst +0 -8
  108. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/compiling.rst +0 -648
  109. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/conf.py +0 -381
  110. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/faq.rst +0 -343
  111. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/index.rst +0 -48
  112. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/installing.rst +0 -105
  113. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/limitations.rst +0 -72
  114. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11-logo.png +0 -0
  115. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python1.png +0 -0
  116. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python1.svg +0 -427
  117. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python2.png +0 -0
  118. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python2.svg +0 -427
  119. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/reference.rst +0 -130
  120. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/release.rst +0 -96
  121. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/requirements.txt +0 -8
  122. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/upgrade.rst +0 -548
  123. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/attr.h +0 -605
  124. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/buffer_info.h +0 -144
  125. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/cast.h +0 -1432
  126. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/chrono.h +0 -213
  127. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/common.h +0 -2
  128. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/complex.h +0 -65
  129. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/class.h +0 -709
  130. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/common.h +0 -1021
  131. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/descr.h +0 -104
  132. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/init.h +0 -346
  133. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/internals.h +0 -467
  134. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/type_caster_base.h +0 -978
  135. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/typeid.h +0 -55
  136. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/eigen.h +0 -606
  137. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/embed.h +0 -284
  138. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/eval.h +0 -163
  139. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/functional.h +0 -121
  140. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/gil.h +0 -193
  141. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/iostream.h +0 -275
  142. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/numpy.h +0 -1741
  143. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/operators.h +0 -163
  144. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/options.h +0 -65
  145. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/pybind11.h +0 -2497
  146. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/pytypes.h +0 -1879
  147. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl/filesystem.h +0 -103
  148. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl.h +0 -375
  149. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl_bind.h +0 -747
  150. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/noxfile.py +0 -88
  151. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/__init__.py +0 -11
  152. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/__main__.py +0 -52
  153. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/_version.py +0 -12
  154. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/_version.pyi +0 -6
  155. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/commands.py +0 -21
  156. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/py.typed +0 -0
  157. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/setup_helpers.py +0 -482
  158. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/setup_helpers.pyi +0 -63
  159. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pyproject.toml +0 -41
  160. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/setup.cfg +0 -56
  161. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/setup.py +0 -155
  162. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/CMakeLists.txt +0 -503
  163. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/conftest.py +0 -208
  164. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/constructor_stats.h +0 -275
  165. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/cross_module_gil_utils.cpp +0 -73
  166. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/env.py +0 -33
  167. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_python_package/pytest.ini +0 -0
  168. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_python_package/test_files.py +0 -279
  169. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_setuptools/pytest.ini +0 -0
  170. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_setuptools/test_setuphelper.py +0 -143
  171. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/local_bindings.h +0 -85
  172. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/object.h +0 -179
  173. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_cross_module_tests.cpp +0 -151
  174. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_tests.cpp +0 -91
  175. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_tests.h +0 -85
  176. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pytest.ini +0 -19
  177. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/requirements.txt +0 -12
  178. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_async.cpp +0 -26
  179. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_async.py +0 -25
  180. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_buffers.cpp +0 -216
  181. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_buffers.py +0 -163
  182. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_builtin_casters.cpp +0 -286
  183. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_builtin_casters.py +0 -536
  184. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_call_policies.cpp +0 -107
  185. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_call_policies.py +0 -248
  186. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_callbacks.cpp +0 -227
  187. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_callbacks.py +0 -202
  188. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_chrono.cpp +0 -84
  189. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_chrono.py +0 -210
  190. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_class.cpp +0 -550
  191. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_class.py +0 -473
  192. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/CMakeLists.txt +0 -84
  193. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/embed.cpp +0 -21
  194. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt +0 -28
  195. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_function/CMakeLists.txt +0 -39
  196. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_target/CMakeLists.txt +0 -46
  197. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/main.cpp +0 -6
  198. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt +0 -41
  199. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_function/CMakeLists.txt +0 -35
  200. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_target/CMakeLists.txt +0 -41
  201. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/test.py +0 -10
  202. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_constants_and_functions.cpp +0 -165
  203. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_constants_and_functions.py +0 -53
  204. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_copy_move.cpp +0 -238
  205. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_copy_move.py +0 -126
  206. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_casters.cpp +0 -141
  207. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_casters.py +0 -117
  208. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_setup.cpp +0 -41
  209. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_setup.py +0 -50
  210. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_docstring_options.cpp +0 -69
  211. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_docstring_options.py +0 -42
  212. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eigen.cpp +0 -348
  213. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eigen.py +0 -771
  214. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/CMakeLists.txt +0 -47
  215. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/catch.cpp +0 -22
  216. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/external_module.cpp +0 -23
  217. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/test_interpreter.cpp +0 -326
  218. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/test_interpreter.py +0 -15
  219. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_enum.cpp +0 -148
  220. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_enum.py +0 -272
  221. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval.cpp +0 -119
  222. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval.py +0 -51
  223. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval_call.py +0 -5
  224. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.cpp +0 -285
  225. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.h +0 -12
  226. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.py +0 -265
  227. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_factory_constructors.cpp +0 -397
  228. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_factory_constructors.py +0 -520
  229. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_gil_scoped.cpp +0 -49
  230. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_gil_scoped.py +0 -94
  231. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_iostream.cpp +0 -125
  232. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_iostream.py +0 -331
  233. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_kwargs_and_defaults.cpp +0 -153
  234. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_kwargs_and_defaults.py +0 -284
  235. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_local_bindings.cpp +0 -107
  236. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_local_bindings.py +0 -257
  237. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_methods_and_attributes.cpp +0 -412
  238. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_methods_and_attributes.py +0 -517
  239. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_modules.cpp +0 -102
  240. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_modules.py +0 -92
  241. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_multiple_inheritance.cpp +0 -233
  242. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_multiple_inheritance.py +0 -360
  243. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_array.cpp +0 -472
  244. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_array.py +0 -593
  245. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_dtypes.cpp +0 -524
  246. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_dtypes.py +0 -441
  247. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_vectorize.cpp +0 -103
  248. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_vectorize.py +0 -267
  249. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_opaque_types.cpp +0 -73
  250. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_opaque_types.py +0 -59
  251. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_operator_overloading.cpp +0 -235
  252. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_operator_overloading.py +0 -146
  253. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pickling.cpp +0 -189
  254. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pickling.py +0 -82
  255. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pytypes.cpp +0 -560
  256. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pytypes.py +0 -651
  257. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_sequences_and_iterators.cpp +0 -500
  258. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_sequences_and_iterators.py +0 -253
  259. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_smart_ptr.cpp +0 -452
  260. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_smart_ptr.py +0 -318
  261. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl.cpp +0 -342
  262. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl.py +0 -291
  263. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl_binders.cpp +0 -131
  264. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl_binders.py +0 -318
  265. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_tagbased_polymorphic.cpp +0 -144
  266. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_tagbased_polymorphic.py +0 -29
  267. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_thread.cpp +0 -66
  268. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_thread.py +0 -44
  269. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_union.cpp +0 -22
  270. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_union.py +0 -9
  271. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_virtual_functions.cpp +0 -510
  272. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_virtual_functions.py +0 -408
  273. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/valgrind-numpy-scipy.supp +0 -140
  274. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/valgrind-python.supp +0 -117
  275. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindCatch.cmake +0 -70
  276. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindEigen3.cmake +0 -86
  277. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindPythonLibsNew.cmake +0 -257
  278. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/check-style.sh +0 -44
  279. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/cmake_uninstall.cmake.in +0 -23
  280. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/libsize.py +0 -39
  281. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/make_changelog.py +0 -64
  282. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Common.cmake +0 -402
  283. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Config.cmake.in +0 -233
  284. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11NewTools.cmake +0 -276
  285. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Tools.cmake +0 -214
  286. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pyproject.toml +0 -3
  287. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/setup_global.py.in +0 -65
  288. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/setup_main.py.in +0 -41
  289. xcoll/scattering_routines/geant4/collimasim/pyproject.toml +0 -8
  290. xcoll/scattering_routines/geant4/collimasim/setup.py +0 -144
  291. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSPyATInterface.cpp +0 -403
  292. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSPyATInterface.hh +0 -100
  293. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSXtrackInterface.cpp +0 -763
  294. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSXtrackInterface.hh +0 -118
  295. xcoll/scattering_routines/geant4/collimasim/src/collimasim/__init__.py +0 -8
  296. xcoll/scattering_routines/geant4/collimasim/src/collimasim/bindings.cpp +0 -63
  297. xcoll/scattering_routines/geant4/collimasim/src/collimasim/pyCollimatorPass.py +0 -142
  298. xcoll/scattering_routines/geant4/collimasim/src/collimasim/xtrack_collimator.py +0 -556
  299. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/PKG-INFO +0 -6
  300. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/SOURCES.txt +0 -24
  301. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/dependency_links.txt +0 -1
  302. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/not-zip-safe +0 -1
  303. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/top_level.txt +0 -1
  304. xcoll/scattering_routines/geant4/collimasim/tests/README.md +0 -25
  305. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_forions.dat +0 -25
  306. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_new_example.dat +0 -18
  307. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_old_example.dat +0 -68
  308. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_testing.dat +0 -15
  309. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_yaml_example.yaml +0 -110
  310. xcoll/scattering_routines/geant4/collimasim/tests/resources/collgaps.dat +0 -7
  311. xcoll/scattering_routines/geant4/collimasim/tests/resources/collgaps_pyat_test.dat +0 -3
  312. xcoll/scattering_routines/geant4/collimasim/tests/resources/collonly_twiss_file_example.tfs +0 -54
  313. xcoll/scattering_routines/geant4/collimasim/tests/resources/settings.gmad +0 -3
  314. xcoll/scattering_routines/geant4/collimasim/tests/resources/settings_black_absorber.gmad +0 -3
  315. xcoll/scattering_routines/geant4/collimasim/tests/resources/settings_ions.gmad +0 -5
  316. xcoll/scattering_routines/geant4/collimasim/tests/resources/twiss_file_testing.tfs +0 -51
  317. xcoll/scattering_routines/geant4/collimasim/tests/test_pyat.py +0 -65
  318. xcoll/scattering_routines/geant4/collimasim/tests/test_pyat_passmethod.py +0 -59
  319. xcoll/scattering_routines/geant4/collimasim/tests/test_pyat_tracking.py +0 -102
  320. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack.py +0 -75
  321. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_angle.py +0 -74
  322. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_colldb_load.py +0 -84
  323. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_interaction.py +0 -159
  324. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_interaction_ion.py +0 -99
  325. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_ions.py +0 -78
  326. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_lost_energy.py +0 -88
  327. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tilt.py +0 -80
  328. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tracking.py +0 -97
  329. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tracking_ions.py +0 -96
  330. xcoll-0.5.12.dist-info/RECORD +0 -415
  331. {xcoll-0.5.12.dist-info → xcoll-0.6.0.dist-info}/LICENSE +0 -0
  332. {xcoll-0.5.12.dist-info → xcoll-0.6.0.dist-info}/NOTICE +0 -0
  333. {xcoll-0.5.12.dist-info → xcoll-0.6.0.dist-info}/WHEEL +0 -0
@@ -1,1741 +0,0 @@
1
- /*
2
- pybind11/numpy.h: Basic NumPy support, vectorize() wrapper
3
-
4
- Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
-
6
- All rights reserved. Use of this source code is governed by a
7
- BSD-style license that can be found in the LICENSE file.
8
- */
9
-
10
- #pragma once
11
-
12
- #include "pybind11.h"
13
- #include "complex.h"
14
- #include <numeric>
15
- #include <algorithm>
16
- #include <array>
17
- #include <cstdint>
18
- #include <cstdlib>
19
- #include <cstring>
20
- #include <sstream>
21
- #include <string>
22
- #include <functional>
23
- #include <type_traits>
24
- #include <utility>
25
- #include <vector>
26
- #include <typeindex>
27
-
28
- /* This will be true on all flat address space platforms and allows us to reduce the
29
- whole npy_intp / ssize_t / Py_intptr_t business down to just ssize_t for all size
30
- and dimension types (e.g. shape, strides, indexing), instead of inflicting this
31
- upon the library user. */
32
- static_assert(sizeof(::pybind11::ssize_t) == sizeof(Py_intptr_t), "ssize_t != Py_intptr_t");
33
- static_assert(std::is_signed<Py_intptr_t>::value, "Py_intptr_t must be signed");
34
- // We now can reinterpret_cast between py::ssize_t and Py_intptr_t (MSVC + PyPy cares)
35
-
36
- PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
37
-
38
- class array; // Forward declaration
39
-
40
- PYBIND11_NAMESPACE_BEGIN(detail)
41
-
42
- template <> struct handle_type_name<array> { static constexpr auto name = _("numpy.ndarray"); };
43
-
44
- template <typename type, typename SFINAE = void> struct npy_format_descriptor;
45
-
46
- struct PyArrayDescr_Proxy {
47
- PyObject_HEAD
48
- PyObject *typeobj;
49
- char kind;
50
- char type;
51
- char byteorder;
52
- char flags;
53
- int type_num;
54
- int elsize;
55
- int alignment;
56
- char *subarray;
57
- PyObject *fields;
58
- PyObject *names;
59
- };
60
-
61
- struct PyArray_Proxy {
62
- PyObject_HEAD
63
- char *data;
64
- int nd;
65
- ssize_t *dimensions;
66
- ssize_t *strides;
67
- PyObject *base;
68
- PyObject *descr;
69
- int flags;
70
- };
71
-
72
- struct PyVoidScalarObject_Proxy {
73
- PyObject_VAR_HEAD
74
- char *obval;
75
- PyArrayDescr_Proxy *descr;
76
- int flags;
77
- PyObject *base;
78
- };
79
-
80
- struct numpy_type_info {
81
- PyObject* dtype_ptr;
82
- std::string format_str;
83
- };
84
-
85
- struct numpy_internals {
86
- std::unordered_map<std::type_index, numpy_type_info> registered_dtypes;
87
-
88
- numpy_type_info *get_type_info(const std::type_info& tinfo, bool throw_if_missing = true) {
89
- auto it = registered_dtypes.find(std::type_index(tinfo));
90
- if (it != registered_dtypes.end())
91
- return &(it->second);
92
- if (throw_if_missing)
93
- pybind11_fail(std::string("NumPy type info missing for ") + tinfo.name());
94
- return nullptr;
95
- }
96
-
97
- template<typename T> numpy_type_info *get_type_info(bool throw_if_missing = true) {
98
- return get_type_info(typeid(typename std::remove_cv<T>::type), throw_if_missing);
99
- }
100
- };
101
-
102
- PYBIND11_NOINLINE void load_numpy_internals(numpy_internals* &ptr) {
103
- ptr = &get_or_create_shared_data<numpy_internals>("_numpy_internals");
104
- }
105
-
106
- inline numpy_internals& get_numpy_internals() {
107
- static numpy_internals* ptr = nullptr;
108
- if (!ptr)
109
- load_numpy_internals(ptr);
110
- return *ptr;
111
- }
112
-
113
- template <typename T> struct same_size {
114
- template <typename U> using as = bool_constant<sizeof(T) == sizeof(U)>;
115
- };
116
-
117
- template <typename Concrete> constexpr int platform_lookup() { return -1; }
118
-
119
- // Lookup a type according to its size, and return a value corresponding to the NumPy typenum.
120
- template <typename Concrete, typename T, typename... Ts, typename... Ints>
121
- constexpr int platform_lookup(int I, Ints... Is) {
122
- return sizeof(Concrete) == sizeof(T) ? I : platform_lookup<Concrete, Ts...>(Is...);
123
- }
124
-
125
- struct npy_api {
126
- enum constants {
127
- NPY_ARRAY_C_CONTIGUOUS_ = 0x0001,
128
- NPY_ARRAY_F_CONTIGUOUS_ = 0x0002,
129
- NPY_ARRAY_OWNDATA_ = 0x0004,
130
- NPY_ARRAY_FORCECAST_ = 0x0010,
131
- NPY_ARRAY_ENSUREARRAY_ = 0x0040,
132
- NPY_ARRAY_ALIGNED_ = 0x0100,
133
- NPY_ARRAY_WRITEABLE_ = 0x0400,
134
- NPY_BOOL_ = 0,
135
- NPY_BYTE_, NPY_UBYTE_,
136
- NPY_SHORT_, NPY_USHORT_,
137
- NPY_INT_, NPY_UINT_,
138
- NPY_LONG_, NPY_ULONG_,
139
- NPY_LONGLONG_, NPY_ULONGLONG_,
140
- NPY_FLOAT_, NPY_DOUBLE_, NPY_LONGDOUBLE_,
141
- NPY_CFLOAT_, NPY_CDOUBLE_, NPY_CLONGDOUBLE_,
142
- NPY_OBJECT_ = 17,
143
- NPY_STRING_, NPY_UNICODE_, NPY_VOID_,
144
- // Platform-dependent normalization
145
- NPY_INT8_ = NPY_BYTE_,
146
- NPY_UINT8_ = NPY_UBYTE_,
147
- NPY_INT16_ = NPY_SHORT_,
148
- NPY_UINT16_ = NPY_USHORT_,
149
- // `npy_common.h` defines the integer aliases. In order, it checks:
150
- // NPY_BITSOF_LONG, NPY_BITSOF_LONGLONG, NPY_BITSOF_INT, NPY_BITSOF_SHORT, NPY_BITSOF_CHAR
151
- // and assigns the alias to the first matching size, so we should check in this order.
152
- NPY_INT32_ = platform_lookup<std::int32_t, long, int, short>(
153
- NPY_LONG_, NPY_INT_, NPY_SHORT_),
154
- NPY_UINT32_ = platform_lookup<std::uint32_t, unsigned long, unsigned int, unsigned short>(
155
- NPY_ULONG_, NPY_UINT_, NPY_USHORT_),
156
- NPY_INT64_ = platform_lookup<std::int64_t, long, long long, int>(
157
- NPY_LONG_, NPY_LONGLONG_, NPY_INT_),
158
- NPY_UINT64_ = platform_lookup<std::uint64_t, unsigned long, unsigned long long, unsigned int>(
159
- NPY_ULONG_, NPY_ULONGLONG_, NPY_UINT_),
160
- };
161
-
162
- struct PyArray_Dims {
163
- Py_intptr_t *ptr;
164
- int len;
165
- };
166
-
167
- static npy_api& get() {
168
- static npy_api api = lookup();
169
- return api;
170
- }
171
-
172
- bool PyArray_Check_(PyObject *obj) const {
173
- return (bool) PyObject_TypeCheck(obj, PyArray_Type_);
174
- }
175
- bool PyArrayDescr_Check_(PyObject *obj) const {
176
- return (bool) PyObject_TypeCheck(obj, PyArrayDescr_Type_);
177
- }
178
-
179
- unsigned int (*PyArray_GetNDArrayCFeatureVersion_)();
180
- PyObject *(*PyArray_DescrFromType_)(int);
181
- PyObject *(*PyArray_NewFromDescr_)
182
- (PyTypeObject *, PyObject *, int, Py_intptr_t const *,
183
- Py_intptr_t const *, void *, int, PyObject *);
184
- // Unused. Not removed because that affects ABI of the class.
185
- PyObject *(*PyArray_DescrNewFromType_)(int);
186
- int (*PyArray_CopyInto_)(PyObject *, PyObject *);
187
- PyObject *(*PyArray_NewCopy_)(PyObject *, int);
188
- PyTypeObject *PyArray_Type_;
189
- PyTypeObject *PyVoidArrType_Type_;
190
- PyTypeObject *PyArrayDescr_Type_;
191
- PyObject *(*PyArray_DescrFromScalar_)(PyObject *);
192
- PyObject *(*PyArray_FromAny_) (PyObject *, PyObject *, int, int, int, PyObject *);
193
- int (*PyArray_DescrConverter_) (PyObject *, PyObject **);
194
- bool (*PyArray_EquivTypes_) (PyObject *, PyObject *);
195
- int (*PyArray_GetArrayParamsFromObject_)(PyObject *, PyObject *, unsigned char, PyObject **, int *,
196
- Py_intptr_t *, PyObject **, PyObject *);
197
- PyObject *(*PyArray_Squeeze_)(PyObject *);
198
- // Unused. Not removed because that affects ABI of the class.
199
- int (*PyArray_SetBaseObject_)(PyObject *, PyObject *);
200
- PyObject* (*PyArray_Resize_)(PyObject*, PyArray_Dims*, int, int);
201
- PyObject* (*PyArray_Newshape_)(PyObject*, PyArray_Dims*, int);
202
- PyObject* (*PyArray_View_)(PyObject*, PyObject*, PyObject*);
203
-
204
- private:
205
- enum functions {
206
- API_PyArray_GetNDArrayCFeatureVersion = 211,
207
- API_PyArray_Type = 2,
208
- API_PyArrayDescr_Type = 3,
209
- API_PyVoidArrType_Type = 39,
210
- API_PyArray_DescrFromType = 45,
211
- API_PyArray_DescrFromScalar = 57,
212
- API_PyArray_FromAny = 69,
213
- API_PyArray_Resize = 80,
214
- API_PyArray_CopyInto = 82,
215
- API_PyArray_NewCopy = 85,
216
- API_PyArray_NewFromDescr = 94,
217
- API_PyArray_DescrNewFromType = 96,
218
- API_PyArray_Newshape = 135,
219
- API_PyArray_Squeeze = 136,
220
- API_PyArray_View = 137,
221
- API_PyArray_DescrConverter = 174,
222
- API_PyArray_EquivTypes = 182,
223
- API_PyArray_GetArrayParamsFromObject = 278,
224
- API_PyArray_SetBaseObject = 282
225
- };
226
-
227
- static npy_api lookup() {
228
- module_ m = module_::import("numpy.core.multiarray");
229
- auto c = m.attr("_ARRAY_API");
230
- #if PY_MAJOR_VERSION >= 3
231
- void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), NULL);
232
- #else
233
- void **api_ptr = (void **) PyCObject_AsVoidPtr(c.ptr());
234
- #endif
235
- npy_api api;
236
- #define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func];
237
- DECL_NPY_API(PyArray_GetNDArrayCFeatureVersion);
238
- if (api.PyArray_GetNDArrayCFeatureVersion_() < 0x7)
239
- pybind11_fail("pybind11 numpy support requires numpy >= 1.7.0");
240
- DECL_NPY_API(PyArray_Type);
241
- DECL_NPY_API(PyVoidArrType_Type);
242
- DECL_NPY_API(PyArrayDescr_Type);
243
- DECL_NPY_API(PyArray_DescrFromType);
244
- DECL_NPY_API(PyArray_DescrFromScalar);
245
- DECL_NPY_API(PyArray_FromAny);
246
- DECL_NPY_API(PyArray_Resize);
247
- DECL_NPY_API(PyArray_CopyInto);
248
- DECL_NPY_API(PyArray_NewCopy);
249
- DECL_NPY_API(PyArray_NewFromDescr);
250
- DECL_NPY_API(PyArray_DescrNewFromType);
251
- DECL_NPY_API(PyArray_Newshape);
252
- DECL_NPY_API(PyArray_Squeeze);
253
- DECL_NPY_API(PyArray_View);
254
- DECL_NPY_API(PyArray_DescrConverter);
255
- DECL_NPY_API(PyArray_EquivTypes);
256
- DECL_NPY_API(PyArray_GetArrayParamsFromObject);
257
- DECL_NPY_API(PyArray_SetBaseObject);
258
-
259
- #undef DECL_NPY_API
260
- return api;
261
- }
262
- };
263
-
264
- inline PyArray_Proxy* array_proxy(void* ptr) {
265
- return reinterpret_cast<PyArray_Proxy*>(ptr);
266
- }
267
-
268
- inline const PyArray_Proxy* array_proxy(const void* ptr) {
269
- return reinterpret_cast<const PyArray_Proxy*>(ptr);
270
- }
271
-
272
- inline PyArrayDescr_Proxy* array_descriptor_proxy(PyObject* ptr) {
273
- return reinterpret_cast<PyArrayDescr_Proxy*>(ptr);
274
- }
275
-
276
- inline const PyArrayDescr_Proxy* array_descriptor_proxy(const PyObject* ptr) {
277
- return reinterpret_cast<const PyArrayDescr_Proxy*>(ptr);
278
- }
279
-
280
- inline bool check_flags(const void* ptr, int flag) {
281
- return (flag == (array_proxy(ptr)->flags & flag));
282
- }
283
-
284
- template <typename T> struct is_std_array : std::false_type { };
285
- template <typename T, size_t N> struct is_std_array<std::array<T, N>> : std::true_type { };
286
- template <typename T> struct is_complex : std::false_type { };
287
- template <typename T> struct is_complex<std::complex<T>> : std::true_type { };
288
-
289
- template <typename T> struct array_info_scalar {
290
- using type = T;
291
- static constexpr bool is_array = false;
292
- static constexpr bool is_empty = false;
293
- static constexpr auto extents = _("");
294
- static void append_extents(list& /* shape */) { }
295
- };
296
- // Computes underlying type and a comma-separated list of extents for array
297
- // types (any mix of std::array and built-in arrays). An array of char is
298
- // treated as scalar because it gets special handling.
299
- template <typename T> struct array_info : array_info_scalar<T> { };
300
- template <typename T, size_t N> struct array_info<std::array<T, N>> {
301
- using type = typename array_info<T>::type;
302
- static constexpr bool is_array = true;
303
- static constexpr bool is_empty = (N == 0) || array_info<T>::is_empty;
304
- static constexpr size_t extent = N;
305
-
306
- // appends the extents to shape
307
- static void append_extents(list& shape) {
308
- shape.append(N);
309
- array_info<T>::append_extents(shape);
310
- }
311
-
312
- static constexpr auto extents = _<array_info<T>::is_array>(
313
- concat(_<N>(), array_info<T>::extents), _<N>()
314
- );
315
- };
316
- // For numpy we have special handling for arrays of characters, so we don't include
317
- // the size in the array extents.
318
- template <size_t N> struct array_info<char[N]> : array_info_scalar<char[N]> { };
319
- template <size_t N> struct array_info<std::array<char, N>> : array_info_scalar<std::array<char, N>> { };
320
- template <typename T, size_t N> struct array_info<T[N]> : array_info<std::array<T, N>> { };
321
- template <typename T> using remove_all_extents_t = typename array_info<T>::type;
322
-
323
- template <typename T> using is_pod_struct = all_of<
324
- std::is_standard_layout<T>, // since we're accessing directly in memory we need a standard layout type
325
- #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20150422 || __GLIBCXX__ == 20150426 || __GLIBCXX__ == 20150623 || __GLIBCXX__ == 20150626 || __GLIBCXX__ == 20160803)
326
- // libstdc++ < 5 (including versions 4.8.5, 4.9.3 and 4.9.4 which were released after 5)
327
- // don't implement is_trivially_copyable, so approximate it
328
- std::is_trivially_destructible<T>,
329
- satisfies_any_of<T, std::has_trivial_copy_constructor, std::has_trivial_copy_assign>,
330
- #else
331
- std::is_trivially_copyable<T>,
332
- #endif
333
- satisfies_none_of<T, std::is_reference, std::is_array, is_std_array, std::is_arithmetic, is_complex, std::is_enum>
334
- >;
335
-
336
- // Replacement for std::is_pod (deprecated in C++20)
337
- template <typename T> using is_pod = all_of<
338
- std::is_standard_layout<T>,
339
- std::is_trivial<T>
340
- >;
341
-
342
- template <ssize_t Dim = 0, typename Strides> ssize_t byte_offset_unsafe(const Strides &) { return 0; }
343
- template <ssize_t Dim = 0, typename Strides, typename... Ix>
344
- ssize_t byte_offset_unsafe(const Strides &strides, ssize_t i, Ix... index) {
345
- return i * strides[Dim] + byte_offset_unsafe<Dim + 1>(strides, index...);
346
- }
347
-
348
- /**
349
- * Proxy class providing unsafe, unchecked const access to array data. This is constructed through
350
- * the `unchecked<T, N>()` method of `array` or the `unchecked<N>()` method of `array_t<T>`. `Dims`
351
- * will be -1 for dimensions determined at runtime.
352
- */
353
- template <typename T, ssize_t Dims>
354
- class unchecked_reference {
355
- protected:
356
- static constexpr bool Dynamic = Dims < 0;
357
- const unsigned char *data_;
358
- // Storing the shape & strides in local variables (i.e. these arrays) allows the compiler to
359
- // make large performance gains on big, nested loops, but requires compile-time dimensions
360
- conditional_t<Dynamic, const ssize_t *, std::array<ssize_t, (size_t) Dims>>
361
- shape_, strides_;
362
- const ssize_t dims_;
363
-
364
- friend class pybind11::array;
365
- // Constructor for compile-time dimensions:
366
- template <bool Dyn = Dynamic>
367
- unchecked_reference(const void *data, const ssize_t *shape, const ssize_t *strides, enable_if_t<!Dyn, ssize_t>)
368
- : data_{reinterpret_cast<const unsigned char *>(data)}, dims_{Dims} {
369
- for (size_t i = 0; i < (size_t) dims_; i++) {
370
- shape_[i] = shape[i];
371
- strides_[i] = strides[i];
372
- }
373
- }
374
- // Constructor for runtime dimensions:
375
- template <bool Dyn = Dynamic>
376
- unchecked_reference(const void *data, const ssize_t *shape, const ssize_t *strides, enable_if_t<Dyn, ssize_t> dims)
377
- : data_{reinterpret_cast<const unsigned char *>(data)}, shape_{shape}, strides_{strides}, dims_{dims} {}
378
-
379
- public:
380
- /**
381
- * Unchecked const reference access to data at the given indices. For a compile-time known
382
- * number of dimensions, this requires the correct number of arguments; for run-time
383
- * dimensionality, this is not checked (and so is up to the caller to use safely).
384
- */
385
- template <typename... Ix> const T &operator()(Ix... index) const {
386
- static_assert(ssize_t{sizeof...(Ix)} == Dims || Dynamic,
387
- "Invalid number of indices for unchecked array reference");
388
- return *reinterpret_cast<const T *>(data_ + byte_offset_unsafe(strides_, ssize_t(index)...));
389
- }
390
- /**
391
- * Unchecked const reference access to data; this operator only participates if the reference
392
- * is to a 1-dimensional array. When present, this is exactly equivalent to `obj(index)`.
393
- */
394
- template <ssize_t D = Dims, typename = enable_if_t<D == 1 || Dynamic>>
395
- const T &operator[](ssize_t index) const { return operator()(index); }
396
-
397
- /// Pointer access to the data at the given indices.
398
- template <typename... Ix> const T *data(Ix... ix) const { return &operator()(ssize_t(ix)...); }
399
-
400
- /// Returns the item size, i.e. sizeof(T)
401
- constexpr static ssize_t itemsize() { return sizeof(T); }
402
-
403
- /// Returns the shape (i.e. size) of dimension `dim`
404
- ssize_t shape(ssize_t dim) const { return shape_[(size_t) dim]; }
405
-
406
- /// Returns the number of dimensions of the array
407
- ssize_t ndim() const { return dims_; }
408
-
409
- /// Returns the total number of elements in the referenced array, i.e. the product of the shapes
410
- template <bool Dyn = Dynamic>
411
- enable_if_t<!Dyn, ssize_t> size() const {
412
- return std::accumulate(shape_.begin(), shape_.end(), (ssize_t) 1, std::multiplies<ssize_t>());
413
- }
414
- template <bool Dyn = Dynamic>
415
- enable_if_t<Dyn, ssize_t> size() const {
416
- return std::accumulate(shape_, shape_ + ndim(), (ssize_t) 1, std::multiplies<ssize_t>());
417
- }
418
-
419
- /// Returns the total number of bytes used by the referenced data. Note that the actual span in
420
- /// memory may be larger if the referenced array has non-contiguous strides (e.g. for a slice).
421
- ssize_t nbytes() const {
422
- return size() * itemsize();
423
- }
424
- };
425
-
426
- template <typename T, ssize_t Dims>
427
- class unchecked_mutable_reference : public unchecked_reference<T, Dims> {
428
- friend class pybind11::array;
429
- using ConstBase = unchecked_reference<T, Dims>;
430
- using ConstBase::ConstBase;
431
- using ConstBase::Dynamic;
432
- public:
433
- // Bring in const-qualified versions from base class
434
- using ConstBase::operator();
435
- using ConstBase::operator[];
436
-
437
- /// Mutable, unchecked access to data at the given indices.
438
- template <typename... Ix> T& operator()(Ix... index) {
439
- static_assert(ssize_t{sizeof...(Ix)} == Dims || Dynamic,
440
- "Invalid number of indices for unchecked array reference");
441
- return const_cast<T &>(ConstBase::operator()(index...));
442
- }
443
- /**
444
- * Mutable, unchecked access data at the given index; this operator only participates if the
445
- * reference is to a 1-dimensional array (or has runtime dimensions). When present, this is
446
- * exactly equivalent to `obj(index)`.
447
- */
448
- template <ssize_t D = Dims, typename = enable_if_t<D == 1 || Dynamic>>
449
- T &operator[](ssize_t index) { return operator()(index); }
450
-
451
- /// Mutable pointer access to the data at the given indices.
452
- template <typename... Ix> T *mutable_data(Ix... ix) { return &operator()(ssize_t(ix)...); }
453
- };
454
-
455
- template <typename T, ssize_t Dim>
456
- struct type_caster<unchecked_reference<T, Dim>> {
457
- static_assert(Dim == 0 && Dim > 0 /* always fail */, "unchecked array proxy object is not castable");
458
- };
459
- template <typename T, ssize_t Dim>
460
- struct type_caster<unchecked_mutable_reference<T, Dim>> : type_caster<unchecked_reference<T, Dim>> {};
461
-
462
- PYBIND11_NAMESPACE_END(detail)
463
-
464
- class dtype : public object {
465
- public:
466
- PYBIND11_OBJECT_DEFAULT(dtype, object, detail::npy_api::get().PyArrayDescr_Check_);
467
-
468
- explicit dtype(const buffer_info &info) {
469
- dtype descr(_dtype_from_pep3118()(PYBIND11_STR_TYPE(info.format)));
470
- // If info.itemsize == 0, use the value calculated from the format string
471
- m_ptr = descr.strip_padding(info.itemsize != 0 ? info.itemsize : descr.itemsize())
472
- .release()
473
- .ptr();
474
- }
475
-
476
- explicit dtype(const std::string &format) {
477
- m_ptr = from_args(pybind11::str(format)).release().ptr();
478
- }
479
-
480
- explicit dtype(const char *format) : dtype(std::string(format)) {}
481
-
482
- dtype(list names, list formats, list offsets, ssize_t itemsize) {
483
- dict args;
484
- args["names"] = std::move(names);
485
- args["formats"] = std::move(formats);
486
- args["offsets"] = std::move(offsets);
487
- args["itemsize"] = pybind11::int_(itemsize);
488
- m_ptr = from_args(std::move(args)).release().ptr();
489
- }
490
-
491
- /// This is essentially the same as calling numpy.dtype(args) in Python.
492
- static dtype from_args(object args) {
493
- PyObject *ptr = nullptr;
494
- if ((detail::npy_api::get().PyArray_DescrConverter_(args.ptr(), &ptr) == 0) || !ptr)
495
- throw error_already_set();
496
- return reinterpret_steal<dtype>(ptr);
497
- }
498
-
499
- /// Return dtype associated with a C++ type.
500
- template <typename T> static dtype of() {
501
- return detail::npy_format_descriptor<typename std::remove_cv<T>::type>::dtype();
502
- }
503
-
504
- /// Size of the data type in bytes.
505
- ssize_t itemsize() const {
506
- return detail::array_descriptor_proxy(m_ptr)->elsize;
507
- }
508
-
509
- /// Returns true for structured data types.
510
- bool has_fields() const {
511
- return detail::array_descriptor_proxy(m_ptr)->names != nullptr;
512
- }
513
-
514
- /// Single-character code for dtype's kind.
515
- /// For example, floating point types are 'f' and integral types are 'i'.
516
- char kind() const {
517
- return detail::array_descriptor_proxy(m_ptr)->kind;
518
- }
519
-
520
- /// Single-character for dtype's type.
521
- /// For example, ``float`` is 'f', ``double`` 'd', ``int`` 'i', and ``long`` 'd'.
522
- char char_() const {
523
- // Note: The signature, `dtype::char_` follows the naming of NumPy's
524
- // public Python API (i.e., ``dtype.char``), rather than its internal
525
- // C API (``PyArray_Descr::type``).
526
- return detail::array_descriptor_proxy(m_ptr)->type;
527
- }
528
-
529
- private:
530
- static object _dtype_from_pep3118() {
531
- static PyObject *obj = module_::import("numpy.core._internal")
532
- .attr("_dtype_from_pep3118").cast<object>().release().ptr();
533
- return reinterpret_borrow<object>(obj);
534
- }
535
-
536
- dtype strip_padding(ssize_t itemsize) {
537
- // Recursively strip all void fields with empty names that are generated for
538
- // padding fields (as of NumPy v1.11).
539
- if (!has_fields())
540
- return *this;
541
-
542
- struct field_descr { PYBIND11_STR_TYPE name; object format; pybind11::int_ offset; };
543
- std::vector<field_descr> field_descriptors;
544
-
545
- for (auto field : attr("fields").attr("items")()) {
546
- auto spec = field.cast<tuple>();
547
- auto name = spec[0].cast<pybind11::str>();
548
- auto format = spec[1].cast<tuple>()[0].cast<dtype>();
549
- auto offset = spec[1].cast<tuple>()[1].cast<pybind11::int_>();
550
- if ((len(name) == 0u) && format.kind() == 'V')
551
- continue;
552
- field_descriptors.push_back({(PYBIND11_STR_TYPE) name, format.strip_padding(format.itemsize()), offset});
553
- }
554
-
555
- std::sort(field_descriptors.begin(), field_descriptors.end(),
556
- [](const field_descr& a, const field_descr& b) {
557
- return a.offset.cast<int>() < b.offset.cast<int>();
558
- });
559
-
560
- list names, formats, offsets;
561
- for (auto& descr : field_descriptors) {
562
- names.append(descr.name);
563
- formats.append(descr.format);
564
- offsets.append(descr.offset);
565
- }
566
- return dtype(std::move(names), std::move(formats), std::move(offsets), itemsize);
567
- }
568
- };
569
-
570
- class array : public buffer {
571
- public:
572
- PYBIND11_OBJECT_CVT(array, buffer, detail::npy_api::get().PyArray_Check_, raw_array)
573
-
574
- enum {
575
- c_style = detail::npy_api::NPY_ARRAY_C_CONTIGUOUS_,
576
- f_style = detail::npy_api::NPY_ARRAY_F_CONTIGUOUS_,
577
- forcecast = detail::npy_api::NPY_ARRAY_FORCECAST_
578
- };
579
-
580
- array() : array(0, static_cast<const double *>(nullptr)) {}
581
-
582
- using ShapeContainer = detail::any_container<ssize_t>;
583
- using StridesContainer = detail::any_container<ssize_t>;
584
-
585
- // Constructs an array taking shape/strides from arbitrary container types
586
- array(const pybind11::dtype &dt, ShapeContainer shape, StridesContainer strides,
587
- const void *ptr = nullptr, handle base = handle()) {
588
-
589
- if (strides->empty())
590
- *strides = detail::c_strides(*shape, dt.itemsize());
591
-
592
- auto ndim = shape->size();
593
- if (ndim != strides->size())
594
- pybind11_fail("NumPy: shape ndim doesn't match strides ndim");
595
- auto descr = dt;
596
-
597
- int flags = 0;
598
- if (base && ptr) {
599
- if (isinstance<array>(base))
600
- /* Copy flags from base (except ownership bit) */
601
- flags = reinterpret_borrow<array>(base).flags() & ~detail::npy_api::NPY_ARRAY_OWNDATA_;
602
- else
603
- /* Writable by default, easy to downgrade later on if needed */
604
- flags = detail::npy_api::NPY_ARRAY_WRITEABLE_;
605
- }
606
-
607
- auto &api = detail::npy_api::get();
608
- auto tmp = reinterpret_steal<object>(api.PyArray_NewFromDescr_(
609
- api.PyArray_Type_, descr.release().ptr(), (int) ndim,
610
- // Use reinterpret_cast for PyPy on Windows (remove if fixed, checked on 7.3.1)
611
- reinterpret_cast<Py_intptr_t*>(shape->data()),
612
- reinterpret_cast<Py_intptr_t*>(strides->data()),
613
- const_cast<void *>(ptr), flags, nullptr));
614
- if (!tmp)
615
- throw error_already_set();
616
- if (ptr) {
617
- if (base) {
618
- api.PyArray_SetBaseObject_(tmp.ptr(), base.inc_ref().ptr());
619
- } else {
620
- tmp = reinterpret_steal<object>(api.PyArray_NewCopy_(tmp.ptr(), -1 /* any order */));
621
- }
622
- }
623
- m_ptr = tmp.release().ptr();
624
- }
625
-
626
- array(const pybind11::dtype &dt, ShapeContainer shape, const void *ptr = nullptr, handle base = handle())
627
- : array(dt, std::move(shape), {}, ptr, base) { }
628
-
629
- template <typename T, typename = detail::enable_if_t<std::is_integral<T>::value && !std::is_same<bool, T>::value>>
630
- array(const pybind11::dtype &dt, T count, const void *ptr = nullptr, handle base = handle())
631
- : array(dt, {{count}}, ptr, base) { }
632
-
633
- template <typename T>
634
- array(ShapeContainer shape, StridesContainer strides, const T *ptr, handle base = handle())
635
- : array(pybind11::dtype::of<T>(), std::move(shape), std::move(strides), ptr, base) { }
636
-
637
- template <typename T>
638
- array(ShapeContainer shape, const T *ptr, handle base = handle())
639
- : array(std::move(shape), {}, ptr, base) { }
640
-
641
- template <typename T>
642
- explicit array(ssize_t count, const T *ptr, handle base = handle()) : array({count}, {}, ptr, base) { }
643
-
644
- explicit array(const buffer_info &info, handle base = handle())
645
- : array(pybind11::dtype(info), info.shape, info.strides, info.ptr, base) { }
646
-
647
- /// Array descriptor (dtype)
648
- pybind11::dtype dtype() const {
649
- return reinterpret_borrow<pybind11::dtype>(detail::array_proxy(m_ptr)->descr);
650
- }
651
-
652
- /// Total number of elements
653
- ssize_t size() const {
654
- return std::accumulate(shape(), shape() + ndim(), (ssize_t) 1, std::multiplies<ssize_t>());
655
- }
656
-
657
- /// Byte size of a single element
658
- ssize_t itemsize() const {
659
- return detail::array_descriptor_proxy(detail::array_proxy(m_ptr)->descr)->elsize;
660
- }
661
-
662
- /// Total number of bytes
663
- ssize_t nbytes() const {
664
- return size() * itemsize();
665
- }
666
-
667
- /// Number of dimensions
668
- ssize_t ndim() const {
669
- return detail::array_proxy(m_ptr)->nd;
670
- }
671
-
672
- /// Base object
673
- object base() const {
674
- return reinterpret_borrow<object>(detail::array_proxy(m_ptr)->base);
675
- }
676
-
677
- /// Dimensions of the array
678
- const ssize_t* shape() const {
679
- return detail::array_proxy(m_ptr)->dimensions;
680
- }
681
-
682
- /// Dimension along a given axis
683
- ssize_t shape(ssize_t dim) const {
684
- if (dim >= ndim())
685
- fail_dim_check(dim, "invalid axis");
686
- return shape()[dim];
687
- }
688
-
689
- /// Strides of the array
690
- const ssize_t* strides() const {
691
- return detail::array_proxy(m_ptr)->strides;
692
- }
693
-
694
- /// Stride along a given axis
695
- ssize_t strides(ssize_t dim) const {
696
- if (dim >= ndim())
697
- fail_dim_check(dim, "invalid axis");
698
- return strides()[dim];
699
- }
700
-
701
- /// Return the NumPy array flags
702
- int flags() const {
703
- return detail::array_proxy(m_ptr)->flags;
704
- }
705
-
706
- /// If set, the array is writeable (otherwise the buffer is read-only)
707
- bool writeable() const {
708
- return detail::check_flags(m_ptr, detail::npy_api::NPY_ARRAY_WRITEABLE_);
709
- }
710
-
711
- /// If set, the array owns the data (will be freed when the array is deleted)
712
- bool owndata() const {
713
- return detail::check_flags(m_ptr, detail::npy_api::NPY_ARRAY_OWNDATA_);
714
- }
715
-
716
- /// Pointer to the contained data. If index is not provided, points to the
717
- /// beginning of the buffer. May throw if the index would lead to out of bounds access.
718
- template<typename... Ix> const void* data(Ix... index) const {
719
- return static_cast<const void *>(detail::array_proxy(m_ptr)->data + offset_at(index...));
720
- }
721
-
722
- /// Mutable pointer to the contained data. If index is not provided, points to the
723
- /// beginning of the buffer. May throw if the index would lead to out of bounds access.
724
- /// May throw if the array is not writeable.
725
- template<typename... Ix> void* mutable_data(Ix... index) {
726
- check_writeable();
727
- return static_cast<void *>(detail::array_proxy(m_ptr)->data + offset_at(index...));
728
- }
729
-
730
- /// Byte offset from beginning of the array to a given index (full or partial).
731
- /// May throw if the index would lead to out of bounds access.
732
- template<typename... Ix> ssize_t offset_at(Ix... index) const {
733
- if ((ssize_t) sizeof...(index) > ndim())
734
- fail_dim_check(sizeof...(index), "too many indices for an array");
735
- return byte_offset(ssize_t(index)...);
736
- }
737
-
738
- ssize_t offset_at() const { return 0; }
739
-
740
- /// Item count from beginning of the array to a given index (full or partial).
741
- /// May throw if the index would lead to out of bounds access.
742
- template<typename... Ix> ssize_t index_at(Ix... index) const {
743
- return offset_at(index...) / itemsize();
744
- }
745
-
746
- /**
747
- * Returns a proxy object that provides access to the array's data without bounds or
748
- * dimensionality checking. Will throw if the array is missing the `writeable` flag. Use with
749
- * care: the array must not be destroyed or reshaped for the duration of the returned object,
750
- * and the caller must take care not to access invalid dimensions or dimension indices.
751
- */
752
- template <typename T, ssize_t Dims = -1> detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() & {
753
- if (PYBIND11_SILENCE_MSVC_C4127(Dims >= 0) && ndim() != Dims)
754
- throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) +
755
- "; expected " + std::to_string(Dims));
756
- return detail::unchecked_mutable_reference<T, Dims>(mutable_data(), shape(), strides(), ndim());
757
- }
758
-
759
- /**
760
- * Returns a proxy object that provides const access to the array's data without bounds or
761
- * dimensionality checking. Unlike `mutable_unchecked()`, this does not require that the
762
- * underlying array have the `writable` flag. Use with care: the array must not be destroyed or
763
- * reshaped for the duration of the returned object, and the caller must take care not to access
764
- * invalid dimensions or dimension indices.
765
- */
766
- template <typename T, ssize_t Dims = -1> detail::unchecked_reference<T, Dims> unchecked() const & {
767
- if (PYBIND11_SILENCE_MSVC_C4127(Dims >= 0) && ndim() != Dims)
768
- throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) +
769
- "; expected " + std::to_string(Dims));
770
- return detail::unchecked_reference<T, Dims>(data(), shape(), strides(), ndim());
771
- }
772
-
773
- /// Return a new view with all of the dimensions of length 1 removed
774
- array squeeze() {
775
- auto& api = detail::npy_api::get();
776
- return reinterpret_steal<array>(api.PyArray_Squeeze_(m_ptr));
777
- }
778
-
779
- /// Resize array to given shape
780
- /// If refcheck is true and more that one reference exist to this array
781
- /// then resize will succeed only if it makes a reshape, i.e. original size doesn't change
782
- void resize(ShapeContainer new_shape, bool refcheck = true) {
783
- detail::npy_api::PyArray_Dims d = {
784
- // Use reinterpret_cast for PyPy on Windows (remove if fixed, checked on 7.3.1)
785
- reinterpret_cast<Py_intptr_t*>(new_shape->data()),
786
- int(new_shape->size())
787
- };
788
- // try to resize, set ordering param to -1 cause it's not used anyway
789
- auto new_array = reinterpret_steal<object>(
790
- detail::npy_api::get().PyArray_Resize_(m_ptr, &d, int(refcheck), -1)
791
- );
792
- if (!new_array) throw error_already_set();
793
- if (isinstance<array>(new_array)) { *this = std::move(new_array); }
794
- }
795
-
796
- /// Optional `order` parameter omitted, to be added as needed.
797
- array reshape(ShapeContainer new_shape) {
798
- detail::npy_api::PyArray_Dims d
799
- = {reinterpret_cast<Py_intptr_t *>(new_shape->data()), int(new_shape->size())};
800
- auto new_array
801
- = reinterpret_steal<array>(detail::npy_api::get().PyArray_Newshape_(m_ptr, &d, 0));
802
- if (!new_array) {
803
- throw error_already_set();
804
- }
805
- return new_array;
806
- }
807
-
808
- /// Create a view of an array in a different data type.
809
- /// This function may fundamentally reinterpret the data in the array.
810
- /// It is the responsibility of the caller to ensure that this is safe.
811
- /// Only supports the `dtype` argument, the `type` argument is omitted,
812
- /// to be added as needed.
813
- array view(const std::string &dtype) {
814
- auto &api = detail::npy_api::get();
815
- auto new_view = reinterpret_steal<array>(api.PyArray_View_(
816
- m_ptr, dtype::from_args(pybind11::str(dtype)).release().ptr(), nullptr));
817
- if (!new_view) {
818
- throw error_already_set();
819
- }
820
- return new_view;
821
- }
822
-
823
- /// Ensure that the argument is a NumPy array
824
- /// In case of an error, nullptr is returned and the Python error is cleared.
825
- static array ensure(handle h, int ExtraFlags = 0) {
826
- auto result = reinterpret_steal<array>(raw_array(h.ptr(), ExtraFlags));
827
- if (!result)
828
- PyErr_Clear();
829
- return result;
830
- }
831
-
832
- protected:
833
- template<typename, typename> friend struct detail::npy_format_descriptor;
834
-
835
- void fail_dim_check(ssize_t dim, const std::string& msg) const {
836
- throw index_error(msg + ": " + std::to_string(dim) +
837
- " (ndim = " + std::to_string(ndim()) + ")");
838
- }
839
-
840
- template<typename... Ix> ssize_t byte_offset(Ix... index) const {
841
- check_dimensions(index...);
842
- return detail::byte_offset_unsafe(strides(), ssize_t(index)...);
843
- }
844
-
845
- void check_writeable() const {
846
- if (!writeable())
847
- throw std::domain_error("array is not writeable");
848
- }
849
-
850
- template<typename... Ix> void check_dimensions(Ix... index) const {
851
- check_dimensions_impl(ssize_t(0), shape(), ssize_t(index)...);
852
- }
853
-
854
- void check_dimensions_impl(ssize_t, const ssize_t*) const { }
855
-
856
- template<typename... Ix> void check_dimensions_impl(ssize_t axis, const ssize_t* shape, ssize_t i, Ix... index) const {
857
- if (i >= *shape) {
858
- throw index_error(std::string("index ") + std::to_string(i) +
859
- " is out of bounds for axis " + std::to_string(axis) +
860
- " with size " + std::to_string(*shape));
861
- }
862
- check_dimensions_impl(axis + 1, shape + 1, index...);
863
- }
864
-
865
- /// Create array from any object -- always returns a new reference
866
- static PyObject *raw_array(PyObject *ptr, int ExtraFlags = 0) {
867
- if (ptr == nullptr) {
868
- PyErr_SetString(PyExc_ValueError, "cannot create a pybind11::array from a nullptr");
869
- return nullptr;
870
- }
871
- return detail::npy_api::get().PyArray_FromAny_(
872
- ptr, nullptr, 0, 0, detail::npy_api::NPY_ARRAY_ENSUREARRAY_ | ExtraFlags, nullptr);
873
- }
874
- };
875
-
876
- template <typename T, int ExtraFlags = array::forcecast> class array_t : public array {
877
- private:
878
- struct private_ctor {};
879
- // Delegating constructor needed when both moving and accessing in the same constructor
880
- array_t(private_ctor, ShapeContainer &&shape, StridesContainer &&strides, const T *ptr, handle base)
881
- : array(std::move(shape), std::move(strides), ptr, base) {}
882
- public:
883
- static_assert(!detail::array_info<T>::is_array, "Array types cannot be used with array_t");
884
-
885
- using value_type = T;
886
-
887
- array_t() : array(0, static_cast<const T *>(nullptr)) {}
888
- array_t(handle h, borrowed_t) : array(h, borrowed_t{}) { }
889
- array_t(handle h, stolen_t) : array(h, stolen_t{}) { }
890
-
891
- PYBIND11_DEPRECATED("Use array_t<T>::ensure() instead")
892
- array_t(handle h, bool is_borrowed) : array(raw_array_t(h.ptr()), stolen_t{}) {
893
- if (!m_ptr) PyErr_Clear();
894
- if (!is_borrowed) Py_XDECREF(h.ptr());
895
- }
896
-
897
- // NOLINTNEXTLINE(google-explicit-constructor)
898
- array_t(const object &o) : array(raw_array_t(o.ptr()), stolen_t{}) {
899
- if (!m_ptr) throw error_already_set();
900
- }
901
-
902
- explicit array_t(const buffer_info& info, handle base = handle()) : array(info, base) { }
903
-
904
- array_t(ShapeContainer shape, StridesContainer strides, const T *ptr = nullptr, handle base = handle())
905
- : array(std::move(shape), std::move(strides), ptr, base) { }
906
-
907
- explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle())
908
- : array_t(private_ctor{},
909
- std::move(shape),
910
- (ExtraFlags & f_style) != 0 ? detail::f_strides(*shape, itemsize())
911
- : detail::c_strides(*shape, itemsize()),
912
- ptr,
913
- base) {}
914
-
915
- explicit array_t(ssize_t count, const T *ptr = nullptr, handle base = handle())
916
- : array({count}, {}, ptr, base) { }
917
-
918
- constexpr ssize_t itemsize() const {
919
- return sizeof(T);
920
- }
921
-
922
- template<typename... Ix> ssize_t index_at(Ix... index) const {
923
- return offset_at(index...) / itemsize();
924
- }
925
-
926
- template<typename... Ix> const T* data(Ix... index) const {
927
- return static_cast<const T*>(array::data(index...));
928
- }
929
-
930
- template<typename... Ix> T* mutable_data(Ix... index) {
931
- return static_cast<T*>(array::mutable_data(index...));
932
- }
933
-
934
- // Reference to element at a given index
935
- template<typename... Ix> const T& at(Ix... index) const {
936
- if ((ssize_t) sizeof...(index) != ndim())
937
- fail_dim_check(sizeof...(index), "index dimension mismatch");
938
- return *(static_cast<const T*>(array::data()) + byte_offset(ssize_t(index)...) / itemsize());
939
- }
940
-
941
- // Mutable reference to element at a given index
942
- template<typename... Ix> T& mutable_at(Ix... index) {
943
- if ((ssize_t) sizeof...(index) != ndim())
944
- fail_dim_check(sizeof...(index), "index dimension mismatch");
945
- return *(static_cast<T*>(array::mutable_data()) + byte_offset(ssize_t(index)...) / itemsize());
946
- }
947
-
948
- /**
949
- * Returns a proxy object that provides access to the array's data without bounds or
950
- * dimensionality checking. Will throw if the array is missing the `writeable` flag. Use with
951
- * care: the array must not be destroyed or reshaped for the duration of the returned object,
952
- * and the caller must take care not to access invalid dimensions or dimension indices.
953
- */
954
- template <ssize_t Dims = -1> detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() & {
955
- return array::mutable_unchecked<T, Dims>();
956
- }
957
-
958
- /**
959
- * Returns a proxy object that provides const access to the array's data without bounds or
960
- * dimensionality checking. Unlike `unchecked()`, this does not require that the underlying
961
- * array have the `writable` flag. Use with care: the array must not be destroyed or reshaped
962
- * for the duration of the returned object, and the caller must take care not to access invalid
963
- * dimensions or dimension indices.
964
- */
965
- template <ssize_t Dims = -1> detail::unchecked_reference<T, Dims> unchecked() const & {
966
- return array::unchecked<T, Dims>();
967
- }
968
-
969
- /// Ensure that the argument is a NumPy array of the correct dtype (and if not, try to convert
970
- /// it). In case of an error, nullptr is returned and the Python error is cleared.
971
- static array_t ensure(handle h) {
972
- auto result = reinterpret_steal<array_t>(raw_array_t(h.ptr()));
973
- if (!result)
974
- PyErr_Clear();
975
- return result;
976
- }
977
-
978
- static bool check_(handle h) {
979
- const auto &api = detail::npy_api::get();
980
- return api.PyArray_Check_(h.ptr())
981
- && api.PyArray_EquivTypes_(detail::array_proxy(h.ptr())->descr, dtype::of<T>().ptr())
982
- && detail::check_flags(h.ptr(), ExtraFlags & (array::c_style | array::f_style));
983
- }
984
-
985
- protected:
986
- /// Create array from any object -- always returns a new reference
987
- static PyObject *raw_array_t(PyObject *ptr) {
988
- if (ptr == nullptr) {
989
- PyErr_SetString(PyExc_ValueError, "cannot create a pybind11::array_t from a nullptr");
990
- return nullptr;
991
- }
992
- return detail::npy_api::get().PyArray_FromAny_(
993
- ptr, dtype::of<T>().release().ptr(), 0, 0,
994
- detail::npy_api::NPY_ARRAY_ENSUREARRAY_ | ExtraFlags, nullptr);
995
- }
996
- };
997
-
998
- template <typename T>
999
- struct format_descriptor<T, detail::enable_if_t<detail::is_pod_struct<T>::value>> {
1000
- static std::string format() {
1001
- return detail::npy_format_descriptor<typename std::remove_cv<T>::type>::format();
1002
- }
1003
- };
1004
-
1005
- template <size_t N> struct format_descriptor<char[N]> {
1006
- static std::string format() { return std::to_string(N) + "s"; }
1007
- };
1008
- template <size_t N> struct format_descriptor<std::array<char, N>> {
1009
- static std::string format() { return std::to_string(N) + "s"; }
1010
- };
1011
-
1012
- template <typename T>
1013
- struct format_descriptor<T, detail::enable_if_t<std::is_enum<T>::value>> {
1014
- static std::string format() {
1015
- return format_descriptor<
1016
- typename std::remove_cv<typename std::underlying_type<T>::type>::type>::format();
1017
- }
1018
- };
1019
-
1020
- template <typename T>
1021
- struct format_descriptor<T, detail::enable_if_t<detail::array_info<T>::is_array>> {
1022
- static std::string format() {
1023
- using namespace detail;
1024
- static constexpr auto extents = _("(") + array_info<T>::extents + _(")");
1025
- return extents.text + format_descriptor<remove_all_extents_t<T>>::format();
1026
- }
1027
- };
1028
-
1029
- PYBIND11_NAMESPACE_BEGIN(detail)
1030
- template <typename T, int ExtraFlags>
1031
- struct pyobject_caster<array_t<T, ExtraFlags>> {
1032
- using type = array_t<T, ExtraFlags>;
1033
-
1034
- bool load(handle src, bool convert) {
1035
- if (!convert && !type::check_(src))
1036
- return false;
1037
- value = type::ensure(src);
1038
- return static_cast<bool>(value);
1039
- }
1040
-
1041
- static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) {
1042
- return src.inc_ref();
1043
- }
1044
- PYBIND11_TYPE_CASTER(type, handle_type_name<type>::name);
1045
- };
1046
-
1047
- template <typename T>
1048
- struct compare_buffer_info<T, detail::enable_if_t<detail::is_pod_struct<T>::value>> {
1049
- static bool compare(const buffer_info& b) {
1050
- return npy_api::get().PyArray_EquivTypes_(dtype::of<T>().ptr(), dtype(b).ptr());
1051
- }
1052
- };
1053
-
1054
- template <typename T, typename = void>
1055
- struct npy_format_descriptor_name;
1056
-
1057
- template <typename T>
1058
- struct npy_format_descriptor_name<T, enable_if_t<std::is_integral<T>::value>> {
1059
- static constexpr auto name = _<std::is_same<T, bool>::value>(
1060
- _("bool"), _<std::is_signed<T>::value>("numpy.int", "numpy.uint") + _<sizeof(T)*8>()
1061
- );
1062
- };
1063
-
1064
- template <typename T>
1065
- struct npy_format_descriptor_name<T, enable_if_t<std::is_floating_point<T>::value>> {
1066
- static constexpr auto name = _<std::is_same<T, float>::value
1067
- || std::is_same<T, const float>::value
1068
- || std::is_same<T, double>::value
1069
- || std::is_same<T, const double>::value>(
1070
- _("numpy.float") + _<sizeof(T)*8>(), _("numpy.longdouble")
1071
- );
1072
- };
1073
-
1074
- template <typename T>
1075
- struct npy_format_descriptor_name<T, enable_if_t<is_complex<T>::value>> {
1076
- static constexpr auto name = _<std::is_same<typename T::value_type, float>::value
1077
- || std::is_same<typename T::value_type, const float>::value
1078
- || std::is_same<typename T::value_type, double>::value
1079
- || std::is_same<typename T::value_type, const double>::value>(
1080
- _("numpy.complex") + _<sizeof(typename T::value_type)*16>(), _("numpy.longcomplex")
1081
- );
1082
- };
1083
-
1084
- template <typename T>
1085
- struct npy_format_descriptor<T, enable_if_t<satisfies_any_of<T, std::is_arithmetic, is_complex>::value>>
1086
- : npy_format_descriptor_name<T> {
1087
- private:
1088
- // NB: the order here must match the one in common.h
1089
- constexpr static const int values[15] = {
1090
- npy_api::NPY_BOOL_,
1091
- npy_api::NPY_BYTE_, npy_api::NPY_UBYTE_, npy_api::NPY_INT16_, npy_api::NPY_UINT16_,
1092
- npy_api::NPY_INT32_, npy_api::NPY_UINT32_, npy_api::NPY_INT64_, npy_api::NPY_UINT64_,
1093
- npy_api::NPY_FLOAT_, npy_api::NPY_DOUBLE_, npy_api::NPY_LONGDOUBLE_,
1094
- npy_api::NPY_CFLOAT_, npy_api::NPY_CDOUBLE_, npy_api::NPY_CLONGDOUBLE_
1095
- };
1096
-
1097
- public:
1098
- static constexpr int value = values[detail::is_fmt_numeric<T>::index];
1099
-
1100
- static pybind11::dtype dtype() {
1101
- if (auto ptr = npy_api::get().PyArray_DescrFromType_(value))
1102
- return reinterpret_steal<pybind11::dtype>(ptr);
1103
- pybind11_fail("Unsupported buffer format!");
1104
- }
1105
- };
1106
-
1107
- #define PYBIND11_DECL_CHAR_FMT \
1108
- static constexpr auto name = _("S") + _<N>(); \
1109
- static pybind11::dtype dtype() { return pybind11::dtype(std::string("S") + std::to_string(N)); }
1110
- template <size_t N> struct npy_format_descriptor<char[N]> { PYBIND11_DECL_CHAR_FMT };
1111
- template <size_t N> struct npy_format_descriptor<std::array<char, N>> { PYBIND11_DECL_CHAR_FMT };
1112
- #undef PYBIND11_DECL_CHAR_FMT
1113
-
1114
- template<typename T> struct npy_format_descriptor<T, enable_if_t<array_info<T>::is_array>> {
1115
- private:
1116
- using base_descr = npy_format_descriptor<typename array_info<T>::type>;
1117
- public:
1118
- static_assert(!array_info<T>::is_empty, "Zero-sized arrays are not supported");
1119
-
1120
- static constexpr auto name = _("(") + array_info<T>::extents + _(")") + base_descr::name;
1121
- static pybind11::dtype dtype() {
1122
- list shape;
1123
- array_info<T>::append_extents(shape);
1124
- return pybind11::dtype::from_args(pybind11::make_tuple(base_descr::dtype(), shape));
1125
- }
1126
- };
1127
-
1128
- template<typename T> struct npy_format_descriptor<T, enable_if_t<std::is_enum<T>::value>> {
1129
- private:
1130
- using base_descr = npy_format_descriptor<typename std::underlying_type<T>::type>;
1131
- public:
1132
- static constexpr auto name = base_descr::name;
1133
- static pybind11::dtype dtype() { return base_descr::dtype(); }
1134
- };
1135
-
1136
- struct field_descriptor {
1137
- const char *name;
1138
- ssize_t offset;
1139
- ssize_t size;
1140
- std::string format;
1141
- dtype descr;
1142
- };
1143
-
1144
- PYBIND11_NOINLINE void register_structured_dtype(
1145
- any_container<field_descriptor> fields,
1146
- const std::type_info& tinfo, ssize_t itemsize,
1147
- bool (*direct_converter)(PyObject *, void *&)) {
1148
-
1149
- auto& numpy_internals = get_numpy_internals();
1150
- if (numpy_internals.get_type_info(tinfo, false))
1151
- pybind11_fail("NumPy: dtype is already registered");
1152
-
1153
- // Use ordered fields because order matters as of NumPy 1.14:
1154
- // https://docs.scipy.org/doc/numpy/release.html#multiple-field-indexing-assignment-of-structured-arrays
1155
- std::vector<field_descriptor> ordered_fields(std::move(fields));
1156
- std::sort(ordered_fields.begin(), ordered_fields.end(),
1157
- [](const field_descriptor &a, const field_descriptor &b) { return a.offset < b.offset; });
1158
-
1159
- list names, formats, offsets;
1160
- for (auto& field : ordered_fields) {
1161
- if (!field.descr)
1162
- pybind11_fail(std::string("NumPy: unsupported field dtype: `") +
1163
- field.name + "` @ " + tinfo.name());
1164
- names.append(PYBIND11_STR_TYPE(field.name));
1165
- formats.append(field.descr);
1166
- offsets.append(pybind11::int_(field.offset));
1167
- }
1168
- auto dtype_ptr
1169
- = pybind11::dtype(std::move(names), std::move(formats), std::move(offsets), itemsize)
1170
- .release()
1171
- .ptr();
1172
-
1173
- // There is an existing bug in NumPy (as of v1.11): trailing bytes are
1174
- // not encoded explicitly into the format string. This will supposedly
1175
- // get fixed in v1.12; for further details, see these:
1176
- // - https://github.com/numpy/numpy/issues/7797
1177
- // - https://github.com/numpy/numpy/pull/7798
1178
- // Because of this, we won't use numpy's logic to generate buffer format
1179
- // strings and will just do it ourselves.
1180
- ssize_t offset = 0;
1181
- std::ostringstream oss;
1182
- // mark the structure as unaligned with '^', because numpy and C++ don't
1183
- // always agree about alignment (particularly for complex), and we're
1184
- // explicitly listing all our padding. This depends on none of the fields
1185
- // overriding the endianness. Putting the ^ in front of individual fields
1186
- // isn't guaranteed to work due to https://github.com/numpy/numpy/issues/9049
1187
- oss << "^T{";
1188
- for (auto& field : ordered_fields) {
1189
- if (field.offset > offset)
1190
- oss << (field.offset - offset) << 'x';
1191
- oss << field.format << ':' << field.name << ':';
1192
- offset = field.offset + field.size;
1193
- }
1194
- if (itemsize > offset)
1195
- oss << (itemsize - offset) << 'x';
1196
- oss << '}';
1197
- auto format_str = oss.str();
1198
-
1199
- // Sanity check: verify that NumPy properly parses our buffer format string
1200
- auto& api = npy_api::get();
1201
- auto arr = array(buffer_info(nullptr, itemsize, format_str, 1));
1202
- if (!api.PyArray_EquivTypes_(dtype_ptr, arr.dtype().ptr()))
1203
- pybind11_fail("NumPy: invalid buffer descriptor!");
1204
-
1205
- auto tindex = std::type_index(tinfo);
1206
- numpy_internals.registered_dtypes[tindex] = { dtype_ptr, format_str };
1207
- get_internals().direct_conversions[tindex].push_back(direct_converter);
1208
- }
1209
-
1210
- template <typename T, typename SFINAE> struct npy_format_descriptor {
1211
- static_assert(is_pod_struct<T>::value, "Attempt to use a non-POD or unimplemented POD type as a numpy dtype");
1212
-
1213
- static constexpr auto name = make_caster<T>::name;
1214
-
1215
- static pybind11::dtype dtype() {
1216
- return reinterpret_borrow<pybind11::dtype>(dtype_ptr());
1217
- }
1218
-
1219
- static std::string format() {
1220
- static auto format_str = get_numpy_internals().get_type_info<T>(true)->format_str;
1221
- return format_str;
1222
- }
1223
-
1224
- static void register_dtype(any_container<field_descriptor> fields) {
1225
- register_structured_dtype(std::move(fields), typeid(typename std::remove_cv<T>::type),
1226
- sizeof(T), &direct_converter);
1227
- }
1228
-
1229
- private:
1230
- static PyObject* dtype_ptr() {
1231
- static PyObject* ptr = get_numpy_internals().get_type_info<T>(true)->dtype_ptr;
1232
- return ptr;
1233
- }
1234
-
1235
- static bool direct_converter(PyObject *obj, void*& value) {
1236
- auto& api = npy_api::get();
1237
- if (!PyObject_TypeCheck(obj, api.PyVoidArrType_Type_))
1238
- return false;
1239
- if (auto descr = reinterpret_steal<object>(api.PyArray_DescrFromScalar_(obj))) {
1240
- if (api.PyArray_EquivTypes_(dtype_ptr(), descr.ptr())) {
1241
- value = ((PyVoidScalarObject_Proxy *) obj)->obval;
1242
- return true;
1243
- }
1244
- }
1245
- return false;
1246
- }
1247
- };
1248
-
1249
- #ifdef __CLION_IDE__ // replace heavy macro with dummy code for the IDE (doesn't affect code)
1250
- # define PYBIND11_NUMPY_DTYPE(Type, ...) ((void)0)
1251
- # define PYBIND11_NUMPY_DTYPE_EX(Type, ...) ((void)0)
1252
- #else
1253
-
1254
- #define PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, Name) \
1255
- ::pybind11::detail::field_descriptor { \
1256
- Name, offsetof(T, Field), sizeof(decltype(std::declval<T>().Field)), \
1257
- ::pybind11::format_descriptor<decltype(std::declval<T>().Field)>::format(), \
1258
- ::pybind11::detail::npy_format_descriptor<decltype(std::declval<T>().Field)>::dtype() \
1259
- }
1260
-
1261
- // Extract name, offset and format descriptor for a struct field
1262
- #define PYBIND11_FIELD_DESCRIPTOR(T, Field) PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, #Field)
1263
-
1264
- // The main idea of this macro is borrowed from https://github.com/swansontec/map-macro
1265
- // (C) William Swanson, Paul Fultz
1266
- #define PYBIND11_EVAL0(...) __VA_ARGS__
1267
- #define PYBIND11_EVAL1(...) PYBIND11_EVAL0 (PYBIND11_EVAL0 (PYBIND11_EVAL0 (__VA_ARGS__)))
1268
- #define PYBIND11_EVAL2(...) PYBIND11_EVAL1 (PYBIND11_EVAL1 (PYBIND11_EVAL1 (__VA_ARGS__)))
1269
- #define PYBIND11_EVAL3(...) PYBIND11_EVAL2 (PYBIND11_EVAL2 (PYBIND11_EVAL2 (__VA_ARGS__)))
1270
- #define PYBIND11_EVAL4(...) PYBIND11_EVAL3 (PYBIND11_EVAL3 (PYBIND11_EVAL3 (__VA_ARGS__)))
1271
- #define PYBIND11_EVAL(...) PYBIND11_EVAL4 (PYBIND11_EVAL4 (PYBIND11_EVAL4 (__VA_ARGS__)))
1272
- #define PYBIND11_MAP_END(...)
1273
- #define PYBIND11_MAP_OUT
1274
- #define PYBIND11_MAP_COMMA ,
1275
- #define PYBIND11_MAP_GET_END() 0, PYBIND11_MAP_END
1276
- #define PYBIND11_MAP_NEXT0(test, next, ...) next PYBIND11_MAP_OUT
1277
- #define PYBIND11_MAP_NEXT1(test, next) PYBIND11_MAP_NEXT0 (test, next, 0)
1278
- #define PYBIND11_MAP_NEXT(test, next) PYBIND11_MAP_NEXT1 (PYBIND11_MAP_GET_END test, next)
1279
- #if defined(_MSC_VER) && !defined(__clang__) // MSVC is not as eager to expand macros, hence this workaround
1280
- #define PYBIND11_MAP_LIST_NEXT1(test, next) \
1281
- PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0))
1282
- #else
1283
- #define PYBIND11_MAP_LIST_NEXT1(test, next) \
1284
- PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)
1285
- #endif
1286
- #define PYBIND11_MAP_LIST_NEXT(test, next) \
1287
- PYBIND11_MAP_LIST_NEXT1 (PYBIND11_MAP_GET_END test, next)
1288
- #define PYBIND11_MAP_LIST0(f, t, x, peek, ...) \
1289
- f(t, x) PYBIND11_MAP_LIST_NEXT (peek, PYBIND11_MAP_LIST1) (f, t, peek, __VA_ARGS__)
1290
- #define PYBIND11_MAP_LIST1(f, t, x, peek, ...) \
1291
- f(t, x) PYBIND11_MAP_LIST_NEXT (peek, PYBIND11_MAP_LIST0) (f, t, peek, __VA_ARGS__)
1292
- // PYBIND11_MAP_LIST(f, t, a1, a2, ...) expands to f(t, a1), f(t, a2), ...
1293
- #define PYBIND11_MAP_LIST(f, t, ...) \
1294
- PYBIND11_EVAL (PYBIND11_MAP_LIST1 (f, t, __VA_ARGS__, (), 0))
1295
-
1296
- #define PYBIND11_NUMPY_DTYPE(Type, ...) \
1297
- ::pybind11::detail::npy_format_descriptor<Type>::register_dtype \
1298
- (::std::vector<::pybind11::detail::field_descriptor> \
1299
- {PYBIND11_MAP_LIST (PYBIND11_FIELD_DESCRIPTOR, Type, __VA_ARGS__)})
1300
-
1301
- #if defined(_MSC_VER) && !defined(__clang__)
1302
- #define PYBIND11_MAP2_LIST_NEXT1(test, next) \
1303
- PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0))
1304
- #else
1305
- #define PYBIND11_MAP2_LIST_NEXT1(test, next) \
1306
- PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)
1307
- #endif
1308
- #define PYBIND11_MAP2_LIST_NEXT(test, next) \
1309
- PYBIND11_MAP2_LIST_NEXT1 (PYBIND11_MAP_GET_END test, next)
1310
- #define PYBIND11_MAP2_LIST0(f, t, x1, x2, peek, ...) \
1311
- f(t, x1, x2) PYBIND11_MAP2_LIST_NEXT (peek, PYBIND11_MAP2_LIST1) (f, t, peek, __VA_ARGS__)
1312
- #define PYBIND11_MAP2_LIST1(f, t, x1, x2, peek, ...) \
1313
- f(t, x1, x2) PYBIND11_MAP2_LIST_NEXT (peek, PYBIND11_MAP2_LIST0) (f, t, peek, __VA_ARGS__)
1314
- // PYBIND11_MAP2_LIST(f, t, a1, a2, ...) expands to f(t, a1, a2), f(t, a3, a4), ...
1315
- #define PYBIND11_MAP2_LIST(f, t, ...) \
1316
- PYBIND11_EVAL (PYBIND11_MAP2_LIST1 (f, t, __VA_ARGS__, (), 0))
1317
-
1318
- #define PYBIND11_NUMPY_DTYPE_EX(Type, ...) \
1319
- ::pybind11::detail::npy_format_descriptor<Type>::register_dtype \
1320
- (::std::vector<::pybind11::detail::field_descriptor> \
1321
- {PYBIND11_MAP2_LIST (PYBIND11_FIELD_DESCRIPTOR_EX, Type, __VA_ARGS__)})
1322
-
1323
- #endif // __CLION_IDE__
1324
-
1325
- class common_iterator {
1326
- public:
1327
- using container_type = std::vector<ssize_t>;
1328
- using value_type = container_type::value_type;
1329
- using size_type = container_type::size_type;
1330
-
1331
- common_iterator() : m_strides() {}
1332
-
1333
- common_iterator(void* ptr, const container_type& strides, const container_type& shape)
1334
- : p_ptr(reinterpret_cast<char*>(ptr)), m_strides(strides.size()) {
1335
- m_strides.back() = static_cast<value_type>(strides.back());
1336
- for (size_type i = m_strides.size() - 1; i != 0; --i) {
1337
- size_type j = i - 1;
1338
- auto s = static_cast<value_type>(shape[i]);
1339
- m_strides[j] = strides[j] + m_strides[i] - strides[i] * s;
1340
- }
1341
- }
1342
-
1343
- void increment(size_type dim) {
1344
- p_ptr += m_strides[dim];
1345
- }
1346
-
1347
- void* data() const {
1348
- return p_ptr;
1349
- }
1350
-
1351
- private:
1352
- char *p_ptr{0};
1353
- container_type m_strides;
1354
- };
1355
-
1356
- template <size_t N> class multi_array_iterator {
1357
- public:
1358
- using container_type = std::vector<ssize_t>;
1359
-
1360
- multi_array_iterator(const std::array<buffer_info, N> &buffers,
1361
- const container_type &shape)
1362
- : m_shape(shape.size()), m_index(shape.size(), 0),
1363
- m_common_iterator() {
1364
-
1365
- // Manual copy to avoid conversion warning if using std::copy
1366
- for (size_t i = 0; i < shape.size(); ++i)
1367
- m_shape[i] = shape[i];
1368
-
1369
- container_type strides(shape.size());
1370
- for (size_t i = 0; i < N; ++i)
1371
- init_common_iterator(buffers[i], shape, m_common_iterator[i], strides);
1372
- }
1373
-
1374
- multi_array_iterator& operator++() {
1375
- for (size_t j = m_index.size(); j != 0; --j) {
1376
- size_t i = j - 1;
1377
- if (++m_index[i] != m_shape[i]) {
1378
- increment_common_iterator(i);
1379
- break;
1380
- }
1381
- m_index[i] = 0;
1382
- }
1383
- return *this;
1384
- }
1385
-
1386
- template <size_t K, class T = void> T* data() const {
1387
- return reinterpret_cast<T*>(m_common_iterator[K].data());
1388
- }
1389
-
1390
- private:
1391
-
1392
- using common_iter = common_iterator;
1393
-
1394
- void init_common_iterator(const buffer_info &buffer,
1395
- const container_type &shape,
1396
- common_iter &iterator,
1397
- container_type &strides) {
1398
- auto buffer_shape_iter = buffer.shape.rbegin();
1399
- auto buffer_strides_iter = buffer.strides.rbegin();
1400
- auto shape_iter = shape.rbegin();
1401
- auto strides_iter = strides.rbegin();
1402
-
1403
- while (buffer_shape_iter != buffer.shape.rend()) {
1404
- if (*shape_iter == *buffer_shape_iter)
1405
- *strides_iter = *buffer_strides_iter;
1406
- else
1407
- *strides_iter = 0;
1408
-
1409
- ++buffer_shape_iter;
1410
- ++buffer_strides_iter;
1411
- ++shape_iter;
1412
- ++strides_iter;
1413
- }
1414
-
1415
- std::fill(strides_iter, strides.rend(), 0);
1416
- iterator = common_iter(buffer.ptr, strides, shape);
1417
- }
1418
-
1419
- void increment_common_iterator(size_t dim) {
1420
- for (auto &iter : m_common_iterator)
1421
- iter.increment(dim);
1422
- }
1423
-
1424
- container_type m_shape;
1425
- container_type m_index;
1426
- std::array<common_iter, N> m_common_iterator;
1427
- };
1428
-
1429
- enum class broadcast_trivial { non_trivial, c_trivial, f_trivial };
1430
-
1431
- // Populates the shape and number of dimensions for the set of buffers. Returns a broadcast_trivial
1432
- // enum value indicating whether the broadcast is "trivial"--that is, has each buffer being either a
1433
- // singleton or a full-size, C-contiguous (`c_trivial`) or Fortran-contiguous (`f_trivial`) storage
1434
- // buffer; returns `non_trivial` otherwise.
1435
- template <size_t N>
1436
- broadcast_trivial broadcast(const std::array<buffer_info, N> &buffers, ssize_t &ndim, std::vector<ssize_t> &shape) {
1437
- ndim = std::accumulate(buffers.begin(), buffers.end(), ssize_t(0), [](ssize_t res, const buffer_info &buf) {
1438
- return std::max(res, buf.ndim);
1439
- });
1440
-
1441
- shape.clear();
1442
- shape.resize((size_t) ndim, 1);
1443
-
1444
- // Figure out the output size, and make sure all input arrays conform (i.e. are either size 1 or
1445
- // the full size).
1446
- for (size_t i = 0; i < N; ++i) {
1447
- auto res_iter = shape.rbegin();
1448
- auto end = buffers[i].shape.rend();
1449
- for (auto shape_iter = buffers[i].shape.rbegin(); shape_iter != end; ++shape_iter, ++res_iter) {
1450
- const auto &dim_size_in = *shape_iter;
1451
- auto &dim_size_out = *res_iter;
1452
-
1453
- // Each input dimension can either be 1 or `n`, but `n` values must match across buffers
1454
- if (dim_size_out == 1)
1455
- dim_size_out = dim_size_in;
1456
- else if (dim_size_in != 1 && dim_size_in != dim_size_out)
1457
- pybind11_fail("pybind11::vectorize: incompatible size/dimension of inputs!");
1458
- }
1459
- }
1460
-
1461
- bool trivial_broadcast_c = true;
1462
- bool trivial_broadcast_f = true;
1463
- for (size_t i = 0; i < N && (trivial_broadcast_c || trivial_broadcast_f); ++i) {
1464
- if (buffers[i].size == 1)
1465
- continue;
1466
-
1467
- // Require the same number of dimensions:
1468
- if (buffers[i].ndim != ndim)
1469
- return broadcast_trivial::non_trivial;
1470
-
1471
- // Require all dimensions be full-size:
1472
- if (!std::equal(buffers[i].shape.cbegin(), buffers[i].shape.cend(), shape.cbegin()))
1473
- return broadcast_trivial::non_trivial;
1474
-
1475
- // Check for C contiguity (but only if previous inputs were also C contiguous)
1476
- if (trivial_broadcast_c) {
1477
- ssize_t expect_stride = buffers[i].itemsize;
1478
- auto end = buffers[i].shape.crend();
1479
- for (auto shape_iter = buffers[i].shape.crbegin(), stride_iter = buffers[i].strides.crbegin();
1480
- trivial_broadcast_c && shape_iter != end; ++shape_iter, ++stride_iter) {
1481
- if (expect_stride == *stride_iter)
1482
- expect_stride *= *shape_iter;
1483
- else
1484
- trivial_broadcast_c = false;
1485
- }
1486
- }
1487
-
1488
- // Check for Fortran contiguity (if previous inputs were also F contiguous)
1489
- if (trivial_broadcast_f) {
1490
- ssize_t expect_stride = buffers[i].itemsize;
1491
- auto end = buffers[i].shape.cend();
1492
- for (auto shape_iter = buffers[i].shape.cbegin(), stride_iter = buffers[i].strides.cbegin();
1493
- trivial_broadcast_f && shape_iter != end; ++shape_iter, ++stride_iter) {
1494
- if (expect_stride == *stride_iter)
1495
- expect_stride *= *shape_iter;
1496
- else
1497
- trivial_broadcast_f = false;
1498
- }
1499
- }
1500
- }
1501
-
1502
- return
1503
- trivial_broadcast_c ? broadcast_trivial::c_trivial :
1504
- trivial_broadcast_f ? broadcast_trivial::f_trivial :
1505
- broadcast_trivial::non_trivial;
1506
- }
1507
-
1508
- template <typename T>
1509
- struct vectorize_arg {
1510
- static_assert(!std::is_rvalue_reference<T>::value, "Functions with rvalue reference arguments cannot be vectorized");
1511
- // The wrapped function gets called with this type:
1512
- using call_type = remove_reference_t<T>;
1513
- // Is this a vectorized argument?
1514
- static constexpr bool vectorize =
1515
- satisfies_any_of<call_type, std::is_arithmetic, is_complex, is_pod>::value &&
1516
- satisfies_none_of<call_type, std::is_pointer, std::is_array, is_std_array, std::is_enum>::value &&
1517
- (!std::is_reference<T>::value ||
1518
- (std::is_lvalue_reference<T>::value && std::is_const<call_type>::value));
1519
- // Accept this type: an array for vectorized types, otherwise the type as-is:
1520
- using type = conditional_t<vectorize, array_t<remove_cv_t<call_type>, array::forcecast>, T>;
1521
- };
1522
-
1523
-
1524
- // py::vectorize when a return type is present
1525
- template <typename Func, typename Return, typename... Args>
1526
- struct vectorize_returned_array {
1527
- using Type = array_t<Return>;
1528
-
1529
- static Type create(broadcast_trivial trivial, const std::vector<ssize_t> &shape) {
1530
- if (trivial == broadcast_trivial::f_trivial)
1531
- return array_t<Return, array::f_style>(shape);
1532
- return array_t<Return>(shape);
1533
- }
1534
-
1535
- static Return *mutable_data(Type &array) {
1536
- return array.mutable_data();
1537
- }
1538
-
1539
- static Return call(Func &f, Args &... args) {
1540
- return f(args...);
1541
- }
1542
-
1543
- static void call(Return *out, size_t i, Func &f, Args &... args) {
1544
- out[i] = f(args...);
1545
- }
1546
- };
1547
-
1548
- // py::vectorize when a return type is not present
1549
- template <typename Func, typename... Args>
1550
- struct vectorize_returned_array<Func, void, Args...> {
1551
- using Type = none;
1552
-
1553
- static Type create(broadcast_trivial, const std::vector<ssize_t> &) {
1554
- return none();
1555
- }
1556
-
1557
- static void *mutable_data(Type &) {
1558
- return nullptr;
1559
- }
1560
-
1561
- static detail::void_type call(Func &f, Args &... args) {
1562
- f(args...);
1563
- return {};
1564
- }
1565
-
1566
- static void call(void *, size_t, Func &f, Args &... args) {
1567
- f(args...);
1568
- }
1569
- };
1570
-
1571
-
1572
- template <typename Func, typename Return, typename... Args>
1573
- struct vectorize_helper {
1574
-
1575
- // NVCC for some reason breaks if NVectorized is private
1576
- #ifdef __CUDACC__
1577
- public:
1578
- #else
1579
- private:
1580
- #endif
1581
-
1582
- static constexpr size_t N = sizeof...(Args);
1583
- static constexpr size_t NVectorized = constexpr_sum(vectorize_arg<Args>::vectorize...);
1584
- static_assert(NVectorized >= 1,
1585
- "pybind11::vectorize(...) requires a function with at least one vectorizable argument");
1586
-
1587
- public:
1588
- template <typename T,
1589
- // SFINAE to prevent shadowing the copy constructor.
1590
- typename = detail::enable_if_t<
1591
- !std::is_same<vectorize_helper, typename std::decay<T>::type>::value>>
1592
- explicit vectorize_helper(T &&f) : f(std::forward<T>(f)) {}
1593
-
1594
- object operator()(typename vectorize_arg<Args>::type... args) {
1595
- return run(args...,
1596
- make_index_sequence<N>(),
1597
- select_indices<vectorize_arg<Args>::vectorize...>(),
1598
- make_index_sequence<NVectorized>());
1599
- }
1600
-
1601
- private:
1602
- remove_reference_t<Func> f;
1603
-
1604
- // Internal compiler error in MSVC 19.16.27025.1 (Visual Studio 2017 15.9.4), when compiling with "/permissive-" flag
1605
- // when arg_call_types is manually inlined.
1606
- using arg_call_types = std::tuple<typename vectorize_arg<Args>::call_type...>;
1607
- template <size_t Index> using param_n_t = typename std::tuple_element<Index, arg_call_types>::type;
1608
-
1609
- using returned_array = vectorize_returned_array<Func, Return, Args...>;
1610
-
1611
- // Runs a vectorized function given arguments tuple and three index sequences:
1612
- // - Index is the full set of 0 ... (N-1) argument indices;
1613
- // - VIndex is the subset of argument indices with vectorized parameters, letting us access
1614
- // vectorized arguments (anything not in this sequence is passed through)
1615
- // - BIndex is a incremental sequence (beginning at 0) of the same size as VIndex, so that
1616
- // we can store vectorized buffer_infos in an array (argument VIndex has its buffer at
1617
- // index BIndex in the array).
1618
- template <size_t... Index, size_t... VIndex, size_t... BIndex> object run(
1619
- typename vectorize_arg<Args>::type &...args,
1620
- index_sequence<Index...> i_seq, index_sequence<VIndex...> vi_seq, index_sequence<BIndex...> bi_seq) {
1621
-
1622
- // Pointers to values the function was called with; the vectorized ones set here will start
1623
- // out as array_t<T> pointers, but they will be changed them to T pointers before we make
1624
- // call the wrapped function. Non-vectorized pointers are left as-is.
1625
- std::array<void *, N> params{{ &args... }};
1626
-
1627
- // The array of `buffer_info`s of vectorized arguments:
1628
- std::array<buffer_info, NVectorized> buffers{{ reinterpret_cast<array *>(params[VIndex])->request()... }};
1629
-
1630
- /* Determine dimensions parameters of output array */
1631
- ssize_t nd = 0;
1632
- std::vector<ssize_t> shape(0);
1633
- auto trivial = broadcast(buffers, nd, shape);
1634
- auto ndim = (size_t) nd;
1635
-
1636
- size_t size = std::accumulate(shape.begin(), shape.end(), (size_t) 1, std::multiplies<size_t>());
1637
-
1638
- // If all arguments are 0-dimension arrays (i.e. single values) return a plain value (i.e.
1639
- // not wrapped in an array).
1640
- if (size == 1 && ndim == 0) {
1641
- PYBIND11_EXPAND_SIDE_EFFECTS(params[VIndex] = buffers[BIndex].ptr);
1642
- return cast(returned_array::call(f, *reinterpret_cast<param_n_t<Index> *>(params[Index])...));
1643
- }
1644
-
1645
- auto result = returned_array::create(trivial, shape);
1646
-
1647
- if (size == 0) return std::move(result);
1648
-
1649
- /* Call the function */
1650
- auto mutable_data = returned_array::mutable_data(result);
1651
- if (trivial == broadcast_trivial::non_trivial)
1652
- apply_broadcast(buffers, params, mutable_data, size, shape, i_seq, vi_seq, bi_seq);
1653
- else
1654
- apply_trivial(buffers, params, mutable_data, size, i_seq, vi_seq, bi_seq);
1655
-
1656
- return std::move(result);
1657
- }
1658
-
1659
- template <size_t... Index, size_t... VIndex, size_t... BIndex>
1660
- void apply_trivial(std::array<buffer_info, NVectorized> &buffers,
1661
- std::array<void *, N> &params,
1662
- Return *out,
1663
- size_t size,
1664
- index_sequence<Index...>, index_sequence<VIndex...>, index_sequence<BIndex...>) {
1665
-
1666
- // Initialize an array of mutable byte references and sizes with references set to the
1667
- // appropriate pointer in `params`; as we iterate, we'll increment each pointer by its size
1668
- // (except for singletons, which get an increment of 0).
1669
- std::array<std::pair<unsigned char *&, const size_t>, NVectorized> vecparams{{
1670
- std::pair<unsigned char *&, const size_t>(
1671
- reinterpret_cast<unsigned char *&>(params[VIndex] = buffers[BIndex].ptr),
1672
- buffers[BIndex].size == 1 ? 0 : sizeof(param_n_t<VIndex>)
1673
- )...
1674
- }};
1675
-
1676
- for (size_t i = 0; i < size; ++i) {
1677
- returned_array::call(out, i, f, *reinterpret_cast<param_n_t<Index> *>(params[Index])...);
1678
- for (auto &x : vecparams) x.first += x.second;
1679
- }
1680
- }
1681
-
1682
- template <size_t... Index, size_t... VIndex, size_t... BIndex>
1683
- void apply_broadcast(std::array<buffer_info, NVectorized> &buffers,
1684
- std::array<void *, N> &params,
1685
- Return *out,
1686
- size_t size,
1687
- const std::vector<ssize_t> &output_shape,
1688
- index_sequence<Index...>, index_sequence<VIndex...>, index_sequence<BIndex...>) {
1689
-
1690
- multi_array_iterator<NVectorized> input_iter(buffers, output_shape);
1691
-
1692
- for (size_t i = 0; i < size; ++i, ++input_iter) {
1693
- PYBIND11_EXPAND_SIDE_EFFECTS((
1694
- params[VIndex] = input_iter.template data<BIndex>()
1695
- ));
1696
- returned_array::call(out, i, f, *reinterpret_cast<param_n_t<Index> *>(std::get<Index>(params))...);
1697
- }
1698
- }
1699
- };
1700
-
1701
- template <typename Func, typename Return, typename... Args>
1702
- vectorize_helper<Func, Return, Args...>
1703
- vectorize_extractor(const Func &f, Return (*) (Args ...)) {
1704
- return detail::vectorize_helper<Func, Return, Args...>(f);
1705
- }
1706
-
1707
- template <typename T, int Flags> struct handle_type_name<array_t<T, Flags>> {
1708
- static constexpr auto name = _("numpy.ndarray[") + npy_format_descriptor<T>::name + _("]");
1709
- };
1710
-
1711
- PYBIND11_NAMESPACE_END(detail)
1712
-
1713
- // Vanilla pointer vectorizer:
1714
- template <typename Return, typename... Args>
1715
- detail::vectorize_helper<Return (*)(Args...), Return, Args...>
1716
- vectorize(Return (*f) (Args ...)) {
1717
- return detail::vectorize_helper<Return (*)(Args...), Return, Args...>(f);
1718
- }
1719
-
1720
- // lambda vectorizer:
1721
- template <typename Func, detail::enable_if_t<detail::is_lambda<Func>::value, int> = 0>
1722
- auto vectorize(Func &&f) -> decltype(
1723
- detail::vectorize_extractor(std::forward<Func>(f), (detail::function_signature_t<Func> *) nullptr)) {
1724
- return detail::vectorize_extractor(std::forward<Func>(f), (detail::function_signature_t<Func> *) nullptr);
1725
- }
1726
-
1727
- // Vectorize a class method (non-const):
1728
- template <typename Return, typename Class, typename... Args,
1729
- typename Helper = detail::vectorize_helper<decltype(std::mem_fn(std::declval<Return (Class::*)(Args...)>())), Return, Class *, Args...>>
1730
- Helper vectorize(Return (Class::*f)(Args...)) {
1731
- return Helper(std::mem_fn(f));
1732
- }
1733
-
1734
- // Vectorize a class method (const):
1735
- template <typename Return, typename Class, typename... Args,
1736
- typename Helper = detail::vectorize_helper<decltype(std::mem_fn(std::declval<Return (Class::*)(Args...) const>())), Return, const Class *, Args...>>
1737
- Helper vectorize(Return (Class::*f)(Args...) const) {
1738
- return Helper(std::mem_fn(f));
1739
- }
1740
-
1741
- PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)