@@ -48,43 +48,116 @@ struct reduce_helper<F, vector_compound<T, N>> {
4848};
4949} // namespace detail
5050
51+ /* *
52+ * Reduce the elements of the given vector ``input`` into a single value using
53+ * the function ``fun``. This function should be a binary function that takes
54+ * two elements and returns one element. The order in which the elements
55+ * are reduced is not specified and depends on the reduction function and
56+ * the vector type.
57+ *
58+ * Example
59+ * =======
60+ * ```
61+ * vec<int, 3> x = {5, 2, 1};
62+ * int y = reduce(x, [](int a, int b) { return a + b; }); // returns 8
63+ * ```
64+ */
5165template <typename F, typename V>
5266KERNEL_FLOAT_INLINE vector_value_type<V> reduce (F fun, V&& input) {
5367 return detail::reduce_helper<F, into_vector_type<V>>::call (
5468 fun,
5569 into_vector (std::forward<V>(input)));
5670}
5771
72+ /* *
73+ * Find the minimum element in the given vector ``input``.
74+ *
75+ * Example
76+ * =======
77+ * ```
78+ * vec<int, 3> x = {5, 0, 2, 1, 0};
79+ * int y = sum(x); // Returns 8
80+ * ```
81+ */
5882template <typename V, typename T = vector_value_type<V>>
5983KERNEL_FLOAT_INLINE T min (V&& input) {
6084 return reduce (ops::min<T> {}, std::forward<V>(input));
6185}
6286
87+ /* *
88+ * Find the maximum element in the given vector ``input``.
89+ *
90+ * Example
91+ * =======
92+ * ```
93+ * vec<int, 3> x = {5, 0, 2, 1, 0};
94+ * int y = sum(x); // Returns 8
95+ * ```
96+ */
6397template <typename V, typename T = vector_value_type<V>>
6498KERNEL_FLOAT_INLINE T max (V&& input) {
6599 return reduce (ops::max<T> {}, std::forward<V>(input));
66100}
67101
102+ /* *
103+ * Sum the items in the given vector ``input``.
104+ *
105+ * Example
106+ * =======
107+ * ```
108+ * vec<int, 3> x = {5, 0, 2, 1, 0};
109+ * int y = sum(x); // Returns 8
110+ * ```
111+ */
68112template <typename V, typename T = vector_value_type<V>>
69113KERNEL_FLOAT_INLINE T sum (V&& input) {
70114 return reduce (ops::add<T> {}, std::forward<V>(input));
71115}
72116
117+ /* *
118+ * Multiply the items in the given vector ``input``.
119+ *
120+ * Example
121+ * =======
122+ * ```
123+ * vec<int, 5> x = {5, 0, 2, 1, 0};
124+ * int y = sum(x); // Returns 5+0+2+1+0 = 8
125+ * ```
126+ */
73127template <typename V, typename T = vector_value_type<V>>
74128KERNEL_FLOAT_INLINE T product (V&& input) {
75129 return reduce (ops::multiply<T> {}, std::forward<V>(input));
76130}
77131
132+ /* *
133+ * Check if all elements in the given vector ``input`` are non-zero. An element ``v`` is considered
134+ * non-zero if ``bool(v)`` returns ``true``.
135+ */
78136template <typename V>
79137KERNEL_FLOAT_INLINE bool all (V&& input) {
80138 return reduce (ops::bit_and<bool > {}, cast<bool >(input));
81139}
82140
141+ /* *
142+ * Check if any element in the given vector ``input`` is non-zero. An element ``v`` is considered
143+ * non-zero if ``bool(v)`` returns ``true``.
144+ */
83145template <typename V>
84146KERNEL_FLOAT_INLINE bool any (V&& input) {
85147 return reduce (ops::bit_or<bool > {}, cast<bool >(input));
86148}
87149
150+ /* *
151+ * Count the number of non-zero items in the given vector ``input``. An element ``v`` is considered
152+ * non-zero if ``bool(v)`` returns true.
153+ *
154+ * Example
155+ * =======
156+ * ```
157+ * vec<int, 3> x = {5, 0, 2, 1, 0};
158+ * int y = count(x); // Returns 3
159+ * ```
160+ */
88161template <typename V>
89162KERNEL_FLOAT_INLINE int count (V&& input) {
90163 return sum (cast<int >(cast<bool >(input)));
0 commit comments