xcoll 0.3.6__py3-none-any.whl → 0.5.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 (350) hide show
  1. xcoll/__init__.py +13 -4
  2. xcoll/beam_elements/__init__.py +14 -6
  3. xcoll/beam_elements/absorber.py +41 -7
  4. xcoll/beam_elements/base.py +1202 -247
  5. xcoll/beam_elements/blowup.py +198 -0
  6. xcoll/beam_elements/elements_src/black_absorber.h +136 -0
  7. xcoll/beam_elements/elements_src/black_crystal.h +129 -0
  8. xcoll/beam_elements/elements_src/blowup.h +42 -0
  9. xcoll/beam_elements/elements_src/emittance_monitor.h +109 -0
  10. xcoll/beam_elements/{collimators_src → elements_src}/everest_block.h +59 -30
  11. xcoll/beam_elements/elements_src/everest_collimator.h +237 -0
  12. xcoll/beam_elements/elements_src/everest_crystal.h +280 -0
  13. xcoll/beam_elements/everest.py +65 -119
  14. xcoll/beam_elements/monitor.py +428 -0
  15. xcoll/colldb.py +276 -747
  16. xcoll/general.py +5 -5
  17. xcoll/headers/checks.h +1 -1
  18. xcoll/headers/particle_states.h +2 -2
  19. xcoll/initial_distribution.py +207 -0
  20. xcoll/install.py +179 -0
  21. xcoll/interaction_record/__init__.py +1 -0
  22. xcoll/interaction_record/interaction_record.py +298 -0
  23. xcoll/interaction_record/interaction_record_src/interaction_record.h +98 -0
  24. xcoll/{impacts → interaction_record}/interaction_types.py +11 -4
  25. xcoll/line_tools.py +82 -0
  26. xcoll/lossmap.py +219 -0
  27. xcoll/manager.py +2 -937
  28. xcoll/rf_sweep.py +1 -1
  29. xcoll/scattering_routines/everest/amorphous.h +232 -0
  30. xcoll/scattering_routines/everest/channeling.h +240 -0
  31. xcoll/scattering_routines/everest/crystal_parameters.h +137 -0
  32. xcoll/scattering_routines/everest/everest.h +11 -30
  33. xcoll/scattering_routines/everest/everest.py +13 -10
  34. xcoll/scattering_routines/everest/jaw.h +28 -197
  35. xcoll/scattering_routines/everest/materials.py +37 -15
  36. xcoll/scattering_routines/everest/multiple_coulomb_scattering.h +31 -10
  37. xcoll/scattering_routines/everest/nuclear_interaction.h +86 -0
  38. xcoll/scattering_routines/everest/properties.h +6 -1
  39. xcoll/scattering_routines/fluka/flukaio/lib/libFlukaIO64.a +0 -0
  40. xcoll/scattering_routines/geant4/collimasim/.git +1 -0
  41. xcoll/scattering_routines/geant4/collimasim/.gitignore +12 -0
  42. xcoll/scattering_routines/geant4/collimasim/.gitmodules +3 -0
  43. xcoll/scattering_routines/geant4/collimasim/CMakeLists.txt +26 -0
  44. xcoll/scattering_routines/geant4/collimasim/README.md +21 -0
  45. xcoll/scattering_routines/geant4/collimasim/docs/Makefile +20 -0
  46. xcoll/scattering_routines/geant4/collimasim/docs/make.bat +35 -0
  47. xcoll/scattering_routines/geant4/collimasim/docs/source/collimasim.rst +10 -0
  48. xcoll/scattering_routines/geant4/collimasim/docs/source/conf.py +59 -0
  49. xcoll/scattering_routines/geant4/collimasim/docs/source/index.rst +26 -0
  50. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.appveyor.yml +37 -0
  51. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.clang-format +19 -0
  52. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.clang-tidy +65 -0
  53. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.cmake-format.yaml +73 -0
  54. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.git +1 -0
  55. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/CODEOWNERS +9 -0
  56. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/CONTRIBUTING.md +386 -0
  57. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/ISSUE_TEMPLATE/bug-report.yml +45 -0
  58. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/ISSUE_TEMPLATE/config.yml +8 -0
  59. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/dependabot.yml +16 -0
  60. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/labeler.yml +8 -0
  61. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/labeler_merged.yml +3 -0
  62. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/pull_request_template.md +19 -0
  63. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/ci.yml +969 -0
  64. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/configure.yml +84 -0
  65. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/format.yml +48 -0
  66. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/labeler.yml +16 -0
  67. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/pip.yml +103 -0
  68. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.gitignore +45 -0
  69. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.pre-commit-config.yaml +151 -0
  70. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.readthedocs.yml +3 -0
  71. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/CMakeLists.txt +297 -0
  72. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/LICENSE +29 -0
  73. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/MANIFEST.in +6 -0
  74. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/README.rst +180 -0
  75. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/Doxyfile +23 -0
  76. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/Makefile +192 -0
  77. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/_static/theme_overrides.css +11 -0
  78. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/chrono.rst +81 -0
  79. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/custom.rst +93 -0
  80. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/eigen.rst +310 -0
  81. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/functional.rst +109 -0
  82. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/index.rst +43 -0
  83. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/overview.rst +171 -0
  84. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/stl.rst +251 -0
  85. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/strings.rst +305 -0
  86. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/classes.rst +1297 -0
  87. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/embedding.rst +262 -0
  88. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/exceptions.rst +396 -0
  89. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/functions.rst +568 -0
  90. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/misc.rst +337 -0
  91. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/index.rst +13 -0
  92. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/numpy.rst +463 -0
  93. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/object.rst +286 -0
  94. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/utilities.rst +155 -0
  95. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/smart_ptrs.rst +174 -0
  96. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/basics.rst +308 -0
  97. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/benchmark.py +91 -0
  98. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/benchmark.rst +95 -0
  99. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/changelog.rst +2050 -0
  100. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/classes.rst +542 -0
  101. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/cmake/index.rst +8 -0
  102. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/compiling.rst +648 -0
  103. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/conf.py +381 -0
  104. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/faq.rst +343 -0
  105. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/index.rst +48 -0
  106. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/installing.rst +105 -0
  107. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/limitations.rst +72 -0
  108. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11-logo.png +0 -0
  109. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python1.png +0 -0
  110. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python1.svg +427 -0
  111. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python2.png +0 -0
  112. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python2.svg +427 -0
  113. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/reference.rst +130 -0
  114. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/release.rst +96 -0
  115. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/requirements.txt +8 -0
  116. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/upgrade.rst +548 -0
  117. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/attr.h +605 -0
  118. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/buffer_info.h +144 -0
  119. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/cast.h +1432 -0
  120. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/chrono.h +213 -0
  121. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/common.h +2 -0
  122. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/complex.h +65 -0
  123. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/class.h +709 -0
  124. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/common.h +1021 -0
  125. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/descr.h +104 -0
  126. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/init.h +346 -0
  127. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/internals.h +467 -0
  128. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/type_caster_base.h +978 -0
  129. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/typeid.h +55 -0
  130. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/eigen.h +606 -0
  131. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/embed.h +284 -0
  132. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/eval.h +163 -0
  133. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/functional.h +121 -0
  134. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/gil.h +193 -0
  135. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/iostream.h +275 -0
  136. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/numpy.h +1741 -0
  137. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/operators.h +163 -0
  138. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/options.h +65 -0
  139. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/pybind11.h +2497 -0
  140. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/pytypes.h +1879 -0
  141. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl/filesystem.h +103 -0
  142. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl.h +375 -0
  143. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl_bind.h +747 -0
  144. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/noxfile.py +88 -0
  145. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/__init__.py +11 -0
  146. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/__main__.py +52 -0
  147. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/_version.py +12 -0
  148. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/_version.pyi +6 -0
  149. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/commands.py +21 -0
  150. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/py.typed +0 -0
  151. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/setup_helpers.py +482 -0
  152. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/setup_helpers.pyi +63 -0
  153. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pyproject.toml +41 -0
  154. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/setup.cfg +56 -0
  155. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/setup.py +155 -0
  156. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/CMakeLists.txt +503 -0
  157. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/conftest.py +208 -0
  158. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/constructor_stats.h +275 -0
  159. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/cross_module_gil_utils.cpp +73 -0
  160. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/env.py +33 -0
  161. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_python_package/pytest.ini +0 -0
  162. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_python_package/test_files.py +279 -0
  163. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_setuptools/pytest.ini +0 -0
  164. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_setuptools/test_setuphelper.py +143 -0
  165. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/local_bindings.h +85 -0
  166. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/object.h +179 -0
  167. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_cross_module_tests.cpp +151 -0
  168. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_tests.cpp +91 -0
  169. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_tests.h +85 -0
  170. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pytest.ini +19 -0
  171. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/requirements.txt +12 -0
  172. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_async.cpp +26 -0
  173. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_async.py +25 -0
  174. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_buffers.cpp +216 -0
  175. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_buffers.py +163 -0
  176. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_builtin_casters.cpp +286 -0
  177. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_builtin_casters.py +536 -0
  178. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_call_policies.cpp +107 -0
  179. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_call_policies.py +248 -0
  180. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_callbacks.cpp +227 -0
  181. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_callbacks.py +202 -0
  182. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_chrono.cpp +84 -0
  183. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_chrono.py +210 -0
  184. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_class.cpp +550 -0
  185. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_class.py +473 -0
  186. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/CMakeLists.txt +84 -0
  187. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/embed.cpp +21 -0
  188. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt +28 -0
  189. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_function/CMakeLists.txt +39 -0
  190. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_target/CMakeLists.txt +46 -0
  191. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/main.cpp +6 -0
  192. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt +41 -0
  193. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_function/CMakeLists.txt +35 -0
  194. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_target/CMakeLists.txt +41 -0
  195. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/test.py +10 -0
  196. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_constants_and_functions.cpp +165 -0
  197. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_constants_and_functions.py +53 -0
  198. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_copy_move.cpp +238 -0
  199. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_copy_move.py +126 -0
  200. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_casters.cpp +141 -0
  201. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_casters.py +117 -0
  202. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_setup.cpp +41 -0
  203. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_setup.py +50 -0
  204. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_docstring_options.cpp +69 -0
  205. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_docstring_options.py +42 -0
  206. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eigen.cpp +348 -0
  207. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eigen.py +771 -0
  208. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/CMakeLists.txt +47 -0
  209. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/catch.cpp +22 -0
  210. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/external_module.cpp +23 -0
  211. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/test_interpreter.cpp +326 -0
  212. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/test_interpreter.py +15 -0
  213. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_enum.cpp +148 -0
  214. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_enum.py +272 -0
  215. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval.cpp +119 -0
  216. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval.py +51 -0
  217. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval_call.py +5 -0
  218. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.cpp +285 -0
  219. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.h +12 -0
  220. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.py +265 -0
  221. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_factory_constructors.cpp +397 -0
  222. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_factory_constructors.py +520 -0
  223. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_gil_scoped.cpp +49 -0
  224. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_gil_scoped.py +94 -0
  225. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_iostream.cpp +125 -0
  226. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_iostream.py +331 -0
  227. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_kwargs_and_defaults.cpp +153 -0
  228. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_kwargs_and_defaults.py +284 -0
  229. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_local_bindings.cpp +107 -0
  230. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_local_bindings.py +257 -0
  231. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_methods_and_attributes.cpp +412 -0
  232. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_methods_and_attributes.py +517 -0
  233. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_modules.cpp +102 -0
  234. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_modules.py +92 -0
  235. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_multiple_inheritance.cpp +233 -0
  236. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_multiple_inheritance.py +360 -0
  237. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_array.cpp +472 -0
  238. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_array.py +593 -0
  239. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_dtypes.cpp +524 -0
  240. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_dtypes.py +441 -0
  241. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_vectorize.cpp +103 -0
  242. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_vectorize.py +267 -0
  243. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_opaque_types.cpp +73 -0
  244. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_opaque_types.py +59 -0
  245. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_operator_overloading.cpp +235 -0
  246. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_operator_overloading.py +146 -0
  247. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pickling.cpp +189 -0
  248. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pickling.py +82 -0
  249. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pytypes.cpp +560 -0
  250. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pytypes.py +651 -0
  251. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_sequences_and_iterators.cpp +500 -0
  252. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_sequences_and_iterators.py +253 -0
  253. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_smart_ptr.cpp +452 -0
  254. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_smart_ptr.py +318 -0
  255. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl.cpp +342 -0
  256. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl.py +291 -0
  257. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl_binders.cpp +131 -0
  258. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl_binders.py +318 -0
  259. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_tagbased_polymorphic.cpp +144 -0
  260. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_tagbased_polymorphic.py +29 -0
  261. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_thread.cpp +66 -0
  262. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_thread.py +44 -0
  263. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_union.cpp +22 -0
  264. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_union.py +9 -0
  265. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_virtual_functions.cpp +510 -0
  266. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_virtual_functions.py +408 -0
  267. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/valgrind-numpy-scipy.supp +140 -0
  268. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/valgrind-python.supp +117 -0
  269. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindCatch.cmake +70 -0
  270. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindEigen3.cmake +86 -0
  271. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindPythonLibsNew.cmake +257 -0
  272. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/check-style.sh +44 -0
  273. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/cmake_uninstall.cmake.in +23 -0
  274. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/libsize.py +39 -0
  275. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/make_changelog.py +64 -0
  276. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Common.cmake +402 -0
  277. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Config.cmake.in +233 -0
  278. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11NewTools.cmake +276 -0
  279. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Tools.cmake +214 -0
  280. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pyproject.toml +3 -0
  281. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/setup_global.py.in +65 -0
  282. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/setup_main.py.in +41 -0
  283. xcoll/scattering_routines/geant4/collimasim/pyproject.toml +8 -0
  284. xcoll/scattering_routines/geant4/collimasim/setup.py +144 -0
  285. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSPyATInterface.cpp +403 -0
  286. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSPyATInterface.hh +100 -0
  287. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSXtrackInterface.cpp +763 -0
  288. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSXtrackInterface.hh +118 -0
  289. xcoll/scattering_routines/geant4/collimasim/src/collimasim/__init__.py +8 -0
  290. xcoll/scattering_routines/geant4/collimasim/src/collimasim/bindings.cpp +63 -0
  291. xcoll/scattering_routines/geant4/collimasim/src/collimasim/pyCollimatorPass.py +142 -0
  292. xcoll/scattering_routines/geant4/collimasim/src/collimasim/xtrack_collimator.py +556 -0
  293. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/PKG-INFO +6 -0
  294. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/SOURCES.txt +24 -0
  295. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/dependency_links.txt +1 -0
  296. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/not-zip-safe +1 -0
  297. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/top_level.txt +1 -0
  298. xcoll/scattering_routines/geant4/collimasim/tests/README.md +25 -0
  299. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_forions.dat +25 -0
  300. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_new_example.dat +18 -0
  301. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_old_example.dat +68 -0
  302. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_testing.dat +15 -0
  303. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_yaml_example.yaml +110 -0
  304. xcoll/scattering_routines/geant4/collimasim/tests/resources/collgaps.dat +7 -0
  305. xcoll/scattering_routines/geant4/collimasim/tests/resources/collgaps_pyat_test.dat +3 -0
  306. xcoll/scattering_routines/geant4/collimasim/tests/resources/collonly_twiss_file_example.tfs +54 -0
  307. xcoll/scattering_routines/geant4/collimasim/tests/resources/settings.gmad +3 -0
  308. xcoll/scattering_routines/geant4/collimasim/tests/resources/settings_black_absorber.gmad +3 -0
  309. xcoll/scattering_routines/geant4/collimasim/tests/resources/settings_ions.gmad +5 -0
  310. xcoll/scattering_routines/geant4/collimasim/tests/resources/twiss_file_testing.tfs +51 -0
  311. xcoll/scattering_routines/geant4/collimasim/tests/test_pyat.py +65 -0
  312. xcoll/scattering_routines/geant4/collimasim/tests/test_pyat_passmethod.py +59 -0
  313. xcoll/scattering_routines/geant4/collimasim/tests/test_pyat_tracking.py +102 -0
  314. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack.py +75 -0
  315. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_angle.py +74 -0
  316. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_colldb_load.py +84 -0
  317. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_interaction.py +159 -0
  318. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_interaction_ion.py +99 -0
  319. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_ions.py +78 -0
  320. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_lost_energy.py +88 -0
  321. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tilt.py +80 -0
  322. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tracking.py +97 -0
  323. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tracking_ions.py +96 -0
  324. xcoll/scattering_routines/geometry/__init__.py +6 -0
  325. xcoll/scattering_routines/geometry/collimator_geometry.h +218 -0
  326. xcoll/scattering_routines/geometry/crystal_geometry.h +153 -0
  327. xcoll/scattering_routines/geometry/geometry.py +26 -0
  328. xcoll/scattering_routines/geometry/get_s.h +92 -0
  329. xcoll/scattering_routines/geometry/methods.h +111 -0
  330. xcoll/scattering_routines/geometry/objects.h +154 -0
  331. xcoll/scattering_routines/geometry/rotation.h +23 -0
  332. xcoll/scattering_routines/geometry/segments.h +226 -0
  333. xcoll/scattering_routines/geometry/sort.h +184 -0
  334. {xcoll-0.3.6.dist-info → xcoll-0.5.0.dist-info}/METADATA +1 -1
  335. xcoll-0.5.0.dist-info/RECORD +413 -0
  336. xcoll/beam_elements/collimators_src/absorber.h +0 -141
  337. xcoll/beam_elements/collimators_src/everest_collimator.h +0 -142
  338. xcoll/beam_elements/collimators_src/everest_crystal.h +0 -115
  339. xcoll/collimator_settings.py +0 -457
  340. xcoll/impacts/__init__.py +0 -1
  341. xcoll/impacts/impacts.py +0 -102
  342. xcoll/impacts/impacts_src/impacts.h +0 -99
  343. xcoll/scattering_routines/everest/crystal.h +0 -1302
  344. xcoll/scattering_routines/everest/scatter.h +0 -169
  345. xcoll/scattering_routines/everest/scatter_crystal.h +0 -260
  346. xcoll/scattering_routines/fluka/build_fluka_input.py +0 -58
  347. xcoll-0.3.6.dist-info/RECORD +0 -111
  348. {xcoll-0.3.6.dist-info → xcoll-0.5.0.dist-info}/LICENSE +0 -0
  349. {xcoll-0.3.6.dist-info → xcoll-0.5.0.dist-info}/NOTICE +0 -0
  350. {xcoll-0.3.6.dist-info → xcoll-0.5.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1,463 @@
1
+ .. _numpy:
2
+
3
+ NumPy
4
+ #####
5
+
6
+ Buffer protocol
7
+ ===============
8
+
9
+ Python supports an extremely general and convenient approach for exchanging
10
+ data between plugin libraries. Types can expose a buffer view [#f2]_, which
11
+ provides fast direct access to the raw internal data representation. Suppose we
12
+ want to bind the following simplistic Matrix class:
13
+
14
+ .. code-block:: cpp
15
+
16
+ class Matrix {
17
+ public:
18
+ Matrix(size_t rows, size_t cols) : m_rows(rows), m_cols(cols) {
19
+ m_data = new float[rows*cols];
20
+ }
21
+ float *data() { return m_data; }
22
+ size_t rows() const { return m_rows; }
23
+ size_t cols() const { return m_cols; }
24
+ private:
25
+ size_t m_rows, m_cols;
26
+ float *m_data;
27
+ };
28
+
29
+ The following binding code exposes the ``Matrix`` contents as a buffer object,
30
+ making it possible to cast Matrices into NumPy arrays. It is even possible to
31
+ completely avoid copy operations with Python expressions like
32
+ ``np.array(matrix_instance, copy = False)``.
33
+
34
+ .. code-block:: cpp
35
+
36
+ py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
37
+ .def_buffer([](Matrix &m) -> py::buffer_info {
38
+ return py::buffer_info(
39
+ m.data(), /* Pointer to buffer */
40
+ sizeof(float), /* Size of one scalar */
41
+ py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
42
+ 2, /* Number of dimensions */
43
+ { m.rows(), m.cols() }, /* Buffer dimensions */
44
+ { sizeof(float) * m.cols(), /* Strides (in bytes) for each index */
45
+ sizeof(float) }
46
+ );
47
+ });
48
+
49
+ Supporting the buffer protocol in a new type involves specifying the special
50
+ ``py::buffer_protocol()`` tag in the ``py::class_`` constructor and calling the
51
+ ``def_buffer()`` method with a lambda function that creates a
52
+ ``py::buffer_info`` description record on demand describing a given matrix
53
+ instance. The contents of ``py::buffer_info`` mirror the Python buffer protocol
54
+ specification.
55
+
56
+ .. code-block:: cpp
57
+
58
+ struct buffer_info {
59
+ void *ptr;
60
+ py::ssize_t itemsize;
61
+ std::string format;
62
+ py::ssize_t ndim;
63
+ std::vector<py::ssize_t> shape;
64
+ std::vector<py::ssize_t> strides;
65
+ };
66
+
67
+ To create a C++ function that can take a Python buffer object as an argument,
68
+ simply use the type ``py::buffer`` as one of its arguments. Buffers can exist
69
+ in a great variety of configurations, hence some safety checks are usually
70
+ necessary in the function body. Below, you can see a basic example on how to
71
+ define a custom constructor for the Eigen double precision matrix
72
+ (``Eigen::MatrixXd``) type, which supports initialization from compatible
73
+ buffer objects (e.g. a NumPy matrix).
74
+
75
+ .. code-block:: cpp
76
+
77
+ /* Bind MatrixXd (or some other Eigen type) to Python */
78
+ typedef Eigen::MatrixXd Matrix;
79
+
80
+ typedef Matrix::Scalar Scalar;
81
+ constexpr bool rowMajor = Matrix::Flags & Eigen::RowMajorBit;
82
+
83
+ py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
84
+ .def(py::init([](py::buffer b) {
85
+ typedef Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> Strides;
86
+
87
+ /* Request a buffer descriptor from Python */
88
+ py::buffer_info info = b.request();
89
+
90
+ /* Some sanity checks ... */
91
+ if (info.format != py::format_descriptor<Scalar>::format())
92
+ throw std::runtime_error("Incompatible format: expected a double array!");
93
+
94
+ if (info.ndim != 2)
95
+ throw std::runtime_error("Incompatible buffer dimension!");
96
+
97
+ auto strides = Strides(
98
+ info.strides[rowMajor ? 0 : 1] / (py::ssize_t)sizeof(Scalar),
99
+ info.strides[rowMajor ? 1 : 0] / (py::ssize_t)sizeof(Scalar));
100
+
101
+ auto map = Eigen::Map<Matrix, 0, Strides>(
102
+ static_cast<Scalar *>(info.ptr), info.shape[0], info.shape[1], strides);
103
+
104
+ return Matrix(map);
105
+ }));
106
+
107
+ For reference, the ``def_buffer()`` call for this Eigen data type should look
108
+ as follows:
109
+
110
+ .. code-block:: cpp
111
+
112
+ .def_buffer([](Matrix &m) -> py::buffer_info {
113
+ return py::buffer_info(
114
+ m.data(), /* Pointer to buffer */
115
+ sizeof(Scalar), /* Size of one scalar */
116
+ py::format_descriptor<Scalar>::format(), /* Python struct-style format descriptor */
117
+ 2, /* Number of dimensions */
118
+ { m.rows(), m.cols() }, /* Buffer dimensions */
119
+ { sizeof(Scalar) * (rowMajor ? m.cols() : 1),
120
+ sizeof(Scalar) * (rowMajor ? 1 : m.rows()) }
121
+ /* Strides (in bytes) for each index */
122
+ );
123
+ })
124
+
125
+ For a much easier approach of binding Eigen types (although with some
126
+ limitations), refer to the section on :doc:`/advanced/cast/eigen`.
127
+
128
+ .. seealso::
129
+
130
+ The file :file:`tests/test_buffers.cpp` contains a complete example
131
+ that demonstrates using the buffer protocol with pybind11 in more detail.
132
+
133
+ .. [#f2] http://docs.python.org/3/c-api/buffer.html
134
+
135
+ Arrays
136
+ ======
137
+
138
+ By exchanging ``py::buffer`` with ``py::array`` in the above snippet, we can
139
+ restrict the function so that it only accepts NumPy arrays (rather than any
140
+ type of Python object satisfying the buffer protocol).
141
+
142
+ In many situations, we want to define a function which only accepts a NumPy
143
+ array of a certain data type. This is possible via the ``py::array_t<T>``
144
+ template. For instance, the following function requires the argument to be a
145
+ NumPy array containing double precision values.
146
+
147
+ .. code-block:: cpp
148
+
149
+ void f(py::array_t<double> array);
150
+
151
+ When it is invoked with a different type (e.g. an integer or a list of
152
+ integers), the binding code will attempt to cast the input into a NumPy array
153
+ of the requested type. This feature requires the :file:`pybind11/numpy.h`
154
+ header to be included. Note that :file:`pybind11/numpy.h` does not depend on
155
+ the NumPy headers, and thus can be used without declaring a build-time
156
+ dependency on NumPy; NumPy>=1.7.0 is a runtime dependency.
157
+
158
+ Data in NumPy arrays is not guaranteed to packed in a dense manner;
159
+ furthermore, entries can be separated by arbitrary column and row strides.
160
+ Sometimes, it can be useful to require a function to only accept dense arrays
161
+ using either the C (row-major) or Fortran (column-major) ordering. This can be
162
+ accomplished via a second template argument with values ``py::array::c_style``
163
+ or ``py::array::f_style``.
164
+
165
+ .. code-block:: cpp
166
+
167
+ void f(py::array_t<double, py::array::c_style | py::array::forcecast> array);
168
+
169
+ The ``py::array::forcecast`` argument is the default value of the second
170
+ template parameter, and it ensures that non-conforming arguments are converted
171
+ into an array satisfying the specified requirements instead of trying the next
172
+ function overload.
173
+
174
+ There are several methods on arrays; the methods listed below under references
175
+ work, as well as the following functions based on the NumPy API:
176
+
177
+ - ``.dtype()`` returns the type of the contained values.
178
+
179
+ - ``.strides()`` returns a pointer to the strides of the array (optionally pass
180
+ an integer axis to get a number).
181
+
182
+ - ``.flags()`` returns the flag settings. ``.writable()`` and ``.owndata()``
183
+ are directly available.
184
+
185
+ - ``.offset_at()`` returns the offset (optionally pass indices).
186
+
187
+ - ``.squeeze()`` returns a view with length-1 axes removed.
188
+
189
+ - ``.view(dtype)`` returns a view of the array with a different dtype.
190
+
191
+ - ``.reshape({i, j, ...})`` returns a view of the array with a different shape.
192
+ ``.resize({...})`` is also available.
193
+
194
+ - ``.index_at(i, j, ...)`` gets the count from the beginning to a given index.
195
+
196
+
197
+ There are also several methods for getting references (described below).
198
+
199
+ Structured types
200
+ ================
201
+
202
+ In order for ``py::array_t`` to work with structured (record) types, we first
203
+ need to register the memory layout of the type. This can be done via
204
+ ``PYBIND11_NUMPY_DTYPE`` macro, called in the plugin definition code, which
205
+ expects the type followed by field names:
206
+
207
+ .. code-block:: cpp
208
+
209
+ struct A {
210
+ int x;
211
+ double y;
212
+ };
213
+
214
+ struct B {
215
+ int z;
216
+ A a;
217
+ };
218
+
219
+ // ...
220
+ PYBIND11_MODULE(test, m) {
221
+ // ...
222
+
223
+ PYBIND11_NUMPY_DTYPE(A, x, y);
224
+ PYBIND11_NUMPY_DTYPE(B, z, a);
225
+ /* now both A and B can be used as template arguments to py::array_t */
226
+ }
227
+
228
+ The structure should consist of fundamental arithmetic types, ``std::complex``,
229
+ previously registered substructures, and arrays of any of the above. Both C++
230
+ arrays and ``std::array`` are supported. While there is a static assertion to
231
+ prevent many types of unsupported structures, it is still the user's
232
+ responsibility to use only "plain" structures that can be safely manipulated as
233
+ raw memory without violating invariants.
234
+
235
+ Vectorizing functions
236
+ =====================
237
+
238
+ Suppose we want to bind a function with the following signature to Python so
239
+ that it can process arbitrary NumPy array arguments (vectors, matrices, general
240
+ N-D arrays) in addition to its normal arguments:
241
+
242
+ .. code-block:: cpp
243
+
244
+ double my_func(int x, float y, double z);
245
+
246
+ After including the ``pybind11/numpy.h`` header, this is extremely simple:
247
+
248
+ .. code-block:: cpp
249
+
250
+ m.def("vectorized_func", py::vectorize(my_func));
251
+
252
+ Invoking the function like below causes 4 calls to be made to ``my_func`` with
253
+ each of the array elements. The significant advantage of this compared to
254
+ solutions like ``numpy.vectorize()`` is that the loop over the elements runs
255
+ entirely on the C++ side and can be crunched down into a tight, optimized loop
256
+ by the compiler. The result is returned as a NumPy array of type
257
+ ``numpy.dtype.float64``.
258
+
259
+ .. code-block:: pycon
260
+
261
+ >>> x = np.array([[1, 3], [5, 7]])
262
+ >>> y = np.array([[2, 4], [6, 8]])
263
+ >>> z = 3
264
+ >>> result = vectorized_func(x, y, z)
265
+
266
+ The scalar argument ``z`` is transparently replicated 4 times. The input
267
+ arrays ``x`` and ``y`` are automatically converted into the right types (they
268
+ are of type ``numpy.dtype.int64`` but need to be ``numpy.dtype.int32`` and
269
+ ``numpy.dtype.float32``, respectively).
270
+
271
+ .. note::
272
+
273
+ Only arithmetic, complex, and POD types passed by value or by ``const &``
274
+ reference are vectorized; all other arguments are passed through as-is.
275
+ Functions taking rvalue reference arguments cannot be vectorized.
276
+
277
+ In cases where the computation is too complicated to be reduced to
278
+ ``vectorize``, it will be necessary to create and access the buffer contents
279
+ manually. The following snippet contains a complete example that shows how this
280
+ works (the code is somewhat contrived, since it could have been done more
281
+ simply using ``vectorize``).
282
+
283
+ .. code-block:: cpp
284
+
285
+ #include <pybind11/pybind11.h>
286
+ #include <pybind11/numpy.h>
287
+
288
+ namespace py = pybind11;
289
+
290
+ py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
291
+ py::buffer_info buf1 = input1.request(), buf2 = input2.request();
292
+
293
+ if (buf1.ndim != 1 || buf2.ndim != 1)
294
+ throw std::runtime_error("Number of dimensions must be one");
295
+
296
+ if (buf1.size != buf2.size)
297
+ throw std::runtime_error("Input shapes must match");
298
+
299
+ /* No pointer is passed, so NumPy will allocate the buffer */
300
+ auto result = py::array_t<double>(buf1.size);
301
+
302
+ py::buffer_info buf3 = result.request();
303
+
304
+ double *ptr1 = static_cast<double *>(buf1.ptr);
305
+ double *ptr2 = static_cast<double *>(buf2.ptr);
306
+ double *ptr3 = static_cast<double *>(buf3.ptr);
307
+
308
+ for (size_t idx = 0; idx < buf1.shape[0]; idx++)
309
+ ptr3[idx] = ptr1[idx] + ptr2[idx];
310
+
311
+ return result;
312
+ }
313
+
314
+ PYBIND11_MODULE(test, m) {
315
+ m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
316
+ }
317
+
318
+ .. seealso::
319
+
320
+ The file :file:`tests/test_numpy_vectorize.cpp` contains a complete
321
+ example that demonstrates using :func:`vectorize` in more detail.
322
+
323
+ Direct access
324
+ =============
325
+
326
+ For performance reasons, particularly when dealing with very large arrays, it
327
+ is often desirable to directly access array elements without internal checking
328
+ of dimensions and bounds on every access when indices are known to be already
329
+ valid. To avoid such checks, the ``array`` class and ``array_t<T>`` template
330
+ class offer an unchecked proxy object that can be used for this unchecked
331
+ access through the ``unchecked<N>`` and ``mutable_unchecked<N>`` methods,
332
+ where ``N`` gives the required dimensionality of the array:
333
+
334
+ .. code-block:: cpp
335
+
336
+ m.def("sum_3d", [](py::array_t<double> x) {
337
+ auto r = x.unchecked<3>(); // x must have ndim = 3; can be non-writeable
338
+ double sum = 0;
339
+ for (py::ssize_t i = 0; i < r.shape(0); i++)
340
+ for (py::ssize_t j = 0; j < r.shape(1); j++)
341
+ for (py::ssize_t k = 0; k < r.shape(2); k++)
342
+ sum += r(i, j, k);
343
+ return sum;
344
+ });
345
+ m.def("increment_3d", [](py::array_t<double> x) {
346
+ auto r = x.mutable_unchecked<3>(); // Will throw if ndim != 3 or flags.writeable is false
347
+ for (py::ssize_t i = 0; i < r.shape(0); i++)
348
+ for (py::ssize_t j = 0; j < r.shape(1); j++)
349
+ for (py::ssize_t k = 0; k < r.shape(2); k++)
350
+ r(i, j, k) += 1.0;
351
+ }, py::arg().noconvert());
352
+
353
+ To obtain the proxy from an ``array`` object, you must specify both the data
354
+ type and number of dimensions as template arguments, such as ``auto r =
355
+ myarray.mutable_unchecked<float, 2>()``.
356
+
357
+ If the number of dimensions is not known at compile time, you can omit the
358
+ dimensions template parameter (i.e. calling ``arr_t.unchecked()`` or
359
+ ``arr.unchecked<T>()``. This will give you a proxy object that works in the
360
+ same way, but results in less optimizable code and thus a small efficiency
361
+ loss in tight loops.
362
+
363
+ Note that the returned proxy object directly references the array's data, and
364
+ only reads its shape, strides, and writeable flag when constructed. You must
365
+ take care to ensure that the referenced array is not destroyed or reshaped for
366
+ the duration of the returned object, typically by limiting the scope of the
367
+ returned instance.
368
+
369
+ The returned proxy object supports some of the same methods as ``py::array`` so
370
+ that it can be used as a drop-in replacement for some existing, index-checked
371
+ uses of ``py::array``:
372
+
373
+ - ``.ndim()`` returns the number of dimensions
374
+
375
+ - ``.data(1, 2, ...)`` and ``r.mutable_data(1, 2, ...)``` returns a pointer to
376
+ the ``const T`` or ``T`` data, respectively, at the given indices. The
377
+ latter is only available to proxies obtained via ``a.mutable_unchecked()``.
378
+
379
+ - ``.itemsize()`` returns the size of an item in bytes, i.e. ``sizeof(T)``.
380
+
381
+ - ``.ndim()`` returns the number of dimensions.
382
+
383
+ - ``.shape(n)`` returns the size of dimension ``n``
384
+
385
+ - ``.size()`` returns the total number of elements (i.e. the product of the shapes).
386
+
387
+ - ``.nbytes()`` returns the number of bytes used by the referenced elements
388
+ (i.e. ``itemsize()`` times ``size()``).
389
+
390
+ .. seealso::
391
+
392
+ The file :file:`tests/test_numpy_array.cpp` contains additional examples
393
+ demonstrating the use of this feature.
394
+
395
+ Ellipsis
396
+ ========
397
+
398
+ Python 3 provides a convenient ``...`` ellipsis notation that is often used to
399
+ slice multidimensional arrays. For instance, the following snippet extracts the
400
+ middle dimensions of a tensor with the first and last index set to zero.
401
+ In Python 2, the syntactic sugar ``...`` is not available, but the singleton
402
+ ``Ellipsis`` (of type ``ellipsis``) can still be used directly.
403
+
404
+ .. code-block:: python
405
+
406
+ a = ... # a NumPy array
407
+ b = a[0, ..., 0]
408
+
409
+ The function ``py::ellipsis()`` function can be used to perform the same
410
+ operation on the C++ side:
411
+
412
+ .. code-block:: cpp
413
+
414
+ py::array a = /* A NumPy array */;
415
+ py::array b = a[py::make_tuple(0, py::ellipsis(), 0)];
416
+
417
+ .. versionchanged:: 2.6
418
+ ``py::ellipsis()`` is now also available in Python 2.
419
+
420
+ Memory view
421
+ ===========
422
+
423
+ For a case when we simply want to provide a direct accessor to C/C++ buffer
424
+ without a concrete class object, we can return a ``memoryview`` object. Suppose
425
+ we wish to expose a ``memoryview`` for 2x4 uint8_t array, we can do the
426
+ following:
427
+
428
+ .. code-block:: cpp
429
+
430
+ const uint8_t buffer[] = {
431
+ 0, 1, 2, 3,
432
+ 4, 5, 6, 7
433
+ };
434
+ m.def("get_memoryview2d", []() {
435
+ return py::memoryview::from_buffer(
436
+ buffer, // buffer pointer
437
+ { 2, 4 }, // shape (rows, cols)
438
+ { sizeof(uint8_t) * 4, sizeof(uint8_t) } // strides in bytes
439
+ );
440
+ })
441
+
442
+ This approach is meant for providing a ``memoryview`` for a C/C++ buffer not
443
+ managed by Python. The user is responsible for managing the lifetime of the
444
+ buffer. Using a ``memoryview`` created in this way after deleting the buffer in
445
+ C++ side results in undefined behavior.
446
+
447
+ We can also use ``memoryview::from_memory`` for a simple 1D contiguous buffer:
448
+
449
+ .. code-block:: cpp
450
+
451
+ m.def("get_memoryview1d", []() {
452
+ return py::memoryview::from_memory(
453
+ buffer, // buffer pointer
454
+ sizeof(uint8_t) * 8 // buffer size
455
+ );
456
+ })
457
+
458
+ .. note::
459
+
460
+ ``memoryview::from_memory`` is not available in Python 2.
461
+
462
+ .. versionchanged:: 2.6
463
+ ``memoryview::from_memory`` added.