Boost logo

Ublas :

Subject: [ublas] Is it possible to construct alternative "views" of vectors and matrices?
From: Mark Johnson (mj1_at_[hidden])
Date: 2009-09-17 08:40:22

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

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?


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).