Boost logo

Ublas :

Subject: Re: [ublas] Is it possible to construct alternative "views" of vectors and matrices?
From: Nasos Iliopoulos (nasos_i_at_[hidden])
Date: 2009-09-17 10:41:32


Or you could do something like:
ublas::matrix<double> m = ublas::zero_matrix<double>(4,5);

m.begin2()[10] = 1.0; //m(2,0)==1.0

Also, take a look at the views concept recently introduced. It looks to me that the developers are thinking the views should not change the underlying object, the way they want to split to view and mutable traits. I don't have a strong opinion on that myself, although considering how GIL's view works (http://www.boost.org/doc/libs/1_40_0/libs/gil/doc/index.html), it might not be bad idea that the views could change the underlying data. Of course a lot if things must come into consideration if this happens.
https://svn.boost.org/trac/boost/ticket/3396

My understanding for the various types is the following:
X_expression: Most of the classes derive from an expression. So a matrix<double> is an expression, as well as A+B is an expression. When the later is called it creates an expression, that is evaluated into a container when it is assigned to one, i.e. C=A+B; The various types of functional expressions are differentiated through a traits mechanism. (i.e. matrix_matrix_binary_traits vs vector_binary_traits)

X_reference : This is used to define the closure_type (references to variables in the expressions.). For example an expression A+B keeps closures (references) to A and B. Those references are used when A+B needs to be evaluated. Historically I think the name closure was adapted (vs reference) because expression templates are functional in principle.

X_container: is the base for all statically derived classes (matrices and vectors), i.e. those that have self access to data. for example matrix<double> is a container. Note that X_container classes also derived from X_expression.

Hope that helps
Best
Nasos

Date: Thu, 17 Sep 2009 09:29:55 -0400
From: manning.jesse_at_[hidden]
To: ublas_at_[hidden]
Subject: Re: [ublas] Is it possible to construct alternative "views" of vectors and matrices?

For dense matrix types the underlying storage of the matrix is just an array of m*n values. You can access this data directly from the matrix and it will provide the "vector view" so to speak. I am not sure about other types of matrices such as sparse or compressed since I have mostly dealt with dense. Here is some example code:

    ublas::matrix<int> mat1(3,3);

    // fill matrix with values
    mat1(0,0) = 1;
    mat1(0,1) = 3;
    mat1(0,2) = 2;
    mat1(1,0) = 1;
    mat1(1,1) = 0;
    mat1(1,2) = 0;
    mat1(2,0) = 1;

    mat1(2,1) = 2;
    mat1(2,2) = 2;

    // get vector view of matrix elements
    ublas::matrix<int>::array_type& myarray = mat1.data();

    // print contents
    std::cout << "myarray size: " << myarray.size() << std::endl;

    std::cout << "[";
    for (size_t i=0; i < myarray.size(); ++i)
    {
        std::cout << myarray[i];

        if (i != myarray.size()-1)
        {
            std::cout << ",";

        }
    }
    std::cout << "]" << std::endl;

    // modify some contents of the matrix using the vector view
    myarray[0] = 0;
    myarray[1] = 0;
    myarray[2] = 0;

    // print contents to show changes
    std::cout << "myarray size: " << myarray.size() << std::endl;
    std::cout << "[";
    for (size_t i=0; i < myarray.size(); ++i)

    {
        std::cout << myarray[i];

        if (i != myarray.size()-1)
        {
            std::cout << ",";
        }
    }
    std::cout << "]" << std::endl;

On Thu, Sep 17, 2009 at 8:40 AM, Mark Johnson <mj1_at_[hidden]> wrote:

I'm using uBLAS to implement an HMM package, and for that work it would be handy to be able to view the same data both as n by m matrix, and also as a vector of size n*m. Views should permit element modification, so they are really just alternative ways of indexing the same underlying elements.

Perhaps uBLAS already has this capability? In this case I'd be pleased if someone could point me to it.

uBLAS already has vector and matrix ranges and slices, which provide views to a subset of elements, and functions like row() and column() which provide a vector view of some of the elements of a matrix, but I think it would be very useful to have a general facility for doing this.

I think the existing uBLAS slices provide a general enough method of selecting and projecting a subset of elements. Views would provide a way of restructuring such collections of elements.

One way to do this might be to add public constructors to matrix_reference{} and vector_reference{}, so the following code would be acceptable. (Maybe the appropriate class would be matrix_container{} or matrix_expression{}. It's not clear to me what the relationship between all these classes is; I'd appreciate an explanation).

ublas::matrix<double> m = ublas::zero_matrix<double>(4,5);

auto v = ublas::vector_reference(m);

v(10) = 1.0; // now m(2,0) == 1.0

auto m1 = ublas::matrix_reference(m, 5, 4);

m1(1,1) = 2.0; // now m(1,0) == 2.0

A disadvantage of my proposal is that every matrix expression would need to "know" whether it is row-major or column-major. For my work now I don't really care if we just assume that all matrix_expressions are (say) row-major.

We could get the same functionality in other ways, of course. For example, a lower-level way of doing this would be for every matrix and vector expression to provide a view of its data as a random-access sequence, and then provide constructors that can take such sequences and produce matrix or vector proxies from them.

Any suggestions or comments?

Mark

PS. Another feature I'd like is the ability to construct my own "computed on the fly" matrices like zero_matrix and identity_matrix. In other words, if f is a function object of two integer parameters it would be handy to be able to write:

ublas::matrix<double> m = ublas::computed_matrix<double>(f, 4, 5);

and then m(i,j) == f(i,j).

_______________________________________________

ublas mailing list

ublas_at_[hidden]

http://lists.boost.org/mailman/listinfo.cgi/ublas

Sent to: manning.jesse_at_[hidden]

_________________________________________________________________
Hotmail: Free, trusted and rich email service.
http://clk.atdmt.com/GBL/go/171222984/direct/01/