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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use tenferro_ops::std_tensor_op::StdTensorOp;
use crate::eager::EagerTensor;
use crate::eager_ops::{broadcast_binary, broadcast_ternary};
use crate::error::Result;
use crate::CompareDir;
impl EagerTensor {
/// Elementwise absolute value.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![-1.0_f64, 2.0]).unwrap(), ctx.clone()).unwrap();
/// let y = x.abs().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[1.0, 2.0]);
/// ```
pub fn abs(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Abs)
}
/// Elementwise complex conjugate.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![1.0_f64, -2.0]).unwrap(), ctx.clone()).unwrap();
/// let y = x.conj().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[1.0, -2.0]);
/// ```
pub fn conj(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Conj)
}
/// Elementwise sign.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![-2.0_f64, 3.0]).unwrap(), ctx.clone()).unwrap();
/// let y = x.sign().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[-1.0, 1.0]);
/// ```
pub fn sign(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Sign)
}
/// Elementwise natural logarithm.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![1], vec![1.0_f64]).unwrap(), ctx.clone()).unwrap();
/// let y = x.log().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[0.0]);
/// ```
pub fn log(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Log)
}
/// Elementwise square root.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![1], vec![4.0_f64]).unwrap(), ctx.clone()).unwrap();
/// let y = x.sqrt().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[2.0]);
/// ```
pub fn sqrt(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Sqrt)
}
/// Elementwise reciprocal square root.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![1], vec![4.0_f64]).unwrap(), ctx.clone()).unwrap();
/// let y = x.rsqrt().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[0.5]);
/// ```
pub fn rsqrt(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Rsqrt)
}
/// Elementwise sine.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![1], vec![0.0_f64]).unwrap(), ctx.clone()).unwrap();
/// let y = x.sin().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[0.0]);
/// ```
pub fn sin(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Sin)
}
/// Elementwise cosine.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![1], vec![0.0_f64]).unwrap(), ctx.clone()).unwrap();
/// let y = x.cos().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[1.0]);
/// ```
pub fn cos(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Cos)
}
/// Elementwise hyperbolic tangent.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![1], vec![0.0_f64]).unwrap(), ctx.clone()).unwrap();
/// let y = x.tanh().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[0.0]);
/// ```
pub fn tanh(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Tanh)
}
/// Elementwise `exp(x) - 1`.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![1], vec![0.0_f64]).unwrap(), ctx.clone()).unwrap();
/// let y = x.expm1().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[0.0]);
/// ```
pub fn expm1(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Expm1)
}
/// Elementwise `log(1 + x)`.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![1], vec![0.0_f64]).unwrap(), ctx.clone()).unwrap();
/// let y = x.log1p().unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[0.0]);
/// ```
pub fn log1p(&self) -> Result<Self> {
self.unary_op(StdTensorOp::Log1p)
}
/// Elementwise division.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![3], vec![8.0_f64, -6.0, 9.0]).unwrap(), ctx.clone()).unwrap();
/// let y = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![3], vec![2.0_f64, 3.0, 3.0]).unwrap(), ctx.clone()).unwrap();
/// let z = x.div(&y).unwrap();
///
/// assert_eq!(z.materialized().unwrap().as_slice::<f64>().unwrap(), &[4.0, -2.0, 3.0]);
/// ```
pub fn div(&self, other: &Self) -> Result<Self> {
let (lhs, rhs) = broadcast_binary("div", self, other)?;
lhs.binary_op(&rhs, StdTensorOp::Div)
}
/// Elementwise power.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let base = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![2.0_f64, 3.0]).unwrap(), ctx.clone()).unwrap();
/// let exp = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![3.0_f64, 2.0]).unwrap(), ctx.clone()).unwrap();
/// let y = base.pow(&exp).unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[8.0, 9.0]);
/// ```
pub fn pow(&self, other: &Self) -> Result<Self> {
let (lhs, rhs) = broadcast_binary("pow", self, other)?;
lhs.binary_op(&rhs, StdTensorOp::Pow)
}
/// Elementwise maximum.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 5.0]).unwrap(), ctx.clone()).unwrap();
/// let y = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![3.0_f64, 4.0]).unwrap(), ctx.clone()).unwrap();
/// let z = x.maximum(&y).unwrap();
///
/// assert_eq!(z.materialized().unwrap().as_slice::<f64>().unwrap(), &[3.0, 5.0]);
/// ```
pub fn maximum(&self, other: &Self) -> Result<Self> {
let (lhs, rhs) = broadcast_binary("maximum", self, other)?;
lhs.binary_op(&rhs, StdTensorOp::Maximum)
}
/// Elementwise minimum.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 5.0]).unwrap(), ctx.clone()).unwrap();
/// let y = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![3.0_f64, 4.0]).unwrap(), ctx.clone()).unwrap();
/// let z = x.minimum(&y).unwrap();
///
/// assert_eq!(z.materialized().unwrap().as_slice::<f64>().unwrap(), &[1.0, 4.0]);
/// ```
pub fn minimum(&self, other: &Self) -> Result<Self> {
let (lhs, rhs) = broadcast_binary("minimum", self, other)?;
lhs.binary_op(&rhs, StdTensorOp::Minimum)
}
/// Elementwise comparison.
pub fn compare(&self, other: &Self, dir: CompareDir) -> Result<Self> {
let (lhs, rhs) = broadcast_binary("compare", self, other)?;
lhs.binary_op(&rhs, StdTensorOp::Compare(dir))
}
/// Select values from `on_true` or `on_false` using `condition`.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let condition = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![false, true]).unwrap(), ctx.clone()).unwrap();
/// let on_true = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![10.0_f64, 20.0]).unwrap(), ctx.clone()).unwrap();
/// let on_false = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap(), ctx.clone()).unwrap();
/// let y = EagerTensor::select(&condition, &on_true, &on_false).unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[1.0, 20.0]);
/// ```
pub fn select(condition: &Self, on_true: &Self, on_false: &Self) -> Result<Self> {
Self::where_select(condition, on_true, on_false)
}
/// Select values from `on_true` or `on_false` using `condition`.
pub fn where_select(condition: &Self, on_true: &Self, on_false: &Self) -> Result<Self> {
let (condition, on_true, on_false) =
broadcast_ternary("where_select", condition, on_true, on_false)?;
condition.ternary_op(&on_true, &on_false, StdTensorOp::Select)
}
/// Clamp values elementwise between lower and upper bounds.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![3], vec![-2.0_f64, 0.5, 5.0]).unwrap(), ctx.clone()).unwrap();
/// let lower = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![3], vec![-1.0_f64, 0.0, 1.0]).unwrap(), ctx.clone()).unwrap();
/// let upper = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![3], vec![1.0_f64, 2.0, 4.0]).unwrap(), ctx.clone()).unwrap();
/// let y = x.clamp(&lower, &upper).unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[-1.0, 0.5, 4.0]);
/// ```
pub fn clamp(&self, lower: &Self, upper: &Self) -> Result<Self> {
let (input, lower, upper) = broadcast_ternary("clamp", self, lower, upper)?;
input.ternary_op(&lower, &upper, StdTensorOp::Clamp)
}
}