Skip to content

Commit 96d7a06

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

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-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

+39-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
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(
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+
)]
1521
//! # Example
1622
//!
1723
//! ```
@@ -26,7 +32,39 @@
2632
//! py_array.readonly().as_array(),
2733
//! array![[1i64, 2], [3, 4]]
2834
//! );
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+
//! });
3068
//! ```
3169
//!
3270
//! [c-api]: https://numpy.org/doc/stable/reference/c-api

0 commit comments

Comments
 (0)