Implement abstract method to convert a slice to a BaseVector, Implement RealNumberVector over BaseVector instead of over Vec<T>

This commit is contained in:
Luis Moreno
2020-11-11 20:53:50 -04:00
parent 82464f41e4
commit 900078cb04
3 changed files with 22 additions and 9 deletions
+15
View File
@@ -83,6 +83,21 @@ pub trait BaseVector<T: RealNumber>: Clone + Debug {
self.len() == 0
}
/// Create a new vector from a &[T]
/// ```
/// use smartcore::linalg::naive::dense_matrix::*;
/// let slice: &[f64] = &[0., 0.5, 2., 3., 4.];
/// let a: Vec<f64> = BaseVector::from_slice(slice);
/// assert_eq!(a, vec![0., 0.5, 2., 3., 4.]);
/// ```
fn from_slice(f: &[T]) -> Self {
let mut v = Self::zeros(f.len());
for (i, elem) in f.iter().enumerate() {
v.set(i, *elem);
}
v
}
/// Return a vector with the elements of the one-dimensional array.
fn to_vec(&self) -> Vec<T>;