-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAlgorithm.hs
More file actions
812 lines (765 loc) · 21.2 KB
/
Copy pathAlgorithm.hs
File metadata and controls
812 lines (765 loc) · 21.2 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
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
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
--------------------------------------------------------------------------------
-- |
-- Module : ArrayFire.Algorithm
-- Copyright : David Johnson (c) 2019-2026
-- License : BSD 3
-- Maintainer : David Johnson <code@dmj.io>
-- Stability : Experimental
-- Portability : GHC
--
-- Functions for aggregation, manipulation of 'Array'
--
-- @
-- module Main where
--
-- import qualified ArrayFire as A
--
-- main :: IO ()
-- main = print $ A.sum (A.vector @Double 10 [1..]) 0
-- -- ArrayFire Array
-- -- [1 1 1 1]
-- -- 55.0000
-- @
--------------------------------------------------------------------------------
module ArrayFire.Algorithm where
import Data.Word (Word32)
import Foreign.C.Types (CBool)
import ArrayFire.Arith (cast)
import ArrayFire.FFI
import ArrayFire.Internal.Algorithm
import ArrayFire.Internal.Types
-- | Sum all of the elements in 'Array' along the specified dimension
--
-- >>> A.sum (A.vector @Double 10 [1..]) 0
-- ArrayFire Array
-- [1 1 1 1]
-- 55.0000
--
-- >>> A.sum (A.matrix @Double (10,10) $ replicate 10 [1..]) 1
-- ArrayFire Array
-- [10 1 1 1]
-- 10.0000
-- 20.0000
-- 30.0000
-- 40.0000
-- 50.0000
-- 60.0000
-- 70.0000
-- 80.0000
-- 90.0000
-- 100.0000
--
sum
:: AFType a
=> Array a
-- ^ Array to sum
-> Int
-- ^ 0-based Dimension along which to perform sum
-> Array a
-- ^ Will return the sum of all values in the input array along the specified dimension
sum x (fromIntegral -> n) = (x `op1` (\p a -> af_sum p a n))
-- | Sum all of the elements in 'Array' along the specified dimension, using a default value for NaN
--
-- >>> let nan = 0/0 in A.sumNaN (A.vector @Double 10 (nan : [1..])) 0 10.0
-- ArrayFire Array
-- [1 1 1 1]
-- 55.0000
sumNaN
:: (Fractional a, AFType a)
=> Array a
-- ^ Array to sum
-> Int
-- ^ Dimension along which to perform sum
-> Double
-- ^ Default value to use in the case of NaN
-> Array a
-- ^ Will return the sum of all values in the input array along the specified dimension, substituted with the default value
sumNaN n (fromIntegral -> i) d = (n `op1` (\p a -> af_sum_nan p a i d))
-- | Product all of the elements in 'Array' along the specified dimension
--
-- >>> A.product (A.vector @Double 10 [1..]) 0
-- ArrayFire Array
-- [1 1 1 1]
-- 3628800.0000
product
:: AFType a
=> Array a
-- ^ Array to product
-> Int
-- ^ Dimension along which to perform product
-> Array a
-- ^ Will return the product of all values in the input array along the specified dimension
product x (fromIntegral -> n) = (x `op1` (\p a -> af_product p a n))
-- | Product all of the elements in 'Array' along the specified dimension, using a default value for NaN
--
-- >>> let nan = 0/0 in A.productNaN (A.vector @Double 10 (nan : [1..])) 0 2.0
-- ArrayFire Array
-- [1 1 1 1]
-- 3628800.0000
productNaN
:: (AFType a, Fractional a)
=> Array a
-- ^ Array to product
-> Int
-- ^ Dimension along which to perform product
-> Double
-- ^ Default value to use in the case of NaN
-> Array a
-- ^ Will return the product of all values in the input array along the specified dimension, substituted with the default value
productNaN n (fromIntegral -> i) d = n `op1` (\p a -> af_product_nan p a i d)
-- | Take the minimum of an 'Array' along a specific dimension
--
-- >>> A.min (A.vector @Double 10 [1..]) 0
-- ArrayFire Array
-- [1 1 1 1]
-- 1.0000
min
:: AFType a
=> Array a
-- ^ Array input
-> Int
-- ^ Dimension along which to retrieve the min element
-> Array a
-- ^ Will contain the minimum of all values in the input array along dim
min x (fromIntegral -> n) = x `op1` (\p a -> af_min p a n)
-- | Take the maximum of an 'Array' along a specific dimension
--
-- >>> A.max (A.vector @Double 10 [1..]) 0
-- ArrayFire Array
-- [1 1 1 1]
-- 10.0000
max
:: AFType a
=> Array a
-- ^ Array input
-> Int
-- ^ Dimension along which to retrieve the max element
-> Array a
-- ^ Will contain the maximum of all values in the input array along dim
max x (fromIntegral -> n) = x `op1` (\p a -> af_max p a n)
-- | Find if all elements in an 'Array' are 'True' along a dimension
--
-- >>> A.allTrue (A.vector @CBool 10 (repeat 1)) 0
-- ArrayFire Array
-- [1 1 1 1]
-- 1
allTrue
:: AFType a
=> Array a
-- ^ Array input
-> Int
-- ^ Dimension along which to see if all elements are True
-> Array CBool
-- ^ Will contain 1 where all elements along dim are true, 0 otherwise
allTrue x (fromIntegral -> n) =
x `op1` (\p a -> af_all_true p a n)
-- | Find if any elements in an 'Array' are 'True' along a dimension
--
-- >>> A.anyTrue (A.vector @CBool 10 (repeat 0)) 0
-- ArrayFire Array
-- [1 1 1 1]
-- 0
anyTrue
:: AFType a
=> Array a
-- ^ Array input
-> Int
-- ^ Dimension along which to see if any elements are True
-> Array CBool
-- ^ Will contain 1 where any element along dim is true, 0 otherwise
anyTrue x (fromIntegral -> n) =
(x `op1` (\p a -> af_any_true p a n))
-- | Count elements in an 'Array' along a dimension
--
-- >>> A.count (A.vector @Double 10 [1..]) 0
-- ArrayFire Array
-- [1 1 1 1]
-- 10
count
:: forall a . AFType a
=> Array a
-- ^ Array input
-> Int
-- ^ Dimension along which to count
-> Array Int
-- ^ Count of all elements along dimension
count x (fromIntegral -> n) =
-- af_count produces a u32 array; cast to s64 so the data matches the
-- declared element type (otherwise host reads via toVector/toList would
-- read 8 bytes per element from a 4-byte-per-element buffer).
cast (x `op1` (\p a -> af_count p a n) :: Array Word32)
-- | Sum all elements in an 'Array' along all dimensions
--
-- >>> A.sumAll (A.vector @Double 10 [1..])
-- (55.0,0.0)
sumAll
:: forall a . AFResult a
=> Array a
-- ^ Input array
-> Scalar a
-- ^ imaginary and real part
sumAll = toAFResult @a . (`infoFromArray2` af_sum_all)
-- | Sum all elements in an 'Array' along all dimensions, using a default value for NaN
--
-- >>> let nan = 0/0 in A.sumNaNAll (A.vector @Double 10 (nan : [1..])) 0.0
-- (55.0,0.0)
sumNaNAll
:: forall a . (AFResult a, Fractional a)
=> Array a
-- ^ Input array
-> Double
-- ^ NaN substitute
-> Scalar a
-- ^ imaginary and real part
sumNaNAll a d = toAFResult @a $ infoFromArray2 a (\p g x -> af_sum_nan_all p g x d)
-- | Product all elements in an 'Array' along all dimensions, using a default value for NaN
--
-- >>> A.productAll (A.vector @Double 10 [1..])
-- (3628800.0,0.0)
productAll
:: forall a . AFResult a
=> Array a
-- ^ Input array
-> Scalar a
-- ^ imaginary and real part
productAll = toAFResult @a . (`infoFromArray2` af_product_all)
-- | Product all elements in an 'Array' along all dimensions, using a default value for NaN
--
-- >>> A.productNaNAll (A.vector @Double 10 [1..]) 1.0
-- (3628800.0,0.0)
productNaNAll
:: forall a . (AFResult a, Fractional a)
=> Array a
-- ^ Input array
-> Double
-- ^ NaN substitute
-> Scalar a
-- ^ imaginary and real part
productNaNAll a d = toAFResult @a $ infoFromArray2 a (\p x y -> af_product_nan_all p x y d)
-- | Take the minimum across all elements along all dimensions in 'Array'
--
-- >>> A.minAll (A.vector @Double 10 [1..])
-- (1.0,0.0)
minAll
:: forall a . AFResult a
=> Array a
-- ^ Input array
-> Scalar a
-- ^ imaginary and real part
minAll = toAFResult @a . (`infoFromArray2` af_min_all)
-- | Take the maximum across all elements along all dimensions in 'Array'
--
-- >>> A.maxAll (A.vector @Double 10 [1..])
-- (10.0,0.0)
maxAll
:: forall a . AFResult a
=> Array a
-- ^ Input array
-> Scalar a
-- ^ imaginary and real part
maxAll = toAFResult @a . (`infoFromArray2` af_max_all)
-- | Decide if all elements along all dimensions in 'Array' are True
--
-- >>> A.allTrueAll (A.vector @CBool 10 (repeat 1))
-- (1.0, 0.0)
allTrueAll
:: forall a . AFResult a
=> Array a
-- ^ Input array
-> Scalar a
-- ^ imaginary and real part
allTrueAll = toAFResult @a . (`infoFromArray2` af_all_true_all)
-- | Decide if any elements along all dimensions in 'Array' are True
--
-- >>> A.anyTrueAll $ A.vector @CBool 10 (repeat 0)
-- (0.0,0.0)
anyTrueAll
:: forall a . AFResult a
=> Array a
-- ^ Input array
-> Scalar a
-- ^ imaginary and real part
anyTrueAll = toAFResult @a . (`infoFromArray2` af_any_true_all)
-- | Count all elements along all dimensions in 'Array'
--
-- >>> A.countAll (A.matrix @Double (100,100) (replicate 100 [1..]))
-- (10000.0,0.0)
countAll
:: forall a . AFResult a
=> Array a
-- ^ Input array
-> Scalar a
-- ^ imaginary and real part
countAll = toAFResult @a . (`infoFromArray2` af_count_all)
-- | Find the minimum element along a specified dimension in 'Array'
--
-- >>> A.imin (A.vector @Double 10 [1..]) 0
-- (ArrayFire Array
-- [1 1 1 1]
-- 1.0000
-- ,ArrayFire Array
-- [1 1 1 1]
-- 0
-- )
imin
:: AFType a
=> Array a
-- ^ Input array
-> Int
-- ^ The dimension along which the minimum value is extracted
-> (Array a, Array Word32)
-- ^ will contain the minimum of all values along dim, will also contain the location of minimum of all values in in along dim
imin a (fromIntegral -> n) = op2p a (\x y z -> af_imin x y z n)
-- | Find the maximum element along a specified dimension in 'Array'
--
-- >>> A.imax (A.vector @Double 10 [1..]) 0
-- (ArrayFire Array
-- [1 1 1 1]
-- 10.0000
-- ,ArrayFire Array
-- [1 1 1 1]
-- 9
-- )
imax
:: AFType a
=> Array a
-- ^ Input array
-> Int
-- ^ The dimension along which the minimum value is extracted
-> (Array a, Array Word32)
-- ^ will contain the maximum of all values in in along dim, will also contain the location of maximum of all values in in along dim
imax a (fromIntegral -> n) = op2p a (\x y z -> af_imax x y z n)
-- | Find the minimum element along all dimensions in 'Array'
--
-- >>> A.iminAll (A.vector @Double 10 [1..])
-- (1.0,0.0,0)
iminAll
:: forall a . AFResult a
=> Array a
-- ^ Input array
-> (Scalar a, Int)
-- ^ will contain the real part of minimum value of all elements in input in, also will contain the imaginary part of minimum value of all elements in input in, will contain the location of minimum of all values in
iminAll a = do
let (x,y,fromIntegral -> z) = a `infoFromArray3` af_imin_all
(toAFResult @a (x,y), z)
-- | Find the maximum element along all dimensions in 'Array'
--
-- >>> A.imaxAll (A.vector @Double 10 [1..])
-- (10.0,0.0,9)
imaxAll
:: forall a . AFResult a
=> Array a
-- ^ Input array
-> (Scalar a, Int)
-- ^ will contain the real part of maximum value of all elements in input in, also will contain the imaginary part of maximum value of all elements in input in, will contain the location of maximum of all values in
imaxAll a = do
let (x,y,fromIntegral -> z) = a `infoFromArray3` af_imax_all
(toAFResult @a (x,y), z)
-- | Calculate sum of 'Array' across specified dimension
--
-- >>> A.accum (A.vector @Double 10 [1..]) 0
-- ArrayFire Array
-- [10 1 1 1]
-- 1.0000
-- 3.0000
-- 6.0000
-- 10.0000
-- 15.0000
-- 21.0000
-- 28.0000
-- 36.0000
-- 45.0000
-- 55.0000
accum
:: AFType a
=> Array a
-- ^ Input array
-> Int
-- ^ Dimension along which to calculate the sum
-> Array a
-- ^ Contains inclusive sum
accum a (fromIntegral -> n) = a `op1` (\x y -> af_accum x y n)
-- | Scan elements of an 'Array' across a dimension, using a 'BinaryOp', specifying inclusivity.
--
-- >>> A.scan (A.vector @Double 10 [1..]) 0 Add True
-- ArrayFire Array
-- [10 1 1 1]
-- 1.0000
-- 3.0000
-- 6.0000
-- 10.0000
-- 15.0000
-- 21.0000
-- 28.0000
-- 36.0000
-- 45.0000
-- 55.0000
scan
:: AFType a
=> Array a
-- ^ The input array
-> Int
-- ^ The dimension along which the scan is performed
-> BinaryOp
-- ^ Binary operation to be used
-> Bool
-- ^ Should the scan be inclusive or not
-> Array a
-- ^ The scan of the input
scan a (fromIntegral -> d) op (fromIntegral . fromEnum -> inclusive) =
a `op1` (\x y -> af_scan x y d (toBinaryOp op) inclusive)
-- | Scan elements of an 'Array' across a dimension, by key, using a 'BinaryOp', specifying inclusivity.
--
-- >>> A.scanByKey (A.vector @Int 7 [2..]) (A.vector @Int 10 [1..]) 1 Add True
-- ArrayFire Array
-- [10 1 1 1]
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
-- 10
scanByKey
:: (AFType a, AFType k)
=> Array k
-- ^ The key array
-> Array a
-- ^ The input array
-> Int
-- ^ Dimension along which scan is performed
-> BinaryOp
-- ^ Type of binary operation used
-> Bool
-- ^ Is the scan incluside or not
-> Array a
scanByKey a b (fromIntegral -> d) op (fromIntegral . fromEnum -> inclusive) =
op2 a b (\x y z -> af_scan_by_key x y z d (toBinaryOp op) inclusive)
-- | Find indices where input Array is non zero
--
-- >>> A.where' (A.vector @Double 10 (repeat 0))
-- ArrayFire Array
-- [0 1 1 1]
-- <empty>
where'
:: AFType a
=> Array a
-- ^ Is the input array.
-> Array Word32
-- ^ Indices where input array is non-zero
where' = (`op1` af_where)
-- | First order numerical difference along specified dimension.
--
-- >>> A.diff1 (A.vector @Double 4 [10,35,65,95]) 0
-- ArrayFire Array
-- [3 1 1 1]
-- 25.0000
-- 30.0000
-- 30.0000
diff1
:: AFType a
=> Array a
-- ^ Input array
-> Int
-- ^ Dimension along which numerical difference is performed
-> Array a
-- ^ Will contain first order numerical difference
diff1 a (fromIntegral -> n) = a `op1` (\p x -> af_diff1 p x n)
-- | Second order numerical difference along specified dimension.
--
-- >>> A.diff2 (A.vector @Double 5 [1.0,20,55,89,44]) 0
-- ArrayFire Array
-- [3 1 1 1]
-- 16.0000
-- -1.0000
-- -79.0000
diff2
:: AFType a
=> Array a
-- ^ Input array
-> Int
-- ^ Dimension along which numerical difference is performed
-> Array a
-- ^ Will contain second order numerical difference
diff2 a (fromIntegral -> n) = a `op1` (\p x -> af_diff2 p x n)
-- | Sort an Array along a specified dimension, specifying ordering of results (ascending / descending)
--
-- >>> A.sort (A.vector @Double 4 [ 2,4,3,1 ]) 0 Asc
-- ArrayFire Array
-- [4 1 1 1]
-- 1.0000
-- 2.0000
-- 3.0000
-- 4.0000
--
-- >>> A.sort (A.vector @Double 4 [ 2,4,3,1 ]) 0 Desc
-- ArrayFire Array
-- [4 1 1 1]
-- 4.0000
-- 3.0000
-- 2.0000
-- 1.0000
sort
:: AFType a
=> Array a
-- ^ Input array
-> Int
-- ^ Dimension along `sort` is performed
-> Order
-- ^ Return results in ascending order
-> Array a
-- ^ Will contain sorted input
sort a (fromIntegral -> n) (fromIntegral . fromEnum -> b) =
a `op1` (\p x -> af_sort p x n b)
-- | Sort an 'Array' along a specified dimension, specifying ordering of results (ascending / descending), returns indices of sorted results
--
-- >>> A.sortIndex (A.vector @Double 4 [3,2,1,4]) 0 Asc
-- (ArrayFire Array
-- [4 1 1 1]
-- 1.0000
-- 2.0000
-- 3.0000
-- 4.0000
-- ,ArrayFire Array
-- [4 1 1 1]
-- 2
-- 1
-- 0
-- 3
-- )
sortIndex
:: AFType a
=> Array a
-- ^ Input array
-> Int
-- ^ Dimension along `sortIndex` is performed
-> Order
-- ^ Return results in ascending order
-> (Array a, Array Word32)
-- ^ Contains the sorted, contains indices for original input
sortIndex a (fromIntegral -> n) (fromIntegral . fromEnum -> b) =
a `op2p` (\p1 p2 p3 -> af_sort_index p1 p2 p3 n b)
-- | Data type for expressing sort order
data Order = Desc | Asc
deriving (Enum, Show, Eq)
-- | Sort an 'Array' along a specified dimension by keys, specifying ordering of results (ascending / descending)
--
-- >>> A.sortByKey (A.vector @Double 4 [2,1,4,3]) (A.vector @Double 4 [10,9,8,7]) 0 True
-- (ArrayFire Array
-- [4 1 1 1]
-- 1.0000
-- 2.0000
-- 3.0000
-- 4.0000
-- ,ArrayFire Array
-- [4 1 1 1]
-- 9.0000
-- 10.0000
-- 7.0000
-- 8.0000
-- )
sortByKey
:: AFType a
=> Array a
-- ^ Keys input array
-> Array a
-- ^ Values input array
-> Int
-- ^ Dimension along which to perform the operation
-> Order
-- ^ Return results in ascending order
-> (Array a, Array a)
sortByKey a1 a2 (fromIntegral -> n) (fromIntegral . fromEnum -> b) =
op2p2 a1 a2 (\w x y z -> af_sort_by_key w x y z n b)
-- | Finds the unique values in an 'Array', specifying if sorting should occur.
--
-- >>> A.setUnique (A.vector @Double 2 [1.0,1.0]) True
-- ArrayFire Array
-- [1 1 1 1]
-- 1.0000
setUnique
:: AFType a
=> Array a
-- ^ input array
-> Bool
-- ^ if true, skips the sorting steps internally
-> Array a
-- ^ Will contain the unique values from in
setUnique a (fromIntegral . fromEnum -> b) =
op1 a (\x y -> af_set_unique x y b)
-- | Takes the union of two 'Array's, specifying if `setUnique` should be called first.
--
-- >>> A.setUnion (A.vector @Double 3 [3,4,5]) (A.vector @Double 3 [1,2,3]) True
-- ArrayFire Array
-- [5 1 1 1]
-- 1.0000
-- 2.0000
-- 3.0000
-- 4.0000
-- 5.0000
setUnion
:: AFType a
=> Array a
-- ^ First input array
-> Array a
-- ^ Second input array
-> Bool
-- ^ If true, skips calling unique internally
-> Array a
setUnion a1 a2 (fromIntegral . fromEnum -> b) =
op2 a1 a2 (\x y z -> af_set_union x y z b)
-- | Takes the intersection of two 'Array's, specifying if `setUnique` should be called first.
--
-- >>> A.setIntersect (A.vector @Double 3 [3,4,5]) (A.vector @Double 3 [1,2,3]) True
-- ArrayFire Array
-- [1 1 1 1]
-- 3.0000
setIntersect
:: AFType a
=> Array a
-- ^ First input array
-> Array a
-- ^ Second input array
-> Bool
-- ^ If true, skips calling unique internally
-> Array a
-- ^ Intersection of first and second array
setIntersect a1 a2 (fromIntegral . fromEnum -> b) =
op2 a1 a2 (\x y z -> af_set_intersect x y z b)
-- | Sum values in 'Array' grouped by keys along a dimension.
--
-- Each contiguous run of equal keys in @keys@ produces one output element.
-- Returns @(keys_out, vals_out)@.
--
-- >>> sumByKey (vector @Int 5 [1,1,2,2,2]) (vector @Double 5 [10,20,1,2,3]) 0
-- (ArrayFire Array
-- [2 1 1 1]
-- 1 2,
-- ArrayFire Array
-- [2 1 1 1]
-- 30.0000 6.0000)
sumByKey
:: AFType a
=> Array Int
-- ^ Keys array (contiguous equal keys form a group)
-> Array a
-- ^ Values array
-> Int
-- ^ Dimension along which to reduce
-> (Array Int, Array a)
-- ^ (reduced keys, reduced values)
sumByKey keys vals (fromIntegral -> dim) =
op2p2kv keys vals (\ko vo k v -> af_sum_by_key ko vo k v dim)
-- | 'sumByKey' replacing NaN values with a substitute before summing.
sumByKeyNaN
:: AFType a
=> Array Int
-- ^ Keys array
-> Array a
-- ^ Values array
-> Int
-- ^ Dimension
-> Double
-- ^ Substitute for NaN values
-> (Array Int, Array a)
-- ^ (reduced keys, reduced values)
sumByKeyNaN keys vals (fromIntegral -> dim) nanval =
op2p2kv keys vals (\ko vo k v -> af_sum_by_key_nan ko vo k v dim nanval)
-- | Product of values in 'Array' grouped by keys along a dimension.
productByKey
:: AFType a
=> Array Int
-- ^ Keys array
-> Array a
-- ^ Values array
-> Int
-- ^ Dimension
-> (Array Int, Array a)
productByKey keys vals (fromIntegral -> dim) =
op2p2kv keys vals (\ko vo k v -> af_product_by_key ko vo k v dim)
-- | 'productByKey' replacing NaN values with a substitute before multiplying.
productByKeyNaN
:: AFType a
=> Array Int
-- ^ Keys array
-> Array a
-- ^ Values array
-> Int
-- ^ Dimension
-> Double
-- ^ Substitute for NaN values
-> (Array Int, Array a)
productByKeyNaN keys vals (fromIntegral -> dim) nanval =
op2p2kv keys vals (\ko vo k v -> af_product_by_key_nan ko vo k v dim nanval)
-- | Minimum of values in 'Array' grouped by keys along a dimension.
minByKey
:: AFType a
=> Array Int
-- ^ Keys array
-> Array a
-- ^ Values array
-> Int
-- ^ Dimension
-> (Array Int, Array a)
minByKey keys vals (fromIntegral -> dim) =
op2p2kv keys vals (\ko vo k v -> af_min_by_key ko vo k v dim)
-- | Maximum of values in 'Array' grouped by keys along a dimension.
maxByKey
:: AFType a
=> Array Int
-- ^ Keys array
-> Array a
-- ^ Values array
-> Int
-- ^ Dimension
-> (Array Int, Array a)
maxByKey keys vals (fromIntegral -> dim) =
op2p2kv keys vals (\ko vo k v -> af_max_by_key ko vo k v dim)
-- | True if all values are true within each key group.
--
-- The value output is always boolean (@b8@) regardless of the input value type.
allTrueByKey
:: AFType a
=> Array Int
-- ^ Keys array
-> Array a
-- ^ Values array (treated as boolean)
-> Int
-- ^ Dimension
-> (Array Int, Array CBool)
allTrueByKey keys vals (fromIntegral -> dim) =
op2p2kv keys vals (\ko vo k v -> af_all_true_by_key ko vo k v dim)
-- | True if any value is true within each key group.
--
-- The value output is always boolean (@b8@) regardless of the input value type.
anyTrueByKey
:: AFType a
=> Array Int
-- ^ Keys array
-> Array a
-- ^ Values array (treated as boolean)
-> Int
-- ^ Dimension
-> (Array Int, Array CBool)
anyTrueByKey keys vals (fromIntegral -> dim) =
op2p2kv keys vals (\ko vo k v -> af_any_true_by_key ko vo k v dim)
-- | Count non-zero values within each key group.
--
-- The value output is always @u32@ regardless of the input value type.
countByKey
:: AFType a
=> Array Int
-- ^ Keys array
-> Array a
-- ^ Values array
-> Int
-- ^ Dimension
-> (Array Int, Array Word32)
countByKey keys vals (fromIntegral -> dim) =
op2p2kv keys vals (\ko vo k v -> af_count_by_key ko vo k v dim)