IT++ Logo
Users Guide

Table of Contents

Introduction

The itbase library is the core of IT++ and it contains classes and functions for mathematics with scalars, vectors, and matrices. This document does not cover all the aspects of the itbase library. It does however explain the most important things you need to know in order to start using IT++. Once you are more familiar with the itbase library you will find the online reference manual more useful.

Predefined Data Types

Predefined Scalar Types

Apart from the standard C++ types e.g. char, short, int, long, double, float, and long long, the following types are specific for IT++:

Prepared Vector Types

A vector can in principle be of arbitrary type (that support addition, subtraction, multiplication and division), since the general vector class Vec<TYPE> is templated. However, the most commonly used vector types are predefined. These predefined vector types are:

The general vector class is used to define the specialized classes above. The vec class is actually a Vec<double>. We urge you to use these predefined classes instead of Vec<TYPE> when ever possible.

Prepared Matrix Types

The general matrix class is called Mat<TYPE>. These predefined matrix types are:

As with vector, the general matrix class is used to define the specialized classes above. The mat class is thus a Mat<double>. We urge you to use these predefined classes instead of Mat<TYPE> whenever possible.

Using Vectors

Vectors and matrices in IT++ are very similar. We therefore begin to describe the vector class in detail and then briefly explain the differences regarding matrices in the next section.

Defining a Vector

A vector containing elements of type double is defined with:

vec my_vector;

However, this will not assign a size (memory) to the vector. To assign size 10 to the vector we may use:

vec my_vector(10);

or

vec my_vector;
my_vector.set_size(10,false);

where the second parameter in the set_size call (true or false) determines if you want to copy the contents of the old data area into the new resized one, or not. This may be useful when down-sizing a vector, but in this case it is not. It is also equivalent to use

my_vector.set_length(10,false);

instead of set_size.

Observe that a declared vector (or matrix) is not cleared (the element values are undefined). To clear a vector we simply write

my_vector.clear();

or

my_vector.zeros();

To fill the vector with ones we write

my_vector.ones();

It is possible to retrieve the length (size) of a vector in any of the following ways:

length_of_vector = my_vector.length();
length_of_vector = my_vector.size();
length_of_vector = length(my_vector);

To assign values to a vector

vec  a = "0 0.7 5 9.3";        // that is a = [0 0.7 5 9.3]
ivec b = "0:5";                // that is b = [0 1 2 3 4 5]
vec  c = "3:2.5:13";           // that is c = [3 5.5 8 10.5 13]
ivec d = "1:3:5,0:2:4";        // that is d = [1 3 5 0 2 4]
vec e("1.2,3.4,5.6");          // that is e = [1.2 3.4 5.6]
vec f;
f.set("1.0 2.0 3.0 4.0");      // that is f = [1.0 2.0 3.0 4.0]
vec g;
g = f;                         // that is g is a copy of f

A comma or a space character separates the vector elements. When assigning or retrieving a specific vector element use

a(i) = 3.14;
double p = a(i);

for element number i. Vector elements are numbered such that a(0) denotes the first element. It is also possible to use square brackets as in the C language, i.e.

a[i] = 3.14;
double p = a[i];

Parts or a vector are retrieved by

a.left(3);   // a vector containing the first 3 elements of a
a.right(2);  // a vector containing the last 2 elements of a
a.mid(1,2);  // a vector containing the 2 elements starting with a(1)
a(2,4);      // a vector containing all elements from a(2) to a(4)
a(2,-1);     // a vector containing all elements from a(2) to the end of a

Alternatively you can use get() methods instead of () or [] operators, e.g.

a.get(4);
a.get(5,-1);

If you have a vector called index_list containing indexes (ivec) you may write

// these give a vector containing elements with indexes in index_list
a(index_list);
a.get(index_list);

If you have a bvec called e.g. bin_list you may write

// these give a vector containing all elements a(i) for which bin_list(i) equals 1
a(bin_list);
a.get(bin_list);

