RE: [Boost-Users] ublas::matrix sharing storage with ublas::vecto r
Hi Stephen Yes, just use a (1, n) or (n, 1) matrix as appropriate and do matrix operations instead of vector ops. The results will be correct as long as you make sure you have a correctly dimensioned matrix in place of your vector (i.e one that matches your vectors size and orientation). After all a matrix is just a collection of vectors ;-) cheers, Scott On 27/11/2002 05:10, Stephen Crowley wrote
It is possible for a vector and a matrix to share the same storage, as long as the dimensions agree?
Some of the algorithms I am implementing require some objects to be treated as an arbitrarily sized matrix, and sometimes as a vector, and it would be convient to have a matrix and a vector share the same memory.
-- Stephen
The contents of this email may be confidential. Unauthorised use is prohibited. Umgeni Water does not accept liability for any statements and opinions which are the sender's own view and not expressly made on its behalf.
What do you mean by "just use it as" ? For example.. I have a matrix<double> m(5,5); In some code I actually need to use it as a 5x5 matrix.. but other parts must reference it as a vector (to pass to another library). Currently I am doing something like this matrix<double> m(5,5); // do stuff to m vector<double> v(25); v.data() = m.data(); // pass v to library // get results back m.data() = v.data(); This is obviously not optimal.. and if both m.data() and v.data() pointed to the same location in memory the problem would be solved. I suppose I might be able to get around it by making my own storage class that allows you to specify a location and size instead of allocating one itself. --Stephen On Wed, Nov 27, 2002 at 08:31:37AM +0200, Scott Sinclair wrote:
Hi Stephen
Yes, just use a (1, n) or (n, 1) matrix as appropriate and do matrix operations instead of vector ops. The results will be correct as long as you make sure you have a correctly dimensioned matrix in place of your vector (i.e one that matches your vectors size and orientation). After all a matrix is just a collection of vectors ;-)
cheers, Scott
On 27/11/2002 05:10, Stephen Crowley wrote
It is possible for a vector and a matrix to share the same storage, as long as the dimensions agree?
Some of the algorithms I am implementing require some objects to be treated as an arbitrarily sized matrix, and sometimes as a vector, and it would be convient to have a matrix and a vector share the same memory.
-- Stephen
The contents of this email may be confidential. Unauthorised use is prohibited. Umgeni Water does not accept liability for any statements and opinions which are the sender's own view and not expressly made on its behalf.
Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
-- Stephen
Stephen Crowley wrote:
For example.. I have a matrix<double> m(5,5);
In some code I actually need to use it as a 5x5 matrix.. but other parts must reference it as a vector (to pass to another library).
Currently I am doing something like this
matrix<double> m(5,5); // do stuff to m vector<double> v(25); v.data() = m.data(); // pass v to library // get results back m.data() = v.data();
This is obviously not optimal.. and if both m.data() and v.data() pointed to the same location in memory the problem would be solved.
I suppose I might be able to get around it by making my own storage class that allows you to specify a location and size instead of allocating one itself.
There is a storage class `array_adaptor<>' (in storage.hpp, currently
undocumented), which adapts some other array to interface
which ublas classes expect:
==============================================
#include <cstddef>
#include <iostream>
#define BOOST_UBLAS_SIMPLE_ARRAY_ADAPTOR
#include
Great! This is exactly what I was looking for. Is there a list of all these
undocumenated features in boost anywhere besides the source?
Here is a snippet of code that does exactly what I want.
#define BOOST_UBLAS_SIMPLE_ARRAY_ADAPTOR
#include
Stephen Crowley wrote:
For example.. I have a matrix<double> m(5,5);
In some code I actually need to use it as a 5x5 matrix.. but other parts must reference it as a vector (to pass to another library).
Currently I am doing something like this
matrix<double> m(5,5); // do stuff to m vector<double> v(25); v.data() = m.data(); // pass v to library // get results back m.data() = v.data();
This is obviously not optimal.. and if both m.data() and v.data() pointed to the same location in memory the problem would be solved.
I suppose I might be able to get around it by making my own storage class that allows you to specify a location and size instead of allocating one itself.
There is a storage class `array_adaptor<>' (in storage.hpp, currently undocumented), which adapts some other array to interface which ublas classes expect: ============================================== #include <cstddef> #include <iostream>
#define BOOST_UBLAS_SIMPLE_ARRAY_ADAPTOR
#include
#include #include typedef boost::numeric::ublas::array_adaptor<double> storage_t; typedef boost::numeric::ublas::vector
vct_t; typedef boost::numeric::ublas::row_major rm_t; typedef boost::numeric::ublas::matrix matr_t; int main() {
std::size_t n = 5; std::size_t nxn = n * n;
double *a = new double[nxn]; storage_t st (nxn, a);
vct_t v (nxn, st); for (std::size_t i = 0; i < nxn; ++i) v (i) = i + 0.1; std::cout << v << std::endl;
matr_t m (n, n, st); std::cout << m << std::endl;
for (std::size_t i = 0; i < n; ++i) for (std::size_t j = 0; j < n; ++j) m (i, j) = 10 * i + j + 0.1;
std::cout << v << std::endl; std::cout << m << std::endl;
delete[] a; } ============================================== [Note that BOOST_UBLAS_SIMPLE_ARRAY_ADAPTOR must be defined. Otherwise array_adaptor<> uses boost::shared_array<> which doesn't have operator+, which is in turn needed in some functions -- Joerg, are you listening?]
Hope this helps,
fres
Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
-- Stephen
Kresimir Fresl wrote: [snip]
There is a storage class `array_adaptor<>' (in storage.hpp, currently undocumented), which adapts some other array to interface which ublas classes expect: ============================================== #include <cstddef> #include <iostream>
#define BOOST_UBLAS_SIMPLE_ARRAY_ADAPTOR
#include
#include #include typedef boost::numeric::ublas::array_adaptor<double> storage_t; typedef boost::numeric::ublas::vector
vct_t; typedef boost::numeric::ublas::row_major rm_t; typedef boost::numeric::ublas::matrix matr_t; int main() {
std::size_t n = 5; std::size_t nxn = n * n;
double *a = new double[nxn]; storage_t st (nxn, a);
vct_t v (nxn, st); for (std::size_t i = 0; i < nxn; ++i) v (i) = i + 0.1; std::cout << v << std::endl;
matr_t m (n, n, st); std::cout << m << std::endl;
for (std::size_t i = 0; i < n; ++i) for (std::size_t j = 0; j < n; ++j) m (i, j) = 10 * i + j + 0.1;
std::cout << v << std::endl; std::cout << m << std::endl;
delete[] a; } ============================================== [Note that BOOST_UBLAS_SIMPLE_ARRAY_ADAPTOR must be defined. Otherwise array_adaptor<> uses boost::shared_array<> which doesn't have operator+, which is in turn needed in some functions -- Joerg, are you listening?]
Err, what's up? Oh, a new test case (and a new bug ;-( I'll look into it. Thanks Joerg
participants (4)
-
jhr.walter@t-online.de
-
Kresimir Fresl
-
Scott Sinclair
-
Stephen Crowley