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<double,MyAllocator> A; Failing that, I'd like an adaptor that allows me to effectively do something like: matrix<double> A; double *p; // Allocate p and fill with stuff! A = adaptor<matrix<double> >(p,nCol,nRows); A is now a 10x10 matrix<double> using the memory pointed to by p. Obviously there are issues regarding who is now responsible for this memory, something handled by the memory policy in Blitz++ (neverDelete, etc). Help!! Is there a way to do this? -ed

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 <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/matrix.hpp> #include <iostream> int main() { using namespace boost::numeric::ublas; unsigned int N=100,M=200; double *data_vector, *data_matrix; data_vector = new double[N]; data_matrix = new double[N*M]; // Set Mat(0,1) to -Pi. data_matrix[1] = -3.14159; // Crate a boost vector object using the data_vector as storage. vector<double,shallow_array_adaptor<double> > Vec (N,shallow_array_adaptor<double>(N,data_vector) ); // Create a boost matrix object using the data_matrix pointer as storage. matrix<double,row_major, shallow_array_adaptor<double> > Mat (N,M,shallow_array_adaptor<double>(N*M,data_matrix) ); Mat(3,3) = 3.0; // Should print -3.14159, 3 std::cout << Mat(0,1) << "," << Mat(3,3) << std::endl; return 0; };
participants (2)
-
Edward Grace
-
François Duranleau