use super::super::error::TensorError;
use super::super::simd;
use super::super::tensor::Tensor;
impl Tensor {
pub fn eq_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
if self.shape() == rhs.shape() {
return self.binary_same_shape(rhs, |l, r| {
if (l - r).abs() < f32::EPSILON {
1.0
} else {
0.0
}
});
}
self.binary_broadcast_op(rhs, |l, r| {
if (l - r).abs() < f32::EPSILON {
1.0
} else {
0.0
}
})
}
pub fn gt_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
if self.shape() == rhs.shape() {
return self.binary_same_shape(rhs, |l, r| if l > r { 1.0 } else { 0.0 });
}
self.binary_broadcast_op(rhs, |l, r| if l > r { 1.0 } else { 0.0 })
}
pub fn lt_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
if self.shape() == rhs.shape() {
return self.binary_same_shape(rhs, |l, r| if l < r { 1.0 } else { 0.0 });
}
self.binary_broadcast_op(rhs, |l, r| if l < r { 1.0 } else { 0.0 })
}
pub fn gt_tensor_into(&self, rhs: &Self, output: &mut Self) {
debug_assert_eq!(self.shape(), rhs.shape());
debug_assert_eq!(self.shape(), output.shape());
simd::cmp_dispatch(
self.data(),
rhs.data(),
output.data_mut(),
simd::CmpKind::Gt,
);
}
pub fn eq_tensor_into(&self, rhs: &Self, output: &mut Self) {
debug_assert_eq!(self.shape(), rhs.shape());
debug_assert_eq!(self.shape(), output.shape());
simd::cmp_dispatch(
self.data(),
rhs.data(),
output.data_mut(),
simd::CmpKind::Eq,
);
}
pub fn lt_tensor_into(&self, rhs: &Self, output: &mut Self) {
debug_assert_eq!(self.shape(), rhs.shape());
debug_assert_eq!(self.shape(), output.shape());
simd::cmp_dispatch(
self.data(),
rhs.data(),
output.data_mut(),
simd::CmpKind::Lt,
);
}
pub fn ne_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
if self.shape() == rhs.shape() {
return self.binary_same_shape(
rhs,
|l, r| {
if (l - r).abs() >= 1e-7 { 1.0 } else { 0.0 }
},
);
}
self.binary_broadcast_op(rhs, |l, r| if (l - r).abs() >= 1e-7 { 1.0 } else { 0.0 })
}
pub fn le_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
if self.shape() == rhs.shape() {
return self.binary_same_shape(rhs, |l, r| if l <= r { 1.0 } else { 0.0 });
}
self.binary_broadcast_op(rhs, |l, r| if l <= r { 1.0 } else { 0.0 })
}
pub fn ge_tensor(&self, rhs: &Self) -> Result<Self, TensorError> {
if self.shape() == rhs.shape() {
return self.binary_same_shape(rhs, |l, r| if l >= r { 1.0 } else { 0.0 });
}
self.binary_broadcast_op(rhs, |l, r| if l >= r { 1.0 } else { 0.0 })
}
pub fn all_finite(&self) -> bool {
self.data().iter().all(|v| v.is_finite())
}
}