Logo MTL4

Vector Reductions

The sum and the product of all vector's elements can be computed:

// File: vector_reduction.cpp

#include <iostream>
#include <boost/numeric/mtl/mtl.hpp>

int main(int, char**)
{
    using namespace mtl;

    typedef std::complex<double>  cdouble;
    dense_vector<cdouble>         v(100);

    for (unsigned i= 0; i < size(v); i++)
        v[i]= cdouble(i+1, 100-i);

    std::cout << "sum(v) is " << sum(v)<< "\n";
    
    std::cout << "product(v) is " <<  product(v)<< "\n";
    
    std::cout << "sum<6>(v) is " <<  sum<6>(v)<< "\n";

    return 0;
}

As vector reductions base on the same implementation as norms, the unrolling can be explicitly controlled as shown in the last command. The results of these reductions are the value type of the vector.

// File: vector_min_max.cpp

#include <iostream>
#include <cmath>
#include <boost/numeric/mtl/mtl.hpp>

int main(int, char**)
{
    using mtl::max;

    mtl::dense_vector<double>         v(100);

    for (unsigned i= 0; i < size(v); i++)
        v[i]= double(i+1) * pow(-1.0, int(i)); // Amb. in MSVC

    std::cout << "max(v) is " << max(v)<< "\n";
    
    std::cout << "min(v) is " <<  min(v)<< "\n";
    
    std::cout << "max<6>(v) is " <<  max<6>(v)<< "\n";

    return 0;
}

The dot product of two vectors is computed with the function dot:

// File: dot.cpp

#include <iostream>
#include <boost/numeric/mtl/mtl.hpp>

int main(int, char**)
{
    using namespace mtl;

    typedef std::complex<double>  cdouble;
    dense_vector<cdouble>         v(10000), x(10, cdouble(3, 2));
    dense_vector<double>          w(10000);

    for (unsigned i= 0; i < size(v); i++)
        v[i]= cdouble(i+1, 10000-i), w[i]= 2 * i + 2;

    std::cout << "dot(v, w) is " << dot(v, w)<< "\n";
    
    std::cout << "dot<6>(v, w) is " <<  dot<6>(v, w)<< "\n";
    
    std::cout << "conj(x) is " <<  conj(x)<< "\n";

    return 0;
}

As the previous computation the evaluation is unrolled, either with a user-defined parameter or by default eight times.

The result type of dot is of type of the values' product. If MTL4 is compiled with a concept-compiler, the result type is taken from the concept std::Multiple and without concepts Joel de Guzman's result type deduction from Boost is used.

Return to Matrix Norms                                Table of Content                                Proceed to Conjugates


Vector Reductions -- MTL 4 -- Peter Gottschling and Andrew Lumsdaine -- Gen. with rev. 7542 on 7 Apr 2011 by doxygen 1.5.9 -- © 2010 by SimuNova UG.