 # Ublas :

Subject: Re: [ublas] Is it possible to construct alternative "views" of vectors and matrices?
From: Leon Gu (leongu_at_[hidden])
Date: 2009-09-19 15:39:56

You can use array_type together with shallow_array_adaptor. Check this
example,
ublas::matrix<float> mtx(10, 10, 0.);
mtx.data().begin());

refvec is an ublas vector, so you can use it to do whatever you want,
but always use "noalias(refvec) = prod(A,b)" or
"refvec.assign(prod(A,b))" because of temporary memory issue. Check this
post http://lists.boost.org/MailArchives/ublas/2007/06/2162.php for more
details.

Leon

Jesse Manning wrote:
> 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;
> myarray = 0;
> myarray = 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]
> <mailto: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.
>
>
> 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] <mailto:ublas_at_[hidden]>

> Sent to: manning.jesse_at_[hidden] <mailto:manning.jesse_at_[hidden]>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]

> Sent to: leongu_at_[hidden]