Skip to content

Commit dc833d8

Browse files
committed
Add linspace_exclusive function.
Add a `linspace_exclusive` function which creates a `Linspace` iterator which is exclusive at its upper bound. This is essentially the same as `linspace(endpoint=False)` in numpy.
1 parent f6f06ce commit dc833d8

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ mod layout;
201201
mod linalg_traits;
202202
mod linspace;
203203
#[cfg(feature = "std")]
204-
pub use crate::linspace::{linspace, range, Linspace};
204+
pub use crate::linspace::{linspace, linspace_exclusive, range, Linspace};
205205
mod logspace;
206206
#[cfg(feature = "std")]
207207
pub use crate::logspace::{logspace, Logspace};

src/linspace.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,32 @@ where F: Float
9090
}
9191
}
9292

93+
/// Return an iterator of evenly spaced floats.
94+
///
95+
/// The `Linspace` has `n` elements from `a` to `b` (exclusive).
96+
///
97+
/// The iterator element type is `F`, where `F` must implement [`Float`], e.g.
98+
/// [`f32`] or [`f64`].
99+
///
100+
/// **Panics** if converting `n` to type `F` fails.
101+
#[inline]
102+
pub fn linspace_exclusive<F>(a: F, b: F, n: usize) -> Linspace<F>
103+
where F: Float
104+
{
105+
let step = if n > 1 {
106+
let num_steps = F::from(n).expect("Converting number of steps to `A` must not fail.");
107+
(b - a) / num_steps
108+
} else {
109+
F::zero()
110+
};
111+
Linspace {
112+
start: a,
113+
step,
114+
index: 0,
115+
len: n,
116+
}
117+
}
118+
93119
/// Return an iterator of floats from `a` to `b` (exclusive),
94120
/// incrementing by `step`.
95121
///

0 commit comments

Comments
 (0)