Skip to content

Use internal copy of linspace #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extern crate serde;
#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize as serialize;

extern crate itertools as it;
extern crate itertools;
extern crate num as libnum;

use libnum::Float;
Expand All @@ -73,7 +73,7 @@ use std::rc::Rc;
use std::slice::{self, Iter, IterMut};
use std::marker::PhantomData;

use it::ZipSlices;
use itertools::ZipSlices;

pub use dimension::{Dimension, RemoveAxis};
pub use dimension::NdIndex;
Expand Down Expand Up @@ -103,6 +103,7 @@ pub mod blas;
mod dimension;
mod indexes;
mod iterators;
mod linspace;
mod numeric_util;
mod si;
mod shape_error;
Expand Down Expand Up @@ -544,9 +545,8 @@ impl<S> ArrayBase<S, Ix>
pub fn linspace<F>(start: F, end: F, n: usize) -> ArrayBase<S, Ix>
where S: Data<Elem=F>,
F: libnum::Float,
usize: it::misc::ToFloat<F>,
{
Self::from_iter(it::linspace(start, end, n))
Self::from_iter(linspace::linspace(start, end, n))
}

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

fn zipsl<T, U>(t: T, u: U) -> ZipSlices<T, U>
where T: it::misc::Slice, U: it::misc::Slice
where T: itertools::misc::Slice, U: itertools::misc::Slice
{
ZipSlices::from_slices(t, u)
}
Expand Down
78 changes: 78 additions & 0 deletions src/linspace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use libnum::Float;

/// An iterator of a sequence of evenly spaced floats.
///
/// Iterator element type is `F`.
pub struct Linspace<F> {
start: F,
step: F,
index: usize,
len: usize,
}

impl<F> Iterator for Linspace<F>
where F: Float,
{
type Item = F;

#[inline]
fn next(&mut self) -> Option<F> {
if self.index >= self.len {
None
} else {
// Calculate the value just like numpy.linspace does
let i = self.index;
self.index += 1;
Some(self.start + self.step * F::from(i).unwrap())
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let n = self.len - self.index;
(n, Some(n))
}
}

impl<F> DoubleEndedIterator for Linspace<F>
where F: Float,
{
#[inline]
fn next_back(&mut self) -> Option<F> {
if self.index >= self.len {
None
} else {
// Calculate the value just like numpy.linspace does
self.len -= 1;
let i = self.len;
Some(self.start + self.step * F::from(i).unwrap())
}
}
}

impl<F> ExactSizeIterator for Linspace<F> where Linspace<F>: Iterator { }

/// Return an iterator of evenly spaced floats.
///
/// The `Linspace` has `n` elements, where the first
/// element is `a` and the last element is `b`.
///
/// Iterator element type is `F`, where `F` must be
/// either `f32` or `f64`.
#[inline]
pub fn linspace<F>(a: F, b: F, n: usize) -> Linspace<F>
where F: Float,
{
let step = if n > 1 {
let nf: F = F::from(n).unwrap();
(b - a)/(nf - F::one())
} else {
F::zero()
};
Linspace {
start: a,
step: step,
index: 0,
len: n,
}
}