boost::numeric::ublas::matrix adaptors for C style arrays
Dear All,
Is it possible to adapt e.g. a pointer to double in to a
ublas::matrix<double>, or construct a matrix from a pointer to double?
I have used the Blitz++ libraries, with those it is possible to use
storage allocated elsewhere.
This is to allow the painless interface of my code with legacy C
code. I would like to do something like the following:
using namespace boost::numeric::ublas;
double *p;
p = new double[100];
.... Fill p[*] with data
// Construct a 10x10 matrix using externally allocated storage.
matrix<double> A(10,10,p);
There does appear to be a hint that I can use my own allocator, but
it is far from clear how I go about doing this. For example coming up
with an Allocator function that doesn't allocate but instead uses
existing memory.
class MyAllocator : public GeneralAllocator {
double *TheMemory;
public:
MyAllocator(double *p) {
TheMemory = p;
}
allocate() {}
};
matrix
On Tue, 7 Nov 2006, Edward Grace wrote:
Dear All,
Is it possible to adapt e.g. a pointer to double in to a ublas::matrix<double>, or construct a matrix from a pointer to double?
I have used the Blitz++ libraries, with those it is possible to use storage allocated elsewhere.
This is to allow the painless interface of my code with legacy C code. I would like to do something like the following:
using namespace boost::numeric::ublas;
double *p; p = new double[100];
.... Fill p[*] with data
// Construct a 10x10 matrix using externally allocated storage. matrix<double> A(10,10,p); [...] Help!! Is there a way to do this?
You could write your own storage class (see the Storage concept in the uBLAS documentation) which is an adaptor for a pointer and you can assign set the pointer via the data() member function of matrix<double>. -- François Duranleau LIGUM, Université de Montréal "There are as many truths as there are people." - from _Neon Genesis Evangelion_
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
You could write your own storage class (see the Storage concept in the uBLAS documentation) which is an adaptor for a pointer and you can assign set the pointer via the data() member function of matrix<double>.
-- François Duranleau LIGUM, Université de Montréal
That looks promising. At least I now know where to start looking. Merci! -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (Darwin) iD8DBQFFUZMn+i4TdNhCMrYRAt8xAKCzb2gwBqWT/DS3rJitzdOX+a2uaACfZSJm dezeFUBn4dsUnumCjuJ15aE= =A21J -----END PGP SIGNATURE-----
Following my original request:
Is it possible to adapt e.g. a pointer to double in to a ublas::matrix<double>, or construct a matrix from a pointer to double?
And the following suggestion,
You could write your own storage class (see the Storage concept in the uBLAS documentation) which is an adaptor for a pointer and you can assign set the pointer via the data() member function of matrix<double>.
-- François Duranleau LIGUM, Université de Montréal
I have found the following seems to work. That said I would like
comments on this solution. Is this the "right" way to achieve what I
want? If not, what is?
If this is the best way, I recommend adding a note to the
ublas:....matrix|vector documentation to this effect, perhaps with
something like this as an example. I imagine that this would be a
common requirement for many people.
Regards,
-ed
/**
*
* Example of constructing a boost matrix and vector object from
* previously allocated heap memory using an array_adaptor.
*
* Comments please on the legitamacy of this and potential catches.
*
* ej.grace@imperial.ac.uk
*/
// Why should I have to define the following in order to use
// shallow_array_adaptor?
#define BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
// Definitions for vector and matrix.
#include
participants (2)
-
Edward Grace
-
François Duranleau