Python bindings for the xtensor C++ multi-dimensional array library.
xtensoris a C++ library for multi-dimensional arrays enabling numpy-style broadcasting and lazy computing.xtensor-pythonenables inplace use of numpy arrays with all the benefits fromxtensor- C++ universal function 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.
xtensor-python is a header-only library. We provide a package for the conda package manager.
conda install -c conda-forge xtensor-pythonxtensor-python depends on the xtensor and pybind11 libraries
xtensor-python | xtensor | pybind11 |
|---|---|---|
| master | master | ^2.0.0 |
| 0.2.0 | ^0.2.1 | ^1.8.1 |
| 0.1.0 | ^0.1.1 | ^1.8.1 |
These dependencies are automatically resolved when using the conda package manager.
C++ code
#include<numeric>// Standard library import for std::accumulate #include"pybind11/pybind11.h"// Pybind11 import to define Python bindings #include"xtensor/xmath.hpp"// xtensor import for the C++ universal functions #include"xtensor-python/pyarray.hpp"// Numpy bindingsdoublesum_of_sines(xt::pyarray<double> &m){auto sines = xt::sin(m); // sines does not actually hold any value, which are only computed upon accessreturnstd::accumulate(sines.begin(), sines.end(), 0.0)} PYBIND11_PLUGIN(xtensor_python_test){pybind11::modulem("xtensor_python_test", "Test module for xtensor python bindings"); m.def("sum_of_sines", sum_of_sines, "Computes the sum of the sines of the values of the input array"); return m.ptr()}Python Code
importnumpyasnpimportxtensor_python_testasxta=np.arange(15).reshape(3, 5) s=xt.sum_of_sines(v) sOutputs
1.2853996391883833 C++ code
#include"pybind11/pybind11.h" #include"xtensor-python/pyvectorize.hpp" #include<numeric> #include<cmath>namespacepy= pybind11; doublescalar_func(double i, double j){returnstd::sin(i) - std::cos(j)} PYBIND11_PLUGIN(xtensor_python_test){py::modulem("xtensor_python_test", "Test module for xtensor python bindings"); m.def("vectorized_func", xt::pyvectorize(scalar_func), ""); return m.ptr()}Python Code
importnumpyasnpimportxtensor_python_testasxtx=np.arange(15).reshape(3, 5) y= [1, 2, 3, 4, 5] z=xt.vectorized_func(x, y) zOutputs
[[-0.540302, 1.257618, 1.89929 , 0.794764, -1.040465], [-1.499227, 0.136731, 1.646979, 1.643002, 0.128456], [-1.084323, -0.583843, 0.45342 , 1.073811, 0.706945]] We provide a package for the conda package manager.
conda install -c conda-forge xtensor-pythonThis will pull the dependencies to xtensor-python, that is pybind11 and xtensor.
A template for a project making use of xtensor-python is available in the form of a cookie cutter here.
This project is meant to help library authors get started with the xtensor python bindings.
It produces a project following the best practices for the packaging and distribution of Python extensions based on xtensor-python, including a setup.py file and a conda recipe.
Testing xtensor-python requires pytest
py.test .To pick up changes in xtensor-python while rebuilding, delete the build/ directory.
xtensor-python's documentation is built with three tools
While doxygen must be installed separately, you can install breathe by typing
pip install breatheBreathe can also be installed with conda
conda install -c conda-forge breatheFinally, build the documentation with
make htmlfrom the docs subdirectory.
We use a shared copyright model that enables all contributors to maintain the copyright on their contributions.
This software is licensed under the BSD-3-Clause license. See the LICENSE file for details.