|
View:
New views
9 Messages
—
Rating Filter:
Alert me
|
|
|
Is it possible to construct alternative "views" of vectors and matrices?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@... http://lists.boost.org/mailman/listinfo.cgi/ublas Sent to: lists@... |
|
|
Re: 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@...> 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. _______________________________________________ ublas mailing list ublas@... http://lists.boost.org/mailman/listinfo.cgi/ublas Sent to: lists@... |
|
|
Re: Is it possible to construct alternative "views" of vectors and matrices?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@... To: ublas@... 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@...> 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. Hotmail: Free, trusted and rich email service. Get it now. _______________________________________________ ublas mailing list ublas@... http://lists.boost.org/mailman/listinfo.cgi/ublas Sent to: lists@... |
|
|
|
|
|
Re: Is it possible to construct alternative "views" of vectors and matrices?the ublas matrix and vector classes both have a constructor that takes the array_data as the third parameter
// pulled from vector.hpp vector (size_type size, const array_type &data) // pulled from matrix.hpp matrix (size_type size1, size_type size2, const array_type &data) these take the data by const reference and then make a copy of it to their internal array_type variable data_ so they don't do exactly what you want theoretically there could be a different constructor that would allow pointing to the same array_type data, but this could be risky if you were to manipulate the sizing of the data by adding or removing elements and this could be why the view adapters that were mentioned by Nasos allowing read only access. On Thu, Sep 17, 2009 at 1:11 PM, Mark Johnson <mj1@...> wrote: Thanks Jesse for your answer and the code fragment! _______________________________________________ ublas mailing list ublas@... http://lists.boost.org/mailman/listinfo.cgi/ublas Sent to: lists@... |
|
|
Re: Is it possible to construct alternative "views" of vectors and matrices?Mark Johnson schrieb:
> Thanks Jesse for your answer and the code fragment! > > Is there a way of building a matrix or vector out of a pre-existing > array_type? I'd like to be able to view a matrix as a vector, and use > vector arithmetic on it. > > I'd be willing to implement such a thing, but I don't know whether the > returned object should be a matrix_expression, a matrix_reference or > whatever, and I don't know what things an object has to define in order > to be a vector or a matrix. If someone can point me to the appropriate > documentation that would be great! > the experimental folder. The matrix_view.hpp file aleady contains a class which presents a pair of raw pointer and size as an array. The class is still under developement, thus any contributions are very welcome. mfg Gunter _______________________________________________ ublas mailing list ublas@... http://lists.boost.org/mailman/listinfo.cgi/ublas Sent to: lists@... |
|
|
Re: Is it possible to construct alternative "views" of vectors and matrices?http://svn.boost.org/svn/boost/trunk/boost/numeric/ublas/experimental/
Hi Gunter, is this the folder you are talking about? I couldn't find the matrix_view.hpp file there. Leon On Thu, Sep 17, 2009 at 5:00 PM, Gunter Winkler <guwi17@...> wrote: Mark Johnson schrieb: _______________________________________________ ublas mailing list ublas@... http://lists.boost.org/mailman/listinfo.cgi/ublas Sent to: lists@... |
|
|
Re: Is it possible to construct alternative "views" of vectors and matrices?You can use array_type together with shallow_array_adaptor. Check this
example, ublas::matrix<float> mtx(10, 10, 0.); ublas::shallow_array_adaptor<float> adpt(mtx.data().size(), mtx.data().begin()); ublas::vector<float, ublas::shallow_array_adaptor<float> > refvec(mtx.data().size(), adpt); \ 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] = 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@... > <mailto:mj1@...>> 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@... <mailto:ublas@...> > http://lists.boost.org/mailman/listinfo.cgi/ublas > Sent to: manning.jesse@... <mailto:manning.jesse@...> > > > ------------------------------------------------------------------------ > > _______________________________________________ > ublas mailing list > ublas@... > http://lists.boost.org/mailman/listinfo.cgi/ublas > Sent to: leongu@... _______________________________________________ ublas mailing list ublas@... http://lists.boost.org/mailman/listinfo.cgi/ublas Sent to: lists@... |
|
|
Re: Is it possible to construct alternative "views" of vectors and matrices?Leon schrieb:
> http://svn.boost.org/svn/boost/trunk/boost/numeric/ublas/experimental/ > > Hi Gunter, is this the folder you are talking about? I couldn't find > the matrix_view.hpp file there. > oh, I gave the wrong name -> I meant "sparse_view.hpp" mfg Gunter _______________________________________________ ublas mailing list ublas@... http://lists.boost.org/mailman/listinfo.cgi/ublas Sent to: lists@... |
| Free embeddable forum powered by Nabble | Forum Help |