Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Matrix

 

A matrix is a two-dimensional container (that is a container in which two values are needed to retrieve an element). For one-dimensional containers, go to the container page.

 

There exists no std::matrix (yet).

 

Possibilities are:

  1. std::vector<std::vector<int> >
  2. boost::multi_array
  3. boost::numeric::ublas::matrix
  4. blitz::Array
  5. Techsoft's matrix
  6. Flood::Matrix

 

These possibilities are described below in more detail.

 

 

 

 

 

std::vector<std::vector<int> >

 

A std::vector can contain a collection of std::vectors. If all std::vectors in this collection are of the same size, one has a matrix.

 

When using std::vector<std::vector<int> > for a two-dimensional matrix, the choice between x-y-ordering or y-x-ordering must be made. The run-time speed difference does not reside in individual element read/write, but when obtaining a row or collumn: in a y-x-ordered std::vector<std::vector<int> > an individual row can be obtained, in an x-y-ordered std::vector<std::vector<int> > an individual collumn can be obtained.

 

Below is an example of a y-x-ordered std::vector<std::vector<int> >.

 

#include <cassert>
#include <vector>

int main()
{
  const int n_rows = 5;
  const int n_cols = 10;
  //Create the y-x-ordered 2D-vector
  std::vector<std::vector<int> > v(n_rows, std::vector<int>(n_cols,0));
  const int y = n_rows - 1;
  const int x = n_cols - 1;
  assert(y < static_cast<int>(v.size()));
  assert(x < static_cast<int>(v[y].size()));
  v[y][x] = 10; //y-x-ordered
}

 

 

 

 

 

std::vector<std::vector<int> > code snippets

 

Note that some of these code snippets also work on other containers.

 

  1. Convert Matrix<X> to Matrix<Y>, ConvertMatrix
  2. Convert std::vector<std::vector<X> > to std::vector<std::vector<Y> >, ConvertMatrix
  3. Convert two 2D std::vector<X> to 2D std::vector<Y>, ConvertMatrix
  4. ConvertMatrix, convert Matrix<X> to Matrix<Y>
  5. ConvertMatrix, convert std::vector<std::vector<X> > to std::vector<std::vector<Y> >
  6. ConvertMatrix, convert two 2D std::vector<X> to 2D std::vector<Y>
  7. Get the maximal element of a 2D container, MaxElement
  8. Get the minimum element of a 2D container, MinElement
  9. Get the sizes of the std::vectors in a 2D std::vector, GetSizes
  10. Get the sum of a 2D std::vector, GetSum
  11. GetSizes, get the sizes of the std::vectors in a 2D std::vector
  12. GetSum, get the sum of a 2D std::vector
  13. MaxElement, get the maximal element of a 2D container
  14. MinElement, get the minimum element of a 2D container

 

 

 

 

 

boost::multi_array

 

The boost::multi_array (part of the Boost library) is not only support a two-dimensional matrix, but to many more dimensions.

 

When using C++ Builder 6.0, this does not compile (it results in the compile error borland.hpp: Only member functions may be 'const' or 'volatile').

 

 

 

 

 

boost::numeric::ublas::matrix

 

The boost::numeric::ublas::matrix (part of the Boost library) support a two-dimensional matrix.

 

When using C++ Builder 6.0, this does not compile (it results in the compile error Your compiler and/or configuration is unsupported by this verions of uBLAS).

 

 

 

 

 

blitz::Array

 

The blitz::Array (part of the Blitz++ library) is not only support a two-dimensional matrix, but to many more dimensions.

 

When using C++ Builder 6.0, this does not compile (it results in the compile error bzconfig.h: Unknown compiler).

 

 

 

 

 

Techsoft's matrix

 

Techsoft's matrix supports a x-y-ordered two-dimensional matrix.

 

#include <cassert>
#include <techsoft/matrix.h>

int main()
{
  const int n_rows = 5;
  const int n_cols = 10;
  math::matrix<int> v(n_rows,n_cols);
  const int y = n_rows - 1;
  const int x = n_cols - 1;
  assert(v(x,y)==0);
  v(x,y) = 10;
  assert(v(x,y)==10);
}

 

 

 

 

 

Flood::Matrix

 

The Flood::Matrix (from the Flood library) supports a x-y-ordered two-dimensional matrix.

 

#include <cassert>
#include <flood/utilities/matrix.h>
 
int main()
{
  const int n_rows = 5;
  const int n_cols = 10;
  Flood::Matrix<int> v(n_rows,n_cols);
  const int y = n_rows - 1;
  const int x = n_cols - 1;
  assert( x < v.getNumberOfRows());
  assert( y < v.getNumberOfColumns());
  assert(v[x][y]==0);
  v[x][y] = 10;
  assert(v[x][y]==10);
}

 

 

 

 

 

External links

 

  1. Wikipedia page about matrices
  2. Boost page about boost::multi_array
  3. Blitz++ homepage
  4. Techsoft homepage

 

 

 

 

 

References

 

  1. Wikipedia page about matrices

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict