Boost logo

Boost Users :

From: Edward Grace (ej.grace_at_[hidden])
Date: 2006-11-07 05:17:05


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net