Logo MTL4
Vector Norms

Principal MTL4 functions are all defined in namespace mtl. Helper functions are defined in sub-namespaces to avoid namespace pollution.

The following program shows how to compute norms:

// File: vector_norm.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);

    // Initialize vector
    for (unsigned i= 0; i < size(v); i++)
        v[i]= cdouble(i+1, 10000-i);

    std::cout << "one_norm(v) is " << one_norm(v)<< "\n";
    
    std::cout << "two_norm(v) is " << two_norm(v)<< "\n";
    
    std::cout << "infinity_norm(v) is " << infinity_norm(v)<< "\n";
    
    // Unroll computation of two-norm to 6 independent statements
    std::cout << "two_norm<6>(v) is " << two_norm<6>(v)<< "\n";

    return 0;
}

Since this code is almost self-explanatory, we give only a few comments here. The definitions of the one_norm(A), two_norm, and infinity_norm(A) can be found in their respective documentations. Vector norms are for performance reasons computed with unrolled loops. Since we do not want to rely on the compilers' capability and in order to have more control over the optimization, the unrolling is realized by meta-programming. Specializations for certain compilers might be added later if there is a considerable performance gain over the meta-programming solution.

Loops in reduction operations, like norms, are by default unrolled to 8 statements. The optimal unrolling depends on several factors, in particular the number of registers and the value type of the vector. The last statement shows how to unroll the computation to six statements. The maximum for unrolling is 8 (it might be increased later).

The norms return the magnitude type of the vectors' value type, see Magnitude.

Return to Matrix-Vector Expressions                                Table of Content                                Proceed to Matrix Norms


Vector Norms -- MTL 4 -- Peter Gottschling and Andrew Lumsdaine -- Gen. with rev. 7542 on Sat Aug 11 2012 by doxygen 1.7.6.1 -- © 2010 by SimuNova UG.