### Install cookiecutter Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/cookiecutter.rst Installs the cookiecutter tool, which is required to use the xtensor-python-cookiecutter template. ```bash pip install cookiecutter ``` -------------------------------- ### Basic CMake Build for C++ Module Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst Demonstrates a basic CMake configuration for building a C++ module with xtensor-python. It includes finding Python, pybind11, and NumPy, and setting up the build process. This example is crucial for understanding the initial setup. ```cmake cmake_minimum_required(VERSION 3.18..3.20) find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy) find_package(pybind11 REQUIRED CONFIG) ``` -------------------------------- ### Build and Install xtensor-python from Source with CMake (Unix) Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/installation.rst Builds and installs xtensor-python from source on Unix-like systems using cmake. It creates a build directory, configures the build with a specified installation prefix, and then installs the library. ```cmake mkdir build cd build cmake -DCMAKE_INSTALL_PREFIX=/path/to/prefix .. make install ``` -------------------------------- ### Build and Install xtensor-python from Source with CMake (Windows) Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/installation.rst Builds and installs xtensor-python from source on Windows systems using cmake with the NMake Makefiles generator. It creates a build directory, configures the build with a specified installation prefix, and then installs the library. ```cmake mkdir build cd build cmake -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX=/path/to/prefix .. nmake nmake install ``` -------------------------------- ### CMakeLists.txt for Basic Example Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst The CMakeLists.txt file for the basic readme example. It specifies build targets, dependencies, and compilation settings. ```cmake // Placeholder for CMakeLists.txt content // This would include find_package calls for Python, pybind11, xtensor-python // and define the C++ executable or library. ``` -------------------------------- ### CMakeLists.txt for SFINAE Example Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst The CMakeLists.txt file for the SFINAE example. It configures the build process, including dependencies on Python, pybind11, and xtensor-python, similar to the basic example but tailored for the SFINAE module. ```cmake cmake_minimum_required(VERSION 3.18..3.20) find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy) find_package(pybind11 REQUIRED CONFIG) # ... other build configurations for the SFINAE module ``` -------------------------------- ### Generate xtensor-python project Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/cookiecutter.rst Generates a new Python extension project using the xtensor-python-cookiecutter template from a GitHub repository. ```bash cookiecutter https://github.com/xtensor-stack/xtensor-python-cookiecutter.git ``` -------------------------------- ### C++ Code for Basic Example Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst The C++ source code for the basic readme example. This file defines the core functionality of the module being built. ```cpp // Placeholder for main.cpp content // This would typically include xtensor and pybind11 headers // and define the functions exposed to Python. ``` -------------------------------- ### Install xtensor-python with Debian Package Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/installation.rst Installs the xtensor-python development package on Debian-based systems using apt-get. ```bash sudo apt-get install xtensor-python-dev ``` -------------------------------- ### Python Example Usage (General) Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst A generic Python script example, likely used across different C++ module examples to test the compiled functionality. It demonstrates importing and interacting with the C++ code from Python. ```python # Placeholder for example.py content # This script is used to test the compiled C++ modules. ``` -------------------------------- ### Python Script for Basic Example Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst A Python script to test the compiled C++ module from the basic example. It demonstrates how to import and use the C++ functions within Python. ```python # Placeholder for example.py content # This would import the compiled module and call its functions. ``` -------------------------------- ### xtensor-python Overview Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/index.rst This section provides a high-level overview of xtensor and xtensor-python, highlighting their core functionalities and the benefits of using them together. It mentions the use of pybind11 for C++/Python interoperability and the availability of a cookiecutter template for project setup. ```markdown Python bindings for the xtensor_ C++ multi-dimensional array library. What are ``xtensor`` and ``xtensor-python``? - ``xtensor`` is a C++ library for multi-dimensional arrays enabling numpy-style broadcasting and lazy computing. - ``xtensor-python`` enables inplace use of numpy arrays with all the benefits from ``xtensor`` - C++ universal functions and broadcasting - STL - compliant APIs. The Python bindings for ``xtensor`` are based on the pybind11_ C++ library, which enables seemless interoperability between C++ and Python. ``` -------------------------------- ### C++ Source for SFINAE Example Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst The C++ source file (main.cpp) for the SFINAE example. This file contains the implementation of the templated function and its usage. ```cpp // Placeholder for main.cpp content in SFINAE example // This would include the mymodule.hpp and implement times_dimension. ``` -------------------------------- ### Installation Configuration Source: https://github.com/xtensor-stack/xtensor-python/blob/master/CMakeLists.txt Configures the installation of the xtensor-python targets using CMake's installation directives and package helpers. ```cmake # Installation # ============ include(GNUInstallDirs) include(CMakePackageConfigHelpers) install(TARGETS xtensor-python EXPORT ${PROJECT_NAME}-targets) ``` -------------------------------- ### Install xtensor-python with Conda Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/installation.rst Installs the xtensor-python package using the mamba (or conda) package manager from the conda-forge channel. ```bash mamba install -c conda-forge xtensor-python ``` -------------------------------- ### Install Breathe for Documentation Source: https://github.com/xtensor-stack/xtensor-python/blob/master/README.md Installs the Breathe Sphinx extension, which is used for building the HTML documentation of xtensor-python. It can be installed via pip or conda. ```bash pip install breathe ``` ```bash conda install -c conda-forge breathe ``` -------------------------------- ### CMakeLists.txt Configuration for xtensor-python Module Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples/readme_example_1/CMakeLists.txt This snippet shows the essential CMake commands to set up a Python extension module that utilizes xtensor-python. It includes finding required packages such as Python, pybind11, xtensor, and xtensor-python, and then linking these dependencies to the target module. It also demonstrates setting compile definitions. ```cmake cmake_minimum_required(VERSION 3.29) project(mymodule) find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy) find_package(pybind11 REQUIRED CONFIG) find_package(xtensor REQUIRED) find_package(xtensor-python REQUIRED) pybind11_add_module(mymodule main.cpp) target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python::NumPy) target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0) ``` -------------------------------- ### CMake Project Configuration for xtensor-python Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples/sfinae/CMakeLists.txt This snippet shows the basic CMake configuration for a project that uses xtensor-python. It includes setting the minimum CMake version, project name, finding required packages like Python, pybind11, xtensor, and xtensor-python, and linking them to a Python module and an executable. ```cmake cmake_minimum_required(VERSION 3.29) project(mymodule) find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy) find_package(pybind11 REQUIRED CONFIG) find_package(xtensor REQUIRED) find_package(xtensor-python REQUIRED) pybind11_add_module(mymodule python.cpp) target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python::NumPy) target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0) add_executable(myexec main.cpp) target_link_libraries(myexec PUBLIC xtensor) ``` -------------------------------- ### C++ Header for SFINAE Example Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst The C++ header file (mymodule.hpp) for the SFINAE example. It defines a templated function and uses SFINAE to restrict template arguments. ```cpp template void times_dimension(T& t); ``` -------------------------------- ### CMake Project Setup and Dependencies Source: https://github.com/xtensor-stack/xtensor-python/blob/master/test/CMakeLists.txt Configures the CMake project, finds necessary packages like pybind11, xtensor, and xtensor-python, and sets include directories. It also enforces a Release build type and C++17 standard. ```cmake cmake_minimum_required(VERSION 3.29) if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) project(xtensor-python-test) find_package(pybind11 REQUIRED) set(PYBIND11_INCLUDE_DIR ${pybind11_INCLUDE_DIRS}) find_package(xtensor REQUIRED CONFIG) set(XTENSOR_INCLUDE_DIR ${xtensor_INCLUDE_DIRS}) find_package(xtensor-python REQUIRED CONFIG) set(XTENSOR_PYTHON_INCLUDE_DIR ${xtensor-python_INCLUDE_DIRS}) endif () message(STATUS "Forcing tests build type to Release") set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) include(CheckCXXCompilerFlag) string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP /bigobj") set(CMAKE_EXE_LINKER_FLAGS /MANIFEST:NO) endif() ``` -------------------------------- ### Build Configuration and Library Setup Source: https://github.com/xtensor-stack/xtensor-python/blob/master/CMakeLists.txt Defines the xtensor-python library as an INTERFACE library and sets its include directories. It also configures options for building tests and downloading Google Test. ```cmake # Build # ===== set(XTENSOR_PYTHON_HEADERS ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pyarray.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pyarray_backstrides.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pycontainer.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pynative_casters.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pystrides_adaptor.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pytensor.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pyvectorize.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/xtensor_python_config.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/xtensor_type_caster_base.hpp ) add_library(xtensor-python INTERFACE) target_include_directories(xtensor-python INTERFACE "$" $) target_link_libraries(xtensor-python INTERFACE xtensor) get_target_property(inc_dir xtensor-python INTERFACE_INCLUDE_DIRECTORIES) OPTION(BUILD_TESTS "xtensor test suite" OFF) OPTION(DOWNLOAD_GTEST "build gtest from downloaded sources" OFF) if(DOWNLOAD_GTEST OR GTEST_SRC_DIR) set(BUILD_TESTS ON) endif() if(BUILD_TESTS) if(MSVC) set(PYTHON_MODULE_EXTENSION ".pyd") else() set(PYTHON_MODULE_EXTENSION ".so") endif() add_subdirectory(test) add_subdirectory(benchmark) endif() ``` -------------------------------- ### C++ Python Bindings for SFINAE Example Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst The C++ file (python.cpp) responsible for creating Python bindings for the SFINAE-enabled C++ module. It specifies which C++ functions and templates are exposed to Python. ```cpp // Placeholder for python.cpp content in SFINAE example // This would use pybind11 to expose the C++ functions, specifying template types like xt::pyarray. ``` -------------------------------- ### CMake Python Executable Configuration Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst Illustrates how to specify a particular Python executable for CMake when using the 'FindPython' module, which is crucial for ensuring the correct Python environment is used for building and linking. ```cmake cmake -Bbuild -DPython_EXECUTABLE=`which python` ``` -------------------------------- ### xtensor-python Cookiecutter Template Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/index.rst This entry highlights the availability of a cookiecutter template for xtensor-python, which simplifies the process of setting up a new C++ extension project with xtensor-python, including examples and unit tests. ```markdown Finally, a cookiecutter template project is provided. It takes care of the initial work of generating a project skeleton for a C++ extension based on ``xtensor-python`` containing a few examples, unit tests and HTML documentation. Find out more about the xtensor-python-cookiecutter_. ``` -------------------------------- ### CMake Python and pybind11 Find Order Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst Demonstrates the correct order for finding Python and pybind11 packages in CMake when using the 'FindPython' module to ensure compatibility, especially when pybind11 needs access to Python development headers and libraries. ```cmake find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy) find_package(pybind11 REQUIRED CONFIG) ``` -------------------------------- ### Install xtensor-python using Conda Source: https://github.com/xtensor-stack/xtensor-python/blob/master/README.md This command installs the xtensor-python library using the mamba package manager, which is recommended for its speed and efficiency. It installs the package from the conda-forge channel. ```bash mamba install -c conda-forge xtensor-python ``` -------------------------------- ### Install xtensor-python with Conda Source: https://github.com/xtensor-stack/xtensor-python/blob/master/README.md Installs the xtensor-python package and its dependencies (pybind11 and xtensor) using the conda package manager from the conda-forge channel. ```bash conda install -c conda-forge xtensor-python ``` -------------------------------- ### Multi-File Extension Module Setup Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/numpy_capi.rst Illustrates the setup for a multi-file extension module. FORCE_IMPORT_ARRAY should be defined only once in the main initialization file, while other files can include xtensor-python headers directly without this definition. ```cpp // image.hpp // Do NOT define FORCE_IMPORT_ARRAY here #include "xtensor-python/pyarray.hpp" class image { // .... private: xt::pyarray m_data; }; // image.cpp // Do NOT define FORCE_IMPORT_ARRAY here #include "image.hpp" // definition of the image class // main.cpp // FORCE_IMPORT_ARRAY must be define ONCE, BEFORE including // any header from xtensor-python (even indirectly) #define FORCE_IMPORT_ARRAY #include "image.hpp" PYBIND11_MODULE(plugin_name, m) { xt::import_numpy(); //... } ``` -------------------------------- ### CMake Export and Install Targets Source: https://github.com/xtensor-stack/xtensor-python/blob/master/CMakeLists.txt Exports targets for the xtensor-python project and installs them along with CMake configuration files. This ensures that the project can be found and used by other CMake projects. ```cmake export(EXPORT ${PROJECT_NAME}-targets FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake") install(FILES ${XTENSOR_PYTHON_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xtensor-python) configure_file(${PROJECT_NAME}.pc.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig/") set(XTENSOR_PYTHON_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" CACHE STRING "install path for xtensor-pythonConfig.cmake") configure_package_config_file(${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${XTENSOR_PYTHON_CMAKECONFIG_INSTALL_DIR}) # xtensor-python is header-only and does not depend on the architecture. # Remove CMAKE_SIZEOF_VOID_P from xtensor-pythonConfigVersion.cmake so that an xtensor-pythonConfig.cmake # generated for a 64 bit target can be used for 32 bit targets and vice versa. set(_XTENSOR_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) unset(CMAKE_SIZEOF_VOID_P) write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake VERSION ${${PROJECT_NAME}_VERSION} COMPATIBILITY AnyNewerVersion) set(CMAKE_SIZEOF_VOID_P ${_XTENSOR_CMAKE_SIZEOF_VOID_P}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake DESTINATION ${XTENSOR_PYTHON_CMAKECONFIG_INSTALL_DIR}) install(EXPORT ${PROJECT_NAME}-targets FILE ${PROJECT_NAME}Targets.cmake DESTINATION ${XTENSOR_PYTHON_CMAKECONFIG_INSTALL_DIR}) ``` -------------------------------- ### C++ xarray Passing Mechanisms Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples.rst Demonstrates different ways to pass C++ xarray objects to functions: by constant reference (read-only), by reference (read/write), and by value (copy). Python bindings typically involve copying data. ```cpp void foo(const xt::xarray& a); void foo(xt::xarray& a); void foo(xt::xarray a); ``` -------------------------------- ### Single-File Extension Module Setup Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/numpy_capi.rst Basic skeleton for a self-contained C++ extension module using xtensor-python. Requires defining FORCE_IMPORT_ARRAY before including headers and calling xt::import_numpy() during module initialization. ```cpp #define FORCE_IMPORT_ARRAY #include "xtensor-python/pyarray.hpp" PYBIND11_MODULE(plugin_name, m) { xt::import_numpy(); //... } ``` -------------------------------- ### Vectorized Function from C++ Scalar Function Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/basic_usage.rst This example demonstrates creating a NumPy-style universal function (ufunc) from a C++ scalar function using xt::pyvectorize. The C++ function computes sin(i) - cos(j). ```cpp #include "pybind11/pybind11.h" #define FORCE_IMPORT_ARRAY #include "xtensor-python/pyvectorize.hpp" #include #include namespace py = pybind11; double scalar_func(double i, double j) { return std::sin(i) - std::cos(j); } PYBIND11_MODULE(xtensor_python_test, m) { xt::import_numpy(); m.doc() = "Test module for xtensor python bindings"; m.def("vectorized_func", xt::pyvectorize(scalar_func), ""); } ``` ```python import numpy as np import xtensor_python_test as xt x = np.arange(15).reshape(3, 5) y = [1, 2, 3, 4, 5] z = xt.vectorized_func(x, y) z ``` -------------------------------- ### Run xbenchmark with CMake Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/dev_build_options.rst This snippet shows the command to build and execute the benchmarks for xtensor-python after the build system has been configured. ```cmake make xbenchmark ``` -------------------------------- ### Build HTML Documentation Source: https://github.com/xtensor-stack/xtensor-python/blob/master/README.md Builds the HTML documentation for xtensor-python by running the 'make html' command from the 'docs' subdirectory. This process utilizes Doxygen, Sphinx, and Breathe. ```bash make html ``` -------------------------------- ### Build and Run xtest with CMake Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/dev_build_options.rst This snippet demonstrates how to configure the build for xtensor-python using CMake, enabling the download of Google Test (gtest) and building the test suite. ```cmake mkdir build cd build cmake -DDOWNLOAD_GTEST=ON ../ make xtest ``` -------------------------------- ### Test Python Bindings with pytest Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/dev_build_options.rst This snippet illustrates how to run the test suite for the Python bindings of xtensor-python using the pytest framework. ```python cd .. pytest -s ``` -------------------------------- ### Run xtensor-python Tests Source: https://github.com/xtensor-stack/xtensor-python/blob/master/README.md Executes the tests for xtensor-python using the pytest framework. It's recommended to delete the 'build/' directory to ensure changes are picked up during rebuilding. ```bash py.test . ``` -------------------------------- ### xt::pyarray Class Documentation Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/pyarray.rst Detailed documentation for the xt::pyarray class, including all its members and their functionalities. This class is central to the integration of xtensor with Python. ```APIDOC xt::pyarray: // Represents a multi-dimensional array that can be seamlessly used with Python. // It provides functionalities for array creation, manipulation, and interoperability. // Key features include: // - Efficient memory management. // - Support for various data types. // - Integration with Python's NumPy and other libraries. // // Members: // (Detailed documentation for each member, including constructors, methods, and properties, would typically follow here, as generated by doxygen.) // Example (conceptual): // xt::pyarray arr(shape, value); // - Creates a pyarray of doubles with the specified shape and initializes elements with 'value'. // // xt::pyarray arr2 = create_array({2, 3}); // - Creates an integer array with shape (2, 3). // // arr.shape() // - Returns the shape of the array. // // arr.data() // - Returns a pointer to the underlying data. // // arr.fill(value) // - Fills the array with a specified value. ``` -------------------------------- ### Google Test Integration Source: https://github.com/xtensor-stack/xtensor-python/blob/master/test/CMakeLists.txt Handles the integration of Google Test (GTest), either by downloading it or using a local source directory. It configures GTest, builds it, and makes its targets available for linking. ```cmake if (DOWNLOAD_GTEST OR GTEST_SRC_DIR) if(DOWNLOAD_GTEST) # Download and unpack googletest at configure time configure_file(downloadGTest.cmake.in googletest-download/CMakeLists.txt) else() # Copy local source of googletest at configure time configure_file(copyGTest.cmake.in googletest-download/CMakeLists.txt) endif() execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) if(result) message(FATAL_ERROR "CMake step for googletest failed: ${result}") endif() execute_process(COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE result WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) if(result) message(FATAL_ERROR "Build step for googletest failed: ${result}") endif() set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # Add googletest directly to our build. This defines # the gtest and gtest_main targets. add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src ${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL) set(GTEST_INCLUDE_DIRS "${gtest_SOURCE_DIR}/include") set(GTEST_BOTH_LIBRARIES gtest_main gtest) else() find_package(GTest REQUIRED) endif() find_package(Threads) include_directories(${GTEST_INCLUDE_DIRS}) ``` -------------------------------- ### CMake Build Configuration for xtensor-python Module Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/examples/copy_cast/CMakeLists.txt This snippet shows the CMake script used to build a Python extension module. It finds and links essential libraries like pybind11, xtensor, xtensor-python, and NumPy, and defines the module name and version. ```cmake cmake_minimum_required(VERSION 3.29) project(mymodule) find_package(pybind11 CONFIG REQUIRED) find_package(xtensor REQUIRED) find_package(xtensor-python REQUIRED) find_package(Python REQUIRED COMPONENTS NumPy) pybind11_add_module(mymodule main.cpp) target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python::NumPy) target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0) ``` -------------------------------- ### CMake Build Configuration for xtensor-python Benchmarks Source: https://github.com/xtensor-stack/xtensor-python/blob/master/benchmark/CMakeLists.txt This snippet details the CMake configuration for building the xtensor-python benchmark executable. It includes setting the build type, compiler flags for different compilers (GCC, Clang, Intel, MSVC), enabling C++14 support, link-time optimization, and handling platform-specific linking. It also defines a custom target 'xbenchmark' to execute Python benchmark scripts. ```cmake if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) project(xtensor-python-benchmark) find_package(xtensor-python REQUIRED CONFIG) set(XTENSOR_PYTHON_INCLUDE_DIR ${xtensor-python_INCLUDE_DIRS}) endif () message(STATUS "Forcing tests build type to Release") set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) include(CheckCXXCompilerFlag) string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wunused-parameter -Wextra -Wreorder -Wconversion") CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG) if (HAS_CPP14_FLAG) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") else() message(FATAL_ERROR "Unsupported compiler -- xtensor requires C++14 support!") endif() # Enable link time optimization and set the default symbol # visibility to hidden (very important to obtain small binaries) if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) # Default symbol visibility set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden") # Check for Link Time Optimization support # (GCC/Clang) CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG) if (HAS_LTO_FLAG) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto") endif() # Intel equivalent to LTO is called IPO if (CMAKE_CXX_COMPILER_ID MATCHES "Intel") CHECK_CXX_COMPILER_FLAG("-ipo" HAS_IPO_FLAG) if (HAS_IPO_FLAG) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ipo") endif() endif() endif() endif() if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP /bigobj") set(CMAKE_EXE_LINKER_FLAGS /MANIFEST:NO) foreach(flag_var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}") endforeach() endif() set(XTENSOR_PYTHON_BENCHMARK main.cpp ) set(XTENSOR_PYTHON_BENCHMARK_TARGET benchmark_xtensor_python) add_library(${XTENSOR_PYTHON_BENCHMARK_TARGET} MODULE EXCLUDE_FROM_ALL ${XTENSOR_PYTHON_BENCHMARK} ${XTENSOR_PYTHON_HEADERS}) set_target_properties(${XTENSOR_PYTHON_BENCHMARK_TARGET} PROPERTIES PREFIX "") set_target_properties(${XTENSOR_PYTHON_BENCHMARK_TARGET} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}") if (APPLE) target_link_libraries(${XTENSOR_PYTHON_BENCHMARK_TARGET} PRIVATE "-undefined dynamic_lookup") elseif (MSVC) target_link_libraries(${XTENSOR_PYTHON_BENCHMARK_TARGET} ${PYTHON_LIBRARIES}) else () target_link_libraries(${XTENSOR_PYTHON_BENCHMARK_TARGET} PRIVATE xtensor-python) endif() configure_file(benchmark_pyarray.py benchmark_pyarray.py COPYONLY) configure_file(benchmark_pytensor.py benchmark_pytensor.py COPYONLY) configure_file(benchmark_pybind_array.py benchmark_pybind_array.py COPYONLY) configure_file(benchmark_pyvectorize.py benchmark_pyvectorize.py COPYONLY) configure_file(benchmark_pybind_vectorize.py benchmark_pybind_vectorize.py COPYONLY) add_custom_target(xbenchmark COMMAND "${PYTHON_EXECUTABLE}" "benchmark_pyarray.py" COMMAND "${PYTHON_EXECUTABLE}" "benchmark_pytensor.py" COMMAND "${PYTHON_EXECUTABLE}" "benchmark_pybind_array.py" COMMAND "${PYTHON_EXECUTABLE}" "benchmark_pyvectorize.py" COMMAND "${PYTHON_EXECUTABLE}" "benchmark_pybind_vectorize.py" DEPENDS ${XTENSOR_PYTHON_BENCHMARK_TARGET}) ``` -------------------------------- ### Test Executable and Linking Source: https://github.com/xtensor-stack/xtensor-python/blob/master/test/CMakeLists.txt Defines the main test executable 'test_xtensor_python' by compiling source files and linking against necessary libraries including xtensor-python, GTest, Threads, and Python. ```cmake set(XTENSOR_PYTHON_TESTS main.cpp test_pyarray.cpp test_pyarray_traits.cpp test_pytensor.cpp test_pyvectorize.cpp test_sfinae.cpp ) add_executable(test_xtensor_python ${XTENSOR_PYTHON_TESTS} ${XTENSOR_PYTHON_HEADERS}) target_link_libraries(test_xtensor_python xtensor-python ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${PYTHON_LIBRARIES}) if(DOWNLOAD_GTEST OR GTEST_SRC_DIR) add_dependencies(test_xtensor_python gtest_main) endif() add_custom_target(xtest COMMAND ./test_xtensor_python DEPENDS test_xtensor_python) ``` -------------------------------- ### Project Versioning and Configuration Source: https://github.com/xtensor-stack/xtensor-python/blob/master/CMakeLists.txt Reads the XTENSOR_PYTHON_VERSION from the configuration header and sets the project version. It also defines the include directory for the project. ```cmake cmake_minimum_required(VERSION 3.29) project(xtensor-python) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) set(XTENSOR_PYTHON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) # Versioning # ========== set(XTENSOR_PYTHON_CONFIG_FILE "${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/xtensor_python_config.hpp") file(STRINGS ${XTENSOR_PYTHON_CONFIG_FILE} xtensor_python_version_defines REGEX "#define XTENSOR_PYTHON_VERSION_(MAJOR|MINOR|PATCH)") foreach(ver ${xtensor_python_version_defines}) if(ver MATCHES "#define XTENSOR_PYTHON_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$") set(XTENSOR_PYTHON_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "") endif() endforeach() set(${PROJECT_NAME}_VERSION ${XTENSOR_PYTHON_VERSION_MAJOR}.${XTENSOR_PYTHON_VERSION_MINOR}.${XTENSOR_PYTHON_VERSION_PATCH}) message(STATUS "xtensor-python v${${PROJECT_NAME}_VERSION}") ``` -------------------------------- ### Python Benchmark Scripts Execution Source: https://github.com/xtensor-stack/xtensor-python/blob/master/benchmark/CMakeLists.txt This snippet shows how the custom CMake target 'xbenchmark' executes several Python benchmark scripts using the detected Python executable. These scripts likely test different aspects of the xtensor-python library's performance. ```python "${PYTHON_EXECUTABLE}" "benchmark_pyarray.py" "${PYTHON_EXECUTABLE}" "benchmark_pytensor.py" "${PYTHON_EXECUTABLE}" "benchmark_pybind_array.py" "${PYTHON_EXECUTABLE}" "benchmark_pyvectorize.py" "${PYTHON_EXECUTABLE}" "benchmark_pybind_vectorize.py" ``` -------------------------------- ### Git Release Workflow Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/releasing.rst Steps for releasing a new version of xtensor-python using Git. This involves updating version macros, committing changes, adding a tag, and pushing to the remote repository. ```git git add . git commit -m "Release Major.minor.patch" git push git push --tags ``` -------------------------------- ### xt::pytensor Class Documentation Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/pytensor.rst Documentation for the xt::pytensor class, which is part of the xtensor-python project. This entry details the class members and their functionalities as generated by doxygen. ```APIDOC xt::pytensor: // Class members and functionalities for xt::pytensor as documented by doxygen. // This includes methods for tensor manipulation and integration with Python. ``` -------------------------------- ### Sum of Sines using C++ and NumPy Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/basic_usage.rst This snippet shows how to use a C++ function that calculates the sine of each element in a NumPy array and then sums them up. It utilizes pybind11 for Python bindings and xtensor for C++ universal functions. ```cpp #include // Standard library import for std::accumulate #include "pybind11/pybind11.h" // Pybind11 import to define Python bindings #include "xtensor/core/xmath.hpp" // xtensor import for the C++ universal functions #define FORCE_IMPORT_ARRAY // numpy C api loading #include "xtensor-python/pyarray.hpp" // Numpy bindings double sum_of_sines(xt::pyarray& m) { auto sines = xt::sin(m); // sines does not actually hold values. return std::accumulate(sines.cbegin(), sines.cend(), 0.0); } PYBIND11_MODULE(xtensor_python_test, m) { xt::import_numpy(); m.doc() = "Test module for xtensor python bindings"; m.def("sum_of_sines", sum_of_sines, "Sum the sines of the input values"); } ``` ```python import numpy as np import xtensor_python_test as xt a = np.arange(15).reshape(3, 5) s = xt.sum_of_sines(v) s ``` -------------------------------- ### C++ Compiler Requirements Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/index.rst This snippet details the C++ compiler requirements for xtensor and xtensor-python, specifying the minimum versions for GCC, Clang, and Visual C++ on different platforms. ```markdown ``xtensor`` and ``xtensor-python`` require a modern C++ compiler supporting C++14. The following C++ compilers are supported: - On Windows platforms, Visual C++ 2015 Update 2, or more recent - On Unix platforms, gcc 4.9 or a recent version of Clang ``` -------------------------------- ### Sum of Sines using xtensor-python (Python Usage) Source: https://github.com/xtensor-stack/xtensor-python/blob/master/README.md This Python code snippet shows how to use the `sum_of_sines` function exposed by the xtensor-python C++ module. It creates a NumPy array, passes it to the `sum_of_sines` function, and prints the result. ```python import numpy as np import xtensor_python_test as xt v = np.arange(15).reshape(3, 5) s = xt.sum_of_sines(v) print(s) ``` -------------------------------- ### Dependency Management (xtensor, Python, pybind11, NumPy) Source: https://github.com/xtensor-stack/xtensor-python/blob/master/CMakeLists.txt Finds and checks versions for required dependencies: xtensor, Python, pybind11, and NumPy. It includes version compatibility checks for xtensor and pybind11. ```cmake # Dependencies # ============ set(xtensor_REQUIRED_VERSION 0.26.0) if(TARGET xtensor) set(xtensor_VERSION ${XTENSOR_VERSION_MAJOR}.${XTENSOR_VERSION_MINOR}.${XTENSOR_VERSION_PATCH}) # Note: This is not SEMVER compatible comparison if( NOT ${xtensor_version} VERSION_GREATER_EQUAL ${xtensor_REQUIRED_VERSION}) message(ERROR "Mismatch xtensor versions. Found '${xtensor_VERSION}' but requires: '${xtensor_REQUIRED_VERSION}'") else() message(STATUS "Found xtensor v${xtensor_VERSION}") endif() else() find_package(xtensor ${xtensor_REQUIRED_VERSION} REQUIRED) message(STATUS "Found xtensor: ${xtensor_INCLUDE_DIRS}/xtensor") endif() find_package(Python COMPONENTS Interpreter REQUIRED) set(pybind11_REQUIRED_VERSION 2.6.1) if (NOT TARGET pybind11::headers) # Defaults to ON for cmake >= 3.18 # https://github.com/pybind/pybind11/blob/35ff42b56e9d34d9a944266eb25f2c899dbdfed7/CMakeLists.txt#L96 set(PYBIND11_FINDPYTHON OFF) find_package(pybind11 ${pybind11_REQUIRED_VERSION} REQUIRED) message(STATUS "Found pybind11: ${pybind11_INCLUDE_DIRS}/pybind11") else () # pybind11 has a variable that indicates its version already, so use that message(STATUS "Found pybind11 v${pybind11_VERSION}") endif () # Look for NumPy headers, except if NUMPY_INCLUDE_DIRS is passed, # which is required under some circumstances (such as wasm, where # there is no real python executable) if(NOT NUMPY_INCLUDE_DIRS) find_package(NumPy REQUIRED) endif() message(STATUS "Found numpy: ${NUMPY_INCLUDE_DIRS}") ``` -------------------------------- ### Enabling NumPy Arrays in C++ Libraries Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/index.rst This section explains how xtensor-python facilitates the use of NumPy data structures directly within C++ code via Python's Buffer Protocol. It also touches upon xtensor's expression system and the creation of Universal Functions from C++ scalar functions. ```markdown Instead of exposing new types to python, ``xtensor-python`` enables the use of NumPy_ data structures from C++ using Python's `Buffer Protocol`_. In addition to the basic accessors and iterators of ``xtensor`` containers, it also enables using numpy arrays with ``xtensor``'s expression system. Besides ``xtensor-python`` provides an API to create *Universal functions* from simple scalar functions from your C++ code. ``` -------------------------------- ### Conda-Forge Recipe Update Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/releasing.rst Instructions for updating the conda-forge recipe for xtensor-python. This includes modifying version numbers, build numbers, source tarball hashes, and checking dependencies. ```conda # Update version number to the new Major.minor.patch # Set the build number to 0 # Update the hash of the source tarball # Check for the versions of the dependencies # Optionally, rerender the conda-forge feedstock. ``` -------------------------------- ### xtensor-python Containers Source: https://github.com/xtensor-stack/xtensor-python/blob/master/docs/source/api_reference.rst This section details the container classes available in xtensor-python, including pyarray and pytensor. These classes are fundamental for handling multi-dimensional arrays and tensors within the library. ```python import xtensor # Example usage of pyarray (conceptual) # arr = xtensor.pyarray([1, 2, 3]) # Example usage of pytensor (conceptual) # ten = xtensor.pytensor([[1, 2], [3, 4]]) ``` -------------------------------- ### Create Universal Function from Scalar Function using xtensor-python (Python Usage) Source: https://github.com/xtensor-stack/xtensor-python/blob/master/README.md This Python code snippet demonstrates the usage of a vectorized function created from a C++ scalar function using `xt::pyvectorize`. It calls the `vectorized_func` with a NumPy array `x` and a list `y`, showcasing how the function is applied element-wise with broadcasting. ```python import numpy as np import xtensor_python_test as xt x = np.arange(15).reshape(3, 5) y = [1, 2, 3, 4, 5] z = xt.vectorized_func(x, y) print(z) ``` -------------------------------- ### Sum of Sines using xtensor-python Source: https://github.com/xtensor-stack/xtensor-python/blob/master/README.md This C++ code snippet demonstrates how to use xtensor-python to apply a C++ standard library algorithm (std::accumulate) and an xtensor universal function (xt::sin) to a NumPy array inplace. It defines a Python-callable function `sum_of_sines` that calculates the sum of the sines of the elements in a given pyarray. The PYBIND11_MODULE macro exposes this function to Python. ```cpp #include // Standard library import for std::accumulate #include // Pybind11 import to define Python bindings #include // xtensor import for the C++ universal functions #define FORCE_IMPORT_ARRAY #include // Numpy bindings double sum_of_sines(xt::pyarray& m) { auto sines = xt::sin(m); // sines does not actually hold values. return std::accumulate(sines.begin(), sines.end(), 0.0); } PYBIND11_MODULE(xtensor_python_test, m) { xt::import_numpy(); m.doc() = "Test module for xtensor python bindings"; m.def("sum_of_sines", sum_of_sines, "Sum the sines of the input values"); } ```