{"id":14977865,"url":"https://github.com/pyo3/rust-numpy","last_synced_at":"2025-05-13T00:05:29.166Z","repository":{"id":39059748,"uuid":"88950410","full_name":"PyO3/rust-numpy","owner":"PyO3","description":"PyO3-based Rust bindings of the NumPy C-API","archived":false,"fork":false,"pushed_at":"2025-04-08T12:07:49.000Z","size":15264,"stargazers_count":1228,"open_issues_count":18,"forks_count":121,"subscribers_count":23,"default_branch":"main","last_synced_at":"2025-05-13T00:05:15.825Z","etag":null,"topics":["ndarray","numpy","numpy-capi","rust-bindings","rust-ndarray","rust-numpy"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PyO3.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null}},"created_at":"2017-04-21T06:36:48.000Z","updated_at":"2025-05-08T09:05:58.000Z","dependencies_parsed_at":"2023-02-16T11:05:48.776Z","dependency_job_id":"ce8c31b4-9a43-4998-9972-dbc9993c61e6","html_url":"https://github.com/PyO3/rust-numpy","commit_stats":{"total_commits":771,"total_committers":44,"mean_commits":"17.522727272727273","dds":0.6990920881971465,"last_synced_commit":"161cb2a0af137280607faaa0ebe1328f9ac99654"},"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PyO3%2Frust-numpy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PyO3%2Frust-numpy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PyO3%2Frust-numpy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PyO3%2Frust-numpy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PyO3","download_url":"https://codeload.github.com/PyO3/rust-numpy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253843213,"owners_count":21972873,"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":["ndarray","numpy","numpy-capi","rust-bindings","rust-ndarray","rust-numpy"],"created_at":"2024-09-24T13:56:27.795Z","updated_at":"2025-05-13T00:05:29.145Z","avatar_url":"https://github.com/PyO3.png","language":"Rust","readme":"rust-numpy\n===========\n[![Actions Status](https://github.com/PyO3/rust-numpy/workflows/CI/badge.svg)](https://github.com/PyO3/rust-numpy/actions)\n[![Crate](https://img.shields.io/crates/v/numpy.svg)](https://crates.io/crates/numpy)\n[![Minimum rustc 1.63](https://img.shields.io/badge/rustc-1.63+-blue.svg)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html)\n[![Documentation](https://docs.rs/numpy/badge.svg)](https://docs.rs/numpy)\n[![codecov](https://codecov.io/gh/PyO3/rust-numpy/branch/main/graph/badge.svg)](https://codecov.io/gh/PyO3/rust-numpy)\n\nRust bindings for the NumPy C-API.\n\n## API documentation\n- [Latest release](https://docs.rs/numpy)\n- [Current main](https://pyo3.github.io/rust-numpy)\n\n## Requirements\n- Rust \u003e= 1.63.0\n  - Basically, our MSRV follows the one of [PyO3](https://github.com/PyO3/pyo3)\n- Python \u003e= 3.7\n  - Python 3.6 support was dropped from 0.16\n- Some Rust libraries\n  - [ndarray](https://github.com/rust-ndarray/ndarray) for Rust-side matrix library\n  - [PyO3](https://github.com/PyO3/pyo3) for Python bindings\n  - And more (see [Cargo.toml](Cargo.toml))\n- [numpy](https://numpy.org/) installed in your Python environments (e.g., via `pip install numpy`)\n  - We recommend `numpy \u003e= 1.16.0`, though older versions may work\n\n## Example\n\n### Write a Python module in Rust\n\nPlease see the [simple](examples/simple) example for how to get started.\n\nThere are also examples using [ndarray-linalg](examples/linalg) and [rayon](examples/parallel).\n\n```toml\n[lib]\nname = \"rust_ext\"\ncrate-type = [\"cdylib\"]\n\n[dependencies]\npyo3 = { version = \"0.22\", features = [\"extension-module\"] }\nnumpy = \"0.22\"\n```\n\n```rust\nuse numpy::ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};\nuse numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn, PyArrayMethods};\nuse pyo3::{pymodule, types::PyModule, PyResult, Python, Bound};\n\n#[pymodule]\nfn rust_ext\u003c'py\u003e(_py: Python\u003c'py\u003e, m: \u0026Bound\u003c'py, PyModule\u003e) -\u003e PyResult\u003c()\u003e {\n    // example using immutable borrows producing a new array\n    fn axpy(a: f64, x: ArrayViewD\u003c'_, f64\u003e, y: ArrayViewD\u003c'_, f64\u003e) -\u003e ArrayD\u003cf64\u003e {\n        a * \u0026x + \u0026y\n    }\n\n    // example using a mutable borrow to modify an array in-place\n    fn mult(a: f64, mut x: ArrayViewMutD\u003c'_, f64\u003e) {\n        x *= a;\n    }\n\n    // wrapper of `axpy`\n    #[pyfn(m)]\n    #[pyo3(name = \"axpy\")]\n    fn axpy_py\u003c'py\u003e(\n        py: Python\u003c'py\u003e,\n        a: f64,\n        x: PyReadonlyArrayDyn\u003c'py, f64\u003e,\n        y: PyReadonlyArrayDyn\u003c'py, f64\u003e,\n    ) -\u003e Bound\u003c'py, PyArrayDyn\u003cf64\u003e\u003e {\n        let x = x.as_array();\n        let y = y.as_array();\n        let z = axpy(a, x, y);\n        z.into_pyarray(py)\n    }\n\n    // wrapper of `mult`\n    #[pyfn(m)]\n    #[pyo3(name = \"mult\")]\n    fn mult_py\u003c'py\u003e(a: f64, x: \u0026Bound\u003c'py, PyArrayDyn\u003cf64\u003e\u003e) {\n        let x = unsafe { x.as_array_mut() };\n        mult(a, x);\n    }\n\n    Ok(())\n}\n```\n\n### Execute a Python program from Rust and get results\n\n``` toml\n[package]\nname = \"numpy-test\"\n\n[dependencies]\npyo3 = { version = \"0.22\", features = [\"auto-initialize\"] }\nnumpy = \"0.22\"\n```\n\n```rust\nuse numpy::{PyArray1, PyArrayMethods};\nuse pyo3::{types::{IntoPyDict, PyAnyMethods}, PyResult, Python, ffi::c_str};\n\nfn main() -\u003e PyResult\u003c()\u003e {\n    Python::with_gil(|py| {\n        let np = py.import(\"numpy\")?;\n        let locals = [(\"np\", np)].into_py_dict(py)?;\n\n        let pyarray = py\n            .eval(c_str!(\"np.absolute(np.array([-1, -2, -3], dtype='int32'))\"), Some(\u0026locals), None)?\n            .downcast_into::\u003cPyArray1\u003ci32\u003e\u003e()?;\n\n        let readonly = pyarray.readonly();\n        let slice = readonly.as_slice()?;\n        assert_eq!(slice, \u0026[1, 2, 3]);\n\n        Ok(())\n    })\n}\n```\n\n## Dependency on ndarray\n\nThis crate uses types from `ndarray` in its public API. `ndarray` is re-exported\nin the crate root so that you do not need to specify it as a direct dependency.\n\nFurthermore, this crate is compatible with multiple versions of `ndarray` and therefore depends\non a range of semver-incompatible versions, currently `\u003e= 0.15, \u003c 0.17`. Cargo does not\nautomatically choose a single version of `ndarray` by itself if you depend directly or indirectly\non anything but that exact range. It can therefore be necessary to manually unify these dependencies.\n\nFor example, if you specify the following dependencies\n\n```toml\nnumpy = \"0.22\"\nndarray = \"0.15\"\n```\n\nthis will currently depend on both version `0.15.6` and `0.16.1` of `ndarray` by default\neven though `0.15.6` is within the range `\u003e= 0.15, \u003c 0.17`. To fix this, you can run\n\n```sh\ncargo update --package ndarray:0.16.1 --precise 0.15.6\n```\n\nto achieve a single dependency on version `0.15.6` of `ndarray`.\n\n## Contributing\n\nWe welcome [issues](https://github.com/PyO3/rust-numpy/issues)\nand [pull requests](https://github.com/PyO3/rust-numpy/pulls).\n\nPyO3's [Contributing.md](https://github.com/PyO3/pyo3/blob/main/Contributing.md)\nis a nice guide for starting.\n\nAlso, we have a [Gitter](https://gitter.im/PyO3/Lobby) channel for communicating.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyo3%2Frust-numpy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpyo3%2Frust-numpy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyo3%2Frust-numpy/lists"}