FIX: fix assertion in seq and implement logspace
@@ -29,14 +29,14 @@ where
let e = end.into();
let step = step.into();
- assert!(e > s);
+ assert!(e >= s);
let factor: f64 = (e - s) / step;
let l: usize = factor as usize + 1;
- let mut v: Vec<f64> = Vec::new();
+ let mut v: Vec<f64> = vec![0f64; l];
for i in 0..l {
- v.push(s + step * (i as f64));
+ v[i] = s + step * (i as f64);
}
v
}
@@ -123,6 +123,41 @@ where
seq(start, end, step)
}
+/// MATLAB like logspace
+///
+/// # Examples
+/// ```
+/// extern crate peroxide;
+/// use peroxide::fuga::*;
+///
+/// fn main() {
+/// let a = logspace(1, 1024, 11, 2);
+/// let b = vec![1f64, 2f64, 4f64, 8f64, 16f64, 32f64, 64f64, 128f64, 256f64, 512f64, 1024f64];
+/// assert_eq!(a, b);
+/// }
+/// ```
+pub fn logspace<S, T, U>(start: S, end: T, length: usize, base: U) -> Vec<f64>
+where
+ S: Into<f64> + Copy,
+ T: Into<f64> + Copy,
+ U: Into<f64> + Copy,
+{
+ let s: f64 = start.into();
+ let e: f64 = end.into();
+ let b: f64 = base.into();
+
+ assert!(e >= s);
+
+ let step: f64 = (e.log(b) - s.log(b)) / (length as f64 - 1f64);
+
+ let mut v: Vec<f64> = vec![0f64; length];
+
+ for i in 0..length {
+ v[i] = s * b.powf(step * (i as f64));
+ }
+ v
+}
+
/// R like cbind - concatenate two matrix by column direction
///
/// # Examples
