From ffa1a76df44483d36ee9a346c1b955e08f6fb98e Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sun, 6 Jul 2025 01:55:45 +0100 Subject: [PATCH] Implement map and zip methods for SeriesOps trait and add corresponding tests --- src/frame/ops.rs | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/frame/ops.rs b/src/frame/ops.rs index dea2396..1c3254b 100644 --- a/src/frame/ops.rs +++ b/src/frame/ops.rs @@ -20,6 +20,21 @@ impl SeriesOps for Frame { { self.matrix().apply_axis(axis, f) } + + fn map(&self, f: F) -> FloatMatrix + where + F: Fn(f64) -> f64, + { + self.matrix().map(f) + } + + fn zip(&self, other: &Self, f: F) -> FloatMatrix + where + F: Fn(f64, f64) -> f64, + { + self.matrix().zip(other.matrix(), f) + } + fn matrix_mul(&self, other: &Self) -> FloatMatrix { self.matrix().matrix_mul(other.matrix()) } @@ -113,7 +128,7 @@ mod tests { let col_names = vec!["A".to_string(), "B".to_string()]; let frame = Frame::new( Matrix::from_cols(vec![vec![1.0, 2.0], vec![3.0, 4.0]]), - col_names, + col_names.clone(), None, ); assert_eq!(frame.sum_vertical(), frame.matrix().sum_vertical()); @@ -136,16 +151,32 @@ mod tests { assert_eq!(frame.is_nan(), frame.matrix().is_nan()); assert_eq!(frame.apply_axis(Axis::Row, |x| x[0] + x[1]), vec![4.0, 6.0]); - - assert_eq!(frame.matrix_mul(&frame), frame.matrix().matrix_mul(&frame.matrix())); + assert_eq!( + frame.matrix_mul(&frame), + frame.matrix().matrix_mul(&frame.matrix()) + ); assert_eq!(frame.dot(&frame), frame.matrix().dot(&frame.matrix())); - + // test transpose - returns a matrix. let frame_transposed_mat = frame.transpose(); let frame_mat_transposed = frame.matrix().transpose(); assert_eq!(frame_transposed_mat, frame_mat_transposed); assert_eq!(frame.matrix(), &frame.matrix().transpose().transpose()); + // test map + let mapped_frame = frame.map(|x| x * 2.0); + let expected_matrix = frame.matrix().map(|x| x * 2.0); + assert_eq!(mapped_frame, expected_matrix); + + // test zip + let other_frame = Frame::new( + Matrix::from_cols(vec![vec![5.0, 6.0], vec![7.0, 8.0]]), + col_names.clone(), + None, + ); + let zipped_frame = frame.zip(&other_frame, |x, y| x + y); + let expected_zipped_matrix = frame.matrix().zip(other_frame.matrix(), |x, y| x + y); + assert_eq!(zipped_frame, expected_zipped_matrix); } #[test] @@ -170,7 +201,4 @@ mod tests { vec![false, false] ); } - - - }