Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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.
  • Loading branch information
Kwonunn committed Jan 27, 2026
commit dc833d8bab93a060dead7e5004aba1428daa65cd
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ mod layout;
mod linalg_traits;
mod linspace;
#[cfg(feature = "std")]
pub use crate::linspace::{linspace, range, Linspace};
pub use crate::linspace::{linspace, linspace_exclusive, range, Linspace};
mod logspace;
#[cfg(feature = "std")]
pub use crate::logspace::{logspace, Logspace};
Expand Down
26 changes: 26 additions & 0 deletions src/linspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ where F: Float
}
}

/// Return an iterator of evenly spaced floats.
///
/// The `Linspace` has `n` elements from `a` to `b` (exclusive).
///
/// The iterator element type is `F`, where `F` must implement [`Float`], e.g.
/// [`f32`] or [`f64`].
///
/// **Panics** if converting `n` to type `F` fails.
#[inline]
pub fn linspace_exclusive<F>(a: F, b: F, n: usize) -> Linspace<F>
where F: Float
{
let step = if n > 1 {
let num_steps = F::from(n).expect("Converting number of steps to `A` must not fail.");
(b - a) / num_steps
} else {
F::zero()
};
Linspace {
start: a,
step,
index: 0,
len: n,
}
}

/// Return an iterator of floats from `a` to `b` (exclusive),
/// incrementing by `step`.
///
Expand Down