Skip to main content

yscv_tensor/ops/
compare.rs

1//! Comparison + logical ops (eq/lt/gt/…, all_finite).
2
3use super::super::error::TensorError;
4use super::super::simd;
5use super::super::tensor::Tensor;
6
7impl Tensor {
8    // ── Comparison ──────────────────────────────────────────────────────
9
10    /// Element-wise equality check: 1.0 where equal, 0.0 otherwise.
11    pub fn eq_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
12        if self.shape() == rhs.shape() {
13            return self.binary_same_shape(rhs, |l, r| {
14                if (l - r).abs() < f32::EPSILON {
15                    1.0
16                } else {
17                    0.0
18                }
19            });
20        }
21        self.binary_broadcast_op(rhs, |l, r| {
22            if (l - r).abs() < f32::EPSILON {
23                1.0
24            } else {
25                0.0
26            }
27        })
28    }
29
30    /// Element-wise greater-than: 1.0 where `self > rhs`, 0.0 otherwise.
31    pub fn gt_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
32        if self.shape() == rhs.shape() {
33            return self.binary_same_shape(rhs, |l, r| if l > r { 1.0 } else { 0.0 });
34        }
35        self.binary_broadcast_op(rhs, |l, r| if l > r { 1.0 } else { 0.0 })
36    }
37
38    /// Element-wise less-than: 1.0 where `self < rhs`, 0.0 otherwise.
39    pub fn lt_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
40        if self.shape() == rhs.shape() {
41            return self.binary_same_shape(rhs, |l, r| if l < r { 1.0 } else { 0.0 });
42        }
43        self.binary_broadcast_op(rhs, |l, r| if l < r { 1.0 } else { 0.0 })
44    }
45
46    /// Element-wise greater-than writing into a pre-allocated output tensor.
47    /// `self`, `rhs`, and `output` must all have the same shape.
48    pub fn gt_tensor_into(&self, rhs: &Self, output: &mut Self) {
49        debug_assert_eq!(self.shape(), rhs.shape());
50        debug_assert_eq!(self.shape(), output.shape());
51        simd::cmp_dispatch(
52            self.data(),
53            rhs.data(),
54            output.data_mut(),
55            simd::CmpKind::Gt,
56        );
57    }
58
59    /// Element-wise equality check writing into a pre-allocated output tensor.
60    /// `self`, `rhs`, and `output` must all have the same shape.
61    pub fn eq_tensor_into(&self, rhs: &Self, output: &mut Self) {
62        debug_assert_eq!(self.shape(), rhs.shape());
63        debug_assert_eq!(self.shape(), output.shape());
64        simd::cmp_dispatch(
65            self.data(),
66            rhs.data(),
67            output.data_mut(),
68            simd::CmpKind::Eq,
69        );
70    }
71
72    /// Element-wise less-than writing into a pre-allocated output tensor.
73    /// `self`, `rhs`, and `output` must all have the same shape.
74    pub fn lt_tensor_into(&self, rhs: &Self, output: &mut Self) {
75        debug_assert_eq!(self.shape(), rhs.shape());
76        debug_assert_eq!(self.shape(), output.shape());
77        simd::cmp_dispatch(
78            self.data(),
79            rhs.data(),
80            output.data_mut(),
81            simd::CmpKind::Lt,
82        );
83    }
84
85    /// Element-wise not-equal: 1.0 where not equal (diff.abs() >= 1e-7), 0.0 otherwise.
86    pub fn ne_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
87        if self.shape() == rhs.shape() {
88            return self.binary_same_shape(
89                rhs,
90                |l, r| {
91                    if (l - r).abs() >= 1e-7 { 1.0 } else { 0.0 }
92                },
93            );
94        }
95        self.binary_broadcast_op(rhs, |l, r| if (l - r).abs() >= 1e-7 { 1.0 } else { 0.0 })
96    }
97
98    /// Element-wise less-than-or-equal: 1.0 where `self <= rhs`, 0.0 otherwise.
99    pub fn le_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
100        if self.shape() == rhs.shape() {
101            return self.binary_same_shape(rhs, |l, r| if l <= r { 1.0 } else { 0.0 });
102        }
103        self.binary_broadcast_op(rhs, |l, r| if l <= r { 1.0 } else { 0.0 })
104    }
105
106    /// Element-wise greater-than-or-equal: 1.0 where `self >= rhs`, 0.0 otherwise.
107    pub fn ge_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
108        if self.shape() == rhs.shape() {
109            return self.binary_same_shape(rhs, |l, r| if l >= r { 1.0 } else { 0.0 });
110        }
111        self.binary_broadcast_op(rhs, |l, r| if l >= r { 1.0 } else { 0.0 })
112    }
113
114    /// Returns true if all elements are finite (no NaN or Inf).
115    pub fn all_finite(&self) -> bool {
116        self.data().iter().all(|v| v.is_finite())
117    }
118}