{"id":13474547,"url":"https://github.com/xtensor-stack/xtensor-python","last_synced_at":"2025-05-06T20:48:02.880Z","repository":{"id":13052742,"uuid":"73431892","full_name":"xtensor-stack/xtensor-python","owner":"xtensor-stack","description":"Python bindings for xtensor","archived":false,"fork":false,"pushed_at":"2025-04-11T08:43:52.000Z","size":562,"stargazers_count":353,"open_issues_count":47,"forks_count":61,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-04-11T10:20:00.285Z","etag":null,"topics":["c-plus-plus","numpy-arrays","python-bindings","tensor"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xtensor-stack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-11T00:18:30.000Z","updated_at":"2025-04-11T08:43:56.000Z","dependencies_parsed_at":"2022-07-22T09:22:18.878Z","dependency_job_id":"3d68ff22-03dc-46e3-baea-151d6055106c","html_url":"https://github.com/xtensor-stack/xtensor-python","commit_stats":{"total_commits":287,"total_committers":22,"mean_commits":"13.045454545454545","dds":0.6236933797909407,"last_synced_commit":"815ffe50838924029bb98c69a480c511d1c14fc8"},"previous_names":["quantstack/xtensor-python"],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtensor-stack%2Fxtensor-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtensor-stack%2Fxtensor-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtensor-stack%2Fxtensor-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtensor-stack%2Fxtensor-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xtensor-stack","download_url":"https://codeload.github.com/xtensor-stack/xtensor-python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252769136,"owners_count":21801373,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-plus-plus","numpy-arrays","python-bindings","tensor"],"created_at":"2024-07-31T16:01:13.087Z","updated_at":"2025-05-06T20:48:02.869Z","avatar_url":"https://github.com/xtensor-stack.png","language":"C++","readme":"# ![xtensor-python](docs/source/xtensor-python.svg)\n\n[![GHA Linux](https://github.com/xtensor-stack/xtensor/actions/workflows/linux.yml/badge.svg)](https://github.com/xtensor-stack/xtensor/actions/workflows/linux.yml)\n[![GHA OSX](https://github.com/xtensor-stack/xtensor/actions/workflows/osx.yml/badge.svg)](https://github.com/xtensor-stack/xtensor/actions/workflows/osx.yml)\n[![GHA Windows](https://github.com/xtensor-stack/xtensor/actions/workflows/windows.yml/badge.svg)](https://github.com/xtensor-stack/xtensor/actions/workflows/windows.yml)\n[![Documentation](http://readthedocs.org/projects/xtensor-python/badge/?version=latest)](https://xtensor-python.readthedocs.io/en/latest/?badge=latest)\n[![Join the Gitter Chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/QuantStack/Lobby?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nPython bindings for the [xtensor](https://github.com/xtensor-stack/xtensor) C++ multi-dimensional array library.\n\n - `xtensor` is a C++ library for multi-dimensional arrays enabling numpy-style broadcasting and lazy computing.\n - `xtensor-python` enables inplace use of numpy arrays in C++ with all the benefits from `xtensor`\n\n     - C++ universal function and broadcasting\n     - STL - compliant APIs.\n     - A broad coverage of numpy APIs (see [the numpy to xtensor cheat sheet](http://xtensor.readthedocs.io/en/latest/numpy.html)).\n\nThe Python bindings for `xtensor` are based on the [pybind11](https://github.com/pybind/pybind11/) C++ library, which enables seamless interoperability between C++ and Python.\n\n## Installation\n\n`xtensor-python` is a header-only library. We provide a package for the mamba (or conda) package manager.\n\n```bash\nmamba install -c conda-forge xtensor-python\n```\n\n## Documentation\n\nTo get started with using `xtensor-python`, check out the full documentation\n\nhttp://xtensor-python.readthedocs.io/\n\n## Usage\n\nxtensor-python offers two container types wrapping numpy arrays inplace to provide an xtensor semantics\n\n - `pytensor`\n - `pyarray`.\n\nBoth containers enable the numpy-style APIs of xtensor (see [the numpy to xtensor cheat sheet](http://xtensor.readthedocs.io/en/latest/numpy.html)).\n\n - On the one hand, `pyarray` has a dynamic number of dimensions. Just like numpy arrays, it can be reshaped with a shape of a different length (and the new shape is reflected on the python side).\n\n - On the other hand `pytensor` has a compile time number of dimensions, specified with a template parameter. Shapes of `pytensor` instances are stack allocated, making `pytensor` a significantly faster expression than `pyarray`.\n\n### Example 1: Use an algorithm of the C++ standard library on a numpy array inplace.\n\n**C++ code**\n\n```cpp\n#include \u003cnumeric\u003e                        // Standard library import for std::accumulate\n#include \u003cpybind11/pybind11.h\u003e            // Pybind11 import to define Python bindings\n#include \u003cxtensor/core/xmath.hpp\u003e              // xtensor import for the C++ universal functions\n#define FORCE_IMPORT_ARRAY\n#include \u003cxtensor-python/pyarray.hpp\u003e     // Numpy bindings\n\ndouble sum_of_sines(xt::pyarray\u003cdouble\u003e\u0026 m)\n{\n    auto sines = xt::sin(m);  // sines does not actually hold values.\n    return std::accumulate(sines.begin(), sines.end(), 0.0);\n}\n\nPYBIND11_MODULE(xtensor_python_test, m)\n{\n    xt::import_numpy();\n    m.doc() = \"Test module for xtensor python bindings\";\n\n    m.def(\"sum_of_sines\", sum_of_sines, \"Sum the sines of the input values\");\n}\n```\n\n**Python Code**\n\n```python\nimport numpy as np\nimport xtensor_python_test as xt\n\nv = np.arange(15).reshape(3, 5)\ns = xt.sum_of_sines(v)\nprint(s)\n```\n\n**Outputs**\n\n```\n1.2853996391883833\n```\n\n**Working example**\n\nGet the working example here:\n\n*   [`CMakeLists.txt`](docs/source/examples/readme_example_1/CMakeLists.txt)\n*   [`main.cpp`](docs/source/examples/readme_example_1/main.cpp)\n*   [`example.py`](docs/source/examples/readme_example_1/example.py)\n\n### Example 2: Create a universal function from a C++ scalar function\n\n**C++ code**\n\n```cpp\n#include \u003cpybind11/pybind11.h\u003e\n#define FORCE_IMPORT_ARRAY\n#include \u003cxtensor-python/pyvectorize.hpp\u003e\n#include \u003cnumeric\u003e\n#include \u003ccmath\u003e\n\nnamespace py = pybind11;\n\ndouble scalar_func(double i, double j)\n{\n    return std::sin(i) - std::cos(j);\n}\n\nPYBIND11_MODULE(xtensor_python_test, m)\n{\n    xt::import_numpy();\n    m.doc() = \"Test module for xtensor python bindings\";\n\n    m.def(\"vectorized_func\", xt::pyvectorize(scalar_func), \"\");\n}\n```\n\n**Python Code**\n\n```python\nimport numpy as np\nimport xtensor_python_test as xt\n\nx = np.arange(15).reshape(3, 5)\ny = [1, 2, 3, 4, 5]\nz = xt.vectorized_func(x, y)\nprint(z)\n```\n\n**Outputs**\n\n```\n[[-0.540302,  1.257618,  1.89929 ,  0.794764, -1.040465],\n [-1.499227,  0.136731,  1.646979,  1.643002,  0.128456],\n [-1.084323, -0.583843,  0.45342 ,  1.073811,  0.706945]]\n```\n\n## Installation\n\nWe provide a package for the conda package manager.\n\n```bash\nconda install -c conda-forge xtensor-python\n```\n\nThis will pull the dependencies to xtensor-python, that is `pybind11` and `xtensor`.\n\n## Project cookiecutter\n\nA template for a project making use of `xtensor-python` is available in the form of a cookiecutter [here](https://github.com/xtensor-stack/xtensor-python-cookiecutter).\n\nThis project is meant to help library authors get started with the xtensor python bindings.\n\nIt 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.\n\n## Building and Running the Tests\n\nTesting `xtensor-python` requires `pytest`\n\n  ``` bash\n  py.test .\n  ```\n\nTo pick up changes in `xtensor-python` while rebuilding, delete the `build/` directory.\n\n## Building the HTML Documentation\n\n`xtensor-python`'s documentation is built with three tools\n\n - [doxygen](http://www.doxygen.org)\n - [sphinx](http://www.sphinx-doc.org)\n - [breathe](https://breathe.readthedocs.io)\n\nWhile doxygen must be installed separately, you can install breathe by typing\n\n```bash\npip install breathe\n```\n\nBreathe can also be installed with `conda`\n\n```bash\nconda install -c conda-forge breathe\n```\n\nFinally, build the documentation with\n\n```bash\nmake html\n```\n\nfrom the `docs` subdirectory.\n\n## Dependencies on `xtensor` and `pybind11`\n\n`xtensor-python` depends on the `xtensor` and `pybind11` libraries\n\n| `xtensor-python` | `xtensor` |  `pybind11`      |\n|------------------|-----------|------------------|\n| master           |  ^0.26.0  | \u003e=2.6.1,\u003c3       |\n| 0.28.0           |  ^0.26.0  | \u003e=2.6.1,\u003c3       |\n| 0.27.0           |  ^0.25.0  | \u003e=2.6.1,\u003c3       |\n| 0.26.1           |  ^0.24.0  | ~2.4.3           |\n| 0.26.0           |  ^0.24.0  | ~2.4.3           |\n| 0.25.3           |  ^0.23.0  | ~2.4.3           |\n| 0.25.2           |  ^0.23.0  | ~2.4.3           |\n| 0.25.1           |  ^0.23.0  | ~2.4.3           |\n| 0.25.0           |  ^0.23.0  | ~2.4.3           |\n| 0.24.1           |  ^0.21.2  | ~2.4.3           |\n| 0.24.0           |  ^0.21.1  | ~2.4.3           |\n\nThese dependencies are automatically resolved when using the conda package manager.\n\n## License\n\nWe use a shared copyright model that enables all contributors to maintain the\ncopyright on their contributions.\n\nThis software is licensed under the BSD-3-Clause license. See the [LICENSE](LICENSE) file for details.\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtensor-stack%2Fxtensor-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxtensor-stack%2Fxtensor-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtensor-stack%2Fxtensor-python/lists"}