use std::fmt;
use std::ops;
use ruxla_ir::Shape;
use ruxla_kernels::array::ReduceOp;
use ruxla_kernels::{BinaryOp, CompareOp, Element, UnaryOp, bf16, c64, c128, f16};
use crate::backend::{Backend, Native, NativeArray, ToReal};
pub trait Float: Num + Inexact {}
impl Float for f16 {}
impl Float for bf16 {}
impl Float for f32 {}
impl Float for f64 {}
pub trait LinalgFloat: Float + LinalgScalar + ruxla_kernels::linalg::LinalgFloat {}
impl LinalgFloat for f32 {}
impl LinalgFloat for f64 {}
pub trait LinalgScalar:
Inexact
+ ruxla_kernels::linalg::LinalgScalar<LinalgReal: Float + ruxla_kernels::linalg::LinalgFloat>
{
type Complex: ComplexFloat;
#[doc(hidden)]
fn complex_vec(v: Vec<ruxla_kernels::linalg::Cx<Self>>) -> Vec<Self::Complex>;
}
macro_rules! impl_linalg_scalar {
($($t:ty => $c:ty),*) => {$(
impl LinalgScalar for $t {
type Complex = $c;
fn complex_vec(v: Vec<ruxla_kernels::linalg::Cx<Self>>) -> Vec<$c> {
v
}
}
)*};
}
impl_linalg_scalar!(f32 => c64, f64 => c128, c64 => c64, c128 => c128);
pub trait Numeric: Element {}
macro_rules! numeric {
($($t:ty)*) => {$(impl Numeric for $t {})*};
}
numeric!(i8 i16 i32 i64 u8 u16 u32 u64 f16 bf16 f32 f64 c64 c128);
pub trait Inexact: Numeric {}
impl Inexact for f16 {}
impl Inexact for bf16 {}
impl Inexact for f32 {}
impl Inexact for f64 {}
impl Inexact for c64 {}
impl Inexact for c128 {}
pub trait ComplexFloat: Inexact + Element<Real: Float> {}
impl ComplexFloat for c64 {}
impl ComplexFloat for c128 {}
pub trait RealFloat: Float {
type Complex: ComplexFloat + Element<Real = Self>;
}
impl RealFloat for f32 {
type Complex = c64;
}
impl RealFloat for f64 {
type Complex = c128;
}
pub trait Int: Num {}
impl Int for i8 {}
impl Int for i16 {}
impl Int for i32 {}
impl Int for i64 {}
impl Int for u8 {}
impl Int for u16 {}
impl Int for u32 {}
impl Int for u64 {}
pub trait Num: Numeric + Element<Real = Self> {}
macro_rules! num {
($($t:ty)*) => {$(impl Num for $t {})*};
}
num!(i8 i16 i32 i64 u8 u16 u32 u64 f16 bf16 f32 f64);
#[repr(transparent)]
pub struct Scalar<B: Backend, T: Element>(pub(crate) B::Scalar<T>);
pub type Bool<B> = Scalar<B, bool>;
pub type I32<B> = Scalar<B, i32>;
pub type I64<B> = Scalar<B, i64>;
pub type U32<B> = Scalar<B, u32>;
pub type U8<B> = Scalar<B, u8>;
pub type F32<B> = Scalar<B, f32>;
pub type F64<B> = Scalar<B, f64>;
pub type C64<B> = Scalar<B, c64>;
pub type C128<B> = Scalar<B, c128>;
impl<B: Backend, T: Element> Clone for Scalar<B, T> {
fn clone(&self) -> Self {
*self
}
}
impl<B: Backend, T: Element> Copy for Scalar<B, T> {}
impl<B: Backend, T: Element> fmt::Debug for Scalar<B, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Scalar<{}>({:?})", T::DTYPE, self.0)
}
}
impl<B: Backend, T: Element> From<T> for Scalar<B, T> {
fn from(v: T) -> Self {
Self::new(v)
}
}
impl<B: Backend, T: Element> Scalar<B, T> {
pub fn new(v: T) -> Self {
Scalar(B::lit(v))
}
pub fn from_repr(r: B::Scalar<T>) -> Self {
Scalar(r)
}
pub fn repr(self) -> B::Scalar<T> {
self.0
}
fn unary(self, op: UnaryOp) -> Self {
Scalar(B::unary::<T>(op, self.0))
}
fn binary(self, op: BinaryOp, other: impl Into<Self>) -> Self {
Scalar(B::binary::<T>(op, self.0, other.into().0))
}
fn compare(self, op: CompareOp, other: impl Into<Self>) -> Bool<B> {
Scalar(B::compare::<T>(op, self.0, other.into().0))
}
pub fn eq(self, other: impl Into<Self>) -> Bool<B> {
self.compare(CompareOp::Eq, other)
}
pub fn ne(self, other: impl Into<Self>) -> Bool<B> {
self.compare(CompareOp::Ne, other)
}
pub fn lt(self, other: impl Into<Self>) -> Bool<B> {
self.compare(CompareOp::Lt, other)
}
pub fn le(self, other: impl Into<Self>) -> Bool<B> {
self.compare(CompareOp::Le, other)
}
pub fn gt(self, other: impl Into<Self>) -> Bool<B> {
self.compare(CompareOp::Gt, other)
}
pub fn ge(self, other: impl Into<Self>) -> Bool<B> {
self.compare(CompareOp::Ge, other)
}
pub fn max(self, other: impl Into<Self>) -> Self {
self.binary(BinaryOp::Max, other)
}
pub fn min(self, other: impl Into<Self>) -> Self {
self.binary(BinaryOp::Min, other)
}
pub fn cast<U: Element>(self) -> Scalar<B, U> {
Scalar(B::cast::<T, U>(self.0))
}
pub fn bitcast<U: Element>(self) -> Scalar<B, U> {
Scalar(B::bitcast::<T, U>(self.0))
}
pub fn to_array(self) -> Array<B, T> {
Array(B::to_array::<T>(self.0))
}
}
impl<T: Element> Scalar<Native, T> {
pub fn value(self) -> T {
self.0
}
}
impl<B: Backend, T: Numeric> Scalar<B, T> {
pub fn abs(self) -> Scalar<B, T::Real> {
Scalar(B::to_real::<T>(ToReal::Abs, self.0))
}
pub fn sign(self) -> Self {
self.unary(UnaryOp::Sign)
}
pub fn square(self) -> Self {
self.unary(UnaryOp::Square)
}
pub fn integer_pow(self, y: i32) -> Self {
Scalar(B::integer_pow::<T>(self.0, y))
}
}
impl<B: Backend, T: Num> Scalar<B, T> {
pub fn clamp(self, lo: impl Into<Self>, hi: impl Into<Self>) -> Self {
self.max(lo).min(hi)
}
}
impl<B: Backend, T: ComplexFloat> Scalar<B, T> {
pub fn complex(re: impl Into<Scalar<B, T::Real>>, im: impl Into<Scalar<B, T::Real>>) -> Self {
Scalar(B::complex::<T>(re.into().0, im.into().0))
}
pub fn real(self) -> Scalar<B, T::Real> {
Scalar(B::to_real::<T>(ToReal::Real, self.0))
}
pub fn imag(self) -> Scalar<B, T::Real> {
Scalar(B::to_real::<T>(ToReal::Imag, self.0))
}
pub fn conj(self) -> Self {
Scalar(B::conj::<T>(self.0))
}
pub fn angle(self) -> Scalar<B, T::Real> {
self.imag().atan2(self.real())
}
}
impl<B: Backend, T: ComplexFloat> Array<B, T> {
#[track_caller]
pub fn complex(re: &Array<B, T::Real>, im: &Array<B, T::Real>) -> Self {
Array(B::a_complex::<T>(&re.0, &im.0))
}
pub fn real(&self) -> Array<B, T::Real> {
Array(B::a_to_real::<T>(ToReal::Real, &self.0))
}
pub fn imag(&self) -> Array<B, T::Real> {
Array(B::a_to_real::<T>(ToReal::Imag, &self.0))
}
pub fn conj(&self) -> Self {
Array(B::a_conj::<T>(&self.0))
}
pub fn angle(&self) -> Array<B, T::Real> {
crate::jnp::arctan2(self.imag(), self.real())
}
}
impl<B: Backend, T: Numeric> Array<B, T> {
pub fn abs(&self) -> Array<B, T::Real> {
Array(B::a_to_real::<T>(ToReal::Abs, &self.0))
}
pub fn sign(&self) -> Self {
self.unary(UnaryOp::Sign)
}
pub fn square(&self) -> Self {
self.unary(UnaryOp::Square)
}
pub fn integer_pow(&self, y: i32) -> Self {
Array(B::a_integer_pow::<T>(&self.0, y))
}
}
macro_rules! inexact_methods {
($($(#[$m:meta])* $name:ident => $op:ident;)*) => {
impl<B: Backend, T: Inexact> Scalar<B, T> {
$($(#[$m])* pub fn $name(self) -> Self { self.unary(UnaryOp::$op) })*
pub fn pow(self, y: impl Into<Self>) -> Self {
self.binary(BinaryOp::Pow, y)
}
}
impl<B: Backend, T: Inexact> Array<B, T> {
$($(#[$m])* pub fn $name(&self) -> Self { self.unary(UnaryOp::$op) })*
}
};
}
inexact_methods! {
exp => Exp;
expm1 => Expm1;
ln => Log;
ln_1p => Log1p;
sqrt => Sqrt;
rsqrt => Rsqrt;
sin => Sin;
cos => Cos;
tan => Tan;
tanh => Tanh;
logistic => Logistic;
}
macro_rules! float_methods {
($($(#[$m:meta])* $name:ident => $op:ident;)*) => {
impl<B: Backend, T: Float> Scalar<B, T> {
$($(#[$m])* pub fn $name(self) -> Self { self.unary(UnaryOp::$op) })*
pub fn atan2(self, x: impl Into<Self>) -> Self {
self.binary(BinaryOp::Atan2, x)
}
pub fn is_finite(self) -> Bool<B> {
Scalar(B::is_finite::<T>(self.0))
}
}
impl<B: Backend, T: Float> Array<B, T> {
$($(#[$m])* pub fn $name(&self) -> Self { self.unary(UnaryOp::$op) })*
pub fn is_finite(&self) -> Array<B, bool> {
Array(B::a_is_finite::<T>(&self.0))
}
}
};
}
float_methods! {
floor => Floor;
ceil => Ceil;
round => Round;
round_even => RoundEven;
cbrt => Cbrt;
erf_inv => ErfInv;
lgamma => Lgamma;
digamma => Digamma;
erf => Erf;
erfc => Erfc;
}
macro_rules! scalar_binop {
($($tr:ident $f:ident $op:ident $bound:ident;)*) => {$(
impl<B: Backend, T: $bound> ops::$tr for Scalar<B, T> {
type Output = Self;
fn $f(self, rhs: Self) -> Self {
self.binary(BinaryOp::$op, rhs)
}
}
impl<B: Backend, T: $bound> ops::$tr<T> for Scalar<B, T> {
type Output = Self;
fn $f(self, rhs: T) -> Self {
self.binary(BinaryOp::$op, rhs)
}
}
)*};
}
scalar_binop! {
Add add Add Numeric;
Sub sub Sub Numeric;
Mul mul Mul Numeric;
Div div Div Numeric;
Rem rem Rem Num;
BitAnd bitand And Element;
BitOr bitor Or Element;
BitXor bitxor Xor Element;
}
macro_rules! scalar_lhs_literal {
($($t:ty)*) => {$(
impl<B: Backend> ops::Add<Scalar<B, $t>> for $t {
type Output = Scalar<B, $t>;
fn add(self, rhs: Scalar<B, $t>) -> Scalar<B, $t> { Scalar::new(self) + rhs }
}
impl<B: Backend> ops::Sub<Scalar<B, $t>> for $t {
type Output = Scalar<B, $t>;
fn sub(self, rhs: Scalar<B, $t>) -> Scalar<B, $t> { Scalar::new(self) - rhs }
}
impl<B: Backend> ops::Mul<Scalar<B, $t>> for $t {
type Output = Scalar<B, $t>;
fn mul(self, rhs: Scalar<B, $t>) -> Scalar<B, $t> { Scalar::new(self) * rhs }
}
impl<B: Backend> ops::Div<Scalar<B, $t>> for $t {
type Output = Scalar<B, $t>;
fn div(self, rhs: Scalar<B, $t>) -> Scalar<B, $t> { Scalar::new(self) / rhs }
}
)*};
}
scalar_lhs_literal!(i8 i16 i32 i64 u8 u16 u32 u64 f16 bf16 f32 f64 c64 c128);
impl<B: Backend, T: Numeric> ops::Neg for Scalar<B, T> {
type Output = Self;
fn neg(self) -> Self {
self.unary(UnaryOp::Neg)
}
}
impl<B: Backend, T: Element> ops::Not for Scalar<B, T> {
type Output = Self;
fn not(self) -> Self {
self.unary(UnaryOp::Not)
}
}
impl<B: Backend, T: Int> ops::Shl for Scalar<B, T> {
type Output = Self;
fn shl(self, rhs: Self) -> Self {
self.binary(BinaryOp::ShiftLeft, rhs)
}
}
impl<B: Backend, T: Int> ops::Shr for Scalar<B, T> {
type Output = Self;
fn shr(self, rhs: Self) -> Self {
let op = if T::DTYPE.kind() == ruxla_ir::DTypeKind::UnsignedInt {
BinaryOp::ShiftRightLogical
} else {
BinaryOp::ShiftRightArithmetic
};
self.binary(op, rhs)
}
}
impl<B: Backend, T: Int> ops::Shl<T> for Scalar<B, T> {
type Output = Self;
fn shl(self, rhs: T) -> Self {
self << Scalar::new(rhs)
}
}
impl<B: Backend, T: Int> ops::Shr<T> for Scalar<B, T> {
type Output = Self;
fn shr(self, rhs: T) -> Self {
self >> Scalar::new(rhs)
}
}
#[repr(transparent)]
pub struct Array<B: Backend, T: Element>(pub(crate) B::Array<T>);
impl<B: Backend, T: Element> Clone for Array<B, T> {
#[inline]
fn clone(&self) -> Self {
Array(self.0.clone())
}
}
impl<B: Backend, T: Element> fmt::Debug for Array<B, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Array<{}>({:?})", T::DTYPE, self.0)
}
}
impl<B: Backend, T: Element> Array<B, T> {
#[track_caller]
pub fn from_vec(data: Vec<T>, shape: impl Into<Shape>) -> Self {
Array(B::constant_owned(NativeArray::new(data, shape)))
}
#[track_caller]
#[inline]
pub fn from_slice(data: &[T], shape: impl Into<Shape>) -> Self {
Array(B::constant_owned(NativeArray::from_slice(data, shape)))
}
pub fn constant(x: &NativeArray<T>) -> Self {
Array(B::constant(x))
}
pub fn from_repr(r: B::Array<T>) -> Self {
Array(r)
}
pub fn repr(&self) -> &B::Array<T> {
&self.0
}
pub(crate) fn reprs(xs: &[Self]) -> &[B::Array<T>] {
unsafe { std::slice::from_raw_parts(xs.as_ptr().cast::<B::Array<T>>(), xs.len()) }
}
pub fn shape(&self) -> Shape {
B::shape::<T>(&self.0)
}
#[inline]
pub fn ndim(&self) -> usize {
B::rank::<T>(&self.0)
}
#[inline]
pub fn size(&self) -> usize {
match B::dims(&self.0) {
Some(dims) => dims.iter().product(),
None => self.shape().size(),
}
}
#[track_caller]
pub fn to_scalar(&self) -> Scalar<B, T> {
Scalar(B::to_scalar::<T>(&self.0))
}
pub fn cast<U: Element>(&self) -> Array<B, U> {
Array(B::a_cast::<T, U>(&self.0))
}
#[track_caller]
pub fn reshape(&self, shape: impl Into<Shape>) -> Self {
Array(B::reshape::<T>(&self.0, &shape.into()))
}
#[track_caller]
pub fn transpose(&self, perm: &[usize]) -> Self {
Array(B::transpose::<T>(&self.0, perm))
}
#[track_caller]
pub fn broadcast_to(&self, shape: impl Into<Shape>) -> Self {
let shape = shape.into();
let s = self.shape();
if s == shape {
return self.clone();
}
assert!(
s.rank() <= shape.rank(),
"cannot broadcast [{s}] to [{shape}]"
);
let offset = shape.rank() - s.rank();
let dims: Vec<usize> = (0..s.rank()).map(|i| i + offset).collect();
for (i, &d) in dims.iter().enumerate() {
assert!(
s[i] == 1 || s[i] == shape[d],
"cannot broadcast [{s}] to [{shape}]"
);
}
Array(B::broadcast_in_dim::<T>(&self.0, &shape, &dims))
}
pub(crate) fn unary(&self, op: UnaryOp) -> Self {
Array(B::a_unary::<T>(op, &self.0))
}
pub(crate) fn into_unary(mut self, op: UnaryOp) -> Self {
if B::FAST_PATHS && B::a_unary_assign::<T>(op, &mut self.0) {
return self;
}
self.unary(op)
}
#[track_caller]
pub fn reduce_sum(&self, axes: &[usize]) -> Self {
Array(B::reduce::<T>(ReduceOp::Sum, &self.0, axes))
}
#[track_caller]
pub fn reduce_max(&self, axes: &[usize]) -> Self {
Array(B::reduce::<T>(ReduceOp::Max, &self.0, axes))
}
#[track_caller]
pub fn reduce_min(&self, axes: &[usize]) -> Self {
Array(B::reduce::<T>(ReduceOp::Min, &self.0, axes))
}
}
impl<T: Element> Array<Native, T> {
pub fn to_vec(&self) -> Vec<T> {
self.0.data().to_vec()
}
pub fn native(&self) -> &NativeArray<T> {
&self.0
}
}
#[track_caller]
pub(crate) fn broadcast_shapes(a: &Shape, b: &Shape) -> Shape {
let r = a.rank().max(b.rank());
let dim = |s: &Shape, i: usize| {
if i + s.rank() < r {
1
} else {
s[i + s.rank() - r]
}
};
if a == b {
return a.clone();
}
(0..r)
.map(|i| {
let (x, y) = (dim(a, i), dim(b, i));
assert!(
x == y || x == 1 || y == 1,
"shapes [{a}] and [{b}] do not broadcast"
);
x.max(y)
})
.collect()
}
#[track_caller]
pub(crate) fn broadcast_binary<B: Backend, T: Element>(
op: BinaryOp,
a: &Array<B, T>,
b: &Array<B, T>,
) -> Array<B, T> {
if let Some(r) = B::a_binary_broadcast::<T>(op, &a.0, &b.0) {
return Array(r);
}
let s = broadcast_shapes(&a.shape(), &b.shape());
Array(B::a_binary::<T>(
op,
&a.broadcast_to(s.clone()).0,
&b.broadcast_to(s).0,
))
}
#[track_caller]
pub(crate) fn broadcast_compare<B: Backend, T: Element>(
op: CompareOp,
a: &Array<B, T>,
b: &Array<B, T>,
) -> Array<B, bool> {
if let Some(r) = B::a_compare_broadcast::<T>(op, &a.0, &b.0) {
return Array(r);
}
let s = broadcast_shapes(&a.shape(), &b.shape());
Array(B::a_compare::<T>(
op,
&a.broadcast_to(s.clone()).0,
&b.broadcast_to(s).0,
))
}
pub trait ArrayLike<B: Backend, T: Element>: Sized {
fn into_array(self) -> Array<B, T>;
#[inline]
fn with_array<R>(self, f: impl FnOnce(&Array<B, T>) -> R) -> R {
f(&self.into_array())
}
#[doc(hidden)]
#[inline]
fn as_scalar(&self) -> Option<Scalar<B, T>> {
None
}
#[doc(hidden)]
#[inline]
fn is_borrowed(&self) -> bool {
false
}
}
impl<B: Backend, T: Element> ArrayLike<B, T> for Array<B, T> {
fn into_array(self) -> Array<B, T> {
self
}
}
impl<B: Backend, T: Element> ArrayLike<B, T> for &Array<B, T> {
fn into_array(self) -> Array<B, T> {
self.clone()
}
#[inline]
fn with_array<R>(self, f: impl FnOnce(&Array<B, T>) -> R) -> R {
f(self)
}
#[inline]
fn is_borrowed(&self) -> bool {
true
}
}
impl<B: Backend, T: Element> ArrayLike<B, T> for Scalar<B, T> {
#[inline]
fn into_array(self) -> Array<B, T> {
self.to_array()
}
#[inline]
fn as_scalar(&self) -> Option<Scalar<B, T>> {
Some(*self)
}
}
impl<B: Backend, T: Element> ArrayLike<B, T> for T {
#[inline]
fn into_array(self) -> Array<B, T> {
Scalar::<B, T>::new(self).to_array()
}
#[inline]
fn as_scalar(&self) -> Option<Scalar<B, T>> {
Some(Scalar::new(*self))
}
}
macro_rules! array_binop {
($($tr:ident $f:ident $op:ident $bound:ident;)*) => {$(
impl<B: Backend, T: $bound, R: ArrayLike<B, T>> ops::$tr<R> for Array<B, T> {
type Output = Array<B, T>;
#[track_caller]
fn $f(mut self, rhs: R) -> Array<B, T> {
rhs.with_array(|r| {
if B::FAST_PATHS && B::a_binary_assign::<T>(BinaryOp::$op, &mut self.0, &r.0) {
return self;
}
broadcast_binary(BinaryOp::$op, &self, r)
})
}
}
impl<B: Backend, T: $bound, R: ArrayLike<B, T>> ops::$tr<R> for &Array<B, T> {
type Output = Array<B, T>;
#[track_caller]
fn $f(self, rhs: R) -> Array<B, T> {
if B::FAST_PATHS && BinaryOp::$op.is_commutative() && !rhs.is_borrowed() {
let mut r = rhs.into_array();
if B::a_binary_assign::<T>(BinaryOp::$op, &mut r.0, &self.0) {
return r;
}
return broadcast_binary(BinaryOp::$op, self, &r);
}
rhs.with_array(|r| broadcast_binary(BinaryOp::$op, self, r))
}
}
impl<B: Backend, T: $bound> ops::$tr<Array<B, T>> for Scalar<B, T> {
type Output = Array<B, T>;
#[track_caller]
fn $f(self, rhs: Array<B, T>) -> Array<B, T> {
broadcast_binary(BinaryOp::$op, &self.to_array(), &rhs)
}
}
impl<B: Backend, T: $bound> ops::$tr<&Array<B, T>> for Scalar<B, T> {
type Output = Array<B, T>;
#[track_caller]
fn $f(self, rhs: &Array<B, T>) -> Array<B, T> {
broadcast_binary(BinaryOp::$op, &self.to_array(), rhs)
}
}
)*};
}
array_binop! {
Add add Add Numeric;
Sub sub Sub Numeric;
Mul mul Mul Numeric;
Div div Div Numeric;
Rem rem Rem Num;
BitAnd bitand And Element;
BitOr bitor Or Element;
BitXor bitxor Xor Element;
}
macro_rules! array_lhs_literal {
($($t:ty)*) => {$(
impl<B: Backend> ops::Add<Array<B, $t>> for $t {
type Output = Array<B, $t>;
fn add(self, rhs: Array<B, $t>) -> Array<B, $t> { broadcast_binary(BinaryOp::Add, &self.into_array(), &rhs) }
}
impl<B: Backend> ops::Sub<Array<B, $t>> for $t {
type Output = Array<B, $t>;
fn sub(self, rhs: Array<B, $t>) -> Array<B, $t> { broadcast_binary(BinaryOp::Sub, &self.into_array(), &rhs) }
}
impl<B: Backend> ops::Mul<Array<B, $t>> for $t {
type Output = Array<B, $t>;
fn mul(self, rhs: Array<B, $t>) -> Array<B, $t> { broadcast_binary(BinaryOp::Mul, &self.into_array(), &rhs) }
}
impl<B: Backend> ops::Div<Array<B, $t>> for $t {
type Output = Array<B, $t>;
fn div(self, rhs: Array<B, $t>) -> Array<B, $t> { broadcast_binary(BinaryOp::Div, &self.into_array(), &rhs) }
}
)*};
}
array_lhs_literal!(i8 i16 i32 i64 u8 u16 u32 u64 f16 bf16 f32 f64 c64 c128);
impl<B: Backend, T: Numeric> ops::Neg for Array<B, T> {
type Output = Self;
fn neg(self) -> Self {
self.into_unary(UnaryOp::Neg)
}
}
impl<B: Backend, T: Numeric> ops::Neg for &Array<B, T> {
type Output = Array<B, T>;
fn neg(self) -> Array<B, T> {
self.unary(UnaryOp::Neg)
}
}
impl<B: Backend, T: Element> ops::Not for Array<B, T> {
type Output = Self;
fn not(self) -> Self {
self.into_unary(UnaryOp::Not)
}
}
impl<B: Backend, T: Element> ops::Not for &Array<B, T> {
type Output = Array<B, T>;
fn not(self) -> Array<B, T> {
self.unary(UnaryOp::Not)
}
}
impl<B: Backend, T: Element> Array<B, T> {
#[track_caller]
pub fn eq(&self, other: impl ArrayLike<B, T>) -> Array<B, bool> {
broadcast_compare(CompareOp::Eq, self, &other.into_array())
}
#[track_caller]
pub fn ne(&self, other: impl ArrayLike<B, T>) -> Array<B, bool> {
broadcast_compare(CompareOp::Ne, self, &other.into_array())
}
#[track_caller]
pub fn lt(&self, other: impl ArrayLike<B, T>) -> Array<B, bool> {
broadcast_compare(CompareOp::Lt, self, &other.into_array())
}
#[track_caller]
pub fn le(&self, other: impl ArrayLike<B, T>) -> Array<B, bool> {
broadcast_compare(CompareOp::Le, self, &other.into_array())
}
#[track_caller]
pub fn gt(&self, other: impl ArrayLike<B, T>) -> Array<B, bool> {
broadcast_compare(CompareOp::Gt, self, &other.into_array())
}
#[track_caller]
pub fn ge(&self, other: impl ArrayLike<B, T>) -> Array<B, bool> {
broadcast_compare(CompareOp::Ge, self, &other.into_array())
}
}