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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
// Copyright (C) 2025 zk4x
// SPDX-License-Identifier: LGPL-3.0-only WITH Classpath-exception-2.0
use std::ops::{Neg, Not};
use crate::{DType, Float, RT, Scalar, Tensor, error::ZyxError, kernel::UOp};
impl Tensor {
#[allow(clippy::needless_pass_by_value)]
fn poly_n(x: Tensor, coeffs: [f32; 5]) -> Tensor {
let mut result: Tensor = 0.0f32.into();
for c in coeffs {
result = result * x.clone() + Tensor::from(c);
}
result
}
/// Absolute value of each element.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-3.0f32, -1.0, 2.0]);
/// let y = t.abs();
/// ```
#[must_use]
pub fn abs(&self) -> Tensor {
self.relu() + (-self).relu()
}
/// Element-wise square: `x * x`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.0f32, -2.0, 3.0]);
/// let y = t.square();
/// ```
#[must_use]
pub fn square(&self) -> Tensor {
self.clone() * self.clone()
}
/// Element-wise sign: -1 for negatives, 1 for positives, 0 for zero.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.0, 5.0]);
/// let y = t.sign();
/// ```
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn sign(&self) -> Tensor {
let zero = Tensor::zeros_like(self.clone());
let neg_one = Tensor::from(-1i32);
let pos_one = Tensor::from(1i32);
let is_neg = self.clone().cmplt(zero.clone()).unwrap();
let result = is_neg.where_(&neg_one, &pos_one).unwrap();
self.nonzero().where_(&result, &zero).unwrap()
}
/// Error function `erf(x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, 0.5, 1.0]);
/// let y = t.erf();
/// ```
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn erf(&self) -> Tensor {
let x = self.float_cast().unwrap();
let t: Tensor = 1 / (1 + 0.327_591_1f32 * x.abs());
let coeffs = [1.061_405_4f32, -1.453_152_1, 1.421_413_8, -0.284_496_74, 0.254_829_6];
let poly = Self::poly_n(t.clone(), coeffs);
x.sign() * (1 - t * poly * (-x.clone() * x).exp())
}
/// Inverse error function `erfinv(x)`, defined for `|x| < 1`
/// via the Winitzki approximation.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, 0.5, -0.5]);
/// let y = t.erfinv();
/// ```
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn erfinv(&self) -> Tensor {
let x = self.float_cast().unwrap();
let a = Tensor::from(0.147f32);
let four_over_pi = Tensor::from(4.0f32 / core::f32::consts::PI);
let one = Tensor::from(1.0f32);
let xsq = x.clone().square();
let one_minus_xsq = one - xsq;
let l = one_minus_xsq.ln();
let a_val = a.clone();
let big_a = four_over_pi + a_val.clone() * l.clone();
let inner = big_a.clone().square() - (Tensor::from(4.0f32) * a_val.clone() * l.clone());
let t = (inner.sqrt() - big_a) / (Tensor::from(2.0f32) * a_val);
x.sign() * t.sqrt()
}
/// CELU activation: `max(0, x) + min(0, α*(exp(x/α) - 1))`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.celu(1.0f32);
/// ```
#[must_use]
pub fn celu(&self, alpha: impl Scalar) -> Tensor {
self.relu() - (-((self / alpha).exp() - 1) * alpha).relu()
}
/// Element-wise cosine.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, core::f32::consts::PI / 2.0]);
/// let y = t.cos();
/// ```
#[must_use]
pub fn cos(&self) -> Tensor {
let x = self.float_cast().unwrap();
Tensor { id: RT.lock().unary(x.id, UOp::Cos) }
}
/// Element-wise hyperbolic cosine: `cosh(x) = (exp(x) + exp(-x)) / 2`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, 1.0]);
/// let y = t.cosh();
/// ```
#[must_use]
pub fn cosh(&self) -> Tensor {
// (e^x + e^-x) / 2
let nx = self.neg();
let enx = nx.exp();
let ex = self.exp();
(ex + enx) / 2
}
/// Exponential Linear Unit (ELU) activation: `x` for `x > 0`,
/// `α*(exp(x) - 1)` otherwise.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.elu(1.0f32);
/// ```
#[must_use]
pub fn elu(&self, alpha: impl Scalar) -> Tensor {
self.relu() - (self.exp().neg() + 1).relu() * alpha
}
/// Element-wise power of 2: `2^x`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, 1.0, 2.0]);
/// let y = t.exp2();
/// ```
#[must_use]
pub fn exp2(&self) -> Tensor {
let x = self.float_cast().unwrap();
Tensor { id: RT.lock().unary(x.id, UOp::Exp2) }
}
/// Element-wise floor: greatest integer less than or equal to each element.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.2f32, -1.7, 3.0]);
/// let y = t.floor();
/// ```
#[must_use]
pub fn floor(&self) -> Tensor {
let x = self.float_cast().unwrap();
Tensor { id: RT.lock().unary(x.id, UOp::Floor) }
}
/// Element-wise truncation toward zero (drops the fractional part).
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.7f32, -2.3, 3.0]);
/// let y = t.trunc();
/// ```
#[must_use]
pub fn trunc(&self) -> Tensor {
let x = self.float_cast().unwrap();
Tensor { id: RT.lock().unary(x.id, UOp::Trunc) }
}
/// Element-wise exponential: `e^x`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, 1.0, -1.0]);
/// let y = t.exp();
/// ```
#[must_use]
pub fn exp(&self) -> Tensor {
let x = self.float_cast().unwrap();
Tensor { id: RT.lock().unary(x.id, UOp::Exp) }
}
/// Gaussian Error Linear Unit (Gelu) activation:
/// `gelu(x) = x * 0.5 * (1 + tanh(sqrt(2/π) * (x + x³ * 0.044715)))`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.gelu();
/// ```
#[allow(clippy::missing_panics_doc)]
#[must_use]
pub fn gelu(&self) -> Tensor {
self * 0.5f32 * (((self + self * self * self * 0.044_715f32) * (2f32 / core::f32::consts::PI).sqrt()).tanh() + 1f32)
}
/// Leaky ReLU activation: `max(0, x) + neg_slope * min(0, x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.leaky_relu(0.01f32);
/// ```
#[must_use]
pub fn leaky_relu(&self, neg_slope: impl Scalar) -> Tensor {
self.relu() - (self * (-Tensor::from(neg_slope))).relu()
}
/// Element-wise base-2 logarithm: `log2(x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.0f32, 2.0, 4.0]);
/// let y = t.log2();
/// ```
#[must_use]
pub fn log2(&self) -> Tensor {
let x = self.float_cast().unwrap();
return Tensor { id: RT.lock().unary(x.id, UOp::Log2) };
}
/// Element-wise natural logarithm: `ln(x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.0f32, 2.718, 10.0]);
/// let y = t.ln();
/// ```
#[must_use]
pub fn ln(&self) -> Tensor {
self.log2() * core::f64::consts::LN_2
}
/// Element-wise logarithm with an arbitrary base: `log_base(x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.0f32, 10.0, 100.0]);
/// let y = t.log(Tensor::from(10.0f32));
/// ```
#[must_use]
#[allow(clippy::suboptimal_flops)]
pub fn log(&self, base: impl Into<Tensor>) -> Tensor {
self.log2() / base.into().log2()
}
/// Mish activation: `x * tanh(softplus(x))`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.mish();
/// ```
#[must_use]
pub fn mish(&self) -> Tensor {
self * self.softplus(1., 20.).tanh()
}
/// QuickGELU activation: `x * sigmoid(1.702 * x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.quick_gelu();
/// ```
#[must_use]
pub fn quick_gelu(&self) -> Tensor {
self * (1.702f32 * self).sigmoid()
}
/// Element-wise reciprocal: `1 / x`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.5f32, 1.0, 2.0]);
/// let y = t.reciprocal();
/// ```
#[must_use]
pub fn reciprocal(&self) -> Tensor {
return Tensor { id: RT.lock().unary(self.id, UOp::Reciprocal) };
}
/// Rectified Linear Unit (ReLU) activation: `max(0, x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.relu();
/// ```
#[must_use]
#[track_caller]
#[allow(clippy::missing_panics_doc)]
pub fn relu(&self) -> Tensor {
self.cmpgt(0f32).unwrap() * self
}
/// Element-wise reciprocal square root: `1 / sqrt(x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.0f32, 4.0, 9.0]);
/// let y = t.rsqrt();
/// ```
#[must_use]
pub fn rsqrt(&self) -> Tensor {
let x = self.float_cast().unwrap();
Tensor { id: RT.lock().unary(x.id, UOp::Rsqrt) }
}
/// Self-Normalized Linear Unit (SELU) activation.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.selu();
/// ```
#[must_use]
pub fn selu(&self) -> Tensor {
1.050_701_f32 * (self.relu() - (1.673_263_2_f32 * (self.exp().neg() + 1)).relu())
}
/// Rounds each element to the nearest integer (half to even).
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.2f32, 2.7, 3.5, -1.5, -2.3]);
/// let y = t.round();
/// ```
#[must_use]
pub fn round(&self) -> Tensor {
let x = self.float_cast().unwrap();
let original_dtype = self.dtype();
// Round half to even (banker's rounding), matching torch.round and numpy.
let floor_x = x.floor();
let frac = x - floor_x.clone();
// Round up when the fraction is past the midpoint.
let round_up = frac.cmpgt(0.5f32).unwrap().cast(DType::F32);
// On a tie, round to the nearest even integer.
let is_half = frac.equal(0.5f32).unwrap().cast(DType::F32);
let floor_odd = (floor_x.clone() / 2.0f32).frac().ne(0.0f32).unwrap().cast(DType::F32);
let add = round_up + is_half * floor_odd;
let rounded = floor_x + add;
rounded.cast(original_dtype)
}
/// Element-wise fractional part: `x - floor(x)`, always non-negative.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.2f32, 2.7, 3.5, -1.7, -2.3]);
/// let y = t.frac();
/// ```
#[must_use]
pub fn frac(&self) -> Tensor {
let x = self.float_cast().unwrap();
let original_dtype = self.dtype();
// Fractional part = x - floor(x)
let fractional = x.clone() - x.floor();
// For negative numbers, add 1 to make fractional part positive
// For positive numbers, keep as is
let is_negative = fractional.cmplt(0).unwrap();
let fractional_positive = is_negative.clone() * (fractional.clone() + 1) + is_negative.not() * fractional;
fractional_positive.cast(original_dtype)
}
/// Element-wise ceiling: smallest integer greater than or equal to each
/// element.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.2f32, 2.7, 3.0, -1.7, -2.3]);
/// let y = t.ceil();
/// ```
#[must_use]
pub fn ceil(&self) -> Tensor {
let x = self.float_cast().unwrap();
let original_dtype = self.dtype();
// Since we don't have a direct ceil operation, we implement it using:
// ceil(x) = -floor(-x)
let ceiled = (-x).floor() * -1;
ceiled.cast(original_dtype)
}
/// Element-wise sigmoid: `1 / (1 + exp(-x))`, in [0, 1].
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.sigmoid();
/// ```
#[must_use]
pub fn sigmoid(&self) -> Tensor {
let exp_x = self.exp();
exp_x.clone() / (exp_x + 1)
}
/// Element-wise hard sigmoid: `clamp(x/6 + 0.5, 0, 1)` for `x > -3`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-4.0f32, -3.0, 0.0, 3.0, 4.0]);
/// let y = t.hard_sigmoid();
/// ```
#[must_use]
pub fn hard_sigmoid(&self) -> Tensor {
(self.cmpgt(-3).unwrap() * (self / 6 + 0.5)).minimum(1).unwrap()
}
/// Element-wise sine.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, core::f32::consts::PI / 2.0, core::f32::consts::PI]);
/// let y = t.sin();
/// ```
#[must_use]
pub fn sin(&self) -> Tensor {
let x = self.float_cast().unwrap();
Tensor { id: RT.lock().unary(x.id, UOp::Sin) }
}
/// Element-wise hyperbolic sine: `sinh(x) = (exp(x) - exp(-x)) / 2`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, 1.0]);
/// let y = t.sinh();
/// ```
#[must_use]
pub fn sinh(&self) -> Tensor {
// (e^x - e^-x) / 2
let nx = self.neg();
let enx = nx.exp();
let ex = self.exp();
(ex - enx) / 2
}
/// Softplus activation: `log(exp(beta * x) / beta)` for `beta * x > threshold`,
/// `beta * x` otherwise.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.softplus(1.0f32, 0.0);
/// ```
#[allow(clippy::missing_panics_doc)]
#[must_use]
pub fn softplus(&self, beta: impl Float, threshold: impl Float) -> Tensor {
let x = self * beta;
x.cmplt(threshold).unwrap().where_(((x).exp() + 1).ln() * beta.reciprocal(), x).unwrap()
}
/// Element-wise square root: `sqrt(x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.0f32, 4.0, 9.0]);
/// let y = t.sqrt();
/// ```
#[must_use]
pub fn sqrt(&self) -> Tensor {
let x = self.float_cast().unwrap();
Tensor { id: RT.lock().unary(x.id, UOp::Sqrt) }
}
/// Swish activation: `x * sigmoid(x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([-2.0f32, -0.5, 0.5, 2.0]);
/// let y = t.swish();
/// ```
#[must_use]
pub fn swish(&self) -> Tensor {
self * self.sigmoid()
}
/// Element-wise tangent: `sin(x) / cos(x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, core::f32::consts::PI / 4.0, core::f32::consts::PI]);
/// let y = t.tan();
/// ```
#[must_use]
pub fn tan(&self) -> Tensor {
self.sin() / self.cos()
}
/// Element-wise hyperbolic tangent: `tanh(x) = (exp(2x) - 1) / (exp(2x) + 1)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from(vec![0.5f32, 1.0]);
/// assert_eq!(t.tanh(), [0.46211715738221946f32, 0.761594166564993]);
/// ```
#[must_use]
pub fn tanh(&self) -> Tensor {
let exp2x = (self + self).exp();
(exp2x.clone() - 1) / (exp2x + 1)
}
/// Element-wise conversion of angles from degrees to radians.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, 90.0, 180.0]);
/// let y = t.deg2rad();
/// ```
#[must_use]
pub fn deg2rad(&self) -> Tensor {
self * (core::f32::consts::PI / 180.0)
}
/// Boolean tensor true where `self` and `other` are within
/// `atol + rtol * |other|` of each other.
///
/// # Example
///
/// ```rust no_run
/// # use zyx::Tensor;
/// let a = Tensor::from([0.1f32, 0.2, 0.3]);
/// let b = Tensor::from([0.1f32, 0.200001, 0.4]);
/// let y = a.isclose(b, Tensor::from(1e-5f32), Tensor::from(1e-8f32))?;
/// # Ok::<(), zyx::ZyxError>(())
/// ```
///
/// # Errors
///
/// Returns an error if the tensors have non-broadcastable shapes.
pub fn isclose(
&self,
other: impl Into<Tensor>,
rtol: impl Into<Tensor>,
atol: impl Into<Tensor>,
) -> Result<Tensor, ZyxError> {
let other = other.into();
let rtol = rtol.into();
let atol = atol.into();
let diff = (self - &other).abs();
let tolerance = &atol + &other * &rtol;
diff.cmplt(tolerance)
}
/// Boolean tensor true where elements are infinite.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.0f32, f32::INFINITY, f32::NEG_INFINITY]);
/// let y = t.isinf();
/// ```
#[must_use]
pub fn isinf(&self) -> Tensor {
self.equal(f32::INFINITY).unwrap()
}
/// Boolean tensor true where elements are NaN.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.0f32, f32::NAN, 0.0]);
/// let y = t.isnan();
/// ```
#[must_use]
pub fn isnan(&self) -> Tensor {
self.equal(f32::NAN).unwrap()
}
/// Element-wise base-10 logarithm: `log10(x)`.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([1.0f32, 10.0, 100.0]);
/// let y = t.log10();
/// ```
#[must_use]
pub fn log10(&self) -> Tensor {
(self.log(10)).cast(self.dtype())
}
/// Element-wise conversion of angles from radians to degrees.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0.0f32, core::f32::consts::PI / 2.0, core::f32::consts::PI]);
/// let y = t.rad2deg();
/// ```
#[must_use]
pub fn rad2deg(&self) -> Tensor {
self * (180.0 / core::f32::consts::PI)
}
/// Element-wise bitwise NOT.
///
/// # Example
///
/// ```rust
/// # use zyx::Tensor;
/// let t = Tensor::from([0u8, 255, 170, 85]);
/// let y = t.bitnot();
/// ```
#[must_use]
pub fn bitnot(&self) -> Tensor {
Tensor { id: RT.lock().unary(self.id, UOp::BitNot) }
}
/// Clamp elements to the range `[min, max]`, broadcastable.
///
/// # Example
///
/// ```rust no_run
/// # use zyx::{Tensor, DType};
/// let x = Tensor::from([0.5f32, 2.0, 3.5]);
/// let y = x.clamp(Tensor::from(1.0f32), Tensor::from(3.0f32))?;
/// # Ok::<(), zyx::ZyxError>(())
/// ```
///
/// # Errors
///
/// Returns an error if `min`/`max` are non-broadcastable with `self`.
#[allow(clippy::missing_panics_doc)]
pub fn clamp(&self, min: impl Into<Tensor>, max: impl Into<Tensor>) -> Result<Tensor, ZyxError> {
self.maximum(min.into())?.minimum(max.into())
}
/// Element-wise `self < rhs` (broadcastable), as a bool tensor.
///
/// # Example
///
/// ```rust no_run
/// # use zyx::{Tensor, DType};
/// let a = Tensor::from([1.0f32, 2.0, 3.0]);
/// let y = a.cmplt(Tensor::from([2.0f32, 2.0, 3.0]))?;
/// # Ok::<(), zyx::ZyxError>(())
/// ```
///
/// # Errors
///
/// Returns an error if the tensors are non-broadcastable.
pub fn cmplt(&self, rhs: impl Into<Tensor>) -> Result<Tensor, ZyxError> {
let (x, y) = Tensor::broadcast(self.clone(), rhs)?;
let id = RT.lock().binary(x.id, y.id, crate::kernel::BOp::Cmplt)?;
Ok(Tensor { id })
}
/// Element-wise `self > rhs` (broadcastable), as a bool tensor.
///
/// # Example
///
/// ```rust no_run
/// # use zyx::{Tensor, DType};
/// let a = Tensor::from([1.0f32, 2.0, 3.0]);
/// let y = a.cmpgt(Tensor::from([2.0f32, 2.0, 3.0]))?;
/// # Ok::<(), zyx::ZyxError>(())
/// ```
///
/// # Errors
///
/// Returns an error if the tensors are non-broadcastable.
pub fn cmpgt(&self, rhs: impl Into<Tensor>) -> Result<Tensor, ZyxError> {
let (x, y) = Tensor::broadcast(self.clone(), rhs)?;
let id = RT.lock().binary(x.id, y.id, crate::kernel::BOp::Cmpgt)?;
Ok(Tensor { id })
}
/// Element-wise `max(self, rhs)` (broadcastable).
///
/// # Example
///
/// ```rust no_run
/// # use zyx::{Tensor, DType};
/// let a = Tensor::from([1.0f32, 2.0, 3.0]);
/// let y = a.maximum(Tensor::from([2.0f32, 1.0, 3.0]))?;
/// # Ok::<(), zyx::ZyxError>(())
/// ```
///
/// # Errors
///
/// Returns an error if the tensors are non-broadcastable.
pub fn maximum(&self, rhs: impl Into<Tensor>) -> Result<Tensor, ZyxError> {
let (x, y) = Tensor::broadcast(self.clone(), rhs)?;
let id = RT.lock().binary(x.id, y.id, crate::kernel::BOp::Max)?;
Ok(Tensor { id })
}
/// Element-wise `min(self, rhs)` (broadcastable).
///
/// # Example
///
/// ```rust no_run
/// # use zyx::{Tensor, DType};
/// let a = Tensor::from([1.0f32, 2.0, 3.0]);
/// let y = a.minimum(Tensor::from([2.0f32, 1.0, 3.0]))?;
/// # Ok::<(), zyx::ZyxError>(())
/// ```
///
/// # Errors
///
/// Returns an error if the tensors are non-broadcastable.
pub fn minimum(&self, rhs: impl Into<Tensor>) -> Result<Tensor, ZyxError> {
Ok(-(-self).maximum(-rhs.into())?)
}
}