ndarray: an N-dimensional array with array views, multidimensional slicing, and efficient operations
Repo rust-ndarray/ndarray, read from GitHub 1 min ago. rust-ndarray has not joined Take My Repo.
Version 0.17.2 is mainly a patch fix to bugs related to the new ArrayRef implementation.
In addition, ndarray has reduced its packaging footprint to ease supply chain reviews (and shrink the binary size!).
A special thanks to @SwishSwushPow and @weiznich for bringing this to our attention and making the necessary changes.
This release of ndarray-rand adds compatibility for the new ArrayRef type in ndarray 0.17. It adds the the new RandomRefExt trait, providing sample_axis and sample_axis_using methods on ArrayRef.
This release also bumps the requirements for rand to 0.9.0 and for rand_distr to 0.5.0.
Version 0.17.1 provides a patch to fix the originally-unsound implementation of the new array reference types.
The reference types are now all unsized. Practically speaking, this has one major implication: writing functions and traits that accept RawRef and LayoutRef will now need a + ?Sized bound to work ergonomically with ArrayRef. For example, the release notes for 0.17.0 said
Reading / Writing Shape:
LayoutRef<A, D>LayoutRef lets functions view or modify shape/stride information without touching data.
This replaces verbose signatures like:fn alter_view<S>(a: &mut ArrayBase<S, Ix1>) where S: Data<Elem = f64>;Use AsRef / AsMut for best compatibility:
fn alter_shape<T>(a: &mut T) where T: AsMut<LayoutRef<f64>>;
However, these functions now need an additional bound to allow for callers to pass in &ArrayRef types:
fn alter_shape<T>(a: &mut T) where T: AsMut<LayoutRef<f64>> + ?Sized; // Added bound here
A huge thank you to Sarah Quiñones (@sarah-quinones) for catching the original unsound bug and helping to fix it. She does truly excellent work with faer-rs; check it out!
Note: 0.17.0 was yanked due to a bug in the reference type implementation that could cause use-after-free. That bug was fixed in 0.17.1. All the changes listed here are once again available in 0.17.1, with a small caveat for the reference types. See the release notes for 0.17.1 for details.
Version 0.17.0 introduces a new array reference type — the preferred way to write functions and extension traits in ndarray. This release is fully backwards-compatible but represents a major usability improvement. The first section of this changelog explains the change in detail.
It also includes numerous new methods, math functions, and internal improvements — all credited below.
ndarray 0.17.0 adds new reference types for writing functions and traits that work seamlessly with owned arrays and views.
When writing functions that accept array arguments:
&ArrayRef<A, D> to read elements from any array.&mut ArrayRef<A, D> to modify elements.&T where T: AsRef<LayoutRef<A, D>> to inspect shape/stride only.&mut T where T: AsMut<LayoutRef<A, D>> to modify shape/stride only.All existing function signatures continue to work; these new types are fully opt-in.
ndarray has multiple ways to write functions that take arrays (a problem captured well in issue #1059). For example:
fn sum(a: ArrayView1<f64>) -> f64; fn sum(a: &ArrayView1<f64>) -> f64; fn sum(a: &Array1<f64>) -> f64;
All of these work, but having several equivalent forms causes confusion. The most general solution, writing generically over storage types:
fn sum<S>(a: &ArrayBase<S, Ix1>) -> f64 where S: Data<Elem = f64>;
is powerful but verbose and often hard to read. Version 0.17.0 introduces a new, simpler pattern that expresses the same flexibility more clearly.
Three new reference types make it easier to write functions that accept any kind of array while clearly expressing what kind of access (data or layout) they need.
ArrayRef<A, D>ArrayRef is the Deref target of ArrayBase. It behaves like &[T] for Vec<T>, giving access to elements and layout. Mutability is expressed through the reference itself (& vs &mut), not through a trait bound or the type itself. It is used as follows:
fn sum(a: &ArrayRef1<f64>) -> f64; fn cumsum_mut(a: &mut ArrayRef1<f64>);
(ArrayRef1 is available from the prelude.)
LayoutRef<A, D>LayoutRef lets functions view or modify shape/stride information without touching data. This replaces verbose signatures like:
fn alter_view<S>(a: &mut ArrayBase<S, Ix1>) where S: Data<Elem = f64>;
Use AsRef / AsMut for best compatibility:
fn alter_shape<T>(a: &mut T) where T: AsMut<LayoutRef<f64>>;
(Accepting a LayoutRef directly can cause unnecessary copies; see #1440.)
RawRef<A, D>RawRef augments RawArrayView and RawArrayViewMut for power users needing unsafe element access (e.g. uninitialized buffers). Like LayoutRef, it is best used via AsRef / AsMut.
diff method for calculating the difference between elements by @johann-cm #1437partition method for partially sorting an array by @NewBornRustacean #1498meshgrid method for building regular grids of values by @akern40 #1477cumprod method for cumulative products by @NewBornRustacean #1491exp_m1, ln_1p, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, and hypotaxis_windows_with_stride method for strided windows by @goertzenator #1460permute_axes) and reversing (reverse_axes) axes by @NewBornRustacean #1505into_*_iter functions as lifetime-preserving versions of into-iterator functionality by @akern40 #1510remove_index can now be called on views, in addition to owned arrays by @akern40serde-1, test, and docs feature flags; by @akern40 #1479
approx,serde,rayon instead of docs.serde instead of serde-1last_mut() now guarantees that the underlying data is uniquely held by @bluss #1429ArrayView is now covariant over lifetime by @akern40 #1480, so that the following code now compilesfn fn_cov<'a>(x: ArrayView1<'static, f64>) -> ArrayView1<'a, f64> { x }
select by @Drazharinto_raw_vec_and_offset by @benliepertArray::zeros with how to control the return type by @akern40.into_shape() is now deprecated..into_shape_with_order() or .to_shape() instead, which don't have into_shape's drawbacks.RawViewMut::from_shape_ptr with a debug assertion by @bluss #1413CowArray an owned storage array, require Clone bound for into_shared by @jturner314 #1028NdProducer::Dim of axis_windows() to Ix1 by @jonasBoss #1305squeeze() to dynamic dimension arrays by @barakugav #1396flatten, flatten_with_order and into_flat to arrays by @barakugav #1397is_unique for ArcArray by @daniellga #1399triu and tril methods directly to ArrayBase by @akern40 #1386product_axis by @akern40 #1387LanesIter by @Muthsera #1237From<&[[A; N]]> for ArrayView and From<&mut [[A; N]]> for ArrayViewMut by @jturner314 #1131s![] macro by @jturner314 #1196base Computations to be Safer by @LazaroHurtado #1297Windows by @LazaroHurtado #1249clone_from() in two places by @ChayimFriedman2 #1347