◆ transform_reduce() [1/2]

template<typename T , typename Unop , typename Binop >
T boost::simd::transform_reduce ( T const *  first,
T const *  last,
Unop  unop,
Binop  binop 

Applies unary_op to each element in the range [first; last) and reduces the results (possibly permuted and aggregated in unspecified manner) along with the initial value init over binary_op.

firstBeginning of the range of elements to transform
lastEnd of the range of elements to transform
unopunary operation function object that will be applied.
initthe initial value of the reduction
binopbinary operation function object that will be applied.
  • first, last must be pointer to Vectorizable type.
  • unop and binop must be a polymorphic unary function object, i.e callable on generic types, at least T and boost::simd::pack<T>
  • returns the generalized sum of init and unary_op(*first), unary_op(*(first+1)), . unary_op(*(last-1)) over binary_op,

where generalized sum GSUM(op, a1, ., aN) is defined as follows:

if N=1, a1 if N > 1, op(GSUM(op, b1, ., bK), GSUM(op, bM, ., bN)) where

  • b1, ., bN may be any permutation of a1, ., aN and
  • 1 < K+1 = M <= N

in other words, the results of unary_op may be grouped and arranged in arbitrary order. a pointer to the element past the last element transformed.

  • unop is not applied to init
  • If the range is empty, init is returned, unmodified

The following code uses simd::transform to compute sum of the square of the values stored in a std::vector.

#include <boost/simd/algorithm/transform_reduce.hpp>
#include <boost/simd/function/sqr.hpp>
#include <iostream>
#include <vector>
int main()
namespace bs = boost::simd;
std::vector<float> d{1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f} ;
auto s1 = bs::transform_reduce( d.data(), d.data()+10, bs::sqr, 0.0f, bs::plus);
std::cout << "s1 = " << s1 << std::endl;
return 0;
possible output:
s1 = 385