From 68ff90b0ba87d436bf3a05cef67db8504512756c Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 26 Apr 2025 15:24:40 +0100 Subject: [PATCH] Implement SeriesOps and BoolOps traits for Frame, adding matrix delegation methods --- src/frame/ops.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/frame/ops.rs diff --git a/src/frame/ops.rs b/src/frame/ops.rs new file mode 100644 index 0000000..a1a2ece --- /dev/null +++ b/src/frame/ops.rs @@ -0,0 +1,97 @@ +use crate::frame::Frame; +use crate::matrix::{Axis, BoolMatrix, BoolOps, FloatMatrix, SeriesOps}; + +// Macro to delegate method calls to self.matrix() +macro_rules! delegate_to_matrix { + ($($method_name:ident -> $return_type:ty),* $(,)?) => { + $( + fn $method_name(&self) -> $return_type { + self.matrix().$method_name() + } + )* + }; +} + +impl SeriesOps for Frame { + #[allow(unused_mut)] + fn apply_axis(&self, axis: Axis, mut f: F) -> Vec + where + F: FnMut(&[f64]) -> U, + { + self.matrix().apply_axis(axis, f) + } + + delegate_to_matrix!( + sum_vertical -> Vec, + sum_horizontal -> Vec, + prod_horizontal -> Vec, + prod_vertical -> Vec, + cumsum_horizontal -> FloatMatrix, + cumsum_vertical -> FloatMatrix, + count_nan_vertical -> Vec, + count_nan_horizontal -> Vec, + is_nan -> BoolMatrix + ); +} + +impl BoolOps for Frame { + fn apply_axis(&self, axis: Axis, f: F) -> Vec + where + F: FnMut(&[bool]) -> U, + { + self.matrix().apply_axis(axis, f) + } + + delegate_to_matrix!( + any_vertical -> Vec, + any_horizontal -> Vec, + all_vertical -> Vec, + all_horizontal -> Vec, + count_vertical -> Vec, + count_horizontal -> Vec, + any -> bool, + all -> bool, + count -> usize + ); +} + +// use crate::frame::Frame; +// use crate::matrix::{Axis, SeriesOps, FloatMatrix, BoolMatrix}; + +// impl SeriesOps for Frame { +// fn apply_axis(&self, axis: Axis, mut f: F) -> Vec +// where +// F: FnMut(&[f64]) -> U, +// { +// self.matrix().apply_axis(axis, f) +// } + +// fn sum_vertical(&self) -> Vec { +// self.matrix().sum_vertical() +// } +// fn sum_horizontal(&self) -> Vec { +// self.matrix().sum_horizontal() +// } +// fn prod_horizontal(&self) -> Vec { +// self.matrix().prod_horizontal() +// } +// fn prod_vertical(&self) -> Vec { +// self.matrix().prod_vertical() +// } +// fn cumsum_horizontal(&self) -> FloatMatrix { +// self.matrix().cumsum_horizontal() +// } +// fn cumsum_vertical(&self) -> FloatMatrix { +// self.matrix().cumsum_vertical() +// } + +// fn count_nan_vertical(&self) -> Vec { +// self.matrix().count_nan_vertical() +// } +// fn count_nan_horizontal(&self) -> Vec { +// self.matrix().count_nan_horizontal() +// } +// fn is_nan(&self) -> BoolMatrix { +// self.matrix().is_nan() +// } +// }