Logo MTL4

Vector Types

To start the tutorial we want to give a very short example (we could call it the MTL4-hello-world).

// File: vector1.cpp

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

int main(int argc, char* argv[])
{
    using namespace mtl;

    // Define dense vector of doubles with 10 elements all set to 0.0.
    dense_vector<double>   v(10, 0.0);

    // Set element 7 to 3.0.
    v[7]= 3.0;

    std::cout << "v is " << v << "\n";
    return 0;
}

The Boost library is used and must also be downloaded. See the installation guide for more details. To compile a MTL4 program you only need to include the MTL and the boost path. A compile command could read:
g++ -I/u/peter/mtl4 -I/u/peter/boost -O2 vector1.cpp -o vector1
As most modern C++ software MTL4 uses intensively function inlining. As a consequence, the performance is rather poor if compiled without optimization. But don't worry: despite the aggressive source code transformation at compile time, the compilation rarely took more than a minute, in most cases only a few seconds.

The short program certainly does not need much explanation only some brief comments. The vector in the program above is a column vector. The constructor in the example takes two arguments: the size and the initial value.

Indices always start with zero. Earlier efforts to support one-based indices were abandoned because code became rather complicated when mixed indexing for different arguments of a function. We decided that the additional development effort and the potential performance penalty are not acceptable. Extra functionality will be provided in the future if necessary for interoperability with Fortran libraries.

The following program defines a row vector of 7 elements without (explicit) initialization.

// File: vector2.cpp

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

int main(int argc, char* argv[])
{
    using namespace mtl;

    // Define dense vector of complex with 7 elements.
    dense_vector<std::complex<float>, vector::parameters<tag::row_major> >  v(7);

    // Set all elements to 3+2i
    v= std::complex<float>(3.0, 2.0);
    std::cout << "v is " << v << "\n";

    // Set all elements to 5+0i
    v= 5.0;
    std::cout << "v is " << v << "\n";

    v= 6;
    std::cout << "v is " << v << "\n";

    return 0;
}

Scalar values can be assigned to vectors if the type of the scalar value is assignable to the type of the elements. Scalar types are in MTL4 all types that are not explicitly defined by type traits as vectors or matrices, thus almost all types.

                               Table of Content                                Proceed to Matrix Types


Vector Types -- MTL 4 -- Peter Gottschling and Andrew Lumsdaine -- Generated on 24 Aug 2009 by Doxygen 1.5.9 -- Copyright 2008-09 by TU Dresden and the Trustees of Indiana University.