-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmath.v
More file actions
646 lines (489 loc) · 16.9 KB
/
Copy pathmath.v
File metadata and controls
646 lines (489 loc) · 16.9 KB
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
module vtl
import math
// abs returns the elementwise abs of an tensor
// abs exposes this operation as part of the public API.
// abs exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) abs[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
// TODO: Figure out a way to do this without casting to f64
return cast[T](math.abs(td(x).f64()))
})
}
// acos returns the elementwise acos of an tensor
// acos exposes this operation as part of the public API.
// acos exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) acos[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.acos(td(x).f64()))
})
}
// acosh returns the elementwise acosh of an tensor
// acosh exposes this operation as part of the public API.
// acosh exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) acosh[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.acosh(td(x).f64()))
})
}
// asin returns the elementwise asin of an tensor
// asin exposes this operation as part of the public API.
// asin exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) asin[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.asin(td(x).f64()))
})
}
// asinh returns the elementwise asinh of an tensor
// asinh exposes this operation as part of the public API.
// asinh exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) asinh[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.asinh(td(x).f64()))
})
}
// atan returns the elementwise atan of an tensor
// atan exposes this operation as part of the public API.
// atan exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) atan[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.atan(td(x).f64()))
})
}
// atan2 returns the atan2 elementwise of two tensors
// atan2 exposes this operation as part of the public API.
// atan2 exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) atan2[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap([b], fn [T](xs []T, _ []int) T {
x := xs[0]
y := xs[1]
return cast[T](math.atan2(td(x).f64(), td(y).f64()))
})
}
// atanh returns the elementwise atanh of an tensor
// atanh exposes this operation as part of the public API.
// atanh exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) atanh[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.atanh(td(x).f64()))
})
}
// cbrt returns the elementwise cbrt of an tensor
// cbrt exposes this operation as part of the public API.
// cbrt exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) cbrt[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.cbrt(td(x).f64()))
})
}
// ceil returns the elementwise ceil of an tensor
// ceil exposes this operation as part of the public API.
// ceil exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) ceil[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.ceil(td(x).f64()))
})
}
// cos returns the elementwise cos of an tensor
// cos exposes this operation as part of the public API.
// cos exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) cos[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.cos(td(x).f64()))
})
}
// cosh returns the elementwise cosh of an tensor
// cosh exposes this operation as part of the public API.
// cosh exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) cosh[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.cosh(td(x).f64()))
})
}
// cot returns the elementwise cot of an tensor
// cot exposes this operation as part of the public API.
// cot exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) cot[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.cot(td(x).f64()))
})
}
// degrees returns the elementwise degrees of an tensor
// degrees exposes this operation as part of the public API.
// degrees exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) degrees[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.degrees(td(x).f64()))
})
}
// erf returns the elementwise erf of an tensor
// erf exposes this operation as part of the public API.
// erf exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) erf[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.erf(td(x).f64()))
})
}
// erfc returns the elementwise erfc of an tensor
// erfc exposes this operation as part of the public API.
// erfc exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) erfc[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.erfc(td(x).f64()))
})
}
// exp returns the elementwise exp of an tensor
// exp exposes this operation as part of the public API.
// exp exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) exp[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.exp(td(x).f64()))
})
}
// exp2 returns the elementwise exp2 of an tensor
// exp2 exposes this operation as part of the public API.
// exp2 exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) exp2[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.exp2(td(x).f64()))
})
}
// expm1 returns the elementwise expm1 of an tensor
// expm1 exposes this operation as part of the public API.
// expm1 exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) expm1[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.expm1(td(x).f64()))
})
}
// f32_bits returns the elementwise f32_bits of an tensor
// f32_bits exposes this operation as part of the public API.
// f32_bits exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) f32_bits[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.f32_bits(td(x).f32()))
})
}
// f32_from_bits returns the elementwise f32_from_bits of an tensor
// f32_from_bits exposes this operation as part of the public API.
// f32_from_bits exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) f32_from_bits[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.f32_from_bits(td(x).u32()))
})
}
// f64_bits returns the elementwise f64_bits of an tensor
// f64_bits exposes this operation as part of the public API.
// f64_bits exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) f64_bits[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.f64_bits(td(x).f64()))
})
}
// f64_from_bits returns the elementwise f64_from_bits of an tensor
// f64_from_bits exposes this operation as part of the public API.
// f64_from_bits exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) f64_from_bits[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.f64_from_bits(td(x).u64()))
})
}
// factorial returns the elementwise factorial of an tensor
// factorial exposes this operation as part of the public API.
// factorial exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) factorial[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.factorial(td(x).f64()))
})
}
// floor returns the elementwise floor of an tensor
// floor exposes this operation as part of the public API.
// floor exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) floor[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.floor(td(x).f64()))
})
}
// fmod returns the fmod elementwise of two tensors
// fmod exposes this operation as part of the public API.
// fmod exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) fmod[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
x := xs[0]
y := xs[1]
return cast[T](math.fmod(td(x).f64(), td(y).f64()))
})
}
// gamma returns the elementwise gamma of an tensor
// gamma exposes this operation as part of the public API.
// gamma exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) gamma[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.gamma(td(x).f64()))
})
}
// gcd returns the gcd elementwise of two tensors
// gcd exposes this operation as part of the public API.
// gcd exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) gcd[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
x := xs[0]
y := xs[1]
return cast[T](math.gcd(td(x).i64(), td(y).i64()))
})
}
// hypot returns the hypot elementwise of two tensors
// hypot exposes this operation as part of the public API.
// hypot exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) hypot[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
x := xs[0]
y := xs[1]
return cast[T](math.hypot(td(x).f64(), td(y).f64()))
})
}
// lcm returns the lcm elementwise of two tensors
// lcm exposes this operation as part of the public API.
// lcm exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) lcm[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
x := xs[0]
y := xs[1]
return cast[T](math.lcm(td(x).i64(), td(y).i64()))
})
}
// log returns the elementwise log of an tensor
// log exposes this operation as part of the public API.
// log exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) log[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.log(td(x).f64()))
})
}
// log10 returns the elementwise log10 of an tensor
// log10 exposes this operation as part of the public API.
// log10 exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) log10[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.log10(td(x).f64()))
})
}
// log1p returns the elementwise log1p of an tensor
// log1p exposes this operation as part of the public API.
// log1p exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) log1p[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.log1p(td(x).f64()))
})
}
// log2 returns the elementwise log2 of an tensor
// log2 exposes this operation as part of the public API.
// log2 exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) log2[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.log2(td(x).f64()))
})
}
// log_factorial returns the elementwise log_factorial of an tensor
// log_factorial exposes this operation as part of the public API.
// log_factorial exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) log_factorial[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.log_factorial(td(x).f64()))
})
}
// log_gamma returns the elementwise log_gamma of an tensor
// log_gamma exposes this operation as part of the public API.
// log_gamma exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) log_gamma[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.log_gamma(td(x).f64()))
})
}
// log_n returns the log_n elementwise of two tensors
// log_n exposes this operation as part of the public API.
// log_n exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) log_n[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
x := xs[0]
y := xs[1]
return cast[T](math.log_n(td(x).f64(), td(y).f64()))
})
}
// max returns the max elementwise of two tensors
// max exposes this operation as part of the public API.
// max exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) max[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
return math.max(xs[0], xs[1])
})
}
// min returns the min elementwise of two tensors
// min exposes this operation as part of the public API.
// min exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) min[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
return math.min(xs[0], xs[1])
})
}
// nextafter returns the nextafter elementwise of two tensors
// nextafter exposes this operation as part of the public API.
// nextafter exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) nextafter[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
x := xs[0]
y := xs[1]
return cast[T](math.nextafter(td(x).f64(), td(y).f64()))
})
}
// nextafter32 returns the nextafter32 elementwise of two tensors
// nextafter32 exposes this operation as part of the public API.
// nextafter32 exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) nextafter32[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
x := xs[0]
y := xs[1]
return cast[T](math.nextafter32(td(x).f32(), td(y).f32()))
})
}
// pow returns the pow elementwise of two tensors
// pow exposes this operation as part of the public API.
// pow exposes this operation as part of the public API.
@[inline]
pub fn (a &Tensor[T]) pow[T](b &Tensor[T]) !&Tensor[T] {
return a.nmap[T]([b], fn [T](xs []T, _ []int) T {
x := xs[0]
y := xs[1]
return cast[T](math.pow(td(x).f64(), td(y).f64()))
})
}
// pow10 returns the elementwise pow10 of an tensor
// pow10 exposes this operation as part of the public API.
// pow10 exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) pow10[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.pow10(td(x).int()))
})
}
// radians returns the elementwise deg2rad of an tensor
// radians exposes this operation as part of the public API.
// radians exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) radians[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.radians(td(x).f64()))
})
}
// round rounds elements of an tensor elementwise
// round exposes this operation as part of the public API.
// round exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) round[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.round(td(x).f64()))
})
}
// round_to_even round_to_evens elements of an tensor elementwise
// round_to_even exposes this operation as part of the public API.
// round_to_even exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) round_to_even[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.round_to_even(td(x).f64()))
})
}
// sin returns the elementwise sin of an tensor
// sin exposes this operation as part of the public API.
// sin exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) sin[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.sin(td(x).f64()))
})
}
// sinh returns the elementwise sinh of an tensor
// sinh exposes this operation as part of the public API.
// sinh exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) sinh[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.sinh(td(x).f64()))
})
}
// sqrt returns the elementwise square root of an tensor
// sqrt exposes this operation as part of the public API.
// sqrt exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) sqrt[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.sqrt(td(x).f64()))
})
}
// tan returns the elementwise tan of an tensor
// tan exposes this operation as part of the public API.
// tan exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) tan[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.tan(td(x).f64()))
})
}
// tanh returns the elementwise tanh of an tensor
// tanh exposes this operation as part of the public API.
// tanh exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) tanh[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.tanh(td(x).f64()))
})
}
// trunc returns the elementwise trunc of an tensor
// trunc exposes this operation as part of the public API.
// trunc exposes this operation as part of the public API.
@[inline]
pub fn (t &Tensor[T]) trunc[T]() &Tensor[T] {
return t.map(fn [T](x T, _ []int) T {
return cast[T](math.trunc(td(x).f64()))
})
}