Primitives
map
-
template<typename Accuracy = default_policy, typename F, typename ...Args>
inline map_type<F, Args...> kernel_float::map(F fun, const Args&... args) Apply the function
F
to each element from the vectorinput
and return the results as a new vector.Examples
vec<float, 4> input = {1.0f, 2.0f, 3.0f, 4.0f}; vec<float, 4> squared = map([](auto x) { return x * x; }, input); // [1.0f, 4.0f, 9.0f, 16.0f]
reduce
-
template<typename F, typename V>
inline vector_value_type<V> kernel_float::reduce(F fun, const V &input) Reduce the elements of the given vector
input
into a single value using the functionfun
. This function should be a binary function that takes two elements and returns one element. The order in which the elements are reduced is not specified and depends on both the reduction function and the vector type.Example
vec<int, 3> x = {5, 2, 1}; int y = reduce(x, [](int a, int b) { return a + b; }); // returns 5+2+1=8
zip
-
template<typename F, typename L, typename R>
inline zip_type<F, L, R> kernel_float::zip(F fun, const L &left, const R &right) Combines the elements from the two inputs (
left
andright
) element-wise, applying a provided binary function (fun
) to each pair of corresponding elements.Example
vec<bool, 3> make_negative = {true, false, true}; vec<int, 3> input = {1, 2, 3}; vec<int, 3> output = zip([](bool b, int n){ return b ? -n : +n; }, make_negative, input); // returns [-1, 2, -3]
zip_common
-
template<typename F, typename L, typename R>
inline zip_common_type<F, L, R> kernel_float::zip_common(F fun, const L &left, const R &right) Combines the elements from the two inputs (
left
andright
) element-wise, applying a provided binary function (fun
) to each pair of corresponding elements. The elements are promoted to a common type before applying the binary function.Example
vec<float, 3> a = {1.0f, 2.0f, 3.0f}; vec<int, 3> b = {4, 5, 6}; vec<float, 3> c = zip_common([](float x, float y){ return x + y; }, a, b); // returns [5.0f, 7.0f, 9.0f]
make_vec
-
template<typename ...Args>
inline vec<promote_t<Args...>, sizeof...(Args)> kernel_float::make_vec(Args&&... args) Create a vector from a variable number of input values.
The resulting vector type is determined by promoting the types of the input values into a common type. The number of input values determines the dimension of the resulting vector.
Example
auto v1 = make_vec(1.0f, 2.0f, 3.0f); // Creates a vec<float, 3> [1.0f, 2.0f, 3.0f] auto v2 = make_vec(1, 2, 3, 4); // Creates a vec<int, 4> [1, 2, 3, 4]
into_vec
-
template<typename V>
inline into_vector_type<V> kernel_float::into_vec(V &&input) Convert the given
input
into a vector. This function can perform one of the following actions:For vectors
vec<T, N>
, it simply returns the original vector.For primitive types
T
(e.g.,int
,float
,double
), it returns avec<T, 1>
.For array-like types (e.g.,
std::array<T, N>
,T[N]
), it returnsvec<T, N>
.For vector-like types (e.g.,
int2
,dim3
), it returnsvec<T, N>
.
concat
-
template<typename ...Vs>
inline concat_type<Vs...> kernel_float::concat(const Vs&... inputs) Concatenates the provided input values into a single one-dimensional vector.
This function works in three steps:
All input values are converted into vectors using the
into_vector
operation.The resulting vectors’ elements are then promoted into a shared value type.
The resultant vectors are finally concatenated together.
For instance, when invoking this function with arguments of types
float, double2, double
:After the first step:
vec<float, 1>, vec<double, 2>, vec<double, 1>
After the second step:
vec<double, 1>, vec<double, 2>, vec<double, 1>
After the third step:
vec<double, 4>
Example
double vec1 = 1.0; double3 vec2 = {2.0, 3.0, 4.0); double4 vec3 = {5.0, 6.0, 7.0, 8.0}; vec<double, 8> concatenated = concat(vec1, vec2, vec3); // contains [1, 2, 3, 4, 5, 6, 7, 8] int num1 = 42; float num2 = 3.14159; int2 num3 = {-10, 10}; vec<float, 4> concatenated = concat(num1, num2, num3); // contains [42, 3.14159, -10, 10]
select
-
template<typename V, typename ...Is>
inline select_type<V, Is...> kernel_float::select(const V &input, const Is&... indices) Selects elements from the this vector based on the specified indices.
Example
vec<float, 6> input = {0, 10, 20, 30, 40, 50}; vec<float, 4> vec1 = select(input, 0, 4, 4, 2); // [0, 40, 40, 20] vec<int, 4> indices = {0, 4, 4, 2}; vec<float, 4> vec2 = select(input, indices); // [0, 40, 40, 20]