ndarray/
linspace.rs

1// Copyright 2014-2016 bluss and ndarray developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8#![cfg(feature = "std")]
9use num_traits::Float;
10
11/// An iterator of a sequence of evenly spaced floats.
12///
13/// Iterator element type is `F`.
14pub struct Linspace<F> {
15    start: F,
16    step: F,
17    index: usize,
18    len: usize,
19}
20
21impl<F> Iterator for Linspace<F>
22where
23    F: Float,
24{
25    type Item = F;
26
27    #[inline]
28    fn next(&mut self) -> Option<F> {
29        if self.index >= self.len {
30            None
31        } else {
32            // Calculate the value just like numpy.linspace does
33            let i = self.index;
34            self.index += 1;
35            Some(self.start + self.step * F::from(i).unwrap())
36        }
37    }
38
39    #[inline]
40    fn size_hint(&self) -> (usize, Option<usize>) {
41        let n = self.len - self.index;
42        (n, Some(n))
43    }
44}
45
46impl<F> DoubleEndedIterator for Linspace<F>
47where
48    F: Float,
49{
50    #[inline]
51    fn next_back(&mut self) -> Option<F> {
52        if self.index >= self.len {
53            None
54        } else {
55            // Calculate the value just like numpy.linspace does
56            self.len -= 1;
57            let i = self.len;
58            Some(self.start + self.step * F::from(i).unwrap())
59        }
60    }
61}
62
63impl<F> ExactSizeIterator for Linspace<F> where Linspace<F>: Iterator {}
64
65/// Return an iterator of evenly spaced floats.
66///
67/// The `Linspace` has `n` elements from `a` to `b` (inclusive).
68///
69/// The iterator element type is `F`, where `F` must implement `Float`, e.g.
70/// `f32` or `f64`.
71///
72/// **Panics** if converting `n - 1` to type `F` fails.
73#[inline]
74pub fn linspace<F>(a: F, b: F, n: usize) -> Linspace<F>
75where
76    F: Float,
77{
78    let step = if n > 1 {
79        let num_steps = F::from(n - 1).expect("Converting number of steps to `A` must not fail.");
80        (b - a) / num_steps
81    } else {
82        F::zero()
83    };
84    Linspace {
85        start: a,
86        step,
87        index: 0,
88        len: n,
89    }
90}
91
92/// Return an iterator of floats from `start` to `end` (exclusive),
93/// incrementing by `step`.
94///
95/// Numerical reasons can result in `b` being included in the result.
96///
97/// The iterator element type is `F`, where `F` must implement `Float`, e.g.
98/// `f32` or `f64`.
99///
100/// **Panics** if converting `((b - a) / step).ceil()` to type `F` fails.
101#[inline]
102pub fn range<F>(a: F, b: F, step: F) -> Linspace<F>
103where
104    F: Float,
105{
106    let len = b - a;
107    let steps = F::ceil(len / step);
108    Linspace {
109        start: a,
110        step,
111        len: steps.to_usize().expect(
112            "Converting the length to `usize` must not fail. The most likely \
113             cause of this failure is if the sign of `end - start` is \
114             different from the sign of `step`.",
115        ),
116        index: 0,
117    }
118}