Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README-quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn main() {
use ndarray::prelude::*;
use ndarray::{Array, Ix3};
fn main() {
let a = Array::<f64, _>::linspace(0., 5., 11);
let a = Array::<f64, _>::linspace(0.0..=5.0, 11);
println!("{:?}", a);
}
```
Expand Down
2 changes: 1 addition & 1 deletion benches/bench1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ const MEAN_SUM_N: usize = 127;
fn range_mat(m: Ix, n: Ix) -> Array2<f32>
{
assert!(m * n != 0);
Array::linspace(0., (m * n - 1) as f32, m * n)
Array::linspace(0.0..=(m * n - 1) as f32, m * n)
.into_shape_with_order((m, n))
.unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions benches/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn zeros_f64(bench: &mut Bencher)
#[bench]
fn map_regular(bench: &mut test::Bencher)
{
let a = Array::linspace(0., 127., 128)
let a = Array::linspace(0.0..=127.0, 128)
.into_shape_with_order((8, 16))
.unwrap();
bench.iter(|| a.map(|&x| 2. * x));
Expand All @@ -31,7 +31,7 @@ fn map_regular(bench: &mut test::Bencher)
#[bench]
fn map_stride(bench: &mut test::Bencher)
{
let a = Array::linspace(0., 127., 256)
let a = Array::linspace(0.0..=127.0, 256)
.into_shape_with_order((8, 32))
.unwrap();
let av = a.slice(s![.., ..;2]);
Expand Down
10 changes: 5 additions & 5 deletions benches/higher-order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Y: usize = 16;
#[bench]
fn map_regular(bench: &mut Bencher)
{
let a = Array::linspace(0., 127., N)
let a = Array::linspace(0.0..=127.0, N)
.into_shape_with_order((X, Y))
.unwrap();
bench.iter(|| a.map(|&x| 2. * x));
Expand All @@ -29,7 +29,7 @@ pub fn double_array(mut a: ArrayViewMut2<'_, f64>)
#[bench]
fn map_stride_double_f64(bench: &mut Bencher)
{
let mut a = Array::linspace(0., 127., N * 2)
let mut a = Array::linspace(0.0..=127.0, N * 2)
.into_shape_with_order([X, Y * 2])
.unwrap();
let mut av = a.slice_mut(s![.., ..;2]);
Expand All @@ -42,7 +42,7 @@ fn map_stride_double_f64(bench: &mut Bencher)
#[bench]
fn map_stride_f64(bench: &mut Bencher)
{
let a = Array::linspace(0., 127., N * 2)
let a = Array::linspace(0.0..=127.0, N * 2)
.into_shape_with_order([X, Y * 2])
.unwrap();
let av = a.slice(s![.., ..;2]);
Expand All @@ -53,7 +53,7 @@ fn map_stride_f64(bench: &mut Bencher)
#[bench]
fn map_stride_u32(bench: &mut Bencher)
{
let a = Array::linspace(0., 127., N * 2)
let a = Array::linspace(0.0..=127.0, N * 2)
.into_shape_with_order([X, Y * 2])
.unwrap();
let b = a.mapv(|x| x as u32);
Expand All @@ -65,7 +65,7 @@ fn map_stride_u32(bench: &mut Bencher)
#[bench]
fn fold_axis(bench: &mut Bencher)
{
let a = Array::linspace(0., 127., N * 2)
let a = Array::linspace(0.0..=127.0, N * 2)
.into_shape_with_order([X, Y * 2])
.unwrap();
bench.iter(|| a.fold_axis(Axis(0), 0., |&acc, &elt| acc + elt));
Expand Down
12 changes: 6 additions & 6 deletions benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn iter_sum_2d_transpose(bench: &mut Bencher)
#[bench]
fn iter_filter_sum_2d_u32(bench: &mut Bencher)
{
let a = Array::linspace(0., 1., 256)
let a = Array::linspace(0.0..=1.0, 256)
.into_shape_with_order((16, 16))
.unwrap();
let b = a.mapv(|x| (x * 100.) as u32);
Expand All @@ -58,7 +58,7 @@ fn iter_filter_sum_2d_u32(bench: &mut Bencher)
#[bench]
fn iter_filter_sum_2d_f32(bench: &mut Bencher)
{
let a = Array::linspace(0., 1., 256)
let a = Array::linspace(0.0..=1.0, 256)
.into_shape_with_order((16, 16))
.unwrap();
let b = a * 100.;
Expand All @@ -69,7 +69,7 @@ fn iter_filter_sum_2d_f32(bench: &mut Bencher)
#[bench]
fn iter_filter_sum_2d_stride_u32(bench: &mut Bencher)
{
let a = Array::linspace(0., 1., 256)
let a = Array::linspace(0.0..=1.0, 256)
.into_shape_with_order((16, 16))
.unwrap();
let b = a.mapv(|x| (x * 100.) as u32);
Expand All @@ -81,7 +81,7 @@ fn iter_filter_sum_2d_stride_u32(bench: &mut Bencher)
#[bench]
fn iter_filter_sum_2d_stride_f32(bench: &mut Bencher)
{
let a = Array::linspace(0., 1., 256)
let a = Array::linspace(0.0..=1.0, 256)
.into_shape_with_order((16, 16))
.unwrap();
let b = a * 100.;
Expand All @@ -93,7 +93,7 @@ fn iter_filter_sum_2d_stride_f32(bench: &mut Bencher)
#[bench]
fn iter_rev_step_by_contiguous(bench: &mut Bencher)
{
let a = Array::linspace(0., 1., 512);
let a = Array::linspace(0.0..=1.0, 512);
bench.iter(|| {
a.iter().rev().step_by(2).for_each(|x| {
black_box(x);
Expand All @@ -105,7 +105,7 @@ fn iter_rev_step_by_contiguous(bench: &mut Bencher)
#[bench]
fn iter_rev_step_by_discontiguous(bench: &mut Bencher)
{
let mut a = Array::linspace(0., 1., 1024);
let mut a = Array::linspace(0.0..=1.0, 1024);
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
bench.iter(|| {
a.iter().rev().step_by(2).for_each(|x| {
Expand Down
2 changes: 1 addition & 1 deletion benches/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Y: usize = 16;
#[bench]
fn clip(bench: &mut Bencher)
{
let mut a = Array::linspace(0., 127., N * 2)
let mut a = Array::linspace(0.0..=127.0, N * 2)
.into_shape_with_order([X, Y * 2])
.unwrap();
let min = 2.;
Expand Down
2 changes: 1 addition & 1 deletion examples/sort-axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ where D: Dimension
#[cfg(feature = "std")]
fn main()
{
let a = Array::linspace(0., 63., 64)
let a = Array::linspace(0.0..=63.0, 64)
.into_shape_with_order((8, 8))
.unwrap();
let strings = a.map(|x| x.to_string());
Expand Down
4 changes: 2 additions & 2 deletions src/doc/ndarray_for_numpy_users/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@
//! ------|-----------|------
//! `np.array([[1.,2.,3.], [4.,5.,6.]])` | [`array![[1.,2.,3.], [4.,5.,6.]]`][array!] or [`arr2(&[[1.,2.,3.], [4.,5.,6.]])`][arr2()] | 2×3 floating-point array literal
//! `np.arange(0., 10., 0.5)` or `np.r_[:10.:0.5]` | [`Array::range(0., 10., 0.5)`][::range()] | create a 1-D array with values `0.`, `0.5`, …, `9.5`
//! `np.linspace(0., 10., 11)` or `np.r_[:10.:11j]` | [`Array::linspace(0., 10., 11)`][::linspace()] | create a 1-D array with 11 elements with values `0.`, …, `10.`
//! `np.logspace(2.0, 3.0, num=4, base=10.0)` | [`Array::logspace(10.0, 2.0, 3.0, 4)`][::logspace()] | create a 1-D array with 4 elements with values `100.`, `215.4`, `464.1`, `1000.`
//! `np.linspace(0., 10., 11)` or `np.r_[:10.:11j]` | [`Array::linspace(0.0..=10.0, 11)`][::linspace()] | create a 1-D array with 11 elements with values `0.`, …, `10.`
//! `np.logspace(2.0, 3.0, num=4, base=10.0)` | [`Array::logspace(10.0, 2.0..=3.0, 4)`][::logspace()] | create a 1-D array with 4 elements with values `100.`, `215.4`, `464.1`, `1000.`
//! `np.geomspace(1., 1000., num=4)` | [`Array::geomspace(1e0, 1e3, 4)`][::geomspace()] | create a 1-D array with 4 elements with values `1.`, `10.`, `100.`, `1000.`
//! `np.ones((3, 4, 5))` | [`Array::ones((3, 4, 5))`][::ones()] | create a 3×4×5 array filled with ones (inferring the element type)
//! `np.zeros((3, 4, 5))` | [`Array::zeros((3, 4, 5))`][::zeros()] | create a 3×4×5 array filled with zeros (inferring the element type)
Expand Down
42 changes: 42 additions & 0 deletions src/finite_bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use num_traits::Float;

pub enum Bound<F>
{
Included(F),
Excluded(F),
}

/// A version of std::ops::RangeBounds that only implements a..b and a..=b ranges.
pub trait FiniteBounds<F>
{
fn start_bound(&self) -> F;
fn end_bound(&self) -> Bound<F>;
}

impl<F> FiniteBounds<F> for std::ops::Range<F>
where F: Float
{
fn start_bound(&self) -> F
{
self.start
}

fn end_bound(&self) -> Bound<F>
{
Bound::Excluded(self.end)
}
}

impl<F> FiniteBounds<F> for std::ops::RangeInclusive<F>
where F: Float
{
fn start_bound(&self) -> F
{
*self.start()
}

fn end_bound(&self) -> Bound<F>
{
Bound::Included(*self.end())
}
}
27 changes: 14 additions & 13 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ where S: DataOwned<Elem = A>
pub fn from_vec(v: Vec<A>) -> Self
{
if mem::size_of::<A>() == 0 {
assert!(
v.len() <= isize::MAX as usize,
"Length must fit in `isize`.",
);
assert!(v.len() <= isize::MAX as usize, "Length must fit in `isize`.",);
}
unsafe { Self::from_shape_vec_unchecked(v.len() as Ix, v) }
}
Expand Down Expand Up @@ -95,14 +92,16 @@ where S: DataOwned<Elem = A>
/// ```rust
/// use ndarray::{Array, arr1};
///
/// let array = Array::linspace(0., 1., 5);
/// let array = Array::linspace(0.0..=1.0, 5);
/// assert!(array == arr1(&[0.0, 0.25, 0.5, 0.75, 1.0]))
/// ```
#[cfg(feature = "std")]
pub fn linspace(start: A, end: A, n: usize) -> Self
where A: Float
pub fn linspace<R>(range: R, n: usize) -> Self
where
R: crate::finite_bounds::FiniteBounds<A>,
A: Float,
{
Self::from(to_vec(linspace::linspace(start, end, n)))
Self::from(to_vec(linspace::linspace(range, n)))
}

/// Create a one-dimensional array with elements from `start` to `end`
Expand Down Expand Up @@ -137,18 +136,20 @@ where S: DataOwned<Elem = A>
/// use approx::assert_abs_diff_eq;
/// use ndarray::{Array, arr1};
///
/// let array = Array::logspace(10.0, 0.0, 3.0, 4);
/// let array = Array::logspace(10.0, 0.0..=3.0, 4);
/// assert_abs_diff_eq!(array, arr1(&[1e0, 1e1, 1e2, 1e3]), epsilon = 1e-12);
///
/// let array = Array::logspace(-10.0, 3.0, 0.0, 4);
/// let array = Array::logspace(-10.0, 3.0..=0.0, 4);
/// assert_abs_diff_eq!(array, arr1(&[-1e3, -1e2, -1e1, -1e0]), epsilon = 1e-12);
/// # }
/// ```
#[cfg(feature = "std")]
pub fn logspace(base: A, start: A, end: A, n: usize) -> Self
where A: Float
pub fn logspace<R>(base: A, range: R, n: usize) -> Self
where
R: crate::finite_bounds::FiniteBounds<A>,
A: Float,
{
Self::from(to_vec(logspace::logspace(base, start, end, n)))
Self::from(to_vec(logspace::logspace(base, range, n)))
}

/// Create a one-dimensional array with `n` geometrically spaced elements
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ mod indexes;
mod iterators;
mod layout;
mod linalg_traits;
#[cfg(feature = "std")]
mod finite_bounds;
mod linspace;
#[cfg(feature = "std")]
pub use crate::linspace::{linspace, range, Linspace};
Expand Down
20 changes: 15 additions & 5 deletions src/linspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![cfg(feature = "std")]

use crate::finite_bounds::{Bound, FiniteBounds};

use num_traits::Float;

/// An iterator of a sequence of evenly spaced floats.
Expand Down Expand Up @@ -71,17 +74,24 @@ impl<F> ExactSizeIterator for Linspace<F> where Linspace<F>: Iterator {}
/// The iterator element type is `F`, where `F` must implement [`Float`], e.g.
/// [`f32`] or [`f64`].
///
/// **Panics** if converting `n - 1` to type `F` fails.
/// **Panics** if converting `n` to type `F` fails.
#[inline]
pub fn linspace<F>(a: F, b: F, n: usize) -> Linspace<F>
where F: Float
pub fn linspace<R, F>(range: R, n: usize) -> Linspace<F>
where
R: FiniteBounds<F>,
F: Float,
{
let step = if n > 1 {
let num_steps = F::from(n - 1).expect("Converting number of steps to `A` must not fail.");
let (a, b, num_steps) = match (range.start_bound(), range.end_bound()) {
(a, Bound::Included(b)) => (a, b, F::from(n - 1).expect("Converting number of steps to `A` must not fail.")),
(a, Bound::Excluded(b)) => (a, b, F::from(n).expect("Converting number of steps to `A` must not fail.")),
};

let step = if num_steps > F::zero() {
(b - a) / num_steps
} else {
F::zero()
};

Linspace {
start: a,
step,
Expand Down
30 changes: 20 additions & 10 deletions src/logspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![cfg(feature = "std")]

use crate::finite_bounds::{Bound, FiniteBounds};

use num_traits::Float;

/// An iterator of a sequence of logarithmically spaced number.
Expand Down Expand Up @@ -79,15 +82,22 @@ impl<F> ExactSizeIterator for Logspace<F> where Logspace<F>: Iterator {}
///
/// **Panics** if converting `n - 1` to type `F` fails.
#[inline]
pub fn logspace<F>(base: F, a: F, b: F, n: usize) -> Logspace<F>
where F: Float
pub fn logspace<R, F>(base: F, range: R, n: usize) -> Logspace<F>
where
R: FiniteBounds<F>,
F: Float,
{
let step = if n > 1 {
let num_steps = F::from(n - 1).expect("Converting number of steps to `A` must not fail.");
let (a, b, num_steps) = match (range.start_bound(), range.end_bound()) {
(a, Bound::Included(b)) => (a, b, F::from(n - 1).expect("Converting number of steps to `A` must not fail.")),
(a, Bound::Excluded(b)) => (a, b, F::from(n).expect("Converting number of steps to `A` must not fail.")),
};

let step = if num_steps > F::zero() {
(b - a) / num_steps
} else {
F::zero()
};

Logspace {
sign: base.signum(),
base: base.abs(),
Expand All @@ -110,23 +120,23 @@ mod tests
use crate::{arr1, Array1};
use approx::assert_abs_diff_eq;

let array: Array1<_> = logspace(10.0, 0.0, 3.0, 4).collect();
let array: Array1<_> = logspace(10.0, 0.0..=3.0, 4).collect();
assert_abs_diff_eq!(array, arr1(&[1e0, 1e1, 1e2, 1e3]), epsilon = 1e-12);

let array: Array1<_> = logspace(10.0, 3.0, 0.0, 4).collect();
let array: Array1<_> = logspace(10.0, 3.0..=0.0, 4).collect();
assert_abs_diff_eq!(array, arr1(&[1e3, 1e2, 1e1, 1e0]), epsilon = 1e-12);

let array: Array1<_> = logspace(-10.0, 3.0, 0.0, 4).collect();
let array: Array1<_> = logspace(-10.0, 3.0..=0.0, 4).collect();
assert_abs_diff_eq!(array, arr1(&[-1e3, -1e2, -1e1, -1e0]), epsilon = 1e-12);

let array: Array1<_> = logspace(-10.0, 0.0, 3.0, 4).collect();
let array: Array1<_> = logspace(-10.0, 0.0..=3.0, 4).collect();
assert_abs_diff_eq!(array, arr1(&[-1e0, -1e1, -1e2, -1e3]), epsilon = 1e-12);
}

#[test]
fn iter_forward()
{
let mut iter = logspace(10.0f64, 0.0, 3.0, 4);
let mut iter = logspace(10.0f64, 0.0..=3.0, 4);

assert!(iter.size_hint() == (4, Some(4)));

Expand All @@ -142,7 +152,7 @@ mod tests
#[test]
fn iter_backward()
{
let mut iter = logspace(10.0f64, 0.0, 3.0, 4);
let mut iter = logspace(10.0f64, 0.0..=3.0, 4);

assert!(iter.size_hint() == (4, Some(4)));

Expand Down
Loading