coeus-ops 0.14.0

Numerical tensor operators for Coeus.
Documentation
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
// ── Unary math ops ──

use super::kernel::{elementwise_unary, elementwise_unary_assign};
use crate::backend_ops::{BackendOps, ElementwiseOps, UnaryOp};
use coeus_core::{Float, Scalar};
use coeus_tensor::Tensor;

/// Element-wise sine: sin(x).
///
/// Backward: `d/dx sin(x) = cos(x)`.
#[inline]
pub fn sin<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Sin)
}
/// Element-wise cosine: cos(x).
///
/// Backward: `d/dx cos(x) = -sin(x)`.
#[inline]
pub fn cos<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Cos)
}

/// Element-wise exponential.
///
/// # Examples
///
/// ```
/// use coeus_tensor::Tensor;
/// use coeus_core::SequentialBackend;
/// use coeus_ops::exp;
///
/// let backend = SequentialBackend::new();
/// let a = Tensor::<f32, SequentialBackend>::from_slice([2], &[0.0, 1.0])
///     .expect("example tensor allocation succeeds");
/// let b = exp(&a, &backend).expect("example exponential succeeds");
/// let s = b.as_slice();
/// assert!((s[0] - 1.0).abs() < 1e-5);
/// assert!((s[1] - std::f32::consts::E).abs() < 1e-5);
/// ```
#[inline]
pub fn exp<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Exp)
}

/// Element-wise natural log.
#[inline]
pub fn log<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Log)
}

/// Element-wise Gauss error function: erf(x) = (2/√π) ∫₀ˣ e^(−t²) dt.
///
/// # Examples
///
/// ```
/// use coeus_tensor::Tensor;
/// use coeus_core::SequentialBackend;
/// use coeus_ops::erf;
///
/// let backend = SequentialBackend::new();
/// let a = Tensor::<f64, SequentialBackend>::from_slice([2], &[0.0, 1.0])
///     .expect("example tensor allocation succeeds");
/// let b = erf(&a, &backend).expect("example error function succeeds");
/// let s = b.as_slice();
/// assert!(s[0].abs() < 1e-15);
/// assert!((s[1] - 0.842_700_792_949_714_9).abs() < 1e-12);
/// ```
#[inline]
pub fn erf<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Erf)
}

/// Element-wise complementary error function: erfc(x) = 1 − erf(x).
///
/// Backward: `d/dx erfc(x) = −d/dx erf(x) = −(2/√π)·e^(−x²)`.
#[inline]
pub fn erfc<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Erfc)
}

/// Element-wise natural logarithm of the absolute gamma function: `ln|Γ(x)|`.
///
/// Autograd for this operation requires `digamma(x)`, which is not exposed by
/// the current datatype-law provider.
#[inline]
pub fn lgamma<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Lgamma)
}

/// Element-wise tangent: tan(x).
///
/// Backward: `d/dx tan(x) = 1 / cos²(x)` (sec²(x)).
#[inline]
pub fn tan<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Tan)
}

/// Element-wise arc-sine: asin(x), domain x ∈ [-1, 1].
///
/// Backward: `d/dx asin(x) = 1 / sqrt(1 - x²)`.
#[inline]
pub fn asin<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Asin)
}

/// Element-wise arc-cosine: acos(x), domain x ∈ [-1, 1].
///
/// Backward: `d/dx acos(x) = -1 / sqrt(1 - x²)`.
#[inline]
pub fn acos<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Acos)
}

/// Element-wise arc-tangent: atan(x).
///
/// Backward: `d/dx atan(x) = 1 / (1 + x²)`.
#[inline]
pub fn atan<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Atan)
}

/// Element-wise hyperbolic sine: sinh(x).
/// Backward: d/dx sinh(x) = cosh(x).
#[inline]
pub fn sinh<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Sinh)
}
/// Element-wise hyperbolic cosine: cosh(x).
/// Backward: d/dx cosh(x) = sinh(x).
#[inline]
pub fn cosh<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Cosh)
}
/// Element-wise base-2 logarithm: log2(x).
/// Backward: d/dx log2(x) = 1/(x * ln(2)).
#[inline]
pub fn log2<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Log2)
}
/// Element-wise base-10 logarithm: log10(x).
/// Backward: d/dx log10(x) = 1/(x * ln(10)).
#[inline]
pub fn log10<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Log10)
}
/// Element-wise base-2 exponential: 2^x.
/// Backward: d/dx 2^x = 2^x * ln(2).
#[inline]
pub fn exp2<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Exp2)
}
/// Element-wise inverse hyperbolic tangent: atanh(x), domain |x| < 1.
/// Backward: d/dx atanh(x) = 1/(1 - x²).
#[inline]
pub fn atanh<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Atanh)
}
/// Element-wise inverse hyperbolic sine: asinh(x).
/// Backward: d/dx asinh(x) = 1/sqrt(x² + 1).
#[inline]
pub fn asinh<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Asinh)
}
/// Element-wise inverse hyperbolic cosine: acosh(x), domain x > 1.
/// Backward: d/dx acosh(x) = 1/sqrt(x² - 1).
#[inline]
pub fn acosh<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Acosh)
}
/// Element-wise exp(x) - 1.
/// Backward: d/dx expm1(x) = exp(x).
#[inline]
pub fn expm1<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Expm1)
}
/// Element-wise ln(1 + x).
/// Backward: d/dx log1p(x) = 1/(1 + x).
#[inline]
pub fn log1p<T: Float, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Log1p)
}

