From fbf378d6ee00c92ad691ff3b7394f96f10f40ad5 Mon Sep 17 00:00:00 2001 From: "Andrew X. Shah" Date: Fri, 8 Sep 2023 12:19:38 -0600 Subject: [PATCH] feat(tensor): create method abs --- src/linalg/tensor.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/linalg/tensor.rs b/src/linalg/tensor.rs index c7fbab4..aab0c3c 100644 --- a/src/linalg/tensor.rs +++ b/src/linalg/tensor.rs @@ -808,6 +808,20 @@ impl Tensor { Tensor1D::from(flat_data) } + /// Returns a tensor with the absolute value of each element in the tensor. + /// + /// # Examples + /// + /// ``` + /// # use engram::*; + /// let a = tensor![[-1.0, 2.0], [-3.0, 4.0]]; + /// let b = a.abs(); + /// assert_eq!(b.data, vec![vec![1.0, 2.0], vec![3.0, 4.0]]); + /// ``` + pub fn abs(&self) -> Tensor { + self.mapv(&|x| x.abs()) + } + /// Returns the transpose of the tensor. /// /// # Examples