Skip to content

Commit 0295d46

Browse files
committed
Improve docs of linspace, range, logspace, geomspace
This includes a few corrections and clarifications: * The old docs used interval notation in some cases, which was confusing in the case that `start > end`. * `linspace` now has a note describing the difference in behavior from `std::ops::RangeInclusive` for the case that `start > end`. * Panics in conversions from `usize` to `A` are now mentioned. * The docs now say that the element type must implement `Float` instead of saying that it must be `f32` or `f64`.
1 parent 95ce9e7 commit 0295d46

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

src/geomspace.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@ impl<F> ExactSizeIterator for Geomspace<F> where Geomspace<F>: Iterator {}
6666

6767
/// An iterator of a sequence of geometrically spaced values.
6868
///
69-
/// The `Geomspace` has `n` elements, where the first element is `a` and the
70-
/// last element is `b`.
69+
/// The `Geomspace` has `n` geometrically spaced elements from `start` to `end`
70+
/// (inclusive).
7171
///
72-
/// Iterator element type is `F`, where `F` must be either `f32` or `f64`.
72+
/// The iterator element type is `F`, where `F` must implement `Float`, e.g.
73+
/// `f32` or `f64`.
7374
///
7475
/// Returns `None` if `start` and `end` have different signs or if either one
7576
/// is zero. Conceptually, this means that in order to obtain a `Some` result,
7677
/// `end / start` must be positive.
78+
///
79+
/// **Panics** if converting `n - 1` to type `F` fails.
7780
#[inline]
7881
pub fn geomspace<F>(a: F, b: F, n: usize) -> Option<Geomspace<F>>
7982
where

src/impl_constructors.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ impl<S, A> ArrayBase<S, Ix1>
6767
Self::from_vec(iterable.into_iter().collect())
6868
}
6969

70-
/// Create a one-dimensional array from the inclusive interval
71-
/// `[start, end]` with `n` elements. `A` must be a floating point type.
70+
/// Create a one-dimensional array with `n` evenly spaced elements from
71+
/// `start` to `end` (inclusive). `A` must be a floating point type.
7272
///
73-
/// **Panics** if `n` is greater than `isize::MAX`.
73+
/// Note that if `start > end`, the first element will still be `start`,
74+
/// and the following elements will be decreasing. This is different from
75+
/// the behavior of `std::ops::RangeInclusive`, which interprets `start >
76+
/// end` to mean that the range is empty.
77+
///
78+
/// **Panics** if `n` is greater than `isize::MAX` or if converting `n - 1`
79+
/// to type `A` fails.
7480
///
7581
/// ```rust
7682
/// use ndarray::{Array, arr1};
@@ -84,9 +90,8 @@ impl<S, A> ArrayBase<S, Ix1>
8490
Self::from_vec(to_vec(linspace::linspace(start, end, n)))
8591
}
8692

87-
/// Create a one-dimensional array from the half-open interval
88-
/// `[start, end)` with elements spaced by `step`. `A` must be a floating
89-
/// point type.
93+
/// Create a one-dimensional array with elements from `start` to `end`
94+
/// (exclusive), incrementing by `step`. `A` must be a floating point type.
9095
///
9196
/// **Panics** if the length is greater than `isize::MAX`.
9297
///
@@ -102,13 +107,14 @@ impl<S, A> ArrayBase<S, Ix1>
102107
Self::from_vec(to_vec(linspace::range(start, end, step)))
103108
}
104109

105-
/// Create a one-dimensional array with `n` elements logarithmically spaced,
106-
/// with the starting value being `base.powf(start)` and the final one being
107-
/// `base.powf(end)`. `A` must be a floating point type.
110+
/// Create a one-dimensional array with `n` logarithmically spaced
111+
/// elements, with the starting value being `base.powf(start)` and the
112+
/// final one being `base.powf(end)`. `A` must be a floating point type.
108113
///
109114
/// If `base` is negative, all values will be negative.
110115
///
111-
/// **Panics** if the length is greater than `isize::MAX`.
116+
/// **Panics** if `n` is greater than `isize::MAX` or if converting `n - 1`
117+
/// to type `A` fails.
112118
///
113119
/// ```rust
114120
/// use approx::assert_abs_diff_eq;
@@ -129,15 +135,15 @@ impl<S, A> ArrayBase<S, Ix1>
129135
Self::from_vec(to_vec(logspace::logspace(base, start, end, n)))
130136
}
131137

