Skip to content

Commit 3b0dfe4

Browse files
committed
Merge pull request #62 from bluss/linspace
Use internal copy of linspace
2 parents ee6e656 + fc0846d commit 3b0dfe4

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extern crate serde;
5656
#[cfg(feature = "rustc-serialize")]
5757
extern crate rustc_serialize as serialize;
5858

59-
extern crate itertools as it;
59+
extern crate itertools;
6060
extern crate num as libnum;
6161

6262
use libnum::Float;
@@ -73,7 +73,7 @@ use std::rc::Rc;
7373
use std::slice::{self, Iter, IterMut};
7474
use std::marker::PhantomData;
7575

76-
use it::ZipSlices;
76+
use itertools::ZipSlices;
7777

7878
pub use dimension::{Dimension, RemoveAxis};
7979
pub use dimension::NdIndex;
@@ -103,6 +103,7 @@ pub mod blas;
103103
mod dimension;
104104
mod indexes;
105105
mod iterators;
106+
mod linspace;
106107
mod numeric_util;
107108
mod si;
108109
mod shape_error;
@@ -544,9 +545,8 @@ impl<S> ArrayBase<S, Ix>
544545
pub fn linspace<F>(start: F, end: F, n: usize) -> ArrayBase<S, Ix>
545546
where S: Data<Elem=F>,
546547
F: libnum::Float,
547-
usize: it::misc::ToFloat<F>,
548548
{
549-
Self::from_iter(it::linspace(start, end, n))
549+
Self::from_iter(linspace::linspace(start, end, n))
550550
}
551551

552552
/// Create a one-dimensional array from interval `[start, end)`
@@ -2783,7 +2783,7 @@ pub struct Indexed<'a, A: 'a, D>(ElementsBase<'a, A, D>);
27832783
pub struct IndexedMut<'a, A: 'a, D>(ElementsBaseMut<'a, A, D>);
27842784

27852785
fn zipsl<T, U>(t: T, u: U) -> ZipSlices<T, U>
2786-
where T: it::misc::Slice, U: it::misc::Slice
2786+
where T: itertools::misc::Slice, U: itertools::misc::Slice
27872787
{
27882788
ZipSlices::from_slices(t, u)
27892789
}

src/linspace.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use libnum::Float;
2+
3+
/// An iterator of a sequence of evenly spaced floats.
4+
///
5+
/// Iterator element type is `F`.
6+
pub struct Linspace<F> {
7+
start: F,
8+
step: F,
9+
index: usize,
10+
len: usize,
11+
}
12+
13+
impl<F> Iterator for Linspace<F>
14+
where F: Float,
15+
{
16+
type Item = F;
17+
18+
#[inline]
19+
fn next(&mut self) -> Option<F> {
20+
if self.index >= self.len {
21+
None
22+
} else {
23+
// Calculate the value just like numpy.linspace does
24+
let i = self.index;
25+
self.index += 1;
26+
Some(self.start + self.step * F::from(i).unwrap())
27+
}
28+
}
29+
30+
#[inline]
31+
fn size_hint(&self) -> (usize, Option<usize>) {
32+
let n = self.len - self.index;
33+
(n, Some(n))
34+
}
35+
}
36+
37+
impl<F> DoubleEndedIterator for Linspace<F>
38+
where F: Float,
39+
{
40+
#[inline]
41+
fn next_back(&mut self) -> Option<F> {
42+
if self.index >= self.len {
43+
None
44+
} else {
45+
// Calculate the value just like numpy.linspace does
46+
self.len -= 1;
47+
let i = self.len;
48+
Some(self.start + self.step * F::from(i).unwrap())
49+
}
50+
}
51+
}
52+
53+
impl<F> ExactSizeIterator for Linspace<F> where Linspace<F>: Iterator { }
54+
55+
/// Return an iterator of evenly spaced floats.
56+
///
57+
/// The `Linspace` has `n` elements, where the first
58+
/// element is `a` and the last element is `b`.
59+
///
60+
/// Iterator element type is `F`, where `F` must be
61+
/// either `f32` or `f64`.
62+
#[inline]
63+
pub fn linspace<F>(a: F, b: F, n: usize) -> Linspace<F>
64+
where F: Float,
65+
{
66+
let step = if n > 1 {
67+
let nf: F = F::from(n).unwrap();
68+
(b - a)/(nf - F::one())
69+
} else {
70+
F::zero()
71+
};
72+
Linspace {
73+
start: a,
74+
step: step,
75+
index: 0,
76+
len: n,
77+
}
78+
}

0 commit comments

Comments
 (0)