Boost logo

Ublas :

Subject: Re: [ublas] How to do a generic projection on a matrix?
From: Ralf Denzer (Ralf.Denzer_at_[hidden])
Date: 2009-01-26 09:52:50


Dear Niko,

there exists an undocumented feature in ublas.
It's called indirect_array. See storage.hpp for more details.

Enclose, an example how to use it.
(I copy and paste most lines from my example code, should work, but not tested).

//------------------------------------------------------------------
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.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;

    //hint: use ublas::indirect_array<>::all() to access all indices
    // use ia 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;
}
//----------------------------------------------------

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)

Ok, use a second indirect_array<> ib(m) and
project(M, ia, ib) to access different rows and columns.

Hope this helps

Ciao

Ralf

__________________________________________________________________
Deutschlands größte Online-Videothek schenkt Ihnen 12.000 Videos!*
http://entertainment.web.de/de/entertainment/maxdome/index.html