Skip to content

Commit f540cd4

Browse files
authored
Merge pull request #432 from jturner314/ndarray-for-numpy-infer-create
Add note in ndarray for NumPy users docs about type inference for array creation
2 parents 9d83ade + 6008374 commit f540cd4

File tree

1 file changed

+25
-0
lines changed
  • src/doc/ndarray_for_numpy_users

1 file changed

+25
-0
lines changed

src/doc/ndarray_for_numpy_users/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,31 @@
194194
//! `np.array([1, 2, 3, 4]).reshape((2, 2), order='F')` | [`Array::from_shape_vec((2, 2).f(), vec![1, 2, 3, 4])?`][::from_shape_vec()] | create a 2×2 array from the elements in the list/`Vec` using Fortran (column-major) order
195195
//! `np.random` | See the [`ndarray-rand`](https://crates.io/crates/ndarray-rand) crate. | create arrays of random numbers
196196
//!
197+
//! Note that the examples in the table rely on the compiler inferring the
198+
//! element type and dimensionality from context, which is usually sufficient.
199+
//! However, if the compiler cannot infer the types, you can specify them
200+
//! manually. These are examples of creating a 3-D Fortran-layout array of
201+
//! `f64`s:
202+
//!
203+
//! ```
204+
//! # use ndarray::prelude::*;
205+
//! #
206+
//! // This is an example where the compiler can infer the element type
207+
//! // because `f64::sin` can only be called on `f64` elements:
208+
//! let arr1 = Array::zeros((3, 2, 4).f());
209+
//! arr1.mapv(f64::sin);
210+
//!
211+
//! // Specify just the element type and infer the dimensionality:
212+
//! let arr2 = Array::<f64, _>::zeros((3, 2, 4).f());
213+
//! let arr3: Array<f64, _> = Array::zeros((3, 2, 4).f());
214+
//!
215+
//! // Specify both the element type and dimensionality:
216+
//! let arr4 = Array3::<f64>::zeros((3, 2, 4).f());
217+
//! let arr5: Array3<f64> = Array::zeros((3, 2, 4).f());
218+
//! let arr6 = Array::<f64, Ix3>::zeros((3, 2, 4).f());
219+
//! let arr7: Array<f64, Ix3> = Array::zeros((3, 2, 4).f());
220+
//! ```
221+
//!
197222
//! ## Indexing and slicing
198223
//!
199224
//! A few notes:

0 commit comments

Comments
 (0)