-
Notifications
You must be signed in to change notification settings - Fork 6
/
vek.go
1689 lines (1494 loc) · 43.2 KB
/
vek.go
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package vek32
import (
"github.com/chewxy/math32"
"github.com/viterin/vek"
"github.com/viterin/vek/internal/functions"
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
"unsafe"
)
// Arithmetic
// Add returns the result of adding two slices element-wise.
func Add(x, y []float32) []float32 {
x = slices.Clone(x)
Add_Inplace(x, y)
return x
}
// Add_Inplace adds a slice element-wise to the first slice, inplace.
func Add_Inplace(x, y []float32) {
checkEqualLength(x, y)
checkOverlap(x, y)
if functions.UseAVX2 {
functions.Add_AVX2_F32(x, y)
} else {
functions.Add_Go(x, y)
}
}
// Add_Into adds two slices element-wise and stores the result in the destination slice.
func Add_Into(dst, x, y []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Add_Inplace(dst, y)
return dst
}
// Sub returns the result of subtracting two slices element-wise.
func Sub(x, y []float32) []float32 {
x = slices.Clone(x)
Sub_Inplace(x, y)
return x
}
// Sub_Inplace subtracts a slice element-wise from the first slice, inplace.
func Sub_Inplace(x, y []float32) {
checkEqualLength(x, y)
checkOverlap(x, y)
if functions.UseAVX2 {
functions.Sub_AVX2_F32(x, y)
} else {
functions.Sub_Go(x, y)
}
}
// Sub_Into subtracts two slices element-wise and stores the result in the destination slice.
func Sub_Into(dst, x, y []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Sub_Inplace(dst, y)
return dst
}
// Mul returns the result of multiplying two slices element-wise.
func Mul(x, y []float32) []float32 {
x = slices.Clone(x)
Mul_Inplace(x, y)
return x
}
// Mul_Inplace multiplies the first slice element-wise by the second, inplace.
func Mul_Inplace(x, y []float32) {
checkEqualLength(x, y)
checkOverlap(x, y)
if functions.UseAVX2 {
functions.Mul_AVX2_F32(x, y)
} else {
functions.Mul_Go(x, y)
}
}
// Mul_Into multiplies two slices element-wise and stores the result in the destination slice.
func Mul_Into(dst, x, y []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Mul_Inplace(dst, y)
return dst
}
// Div returns the result of dividing two slices element-wise.
func Div(x, y []float32) []float32 {
x = slices.Clone(x)
Div_Inplace(x, y)
return x
}
// Div_Inplace divides the first slice element-wise by the second, inplace.
func Div_Inplace(x, y []float32) {
checkEqualLength(x, y)
checkOverlap(x, y)
if functions.UseAVX2 {
functions.Div_AVX2_F32(x, y)
} else {
functions.Div_Go(x, y)
}
}
// Div_Into divides two slices element-wise and stores the result in the destination slice.
func Div_Into(dst, x, y []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Div_Inplace(dst, y)
return dst
}
// AddNumber returns the result of adding a number to each slice element.
func AddNumber(x []float32, a float32) []float32 {
x = slices.Clone(x)
AddNumber_Inplace(x, a)
return x
}
// AddNumber_Inplace adds a number to each slice element, inplace.
func AddNumber_Inplace(x []float32, a float32) {
if functions.UseAVX2 {
functions.AddNumber_AVX2_F32(x, a)
} else {
functions.AddNumber_Go(x, a)
}
}
// AddNumber_Into adds a number to each slice element and stores the result in the
// destination slice.
func AddNumber_Into(dst, x []float32, a float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
AddNumber_Inplace(dst, a)
return dst
}
// SubNumber returns the result of subtracting a number from each slice element.
func SubNumber(x []float32, a float32) []float32 {
x = slices.Clone(x)
SubNumber_Inplace(x, a)
return x
}
// SubNumber_Inplace subtracts a number from each slice element, inplace.
func SubNumber_Inplace(x []float32, a float32) {
if functions.UseAVX2 {
functions.SubNumber_AVX2_F32(x, a)
} else {
functions.SubNumber_Go(x, a)
}
}
// SubNumber_Into subtracts a number from each slice element and stores the result in the
// destination slice.
func SubNumber_Into(dst, x []float32, a float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
SubNumber_Inplace(dst, a)
return dst
}
// MulNumber returns the result of multiplying each slice element by a number.
func MulNumber(x []float32, a float32) []float32 {
x = slices.Clone(x)
MulNumber_Inplace(x, a)
return x
}
// MulNumber_Inplace multiplies each slice element by a number, inplace.
func MulNumber_Inplace(x []float32, a float32) {
if functions.UseAVX2 {
functions.MulNumber_AVX2_F32(x, a)
} else {
functions.MulNumber_Go(x, a)
}
}
// MulNumber_Into multiplies each slice element by a number and stores the result in the
// destination slice.
func MulNumber_Into(dst, x []float32, a float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
MulNumber_Inplace(dst, a)
return dst
}
// DivNumber returns the result of dividing each slice element by a number.
func DivNumber(x []float32, a float32) []float32 {
x = slices.Clone(x)
DivNumber_Inplace(x, a)
return x
}
// DivNumber_Inplace divides each slice element by a number, inplace.
func DivNumber_Inplace(x []float32, a float32) {
if functions.UseAVX2 {
functions.DivNumber_AVX2_F32(x, a)
} else {
functions.DivNumber_Go(x, a)
}
}
// DivNumber_Into divides each slice element by a number and stores the result in the
// destination slice.
func DivNumber_Into(dst, x []float32, a float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
DivNumber_Inplace(dst, a)
return dst
}
// Abs returns the absolute value of each slice element.
func Abs(x []float32) []float32 {
x = slices.Clone(x)
Abs_Inplace(x)
return x
}
// Abs_Inplace computes the absolute value of each slice element, inplace.
func Abs_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Abs_AVX2_F32(x)
} else {
functions.Abs_Go_F32(x)
}
}
// Abs_Into computes the absolute value of each slice element and stores the result in the
// destination slice.
func Abs_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Abs_Inplace(dst)
return dst
}
// Neg returns the additive inverse of each slice element.
func Neg(x []float32) []float32 {
x = slices.Clone(x)
Neg_Inplace(x)
return x
}
// Neg_Inplace computes the additive inverse of each slice element, inplace.
func Neg_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Neg_AVX2_F32(x)
} else {
functions.Neg_Go(x)
}
}
// Neg_Into computes the additive inverse of each slice element and stores the result in the
// destination slice.
func Neg_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Neg_Inplace(dst)
return dst
}
// Inv returns the multiplicative inverse of each slice element.
func Inv(x []float32) []float32 {
x = slices.Clone(x)
Inv_Inplace(x)
return x
}
// Inv_Inplace computes the multiplicative inverse of each slice element, inplace.
func Inv_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Inv_AVX2_F32(x)
} else {
functions.Inv_Go(x)
}
}
// Inv_Into computes the multiplicative inverse of each slice element and stores the result
// in the destination slice.
func Inv_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Inv_Inplace(dst)
return dst
}
// Aggregates
// Sum returns the sum of all elements in a slice.
func Sum(x []float32) float32 {
if functions.UseAVX2 {
return functions.Sum_AVX2_F32(x)
} else {
return functions.Sum_Go(x)
}
}
// CumSum returns the cumulative sum of a slice. The element at index i equals the sum of x[:i+1].
func CumSum(x []float32) []float32 {
x = slices.Clone(x)
CumSum_Inplace(x)
return x
}
// CumSum_Inplace computes the cumulative sum of a slice, inplace. The new element at index i
// equals the sum of x[:i+1].
func CumSum_Inplace(x []float32) {
if functions.UseAVX2 {
functions.CumSum_AVX2_F32(x)
} else {
functions.CumSum_Go(x)
}
}
// CumSum_Into computes the cumulative sum of a slice and stores the result in the destination
// slice. The element at index i equals the sum of x[:i+1].
func CumSum_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
CumSum_Inplace(dst)
return dst
}
// Prod returns the product of all elements in a slice.
func Prod(x []float32) float32 {
if functions.UseAVX2 {
return functions.Prod_AVX2_F32(x)
} else {
return functions.Prod_Go(x)
}
}
// CumProd returns the cumulative product of a slice. The element at index i equals the product
// of x[:i+1].
func CumProd(x []float32) []float32 {
x = slices.Clone(x)
CumProd_Inplace(x)
return x
}
// CumProd_Inplace computes the cumulative product of a slice, inplace. The new element at index i
// equals the product of x[:i+1].
func CumProd_Inplace(x []float32) {
if functions.UseAVX2 {
functions.CumProd_AVX2_F32(x)
} else {
functions.CumProd_Go(x)
}
}
// CumProd_Into computes the cumulative product of a slice and stores the result in the destination
// slice. The element at index i equals the product of x[:i+1].
func CumProd_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
CumProd_Inplace(dst)
return dst
}
// Mean returns the arithmetic average of the slice elements.
func Mean(x []float32) float32 {
checkNotEmpty(x)
if functions.UseAVX2 {
return functions.Mean_AVX2_F32(x)
} else {
return functions.Mean_Go(x)
}
}
// Median returns the median value of the slice elements.
func Median(x []float32) float32 {
checkNotEmpty(x)
if functions.UseAVX2 {
return functions.Median_AVX2_F32(x)
} else {
return functions.Median_Go(x)
}
}
// Quantile returns the q-th quantile of the slice elements. The value of q should be between
// 0 and 1 (inclusive).
func Quantile(x []float32, q float32) float32 {
if q < 0 || q > 1 {
panic("value of q should be between 0 and 1")
}
checkNotEmpty(x)
if functions.UseAVX2 {
return functions.Quantile_AVX2_F32(x, q)
} else {
return functions.Quantile_Go(x, q)
}
}
// Distance
// Dot returns the dot product of two vectors.
func Dot(x, y []float32) float32 {
checkNotEmpty(x)
checkEqualLength(x, y)
if functions.UseAVX2 {
return functions.Dot_AVX2_F32(x, y)
} else {
return functions.Dot_Go(x, y)
}
}
// Norm returns the Euclidean norm of a vector, i.e. its length.
func Norm(x []float32) float32 {
checkNotEmpty(x)
if functions.UseAVX2 {
return functions.Norm_AVX2_F32(x)
} else {
return functions.Norm_Go_F32(x)
}
}
// Distance returns the Euclidean distance between two vectors.
func Distance(x, y []float32) float32 {
checkNotEmpty(x)
checkEqualLength(x, y)
if functions.UseAVX2 {
return functions.Distance_AVX2_F32(x, y)
} else {
return functions.Distance_Go_F32(x, y)
}
}
// ManhattanNorm returns the sum of absolute values of the slice elements.
func ManhattanNorm(x []float32) float32 {
checkNotEmpty(x)
if functions.UseAVX2 {
return functions.ManhattanNorm_AVX2_F32(x)
} else {
return functions.ManhattanNorm_Go_F32(x)
}
}
// ManhattanDistance returns the sum of element-wise absolute differences between two slices.
func ManhattanDistance(x, y []float32) float32 {
checkNotEmpty(x)
checkEqualLength(x, y)
if functions.UseAVX2 {
return functions.ManhattanDistance_AVX2_F32(x, y)
} else {
return functions.ManhattanDistance_Go_F32(x, y)
}
}
// CosineSimilarity returns the cosine similarity of two vectors.
func CosineSimilarity(x, y []float32) float32 {
checkNotEmpty(x)
checkEqualLength(x, y)
if functions.UseAVX2 {
return functions.CosineSimilarity_AVX2_F32(x, y)
} else {
return functions.CosineSimilarity_Go_F32(x, y)
}
}
// Matrices
func checkDimensions[T constraints.Float](x, y []T, n int) (int, int) {
m := len(x) / n
p := len(y) / n
if m*n < len(x) || n*p < len(y) {
panic("slice lengths must be multiple of n")
}
return m, p
}
// MatMul multiplies an m-by-n and n-by-p matrix and returns the resulting m-by-p matrix.
// The matrices should be in row-major order. To multiply a matrix and a vector pass an
// n-by-1 matrix.
func MatMul(x, y []float32, n int) []float32 {
m, p := checkDimensions(x, y, n)
dst := make([]float32, m*p)
if functions.UseAVX2 {
functions.MatMul_Parallel_AVX2_F32(dst, x, y, m, n, p)
} else {
functions.MatMul_Parallel_Go(dst, x, y, m, n, p)
}
return dst
}
// MatMul_Into multiplies an m-by-n and n-by-p matrix and stores the resulting m-by-p matrix
// in the destination slice. The matrices should be in row-major order. To multiply a matrix
// and a vector pass an n-by-1 matrix.
func MatMul_Into(dst, x, y []float32, n int) []float32 {
m, p := checkDimensions(x, y, n)
if cap(dst) < m*p {
panic("destination slice not large enough to hold result")
}
Zeros_Into(dst, m*p)
if functions.UseAVX2 {
functions.MatMul_Parallel_AVX2_F32(dst, x, y, m, n, p)
} else {
functions.MatMul_Parallel_Go(dst, x, y, m, n, p)
}
return dst[:m*p]
}
// Mat4Mul multiplies two 4-by-4 matrices and returns the resulting 4-by-4 matrix. The matrices
// should be in row-major order. To multiply a matrix and a vector batch them into groups of 4.
func Mat4Mul(x, y []float32) []float32 {
var dst [16]float32
return Mat4Mul_Into(dst[:], x, y)
}
// Mat4Mul_Into multiplies two 4-by-4 matrices and stores the resulting 4-by-4 matrix in the
// destination slice. The matrices should be in row-major order. To multiply a matrix and a vector
// batch them into groups of 4.
func Mat4Mul_Into(dst, x, y []float32) []float32 {
// Note: skipping overlap check due to overhead
if cap(dst) < 16 || len(x) != 16 || len(y) != 16 {
panic("slices must be length 16 (4 by 4)")
}
if functions.UseAVX2 {
functions.Mat4Mul_AVX2_F32(dst, x, y)
} else {
functions.Mat4Mul_Go(dst, x, y)
}
return dst[:16]
}
// Special
// Sqrt returns the square root of each slice element.
func Sqrt(x []float32) []float32 {
x = slices.Clone(x)
Sqrt_Inplace(x)
return x
}
// Sqrt_Inplace computes the square root of each slice element, inplace.
func Sqrt_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Sqrt_AVX2_F32(x)
} else {
functions.Sqrt_Go_F32(x)
}
}
// Sqrt_Into computes the square root of each slice element and stores the result in the
// destination slice.
func Sqrt_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Sqrt_Inplace(dst)
return dst
}
// Round returns the result of rounding each slice element to the nearest integer value.
func Round(x []float32) []float32 {
x = slices.Clone(x)
Round_Inplace(x)
return x
}
// Round_Inplace rounds each slice element to the nearest integer value, inplace.
func Round_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Round_AVX2_F32(x)
} else {
functions.Round_Go_F32(x)
}
}
// Round_Into rounds each slice element to the nearest integer value and stores the result
// in the destination slice.
func Round_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Round_Inplace(dst)
return dst
}
// Floor returns the result of rounding each slice element to the nearest lesser integer value.
func Floor(x []float32) []float32 {
x = slices.Clone(x)
Floor_Inplace(x)
return x
}
// Floor_Inplace rounds each slice element to the nearest lesser integer value, inplace.
func Floor_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Floor_AVX2_F32(x)
} else {
functions.Floor_Go_F32(x)
}
}
// Floor_Into rounds each slice element to the nearest lesser integer value and stores the result
// in the destination slice.
func Floor_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Floor_Inplace(dst)
return dst
}
// Ceil returns the result of rounding each slice element to the nearest greater integer value.
func Ceil(x []float32) []float32 {
x = slices.Clone(x)
Ceil_Inplace(x)
return x
}
// Ceil_Inplace rounds each slice element to the nearest greater integer value, inplace.
func Ceil_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Ceil_AVX2_F32(x)
} else {
functions.Ceil_Go_F32(x)
}
}
// Ceil_Into rounds each slice element to the nearest greater integer value and stores the result
// in the destination slice.
func Ceil_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Ceil_Inplace(dst)
return dst
}
// Pow returns the elements in the first slice raised to the power in the second.
func Pow(x, y []float32) []float32 {
x = slices.Clone(x)
Pow_Inplace(x, y)
return x
}
// Pow_Inplace raises the elements in the first slice to the power in the second, inplace.
func Pow_Inplace(x, y []float32) {
checkEqualLength(x, y)
checkOverlap(x, y)
if functions.UseAVX2 {
functions.Pow_AVX2_F32(x, y)
} else {
functions.Pow_Go_F32(x, y)
}
}
// Pow_Into raises the elements in the first slice to the power in the second and stores the
// result in the destination slice.
func Pow_Into(dst, x, y []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Pow_Inplace(dst, y)
return dst
}
// Special (32-bit only)
// Sin returns the sine of each element.
func Sin(x []float32) []float32 {
x = slices.Clone(x)
Sin_Inplace(x)
return x
}
// Sin_Inplace computes the sine of each element, inplace.
func Sin_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Sin_AVX2_F32(x)
} else {
functions.Sin_Go_F32(x)
}
}
// Sin_Into stores the sine of each element in the destination slice.
func Sin_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Sin_Inplace(dst)
return dst
}
// Cos returns the cosine of each element.
func Cos(x []float32) []float32 {
x = slices.Clone(x)
Cos_Inplace(x)
return x
}
// Cos_Inplace computes the cosine of each element, inplace.
func Cos_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Cos_AVX2_F32(x)
} else {
functions.Cos_Go_F32(x)
}
}
// Cos_Into stores the cosine of each element in the destination slice.
func Cos_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Cos_Inplace(dst)
return dst
}
// SinCos_Into stores the sine and cosine of each element in the destination slices. Faster than
// calling Sin_Into and Cos_Into individually if both are needed.
func SinCos_Into(dstSin, dstCos, x []float32) {
dstSin = checkCapacity(dstSin, x)
dstCos = checkCapacity(dstCos, x)
checkOverlap(dstSin, x)
checkOverlap(dstCos, x)
if functions.UseAVX2 {
functions.SinCos_AVX2_F32(dstSin, dstCos, x)
} else {
functions.SinCos_Go_F32(dstSin, dstCos, x)
}
}
// Exp returns the exponential of each element.
func Exp(x []float32) []float32 {
x = slices.Clone(x)
Exp_Inplace(x)
return x
}
// Exp_Inplace computes the exponential of each element, inplace.
func Exp_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Exp_AVX2_F32(x)
} else {
functions.Exp_Go_F32(x)
}
}
// Exp_Into stores the exponential of each element in the destination slice.
func Exp_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Exp_Inplace(dst)
return dst
}
// Log returns the natural logarithm of each element.
func Log(x []float32) []float32 {
x = slices.Clone(x)
Log_Inplace(x)
return x
}
// Log_Inplace computes the natural logarithm of each element, inplace.
func Log_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Log_AVX2_F32(x)
} else {
functions.Log_Go_F32(x)
}
}
// Log_Into stores the natural logarithm of each element in the destination slice.
func Log_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Log_Inplace(dst)
return dst
}
// Log2 returns the base 2 logarithm of each element.
func Log2(x []float32) []float32 {
x = slices.Clone(x)
Log2_Inplace(x)
return x
}
// Log2_Inplace computes the base 2 logarithm of each element, inplace.
func Log2_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Log2_AVX2_F32(x)
} else {
functions.Log2_Go_F32(x)
}
}
// Log2_Into stores the base 2 logarithm of each element in the destination slice.
func Log2_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Log2_Inplace(dst)
return dst
}
// Log10 returns the base 10 logarithm of each element.
func Log10(x []float32) []float32 {
x = slices.Clone(x)
Log10_Inplace(x)
return x
}
// Log10_Inplace computes the base 10 logarithm of each element, inplace.
func Log10_Inplace(x []float32) {
if functions.UseAVX2 {
functions.Log10_AVX2_F32(x)
} else {
functions.Log10_Go_F32(x)
}
}
// Log10_Into stores the base 10 logarithm of each element in the destination slice.
func Log10_Into(dst, x []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Log10_Inplace(dst)
return dst
}
// Comparison
// Min returns the minimum value of a slice.
func Min(x []float32) float32 {
checkNotEmpty(x)
if functions.UseAVX2 {
return functions.Min_AVX2_F32(x)
} else {
return functions.Min_Go(x)
}
}
// ArgMin returns the (first) index of the minimum value of a slice.
func ArgMin(x []float32) int {
checkNotEmpty(x)
if functions.UseAVX2 {
return functions.ArgMin_AVX2_F32(x)
} else {
return functions.ArgMin_Go(x)
}
}
// Minimum returns the element-wise minimum values between two slices.
func Minimum(x, y []float32) []float32 {
x = slices.Clone(x)
Minimum_Inplace(x, y)
return x
}
// Minimum_Inplace compares two slices element-wise and replaces the values in the first slice
// with the minimum value.
func Minimum_Inplace(x, y []float32) {
checkEqualLength(x, y)
checkOverlap(x, y)
if functions.UseAVX2 {
functions.Minimum_AVX2_F32(x, y)
} else {
functions.Minimum_Go(x, y)
}
}
// Minimum_Into compares two slices element-wise and stores the minimum values in the destination
// slice.
func Minimum_Into(dst, x, y []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Minimum_Inplace(dst, y)
return dst
}
// MinimumNumber returns the minimum of a number and each slice element.
func MinimumNumber(x []float32, a float32) []float32 {
x = slices.Clone(x)
MinimumNumber_Inplace(x, a)
return x
}
// MinimumNumber_Inplace compares a number to each slice element and replaces the values in the
// slice with the minimum value.
func MinimumNumber_Inplace(x []float32, a float32) {
if functions.UseAVX2 {
functions.MinimumNumber_AVX2_F32(x, a)
} else {
functions.MinimumNumber_Go(x, a)
}
}
// MinimumNumber_Into compares a number to each slice element and stores the minimum values in
// the destination slice.
func MinimumNumber_Into(dst, x []float32, a float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
MinimumNumber_Inplace(dst, a)
return dst
}
// Max returns the maximum value of a slice.
func Max(x []float32) float32 {
checkNotEmpty(x)
if functions.UseAVX2 {
return functions.Max_AVX2_F32(x)
} else {
return functions.Max_Go(x)
}
}
// ArgMax returns the (first) index of the maximum value of a slice.
func ArgMax(x []float32) int {
checkNotEmpty(x)
if functions.UseAVX2 {
return functions.ArgMax_AVX2_F32(x)
} else {
return functions.ArgMax_Go(x)
}
}
// Maximum returns the element-wise maximum values between two slices.
func Maximum(x, y []float32) []float32 {
x = slices.Clone(x)
Maximum_Inplace(x, y)
return x
}
// Maximum_Inplace compares two slices element-wise and replaces the values in the first slice
// with the maximum value.
func Maximum_Inplace(x, y []float32) {
checkEqualLength(x, y)
checkOverlap(x, y)
if functions.UseAVX2 {
functions.Maximum_AVX2_F32(x, y)
} else {
functions.Maximum_Go(x, y)
}
}
// Maximum_Into compares two slices element-wise and stores the maximum values in the destination
// slice.
func Maximum_Into(dst, x, y []float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
Maximum_Inplace(dst, y)
return dst
}
// MaximumNumber returns the maximum of a number and each slice element.
func MaximumNumber(x []float32, a float32) []float32 {
x = slices.Clone(x)
MaximumNumber_Inplace(x, a)
return x
}
// MaximumNumber_Inplace compares a number to each slice element and replaces the values in the
// slice with the maximum value.
func MaximumNumber_Inplace(x []float32, a float32) {
if functions.UseAVX2 {
functions.MaximumNumber_AVX2_F32(x, a)
} else {
functions.MaximumNumber_Go(x, a)
}
}
// MaximumNumber_Into compares a number to each slice element and stores the maximum values in
// the destination slice.
func MaximumNumber_Into(dst, x []float32, a float32) []float32 {
dst = checkCapacity(dst, x)
checkOverlap(dst, x)
copy(dst, x)
MaximumNumber_Inplace(dst, a)
return dst
}
// Find returns the index of the first slice element equal to the given value, or -1 if not found.
func Find(x []float32, a float32) int {
if functions.UseAVX2 {