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, char**)
{
    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, char**)
{
    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.

All vectors have free functions for the number of rows and columns and the size.

To find out the number of rows use

  unsigned r= num_rows(v);

It returns an unsigned integer (more precisely the size_type of the vector type). The result is the number of elements for a column vector and 1 for a row vector.

Likewise the number of columns is given

  unsigned c= num_cols(v);

The result is the number of elements for a row vector and 1 for a column vector. The size is given by the function size

  unsigned s= size(v);
  assert (s == r * c);

and is the number of elements. This is equal to the product of the numbers of rows and columns. These definitions are consistent with the according functions for matrices (Matrix Types).

                               Table of Content                                Proceed to Matrix Types


Vector Types -- 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.