From 4fac0ea4513ed8948418b177998a79cf1b626c01 Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Tue, 1 Aug 2023 12:46:31 +0300 Subject: [PATCH] abs --- ta_lib/core/src/math.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ta_lib/core/src/math.rs b/ta_lib/core/src/math.rs index f3e2db18..2c5236b8 100644 --- a/ta_lib/core/src/math.rs +++ b/ta_lib/core/src/math.rs @@ -31,6 +31,10 @@ impl Series { self.fmap(|val| val.map(|v| v.min(scalar)).or(Some(scalar))) } + pub fn abs(&self) -> Self { + self.fmap(|val| val.map(|v| v.abs())) + } + pub fn cumsum(&self) -> Self { let mut sum = 0.0; @@ -152,6 +156,17 @@ mod tests { } } + #[test] + fn test_abs() { + let source = vec![-1.0, 2.0, -3.0, 4.0, 5.0]; + let expected = vec![Some(1.0), Some(2.0), Some(3.0), Some(4.0), Some(5.0)]; + let series = Series::from(&source); + + let result = series.abs(); + + assert_eq!(result, expected); + } + #[test] fn test_cumsum() { let source = vec![1.0, 2.0, 3.0, 4.0, 5.0];