{"id":20535769,"url":"https://github.com/fusion-engineering/nalgebra-numpy","last_synced_at":"2025-04-14T07:20:17.367Z","repository":{"id":54669845,"uuid":"221324506","full_name":"fusion-engineering/nalgebra-numpy","owner":"fusion-engineering","description":"Convert between nalgebra and numpy types.","archived":false,"fork":false,"pushed_at":"2022-11-01T02:26:48.000Z","size":83,"stargazers_count":22,"open_issues_count":2,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-27T20:51:18.399Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/fusion-engineering.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}},"created_at":"2019-11-12T22:30:41.000Z","updated_at":"2024-12-31T05:32:35.000Z","dependencies_parsed_at":"2022-08-13T23:20:52.751Z","dependency_job_id":null,"html_url":"https://github.com/fusion-engineering/nalgebra-numpy","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fusion-engineering%2Fnalgebra-numpy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fusion-engineering%2Fnalgebra-numpy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fusion-engineering%2Fnalgebra-numpy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fusion-engineering%2Fnalgebra-numpy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fusion-engineering","download_url":"https://codeload.github.com/fusion-engineering/nalgebra-numpy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248269768,"owners_count":21075788,"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":[],"created_at":"2024-11-16T00:34:03.591Z","updated_at":"2025-04-14T07:20:17.333Z","avatar_url":"https://github.com/fusion-engineering.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nalgebra-numpy\n\nThis crate provides conversion between [`nalgebra`] and [`numpy`](https://numpy.org/).\nIt is intended to be used when you want to share nalgebra matrices between Python and Rust code,\nfor example with [`inline-python`](https://docs.rs/inline-python).\n\n## Conversion from numpy to nalgebra.\n\nIt is possible to create either a view or a copy of a numpy array.\nYou can use [`matrix_from_numpy`] to copy the data into a new matrix,\nor one of [`matrix_slice_from_numpy`] or [`matrix_slice_mut_from_numpy`] to create a view.\nIf a numpy array is not compatible with the requested matrix type,\nan error is returned.\n\nKeep in mind though that the borrow checker can not enforce rules on data managed by a Python object.\nYou could potentially keep an immutable view around in Rust, and then modify the data from Python.\nFor this reason, creating any view -- even an immutable one -- is unsafe.\n\n## Conversion from nalgebra to numpy.\n\nA nalgebra matrix can also be converted to a numpy array, using [`matrix_to_numpy`].\nThis function always creates a copy.\nSince all nalgebra arrays can be represented as a numpy array,\nthis directly returns a [`pyo3::PyObject`] rather than a `Result`.\n\n## Examples.\n\nCopy a numpy array to a new fixed size matrix:\n\n```rust\nuse inline_python::{Context, python};\nuse nalgebra_numpy::{matrix_from_numpy};\n\nlet gil = pyo3::Python::acquire_gil();\nlet context = Context::new_with_gil(gil.python());\ncontext.run(python! {\n    import numpy as np\n    matrix = np.array([\n        [1.0, 2.0, 3.0],\n        [4.0, 5.0, 6.0],\n        [7.0, 8.0, 9.0],\n    ])\n});\n\nlet matrix = context.globals(gil.python()).get_item(\"matrix\").unwrap();\nlet matrix : nalgebra::Matrix3\u003cf64\u003e = matrix_from_numpy(gil.python(), matrix)?;\n\nassert_eq!(matrix, nalgebra::Matrix3::new(\n    1.0, 2.0, 3.0,\n    4.0, 5.0, 6.0,\n    7.0, 8.0, 9.0,\n));\n```\n\nDynamic matrices are also supported:\n\n```rust\nuse nalgebra::DMatrix;\n#\n\nlet matrix : DMatrix\u003cf64\u003e = matrix_from_numpy(gil.python(), matrix)?;\nassert_eq!(matrix, DMatrix::from_row_slice(3, 3, \u0026[\n    1.0, 2.0, 3.0,\n    4.0, 5.0, 6.0,\n    7.0, 8.0, 9.0,\n]));\n```\n\nAnd so are partially dynamic matrices:\n\n```rust\nuse nalgebra::{MatrixMN, Dynamic, U3};\n\nlet matrix : MatrixMN\u003cf64, U3, Dynamic\u003e = matrix_from_numpy(gil.python(), matrix)?;\nassert_eq!(matrix, MatrixMN::\u003cf64, U3, Dynamic\u003e::from_row_slice(\u0026[\n    1.0, 2.0, 3.0,\n    4.0, 5.0, 6.0,\n    7.0, 8.0, 9.0,\n]));\n```\n\nA conversion to python object looks as follows:\n```rust\nuse nalgebra_numpy::matrix_to_numpy;\nuse nalgebra::Matrix3;\nuse inline_python::python;\n\nlet gil = pyo3::Python::acquire_gil();\nlet matrix = matrix_to_numpy(gil.python(), \u0026Matrix3::\u003ci32\u003e::new(\n    0, 1, 2,\n    3, 4, 5,\n    6, 7, 8,\n));\n\npython! {\n    from numpy import array_equal\n    assert array_equal('matrix, [\n        [0, 1, 2],\n        [3, 4, 5],\n        [6, 7, 8],\n    ])\n}\n```\n\n[`nalgebra`]: https://docs.rs/nalgebra\n[`matrix_from_numpy`]: https://docs.rs/nalgebra-numpy/latest/nalgebra_numpy/fn.matrix_from_numpy.html\n[`matrix_slice_from_numpy`]: https://docs.rs/nalgebra-numpy/latest/nalgebra_numpy/fn.matrix_slice_from_numpy.html\n[`matrix_slice_mut_from_numpy`]: https://docs.rs/nalgebra-numpy/latest/nalgebra_numpy/fn.matrix_slice_mut_from_numpy.html\n[`matrix_to_numpy`]: https://docs.rs/nalgebra-numpy/latest/nalgebra_numpy/fn.matrix_to_numpy.html\n[`pyo3::PyObject`]: https://docs.rs/pyo3/latest/pyo3/type.PyObject.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffusion-engineering%2Fnalgebra-numpy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffusion-engineering%2Fnalgebra-numpy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffusion-engineering%2Fnalgebra-numpy/lists"}