Skip to content

Commit

Permalink
Merge pull request AtheMathmo#53 from zackmdavis/into_data
Browse files Browse the repository at this point in the history
Matrix and Vector constructors accept data as Into<Vec<T>>
  • Loading branch information
AtheMathmo committed Apr 28, 2016
2 parents ef4cd86 + bd02976 commit 6cc4f37
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
6 changes: 3 additions & 3 deletions rusty-machine/src/linalg/matrix/impl_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ mod tests {

#[test]
fn index_slice() {
let mut b = Matrix::new(3, 3, (0..9).collect());
let mut b = Matrix::new(3, 3, (0..9).collect::<Vec<_>>());

let c = MatrixSlice::from_matrix(&b, [1, 1], 2, 2);

Expand Down Expand Up @@ -1187,7 +1187,7 @@ mod tests {
#[test]
fn matrix_sub_assign() {
let mut a = Matrix::new(3, 3, (0..9).collect::<Vec<i32>>());

a -= &2;
assert_eq!(a.into_vec(), (-2..7).collect::<Vec<_>>());

Expand Down Expand Up @@ -1312,7 +1312,7 @@ mod tests {
fn slice_sub_assign() {
let mut a = Matrix::new(3, 3, (0..9).collect::<Vec<i32>>());
let mut a_slice = MatrixSliceMut::from_matrix(&mut a, [0,0], 3, 3);

a_slice -= &2;
assert_eq!(a.into_vec(), (-2..7).collect::<Vec<_>>());

Expand Down
20 changes: 15 additions & 5 deletions rusty-machine/src/linalg/matrix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ impl<T> Matrix<T> {
/// # Panics
///
/// - The input data does not match the given dimensions.
pub fn new(rows: usize, cols: usize, data: Vec<T>) -> Matrix<T> {
pub fn new<U: Into<Vec<T>>>(rows: usize, cols: usize, data: U) -> Matrix<T> {
let our_data = data.into();

assert!(cols * rows == data.len(),
assert!(cols * rows == our_data.len(),
"Data does not match given dimensions.");
Matrix {
cols: cols,
rows: rows,
data: data,
data: our_data,
}
}

Expand Down Expand Up @@ -1210,6 +1211,15 @@ mod tests {
assert_eq!(a, a_redux);
}

#[test]
fn test_new_from_slice() {
let data_vec: Vec<u32> = vec![1, 2, 3, 4, 5, 6];
let data_slice: &[u32] = &data_vec[..];
let from_vec = Matrix::new(3, 2, data_vec.clone());
let from_slice = Matrix::new(3, 2, data_slice);
assert_eq!(from_vec, from_slice);
}

#[test]
fn test_display_formatting() {
let first_matrix = Matrix::new(2, 3, vec![1, 2, 3, 4, 5, 6]);
Expand Down Expand Up @@ -1259,7 +1269,7 @@ mod tests {

#[test]
fn test_split_matrix() {
let a = Matrix::new(3, 3, (0..9).collect());
let a = Matrix::new(3, 3, (0..9).collect::<Vec<_>>());

let (b,c) = a.split_at(1, Axes::Row);

Expand All @@ -1281,7 +1291,7 @@ mod tests {

#[test]
fn test_split_matrix_mut() {
let mut a = Matrix::new(3, 3, (0..9).collect());
let mut a = Matrix::new(3, 3, (0..9).collect::<Vec<_>>());

let (mut b, mut c) = a.split_at_mut(1, Axes::Row);

Expand Down
6 changes: 3 additions & 3 deletions rusty-machine/src/linalg/matrix/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! use rusty_machine::linalg::matrix::MatrixSlice;
//!
//! let a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>());
//!
//!
//! // Manually create our slice - [[4,5],[7,8]].
//! let mat_slice = MatrixSlice::from_matrix(&a, [1,1], 2, 2);
//!
Expand Down Expand Up @@ -413,7 +413,7 @@ mod tests {

#[test]
fn reslice() {
let mut a = Matrix::new(4,4, (0..16).collect());
let mut a = Matrix::new(4,4, (0..16).collect::<Vec<_>>());
let b = MatrixSlice::from_matrix(&a, [1,1], 3, 3);
{
let c = b.reslice([0,1], 2, 2);
Expand Down Expand Up @@ -454,4 +454,4 @@ mod tests {
assert_eq!(e.rows(), 2);
assert_eq!(e.cols(), 2);
}
}
}
20 changes: 14 additions & 6 deletions rusty-machine/src/linalg/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ impl<T> Vector<T> {
///
/// let vec = Vector::new(vec![1.0,2.0,3.0,4.0]);
/// ```
pub fn new(data: Vec<T>) -> Vector<T> {

let size = data.len();
pub fn new<U: Into<Vec<T>>>(data: U) -> Vector<T> {
let our_data = data.into();
let size = our_data.len();

Vector {
size: size,
data: data,
data: our_data,
}
}

Expand Down Expand Up @@ -607,7 +607,7 @@ impl<'a, T: Neg<Output = T> + Copy> Neg for &'a Vector<T> {
type Output = Vector<T>;

fn neg(self) -> Vector<T> {
let new_data = self.data.iter().map(|v| -*v).collect();
let new_data = self.data.iter().map(|v| -*v).collect::<Vec<_>>();

Vector::new(new_data)
}
Expand Down Expand Up @@ -672,6 +672,15 @@ mod tests {
}
}

#[test]
fn create_vector_new_from_slice() {
let data_vec: Vec<u32> = vec![1, 2, 3];
let data_slice: &[u32] = &data_vec[..];
let from_vec = Vector::new(data_vec.clone());
let from_slice = Vector::new(data_slice);
assert_eq!(from_vec, from_slice);
}

#[test]
fn create_vector_zeros() {
let a = Vector::<f32>::zeros(7);
Expand Down Expand Up @@ -906,5 +915,4 @@ mod tests {
assert_eq!(b, (1. + 4. + 9. + 16. + 25. + 36. as f32).sqrt());
}


}

0 comments on commit 6cc4f37

Please sign in to comment.