A high-performance general-purpose compute library

Finds the product of an input array according to an array of keys. More...

Functions

AFAPI void productByKey (array &keys_out, array &vals_out, const array &keys, const array &vals, const int dim=-1)
 C++ Interface for product of elements in an array according to a key.
 
AFAPI void productByKey (array &keys_out, array &vals_out, const array &keys, const array &vals, const int dim, const double nanval)
 C++ Interface for product of elements in an array according to a key while replacing nan values.
 
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 for product of elements in an array according to key.
 
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 for product of elements in an array according to key while replacing nans.
 

Detailed Description

Finds the product of an input array according to an array of keys.

The values corresponding to each group of consecutive equal keys will be multiplied together. Keys can repeat, however only consecutive key values will be considered for each reduction. If a key value is repeated somewhere else in the keys array it will be considered the start of a new reduction. There are two outputs: the reduced set of consecutive keys and the corresponding final reduced values. An example demonstrating the reduction behavior can be seen in the following snippet.

array keys(9, hkeys); // keys = [ 0 0 1 1 1 0 0 2 2 ]
array vals(9, hvals); // vals = [ 1 2 3 4 5 6 7 8 9 ];
array okeys, ovals;
productByKey(okeys, ovals, keys, vals);
// okeys = [ 0 1 0 2 ]
// ovals = [ 2 60 42 72 ]

The keys input type must be an integer type(s32 or u32). This table defines the return types for the corresponding values type

Input Type Output Type
f32, f64, c32, c64 same as input
s32, u32, s64, u64 same as input
s16 s32
u16, u8, b8 u32
f16 f32

The input keys must be a 1-D vector matching the size of the reduced dimension. In the case of multiple dimensions in the input values array, the dim parameter specifies which dimension to reduce along. An example of multi-dimensional reduce by key can be seen below:

array keys(5, hkeys);
array vals(2, 5, hvals);
// keys = [ 1 0 0 2 2 ]
// vals = [[ 1 2 3 4 5 ]
// [ 6 7 8 9 10 ]]
const int reduce_dim = 1;
array okeys, ovals;
productByKey(okeys, ovals, keys, vals, reduce_dim);
// okeys = [ 1 0 2 ]
// ovals = [[ 1 6 20 ],
// [ 6 56 90 ]]

Function Documentation

◆ af_product_by_key()

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 for product of elements in an array according to key.

Parameters
[out]keys_outwill contain the reduced keys in vals along dim
[out]vals_outwill contain the product of all values in vals along dim according to keys
[in]keysis the key array
[in]valsis the array containing the values to be reduced
[in]dimThe dimension along which the product operation occurs
Returns
AF_SUCCESS if the execution completes properly

◆ af_product_by_key_nan()

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 for product of elements in an array according to key while replacing nans.

Parameters
[out]keys_outwill contain the reduced keys in vals along dim
[out]vals_outwill contain the product of all values in vals along dim according to keys
[in]keysis the key array
[in]valsis the array containing the values to be reduced
[in]dimThe dimension along which the product operation occurs
[in]nanvalThe value that will replace the NaNs in vals
Returns
AF_SUCCESS if the execution completes properly

◆ productByKey() [1/2]

AFAPI void productByKey ( array keys_out,
array vals_out,
const array keys,
const array vals,
const int  dim,
const double  nanval 
)

C++ Interface for product of elements in an array according to a key while replacing nan values.

Parameters
[out]keys_outwill contain the reduced keys in vals along dim
[out]vals_outwill contain the product of all values in vals along dim according to keys
[in]keysis the key array
[in]valsis the array containing the values to be reduced
[in]dimThe dimension along which the product operation occurs
[in]nanvalThe value that will replace the NaNs in vals

◆ productByKey() [2/2]

AFAPI void productByKey ( array keys_out,
array vals_out,
const array keys,
const array vals,
const int  dim = -1 
)

C++ Interface for product of elements in an array according to a key.

Parameters
[out]keys_outwill contain the reduced keys in vals along dim
[out]vals_outwill contain the product of all values in vals along dim according to keys
[in]keysThe key array
[in]valsThe array containing the values to be reduced
[in]dimThe dimension along which the product operation occurs
Note
dim is -1 by default. -1 denotes the first non-singleton dimension.