1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use snafu::ResultExt;
use super::*;
/// Unified macro for implementing Tensor operations.
///
/// Automatically handles:
/// - Binary operations (with `other` parameter): Always use Result path
/// - Unary operations with `@infallible` marker: Wrap in Ok()
/// - Unary operations without marker: Use Result path
macro_rules! impl_tensor_ops {
(
binary { $($bin_method:ident => $bin_uop:ident),* $(,)? }
unary_infallible { $($inf_method:ident => $inf_uop:ident),* $(,)? }
unary_fallible { $($fall_method:ident => $fall_uop:ident),* $(,)? }
) => {
// Binary operations (with automatic broadcasting)
$(
#[track_caller]
pub fn $bin_method(&self, other: &Tensor) -> Result<Tensor> {
// Broadcast tensors to common shape
let (lhs, rhs) = self.broadcast_for_binop(other)?;
// Now call UOp operation with matching shapes
lhs.uop().$bin_uop(&rhs.uop()).map(Self::new).context(UOpSnafu)
}
)*
// Unary infallible operations
$(
#[track_caller]
pub fn $inf_method(&self) -> Result<Tensor> {
Ok(Self::new(self.uop().$inf_uop()))
}
)*
// Unary fallible operations
$(
#[track_caller]
pub fn $fall_method(&self) -> Result<Tensor> {
self.uop().$fall_uop().map(Self::new).context(UOpSnafu)
}
)*
};
}
impl Tensor {
impl_tensor_ops! {
binary {
try_add => try_add,
try_sub => try_sub,
try_mul => try_mul,
try_div => try_div,
try_mod => try_mod,
try_pow => try_pow,
try_eq => try_cmpeq,
try_ne => try_cmpne,
try_lt => try_cmplt,
try_le => try_cmple,
try_gt => try_cmpgt,
try_ge => try_cmpge,
try_bitor => try_or_op,
try_bitand => try_and_op,
try_bitxor => try_xor_op,
try_shl => try_shl_op,
try_shr => try_shr_op,
}
unary_infallible {
try_neg => neg,
try_abs => abs,
}
unary_fallible {
try_sqrt => try_sqrt,
try_rsqrt => try_rsqrt,
try_exp => try_exp,
try_exp2 => try_exp2,
try_log => try_log,
try_log2 => try_log2,
}
}
/// Logical NOT for boolean tensors.
///
/// Converts to boolean dtype and applies logical negation.
/// For non-boolean tensors, treats zero as false, non-zero as true.
///
/// # Examples
/// ```ignore
/// let t = Tensor::from_slice(&[true, false, true]);
/// let result = t.logical_not()?; // [false, true, false]
///
/// let nums = Tensor::from_slice(&[0.0f32, 1.0, 2.0]);
/// let result = nums.logical_not()?; // [true, false, false]
/// ```
pub fn logical_not(&self) -> Result<Tensor> {
use svod_dtype::DType;
// Cast to bool (non-zero becomes true)
let as_bool = self.cast(DType::Bool)?;
// Create true constant tensor and broadcast to match shape
let true_scalar = Self::from_slice([true]);
let self_shape = as_bool.shape()?;
let true_broadcast = if self_shape.is_empty() {
// Input is scalar - reshape [1] to []
true_scalar.try_reshape(&[] as &[isize])?
} else {
// Broadcast to match non-scalar shape
true_scalar.broadcast_to(&self_shape)?
};
// Compare: !x ≡ (x != true)
as_bool.try_ne(&true_broadcast)
}
/// Bitwise NOT for integer tensors.
///
/// Applies bitwise NOT operation using two's complement: `~x = -x - 1`.
/// Only works for integer dtypes.
///
/// # Examples
/// ```ignore
/// let t = Tensor::from_slice(&[0i32, 1, 2, -1]);
/// let result = t.bitwise_not()?; // [-1, -2, -3, 0]
/// ```
///
/// # Errors
///
/// Returns error if called on non-integer dtype.
pub fn bitwise_not(&self) -> Result<Tensor> {
// Verify dtype is integer
let dtype = self.uop().dtype();
if !dtype.is_int() {
return Err(Error::SymbolicShapeUnsupported {
operation: format!("bitwise_not on non-integer dtype {:?}", dtype),
});
}
// Bitwise NOT using two's complement: ~x = -x - 1
let negated = self.try_neg()?;
let one_scalar = Self::from_slice([1i32]).cast(dtype)?;
// Broadcast one to match self shape
let self_shape = self.shape()?;
let one_broadcast = if self_shape.is_empty() {
// Input is scalar - reshape [1] to []
one_scalar.try_reshape(&[] as &[isize])?
} else {
// Broadcast to match non-scalar shape
one_scalar.broadcast_to(&self_shape)?
};
negated.try_sub(&one_broadcast)
}
}