132-
/// Create a one-dimensional array from the inclusive interval `[start,
133-
/// end]` with `n` elements geometrically spaced. `A` must be a floating
134-
/// point type.
138+
/// Create a one-dimensional array with `n` geometrically spaced elements
139+
/// from `start` to `end` (inclusive). `A` must be a floating point type.
135140
///
136141
/// Returns `None` if `start` and `end` have different signs or if either
137142
/// one is zero. Conceptually, this means that in order to obtain a `Some`
138143
/// result, `end / start` must be positive.
139144
///
140-
/// **Panics** if `n` is greater than `isize::MAX`.
145+
/// **Panics** if `n` is greater than `isize::MAX` or if converting `n - 1`
146+
/// to type `A` fails.
141147
///
142148
/// ```rust
143149
/// use approx::assert_abs_diff_eq;

src/linspace.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ impl<F> ExactSizeIterator for Linspace<F>
6363

6464
/// Return an iterator of evenly spaced floats.
6565
///
66-
/// The `Linspace` has `n` elements, where the first
67-
/// element is `a` and the last element is `b`.
66+
/// The `Linspace` has `n` elements from `a` to `b` (inclusive).
6867
///
69-
/// Iterator element type is `F`, where `F` must be
70-
/// either `f32` or `f64`.
68+
/// The iterator element type is `F`, where `F` must implement `Float`, e.g.
69+
/// `f32` or `f64`.
70+
///
71+
/// **Panics** if converting `n - 1` to type `F` fails.
7172
#[inline]
7273
pub fn linspace<F>(a: F, b: F, n: usize) -> Linspace<F>
7374
where F: Float
@@ -86,13 +87,15 @@ pub fn linspace<F>(a: F, b: F, n: usize) -> Linspace<F>
8687
}
8788
}
8889

89-
/// Return an iterator of floats spaced by `step`, from
90-
/// the half-open interval [a, b).
91-
/// Numerical reasons can result in `b` being included
92-
/// in the result.
90+
/// Return an iterator of floats from `start` to `end` (exclusive),
91+
/// incrementing by `step`.
92+
///
93+
/// Numerical reasons can result in `b` being included in the result.
94+
///
95+
/// The iterator element type is `F`, where `F` must implement `Float`, e.g.
96+
/// `f32` or `f64`.
9397
///
94-
/// Iterator element type is `F`, where `F` must be
95-
/// either `f32` or `f64`.
98+
/// **Panics** if converting `((b - a) / step).ceil()` to type `F` fails.
9699
#[inline]
97100
pub fn range<F>(a: F, b: F, step: F) -> Linspace<F>
98101
where F: Float
@@ -102,7 +105,11 @@ pub fn range<F>(a: F, b: F, step: F) -> Linspace<F>
102105
Linspace {
103106
start: a,
104107
step: step,
105-
len: steps.to_usize().unwrap(),
108+
len: steps.to_usize().expect(
109+
"Converting the length to `usize` must not fail. The most likely \
110+
cause of this failure is if the sign of `end - start` is \
111+
different from the sign of `step`.",
112+
),
106113
index: 0,
107114
}
108115
}

src/logspace.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ where
6565

6666
impl<F> ExactSizeIterator for Logspace<F> where Logspace<F>: Iterator {}
6767

68-
/// An iterator of a sequence of logarithmically spaced number.
68+
/// An iterator of a sequence of logarithmically spaced numbers.
6969
///
7070
/// The `Logspace` has `n` elements, where the first element is `base.powf(a)`
7171
/// and the last element is `base.powf(b)`. If `base` is negative, this
7272
/// iterator will return all negative values.
7373
///
74-
/// Iterator element type is `F`, where `F` must be either `f32` or `f64`.
74+
/// The iterator element type is `F`, where `F` must implement `Float`, e.g.
75+
/// `f32` or `f64`.
76+
///
77+
/// **Panics** if converting `n - 1` to type `F` fails.
7578
#[inline]
7679
pub fn logspace<F>(base: F, a: F, b: F, n: usize) -> Logspace<F>
7780
where

0 commit comments

Comments
 (0)