forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.py
761 lines (651 loc) · 22.6 KB
/
algorithm.py
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
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
"""
Vector algorithms (sum, min, sort, etc).
"""
from .library import *
from .array import *
def _parallel_dim(a, dim, c_func):
out = Array()
safe_call(c_func(c_pointer(out.arr), a.arr, c_int_t(dim)))
return out
def _reduce_all(a, c_func):
real = c_double_t(0)
imag = c_double_t(0)
safe_call(c_func(c_pointer(real), c_pointer(imag), a.arr))
real = real.value
imag = imag.value
return real if imag == 0 else real + imag * 1j
def _nan_parallel_dim(a, dim, c_func, nan_val):
out = Array()
safe_call(c_func(c_pointer(out.arr), a.arr, c_int_t(dim), c_double_t(nan_val)))
return out
def _nan_reduce_all(a, c_func, nan_val):
real = c_double_t(0)
imag = c_double_t(0)
safe_call(c_func(c_pointer(real), c_pointer(imag), a.arr, c_double_t(nan_val)))
real = real.value
imag = imag.value
return real if imag == 0 else real + imag * 1j
def _FNSD(dim, dims):
if dim >= 0:
return int(dim)
fnsd = 0
for i, d in enumerate(dims):
if d > 1:
fnsd = i
break
return int(fnsd)
def _rbk_dim(keys, vals, dim, c_func):
keys_out = Array()
vals_out = Array()
rdim = _FNSD(dim, vals.dims())
print(rdim)
safe_call(c_func(c_pointer(keys_out.arr), c_pointer(vals_out.arr), keys.arr, vals.arr, c_int_t(rdim)))
return keys_out, vals_out
def _nan_rbk_dim(a, dim, c_func, nan_val):
keys_out = Array()
vals_out = Array()
rdim = _FNSD(dim, vals.dims())
print(rdim)
safe_call(c_func(c_pointer(keys_out.arr), c_pointer(vals_out.arr), keys.arr, vals.arr, c_int_t(rdim), c_double_t(nan_val)))
return keys_out, vals_out
def sum(a, dim=None, nan_val=None):
"""
Calculate the sum of all the elements along a specified dimension.
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: None
Dimension along which the sum is required.
nan_val: optional: scalar. default: None
The value that replaces NaN in the array
Returns
-------
out: af.Array or scalar number
The sum of all elements in `a` along dimension `dim`.
If `dim` is `None`, sum of the entire Array is returned.
"""
if (nan_val is not None):
if dim is not None:
return _nan_parallel_dim(a, dim, backend.get().af_sum_nan, nan_val)
else:
return _nan_reduce_all(a, backend.get().af_sum_nan_all, nan_val)
else:
if dim is not None:
return _parallel_dim(a, dim, backend.get().af_sum)
else:
return _reduce_all(a, backend.get().af_sum_all)
def sumByKey(keys, vals, dim=-1, nan_val=None):
"""
Calculate the sum of elements along a specified dimension according to a key.
Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the sum will occur.
nan_val: optional: scalar. default: None
The value that replaces NaN in the array
Returns
-------
keys: af.Array or scalar number
The reduced keys of all elements in `vals` along dimension `dim`.
values: af.Array or scalar number
The sum of all elements in `vals` along dimension `dim` according to keys
"""
if (nan_val is not None):
return _nan_rbk_dim(keys, vals, dim, backend.get().af_sum_by_key_nan, nan_val)
else:
return _rbk_dim(keys, vals, dim, backend.get().af_sum_by_key)
def product(a, dim=None, nan_val=None):
"""
Calculate the product of all the elements along a specified dimension.
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: None
Dimension along which the product is required.
nan_val: optional: scalar. default: None
The value that replaces NaN in the array
Returns
-------
out: af.Array or scalar number
The product of all elements in `a` along dimension `dim`.
If `dim` is `None`, product of the entire Array is returned.
"""
if (nan_val is not None):
if dim is not None:
return _nan_parallel_dim(a, dim, backend.get().af_product_nan, nan_val)
else:
return _nan_reduce_all(a, backend.get().af_product_nan_all, nan_val)
else:
if dim is not None:
return _parallel_dim(a, dim, backend.get().af_product)
else:
return _reduce_all(a, backend.get().af_product_all)
def productByKey(keys, vals, dim=-1, nan_val=None):
"""
Calculate the product of elements along a specified dimension according to a key.
Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the product will occur.
nan_val: optional: scalar. default: None
The value that replaces NaN in the array
Returns
-------
keys: af.Array or scalar number
The reduced keys of all elements in `vals` along dimension `dim`.
values: af.Array or scalar number
The product of all elements in `vals` along dimension `dim` according to keys
"""
if (nan_val is not None):
return _nan_rbk_dim(keys, vals, dim, backend.get().af_product_by_key_nan, nan_val)
else:
return _rbk_dim(keys, vals, dim, backend.get().af_product_by_key)
def min(a, dim=None):
"""
Find the minimum value of all the elements along a specified dimension.
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: None
Dimension along which the minimum value is required.
Returns
-------
out: af.Array or scalar number
The minimum value of all elements in `a` along dimension `dim`.
If `dim` is `None`, minimum value of the entire Array is returned.
"""
if dim is not None:
return _parallel_dim(a, dim, backend.get().af_min)
else:
return _reduce_all(a, backend.get().af_min_all)
def minByKey(keys, vals, dim=-1):
"""
Calculate the min of elements along a specified dimension according to a key.
Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the min will occur.
Returns
-------
keys: af.Array or scalar number
The reduced keys of all elements in `vals` along dimension `dim`.
values: af.Array or scalar number
The min of all elements in `vals` along dimension `dim` according to keys
"""
return _rbk_dim(keys, vals, dim, backend.get().af_min_by_key)
def max(a, dim=None):
"""
Find the maximum value of all the elements along a specified dimension.
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: None
Dimension along which the maximum value is required.
Returns
-------
out: af.Array or scalar number
The maximum value of all elements in `a` along dimension `dim`.
If `dim` is `None`, maximum value of the entire Array is returned.
"""
if dim is not None:
return _parallel_dim(a, dim, backend.get().af_max)
else:
return _reduce_all(a, backend.get().af_max_all)
def maxByKey(keys, vals, dim=-1):
"""
Calculate the max of elements along a specified dimension according to a key.
Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the max will occur.
Returns
-------
keys: af.Array or scalar number
The reduced keys of all elements in `vals` along dimension `dim`.
values: af.Array or scalar number
The max of all elements in `vals` along dimension `dim` according to keys.
"""
return _rbk_dim(keys, vals, dim, backend.get().af_max_by_key)
def all_true(a, dim=None):
"""
Check if all the elements along a specified dimension are true.
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: None
Dimension along which the product is required.
Returns
-------
out: af.Array or scalar number
Af.array containing True if all elements in `a` along the dimension are True.
If `dim` is `None`, output is True if `a` does not have any zeros, else False.
"""
if dim is not None:
return _parallel_dim(a, dim, backend.get().af_all_true)
else:
return _reduce_all(a, backend.get().af_all_true_all)
def allTrueByKey(keys, vals, dim=-1):
"""
Calculate if all elements are true along a specified dimension according to a key.
Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the all true check will occur.
Returns
-------
keys: af.Array or scalar number
The reduced keys of all true check in `vals` along dimension `dim`.
values: af.Array or scalar number
Booleans denoting if all elements are true in `vals` along dimension `dim` according to keys
"""
return _rbk_dim(keys, vals, dim, backend.get().af_all_true_by_key)
def any_true(a, dim=None):
"""
Check if any the elements along a specified dimension are true.
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: None
Dimension along which the product is required.
Returns
-------
out: af.Array or scalar number
Af.array containing True if any elements in `a` along the dimension are True.
If `dim` is `None`, output is True if `a` does not have any zeros, else False.
"""
if dim is not None:
return _parallel_dim(a, dim, backend.get().af_any_true)
else:
return _reduce_all(a, backend.get().af_any_true_all)
def anyTrueByKey(keys, vals, dim=-1):
"""
Calculate if any elements are true along a specified dimension according to a key.
Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which the any true check will occur.
Returns
-------
keys: af.Array or scalar number
The reduced keys of any true check in `vals` along dimension `dim`.
values: af.Array or scalar number
Booleans denoting if any elements are true in `vals` along dimension `dim` according to keys.
"""
return _rbk_dim(keys, vals, dim, backend.get().af_any_true_by_key)
def count(a, dim=None):
"""
Count the number of non zero elements in an array along a specified dimension.
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: None
Dimension along which the the non zero elements are to be counted.
Returns
-------
out: af.Array or scalar number
The count of non zero elements in `a` along `dim`.
If `dim` is `None`, the total number of non zero elements in `a`.
"""
if dim is not None:
return _parallel_dim(a, dim, backend.get().af_count)
else:
return _reduce_all(a, backend.get().af_count_all)
def countByKey(keys, vals, dim=-1):
"""
Counts non-zero elements along a specified dimension according to a key.
Parameters
----------
keys : af.Array
One dimensional arrayfire array with reduction keys.
vals : af.Array
Multi dimensional arrayfire array that will be reduced.
dim: optional: int. default: -1
Dimension along which to count elements.
Returns
-------
keys: af.Array or scalar number
The reduced keys of count in `vals` along dimension `dim`.
values: af.Array or scalar number
Count of non-zero elements in `vals` along dimension `dim` according to keys.
"""
return _rbk_dim(keys, vals, dim, backend.get().af_count_by_key)
def imin(a, dim=None):
"""
Find the value and location of the minimum value along a specified dimension
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: None
Dimension along which the minimum value is required.
Returns
-------
(val, idx): tuple of af.Array or scalars
`val` contains the minimum value of `a` along `dim`.
`idx` contains the location of where `val` occurs in `a` along `dim`.
If `dim` is `None`, `val` and `idx` value and location of global minimum.
"""
if dim is not None:
out = Array()
idx = Array()
safe_call(backend.get().af_imin(c_pointer(out.arr), c_pointer(idx.arr), a.arr, c_int_t(dim)))
return out,idx
else:
real = c_double_t(0)
imag = c_double_t(0)
idx = c_uint_t(0)
safe_call(backend.get().af_imin_all(c_pointer(real), c_pointer(imag), c_pointer(idx), a.arr))
real = real.value
imag = imag.value
val = real if imag == 0 else real + imag * 1j
return val,idx.value
def imax(a, dim=None):
"""
Find the value and location of the maximum value along a specified dimension
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: None
Dimension along which the maximum value is required.
Returns
-------
(val, idx): tuple of af.Array or scalars
`val` contains the maximum value of `a` along `dim`.
`idx` contains the location of where `val` occurs in `a` along `dim`.
If `dim` is `None`, `val` and `idx` value and location of global maximum.
"""
if dim is not None:
out = Array()
idx = Array()
safe_call(backend.get().af_imax(c_pointer(out.arr), c_pointer(idx.arr), a.arr, c_int_t(dim)))
return out,idx
else:
real = c_double_t(0)
imag = c_double_t(0)
idx = c_uint_t(0)
safe_call(backend.get().af_imax_all(c_pointer(real), c_pointer(imag), c_pointer(idx), a.arr))
real = real.value
imag = imag.value
val = real if imag == 0 else real + imag * 1j
return val,idx.value
def accum(a, dim=0):
"""
Cumulative sum of an array along a specified dimension
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: 0
Dimension along which the cumulative sum is required.
Returns
-------
out: af.Array
array of same size as `a` containing the cumulative sum along `dim`.
"""
return _parallel_dim(a, dim, backend.get().af_accum)
def scan(a, dim=0, op=BINARYOP.ADD, inclusive_scan=True):
"""
Generalized scan of an array.
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim : optional: int. default: 0
Dimension along which the scan is performed.
op : optional: af.BINARYOP. default: af.BINARYOP.ADD.
Binary option the scan algorithm uses. Can be one of:
- af.BINARYOP.ADD
- af.BINARYOP.MUL
- af.BINARYOP.MIN
- af.BINARYOP.MAX
inclusive_scan: optional: bool. default: True
Specifies if the scan is inclusive
Returns
---------
out : af.Array
- will contain scan of input.
"""
out = Array()
safe_call(backend.get().af_scan(c_pointer(out.arr), a.arr, dim, op.value, inclusive_scan))
return out
def scan_by_key(key, a, dim=0, op=BINARYOP.ADD, inclusive_scan=True):
"""
Generalized scan by key of an array.
Parameters
----------
key : af.Array
key array.
a : af.Array
Multi dimensional arrayfire array.
dim : optional: int. default: 0
Dimension along which the scan is performed.
op : optional: af.BINARYOP. default: af.BINARYOP.ADD.
Binary option the scan algorithm uses. Can be one of:
- af.BINARYOP.ADD
- af.BINARYOP.MUL
- af.BINARYOP.MIN
- af.BINARYOP.MAX
inclusive_scan: optional: bool. default: True
Specifies if the scan is inclusive
Returns
---------
out : af.Array
- will contain scan of input.
"""
out = Array()
safe_call(backend.get().af_scan_by_key(c_pointer(out.arr), key.arr, a.arr, dim, op.value, inclusive_scan))
return out
def where(a):
"""
Find the indices of non zero elements
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
Returns
-------
idx: af.Array
Linear indices for non zero elements.
"""
out = Array()
safe_call(backend.get().af_where(c_pointer(out.arr), a.arr))
return out
def diff1(a, dim=0):
"""
Find the first order differences along specified dimensions
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: 0
Dimension along which the differences are required.
Returns
-------
out: af.Array
Array whose length along `dim` is 1 less than that of `a`.
"""
return _parallel_dim(a, dim, backend.get().af_diff1)
def diff2(a, dim=0):
"""
Find the second order differences along specified dimensions
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: 0
Dimension along which the differences are required.
Returns
-------
out: af.Array
Array whose length along `dim` is 2 less than that of `a`.
"""
return _parallel_dim(a, dim, backend.get().af_diff2)
def sort(a, dim=0, is_ascending=True):
"""
Sort the array along a specified dimension
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: 0
Dimension along which sort is to be performed.
is_ascending: optional: bool. default: True
Specifies the direction of the sort
Returns
-------
out: af.Array
array containing the sorted values
Note
-------
Currently `dim` is only supported for 0.
"""
out = Array()
safe_call(backend.get().af_sort(c_pointer(out.arr), a.arr, c_uint_t(dim), c_bool_t(is_ascending)))
return out
def sort_index(a, dim=0, is_ascending=True):
"""
Sort the array along a specified dimension and get the indices.
Parameters
----------
a : af.Array
Multi dimensional arrayfire array.
dim: optional: int. default: 0
Dimension along which sort is to be performed.
is_ascending: optional: bool. default: True
Specifies the direction of the sort
Returns
-------
(val, idx): tuple of af.Array
`val` is an af.Array containing the sorted values.
`idx` is an af.Array containing the original indices of `val` in `a`.
Note
-------
Currently `dim` is only supported for 0.
"""
out = Array()
idx = Array()
safe_call(backend.get().af_sort_index(c_pointer(out.arr), c_pointer(idx.arr), a.arr,
c_uint_t(dim), c_bool_t(is_ascending)))
return out,idx
def sort_by_key(ik, iv, dim=0, is_ascending=True):
"""
Sort an array based on specified keys
Parameters
----------
ik : af.Array
An Array containing the keys
iv : af.Array
An Array containing the values
dim: optional: int. default: 0
Dimension along which sort is to be performed.
is_ascending: optional: bool. default: True
Specifies the direction of the sort
Returns
-------
(ok, ov): tuple of af.Array
`ok` contains the values from `ik` in sorted order
`ov` contains the values from `iv` after sorting them based on `ik`
Note
-------
Currently `dim` is only supported for 0.
"""
ov = Array()
ok = Array()
safe_call(backend.get().af_sort_by_key(c_pointer(ok.arr), c_pointer(ov.arr),
ik.arr, iv.arr, c_uint_t(dim), c_bool_t(is_ascending)))
return ov,ok
def set_unique(a, is_sorted=False):
"""
Find the unique elements of an array.
Parameters
----------
a : af.Array
A 1D arrayfire array.
is_sorted: optional: bool. default: False
Specifies if the input is pre-sorted.
Returns
-------
out: af.Array
an array containing the unique values from `a`
"""
out = Array()
safe_call(backend.get().af_set_unique(c_pointer(out.arr), a.arr, c_bool_t(is_sorted)))
return out
def set_union(a, b, is_unique=False):
"""
Find the union of two arrays.
Parameters
----------
a : af.Array
A 1D arrayfire array.
b : af.Array
A 1D arrayfire array.
is_unique: optional: bool. default: False
Specifies if the both inputs contain unique elements.
Returns
-------
out: af.Array
an array values after performing the union of `a` and `b`.
"""
out = Array()
safe_call(backend.get().af_set_union(c_pointer(out.arr), a.arr, b.arr, c_bool_t(is_unique)))
return out
def set_intersect(a, b, is_unique=False):
"""
Find the intersect of two arrays.
Parameters
----------
a : af.Array
A 1D arrayfire array.
b : af.Array
A 1D arrayfire array.
is_unique: optional: bool. default: False
Specifies if the both inputs contain unique elements.
Returns
-------
out: af.Array
an array values after performing the intersect of `a` and `b`.
"""
out = Array()
safe_call(backend.get().af_set_intersect(c_pointer(out.arr), a.arr, b.arr, c_bool_t(is_unique)))
return out