yscv_tensor/ops/
compare.rs1use super::super::error::TensorError;
4use super::super::simd;
5use super::super::tensor::Tensor;
6
7impl Tensor {
8 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 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 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 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 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 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 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 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 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 pub fn all_finite(&self) -> bool {
116 self.data().iter().all(|v| v.is_finite())
117 }
118}