Skip to content

Commit 9fc9928

Browse files
committed
Add top-level documentation and examples to further improve discoverability of nalgebra support.
1 parent d3159c8 commit 9fc9928

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pyo3 = { version = "0.17", default-features = false, features = ["macros"] }
2727

2828
[dev-dependencies]
2929
pyo3 = { version = "0.17", default-features = false, features = ["auto-initialize"] }
30+
nalgebra = { version = "0.31", default-features = false, features = ["std"] }
3031

3132
[package.metadata.docs.rs]
3233
all-features = true

src/lib.rs

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

0 commit comments

Comments
 (0)