$darkmode
A high-performance general-purpose compute library
algorithm.h
Go to the documentation of this file.
1 /*******************************************************
2  * Copyright (c) 2014, ArrayFire
3  * All rights reserved.
4  *
5  * This file is distributed under 3-clause BSD license.
6  * The complete license agreement can be obtained at:
7  * http://arrayfire.com/licenses/BSD-3-Clause
8  ********************************************************/
9 
10 #pragma once
11 #include <af/defines.h>
12 
13 #ifdef __cplusplus
14 namespace af
15 {
16  class array;
17 
28  AFAPI array sum(const array &in, const int dim = -1);
29 
30 #if AF_API_VERSION >= 31
31 
42  AFAPI array sum(const array &in, const int dim, const double nanval);
43 #endif
44 
45 #if AF_API_VERSION >= 37
46 
59  AFAPI void sumByKey(array &keys_out, array &vals_out,
60  const array &keys, const array &vals,
61  const int dim = -1);
62 
76  AFAPI void sumByKey(array &keys_out, array &vals_out,
77  const array &keys, const array &vals,
78  const int dim, const double nanval);
79 #endif
80 
91  AFAPI array product(const array &in, const int dim = -1);
92 
93 #if AF_API_VERSION >= 31
94 
105  AFAPI array product(const array &in, const int dim, const double nanval);
106 #endif
107 
108 #if AF_API_VERSION >= 37
109 
122  AFAPI void productByKey(array &keys_out, array &vals_out,
123  const array &keys, const array &vals,
124  const int dim = -1);
125 
141  AFAPI void productByKey(array &keys_out, array &vals_out,
142  const array &keys, const array &vals,
143  const int dim, const double nanval);
144 #endif
145 
158  AFAPI array min(const array &in, const int dim = -1);
159 
160 #if AF_API_VERSION >= 37
161 
176  AFAPI void minByKey(array &keys_out, array &vals_out,
177  const array &keys, const array &vals,
178  const int dim = -1);
179 #endif
180 
193  AFAPI array max(const array &in, const int dim = -1);
194 
195 #if AF_API_VERSION >= 37
196 
211  AFAPI void maxByKey(array &keys_out, array &vals_out,
212  const array &keys, const array &vals,
213  const int dim = -1);
214 #endif
215 
216 #if AF_API_VERSION >= 38
217 
232  AFAPI void max(array &val, array &idx, const array &in, const array &ragged_len, const int dim);
233 #endif
234 
247  AFAPI array allTrue(const array &in, const int dim = -1);
248 
249 #if AF_API_VERSION >= 37
250 
264  AFAPI void allTrueByKey(array &keys_out, array &vals_out,
265  const array &keys, const array &vals,
266  const int dim = -1);
267 #endif
268 
281  AFAPI array anyTrue(const array &in, const int dim = -1);
282 
283 #if AF_API_VERSION >= 37
284 
298  AFAPI void anyTrueByKey(array &keys_out, array &vals_out,
299  const array &keys, const array &vals,
300  const int dim = -1);
301 #endif
302 
316  AFAPI array count(const array &in, const int dim = -1);
317 
318 #if AF_API_VERSION >= 37
319 
334  AFAPI void countByKey(array &keys_out, array &vals_out,
335  const array &keys, const array &vals,
336  const int dim = -1);
337 #endif
338 
350  template<typename T> T sum(const array &in);
351 
352 #if AF_API_VERSION >= 31
353 
366  template<typename T> T sum(const array &in, double nanval);
367 #endif
368 
378  template<typename T> T product(const array &in);
379 
380 #if AF_API_VERSION >= 31
381 
391  template<typename T> T product(const array &in, double nanval);
392 #endif
393 
405  template<typename T> T min(const array &in);
406 
418  template<typename T> T max(const array &in);
419 
431  template<typename T> T allTrue(const array &in);
432 
444  template<typename T> T anyTrue(const array &in);
445 
457  template<typename T> T count(const array &in);
458 
473  AFAPI void min(array &val, array &idx, const array &in, const int dim = -1);
474 
489  AFAPI void max(array &val, array &idx, const array &in, const int dim = -1);
490 
505  template<typename T> void min(T *val, unsigned *idx, const array &in);
506 
521  template<typename T> void max(T *val, unsigned *idx, const array &in);
522 
534  AFAPI array accum(const array &in, const int dim = 0);
535 
536 #if AF_API_VERSION >=34
537 
549  AFAPI array scan(const array &in, const int dim = 0,
550  binaryOp op = AF_BINARY_ADD, bool inclusive_scan = true);
551 
566  AFAPI array scanByKey(const array &key, const array& in, const int dim = 0,
567  binaryOp op = AF_BINARY_ADD, bool inclusive_scan = true);
568 #endif
569 
578  AFAPI array where(const array &in);
579 
591  AFAPI array diff1(const array &in, const int dim = 0);
592 
604  AFAPI array diff2(const array &in, const int dim = 0);
605 
617  AFAPI array sort(const array &in, const unsigned dim = 0,
618  const bool isAscending = true);
619 
633  AFAPI void sort(array &out, array &indices, const array &in, const unsigned dim = 0,
634  const bool isAscending = true);
635 
650  AFAPI void sort(array &out_keys, array &out_values, const array &keys,
651  const array &values, const unsigned dim = 0,
652  const bool isAscending = true);
653 
663  AFAPI array setUnique(const array &in, const bool is_sorted=false);
664 
675  AFAPI array setUnion(const array &first, const array &second,
676  const bool is_unique=false);
677 
688  AFAPI array setIntersect(const array &first, const array &second,
689  const bool is_unique=false);
690 }
691 #endif
692 
693 #ifdef __cplusplus
694 extern "C" {
695 #endif
696 
708  AFAPI af_err af_sum(af_array *out, const af_array in, const int dim);
709 
710 #if AF_API_VERSION >= 39
711 
724 #endif
725 
726 #if AF_API_VERSION >= 31
727 
740  AFAPI af_err af_sum_nan(af_array *out, const af_array in,
741  const int dim, const double nanval);
742 #endif
743 
744 #if AF_API_VERSION >= 39
745 
759  AFAPI af_err af_sum_nan_all_array(af_array *out, const af_array in, const double nanval);
760 #endif
761 
762 #if AF_API_VERSION >= 37
763 
777  AFAPI af_err af_sum_by_key(af_array *keys_out, af_array *vals_out,
778  const af_array keys, const af_array vals, const int dim);
779 
795  AFAPI af_err af_sum_by_key_nan(af_array *keys_out, af_array *vals_out,
796  const af_array keys, const af_array vals,
797  const int dim, const double nanval);
798 #endif
799 
811  AFAPI af_err af_product(af_array *out, const af_array in, const int dim);
812 
813 #if AF_API_VERSION >= 39
814 
827 #endif
828 
829 #if AF_API_VERSION >= 31
830 
843  AFAPI af_err af_product_nan(af_array *out, const af_array in, const int dim, const double nanval);
844 #endif
845 
846 #if AF_API_VERSION >= 39
847 
859  AFAPI af_err af_product_nan_all_array(af_array *out, const af_array in, const double nanval);
860 #endif
861 
862 #if AF_API_VERSION >= 37
863 
877  AFAPI af_err af_product_by_key(af_array *keys_out, af_array *vals_out,
878  const af_array keys, const af_array vals, const int dim);
879 
895  AFAPI af_err af_product_by_key_nan(af_array *keys_out, af_array *vals_out,
896  const af_array keys, const af_array vals,
897  const int dim, const double nanval);
898 #endif
899 
911  AFAPI af_err af_min(af_array *out, const af_array in, const int dim);
912 
913 #if AF_API_VERSION >= 37
914 
928  AFAPI af_err af_min_by_key(af_array *keys_out, af_array *vals_out,
929  const af_array keys, const af_array vals,
930  const int dim);
931 #endif
932 
944  AFAPI af_err af_max(af_array *out, const af_array in, const int dim);
945 
946 #if AF_API_VERSION >= 37
947 
961  AFAPI af_err af_max_by_key(af_array *keys_out, af_array *vals_out,
962  const af_array keys, const af_array vals,
963  const int dim);
964 #endif
965 
966 #if AF_API_VERSION >= 38
967 
984  AFAPI af_err af_max_ragged(af_array *val, af_array *idx, const af_array in, const af_array ragged_len, const int dim);
985 #endif
986 
1000  AFAPI af_err af_all_true(af_array *out, const af_array in, const int dim);
1001 
1002 #if AF_API_VERSION >= 37
1003 
1019  AFAPI af_err af_all_true_by_key(af_array *keys_out, af_array *vals_out,
1020  const af_array keys, const af_array vals,
1021  const int dim);
1022 #endif
1023 
1037  AFAPI af_err af_any_true(af_array *out, const af_array in, const int dim);
1038 
1039 #if AF_API_VERSION >= 37
1040 
1055  AFAPI af_err af_any_true_by_key(af_array *keys_out, af_array *vals_out,
1056  const af_array keys, const af_array vals,
1057  const int dim);
1058 #endif
1059 
1074  AFAPI af_err af_count(af_array *out, const af_array in, const int dim);
1075 
1076 #if AF_API_VERSION >= 37
1077 
1093  AFAPI af_err af_count_by_key(af_array *keys_out, af_array *vals_out,
1094  const af_array keys, const af_array vals,
1095  const int dim);
1096 #endif
1097 
1111  AFAPI af_err af_sum_all(double *real, double *imag, const af_array in);
1112 
1113 #if AF_API_VERSION >= 31
1114 
1129  AFAPI af_err af_sum_nan_all(double *real, double *imag,
1130  const af_array in, const double nanval);
1131 #endif
1132 
1146  AFAPI af_err af_product_all(double *real, double *imag, const af_array in);
1147 
1148 #if AF_API_VERSION >= 31
1149 
1164  AFAPI af_err af_product_nan_all(double *real, double *imag,
1165  const af_array in, const double nanval);
1166 #endif
1167 
1181  AFAPI af_err af_min_all(double *real, double *imag, const af_array in);
1182 
1183 #if AF_API_VERSION >= 39
1184 
1194  AFAPI af_err af_min_all_array(af_array *out, const af_array in);
1195 #endif
1196 
1210  AFAPI af_err af_max_all(double *real, double *imag, const af_array in);
1211 
1212 #if AF_API_VERSION >= 39
1213 
1223  AFAPI af_err af_max_all_array(af_array *out, const af_array in);
1224 #endif
1225 
1237  AFAPI af_err af_all_true_all(double *real, double *imag, const af_array in);
1238 
1239 #if AF_API_VERSION >= 39
1240 
1251 #endif
1252 
1264  AFAPI af_err af_any_true_all(double *real, double *imag, const af_array in);
1265 
1266 #if AF_API_VERSION >= 39
1267 
1278 #endif
1279 
1291  AFAPI af_err af_count_all(double *real, double *imag, const af_array in);
1292 
1293 #if AF_API_VERSION >= 39
1294 
1305 #endif
1306 
1320  AFAPI af_err af_imin(af_array *out, af_array *idx, const af_array in,
1321  const int dim);
1322 
1336  AFAPI af_err af_imax(af_array *out, af_array *idx, const af_array in,
1337  const int dim);
1338 
1353  AFAPI af_err af_imin_all(double *real, double *imag, unsigned *idx,
1354  const af_array in);
1355 
1370  AFAPI af_err af_imax_all(double *real, double *imag, unsigned *idx, const af_array in);
1371 
1384  AFAPI af_err af_accum(af_array *out, const af_array in, const int dim);
1385 
1386 #if AF_API_VERSION >=34
1387 
1400  AFAPI af_err af_scan(af_array *out, const af_array in, const int dim,
1401  af_binary_op op, bool inclusive_scan);
1402 
1418  AFAPI af_err af_scan_by_key(af_array *out, const af_array key,
1419  const af_array in, const int dim,
1420  af_binary_op op, bool inclusive_scan);
1421 
1422 #endif
1423 
1434  AFAPI af_err af_where(af_array *idx, const af_array in);
1435 
1448  AFAPI af_err af_diff1(af_array *out, const af_array in, const int dim);
1449 
1462  AFAPI af_err af_diff2(af_array *out, const af_array in, const int dim);
1463 
1476  AFAPI af_err af_sort(af_array *out, const af_array in, const unsigned dim,
1477  const bool isAscending);
1478 
1493  AFAPI af_err af_sort_index(af_array *out, af_array *indices, const af_array in,
1494  const unsigned dim, const bool isAscending);
1510  AFAPI af_err af_sort_by_key(af_array *out_keys, af_array *out_values,
1511  const af_array keys, const af_array values,
1512  const unsigned dim, const bool isAscending);
1513 
1525  AFAPI af_err af_set_unique(af_array *out, const af_array in, const bool is_sorted);
1526 
1539  AFAPI af_err af_set_union(af_array *out, const af_array first,
1540  const af_array second, const bool is_unique);
1541 
1554  AFAPI af_err af_set_intersect(af_array *out, const af_array first,
1555  const af_array second, const bool is_unique);
1556 
1557 #ifdef __cplusplus
1558 }
1559 #endif
AFAPI void anyTrueByKey(array &keys_out, array &vals_out, const array &keys, const array &vals, const int dim=-1)
C++ Interface to check if any values along a given dimension are true, according to an array of keys...
AFAPI af_err af_sum_all_array(af_array *out, const af_array in)
C Interface to sum array elements over all dimensions.
Definition: algorithm.h:14
AFAPI array where(const array &in)
C++ Interface to locate the indices of the non-zero values in an array.
AFAPI af_err af_max_all_array(af_array *out, const af_array in)
C Interface to return the maximum over all dimensions.
AFAPI af_err af_imin_all(double *real, double *imag, unsigned *idx, const af_array in)
C Interface to return the minimum and its location over all dimensions.
AFAPI af_err af_set_union(af_array *out, const af_array first, const af_array second, const bool is_unique)
C Interface to evaluate the union of two arrays.
AFAPI af_err af_count_all(double *real, double *imag, const af_array in)
C Interface to count non-zero values over all dimensions.
AFAPI af_err af_sum_nan(af_array *out, const af_array in, const int dim, const double nanval)
C Interface to sum array elements over a given dimension, replacing any NaNs with a specified value...
AFAPI af_err af_any_true_all(double *real, double *imag, const af_array in)
C Interface to check if any values over all dimensions are true.
AFAPI af_err af_product_nan_all_array(af_array *out, const af_array in, const double nanval)
C Interface to multiply array elements over all dimensions, replacing any NaNs with a specified value...
AFAPI af_err af_product_all(double *real, double *imag, const af_array in)
C Interface to multiply array elements over all dimensions.
AFAPI af_err af_where(af_array *idx, const af_array in)
C Interface to locate the indices of the non-zero values in an array.
AFAPI af_err af_set_unique(af_array *out, const af_array in, const bool is_sorted)
C Interface to return the unique values in an array.
AFAPI array sum(const array &in, const int dim=-1)
C++ Interface to sum array elements over a given dimension.
AFAPI af_err af_accum(af_array *out, const af_array in, const int dim)
C Interface to evaluate the cumulative sum (inclusive) along a given dimension.
AFAPI af_err af_imax_all(double *real, double *imag, unsigned *idx, const af_array in)
C Interface to return the maximum and its location over all dimensions.
AFAPI af_err af_sort_index(af_array *out, af_array *indices, const af_array in, const unsigned dim, const bool isAscending)
C Interface to sort an array over a given dimension and to return the original indices.
AFAPI array allTrue(const array &in, const int dim=-1)
C++ Interface to check if all values along a given dimension are true.
AFAPI void countByKey(array &keys_out, array &vals_out, const array &keys, const array &vals, const int dim=-1)
C++ Interface to count non-zero values in an array, according to an array of keys.
AFAPI af_err af_product_by_key(af_array *keys_out, af_array *vals_out, const af_array keys, const af_array vals, const int dim)
C Interface to multiply array elements over a given dimension, according to an array of keys...
A multi dimensional data container.
Definition: array.h:37
AFAPI af_err af_count(af_array *out, const af_array in, const int dim)
C Interface to count non-zero values in an array along a given dimension.
AFAPI void minByKey(array &keys_out, array &vals_out, const array &keys, const array &vals, const int dim=-1)
C++ Interface to return the minimum along a given dimension, according to an array of keys...
AFAPI af_err af_max(af_array *out, const af_array in, const int dim)
C Interface to return the maximum along a given dimension.
AFAPI af_err af_count_by_key(af_array *keys_out, af_array *vals_out, const af_array keys, const af_array vals, const int dim)
C Interface to count non-zero values in an array, according to an array of keys.
AFAPI af_err af_min_all(double *real, double *imag, const af_array in)
C Interface to return the minimum over all dimensions.
AFAPI af_err af_max_by_key(af_array *keys_out, af_array *vals_out, const af_array keys, const af_array vals, const int dim)
C Interface to return the maximum along a given dimension, according to an array of keys...
AFAPI af_err af_product_all_array(af_array *out, const af_array in)
C Interface to multiply array elements over all dimensions.
AFAPI af_err af_sum_nan_all(double *real, double *imag, const af_array in, const double nanval)
C Interface to sum array elements over all dimensions, replacing any NaNs with a specified value...
af_err
Definition: defines.h:71
AFAPI array scan(const array &in, const int dim=0, binaryOp op=AF_BINARY_ADD, bool inclusive_scan=true)
C++ Interface to scan an array (generalized) over a given dimension.
AFAPI af_err af_min_by_key(af_array *keys_out, af_array *vals_out, const af_array keys, const af_array vals, const int dim)
C Interface to return the minimum along a given dimension, according to an array of keys...
AFAPI af_err af_product(af_array *out, const af_array in, const int dim)
C Interface to multiply array elements over a given dimension.
AFAPI af_err af_imin(af_array *out, af_array *idx, const af_array in, const int dim)
C Interface to return the minimum and its location along a given dimension.
AFAPI af_err af_any_true(af_array *out, const af_array in, const int dim)
C Interface to check if any values along a given dimension are true.
AFAPI array max(const array &in, const int dim=-1)
C++ Interface to return the maximum along a given dimension.
AFAPI af_err af_diff2(af_array *out, const af_array in, const int dim)
C Interface to calculate the second order difference in an array over a given dimension.
AFAPI af_err af_scan_by_key(af_array *out, const af_array key, const af_array in, const int dim, af_binary_op op, bool inclusive_scan)
C Interface to scan an array (generalized) over a given dimension, according to an array of keys...
AFAPI void allTrueByKey(array &keys_out, array &vals_out, const array &keys, const array &vals, const int dim=-1)
C++ Interface to check if all values along a given dimension are true, according to an array of keys...
AFAPI array imag(const array &in)
C++ Interface to return the imaginary part of a complex array.
AFAPI af_err af_max_ragged(af_array *val, af_array *idx, const af_array in, const af_array ragged_len, const int dim)
C Interface to return the ragged maximum over a given dimension.
AFAPI array product(const array &in, const int dim=-1)
C++ Interface to multiply array elements over a given dimension.
AFAPI af_err af_set_intersect(af_array *out, const af_array first, const af_array second, const bool is_unique)
C Interface to evaluate the intersection of two arrays.
AFAPI af_err af_diff1(af_array *out, const af_array in, const int dim)
C Interface to calculate the first order difference in an array over a given dimension.
AFAPI af_err af_all_true_all_array(af_array *out, const af_array in)
C Interface to check if all values over all dimensions are true.
AFAPI af_err af_min(af_array *out, const af_array in, const int dim)
C Interface to return the minimum along a given dimension.
AFAPI af_err af_sum_by_key_nan(af_array *keys_out, af_array *vals_out, const af_array keys, const af_array vals, const int dim, const double nanval)
C Interface to sum array elements over a given dimension, replacing any NaNs with a specified value...
AFAPI array real(const array &in)
C++ Interface to return the real part of a complex array.
AFAPI af_err af_imax(af_array *out, af_array *idx, const af_array in, const int dim)
C Interface to return the maximum and its location along a given dimension.
#define AFAPI
Definition: defines.h:38
AFAPI af_err af_any_true_by_key(af_array *keys_out, af_array *vals_out, const af_array keys, const af_array vals, const int dim)
C Interface to check if any values along a given dimension are true.
AFAPI array setIntersect(const array &first, const array &second, const bool is_unique=false)
C++ Interface to evaluate the intersection of two arrays.
AFAPI array anyTrue(const array &in, const int dim=-1)
C++ Interface to check if any values along a given dimension are true.
AFAPI af_err af_sum_nan_all_array(af_array *out, const af_array in, const double nanval)
C Interface to sum array elements over all dimensions, replacing any NaNs with a specified value...
AFAPI array setUnique(const array &in, const bool is_sorted=false)
C++ Interface to return the unique values in an array.
AFAPI array accum(const array &in, const int dim=0)
C++ Interface to evaluate the cumulative sum (inclusive) along a given dimension. ...
af_binary_op
Definition: defines.h:430
AFAPI af_err af_product_nan(af_array *out, const af_array in, const int dim, const double nanval)
C Interface to multiply array elements over a given dimension, replacing any NaNs with a specified va...
AFAPI array scanByKey(const array &key, const array &in, const int dim=0, binaryOp op=AF_BINARY_ADD, bool inclusive_scan=true)
C++ Interface to scan an array (generalized) over a given dimension, according to an array of keys...
AFAPI af_err af_sort(af_array *out, const af_array in, const unsigned dim, const bool isAscending)
C Interface to sort an array over a given dimension.
AFAPI void productByKey(array &keys_out, array &vals_out, const array &keys, const array &vals, const int dim=-1)
C++ Interface to multiply array elements over a given dimension, according to an array of keys...
AFAPI af_err af_product_by_key_nan(af_array *keys_out, af_array *vals_out, const af_array keys, const af_array vals, const int dim, const double nanval)
C Interface to multiply array elements over a given dimension, replacing any NaNs with a specified va...
AFAPI void sumByKey(array &keys_out, array &vals_out, const array &keys, const array &vals, const int dim=-1)
C++ Interface to sum array elements over a given dimension, according to an array of keys...
AFAPI af_err af_sum_by_key(af_array *keys_out, af_array *vals_out, const af_array keys, const af_array vals, const int dim)
C Interface to sum array elements over a given dimension, according to an array of keys...
AFAPI af_err af_sort_by_key(af_array *out_keys, af_array *out_values, const af_array keys, const af_array values, const unsigned dim, const bool isAscending)
C Interface to sort an array over a given dimension, according to an array of keys.
void * af_array
Definition: defines.h:240
AFAPI af_err af_min_all_array(af_array *out, const af_array in)
C Interface to return the minimum over all dimensions.
AFAPI array min(const array &in, const int dim=-1)
C++ Interface to return the minimum along a given dimension.
AFAPI af_err af_sum(af_array *out, const af_array in, const int dim)
C Interface to sum array elements over a given dimension.
AFAPI af_err af_sum_all(double *real, double *imag, const af_array in)
C Interface to sum array elements over all dimensions.
AFAPI array sort(const array &in, const unsigned dim=0, const bool isAscending=true)
C++ Interface to sort an array over a given dimension.
AFAPI af_err af_all_true_by_key(af_array *keys_out, af_array *vals_out, const af_array keys, const af_array vals, const int dim)
C Interface to check if all values along a given dimension are true, according to an array of keys...
AFAPI af_err af_all_true_all(double *real, double *imag, const af_array in)
C Interface to check if all values over all dimensions are true.
AFAPI af_err af_scan(af_array *out, const af_array in, const int dim, af_binary_op op, bool inclusive_scan)
C Interface to scan an array (generalized) over a given dimension.
AFAPI af_err af_product_nan_all(double *real, double *imag, const af_array in, const double nanval)
C Interface to multiply array elements over all dimensions, replacing any NaNs with a specified value...
AFAPI af_err af_count_all_array(af_array *out, const af_array in)
C Interface to count non-zero values over all dimensions.
AFAPI array count(const array &in, const int dim=-1)
C++ Interface to count non-zero values in an array along a given dimension.
AFAPI array setUnion(const array &first, const array &second, const bool is_unique=false)
C++ Interface to evaluate the union of two arrays.
AFAPI array diff1(const array &in, const int dim=0)
C++ Interface to calculate the first order difference in an array over a given dimension.
AFAPI void maxByKey(array &keys_out, array &vals_out, const array &keys, const array &vals, const int dim=-1)
C++ Interface to return the maximum along a given dimension, according to an array of keys...
AFAPI af_err af_all_true(af_array *out, const af_array in, const int dim)
C Interface to check if all values along a given dimension are true.
AFAPI af_err af_max_all(double *real, double *imag, const af_array in)
C Interface to return the maximum over all dimensions.
AFAPI af_err af_any_true_all_array(af_array *out, const af_array in)
C Interface to check if any values over all dimensions are true.
AFAPI array diff2(const array &in, const int dim=0)
C++ Interface to calculate the second order difference in an array over a given dimension.