Have a look at the following example:

#include <itpp/itbase.h>
using namespace itpp;
using namespace std;

int main() {
  vec a = linspace(0,1,11);
  ivec index_list = "3 5 2 2";
  bvec bin_list = "1 0 1 0 1 0 1 0 1 0 1";

  cout << "a = " << a << endl;
  cout << "a(index_list) = " << a(index_list) << endl;
  cout << "a.get(bin_list) = " << a.get(bin_list) << endl;
}

When you run this program you will see

a = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]
a(index_list) = [0.3 0.5 0.2 0.2]
a.get(bin_list) = [0.0 0.2 0.4 0.6 0.8 1.0]

Vector Manipulation

Below follows a listing of the most common vector manipulation commands that are available. All examples are given for an ivec denoted my_ivec, but of course this will work for other vector types as well.

Vector Converters

In order to convert e.g an ivec to a vec we can write some thing like my_vec = to_vec(my_ivec). The following converters are available:

Vector Functions

There are several functions that operate on vectors. Some examples are: max, max_index, min, min_index, product, energy, geometric_mean, mean, median, norm, round, variance, ceil_i, floor_i, round_i, find.

Examples of functions that generate different kinds of vectors are: linspace, ones_b, ones_c, ones_i, ones zeros_b. There are several more than these. Please refer to the IT++ reference manual for a description of these.

Using Matrices

Matrices are two-dimensional arrays, and most of their functionality is similar to that of vectors. The predefined matrix types are:

Below follows some examples that are specific for matrices only:

Matrix Converters

The following converters are available:

The Array Class

The itbase library contains, among other things, the Array class. An Array can contain any type of data. Below is an example of an Array containing vectors (vec):

#include <itpp/itbase.h>

using namespace itpp;
using namespace std;

int main() {
  Array<vec> my_vec_array(2);
  my_vec_array(0) = linspace(0,1,4);
  my_vec_array(1) = "0.1 0.2 0.3 0.4 0.3 0.2 0.1";
  cout << "my_vec_array = " << my_vec_array << endl;

  return 0;
}

Random Vectors, Matrices, and Generators

Random Vectors and Matrices

Random vectors and matrices are easily obtained by using these predefined functions:

Random Number Generators (RNG)

The following discrete valued random number generators are available. More information about these can be found in the IT++ reference manual.

The following continuous valued random number generators are available.

Deterministic Sources

The following deterministic sources are available:

Filter Classes and Functions

The following filter classes are available:

The following filter functions are available:

Signal Processing Functions

The following signal processing functions are available:

Timer Classes

The Real_Timer class can be used to measure execution time of a program as in the following example:

#include <itpp/itbase.h>

using namespace itpp;
using namespace std;

int main() {
  long sum = 0;
  Real_Timer my_timer;

  my_timer.tic();
  for (int i=0; i<10000000; i++) {
    sum += i;
  }
  my_timer.toc_print();

  cout << "The sum is " << sum << endl;

  return 0;
}

Reading and Writing to Files

The following example saves the variable a to the file my_file_name.it:

#include <itpp/itbase.h>

using namespace itpp;

int main() {
  it_file my_file("my_file_name.it");
  vec a = "1.0 2.0 3.0 4.0";
  my_file << Name("a") << a;

  return 0;
}

The following example reads the variable a from the file my_file_name.it and prints it:

#include <itpp/itbase.h>

using namespace itpp;
using namespace std;

int main() {
  it_file my_file("my_file_name.it");
  vec a;
  my_file >> Name("a") >> a;
  cout << "a = " << a << endl;

  return 0;
}

Note that *.it files can be read and written in Matlab/Octave by using the itload.m and itsave.m functions.

Also available is the class it_ifile that can only be used for reading of files.

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SourceForge Logo

Generated on Sat Mar 17 2012 00:01:07 for IT++ by Doxygen 1.7.4