|
12 | 12 | //! Loading NumPy is done automatically and on demand. So if it is not installed, the functions
|
13 | 13 | //! provided by this crate will panic instead of returning a result.
|
14 | 14 | //!
|
| 15 | +#![cfg_attr( |
| 16 | + feature = "nalgebra", |
| 17 | + doc = "Integration with [`nalgebra`] is rovided via an implementation of [`ToPyArray`] for [`nalgebra::Matrix`] to convert nalgebra matrices into NumPy arrays |
| 18 | +as well as the [`PyReadonlyArray::try_as_matrix`] and [`PyReadwriteArray::try_as_matrix_mut`] methods to treat NumPy array as nalgebra matrix slices. |
| 19 | +" |
| 20 | +)] |
15 | 21 | //! # Example
|
16 | 22 | //!
|
17 | 23 | //! ```
|
|
26 | 32 | //! py_array.readonly().as_array(),
|
27 | 33 | //! array![[1i64, 2], [3, 4]]
|
28 | 34 | //! );
|
29 |
| -//! }) |
| 35 | +//! }); |
| 36 | +//! ``` |
| 37 | +//! |
| 38 | +#![cfg_attr(feature = "nalgebra", doc = "```")] |
| 39 | +#![cfg_attr(not(feature = "nalgebra"), doc = "```rust,ignore")] |
| 40 | +//! use numpy::pyo3::Python; |
| 41 | +//! use numpy::nalgebra::Matrix3; |
| 42 | +//! use numpy::{pyarray, ToPyArray}; |
| 43 | +//! |
| 44 | +//! Python::with_gil(|py| { |
| 45 | +//! let py_array = pyarray![py, [0, 1, 2], [3, 4, 5], [6, 7, 8]]; |
| 46 | +//! |
| 47 | +//! let py_array_square; |
| 48 | +//! |
| 49 | +//! { |
| 50 | +//! let py_array = py_array.readwrite(); |
| 51 | +//! let mut na_matrix = py_array.as_matrix_mut(); |
| 52 | +//! |
| 53 | +//! na_matrix.add_scalar_mut(1); |
| 54 | +//! |
| 55 | +//! py_array_square = na_matrix.pow(2).to_pyarray(py); |
| 56 | +//! } |
| 57 | +//! |
| 58 | +//! assert_eq!( |
| 59 | +//! py_array.readonly().as_matrix(), |
| 60 | +//! Matrix3::new(1, 2, 3, 4, 5, 6, 7, 8, 9) |
| 61 | +//! ); |
| 62 | +//! |
| 63 | +//! assert_eq!( |
| 64 | +//! py_array_square.readonly().as_matrix(), |
| 65 | +//! Matrix3::new(30, 36, 42, 66, 81, 96, 102, 126, 150) |
| 66 | +//! ); |
| 67 | +//! }); |
30 | 68 | //! ```
|
31 | 69 | //!
|
32 | 70 | //! [c-api]: https://numpy.org/doc/stable/reference/c-api
|
|
0 commit comments