Skip to main content

matten/ops/
unary_ops.rs

1//! Unary operators (RFC-006).
2
3use crate::Tensor;
4use std::ops::Neg;
5
6impl Neg for &Tensor {
7    type Output = Tensor;
8    /// Negates every element.
9    ///
10    /// ```
11    /// use matten::Tensor;
12    /// let t = Tensor::new(vec![1.0, -2.0, 3.0], &[3]);
13    /// let r = -&t;
14    /// assert_eq!(r.as_slice(), &[-1.0, 2.0, -3.0]);
15    /// ```
16    fn neg(self) -> Tensor {
17        #[cfg(feature = "dynamic")]
18        if self.is_dynamic() {
19            panic!(
20                "matten unsupported error in neg: unary negation is not supported on dynamic tensors; call try_numeric() first"
21            );
22        }
23        Tensor {
24            data: self.data.iter().map(|&v| -v).collect(),
25            shape: self.shape.clone(),
26            #[cfg(feature = "dynamic")]
27            dynamic: None,
28        }
29    }
30}