1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! This crate provides conversion between [`nalgebra`] and [`numpy`](https://numpy.org/).
//!
//! Currently, only the conversion from numpy to nalgebra is implemented,
//! but the other direction will be added soon.
//!
//! # Conversion from numpy to nalgebra.
//!
//! It is possible to create either a view or a copy of a numpy array.
//! You can use [`matrix_from_python`] to copy the data into a new matrix,
//! or one of [`matrix_slice_from_python`] or [`matrix_slice_mut_from_python`] to create a view.
//!
//! Keep in mind though that the borrow checker can not enforce rules on data managed by a Python object.
//! You could potentially keep an immutable view around in Rust, and then modify the data from Python.
//! For this reason, creating any view -- even an immutable one -- is unsafe.
//!
//! # Examples.
//!
//! Copy a numpy array to a new fixed size matrix:
//!
//! ```
//! #![feature(proc_macro_hygiene)]
//! use inline_python::{Context, python};
//! use nalgebra_numpy::{matrix_from_python};
//!
//! # fn main() -> Result<(), nalgebra_numpy::Error> {
//! let gil = pyo3::Python::acquire_gil();
//! let context = Context::new_with_gil(gil.python()).unwrap();
//! python! {
//! #![context = &context]
//! import numpy as np
//! matrix = np.array([
//! [1.0, 2.0, 3.0],
//! [4.0, 5.0, 6.0],
//! [7.0, 8.0, 9.0],
//! ])
//! }
//!
//! let matrix = context.globals(gil.python()).get_item("matrix").unwrap();
//! let matrix : nalgebra::Matrix3<f64> = matrix_from_python(matrix)?;
//!
//! assert_eq!(matrix, nalgebra::Matrix3::new(
//! 1.0, 2.0, 3.0,
//! 4.0, 5.0, 6.0,
//! 7.0, 8.0, 9.0,
//! ));
//! # Ok(())
//! # }
//! ```
//!
//! Dynamic matrices are also supported:
//!
//! ```
//! # #![feature(proc_macro_hygiene)]
//! # use inline_python::{Context, python};
//! # use nalgebra_numpy::{matrix_from_python};
//! use nalgebra::DMatrix;
//! # fn main() -> Result<(), nalgebra_numpy::Error> {
//! # let gil = pyo3::Python::acquire_gil();
//! # let context = Context::new_with_gil(gil.python()).unwrap();
//! # python! {
//! # #![context = &context]
//! # import numpy as np
//! # matrix = np.array([
//! # [1.0, 2.0, 3.0],
//! # [4.0, 5.0, 6.0],
//! # [7.0, 8.0, 9.0],
//! # ])
//! # }
//! #
//! # let matrix = context.globals(gil.python()).get_item("matrix").unwrap();
//!
//! // <snip>
//!
//! let matrix : DMatrix<f64> = matrix_from_python(matrix)?;
//! assert_eq!(matrix, DMatrix::from_row_slice(3, 3, &[
//! 1.0, 2.0, 3.0,
//! 4.0, 5.0, 6.0,
//! 7.0, 8.0, 9.0,
//! ]));
//! # Ok(())
//! # }
//! ```
//!
//! And so are partially dynamic matrices:
//!
//! ```
//! # #![feature(proc_macro_hygiene)]
//! # use inline_python::{Context, python};
//! # use nalgebra_numpy::{matrix_from_python};
//! use nalgebra::{MatrixMN, Dynamic, U3};
//! # fn main() -> Result<(), nalgebra_numpy::Error> {
//! # let gil = pyo3::Python::acquire_gil();
//! # let context = Context::new_with_gil(gil.python()).unwrap();
//! # python! {
//! # #![context = &context]
//! # import numpy as np
//! # matrix = np.array([
//! # [1.0, 2.0, 3.0],
//! # [4.0, 5.0, 6.0],
//! # [7.0, 8.0, 9.0],
//! # ])
//! # }
//! # let matrix = context.globals(gil.python()).get_item("matrix").unwrap();
//!
//! // <snip>
//!
//! let matrix : MatrixMN<f64, U3, Dynamic> = matrix_from_python(matrix)?;
//! assert_eq!(matrix, MatrixMN::<f64, U3, Dynamic>::from_row_slice(&[
//! 1.0, 2.0, 3.0,
//! 4.0, 5.0, 6.0,
//! 7.0, 8.0, 9.0,
//! ]));
//! # Ok(())
//! # }
//! ```
pub use *;