/// Element-wise negation (works for any Scalar).
///
/// # Examples
///
/// ```
/// use coeus_tensor::Tensor;
/// use coeus_core::SequentialBackend;
/// use coeus_ops::neg;
///
/// let backend = SequentialBackend::new();
/// let a = Tensor::<f32, SequentialBackend>::from_slice([3], &[1.0, -2.0, 3.0])
///     .expect("example tensor allocation succeeds");
/// let b = neg(&a, &backend).expect("example negation succeeds");
/// assert_eq!(b.as_slice(), &[-1.0, 2.0, -3.0]);
/// ```
#[inline]
pub fn neg<T: Scalar, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Neg)
}

/// Element-wise absolute value.
#[inline]
pub fn abs<T: Scalar, B: ElementwiseOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Abs)
}

/// Element-wise square root.
///
/// # Examples
///
/// ```
/// use coeus_tensor::Tensor;
/// use coeus_core::SequentialBackend;
/// use coeus_ops::sqrt;
///
/// let backend = SequentialBackend::new();
/// let a = Tensor::<f32, SequentialBackend>::from_slice([3], &[1.0, 4.0, 9.0])
///     .expect("example tensor allocation succeeds");
/// let b = sqrt(&a, &backend).expect("example square root succeeds");
/// let s = b.as_slice();
/// assert!((s[0] - 1.0).abs() < 1e-5);
/// assert!((s[1] - 2.0).abs() < 1e-5);
/// assert!((s[2] - 3.0).abs() < 1e-5);
/// ```
#[inline]
pub fn sqrt<T: Scalar, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Sqrt)
}

/// In-place element-wise sine.
#[inline]
pub fn sin_assign<T: Float, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Sin)?;
    Ok(())
}

/// In-place element-wise cosine.
#[inline]
pub fn cos_assign<T: Float, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Cos)?;
    Ok(())
}

/// In-place element-wise exponential.
#[inline]
pub fn exp_assign<T: Float, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Exp)?;
    Ok(())
}

/// In-place element-wise natural log.
#[inline]
pub fn log_assign<T: Float, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Log)?;
    Ok(())
}

/// In-place element-wise negation.
#[inline]
pub fn neg_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Neg)?;
    Ok(())
}

/// In-place element-wise absolute value.
#[inline]
pub fn abs_assign<T: Scalar, B: ElementwiseOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Abs)?;
    Ok(())
}

/// In-place element-wise square root.
#[inline]
pub fn sqrt_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Sqrt)?;
    Ok(())
}

/// Element-wise reciprocal: 1/x.
#[inline]
pub fn recip<T: Scalar, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Recip)
}

/// Element-wise signum: -1, 0, or 1.
#[inline]
pub fn sign<T: Scalar, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Sign)
}

/// Element-wise floor.
#[inline]
pub fn floor<T: Scalar, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Floor)
}

/// Element-wise ceil.
#[inline]
pub fn ceil<T: Scalar, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Ceil)
}

/// Element-wise round to nearest integer.
#[inline]
pub fn round<T: Scalar, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Round)
}

/// Element-wise truncation toward zero.
#[inline]
pub fn trunc<T: Scalar, B: BackendOps<T>>(
    input: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    elementwise_unary(input, backend, UnaryOp::Trunc)
}

/// In-place element-wise reciprocal.
#[inline]
pub fn recip_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Recip)?;
    Ok(())
}

/// In-place element-wise signum.
#[inline]
pub fn sign_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Sign)?;
    Ok(())
}

/// In-place element-wise floor.
#[inline]
pub fn floor_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Floor)?;
    Ok(())
}

/// In-place element-wise ceil.
#[inline]
pub fn ceil_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Ceil)?;
    Ok(())
}

/// In-place element-wise round.
#[inline]
pub fn round_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Round)?;
    Ok(())
}

/// In-place element-wise truncation.
#[inline]
pub fn trunc_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Trunc)?;
    Ok(())
}