|
Ublas : |
Subject: Re: [ublas] beginners question
From: Ralf Denzer (Ralf.Denzer_at_[hidden])
Date: 2010-01-06 06:17:22
Hi
> > OK, I'm kidding but the real question is similar: does the new traits
> > system allow to manipulate, it's my Holy Grail, matrices like I do in
> > matlab ? Being able to address a matrix with vectors, matrices, the
> > famous ':' operator, etc...
> > Any pointer to a documentation would be most appreciated.
Maybe, ublas::indirect_array<> is what you like to do?
It works like ublas::range but you use an 'index vector' (indirect_array)
to extract arbitrary submatrices or subvectors.
Unfortunately, this feature is not documented, but works well
to my experience. You should check the source code
in the include files:
#include <boost/numeric/ublas/storage.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
Check this example:
// ----- snip -----
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
namespace ublas = boost::numeric::ublas;
int main()
{
const unsigned N = 7;
ublas::matrix<int> M(N,N);
ublas::vector<int> V(N);
// initialize M and V
std::cout << "M=" << std::endl;
for (unsigned i = 0; i < M.size1(); ++i)
{
V(i) = 111*(i+1);
for (unsigned j = 0; j < M.size2(); ++j)
{
M(i,j) = 10*i+j;
std::cout.width(2);
std::cout.fill('0');
std::cout << M(i,j) << " ";
}
std::cout << std::endl;
}
std::cout << "V=\n" << V << std::endl;
//************************************************
// define an indirect_array
const unsigned n = 3;
ublas::indirect_array<> ia(n);
ia[0] = 1;
ia[1] = 2;
ia[2] = 4;
//*************************************************
// ia(n) to extract some indices
ublas::matrix_indirect<ublas::matrix<int> > Mindirect(M, ia,ia);
std::cout << "Mindirect=\n" << Mindirect << std::endl;
// or use ublas::project
std::cout << "Mprojected=\n" << ublas::project(M, ia, ia) << std::endl;
// similar for vectors
ublas::vector_indirect<ublas::vector<int> > Vindirect(V,ia);
std::cout << "Vindirect=\n" << Vindirect << std::endl;
// and
std::cout << "Vprojected=\n" << ublas::project(V, ia) << std::endl;
}
// ---- snip -----
Output:
M=
00 01 02 03 04 05 06
10 11 12 13 14 15 16
20 21 22 23 24 25 26
30 31 32 33 34 35 36
40 41 42 43 44 45 46
50 51 52 53 54 55 56
60 61 62 63 64 65 66
V=
[7](111,222,333,444,555,666,777)
Mindirect=
[3,3]((11,12,14),(21,22,24),(41,42,44))
Mprojected=
[3,3]((11,12,14),(21,22,24),(41,42,44))
Vindirect=
[3](222,333,555)
Vprojected=
[3](222,333,555)
With ublas::indirect_array<> you can simulate Matlab possibilities to extract
submatrices and subvectors quite well.
Ciao
Ralf
___________________________________________________________
Preisknaller: WEB.DE DSL Flatrate für nur 16,99 Euro/mtl.!
http://produkte.web.de/go/02/